blob: aadbd3016bfaa4b0c1dfaf10d46ca643cf670ae0 [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());
Akira Hatanakabe7daa32018-03-12 17:05:06 +0000143 if (!RD)
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000144 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
Yaron Kerencdae9412016-01-29 19:38:18 +0000204LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000205 raw_ostream &OS = llvm::errs();
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000206 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000207 switch (TheKind) {
208 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000209 OS << "Direct Type=";
Chris Lattner2192fe52011-07-18 04:24:23 +0000210 if (llvm::Type *Ty = getCoerceToType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000211 Ty->print(OS);
212 else
213 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000214 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000215 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000216 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000217 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000218 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000219 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000220 break;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000221 case InAlloca:
222 OS << "InAlloca Offset=" << getInAllocaFieldIndex();
223 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000224 case Indirect:
John McCall7f416cc2015-09-08 08:05:57 +0000225 OS << "Indirect Align=" << getIndirectAlign().getQuantity()
Joerg Sonnenberger4921fe22011-07-15 18:23:44 +0000226 << " ByVal=" << getIndirectByVal()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000227 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000228 break;
229 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000230 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000231 break;
John McCallf26e73d2016-03-11 04:30:43 +0000232 case CoerceAndExpand:
233 OS << "CoerceAndExpand Type=";
234 getCoerceAndExpandType()->print(OS);
235 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000236 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000237 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000238}
239
Petar Jovanovic402257b2015-12-04 00:26:47 +0000240// Dynamically round a pointer up to a multiple of the given alignment.
241static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
242 llvm::Value *Ptr,
243 CharUnits Align) {
244 llvm::Value *PtrAsInt = Ptr;
245 // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
246 PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
247 PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
248 llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
249 PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
250 llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
251 PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
252 Ptr->getType(),
253 Ptr->getName() + ".aligned");
254 return PtrAsInt;
255}
256
John McCall7f416cc2015-09-08 08:05:57 +0000257/// Emit va_arg for a platform using the common void* representation,
258/// where arguments are simply emitted in an array of slots on the stack.
259///
260/// This version implements the core direct-value passing rules.
261///
262/// \param SlotSize - The size and alignment of a stack slot.
263/// Each argument will be allocated to a multiple of this number of
264/// slots, and all the slots will be aligned to this value.
265/// \param AllowHigherAlign - The slot alignment is not a cap;
266/// an argument type with an alignment greater than the slot size
267/// will be emitted on a higher-alignment address, potentially
268/// leaving one or more empty slots behind as padding. If this
269/// is false, the returned address might be less-aligned than
270/// DirectAlign.
271static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
272 Address VAListAddr,
273 llvm::Type *DirectTy,
274 CharUnits DirectSize,
275 CharUnits DirectAlign,
276 CharUnits SlotSize,
277 bool AllowHigherAlign) {
278 // Cast the element type to i8* if necessary. Some platforms define
279 // va_list as a struct containing an i8* instead of just an i8*.
280 if (VAListAddr.getElementType() != CGF.Int8PtrTy)
281 VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
282
283 llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
284
285 // If the CC aligns values higher than the slot size, do so if needed.
286 Address Addr = Address::invalid();
287 if (AllowHigherAlign && DirectAlign > SlotSize) {
Petar Jovanovic402257b2015-12-04 00:26:47 +0000288 Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
289 DirectAlign);
John McCall7f416cc2015-09-08 08:05:57 +0000290 } else {
Petar Jovanovic402257b2015-12-04 00:26:47 +0000291 Addr = Address(Ptr, SlotSize);
John McCall7f416cc2015-09-08 08:05:57 +0000292 }
293
294 // Advance the pointer past the argument, then store that back.
Rui Ueyama83aa9792016-01-14 21:00:27 +0000295 CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
John McCall7f416cc2015-09-08 08:05:57 +0000296 llvm::Value *NextPtr =
297 CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize,
298 "argp.next");
299 CGF.Builder.CreateStore(NextPtr, VAListAddr);
300
301 // If the argument is smaller than a slot, and this is a big-endian
302 // target, the argument will be right-adjusted in its slot.
Strahinja Petrovic515a1eb2016-06-24 12:12:41 +0000303 if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
304 !DirectTy->isStructTy()) {
John McCall7f416cc2015-09-08 08:05:57 +0000305 Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
306 }
307
308 Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
309 return Addr;
310}
311
312/// Emit va_arg for a platform using the common void* representation,
313/// where arguments are simply emitted in an array of slots on the stack.
314///
315/// \param IsIndirect - Values of this type are passed indirectly.
316/// \param ValueInfo - The size and alignment of this type, generally
317/// computed with getContext().getTypeInfoInChars(ValueTy).
318/// \param SlotSizeAndAlign - The size and alignment of a stack slot.
319/// Each argument will be allocated to a multiple of this number of
320/// slots, and all the slots will be aligned to this value.
321/// \param AllowHigherAlign - The slot alignment is not a cap;
322/// an argument type with an alignment greater than the slot size
323/// will be emitted on a higher-alignment address, potentially
324/// leaving one or more empty slots behind as padding.
325static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
326 QualType ValueTy, bool IsIndirect,
327 std::pair<CharUnits, CharUnits> ValueInfo,
328 CharUnits SlotSizeAndAlign,
329 bool AllowHigherAlign) {
330 // The size and alignment of the value that was passed directly.
331 CharUnits DirectSize, DirectAlign;
332 if (IsIndirect) {
333 DirectSize = CGF.getPointerSize();
334 DirectAlign = CGF.getPointerAlign();
335 } else {
336 DirectSize = ValueInfo.first;
337 DirectAlign = ValueInfo.second;
338 }
339
340 // Cast the address we've calculated to the right type.
341 llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
342 if (IsIndirect)
343 DirectTy = DirectTy->getPointerTo(0);
344
345 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
346 DirectSize, DirectAlign,
347 SlotSizeAndAlign,
348 AllowHigherAlign);
349
350 if (IsIndirect) {
351 Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
352 }
353
354 return Addr;
355
356}
357
358static Address emitMergePHI(CodeGenFunction &CGF,
359 Address Addr1, llvm::BasicBlock *Block1,
360 Address Addr2, llvm::BasicBlock *Block2,
361 const llvm::Twine &Name = "") {
362 assert(Addr1.getType() == Addr2.getType());
363 llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
364 PHI->addIncoming(Addr1.getPointer(), Block1);
365 PHI->addIncoming(Addr2.getPointer(), Block2);
366 CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
367 return Address(PHI, Align);
368}
369
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000370TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
371
John McCall3480ef22011-08-30 01:42:09 +0000372// If someone can figure out a general rule for this, that would be great.
373// It's probably just doomed to be platform-dependent, though.
374unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
375 // Verified for:
376 // x86-64 FreeBSD, Linux, Darwin
377 // x86-32 FreeBSD, Linux, Darwin
378 // PowerPC Linux, Darwin
379 // ARM Darwin (*not* EABI)
Tim Northover9bb857a2013-01-31 12:13:10 +0000380 // AArch64 Linux
John McCall3480ef22011-08-30 01:42:09 +0000381 return 32;
382}
383
John McCalla729c622012-02-17 03:33:10 +0000384bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
385 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-09-21 08:08:30 +0000386 // The following conventions are known to require this to be false:
387 // x86_stdcall
388 // MIPS
389 // For everything else, we just prefer false unless we opt out.
390 return false;
391}
392
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000393void
394TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
395 llvm::SmallString<24> &Opt) const {
396 // This assumes the user is passing a library name like "rt" instead of a
397 // filename like "librt.a/so", and that they don't care whether it's static or
398 // dynamic.
399 Opt = "-l";
400 Opt += Lib;
401}
402
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000403unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
Pekka Jaaskelainenfc2629a2017-06-01 07:18:49 +0000404 // OpenCL kernels are called via an explicit runtime API with arguments
405 // set with clSetKernelArg(), not as normal sub-functions.
406 // Return SPIR_KERNEL by default as the kernel calling convention to
407 // ensure the fingerprint is fixed such way that each OpenCL argument
408 // gets one matching argument in the produced kernel function argument
409 // list to enable feasible implementation of clSetKernelArg() with
410 // aggregates etc. In case we would use the default C calling conv here,
411 // clSetKernelArg() might break depending on the target-specific
412 // conventions; different targets might split structs passed as values
413 // to multiple function arguments etc.
414 return llvm::CallingConv::SPIR_KERNEL;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000415}
Yaxun Liu37ceede2016-07-20 19:21:11 +0000416
Yaxun Liu402804b2016-12-15 08:09:08 +0000417llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
418 llvm::PointerType *T, QualType QT) const {
419 return llvm::ConstantPointerNull::get(T);
420}
421
Alexander Richardson6d989432017-10-15 18:48:14 +0000422LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
423 const VarDecl *D) const {
Yaxun Liucbf647c2017-07-08 13:24:52 +0000424 assert(!CGM.getLangOpts().OpenCL &&
425 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
426 "Address space agnostic languages only");
Alexander Richardson6d989432017-10-15 18:48:14 +0000427 return D ? D->getType().getAddressSpace() : LangAS::Default;
Yaxun Liucbf647c2017-07-08 13:24:52 +0000428}
429
Yaxun Liu402804b2016-12-15 08:09:08 +0000430llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
Alexander Richardson6d989432017-10-15 18:48:14 +0000431 CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr,
432 LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const {
Yaxun Liu402804b2016-12-15 08:09:08 +0000433 // Since target may map different address spaces in AST to the same address
434 // space, an address space conversion may end up as a bitcast.
Yaxun Liucbf647c2017-07-08 13:24:52 +0000435 if (auto *C = dyn_cast<llvm::Constant>(Src))
436 return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000437 return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy);
Yaxun Liu402804b2016-12-15 08:09:08 +0000438}
439
Yaxun Liucbf647c2017-07-08 13:24:52 +0000440llvm::Constant *
441TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
Alexander Richardson6d989432017-10-15 18:48:14 +0000442 LangAS SrcAddr, LangAS DestAddr,
Yaxun Liucbf647c2017-07-08 13:24:52 +0000443 llvm::Type *DestTy) const {
444 // Since target may map different address spaces in AST to the same address
445 // space, an address space conversion may end up as a bitcast.
446 return llvm::ConstantExpr::getPointerCast(Src, DestTy);
447}
448
Yaxun Liu39195062017-08-04 18:16:31 +0000449llvm::SyncScope::ID
450TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const {
451 return C.getOrInsertSyncScopeID(""); /* default sync scope */
452}
453
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000454static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000455
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000456/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000457/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000458static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
459 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000460 if (FD->isUnnamedBitfield())
461 return true;
462
463 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000464
Eli Friedman0b3f2012011-11-18 03:47:20 +0000465 // Constant arrays of empty records count as empty, strip them off.
466 // Constant arrays of zero length always count as empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000467 if (AllowArrays)
Eli Friedman0b3f2012011-11-18 03:47:20 +0000468 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
469 if (AT->getSize() == 0)
470 return true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000471 FT = AT->getElementType();
Eli Friedman0b3f2012011-11-18 03:47:20 +0000472 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000473
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000474 const RecordType *RT = FT->getAs<RecordType>();
475 if (!RT)
476 return false;
477
478 // C++ record fields are never empty, at least in the Itanium ABI.
479 //
480 // FIXME: We should use a predicate for whether this behavior is true in the
481 // current ABI.
482 if (isa<CXXRecordDecl>(RT->getDecl()))
483 return false;
484
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000485 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000486}
487
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000488/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000489/// fields. Note that a structure with a flexible array member is not
490/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000491static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000492 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000493 if (!RT)
Denis Zobnin380b2242016-02-11 11:26:03 +0000494 return false;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000495 const RecordDecl *RD = RT->getDecl();
496 if (RD->hasFlexibleArrayMember())
497 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000498
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000499 // If this is a C++ record, check the bases first.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000500 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +0000501 for (const auto &I : CXXRD->bases())
502 if (!isEmptyRecord(Context, I.getType(), true))
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000503 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000504
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000505 for (const auto *I : RD->fields())
506 if (!isEmptyField(Context, I, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000507 return false;
508 return true;
509}
510
511/// isSingleElementStruct - Determine if a structure is a "single
512/// element struct", i.e. it has exactly one non-empty field or
513/// exactly one field which is itself a single element
514/// struct. Structures with flexible array members are never
515/// considered single element structs.
516///
517/// \return The field declaration for the single non-empty field, if
518/// it exists.
519static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Benjamin Kramer83b1bf32015-03-02 16:09:24 +0000520 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000521 if (!RT)
Craig Topper8a13c412014-05-21 05:09:00 +0000522 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000523
524 const RecordDecl *RD = RT->getDecl();
525 if (RD->hasFlexibleArrayMember())
Craig Topper8a13c412014-05-21 05:09:00 +0000526 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000527
Craig Topper8a13c412014-05-21 05:09:00 +0000528 const Type *Found = nullptr;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000529
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000530 // If this is a C++ record, check the bases first.
531 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000532 for (const auto &I : CXXRD->bases()) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000533 // Ignore empty records.
Aaron Ballman574705e2014-03-13 15:41:46 +0000534 if (isEmptyRecord(Context, I.getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000535 continue;
536
537 // If we already found an element then this isn't a single-element struct.
538 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000539 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000540
541 // If this is non-empty and not a single element struct, the composite
542 // cannot be a single element struct.
Aaron Ballman574705e2014-03-13 15:41:46 +0000543 Found = isSingleElementStruct(I.getType(), Context);
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000544 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000545 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000546 }
547 }
548
549 // Check for single element.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000550 for (const auto *FD : RD->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000551 QualType FT = FD->getType();
552
553 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000554 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000555 continue;
556
557 // If we already found an element then this isn't a single-element
558 // struct.
559 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000560 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000561
562 // Treat single element arrays as the element.
563 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
564 if (AT->getSize().getZExtValue() != 1)
565 break;
566 FT = AT->getElementType();
567 }
568
John McCalla1dee5302010-08-22 10:59:02 +0000569 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000570 Found = FT.getTypePtr();
571 } else {
572 Found = isSingleElementStruct(FT, Context);
573 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000574 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000575 }
576 }
577
Eli Friedmanee945342011-11-18 01:25:50 +0000578 // We don't consider a struct a single-element struct if it has
579 // padding beyond the element type.
580 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
Craig Topper8a13c412014-05-21 05:09:00 +0000581 return nullptr;
Eli Friedmanee945342011-11-18 01:25:50 +0000582
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000583 return Found;
584}
585
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000586namespace {
James Y Knight29b5f082016-02-24 02:59:33 +0000587Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
588 const ABIArgInfo &AI) {
589 // This default implementation defers to the llvm backend's va_arg
590 // instruction. It can handle only passing arguments directly
591 // (typically only handled in the backend for primitive types), or
592 // aggregates passed indirectly by pointer (NOTE: if the "byval"
593 // flag has ABI impact in the callee, this implementation cannot
594 // work.)
595
596 // Only a few cases are covered here at the moment -- those needed
597 // by the default abi.
598 llvm::Value *Val;
599
600 if (AI.isIndirect()) {
601 assert(!AI.getPaddingType() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000602 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000603 assert(
604 !AI.getIndirectRealign() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000605 "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000606
607 auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
608 CharUnits TyAlignForABI = TyInfo.second;
609
610 llvm::Type *BaseTy =
611 llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
612 llvm::Value *Addr =
613 CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
614 return Address(Addr, TyAlignForABI);
615 } else {
616 assert((AI.isDirect() || AI.isExtend()) &&
617 "Unexpected ArgInfo Kind in generic VAArg emitter!");
618
619 assert(!AI.getInReg() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000620 "Unexpected InReg seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000621 assert(!AI.getPaddingType() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000622 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000623 assert(!AI.getDirectOffset() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000624 "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000625 assert(!AI.getCoerceToType() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000626 "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000627
628 Address Temp = CGF.CreateMemTemp(Ty, "varet");
629 Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
630 CGF.Builder.CreateStore(Val, Temp);
631 return Temp;
632 }
633}
634
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000635/// DefaultABIInfo - The default implementation for ABI specific
636/// details. This implementation provides information which results in
637/// self-consistent and sensible LLVM IR generation, but does not
638/// conform to any particular ABI.
639class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000640public:
641 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000642
Chris Lattner458b2aa2010-07-29 02:16:43 +0000643 ABIArgInfo classifyReturnType(QualType RetTy) const;
644 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000645
Craig Topper4f12f102014-03-12 06:41:41 +0000646 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000647 if (!getCXXABI().classifyReturnType(FI))
648 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +0000649 for (auto &I : FI.arguments())
650 I.info = classifyArgumentType(I.type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000651 }
652
John McCall7f416cc2015-09-08 08:05:57 +0000653 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
James Y Knight29b5f082016-02-24 02:59:33 +0000654 QualType Ty) const override {
655 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
656 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000657};
658
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000659class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
660public:
Chris Lattner2b037972010-07-29 02:01:43 +0000661 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
662 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000663};
664
Chris Lattner458b2aa2010-07-29 02:16:43 +0000665ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerac385062015-05-18 22:46:30 +0000666 Ty = useFirstFieldIfTransparentUnion(Ty);
667
668 if (isAggregateTypeForABI(Ty)) {
669 // Records with non-trivial destructors/copy-constructors should not be
670 // passed by value.
671 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000672 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Reid Klecknerac385062015-05-18 22:46:30 +0000673
John McCall7f416cc2015-09-08 08:05:57 +0000674 return getNaturalAlignIndirect(Ty);
Reid Klecknerac385062015-05-18 22:46:30 +0000675 }
Daniel Dunbar557893d2010-04-21 19:10:51 +0000676
Chris Lattner9723d6c2010-03-11 18:19:55 +0000677 // Treat an enum type as its underlying type.
678 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
679 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000680
Alex Bradburye41a5e22018-01-12 20:08:16 +0000681 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
682 : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000683}
684
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000685ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
686 if (RetTy->isVoidType())
687 return ABIArgInfo::getIgnore();
688
689 if (isAggregateTypeForABI(RetTy))
John McCall7f416cc2015-09-08 08:05:57 +0000690 return getNaturalAlignIndirect(RetTy);
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000691
692 // Treat an enum type as its underlying type.
693 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
694 RetTy = EnumTy->getDecl()->getIntegerType();
695
Alex Bradburye41a5e22018-01-12 20:08:16 +0000696 return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
697 : ABIArgInfo::getDirect());
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000698}
699
Derek Schuff09338a22012-09-06 17:37:28 +0000700//===----------------------------------------------------------------------===//
Dan Gohmanc2853072015-09-03 22:51:53 +0000701// WebAssembly ABI Implementation
702//
703// This is a very simple ABI that relies a lot on DefaultABIInfo.
704//===----------------------------------------------------------------------===//
705
706class WebAssemblyABIInfo final : public DefaultABIInfo {
707public:
708 explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
709 : DefaultABIInfo(CGT) {}
710
711private:
712 ABIArgInfo classifyReturnType(QualType RetTy) const;
713 ABIArgInfo classifyArgumentType(QualType Ty) const;
714
715 // DefaultABIInfo's classifyReturnType and classifyArgumentType are
Richard Smith81ef0e12016-05-14 01:21:40 +0000716 // non-virtual, but computeInfo and EmitVAArg are virtual, so we
James Y Knight29b5f082016-02-24 02:59:33 +0000717 // overload them.
Dan Gohmanc2853072015-09-03 22:51:53 +0000718 void computeInfo(CGFunctionInfo &FI) const override {
719 if (!getCXXABI().classifyReturnType(FI))
720 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
721 for (auto &Arg : FI.arguments())
722 Arg.info = classifyArgumentType(Arg.type);
723 }
Dan Gohman1fcd10c2016-02-22 19:17:40 +0000724
725 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
726 QualType Ty) const override;
Dan Gohmanc2853072015-09-03 22:51:53 +0000727};
728
729class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
730public:
731 explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
732 : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
733};
734
735/// \brief Classify argument of given type \p Ty.
736ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
737 Ty = useFirstFieldIfTransparentUnion(Ty);
738
739 if (isAggregateTypeForABI(Ty)) {
740 // Records with non-trivial destructors/copy-constructors should not be
741 // passed by value.
Dan Gohmanc2853072015-09-03 22:51:53 +0000742 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000743 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Dan Gohmanc2853072015-09-03 22:51:53 +0000744 // Ignore empty structs/unions.
745 if (isEmptyRecord(getContext(), Ty, true))
746 return ABIArgInfo::getIgnore();
747 // Lower single-element structs to just pass a regular value. TODO: We
748 // could do reasonable-size multiple-element structs too, using getExpand(),
749 // though watch out for things like bitfields.
750 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
751 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
Dan Gohmanc2853072015-09-03 22:51:53 +0000752 }
753
754 // Otherwise just do the default thing.
755 return DefaultABIInfo::classifyArgumentType(Ty);
756}
757
758ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
759 if (isAggregateTypeForABI(RetTy)) {
760 // Records with non-trivial destructors/copy-constructors should not be
761 // returned by value.
762 if (!getRecordArgABI(RetTy, getCXXABI())) {
763 // Ignore empty structs/unions.
764 if (isEmptyRecord(getContext(), RetTy, true))
765 return ABIArgInfo::getIgnore();
766 // Lower single-element structs to just return a regular value. TODO: We
767 // could do reasonable-size multiple-element structs too, using
768 // ABIArgInfo::getDirect().
769 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
770 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
771 }
772 }
773
774 // Otherwise just do the default thing.
775 return DefaultABIInfo::classifyReturnType(RetTy);
776}
777
Dan Gohman1fcd10c2016-02-22 19:17:40 +0000778Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
779 QualType Ty) const {
780 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false,
781 getContext().getTypeInfoInChars(Ty),
782 CharUnits::fromQuantity(4),
783 /*AllowHigherAlign=*/ true);
784}
785
Dan Gohmanc2853072015-09-03 22:51:53 +0000786//===----------------------------------------------------------------------===//
Derek Schuff09338a22012-09-06 17:37:28 +0000787// le32/PNaCl bitcode ABI Implementation
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000788//
789// This is a simplified version of the x86_32 ABI. Arguments and return values
790// are always passed on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000791//===----------------------------------------------------------------------===//
792
793class PNaClABIInfo : public ABIInfo {
794 public:
795 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
796
797 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000798 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff09338a22012-09-06 17:37:28 +0000799
Craig Topper4f12f102014-03-12 06:41:41 +0000800 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +0000801 Address EmitVAArg(CodeGenFunction &CGF,
802 Address VAListAddr, QualType Ty) const override;
Derek Schuff09338a22012-09-06 17:37:28 +0000803};
804
805class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
806 public:
807 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
808 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
809};
810
811void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000812 if (!getCXXABI().classifyReturnType(FI))
Derek Schuff09338a22012-09-06 17:37:28 +0000813 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
814
Reid Kleckner40ca9132014-05-13 22:05:45 +0000815 for (auto &I : FI.arguments())
816 I.info = classifyArgumentType(I.type);
817}
Derek Schuff09338a22012-09-06 17:37:28 +0000818
John McCall7f416cc2015-09-08 08:05:57 +0000819Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
820 QualType Ty) const {
James Y Knight29b5f082016-02-24 02:59:33 +0000821 // The PNaCL ABI is a bit odd, in that varargs don't use normal
822 // function classification. Structs get passed directly for varargs
823 // functions, through a rewriting transform in
824 // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
825 // this target to actually support a va_arg instructions with an
826 // aggregate type, unlike other targets.
827 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000828}
829
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000830/// \brief Classify argument of given type \p Ty.
831ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff09338a22012-09-06 17:37:28 +0000832 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +0000833 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000834 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
835 return getNaturalAlignIndirect(Ty);
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000836 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
837 // Treat an enum type as its underlying type.
Derek Schuff09338a22012-09-06 17:37:28 +0000838 Ty = EnumTy->getDecl()->getIntegerType();
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000839 } else if (Ty->isFloatingType()) {
840 // Floating-point types don't go inreg.
841 return ABIArgInfo::getDirect();
Derek Schuff09338a22012-09-06 17:37:28 +0000842 }
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000843
Alex Bradburye41a5e22018-01-12 20:08:16 +0000844 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
845 : ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000846}
847
848ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
849 if (RetTy->isVoidType())
850 return ABIArgInfo::getIgnore();
851
Eli Benderskye20dad62013-04-04 22:49:35 +0000852 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000853 if (isAggregateTypeForABI(RetTy))
John McCall7f416cc2015-09-08 08:05:57 +0000854 return getNaturalAlignIndirect(RetTy);
Derek Schuff09338a22012-09-06 17:37:28 +0000855
856 // Treat an enum type as its underlying type.
857 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
858 RetTy = EnumTy->getDecl()->getIntegerType();
859
Alex Bradburye41a5e22018-01-12 20:08:16 +0000860 return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
861 : ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000862}
863
Chad Rosier651c1832013-03-25 21:00:27 +0000864/// IsX86_MMXType - Return true if this is an MMX type.
865bool IsX86_MMXType(llvm::Type *IRType) {
866 // 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 +0000867 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
868 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
869 IRType->getScalarSizeInBits() != 64;
870}
871
Jay Foad7c57be32011-07-11 09:56:20 +0000872static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000873 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000874 llvm::Type* Ty) {
Coby Tayree7b49dc92017-08-24 09:07:34 +0000875 bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
876 .Cases("y", "&y", "^Ym", true)
877 .Default(false);
878 if (IsMMXCons && Ty->isVectorTy()) {
Tim Northover0ae93912013-06-07 00:04:50 +0000879 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
880 // Invalid MMX constraint
Craig Topper8a13c412014-05-21 05:09:00 +0000881 return nullptr;
Tim Northover0ae93912013-06-07 00:04:50 +0000882 }
883
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000884 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover0ae93912013-06-07 00:04:50 +0000885 }
886
887 // No operation needed
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000888 return Ty;
889}
890
Reid Kleckner80944df2014-10-31 22:00:51 +0000891/// Returns true if this type can be passed in SSE registers with the
892/// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
893static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
894 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Erich Keanede1b2a92017-07-21 18:50:36 +0000895 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
896 if (BT->getKind() == BuiltinType::LongDouble) {
897 if (&Context.getTargetInfo().getLongDoubleFormat() ==
898 &llvm::APFloat::x87DoubleExtended())
899 return false;
900 }
Reid Kleckner80944df2014-10-31 22:00:51 +0000901 return true;
Erich Keanede1b2a92017-07-21 18:50:36 +0000902 }
Reid Kleckner80944df2014-10-31 22:00:51 +0000903 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
904 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
905 // registers specially.
906 unsigned VecSize = Context.getTypeSize(VT);
907 if (VecSize == 128 || VecSize == 256 || VecSize == 512)
908 return true;
909 }
910 return false;
911}
912
913/// Returns true if this aggregate is small enough to be passed in SSE registers
914/// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
915static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
916 return NumMembers <= 4;
917}
918
Erich Keane521ed962017-01-05 00:20:51 +0000919/// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
920static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
921 auto AI = ABIArgInfo::getDirect(T);
922 AI.setInReg(true);
923 AI.setCanBeFlattened(false);
924 return AI;
925}
926
Chris Lattner0cf24192010-06-28 20:05:43 +0000927//===----------------------------------------------------------------------===//
928// X86-32 ABI Implementation
929//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000930
Reid Kleckner661f35b2014-01-18 01:12:41 +0000931/// \brief Similar to llvm::CCState, but for Clang.
932struct CCState {
Reid Kleckner80944df2014-10-31 22:00:51 +0000933 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
Reid Kleckner661f35b2014-01-18 01:12:41 +0000934
935 unsigned CC;
936 unsigned FreeRegs;
Reid Kleckner80944df2014-10-31 22:00:51 +0000937 unsigned FreeSSERegs;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000938};
939
Erich Keane521ed962017-01-05 00:20:51 +0000940enum {
941 // Vectorcall only allows the first 6 parameters to be passed in registers.
942 VectorcallMaxParamNumAsReg = 6
943};
944
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000945/// X86_32ABIInfo - The X86-32 ABI information.
John McCall12f23522016-04-04 18:33:08 +0000946class X86_32ABIInfo : public SwiftABIInfo {
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000947 enum Class {
948 Integer,
949 Float
950 };
951
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000952 static const unsigned MinABIStackAlignInBytes = 4;
953
David Chisnallde3a0692009-08-17 23:08:21 +0000954 bool IsDarwinVectorABI;
Michael Kupersteindc745202015-10-19 07:52:25 +0000955 bool IsRetSmallStructInRegABI;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000956 bool IsWin32StructABI;
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +0000957 bool IsSoftFloatABI;
Michael Kuperstein68901882015-10-25 08:18:20 +0000958 bool IsMCUABI;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000959 unsigned DefaultNumRegisterParameters;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000960
961 static bool isRegisterSize(unsigned Size) {
962 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
963 }
964
Reid Kleckner80944df2014-10-31 22:00:51 +0000965 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
966 // FIXME: Assumes vectorcall is in use.
967 return isX86VectorTypeForVectorCall(getContext(), Ty);
968 }
969
970 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
971 uint64_t NumMembers) const override {
972 // FIXME: Assumes vectorcall is in use.
973 return isX86VectorCallAggregateSmallEnough(NumMembers);
974 }
975
Reid Kleckner40ca9132014-05-13 22:05:45 +0000976 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000977
Daniel Dunbar557893d2010-04-21 19:10:51 +0000978 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
979 /// such that the argument will be passed in memory.
Reid Kleckner661f35b2014-01-18 01:12:41 +0000980 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
981
John McCall7f416cc2015-09-08 08:05:57 +0000982 ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000983
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000984 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000985 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000986
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000987 Class classify(QualType Ty) const;
Reid Kleckner40ca9132014-05-13 22:05:45 +0000988 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000989 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
Erich Keane4bd39302017-06-21 16:37:22 +0000990
Michael Kupersteinf3163dc2015-12-28 14:39:54 +0000991 /// \brief Updates the number of available free registers, returns
992 /// true if any registers were allocated.
993 bool updateFreeRegs(QualType Ty, CCState &State) const;
994
995 bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
996 bool &NeedsPadding) const;
997 bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000998
Reid Kleckner04046052016-05-02 17:41:07 +0000999 bool canExpandIndirectArgument(QualType Ty) const;
1000
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001001 /// \brief Rewrite the function info so that all memory arguments use
1002 /// inalloca.
1003 void rewriteWithInAlloca(CGFunctionInfo &FI) const;
1004
1005 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001006 CharUnits &StackOffset, ABIArgInfo &Info,
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001007 QualType Type) const;
Erich Keane521ed962017-01-05 00:20:51 +00001008 void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
1009 bool &UsedInAlloca) const;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001010
Rafael Espindola75419dc2012-07-23 23:30:29 +00001011public:
1012
Craig Topper4f12f102014-03-12 06:41:41 +00001013 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00001014 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1015 QualType Ty) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001016
Michael Kupersteindc745202015-10-19 07:52:25 +00001017 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1018 bool RetSmallStructInRegABI, bool Win32StructABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001019 unsigned NumRegisterParameters, bool SoftFloatABI)
John McCall12f23522016-04-04 18:33:08 +00001020 : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
Michael Kupersteindc745202015-10-19 07:52:25 +00001021 IsRetSmallStructInRegABI(RetSmallStructInRegABI),
1022 IsWin32StructABI(Win32StructABI),
Manuel Klimekab2e28e2015-10-19 08:43:46 +00001023 IsSoftFloatABI(SoftFloatABI),
Michael Kupersteind749f232015-10-27 07:46:22 +00001024 IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
Manuel Klimekab2e28e2015-10-19 08:43:46 +00001025 DefaultNumRegisterParameters(NumRegisterParameters) {}
John McCall12f23522016-04-04 18:33:08 +00001026
John McCall56331e22018-01-07 06:28:49 +00001027 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
John McCall12f23522016-04-04 18:33:08 +00001028 bool asReturnValue) const override {
1029 // LLVM's x86-32 lowering currently only assigns up to three
1030 // integer registers and three fp registers. Oddly, it'll use up to
1031 // four vector registers for vectors, but those can overlap with the
1032 // scalar registers.
1033 return occupiesMoreThan(CGT, scalars, /*total*/ 3);
1034 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00001035
1036 bool isSwiftErrorInRegister() const override {
1037 // x86-32 lowering does not support passing swifterror in a register.
1038 return false;
1039 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001040};
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001041
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001042class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
1043public:
Michael Kupersteindc745202015-10-19 07:52:25 +00001044 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1045 bool RetSmallStructInRegABI, bool Win32StructABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001046 unsigned NumRegisterParameters, bool SoftFloatABI)
1047 : TargetCodeGenInfo(new X86_32ABIInfo(
1048 CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
1049 NumRegisterParameters, SoftFloatABI)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +00001050
John McCall1fe2a8c2013-06-18 02:46:29 +00001051 static bool isStructReturnInRegABI(
1052 const llvm::Triple &Triple, const CodeGenOptions &Opts);
1053
Eric Christopher162c91c2015-06-05 22:03:00 +00001054 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00001055 CodeGen::CodeGenModule &CGM) const override;
John McCallbeec5a02010-03-06 00:35:14 +00001056
Craig Topper4f12f102014-03-12 06:41:41 +00001057 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +00001058 // Darwin uses different dwarf register numbers for EH.
John McCallc8e01702013-04-16 22:48:15 +00001059 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCallbeec5a02010-03-06 00:35:14 +00001060 return 4;
1061 }
1062
1063 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00001064 llvm::Value *Address) const override;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001065
Jay Foad7c57be32011-07-11 09:56:20 +00001066 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001067 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +00001068 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001069 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1070 }
1071
Reid Kleckner9b3e3df2014-09-04 20:04:38 +00001072 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
1073 std::string &Constraints,
1074 std::vector<llvm::Type *> &ResultRegTypes,
1075 std::vector<llvm::Type *> &ResultTruncRegTypes,
1076 std::vector<LValue> &ResultRegDests,
1077 std::string &AsmString,
1078 unsigned NumOutputs) const override;
1079
Craig Topper4f12f102014-03-12 06:41:41 +00001080 llvm::Constant *
1081 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourneb453cd62013-10-20 21:29:19 +00001082 unsigned Sig = (0xeb << 0) | // jmp rel8
1083 (0x06 << 8) | // .+0x08
Vedant Kumarbb5d4852017-09-13 00:04:35 +00001084 ('v' << 16) |
1085 ('2' << 24);
Peter Collingbourneb453cd62013-10-20 21:29:19 +00001086 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1087 }
John McCall01391782016-02-05 21:37:38 +00001088
1089 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1090 return "movl\t%ebp, %ebp"
Oliver Stannard7f188642017-08-21 09:54:46 +00001091 "\t\t// marker for objc_retainAutoreleaseReturnValue";
John McCall01391782016-02-05 21:37:38 +00001092 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001093};
1094
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001095}
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001096
Reid Kleckner9b3e3df2014-09-04 20:04:38 +00001097/// Rewrite input constraint references after adding some output constraints.
1098/// In the case where there is one output and one input and we add one output,
1099/// we need to replace all operand references greater than or equal to 1:
1100/// mov $0, $1
1101/// mov eax, $1
1102/// The result will be:
1103/// mov $0, $2
1104/// mov eax, $2
1105static void rewriteInputConstraintReferences(unsigned FirstIn,
1106 unsigned NumNewOuts,
1107 std::string &AsmString) {
1108 std::string Buf;
1109 llvm::raw_string_ostream OS(Buf);
1110 size_t Pos = 0;
1111 while (Pos < AsmString.size()) {
1112 size_t DollarStart = AsmString.find('$', Pos);
1113 if (DollarStart == std::string::npos)
1114 DollarStart = AsmString.size();
1115 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1116 if (DollarEnd == std::string::npos)
1117 DollarEnd = AsmString.size();
1118 OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1119 Pos = DollarEnd;
1120 size_t NumDollars = DollarEnd - DollarStart;
1121 if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1122 // We have an operand reference.
1123 size_t DigitStart = Pos;
1124 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1125 if (DigitEnd == std::string::npos)
1126 DigitEnd = AsmString.size();
1127 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1128 unsigned OperandIndex;
1129 if (!OperandStr.getAsInteger(10, OperandIndex)) {
1130 if (OperandIndex >= FirstIn)
1131 OperandIndex += NumNewOuts;
1132 OS << OperandIndex;
1133 } else {
1134 OS << OperandStr;
1135 }
1136 Pos = DigitEnd;
1137 }
1138 }
1139 AsmString = std::move(OS.str());
1140}
1141
1142/// Add output constraints for EAX:EDX because they are return registers.
1143void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1144 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1145 std::vector<llvm::Type *> &ResultRegTypes,
1146 std::vector<llvm::Type *> &ResultTruncRegTypes,
1147 std::vector<LValue> &ResultRegDests, std::string &AsmString,
1148 unsigned NumOutputs) const {
1149 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1150
1151 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1152 // larger.
1153 if (!Constraints.empty())
1154 Constraints += ',';
1155 if (RetWidth <= 32) {
1156 Constraints += "={eax}";
1157 ResultRegTypes.push_back(CGF.Int32Ty);
1158 } else {
1159 // Use the 'A' constraint for EAX:EDX.
1160 Constraints += "=A";
1161 ResultRegTypes.push_back(CGF.Int64Ty);
1162 }
1163
1164 // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1165 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1166 ResultTruncRegTypes.push_back(CoerceTy);
1167
1168 // Coerce the integer by bitcasting the return slot pointer.
1169 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
1170 CoerceTy->getPointerTo()));
1171 ResultRegDests.push_back(ReturnSlot);
1172
1173 rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1174}
1175
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001176/// shouldReturnTypeInRegister - Determine if the given type should be
Michael Kuperstein68901882015-10-25 08:18:20 +00001177/// returned in a register (for the Darwin and MCU ABI).
Reid Kleckner40ca9132014-05-13 22:05:45 +00001178bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1179 ASTContext &Context) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001180 uint64_t Size = Context.getTypeSize(Ty);
1181
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001182 // For i386, type must be register sized.
1183 // For the MCU ABI, it only needs to be <= 8-byte
1184 if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1185 return false;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001186
1187 if (Ty->isVectorType()) {
1188 // 64- and 128- bit vectors inside structures are not returned in
1189 // registers.
1190 if (Size == 64 || Size == 128)
1191 return false;
1192
1193 return true;
1194 }
1195
Daniel Dunbar4bd95c62010-05-15 00:00:30 +00001196 // If this is a builtin, pointer, enum, complex type, member pointer, or
1197 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +00001198 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +00001199 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +00001200 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001201 return true;
1202
1203 // Arrays are treated like records.
1204 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Reid Kleckner40ca9132014-05-13 22:05:45 +00001205 return shouldReturnTypeInRegister(AT->getElementType(), Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001206
1207 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001208 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001209 if (!RT) return false;
1210
Anders Carlsson40446e82010-01-27 03:25:19 +00001211 // FIXME: Traverse bases here too.
1212
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001213 // Structure types are passed in register if all fields would be
1214 // passed in a register.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001215 for (const auto *FD : RT->getDecl()->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001216 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001217 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001218 continue;
1219
1220 // Check fields recursively.
Reid Kleckner40ca9132014-05-13 22:05:45 +00001221 if (!shouldReturnTypeInRegister(FD->getType(), Context))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001222 return false;
1223 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001224 return true;
1225}
1226
Reid Kleckner04046052016-05-02 17:41:07 +00001227static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1228 // Treat complex types as the element type.
1229 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1230 Ty = CTy->getElementType();
1231
1232 // Check for a type which we know has a simple scalar argument-passing
1233 // convention without any padding. (We're specifically looking for 32
1234 // and 64-bit integer and integer-equivalents, float, and double.)
1235 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1236 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1237 return false;
1238
1239 uint64_t Size = Context.getTypeSize(Ty);
1240 return Size == 32 || Size == 64;
1241}
1242
Reid Kleckner791bbf62017-01-13 17:18:19 +00001243static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
1244 uint64_t &Size) {
1245 for (const auto *FD : RD->fields()) {
1246 // Scalar arguments on the stack get 4 byte alignment on x86. If the
1247 // argument is smaller than 32-bits, expanding the struct will create
1248 // alignment padding.
1249 if (!is32Or64BitBasicType(FD->getType(), Context))
1250 return false;
1251
1252 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1253 // how to expand them yet, and the predicate for telling if a bitfield still
1254 // counts as "basic" is more complicated than what we were doing previously.
1255 if (FD->isBitField())
1256 return false;
1257
1258 Size += Context.getTypeSize(FD->getType());
1259 }
1260 return true;
1261}
1262
1263static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
1264 uint64_t &Size) {
1265 // Don't do this if there are any non-empty bases.
1266 for (const CXXBaseSpecifier &Base : RD->bases()) {
1267 if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
1268 Size))
1269 return false;
1270 }
1271 if (!addFieldSizes(Context, RD, Size))
1272 return false;
1273 return true;
1274}
1275
Reid Kleckner04046052016-05-02 17:41:07 +00001276/// Test whether an argument type which is to be passed indirectly (on the
1277/// stack) would have the equivalent layout if it was expanded into separate
1278/// arguments. If so, we prefer to do the latter to avoid inhibiting
1279/// optimizations.
1280bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1281 // We can only expand structure types.
1282 const RecordType *RT = Ty->getAs<RecordType>();
1283 if (!RT)
1284 return false;
1285 const RecordDecl *RD = RT->getDecl();
Reid Kleckner791bbf62017-01-13 17:18:19 +00001286 uint64_t Size = 0;
Reid Kleckner04046052016-05-02 17:41:07 +00001287 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Reid Kleckner791bbf62017-01-13 17:18:19 +00001288 if (!IsWin32StructABI) {
Reid Kleckner04046052016-05-02 17:41:07 +00001289 // On non-Windows, we have to conservatively match our old bitcode
1290 // prototypes in order to be ABI-compatible at the bitcode level.
1291 if (!CXXRD->isCLike())
1292 return false;
1293 } else {
1294 // Don't do this for dynamic classes.
1295 if (CXXRD->isDynamicClass())
1296 return false;
Reid Kleckner04046052016-05-02 17:41:07 +00001297 }
Reid Kleckner791bbf62017-01-13 17:18:19 +00001298 if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
Reid Kleckner04046052016-05-02 17:41:07 +00001299 return false;
Reid Kleckner791bbf62017-01-13 17:18:19 +00001300 } else {
1301 if (!addFieldSizes(getContext(), RD, Size))
Reid Kleckner04046052016-05-02 17:41:07 +00001302 return false;
Reid Kleckner04046052016-05-02 17:41:07 +00001303 }
1304
1305 // We can do this if there was no alignment padding.
1306 return Size == getContext().getTypeSize(Ty);
1307}
1308
John McCall7f416cc2015-09-08 08:05:57 +00001309ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001310 // If the return value is indirect, then the hidden argument is consuming one
1311 // integer register.
1312 if (State.FreeRegs) {
1313 --State.FreeRegs;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001314 if (!IsMCUABI)
1315 return getNaturalAlignIndirectInReg(RetTy);
Reid Kleckner661f35b2014-01-18 01:12:41 +00001316 }
John McCall7f416cc2015-09-08 08:05:57 +00001317 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
Reid Kleckner661f35b2014-01-18 01:12:41 +00001318}
1319
Eric Christopher7565e0d2015-05-29 23:09:49 +00001320ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1321 CCState &State) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001322 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001323 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001324
Reid Kleckner80944df2014-10-31 22:00:51 +00001325 const Type *Base = nullptr;
1326 uint64_t NumElts = 0;
Erich Keane757d3172016-11-02 18:29:35 +00001327 if ((State.CC == llvm::CallingConv::X86_VectorCall ||
1328 State.CC == llvm::CallingConv::X86_RegCall) &&
Reid Kleckner80944df2014-10-31 22:00:51 +00001329 isHomogeneousAggregate(RetTy, Base, NumElts)) {
1330 // The LLVM struct type for such an aggregate should lower properly.
1331 return ABIArgInfo::getDirect();
1332 }
1333
Chris Lattner458b2aa2010-07-29 02:16:43 +00001334 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001335 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +00001336 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001337 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001338
1339 // 128-bit vectors are a special case; they are returned in
1340 // registers and we need to make sure to pick a type the LLVM
1341 // backend will like.
1342 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001343 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +00001344 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001345
1346 // Always return in register if it fits in a general purpose
1347 // register, or if it is 64 bits and has a single element.
1348 if ((Size == 8 || Size == 16 || Size == 32) ||
1349 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001350 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00001351 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001352
John McCall7f416cc2015-09-08 08:05:57 +00001353 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001354 }
1355
1356 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +00001357 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001358
John McCalla1dee5302010-08-22 10:59:02 +00001359 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +00001360 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +00001361 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001362 if (RT->getDecl()->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00001363 return getIndirectReturnResult(RetTy, State);
Anders Carlsson5789c492009-10-20 22:07:59 +00001364 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001365
David Chisnallde3a0692009-08-17 23:08:21 +00001366 // If specified, structs and unions are always indirect.
Michael Kupersteindc745202015-10-19 07:52:25 +00001367 if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
John McCall7f416cc2015-09-08 08:05:57 +00001368 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001369
Denis Zobnin380b2242016-02-11 11:26:03 +00001370 // Ignore empty structs/unions.
1371 if (isEmptyRecord(getContext(), RetTy, true))
1372 return ABIArgInfo::getIgnore();
1373
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001374 // Small structures which are register sized are generally returned
1375 // in a register.
Reid Kleckner40ca9132014-05-13 22:05:45 +00001376 if (shouldReturnTypeInRegister(RetTy, getContext())) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001377 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +00001378
1379 // As a special-case, if the struct is a "single-element" struct, and
1380 // the field is of type "float" or "double", return it in a
Eli Friedmana98d1f82012-01-25 22:46:34 +00001381 // floating-point register. (MSVC does not apply this special case.)
1382 // We apply a similar transformation for pointer types to improve the
1383 // quality of the generated IR.
Eli Friedmanee945342011-11-18 01:25:50 +00001384 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001385 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedmana98d1f82012-01-25 22:46:34 +00001386 || SeltTy->hasPointerRepresentation())
Eli Friedmanee945342011-11-18 01:25:50 +00001387 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1388
1389 // FIXME: We should be able to narrow this integer in cases with dead
1390 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001391 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001392 }
1393
John McCall7f416cc2015-09-08 08:05:57 +00001394 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001395 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001396
Chris Lattner458b2aa2010-07-29 02:16:43 +00001397 // Treat an enum type as its underlying type.
1398 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1399 RetTy = EnumTy->getDecl()->getIntegerType();
1400
Alex Bradburye41a5e22018-01-12 20:08:16 +00001401 return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
1402 : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001403}
1404
Eli Friedman7919bea2012-06-05 19:40:46 +00001405static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
1406 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1407}
1408
Daniel Dunbared23de32010-09-16 20:42:00 +00001409static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
1410 const RecordType *RT = Ty->getAs<RecordType>();
1411 if (!RT)
1412 return 0;
1413 const RecordDecl *RD = RT->getDecl();
1414
1415 // If this is a C++ record, check the bases first.
1416 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +00001417 for (const auto &I : CXXRD->bases())
1418 if (!isRecordWithSSEVectorType(Context, I.getType()))
Daniel Dunbared23de32010-09-16 20:42:00 +00001419 return false;
1420
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001421 for (const auto *i : RD->fields()) {
Daniel Dunbared23de32010-09-16 20:42:00 +00001422 QualType FT = i->getType();
1423
Eli Friedman7919bea2012-06-05 19:40:46 +00001424 if (isSSEVectorType(Context, FT))
Daniel Dunbared23de32010-09-16 20:42:00 +00001425 return true;
1426
1427 if (isRecordWithSSEVectorType(Context, FT))
1428 return true;
1429 }
1430
1431 return false;
1432}
1433
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001434unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1435 unsigned Align) const {
1436 // Otherwise, if the alignment is less than or equal to the minimum ABI
1437 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001438 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001439 return 0; // Use default alignment.
1440
1441 // On non-Darwin, the stack type alignment is always 4.
1442 if (!IsDarwinVectorABI) {
1443 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001444 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001445 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001446
Daniel Dunbared23de32010-09-16 20:42:00 +00001447 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7919bea2012-06-05 19:40:46 +00001448 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
1449 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbared23de32010-09-16 20:42:00 +00001450 return 16;
1451
1452 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001453}
1454
Rafael Espindola703c47f2012-10-19 05:04:37 +00001455ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
Reid Kleckner661f35b2014-01-18 01:12:41 +00001456 CCState &State) const {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001457 if (!ByVal) {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001458 if (State.FreeRegs) {
1459 --State.FreeRegs; // Non-byval indirects just use one pointer.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001460 if (!IsMCUABI)
1461 return getNaturalAlignIndirectInReg(Ty);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001462 }
John McCall7f416cc2015-09-08 08:05:57 +00001463 return getNaturalAlignIndirect(Ty, false);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001464 }
Daniel Dunbar53fac692010-04-21 19:49:55 +00001465
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001466 // Compute the byval alignment.
1467 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1468 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1469 if (StackAlign == 0)
John McCall7f416cc2015-09-08 08:05:57 +00001470 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001471
1472 // If the stack alignment is less than the type alignment, realign the
1473 // argument.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001474 bool Realign = TypeAlign > StackAlign;
John McCall7f416cc2015-09-08 08:05:57 +00001475 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1476 /*ByVal=*/true, Realign);
Daniel Dunbar557893d2010-04-21 19:10:51 +00001477}
1478
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001479X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1480 const Type *T = isSingleElementStruct(Ty, getContext());
1481 if (!T)
1482 T = Ty.getTypePtr();
1483
1484 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1485 BuiltinType::Kind K = BT->getKind();
1486 if (K == BuiltinType::Float || K == BuiltinType::Double)
1487 return Float;
1488 }
1489 return Integer;
1490}
1491
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001492bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001493 if (!IsSoftFloatABI) {
1494 Class C = classify(Ty);
1495 if (C == Float)
1496 return false;
1497 }
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001498
Rafael Espindola077dd592012-10-24 01:58:58 +00001499 unsigned Size = getContext().getTypeSize(Ty);
1500 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindolae2a9e902012-10-23 02:04:01 +00001501
1502 if (SizeInRegs == 0)
1503 return false;
1504
Michael Kuperstein68901882015-10-25 08:18:20 +00001505 if (!IsMCUABI) {
1506 if (SizeInRegs > State.FreeRegs) {
1507 State.FreeRegs = 0;
1508 return false;
1509 }
1510 } else {
1511 // The MCU psABI allows passing parameters in-reg even if there are
1512 // earlier parameters that are passed on the stack. Also,
1513 // it does not allow passing >8-byte structs in-register,
1514 // even if there are 3 free registers available.
1515 if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1516 return false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001517 }
Rafael Espindola703c47f2012-10-19 05:04:37 +00001518
Reid Kleckner661f35b2014-01-18 01:12:41 +00001519 State.FreeRegs -= SizeInRegs;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001520 return true;
1521}
1522
1523bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1524 bool &InReg,
1525 bool &NeedsPadding) const {
Reid Kleckner04046052016-05-02 17:41:07 +00001526 // On Windows, aggregates other than HFAs are never passed in registers, and
1527 // they do not consume register slots. Homogenous floating-point aggregates
1528 // (HFAs) have already been dealt with at this point.
1529 if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1530 return false;
1531
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001532 NeedsPadding = false;
1533 InReg = !IsMCUABI;
1534
1535 if (!updateFreeRegs(Ty, State))
1536 return false;
1537
1538 if (IsMCUABI)
1539 return true;
Rafael Espindola077dd592012-10-24 01:58:58 +00001540
Reid Kleckner80944df2014-10-31 22:00:51 +00001541 if (State.CC == llvm::CallingConv::X86_FastCall ||
Erich Keane757d3172016-11-02 18:29:35 +00001542 State.CC == llvm::CallingConv::X86_VectorCall ||
1543 State.CC == llvm::CallingConv::X86_RegCall) {
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001544 if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
Rafael Espindolafad28de2012-10-24 01:59:00 +00001545 NeedsPadding = true;
1546
Rafael Espindola077dd592012-10-24 01:58:58 +00001547 return false;
1548 }
1549
Rafael Espindola703c47f2012-10-19 05:04:37 +00001550 return true;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001551}
1552
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001553bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1554 if (!updateFreeRegs(Ty, State))
1555 return false;
1556
1557 if (IsMCUABI)
1558 return false;
1559
1560 if (State.CC == llvm::CallingConv::X86_FastCall ||
Erich Keane757d3172016-11-02 18:29:35 +00001561 State.CC == llvm::CallingConv::X86_VectorCall ||
1562 State.CC == llvm::CallingConv::X86_RegCall) {
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001563 if (getContext().getTypeSize(Ty) > 32)
1564 return false;
1565
1566 return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1567 Ty->isReferenceType());
1568 }
1569
1570 return true;
1571}
1572
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001573ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1574 CCState &State) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001575 // FIXME: Set alignment on indirect arguments.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001576
Reid Klecknerb1be6832014-11-15 01:41:41 +00001577 Ty = useFirstFieldIfTransparentUnion(Ty);
1578
Reid Kleckner80944df2014-10-31 22:00:51 +00001579 // Check with the C++ ABI first.
1580 const RecordType *RT = Ty->getAs<RecordType>();
1581 if (RT) {
1582 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1583 if (RAA == CGCXXABI::RAA_Indirect) {
1584 return getIndirectResult(Ty, false, State);
1585 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1586 // The field index doesn't matter, we'll fix it up later.
1587 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1588 }
1589 }
1590
Erich Keane4bd39302017-06-21 16:37:22 +00001591 // Regcall uses the concept of a homogenous vector aggregate, similar
1592 // to other targets.
Reid Kleckner80944df2014-10-31 22:00:51 +00001593 const Type *Base = nullptr;
1594 uint64_t NumElts = 0;
Erich Keane4bd39302017-06-21 16:37:22 +00001595 if (State.CC == llvm::CallingConv::X86_RegCall &&
Reid Kleckner80944df2014-10-31 22:00:51 +00001596 isHomogeneousAggregate(Ty, Base, NumElts)) {
Erich Keane521ed962017-01-05 00:20:51 +00001597
Erich Keane4bd39302017-06-21 16:37:22 +00001598 if (State.FreeSSERegs >= NumElts) {
1599 State.FreeSSERegs -= NumElts;
1600 if (Ty->isBuiltinType() || Ty->isVectorType())
Reid Kleckner80944df2014-10-31 22:00:51 +00001601 return ABIArgInfo::getDirect();
Erich Keane4bd39302017-06-21 16:37:22 +00001602 return ABIArgInfo::getExpand();
Reid Kleckner80944df2014-10-31 22:00:51 +00001603 }
Erich Keane4bd39302017-06-21 16:37:22 +00001604 return getIndirectResult(Ty, /*ByVal=*/false, State);
Reid Kleckner80944df2014-10-31 22:00:51 +00001605 }
1606
1607 if (isAggregateTypeForABI(Ty)) {
Reid Kleckner04046052016-05-02 17:41:07 +00001608 // Structures with flexible arrays are always indirect.
1609 // FIXME: This should not be byval!
1610 if (RT && RT->getDecl()->hasFlexibleArrayMember())
1611 return getIndirectResult(Ty, true, State);
Daniel Dunbar557893d2010-04-21 19:10:51 +00001612
Reid Kleckner04046052016-05-02 17:41:07 +00001613 // Ignore empty structs/unions on non-Windows.
1614 if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001615 return ABIArgInfo::getIgnore();
1616
Rafael Espindolafad28de2012-10-24 01:59:00 +00001617 llvm::LLVMContext &LLVMContext = getVMContext();
1618 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
Reid Kleckner04046052016-05-02 17:41:07 +00001619 bool NeedsPadding = false;
1620 bool InReg;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001621 if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001622 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperac9201a2013-07-08 04:47:18 +00001623 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001624 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001625 if (InReg)
1626 return ABIArgInfo::getDirectInReg(Result);
1627 else
1628 return ABIArgInfo::getDirect(Result);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001629 }
Craig Topper8a13c412014-05-21 05:09:00 +00001630 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
Rafael Espindola703c47f2012-10-19 05:04:37 +00001631
Daniel Dunbar11c08c82009-11-09 01:33:53 +00001632 // Expand small (<= 128-bit) record types when we know that the stack layout
1633 // of those arguments will match the struct. This is important because the
1634 // LLVM backend isn't smart enough to remove byval, which inhibits many
1635 // optimizations.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001636 // Don't do this for the MCU if there are still free integer registers
1637 // (see X86_64 ABI for full explanation).
Reid Kleckner04046052016-05-02 17:41:07 +00001638 if (getContext().getTypeSize(Ty) <= 4 * 32 &&
1639 (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty))
Reid Kleckner661f35b2014-01-18 01:12:41 +00001640 return ABIArgInfo::getExpandWithPadding(
Reid Kleckner80944df2014-10-31 22:00:51 +00001641 State.CC == llvm::CallingConv::X86_FastCall ||
Erich Keane757d3172016-11-02 18:29:35 +00001642 State.CC == llvm::CallingConv::X86_VectorCall ||
1643 State.CC == llvm::CallingConv::X86_RegCall,
Reid Kleckner80944df2014-10-31 22:00:51 +00001644 PaddingType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001645
Reid Kleckner661f35b2014-01-18 01:12:41 +00001646 return getIndirectResult(Ty, true, State);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001647 }
1648
Chris Lattnerd774ae92010-08-26 20:05:13 +00001649 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +00001650 // On Darwin, some vectors are passed in memory, we handle this by passing
1651 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +00001652 if (IsDarwinVectorABI) {
1653 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +00001654 if ((Size == 8 || Size == 16 || Size == 32) ||
1655 (Size == 64 && VT->getNumElements() == 1))
1656 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1657 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +00001658 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001659
Chad Rosier651c1832013-03-25 21:00:27 +00001660 if (IsX86_MMXType(CGT.ConvertType(Ty)))
1661 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001662
Chris Lattnerd774ae92010-08-26 20:05:13 +00001663 return ABIArgInfo::getDirect();
1664 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001665
1666
Chris Lattner458b2aa2010-07-29 02:16:43 +00001667 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1668 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +00001669
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001670 bool InReg = shouldPrimitiveUseInReg(Ty, State);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001671
1672 if (Ty->isPromotableIntegerType()) {
1673 if (InReg)
Alex Bradburye41a5e22018-01-12 20:08:16 +00001674 return ABIArgInfo::getExtendInReg(Ty);
1675 return ABIArgInfo::getExtend(Ty);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001676 }
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001677
Rafael Espindola703c47f2012-10-19 05:04:37 +00001678 if (InReg)
1679 return ABIArgInfo::getDirectInReg();
1680 return ABIArgInfo::getDirect();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001681}
1682
Erich Keane521ed962017-01-05 00:20:51 +00001683void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
1684 bool &UsedInAlloca) const {
Erich Keane4bd39302017-06-21 16:37:22 +00001685 // Vectorcall x86 works subtly different than in x64, so the format is
1686 // a bit different than the x64 version. First, all vector types (not HVAs)
1687 // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers.
1688 // This differs from the x64 implementation, where the first 6 by INDEX get
1689 // registers.
1690 // After that, integers AND HVAs are assigned Left to Right in the same pass.
1691 // Integers are passed as ECX/EDX if one is available (in order). HVAs will
1692 // first take up the remaining YMM/XMM registers. If insufficient registers
1693 // remain but an integer register (ECX/EDX) is available, it will be passed
1694 // in that, else, on the stack.
Erich Keane521ed962017-01-05 00:20:51 +00001695 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00001696 // First pass do all the vector types.
1697 const Type *Base = nullptr;
1698 uint64_t NumElts = 0;
1699 const QualType& Ty = I.type;
1700 if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
1701 isHomogeneousAggregate(Ty, Base, NumElts)) {
1702 if (State.FreeSSERegs >= NumElts) {
1703 State.FreeSSERegs -= NumElts;
1704 I.info = ABIArgInfo::getDirect();
1705 } else {
1706 I.info = classifyArgumentType(Ty, State);
1707 }
1708 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1709 }
Erich Keane521ed962017-01-05 00:20:51 +00001710 }
Erich Keane4bd39302017-06-21 16:37:22 +00001711
Erich Keane521ed962017-01-05 00:20:51 +00001712 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00001713 // Second pass, do the rest!
1714 const Type *Base = nullptr;
1715 uint64_t NumElts = 0;
1716 const QualType& Ty = I.type;
1717 bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts);
1718
1719 if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) {
1720 // Assign true HVAs (non vector/native FP types).
1721 if (State.FreeSSERegs >= NumElts) {
1722 State.FreeSSERegs -= NumElts;
1723 I.info = getDirectX86Hva();
1724 } else {
1725 I.info = getIndirectResult(Ty, /*ByVal=*/false, State);
1726 }
1727 } else if (!IsHva) {
1728 // Assign all Non-HVAs, so this will exclude Vector/FP args.
1729 I.info = classifyArgumentType(Ty, State);
1730 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1731 }
Erich Keane521ed962017-01-05 00:20:51 +00001732 }
1733}
1734
Rafael Espindolaa6472962012-07-24 00:01:07 +00001735void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001736 CCState State(FI.getCallingConvention());
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001737 if (IsMCUABI)
1738 State.FreeRegs = 3;
1739 else if (State.CC == llvm::CallingConv::X86_FastCall)
Reid Kleckner661f35b2014-01-18 01:12:41 +00001740 State.FreeRegs = 2;
Reid Kleckner80944df2014-10-31 22:00:51 +00001741 else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1742 State.FreeRegs = 2;
1743 State.FreeSSERegs = 6;
1744 } else if (FI.getHasRegParm())
Reid Kleckner661f35b2014-01-18 01:12:41 +00001745 State.FreeRegs = FI.getRegParm();
Erich Keane757d3172016-11-02 18:29:35 +00001746 else if (State.CC == llvm::CallingConv::X86_RegCall) {
1747 State.FreeRegs = 5;
1748 State.FreeSSERegs = 8;
1749 } else
Reid Kleckner661f35b2014-01-18 01:12:41 +00001750 State.FreeRegs = DefaultNumRegisterParameters;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001751
Akira Hatanakabe7daa32018-03-12 17:05:06 +00001752 if (!getCXXABI().classifyReturnType(FI)) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00001753 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
Reid Kleckner677539d2014-07-10 01:58:55 +00001754 } else if (FI.getReturnInfo().isIndirect()) {
1755 // The C++ ABI is not aware of register usage, so we have to check if the
1756 // return value was sret and put it in a register ourselves if appropriate.
1757 if (State.FreeRegs) {
1758 --State.FreeRegs; // The sret parameter consumes a register.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001759 if (!IsMCUABI)
1760 FI.getReturnInfo().setInReg(true);
Reid Kleckner677539d2014-07-10 01:58:55 +00001761 }
1762 }
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001763
Peter Collingbournef7706832014-12-12 23:41:25 +00001764 // The chain argument effectively gives us another free register.
1765 if (FI.isChainCall())
1766 ++State.FreeRegs;
1767
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001768 bool UsedInAlloca = false;
Erich Keane521ed962017-01-05 00:20:51 +00001769 if (State.CC == llvm::CallingConv::X86_VectorCall) {
1770 computeVectorCallArgs(FI, State, UsedInAlloca);
1771 } else {
1772 // If not vectorcall, revert to normal behavior.
1773 for (auto &I : FI.arguments()) {
1774 I.info = classifyArgumentType(I.type, State);
1775 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1776 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001777 }
1778
1779 // If we needed to use inalloca for any argument, do a second pass and rewrite
1780 // all the memory arguments to use inalloca.
1781 if (UsedInAlloca)
1782 rewriteWithInAlloca(FI);
1783}
1784
1785void
1786X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001787 CharUnits &StackOffset, ABIArgInfo &Info,
1788 QualType Type) const {
1789 // Arguments are always 4-byte-aligned.
1790 CharUnits FieldAlign = CharUnits::fromQuantity(4);
1791
1792 assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
Reid Klecknerd378a712014-04-10 19:09:43 +00001793 Info = ABIArgInfo::getInAlloca(FrameFields.size());
1794 FrameFields.push_back(CGT.ConvertTypeForMem(Type));
John McCall7f416cc2015-09-08 08:05:57 +00001795 StackOffset += getContext().getTypeSizeInChars(Type);
Reid Klecknerd378a712014-04-10 19:09:43 +00001796
John McCall7f416cc2015-09-08 08:05:57 +00001797 // Insert padding bytes to respect alignment.
1798 CharUnits FieldEnd = StackOffset;
Rui Ueyama83aa9792016-01-14 21:00:27 +00001799 StackOffset = FieldEnd.alignTo(FieldAlign);
John McCall7f416cc2015-09-08 08:05:57 +00001800 if (StackOffset != FieldEnd) {
1801 CharUnits NumBytes = StackOffset - FieldEnd;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001802 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
John McCall7f416cc2015-09-08 08:05:57 +00001803 Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001804 FrameFields.push_back(Ty);
1805 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001806}
1807
Reid Kleckner852361d2014-07-26 00:12:26 +00001808static bool isArgInAlloca(const ABIArgInfo &Info) {
1809 // Leave ignored and inreg arguments alone.
1810 switch (Info.getKind()) {
1811 case ABIArgInfo::InAlloca:
1812 return true;
1813 case ABIArgInfo::Indirect:
1814 assert(Info.getIndirectByVal());
1815 return true;
1816 case ABIArgInfo::Ignore:
1817 return false;
1818 case ABIArgInfo::Direct:
1819 case ABIArgInfo::Extend:
Reid Kleckner852361d2014-07-26 00:12:26 +00001820 if (Info.getInReg())
1821 return false;
1822 return true;
Reid Kleckner04046052016-05-02 17:41:07 +00001823 case ABIArgInfo::Expand:
1824 case ABIArgInfo::CoerceAndExpand:
1825 // These are aggregate types which are never passed in registers when
1826 // inalloca is involved.
1827 return true;
Reid Kleckner852361d2014-07-26 00:12:26 +00001828 }
1829 llvm_unreachable("invalid enum");
1830}
1831
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001832void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1833 assert(IsWin32StructABI && "inalloca only supported on win32");
1834
1835 // Build a packed struct type for all of the arguments in memory.
1836 SmallVector<llvm::Type *, 6> FrameFields;
1837
John McCall7f416cc2015-09-08 08:05:57 +00001838 // The stack alignment is always 4.
1839 CharUnits StackAlign = CharUnits::fromQuantity(4);
1840
1841 CharUnits StackOffset;
Reid Kleckner852361d2014-07-26 00:12:26 +00001842 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1843
1844 // Put 'this' into the struct before 'sret', if necessary.
1845 bool IsThisCall =
1846 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1847 ABIArgInfo &Ret = FI.getReturnInfo();
1848 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1849 isArgInAlloca(I->info)) {
1850 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1851 ++I;
1852 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001853
1854 // Put the sret parameter into the inalloca struct if it's in memory.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001855 if (Ret.isIndirect() && !Ret.getInReg()) {
1856 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1857 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
Reid Klecknerfab1e892014-02-25 00:59:14 +00001858 // On Windows, the hidden sret parameter is always returned in eax.
1859 Ret.setInAllocaSRet(IsWin32StructABI);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001860 }
1861
1862 // Skip the 'this' parameter in ecx.
Reid Kleckner852361d2014-07-26 00:12:26 +00001863 if (IsThisCall)
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001864 ++I;
1865
1866 // Put arguments passed in memory into the struct.
1867 for (; I != E; ++I) {
Reid Kleckner852361d2014-07-26 00:12:26 +00001868 if (isArgInAlloca(I->info))
1869 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001870 }
1871
1872 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001873 /*isPacked=*/true),
1874 StackAlign);
Rafael Espindolaa6472962012-07-24 00:01:07 +00001875}
1876
John McCall7f416cc2015-09-08 08:05:57 +00001877Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
1878 Address VAListAddr, QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001879
John McCall7f416cc2015-09-08 08:05:57 +00001880 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001881
John McCall7f416cc2015-09-08 08:05:57 +00001882 // x86-32 changes the alignment of certain arguments on the stack.
1883 //
1884 // Just messing with TypeInfo like this works because we never pass
1885 // anything indirectly.
1886 TypeInfo.second = CharUnits::fromQuantity(
1887 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001888
John McCall7f416cc2015-09-08 08:05:57 +00001889 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
1890 TypeInfo, CharUnits::fromQuantity(4),
1891 /*AllowHigherAlign*/ true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001892}
1893
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001894bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1895 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1896 assert(Triple.getArch() == llvm::Triple::x86);
1897
1898 switch (Opts.getStructReturnConvention()) {
1899 case CodeGenOptions::SRCK_Default:
1900 break;
1901 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
1902 return false;
1903 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
1904 return true;
1905 }
1906
Michael Kupersteind749f232015-10-27 07:46:22 +00001907 if (Triple.isOSDarwin() || Triple.isOSIAMCU())
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001908 return true;
1909
1910 switch (Triple.getOS()) {
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001911 case llvm::Triple::DragonFly:
1912 case llvm::Triple::FreeBSD:
1913 case llvm::Triple::OpenBSD:
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001914 case llvm::Triple::Win32:
Reid Kleckner2918fef2014-11-24 22:05:42 +00001915 return true;
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001916 default:
1917 return false;
1918 }
1919}
1920
Simon Atanasyan1a116db2017-07-20 20:34:18 +00001921void X86_32TargetCodeGenInfo::setTargetAttributes(
Rafael Espindoladeb10be2018-02-07 19:04:41 +00001922 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
1923 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00001924 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00001925 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Charles Davis4ea31ab2010-02-13 15:54:06 +00001926 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1927 // Get the LLVM function.
1928 llvm::Function *Fn = cast<llvm::Function>(GV);
1929
1930 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001931 llvm::AttrBuilder B;
Bill Wendlingccf94c92012-10-14 03:28:14 +00001932 B.addStackAlignmentAttr(16);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001933 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
Charles Davis4ea31ab2010-02-13 15:54:06 +00001934 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00001935 if (FD->hasAttr<AnyX86InterruptAttr>()) {
1936 llvm::Function *Fn = cast<llvm::Function>(GV);
1937 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
1938 }
Charles Davis4ea31ab2010-02-13 15:54:06 +00001939 }
1940}
1941
John McCallbeec5a02010-03-06 00:35:14 +00001942bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1943 CodeGen::CodeGenFunction &CGF,
1944 llvm::Value *Address) const {
1945 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallbeec5a02010-03-06 00:35:14 +00001946
Chris Lattnerece04092012-02-07 00:39:47 +00001947 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001948
John McCallbeec5a02010-03-06 00:35:14 +00001949 // 0-7 are the eight integer registers; the order is different
1950 // on Darwin (for EH), but the range is the same.
1951 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +00001952 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +00001953
John McCallc8e01702013-04-16 22:48:15 +00001954 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCallbeec5a02010-03-06 00:35:14 +00001955 // 12-16 are st(0..4). Not sure why we stop at 4.
1956 // These have size 16, which is sizeof(long double) on
1957 // platforms with 8-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001958 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCall943fae92010-05-27 06:19:26 +00001959 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001960
John McCallbeec5a02010-03-06 00:35:14 +00001961 } else {
1962 // 9 is %eflags, which doesn't get a size on Darwin for some
1963 // reason.
John McCall7f416cc2015-09-08 08:05:57 +00001964 Builder.CreateAlignedStore(
1965 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
1966 CharUnits::One());
John McCallbeec5a02010-03-06 00:35:14 +00001967
1968 // 11-16 are st(0..5). Not sure why we stop at 5.
1969 // These have size 12, which is sizeof(long double) on
1970 // platforms with 4-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001971 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCall943fae92010-05-27 06:19:26 +00001972 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1973 }
John McCallbeec5a02010-03-06 00:35:14 +00001974
1975 return false;
1976}
1977
Chris Lattner0cf24192010-06-28 20:05:43 +00001978//===----------------------------------------------------------------------===//
1979// X86-64 ABI Implementation
1980//===----------------------------------------------------------------------===//
1981
1982
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001983namespace {
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001984/// The AVX ABI level for X86 targets.
1985enum class X86AVXABILevel {
1986 None,
Ahmed Bougacha0b938282015-06-22 21:31:43 +00001987 AVX,
1988 AVX512
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001989};
1990
1991/// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
1992static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
1993 switch (AVXLevel) {
Ahmed Bougacha0b938282015-06-22 21:31:43 +00001994 case X86AVXABILevel::AVX512:
1995 return 512;
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001996 case X86AVXABILevel::AVX:
1997 return 256;
1998 case X86AVXABILevel::None:
1999 return 128;
2000 }
Yaron Kerenb76cb042015-06-23 09:45:42 +00002001 llvm_unreachable("Unknown AVXLevel");
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002002}
2003
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002004/// X86_64ABIInfo - The X86_64 ABI information.
John McCall12f23522016-04-04 18:33:08 +00002005class X86_64ABIInfo : public SwiftABIInfo {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002006 enum Class {
2007 Integer = 0,
2008 SSE,
2009 SSEUp,
2010 X87,
2011 X87Up,
2012 ComplexX87,
2013 NoClass,
2014 Memory
2015 };
2016
2017 /// merge - Implement the X86_64 ABI merging algorithm.
2018 ///
2019 /// Merge an accumulating classification \arg Accum with a field
2020 /// classification \arg Field.
2021 ///
2022 /// \param Accum - The accumulating classification. This should
2023 /// always be either NoClass or the result of a previous merge
2024 /// call. In addition, this should never be Memory (the caller
2025 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +00002026 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002027
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002028 /// postMerge - Implement the X86_64 ABI post merging algorithm.
2029 ///
2030 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
2031 /// final MEMORY or SSE classes when necessary.
2032 ///
2033 /// \param AggregateSize - The size of the current aggregate in
2034 /// the classification process.
2035 ///
2036 /// \param Lo - The classification for the parts of the type
2037 /// residing in the low word of the containing object.
2038 ///
2039 /// \param Hi - The classification for the parts of the type
2040 /// residing in the higher words of the containing object.
2041 ///
2042 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
2043
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002044 /// classify - Determine the x86_64 register classes in which the
2045 /// given type T should be passed.
2046 ///
2047 /// \param Lo - The classification for the parts of the type
2048 /// residing in the low word of the containing object.
2049 ///
2050 /// \param Hi - The classification for the parts of the type
2051 /// residing in the high word of the containing object.
2052 ///
2053 /// \param OffsetBase - The bit offset of this type in the
2054 /// containing object. Some parameters are classified different
2055 /// depending on whether they straddle an eightbyte boundary.
2056 ///
Eli Friedman96fd2642013-06-12 00:13:45 +00002057 /// \param isNamedArg - Whether the argument in question is a "named"
2058 /// argument, as used in AMD64-ABI 3.5.7.
2059 ///
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002060 /// If a word is unused its result will be NoClass; if a type should
2061 /// be passed in Memory then at least the classification of \arg Lo
2062 /// will be Memory.
2063 ///
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00002064 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002065 ///
2066 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
2067 /// also be ComplexX87.
Eli Friedman96fd2642013-06-12 00:13:45 +00002068 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
2069 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002070
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002071 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +00002072 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
2073 unsigned IROffset, QualType SourceTy,
2074 unsigned SourceOffset) const;
2075 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
2076 unsigned IROffset, QualType SourceTy,
2077 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002078
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002079 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +00002080 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +00002081 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +00002082
2083 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002084 /// such that the argument will be passed in memory.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002085 ///
2086 /// \param freeIntRegs - The number of free integer registers remaining
2087 /// available.
2088 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002089
Chris Lattner458b2aa2010-07-29 02:16:43 +00002090 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002091
Erich Keane757d3172016-11-02 18:29:35 +00002092 ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
2093 unsigned &neededInt, unsigned &neededSSE,
Eli Friedman96fd2642013-06-12 00:13:45 +00002094 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002095
Erich Keane757d3172016-11-02 18:29:35 +00002096 ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
2097 unsigned &NeededSSE) const;
2098
2099 ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
2100 unsigned &NeededSSE) const;
2101
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002102 bool IsIllegalVectorType(QualType Ty) const;
2103
John McCalle0fda732011-04-21 01:20:55 +00002104 /// The 0.98 ABI revision clarified a lot of ambiguities,
2105 /// unfortunately in ways that were not always consistent with
2106 /// certain previous compilers. In particular, platforms which
2107 /// required strict binary compatibility with older versions of GCC
2108 /// may need to exempt themselves.
2109 bool honorsRevision0_98() const {
John McCallc8e01702013-04-16 22:48:15 +00002110 return !getTarget().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +00002111 }
2112
Richard Smithf667ad52017-08-26 01:04:35 +00002113 /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
2114 /// classify it as INTEGER (for compatibility with older clang compilers).
David Majnemere2ae2282016-03-04 05:26:16 +00002115 bool classifyIntegerMMXAsSSE() const {
Richard Smithf667ad52017-08-26 01:04:35 +00002116 // Clang <= 3.8 did not do this.
2117 if (getCodeGenOpts().getClangABICompat() <=
2118 CodeGenOptions::ClangABI::Ver3_8)
2119 return false;
2120
David Majnemere2ae2282016-03-04 05:26:16 +00002121 const llvm::Triple &Triple = getTarget().getTriple();
2122 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
2123 return false;
2124 if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
2125 return false;
2126 return true;
2127 }
2128
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002129 X86AVXABILevel AVXLevel;
Derek Schuffc7dd7222012-10-11 15:52:22 +00002130 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
2131 // 64-bit hardware.
2132 bool Has64BitPointers;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002133
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002134public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002135 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
John McCall12f23522016-04-04 18:33:08 +00002136 SwiftABIInfo(CGT), AVXLevel(AVXLevel),
Derek Schuff8a872f32012-10-11 18:21:13 +00002137 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffc7dd7222012-10-11 15:52:22 +00002138 }
Chris Lattner22a931e2010-06-29 06:01:59 +00002139
John McCalla729c622012-02-17 03:33:10 +00002140 bool isPassedUsingAVXType(QualType type) const {
2141 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002142 // The freeIntRegs argument doesn't matter here.
Eli Friedman96fd2642013-06-12 00:13:45 +00002143 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
2144 /*isNamedArg*/true);
John McCalla729c622012-02-17 03:33:10 +00002145 if (info.isDirect()) {
2146 llvm::Type *ty = info.getCoerceToType();
2147 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
2148 return (vectorTy->getBitWidth() > 128);
2149 }
2150 return false;
2151 }
2152
Craig Topper4f12f102014-03-12 06:41:41 +00002153 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002154
John McCall7f416cc2015-09-08 08:05:57 +00002155 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2156 QualType Ty) const override;
Charles Davisc7d5c942015-09-17 20:55:33 +00002157 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
2158 QualType Ty) const override;
Peter Collingbourne69b004d2015-02-25 23:18:42 +00002159
2160 bool has64BitPointers() const {
2161 return Has64BitPointers;
2162 }
John McCall12f23522016-04-04 18:33:08 +00002163
John McCall56331e22018-01-07 06:28:49 +00002164 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
John McCall12f23522016-04-04 18:33:08 +00002165 bool asReturnValue) const override {
2166 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2167 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00002168 bool isSwiftErrorInRegister() const override {
2169 return true;
2170 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002171};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002172
Chris Lattner04dc9572010-08-31 16:44:54 +00002173/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
Arnold Schwaighofer4fc955e2016-10-12 18:59:24 +00002174class WinX86_64ABIInfo : public SwiftABIInfo {
Chris Lattner04dc9572010-08-31 16:44:54 +00002175public:
Reid Kleckner11a17192015-10-28 22:29:52 +00002176 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT)
Arnold Schwaighofer4fc955e2016-10-12 18:59:24 +00002177 : SwiftABIInfo(CGT),
Reid Kleckner11a17192015-10-28 22:29:52 +00002178 IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002179
Craig Topper4f12f102014-03-12 06:41:41 +00002180 void computeInfo(CGFunctionInfo &FI) const override;
Chris Lattner04dc9572010-08-31 16:44:54 +00002181
John McCall7f416cc2015-09-08 08:05:57 +00002182 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2183 QualType Ty) const override;
Reid Kleckner80944df2014-10-31 22:00:51 +00002184
2185 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2186 // FIXME: Assumes vectorcall is in use.
2187 return isX86VectorTypeForVectorCall(getContext(), Ty);
2188 }
2189
2190 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2191 uint64_t NumMembers) const override {
2192 // FIXME: Assumes vectorcall is in use.
2193 return isX86VectorCallAggregateSmallEnough(NumMembers);
2194 }
Reid Kleckner11a17192015-10-28 22:29:52 +00002195
John McCall56331e22018-01-07 06:28:49 +00002196 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars,
Arnold Schwaighofer4fc955e2016-10-12 18:59:24 +00002197 bool asReturnValue) const override {
2198 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2199 }
2200
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00002201 bool isSwiftErrorInRegister() const override {
2202 return true;
2203 }
2204
Reid Kleckner11a17192015-10-28 22:29:52 +00002205private:
Erich Keane521ed962017-01-05 00:20:51 +00002206 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
2207 bool IsVectorCall, bool IsRegCall) const;
2208 ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
2209 const ABIArgInfo &current) const;
2210 void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs,
2211 bool IsVectorCall, bool IsRegCall) const;
Reid Kleckner11a17192015-10-28 22:29:52 +00002212
Erich Keane521ed962017-01-05 00:20:51 +00002213 bool IsMingw64;
Chris Lattner04dc9572010-08-31 16:44:54 +00002214};
2215
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002216class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2217public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002218 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
Alexey Bataev00396512015-07-02 03:40:19 +00002219 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002220
John McCalla729c622012-02-17 03:33:10 +00002221 const X86_64ABIInfo &getABIInfo() const {
2222 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2223 }
2224
Craig Topper4f12f102014-03-12 06:41:41 +00002225 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +00002226 return 7;
2227 }
2228
2229 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00002230 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00002231 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002232
John McCall943fae92010-05-27 06:19:26 +00002233 // 0-15 are the 16 integer registers.
2234 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00002235 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +00002236 return false;
2237 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00002238
Jay Foad7c57be32011-07-11 09:56:20 +00002239 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002240 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +00002241 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00002242 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2243 }
2244
John McCalla729c622012-02-17 03:33:10 +00002245 bool isNoProtoCallVariadic(const CallArgList &args,
Craig Topper4f12f102014-03-12 06:41:41 +00002246 const FunctionNoProtoType *fnType) const override {
John McCallcbc038a2011-09-21 08:08:30 +00002247 // The default CC on x86-64 sets %al to the number of SSA
2248 // registers used, and GCC sets this when calling an unprototyped
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002249 // function, so we override the default behavior. However, don't do
Eli Friedmanb8e45b22011-12-06 03:08:26 +00002250 // that when AVX types are involved: the ABI explicitly states it is
2251 // undefined, and it doesn't work in practice because of how the ABI
2252 // defines varargs anyway.
Reid Kleckner78af0702013-08-27 23:08:25 +00002253 if (fnType->getCallConv() == CC_C) {
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002254 bool HasAVXType = false;
John McCalla729c622012-02-17 03:33:10 +00002255 for (CallArgList::const_iterator
2256 it = args.begin(), ie = args.end(); it != ie; ++it) {
2257 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2258 HasAVXType = true;
2259 break;
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002260 }
2261 }
John McCalla729c622012-02-17 03:33:10 +00002262
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002263 if (!HasAVXType)
2264 return true;
2265 }
John McCallcbc038a2011-09-21 08:08:30 +00002266
John McCalla729c622012-02-17 03:33:10 +00002267 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCallcbc038a2011-09-21 08:08:30 +00002268 }
2269
Craig Topper4f12f102014-03-12 06:41:41 +00002270 llvm::Constant *
2271 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Vedant Kumarbb5d4852017-09-13 00:04:35 +00002272 unsigned Sig = (0xeb << 0) | // jmp rel8
2273 (0x06 << 8) | // .+0x08
2274 ('v' << 16) |
2275 ('2' << 24);
Peter Collingbourneb453cd62013-10-20 21:29:19 +00002276 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2277 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00002278
2279 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00002280 CodeGen::CodeGenModule &CGM) const override {
2281 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002282 return;
Alexey Bataevd51e9932016-01-15 04:06:31 +00002283 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Erich Keanebb9c7042017-08-30 21:17:40 +00002284 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2285 // Get the LLVM function.
2286 auto *Fn = cast<llvm::Function>(GV);
2287
2288 // Now add the 'alignstack' attribute with a value of 16.
2289 llvm::AttrBuilder B;
2290 B.addStackAlignmentAttr(16);
2291 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
2292 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00002293 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2294 llvm::Function *Fn = cast<llvm::Function>(GV);
2295 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2296 }
2297 }
2298 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002299};
2300
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002301class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
2302public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002303 PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2304 : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002305
2306 void getDependentLibraryOption(llvm::StringRef Lib,
Alexander Kornienko34eb2072015-04-11 02:00:23 +00002307 llvm::SmallString<24> &Opt) const override {
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002308 Opt = "\01";
Yunzhong Gaod65200c2015-07-20 17:46:56 +00002309 // If the argument contains a space, enclose it in quotes.
2310 if (Lib.find(" ") != StringRef::npos)
2311 Opt += "\"" + Lib.str() + "\"";
2312 else
2313 Opt += Lib;
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002314 }
2315};
2316
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002317static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
Michael Kupersteinf0e4ccf2015-02-16 11:57:43 +00002318 // If the argument does not end in .lib, automatically add the suffix.
2319 // If the argument contains a space, enclose it in quotes.
2320 // This matches the behavior of MSVC.
2321 bool Quote = (Lib.find(" ") != StringRef::npos);
2322 std::string ArgStr = Quote ? "\"" : "";
2323 ArgStr += Lib;
Rui Ueyama727025a2013-10-31 19:12:53 +00002324 if (!Lib.endswith_lower(".lib"))
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002325 ArgStr += ".lib";
Michael Kupersteinf0e4ccf2015-02-16 11:57:43 +00002326 ArgStr += Quote ? "\"" : "";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002327 return ArgStr;
2328}
2329
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002330class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2331public:
John McCall1fe2a8c2013-06-18 02:46:29 +00002332 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Michael Kupersteindc745202015-10-19 07:52:25 +00002333 bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2334 unsigned NumRegisterParameters)
2335 : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00002336 Win32StructABI, NumRegisterParameters, false) {}
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002337
Eric Christopher162c91c2015-06-05 22:03:00 +00002338 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00002339 CodeGen::CodeGenModule &CGM) const override;
Hans Wennborg77dc2362015-01-20 19:45:50 +00002340
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002341 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00002342 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002343 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002344 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002345 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00002346
2347 void getDetectMismatchOption(llvm::StringRef Name,
2348 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00002349 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00002350 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00002351 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002352};
2353
Hans Wennborgd43f40d2018-02-23 13:47:36 +00002354static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2355 CodeGen::CodeGenModule &CGM) {
2356 if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) {
Hans Wennborg77dc2362015-01-20 19:45:50 +00002357
Hans Wennborgd43f40d2018-02-23 13:47:36 +00002358 if (CGM.getCodeGenOpts().StackProbeSize != 4096)
Eric Christopher7565e0d2015-05-29 23:09:49 +00002359 Fn->addFnAttr("stack-probe-size",
2360 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
Hans Wennborgd43f40d2018-02-23 13:47:36 +00002361 if (CGM.getCodeGenOpts().NoStackArgProbe)
2362 Fn->addFnAttr("no-stack-arg-probe");
Hans Wennborg77dc2362015-01-20 19:45:50 +00002363 }
2364}
2365
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002366void WinX86_32TargetCodeGenInfo::setTargetAttributes(
Rafael Espindoladeb10be2018-02-07 19:04:41 +00002367 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2368 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2369 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002370 return;
Hans Wennborgd43f40d2018-02-23 13:47:36 +00002371 addStackProbeTargetAttributes(D, GV, CGM);
Hans Wennborg77dc2362015-01-20 19:45:50 +00002372}
2373
Chris Lattner04dc9572010-08-31 16:44:54 +00002374class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2375public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002376 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2377 X86AVXABILevel AVXLevel)
Alexey Bataev00396512015-07-02 03:40:19 +00002378 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
Chris Lattner04dc9572010-08-31 16:44:54 +00002379
Eric Christopher162c91c2015-06-05 22:03:00 +00002380 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00002381 CodeGen::CodeGenModule &CGM) const override;
Hans Wennborg77dc2362015-01-20 19:45:50 +00002382
Craig Topper4f12f102014-03-12 06:41:41 +00002383 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
Chris Lattner04dc9572010-08-31 16:44:54 +00002384 return 7;
2385 }
2386
2387 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00002388 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00002389 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002390
Chris Lattner04dc9572010-08-31 16:44:54 +00002391 // 0-15 are the 16 integer registers.
2392 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00002393 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattner04dc9572010-08-31 16:44:54 +00002394 return false;
2395 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002396
2397 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00002398 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002399 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002400 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002401 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00002402
2403 void getDetectMismatchOption(llvm::StringRef Name,
2404 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00002405 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00002406 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00002407 }
Chris Lattner04dc9572010-08-31 16:44:54 +00002408};
2409
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002410void WinX86_64TargetCodeGenInfo::setTargetAttributes(
Rafael Espindoladeb10be2018-02-07 19:04:41 +00002411 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2412 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2413 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002414 return;
Alexey Bataevd51e9932016-01-15 04:06:31 +00002415 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Erich Keanebb9c7042017-08-30 21:17:40 +00002416 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2417 // Get the LLVM function.
2418 auto *Fn = cast<llvm::Function>(GV);
2419
2420 // Now add the 'alignstack' attribute with a value of 16.
2421 llvm::AttrBuilder B;
2422 B.addStackAlignmentAttr(16);
2423 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
2424 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00002425 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2426 llvm::Function *Fn = cast<llvm::Function>(GV);
2427 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2428 }
2429 }
2430
Hans Wennborgd43f40d2018-02-23 13:47:36 +00002431 addStackProbeTargetAttributes(D, GV, CGM);
Hans Wennborg77dc2362015-01-20 19:45:50 +00002432}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002433}
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002434
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002435void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2436 Class &Hi) const {
2437 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2438 //
2439 // (a) If one of the classes is Memory, the whole argument is passed in
2440 // memory.
2441 //
2442 // (b) If X87UP is not preceded by X87, the whole argument is passed in
2443 // memory.
2444 //
2445 // (c) If the size of the aggregate exceeds two eightbytes and the first
2446 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2447 // argument is passed in memory. NOTE: This is necessary to keep the
2448 // ABI working for processors that don't support the __m256 type.
2449 //
2450 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2451 //
2452 // Some of these are enforced by the merging logic. Others can arise
2453 // only with unions; for example:
2454 // union { _Complex double; unsigned; }
2455 //
2456 // Note that clauses (b) and (c) were added in 0.98.
2457 //
2458 if (Hi == Memory)
2459 Lo = Memory;
2460 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2461 Lo = Memory;
2462 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2463 Lo = Memory;
2464 if (Hi == SSEUp && Lo != SSE)
2465 Hi = SSE;
2466}
2467
Chris Lattnerd776fb12010-06-28 21:43:59 +00002468X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002469 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2470 // classified recursively so that always two fields are
2471 // considered. The resulting class is calculated according to
2472 // the classes of the fields in the eightbyte:
2473 //
2474 // (a) If both classes are equal, this is the resulting class.
2475 //
2476 // (b) If one of the classes is NO_CLASS, the resulting class is
2477 // the other class.
2478 //
2479 // (c) If one of the classes is MEMORY, the result is the MEMORY
2480 // class.
2481 //
2482 // (d) If one of the classes is INTEGER, the result is the
2483 // INTEGER.
2484 //
2485 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2486 // MEMORY is used as class.
2487 //
2488 // (f) Otherwise class SSE is used.
2489
2490 // Accum should never be memory (we should have returned) or
2491 // ComplexX87 (because this cannot be passed in a structure).
2492 assert((Accum != Memory && Accum != ComplexX87) &&
2493 "Invalid accumulated classification during merge.");
2494 if (Accum == Field || Field == NoClass)
2495 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002496 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002497 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002498 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002499 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002500 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002501 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002502 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2503 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002504 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002505 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002506}
2507
Chris Lattner5c740f12010-06-30 19:14:05 +00002508void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman96fd2642013-06-12 00:13:45 +00002509 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002510 // FIXME: This code can be simplified by introducing a simple value class for
2511 // Class pairs with appropriate constructor methods for the various
2512 // situations.
2513
2514 // FIXME: Some of the split computations are wrong; unaligned vectors
2515 // shouldn't be passed in registers for example, so there is no chance they
2516 // can straddle an eightbyte. Verify & simplify.
2517
2518 Lo = Hi = NoClass;
2519
2520 Class &Current = OffsetBase < 64 ? Lo : Hi;
2521 Current = Memory;
2522
John McCall9dd450b2009-09-21 23:43:11 +00002523 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002524 BuiltinType::Kind k = BT->getKind();
2525
2526 if (k == BuiltinType::Void) {
2527 Current = NoClass;
2528 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2529 Lo = Integer;
2530 Hi = Integer;
2531 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2532 Current = Integer;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002533 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002534 Current = SSE;
2535 } else if (k == BuiltinType::LongDouble) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002536 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002537 if (LDF == &llvm::APFloat::IEEEquad()) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002538 Lo = SSE;
2539 Hi = SSEUp;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002540 } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002541 Lo = X87;
2542 Hi = X87Up;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002543 } else if (LDF == &llvm::APFloat::IEEEdouble()) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002544 Current = SSE;
2545 } else
2546 llvm_unreachable("unexpected long double representation!");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002547 }
2548 // FIXME: _Decimal32 and _Decimal64 are SSE.
2549 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00002550 return;
2551 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002552
Chris Lattnerd776fb12010-06-28 21:43:59 +00002553 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002554 // Classify the underlying integer type.
Eli Friedman96fd2642013-06-12 00:13:45 +00002555 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002556 return;
2557 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002558
Chris Lattnerd776fb12010-06-28 21:43:59 +00002559 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002560 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002561 return;
2562 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002563
Chris Lattnerd776fb12010-06-28 21:43:59 +00002564 if (Ty->isMemberPointerType()) {
Jan Wen Voung01c21e82014-10-02 16:56:57 +00002565 if (Ty->isMemberFunctionPointerType()) {
2566 if (Has64BitPointers) {
2567 // If Has64BitPointers, this is an {i64, i64}, so classify both
2568 // Lo and Hi now.
2569 Lo = Hi = Integer;
2570 } else {
2571 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2572 // straddles an eightbyte boundary, Hi should be classified as well.
2573 uint64_t EB_FuncPtr = (OffsetBase) / 64;
2574 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2575 if (EB_FuncPtr != EB_ThisAdj) {
2576 Lo = Hi = Integer;
2577 } else {
2578 Current = Integer;
2579 }
2580 }
2581 } else {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00002582 Current = Integer;
Jan Wen Voung01c21e82014-10-02 16:56:57 +00002583 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00002584 return;
2585 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002586
Chris Lattnerd776fb12010-06-28 21:43:59 +00002587 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002588 uint64_t Size = getContext().getTypeSize(VT);
David Majnemerf8d14db2015-07-17 05:49:13 +00002589 if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2590 // gcc passes the following as integer:
2591 // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2592 // 2 bytes - <2 x char>, <1 x short>
2593 // 1 byte - <1 x char>
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002594 Current = Integer;
2595
2596 // If this type crosses an eightbyte boundary, it should be
2597 // split.
David Majnemerf8d14db2015-07-17 05:49:13 +00002598 uint64_t EB_Lo = (OffsetBase) / 64;
2599 uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2600 if (EB_Lo != EB_Hi)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002601 Hi = Lo;
2602 } else if (Size == 64) {
David Majnemere2ae2282016-03-04 05:26:16 +00002603 QualType ElementType = VT->getElementType();
2604
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002605 // gcc passes <1 x double> in memory. :(
David Majnemere2ae2282016-03-04 05:26:16 +00002606 if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002607 return;
2608
David Majnemere2ae2282016-03-04 05:26:16 +00002609 // gcc passes <1 x long long> as SSE but clang used to unconditionally
2610 // pass them as integer. For platforms where clang is the de facto
2611 // platform compiler, we must continue to use integer.
2612 if (!classifyIntegerMMXAsSSE() &&
2613 (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2614 ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2615 ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2616 ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002617 Current = Integer;
2618 else
2619 Current = SSE;
2620
2621 // If this type crosses an eightbyte boundary, it should be
2622 // split.
2623 if (OffsetBase && OffsetBase != 64)
2624 Hi = Lo;
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002625 } else if (Size == 128 ||
2626 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002627 // Arguments of 256-bits are split into four eightbyte chunks. The
2628 // least significant one belongs to class SSE and all the others to class
2629 // SSEUP. The original Lo and Hi design considers that types can't be
2630 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2631 // This design isn't correct for 256-bits, but since there're no cases
2632 // where the upper parts would need to be inspected, avoid adding
2633 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman96fd2642013-06-12 00:13:45 +00002634 //
2635 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2636 // registers if they are "named", i.e. not part of the "..." of a
2637 // variadic function.
Ahmed Bougacha0b938282015-06-22 21:31:43 +00002638 //
2639 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2640 // split into eight eightbyte chunks, one SSE and seven SSEUP.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002641 Lo = SSE;
2642 Hi = SSEUp;
2643 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00002644 return;
2645 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002646
Chris Lattnerd776fb12010-06-28 21:43:59 +00002647 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002648 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002649
Chris Lattner2b037972010-07-29 02:01:43 +00002650 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00002651 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002652 if (Size <= 64)
2653 Current = Integer;
2654 else if (Size <= 128)
2655 Lo = Hi = Integer;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002656 } else if (ET == getContext().FloatTy) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002657 Current = SSE;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002658 } else if (ET == getContext().DoubleTy) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002659 Lo = Hi = SSE;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002660 } else if (ET == getContext().LongDoubleTy) {
2661 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002662 if (LDF == &llvm::APFloat::IEEEquad())
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002663 Current = Memory;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002664 else if (LDF == &llvm::APFloat::x87DoubleExtended())
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002665 Current = ComplexX87;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002666 else if (LDF == &llvm::APFloat::IEEEdouble())
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002667 Lo = Hi = SSE;
2668 else
2669 llvm_unreachable("unexpected long double representation!");
2670 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002671
2672 // If this complex type crosses an eightbyte boundary then it
2673 // should be split.
2674 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00002675 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002676 if (Hi == NoClass && EB_Real != EB_Imag)
2677 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002678
Chris Lattnerd776fb12010-06-28 21:43:59 +00002679 return;
2680 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002681
Chris Lattner2b037972010-07-29 02:01:43 +00002682 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002683 // Arrays are treated like structures.
2684
Chris Lattner2b037972010-07-29 02:01:43 +00002685 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002686
2687 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
David Majnemerb229cb02016-08-15 06:39:18 +00002688 // than eight eightbytes, ..., it has class MEMORY.
2689 if (Size > 512)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002690 return;
2691
2692 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2693 // fields, it has class MEMORY.
2694 //
2695 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00002696 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002697 return;
2698
2699 // Otherwise implement simplified merge. We could be smarter about
2700 // this, but it isn't worth it and would be harder to verify.
2701 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00002702 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002703 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00002704
2705 // The only case a 256-bit wide vector could be used is when the array
2706 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2707 // to work for sizes wider than 128, early check and fallback to memory.
David Majnemerb229cb02016-08-15 06:39:18 +00002708 //
2709 if (Size > 128 &&
2710 (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00002711 return;
2712
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002713 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2714 Class FieldLo, FieldHi;
Eli Friedman96fd2642013-06-12 00:13:45 +00002715 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002716 Lo = merge(Lo, FieldLo);
2717 Hi = merge(Hi, FieldHi);
2718 if (Lo == Memory || Hi == Memory)
2719 break;
2720 }
2721
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002722 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002723 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002724 return;
2725 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002726
Chris Lattnerd776fb12010-06-28 21:43:59 +00002727 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002728 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002729
2730 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
David Majnemerb229cb02016-08-15 06:39:18 +00002731 // than eight eightbytes, ..., it has class MEMORY.
2732 if (Size > 512)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002733 return;
2734
Anders Carlsson20759ad2009-09-16 15:53:40 +00002735 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2736 // copy constructor or a non-trivial destructor, it is passed by invisible
2737 // reference.
Mark Lacey3825e832013-10-06 01:33:34 +00002738 if (getRecordArgABI(RT, getCXXABI()))
Anders Carlsson20759ad2009-09-16 15:53:40 +00002739 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002740
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002741 const RecordDecl *RD = RT->getDecl();
2742
2743 // Assume variable sized types are passed in memory.
2744 if (RD->hasFlexibleArrayMember())
2745 return;
2746
Chris Lattner2b037972010-07-29 02:01:43 +00002747 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002748
2749 // Reset Lo class, this will be recomputed.
2750 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002751
2752 // If this is a C++ record, classify the bases first.
2753 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00002754 for (const auto &I : CXXRD->bases()) {
2755 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002756 "Unexpected base class!");
2757 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00002758 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002759
2760 // Classify this field.
2761 //
2762 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2763 // single eightbyte, each is classified separately. Each eightbyte gets
2764 // initialized to class NO_CLASS.
2765 Class FieldLo, FieldHi;
Benjamin Kramer2ef30312012-07-04 18:45:14 +00002766 uint64_t Offset =
2767 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Aaron Ballman574705e2014-03-13 15:41:46 +00002768 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002769 Lo = merge(Lo, FieldLo);
2770 Hi = merge(Hi, FieldHi);
David Majnemercefbc7c2015-07-08 05:14:29 +00002771 if (Lo == Memory || Hi == Memory) {
2772 postMerge(Size, Lo, Hi);
2773 return;
2774 }
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002775 }
2776 }
2777
2778 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002779 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00002780 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002781 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002782 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2783 bool BitField = i->isBitField();
2784
David Majnemerb439dfe2016-08-15 07:20:40 +00002785 // Ignore padding bit-fields.
2786 if (BitField && i->isUnnamedBitfield())
2787 continue;
2788
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002789 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2790 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002791 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002792 // The only case a 256-bit wide vector could be used is when the struct
2793 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2794 // to work for sizes wider than 128, early check and fallback to memory.
2795 //
David Majnemerb229cb02016-08-15 06:39:18 +00002796 if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) ||
2797 Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002798 Lo = Memory;
David Majnemer699dd042015-07-08 05:07:05 +00002799 postMerge(Size, Lo, Hi);
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002800 return;
2801 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002802 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00002803 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002804 Lo = Memory;
David Majnemer699dd042015-07-08 05:07:05 +00002805 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002806 return;
2807 }
2808
2809 // Classify this field.
2810 //
2811 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2812 // exceeds a single eightbyte, each is classified
2813 // separately. Each eightbyte gets initialized to class
2814 // NO_CLASS.
2815 Class FieldLo, FieldHi;
2816
2817 // Bit-fields require special handling, they do not force the
2818 // structure to be passed in memory even if unaligned, and
2819 // therefore they can straddle an eightbyte.
2820 if (BitField) {
David Majnemerb439dfe2016-08-15 07:20:40 +00002821 assert(!i->isUnnamedBitfield());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002822 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00002823 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002824
2825 uint64_t EB_Lo = Offset / 64;
2826 uint64_t EB_Hi = (Offset + Size - 1) / 64;
Sylvestre Ledru0c4813e2013-10-06 09:54:18 +00002827
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002828 if (EB_Lo) {
2829 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2830 FieldLo = NoClass;
2831 FieldHi = Integer;
2832 } else {
2833 FieldLo = Integer;
2834 FieldHi = EB_Hi ? Integer : NoClass;
2835 }
2836 } else
Eli Friedman96fd2642013-06-12 00:13:45 +00002837 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002838 Lo = merge(Lo, FieldLo);
2839 Hi = merge(Hi, FieldHi);
2840 if (Lo == Memory || Hi == Memory)
2841 break;
2842 }
2843
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002844 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002845 }
2846}
2847
Chris Lattner22a931e2010-06-29 06:01:59 +00002848ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002849 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2850 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00002851 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002852 // Treat an enum type as its underlying type.
2853 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2854 Ty = EnumTy->getDecl()->getIntegerType();
2855
Alex Bradburye41a5e22018-01-12 20:08:16 +00002856 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
2857 : ABIArgInfo::getDirect());
Daniel Dunbar53fac692010-04-21 19:49:55 +00002858 }
2859
John McCall7f416cc2015-09-08 08:05:57 +00002860 return getNaturalAlignIndirect(Ty);
Daniel Dunbar53fac692010-04-21 19:49:55 +00002861}
2862
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002863bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2864 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2865 uint64_t Size = getContext().getTypeSize(VecTy);
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002866 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002867 if (Size <= 64 || Size > LargestVector)
2868 return true;
2869 }
2870
2871 return false;
2872}
2873
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002874ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2875 unsigned freeIntRegs) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002876 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2877 // place naturally.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002878 //
2879 // This assumption is optimistic, as there could be free registers available
2880 // when we need to pass this argument in memory, and LLVM could try to pass
2881 // the argument in the free register. This does not seem to happen currently,
2882 // but this code would be much safer if we could mark the argument with
2883 // 'onstack'. See PR12193.
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002884 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002885 // Treat an enum type as its underlying type.
2886 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2887 Ty = EnumTy->getDecl()->getIntegerType();
2888
Alex Bradburye41a5e22018-01-12 20:08:16 +00002889 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
2890 : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002891 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002892
Mark Lacey3825e832013-10-06 01:33:34 +00002893 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00002894 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson20759ad2009-09-16 15:53:40 +00002895
Chris Lattner44c2b902011-05-22 23:21:23 +00002896 // Compute the byval alignment. We specify the alignment of the byval in all
2897 // cases so that the mid-level optimizer knows the alignment of the byval.
2898 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002899
2900 // Attempt to avoid passing indirect results using byval when possible. This
2901 // is important for good codegen.
2902 //
2903 // We do this by coercing the value into a scalar type which the backend can
2904 // handle naturally (i.e., without using byval).
2905 //
2906 // For simplicity, we currently only do this when we have exhausted all of the
2907 // free integer registers. Doing this when there are free integer registers
2908 // would require more care, as we would have to ensure that the coerced value
2909 // did not claim the unused register. That would require either reording the
2910 // arguments to the function (so that any subsequent inreg values came first),
2911 // or only doing this optimization when there were no following arguments that
2912 // might be inreg.
2913 //
2914 // We currently expect it to be rare (particularly in well written code) for
2915 // arguments to be passed on the stack when there are still free integer
2916 // registers available (this would typically imply large structs being passed
2917 // by value), so this seems like a fair tradeoff for now.
2918 //
2919 // We can revisit this if the backend grows support for 'onstack' parameter
2920 // attributes. See PR12193.
2921 if (freeIntRegs == 0) {
2922 uint64_t Size = getContext().getTypeSize(Ty);
2923
2924 // If this type fits in an eightbyte, coerce it into the matching integral
2925 // type, which will end up on the stack (with alignment 8).
2926 if (Align == 8 && Size <= 64)
2927 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2928 Size));
2929 }
2930
John McCall7f416cc2015-09-08 08:05:57 +00002931 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002932}
2933
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002934/// The ABI specifies that a value should be passed in a full vector XMM/YMM
2935/// register. Pick an LLVM IR type that will be passed as a vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002936llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002937 // Wrapper structs/arrays that only contain vectors are passed just like
2938 // vectors; strip them off if present.
2939 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
2940 Ty = QualType(InnerTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002941
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002942 llvm::Type *IRType = CGT.ConvertType(Ty);
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002943 if (isa<llvm::VectorType>(IRType) ||
2944 IRType->getTypeID() == llvm::Type::FP128TyID)
Andrea Di Biagioe7347c62015-06-02 19:34:40 +00002945 return IRType;
2946
2947 // We couldn't find the preferred IR vector type for 'Ty'.
2948 uint64_t Size = getContext().getTypeSize(Ty);
David Majnemerb229cb02016-08-15 06:39:18 +00002949 assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
Andrea Di Biagioe7347c62015-06-02 19:34:40 +00002950
2951 // Return a LLVM IR vector type based on the size of 'Ty'.
2952 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
2953 Size / 64);
Chris Lattner4200fe42010-07-29 04:56:46 +00002954}
2955
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002956/// BitsContainNoUserData - Return true if the specified [start,end) bit range
2957/// is known to either be off the end of the specified type or being in
2958/// alignment padding. The user type specified is known to be at most 128 bits
2959/// in size, and have passed through X86_64ABIInfo::classify with a successful
2960/// classification that put one of the two halves in the INTEGER class.
2961///
2962/// It is conservatively correct to return false.
2963static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2964 unsigned EndBit, ASTContext &Context) {
2965 // If the bytes being queried are off the end of the type, there is no user
2966 // data hiding here. This handles analysis of builtins, vectors and other
2967 // types that don't contain interesting padding.
2968 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2969 if (TySize <= StartBit)
2970 return true;
2971
Chris Lattner98076a22010-07-29 07:43:55 +00002972 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2973 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2974 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2975
2976 // Check each element to see if the element overlaps with the queried range.
2977 for (unsigned i = 0; i != NumElts; ++i) {
2978 // If the element is after the span we care about, then we're done..
2979 unsigned EltOffset = i*EltSize;
2980 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002981
Chris Lattner98076a22010-07-29 07:43:55 +00002982 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2983 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
2984 EndBit-EltOffset, Context))
2985 return false;
2986 }
2987 // If it overlaps no elements, then it is safe to process as padding.
2988 return true;
2989 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002990
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002991 if (const RecordType *RT = Ty->getAs<RecordType>()) {
2992 const RecordDecl *RD = RT->getDecl();
2993 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002994
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002995 // If this is a C++ record, check the bases first.
2996 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00002997 for (const auto &I : CXXRD->bases()) {
2998 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002999 "Unexpected base class!");
3000 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00003001 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003002
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003003 // If the base is after the span we care about, ignore it.
Benjamin Kramer2ef30312012-07-04 18:45:14 +00003004 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003005 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003006
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003007 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
Aaron Ballman574705e2014-03-13 15:41:46 +00003008 if (!BitsContainNoUserData(I.getType(), BaseStart,
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003009 EndBit-BaseOffset, Context))
3010 return false;
3011 }
3012 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003013
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003014 // Verify that no field has data that overlaps the region of interest. Yes
3015 // this could be sped up a lot by being smarter about queried fields,
3016 // however we're only looking at structs up to 16 bytes, so we don't care
3017 // much.
3018 unsigned idx = 0;
3019 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3020 i != e; ++i, ++idx) {
3021 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003022
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003023 // If we found a field after the region we care about, then we're done.
3024 if (FieldOffset >= EndBit) break;
3025
3026 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
3027 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
3028 Context))
3029 return false;
3030 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003031
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003032 // If nothing in this record overlapped the area of interest, then we're
3033 // clean.
3034 return true;
3035 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003036
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003037 return false;
3038}
3039
Chris Lattnere556a712010-07-29 18:39:32 +00003040/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
3041/// float member at the specified offset. For example, {int,{float}} has a
3042/// float at offset 4. It is conservatively correct for this routine to return
3043/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00003044static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmowdd31ca12012-10-08 16:25:52 +00003045 const llvm::DataLayout &TD) {
Chris Lattnere556a712010-07-29 18:39:32 +00003046 // Base case if we find a float.
3047 if (IROffset == 0 && IRType->isFloatTy())
3048 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003049
Chris Lattnere556a712010-07-29 18:39:32 +00003050 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00003051 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00003052 const llvm::StructLayout *SL = TD.getStructLayout(STy);
3053 unsigned Elt = SL->getElementContainingOffset(IROffset);
3054 IROffset -= SL->getElementOffset(Elt);
3055 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
3056 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003057
Chris Lattnere556a712010-07-29 18:39:32 +00003058 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00003059 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3060 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00003061 unsigned EltSize = TD.getTypeAllocSize(EltTy);
3062 IROffset -= IROffset/EltSize*EltSize;
3063 return ContainsFloatAtOffset(EltTy, IROffset, TD);
3064 }
3065
3066 return false;
3067}
3068
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003069
3070/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3071/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003072llvm::Type *X86_64ABIInfo::
3073GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003074 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00003075 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003076 // pass as float if the last 4 bytes is just padding. This happens for
3077 // structs that contain 3 floats.
3078 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
3079 SourceOffset*8+64, getContext()))
3080 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003081
Chris Lattnere556a712010-07-29 18:39:32 +00003082 // We want to pass as <2 x float> if the LLVM IR type contains a float at
3083 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
3084 // case.
Micah Villmowdd31ca12012-10-08 16:25:52 +00003085 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
3086 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner9f8b4512010-08-25 23:39:14 +00003087 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003088
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003089 return llvm::Type::getDoubleTy(getVMContext());
3090}
3091
3092
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003093/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3094/// an 8-byte GPR. This means that we either have a scalar or we are talking
3095/// about the high or low part of an up-to-16-byte struct. This routine picks
3096/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003097/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3098/// etc).
3099///
3100/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3101/// the source type. IROffset is an offset in bytes into the LLVM IR type that
3102/// the 8-byte value references. PrefType may be null.
3103///
Alp Toker9907f082014-07-09 14:06:35 +00003104/// SourceTy is the source-level type for the entire argument. SourceOffset is
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003105/// an offset into this that we're processing (which is always either 0 or 8).
3106///
Chris Lattnera5f58b02011-07-09 17:41:47 +00003107llvm::Type *X86_64ABIInfo::
3108GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003109 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003110 // If we're dealing with an un-offset LLVM IR type, then it means that we're
3111 // returning an 8-byte unit starting with it. See if we can safely use it.
3112 if (IROffset == 0) {
3113 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffc7dd7222012-10-11 15:52:22 +00003114 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3115 IRType->isIntegerTy(64))
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003116 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003117
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003118 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3119 // goodness in the source type is just tail padding. This is allowed to
3120 // kick in for struct {double,int} on the int, but not on
3121 // struct{double,int,int} because we wouldn't return the second int. We
3122 // have to do this analysis on the source type because we can't depend on
3123 // unions being lowered a specific way etc.
3124 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffc7dd7222012-10-11 15:52:22 +00003125 IRType->isIntegerTy(32) ||
3126 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3127 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3128 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003129
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003130 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3131 SourceOffset*8+64, getContext()))
3132 return IRType;
3133 }
3134 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003135
Chris Lattner2192fe52011-07-18 04:24:23 +00003136 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003137 // If this is a struct, recurse into the field at the specified offset.
Micah Villmowdd31ca12012-10-08 16:25:52 +00003138 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003139 if (IROffset < SL->getSizeInBytes()) {
3140 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3141 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003142
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003143 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3144 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003145 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003146 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003147
Chris Lattner2192fe52011-07-18 04:24:23 +00003148 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003149 llvm::Type *EltTy = ATy->getElementType();
Micah Villmowdd31ca12012-10-08 16:25:52 +00003150 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner98076a22010-07-29 07:43:55 +00003151 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003152 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3153 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00003154 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003155
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003156 // Okay, we don't have any better idea of what to pass, so we pass this in an
3157 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00003158 unsigned TySizeInBytes =
3159 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003160
Chris Lattner3f763422010-07-29 17:34:39 +00003161 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003162
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003163 // It is always safe to classify this as an integer type up to i64 that
3164 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00003165 return llvm::IntegerType::get(getVMContext(),
3166 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00003167}
3168
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003169
3170/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3171/// be used as elements of a two register pair to pass or return, return a
3172/// first class aggregate to represent them. For example, if the low part of
3173/// a by-value argument should be passed as i32* and the high part as float,
3174/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003175static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00003176GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmowdd31ca12012-10-08 16:25:52 +00003177 const llvm::DataLayout &TD) {
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003178 // In order to correctly satisfy the ABI, we need to the high part to start
3179 // at offset 8. If the high and low parts we inferred are both 4-byte types
3180 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3181 // the second element at offset 8. Check for this:
3182 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3183 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003184 unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003185 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003186
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003187 // To handle this, we have to increase the size of the low part so that the
3188 // second element will start at an 8 byte offset. We can't increase the size
3189 // of the second element because it might make us access off the end of the
3190 // struct.
3191 if (HiStart != 8) {
Derek Schuff5ec51282015-06-24 22:36:38 +00003192 // There are usually two sorts of types the ABI generation code can produce
3193 // for the low part of a pair that aren't 8 bytes in size: float or
3194 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
3195 // NaCl).
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003196 // Promote these to a larger type.
3197 if (Lo->isFloatTy())
3198 Lo = llvm::Type::getDoubleTy(Lo->getContext());
3199 else {
Derek Schuff3c6a48d2015-06-24 22:36:36 +00003200 assert((Lo->isIntegerTy() || Lo->isPointerTy())
3201 && "Invalid/unknown lo type");
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003202 Lo = llvm::Type::getInt64Ty(Lo->getContext());
3203 }
3204 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003205
Serge Guelton1d993272017-05-09 19:31:30 +00003206 llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003207
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003208 // Verify that the second element is at an 8-byte offset.
3209 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3210 "Invalid x86-64 argument pair!");
3211 return Result;
3212}
3213
Chris Lattner31faff52010-07-28 23:06:14 +00003214ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00003215classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00003216 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3217 // classification algorithm.
3218 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00003219 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner31faff52010-07-28 23:06:14 +00003220
3221 // Check some invariants.
3222 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00003223 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3224
Craig Topper8a13c412014-05-21 05:09:00 +00003225 llvm::Type *ResType = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00003226 switch (Lo) {
3227 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003228 if (Hi == NoClass)
3229 return ABIArgInfo::getIgnore();
3230 // If the low part is just padding, it takes no register, leave ResType
3231 // null.
3232 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3233 "Unknown missing lo part");
3234 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003235
3236 case SSEUp:
3237 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00003238 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00003239
3240 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3241 // hidden argument.
3242 case Memory:
3243 return getIndirectReturnResult(RetTy);
3244
3245 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3246 // available register of the sequence %rax, %rdx is used.
3247 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003248 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003249
Chris Lattner1f3a0632010-07-29 21:42:50 +00003250 // If we have a sign or zero extended integer, make sure to return Extend
3251 // so that the parameter gets the right LLVM IR attributes.
3252 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3253 // Treat an enum type as its underlying type.
3254 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3255 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003256
Chris Lattner1f3a0632010-07-29 21:42:50 +00003257 if (RetTy->isIntegralOrEnumerationType() &&
3258 RetTy->isPromotableIntegerType())
Alex Bradburye41a5e22018-01-12 20:08:16 +00003259 return ABIArgInfo::getExtend(RetTy);
Chris Lattner1f3a0632010-07-29 21:42:50 +00003260 }
Chris Lattner31faff52010-07-28 23:06:14 +00003261 break;
3262
3263 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3264 // available SSE register of the sequence %xmm0, %xmm1 is used.
3265 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003266 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003267 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003268
3269 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3270 // returned on the X87 stack in %st0 as 80-bit x87 number.
3271 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00003272 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003273 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003274
3275 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3276 // part of the value is returned in %st0 and the imaginary part in
3277 // %st1.
3278 case ComplexX87:
3279 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00003280 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Serge Guelton1d993272017-05-09 19:31:30 +00003281 llvm::Type::getX86_FP80Ty(getVMContext()));
Chris Lattner31faff52010-07-28 23:06:14 +00003282 break;
3283 }
3284
Craig Topper8a13c412014-05-21 05:09:00 +00003285 llvm::Type *HighPart = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00003286 switch (Hi) {
3287 // Memory was handled previously and X87 should
3288 // never occur as a hi class.
3289 case Memory:
3290 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00003291 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00003292
3293 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003294 case NoClass:
3295 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003296
Chris Lattner52b3c132010-09-01 00:20:33 +00003297 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003298 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003299 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3300 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00003301 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00003302 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003303 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003304 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3305 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00003306 break;
3307
3308 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003309 // is passed in the next available eightbyte chunk if the last used
3310 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00003311 //
Chris Lattner57540c52011-04-15 05:22:18 +00003312 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00003313 case SSEUp:
3314 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003315 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00003316 break;
3317
3318 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3319 // returned together with the previous X87 value in %st0.
3320 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00003321 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00003322 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00003323 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00003324 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00003325 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003326 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003327 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3328 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00003329 }
Chris Lattner31faff52010-07-28 23:06:14 +00003330 break;
3331 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003332
Chris Lattner52b3c132010-09-01 00:20:33 +00003333 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003334 // known to pass in the high eightbyte of the result. We do this by forming a
3335 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003336 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00003337 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner31faff52010-07-28 23:06:14 +00003338
Chris Lattner1f3a0632010-07-29 21:42:50 +00003339 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00003340}
3341
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003342ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman96fd2642013-06-12 00:13:45 +00003343 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3344 bool isNamedArg)
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003345 const
3346{
Reid Klecknerb1be6832014-11-15 01:41:41 +00003347 Ty = useFirstFieldIfTransparentUnion(Ty);
3348
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003349 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00003350 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003351
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003352 // Check some invariants.
3353 // FIXME: Enforce these by construction.
3354 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003355 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3356
3357 neededInt = 0;
3358 neededSSE = 0;
Craig Topper8a13c412014-05-21 05:09:00 +00003359 llvm::Type *ResType = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003360 switch (Lo) {
3361 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003362 if (Hi == NoClass)
3363 return ABIArgInfo::getIgnore();
3364 // If the low part is just padding, it takes no register, leave ResType
3365 // null.
3366 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3367 "Unknown missing lo part");
3368 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003369
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003370 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3371 // on the stack.
3372 case Memory:
3373
3374 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3375 // COMPLEX_X87, it is passed in memory.
3376 case X87:
3377 case ComplexX87:
Mark Lacey3825e832013-10-06 01:33:34 +00003378 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
Eli Friedman4774b7e2011-06-29 07:04:55 +00003379 ++neededInt;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003380 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003381
3382 case SSEUp:
3383 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00003384 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003385
3386 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3387 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3388 // and %r9 is used.
3389 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00003390 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003391
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003392 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003393 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00003394
3395 // If we have a sign or zero extended integer, make sure to return Extend
3396 // so that the parameter gets the right LLVM IR attributes.
3397 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3398 // Treat an enum type as its underlying type.
3399 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3400 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003401
Chris Lattner1f3a0632010-07-29 21:42:50 +00003402 if (Ty->isIntegralOrEnumerationType() &&
3403 Ty->isPromotableIntegerType())
Alex Bradburye41a5e22018-01-12 20:08:16 +00003404 return ABIArgInfo::getExtend(Ty);
Chris Lattner1f3a0632010-07-29 21:42:50 +00003405 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003406
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003407 break;
3408
3409 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3410 // available SSE register is used, the registers are taken in the
3411 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00003412 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003413 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00003414 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00003415 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003416 break;
3417 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00003418 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003419
Craig Topper8a13c412014-05-21 05:09:00 +00003420 llvm::Type *HighPart = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003421 switch (Hi) {
3422 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00003423 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003424 // which is passed in memory.
3425 case Memory:
3426 case X87:
3427 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00003428 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003429
3430 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003431
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003432 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003433 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003434 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003435 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003436
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003437 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3438 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003439 break;
3440
3441 // X87Up generally doesn't occur here (long double is passed in
3442 // memory), except in situations involving unions.
3443 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003444 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003445 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003446
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003447 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3448 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003449
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003450 ++neededSSE;
3451 break;
3452
3453 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3454 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003455 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003456 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00003457 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003458 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003459 break;
3460 }
3461
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003462 // If a high part was specified, merge it together with the low part. It is
3463 // known to pass in the high eightbyte of the result. We do this by forming a
3464 // first class struct aggregate with the high and low part: {low, high}
3465 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00003466 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003467
Chris Lattner1f3a0632010-07-29 21:42:50 +00003468 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003469}
3470
Erich Keane757d3172016-11-02 18:29:35 +00003471ABIArgInfo
3472X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3473 unsigned &NeededSSE) const {
3474 auto RT = Ty->getAs<RecordType>();
3475 assert(RT && "classifyRegCallStructType only valid with struct types");
3476
3477 if (RT->getDecl()->hasFlexibleArrayMember())
3478 return getIndirectReturnResult(Ty);
3479
3480 // Sum up bases
3481 if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3482 if (CXXRD->isDynamicClass()) {
3483 NeededInt = NeededSSE = 0;
3484 return getIndirectReturnResult(Ty);
3485 }
3486
3487 for (const auto &I : CXXRD->bases())
3488 if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
3489 .isIndirect()) {
3490 NeededInt = NeededSSE = 0;
3491 return getIndirectReturnResult(Ty);
3492 }
3493 }
3494
3495 // Sum up members
3496 for (const auto *FD : RT->getDecl()->fields()) {
3497 if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
3498 if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
3499 .isIndirect()) {
3500 NeededInt = NeededSSE = 0;
3501 return getIndirectReturnResult(Ty);
3502 }
3503 } else {
3504 unsigned LocalNeededInt, LocalNeededSSE;
3505 if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
3506 LocalNeededSSE, true)
3507 .isIndirect()) {
3508 NeededInt = NeededSSE = 0;
3509 return getIndirectReturnResult(Ty);
3510 }
3511 NeededInt += LocalNeededInt;
3512 NeededSSE += LocalNeededSSE;
3513 }
3514 }
3515
3516 return ABIArgInfo::getDirect();
3517}
3518
3519ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
3520 unsigned &NeededInt,
3521 unsigned &NeededSSE) const {
3522
3523 NeededInt = 0;
3524 NeededSSE = 0;
3525
3526 return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
3527}
3528
Chris Lattner22326a12010-07-29 02:31:05 +00003529void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003530
Alexander Ivchenko4b20b3c2018-02-08 11:15:21 +00003531 const unsigned CallingConv = FI.getCallingConvention();
3532 // It is possible to force Win64 calling convention on any x86_64 target by
3533 // using __attribute__((ms_abi)). In such case to correctly emit Win64
3534 // compatible code delegate this call to WinX86_64ABIInfo::computeInfo.
3535 if (CallingConv == llvm::CallingConv::Win64) {
3536 WinX86_64ABIInfo Win64ABIInfo(CGT);
3537 Win64ABIInfo.computeInfo(FI);
3538 return;
3539 }
3540
3541 bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003542
3543 // Keep track of the number of assigned registers.
Erich Keane757d3172016-11-02 18:29:35 +00003544 unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3545 unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3546 unsigned NeededInt, NeededSSE;
3547
Akira Hatanakabe7daa32018-03-12 17:05:06 +00003548 if (!getCXXABI().classifyReturnType(FI)) {
Erich Keanede1b2a92017-07-21 18:50:36 +00003549 if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3550 !FI.getReturnType()->getTypePtr()->isUnionType()) {
3551 FI.getReturnInfo() =
3552 classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
3553 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3554 FreeIntRegs -= NeededInt;
3555 FreeSSERegs -= NeededSSE;
3556 } else {
3557 FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3558 }
3559 } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) {
3560 // Complex Long Double Type is passed in Memory when Regcall
3561 // calling convention is used.
3562 const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>();
3563 if (getContext().getCanonicalType(CT->getElementType()) ==
3564 getContext().LongDoubleTy)
3565 FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3566 } else
3567 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3568 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003569
3570 // If the return value is indirect, then the hidden argument is consuming one
3571 // integer register.
3572 if (FI.getReturnInfo().isIndirect())
Erich Keane757d3172016-11-02 18:29:35 +00003573 --FreeIntRegs;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003574
Peter Collingbournef7706832014-12-12 23:41:25 +00003575 // The chain argument effectively gives us another free register.
3576 if (FI.isChainCall())
Erich Keane757d3172016-11-02 18:29:35 +00003577 ++FreeIntRegs;
Peter Collingbournef7706832014-12-12 23:41:25 +00003578
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003579 unsigned NumRequiredArgs = FI.getNumRequiredArgs();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003580 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3581 // get assigned (in left-to-right order) for passing as follows...
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003582 unsigned ArgNo = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003583 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003584 it != ie; ++it, ++ArgNo) {
3585 bool IsNamedArg = ArgNo < NumRequiredArgs;
Eli Friedman96fd2642013-06-12 00:13:45 +00003586
Erich Keane757d3172016-11-02 18:29:35 +00003587 if (IsRegCall && it->type->isStructureOrClassType())
3588 it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
3589 else
3590 it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
3591 NeededSSE, IsNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003592
3593 // AMD64-ABI 3.2.3p3: If there are no registers available for any
3594 // eightbyte of an argument, the whole argument is passed on the
3595 // stack. If registers have already been assigned for some
3596 // eightbytes of such an argument, the assignments get reverted.
Erich Keane757d3172016-11-02 18:29:35 +00003597 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3598 FreeIntRegs -= NeededInt;
3599 FreeSSERegs -= NeededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003600 } else {
Erich Keane757d3172016-11-02 18:29:35 +00003601 it->info = getIndirectResult(it->type, FreeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003602 }
3603 }
3604}
3605
John McCall7f416cc2015-09-08 08:05:57 +00003606static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3607 Address VAListAddr, QualType Ty) {
3608 Address overflow_arg_area_p = CGF.Builder.CreateStructGEP(
3609 VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003610 llvm::Value *overflow_arg_area =
3611 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3612
3613 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3614 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00003615 // It isn't stated explicitly in the standard, but in practice we use
3616 // alignment greater than 16 where necessary.
Petar Jovanovic402257b2015-12-04 00:26:47 +00003617 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3618 if (Align > CharUnits::fromQuantity(8)) {
3619 overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3620 Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003621 }
3622
3623 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00003624 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003625 llvm::Value *Res =
3626 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003627 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003628
3629 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3630 // l->overflow_arg_area + sizeof(type).
3631 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3632 // an 8 byte boundary.
3633
3634 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00003635 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00003636 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003637 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3638 "overflow_arg_area.next");
3639 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3640
3641 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
Petar Jovanovic402257b2015-12-04 00:26:47 +00003642 return Address(Res, Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003643}
3644
John McCall7f416cc2015-09-08 08:05:57 +00003645Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3646 QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003647 // Assume that va_list type is correct; should be pointer to LLVM type:
3648 // struct {
3649 // i32 gp_offset;
3650 // i32 fp_offset;
3651 // i8* overflow_arg_area;
3652 // i8* reg_save_area;
3653 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00003654 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003655
John McCall7f416cc2015-09-08 08:05:57 +00003656 Ty = getContext().getCanonicalType(Ty);
Eric Christopher7565e0d2015-05-29 23:09:49 +00003657 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
Eli Friedman96fd2642013-06-12 00:13:45 +00003658 /*isNamedArg*/false);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003659
3660 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3661 // in the registers. If not go to step 7.
3662 if (!neededInt && !neededSSE)
John McCall7f416cc2015-09-08 08:05:57 +00003663 return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003664
3665 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3666 // general purpose registers needed to pass type and num_fp to hold
3667 // the number of floating point registers needed.
3668
3669 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3670 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3671 // l->fp_offset > 304 - num_fp * 16 go to step 7.
3672 //
3673 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3674 // register save space).
3675
Craig Topper8a13c412014-05-21 05:09:00 +00003676 llvm::Value *InRegs = nullptr;
John McCall7f416cc2015-09-08 08:05:57 +00003677 Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3678 llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003679 if (neededInt) {
David Blaikie1ed728c2015-04-05 22:45:47 +00003680 gp_offset_p =
John McCall7f416cc2015-09-08 08:05:57 +00003681 CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(),
3682 "gp_offset_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003683 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00003684 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3685 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003686 }
3687
3688 if (neededSSE) {
David Blaikie1ed728c2015-04-05 22:45:47 +00003689 fp_offset_p =
John McCall7f416cc2015-09-08 08:05:57 +00003690 CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4),
3691 "fp_offset_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003692 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3693 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00003694 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3695 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003696 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3697 }
3698
3699 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3700 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3701 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3702 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3703
3704 // Emit code to load the value if it was passed in registers.
3705
3706 CGF.EmitBlock(InRegBlock);
3707
3708 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3709 // an offset of l->gp_offset and/or l->fp_offset. This may require
3710 // copying to a temporary location in case the parameter is passed
3711 // in different register classes or requires an alignment greater
3712 // than 8 for general purpose registers and 16 for XMM registers.
3713 //
3714 // FIXME: This really results in shameful code when we end up needing to
3715 // collect arguments from different places; often what should result in a
3716 // simple assembling of a structure from scattered addresses has many more
3717 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00003718 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00003719 llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
3720 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)),
3721 "reg_save_area");
3722
3723 Address RegAddr = Address::invalid();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003724 if (neededInt && neededSSE) {
3725 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003726 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003727 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
John McCall7f416cc2015-09-08 08:05:57 +00003728 Address Tmp = CGF.CreateMemTemp(Ty);
3729 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003730 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003731 llvm::Type *TyLo = ST->getElementType(0);
3732 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00003733 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003734 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003735 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
3736 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
John McCall7f416cc2015-09-08 08:05:57 +00003737 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
3738 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
Rafael Espindola0a500af2014-06-24 20:01:50 +00003739 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
3740 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003741
John McCall7f416cc2015-09-08 08:05:57 +00003742 // Copy the first element.
Peter Collingbourneb367c562016-11-28 22:30:21 +00003743 // FIXME: Our choice of alignment here and below is probably pessimistic.
3744 llvm::Value *V = CGF.Builder.CreateAlignedLoad(
3745 TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
3746 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
John McCall7f416cc2015-09-08 08:05:57 +00003747 CGF.Builder.CreateStore(V,
3748 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3749
3750 // Copy the second element.
Peter Collingbourneb367c562016-11-28 22:30:21 +00003751 V = CGF.Builder.CreateAlignedLoad(
3752 TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
3753 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
John McCall7f416cc2015-09-08 08:05:57 +00003754 CharUnits Offset = CharUnits::fromQuantity(
3755 getDataLayout().getStructLayout(ST)->getElementOffset(1));
3756 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset));
3757
3758 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003759 } else if (neededInt) {
John McCall7f416cc2015-09-08 08:05:57 +00003760 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
3761 CharUnits::fromQuantity(8));
3762 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003763
3764 // Copy to a temporary if necessary to ensure the appropriate alignment.
3765 std::pair<CharUnits, CharUnits> SizeAlign =
John McCall7f416cc2015-09-08 08:05:57 +00003766 getContext().getTypeInfoInChars(Ty);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003767 uint64_t TySize = SizeAlign.first.getQuantity();
John McCall7f416cc2015-09-08 08:05:57 +00003768 CharUnits TyAlign = SizeAlign.second;
3769
3770 // Copy into a temporary if the type is more aligned than the
3771 // register save area.
3772 if (TyAlign.getQuantity() > 8) {
3773 Address Tmp = CGF.CreateMemTemp(Ty);
3774 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003775 RegAddr = Tmp;
3776 }
John McCall7f416cc2015-09-08 08:05:57 +00003777
Chris Lattner0cf24192010-06-28 20:05:43 +00003778 } else if (neededSSE == 1) {
John McCall7f416cc2015-09-08 08:05:57 +00003779 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3780 CharUnits::fromQuantity(16));
3781 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003782 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00003783 assert(neededSSE == 2 && "Invalid number of needed registers!");
3784 // SSE registers are spaced 16 bytes apart in the register save
3785 // area, we need to collect the two eightbytes together.
John McCall7f416cc2015-09-08 08:05:57 +00003786 // The ABI isn't explicit about this, but it seems reasonable
3787 // to assume that the slots are 16-byte aligned, since the stack is
3788 // naturally 16-byte aligned and the prologue is expected to store
3789 // all the SSE registers to the RSA.
3790 Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3791 CharUnits::fromQuantity(16));
3792 Address RegAddrHi =
3793 CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
3794 CharUnits::fromQuantity(16));
Erich Keane24e68402018-02-02 15:53:35 +00003795 llvm::Type *ST = AI.canHaveCoerceToType()
3796 ? AI.getCoerceToType()
3797 : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy);
John McCall7f416cc2015-09-08 08:05:57 +00003798 llvm::Value *V;
3799 Address Tmp = CGF.CreateMemTemp(Ty);
3800 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
Erich Keane24e68402018-02-02 15:53:35 +00003801 V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
3802 RegAddrLo, ST->getStructElementType(0)));
John McCall7f416cc2015-09-08 08:05:57 +00003803 CGF.Builder.CreateStore(V,
3804 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
Erich Keane24e68402018-02-02 15:53:35 +00003805 V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
3806 RegAddrHi, ST->getStructElementType(1)));
John McCall7f416cc2015-09-08 08:05:57 +00003807 CGF.Builder.CreateStore(V,
3808 CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8)));
3809
3810 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003811 }
3812
3813 // AMD64-ABI 3.5.7p5: Step 5. Set:
3814 // l->gp_offset = l->gp_offset + num_gp * 8
3815 // l->fp_offset = l->fp_offset + num_fp * 16.
3816 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003817 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003818 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3819 gp_offset_p);
3820 }
3821 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003822 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003823 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3824 fp_offset_p);
3825 }
3826 CGF.EmitBranch(ContBlock);
3827
3828 // Emit code to load the value if it was passed in memory.
3829
3830 CGF.EmitBlock(InMemBlock);
John McCall7f416cc2015-09-08 08:05:57 +00003831 Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003832
3833 // Return the appropriate result.
3834
3835 CGF.EmitBlock(ContBlock);
John McCall7f416cc2015-09-08 08:05:57 +00003836 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
3837 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003838 return ResAddr;
3839}
3840
Charles Davisc7d5c942015-09-17 20:55:33 +00003841Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
3842 QualType Ty) const {
3843 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3844 CGF.getContext().getTypeInfoInChars(Ty),
3845 CharUnits::fromQuantity(8),
3846 /*allowHigherAlign*/ false);
3847}
3848
Erich Keane521ed962017-01-05 00:20:51 +00003849ABIArgInfo
3850WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
3851 const ABIArgInfo &current) const {
3852 // Assumes vectorCall calling convention.
3853 const Type *Base = nullptr;
3854 uint64_t NumElts = 0;
3855
3856 if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
3857 isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
3858 FreeSSERegs -= NumElts;
3859 return getDirectX86Hva();
3860 }
3861 return current;
3862}
3863
Reid Kleckner80944df2014-10-31 22:00:51 +00003864ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
Erich Keane521ed962017-01-05 00:20:51 +00003865 bool IsReturnType, bool IsVectorCall,
3866 bool IsRegCall) const {
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003867
3868 if (Ty->isVoidType())
3869 return ABIArgInfo::getIgnore();
3870
3871 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3872 Ty = EnumTy->getDecl()->getIntegerType();
3873
Reid Kleckner80944df2014-10-31 22:00:51 +00003874 TypeInfo Info = getContext().getTypeInfo(Ty);
3875 uint64_t Width = Info.Width;
Reid Kleckner11a17192015-10-28 22:29:52 +00003876 CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003877
Reid Kleckner9005f412014-05-02 00:51:20 +00003878 const RecordType *RT = Ty->getAs<RecordType>();
3879 if (RT) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00003880 if (!IsReturnType) {
Mark Lacey3825e832013-10-06 01:33:34 +00003881 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00003882 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00003883 }
3884
3885 if (RT->getDecl()->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00003886 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003887
Reid Kleckner9005f412014-05-02 00:51:20 +00003888 }
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00003889
Reid Kleckner80944df2014-10-31 22:00:51 +00003890 const Type *Base = nullptr;
3891 uint64_t NumElts = 0;
Erich Keane521ed962017-01-05 00:20:51 +00003892 // vectorcall adds the concept of a homogenous vector aggregate, similar to
3893 // other targets.
3894 if ((IsVectorCall || IsRegCall) &&
3895 isHomogeneousAggregate(Ty, Base, NumElts)) {
3896 if (IsRegCall) {
3897 if (FreeSSERegs >= NumElts) {
3898 FreeSSERegs -= NumElts;
3899 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3900 return ABIArgInfo::getDirect();
3901 return ABIArgInfo::getExpand();
3902 }
3903 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3904 } else if (IsVectorCall) {
3905 if (FreeSSERegs >= NumElts &&
3906 (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
3907 FreeSSERegs -= NumElts;
Reid Kleckner80944df2014-10-31 22:00:51 +00003908 return ABIArgInfo::getDirect();
Erich Keane521ed962017-01-05 00:20:51 +00003909 } else if (IsReturnType) {
3910 return ABIArgInfo::getExpand();
3911 } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
3912 // HVAs are delayed and reclassified in the 2nd step.
3913 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3914 }
Reid Kleckner80944df2014-10-31 22:00:51 +00003915 }
Reid Kleckner80944df2014-10-31 22:00:51 +00003916 }
3917
Reid Klecknerec87fec2014-05-02 01:17:12 +00003918 if (Ty->isMemberPointerType()) {
Reid Kleckner7f5f0f32014-05-02 01:14:59 +00003919 // If the member pointer is represented by an LLVM int or ptr, pass it
3920 // directly.
3921 llvm::Type *LLTy = CGT.ConvertType(Ty);
3922 if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3923 return ABIArgInfo::getDirect();
Reid Kleckner9005f412014-05-02 00:51:20 +00003924 }
3925
Michael Kuperstein4f818702015-02-24 09:35:58 +00003926 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00003927 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3928 // not 1, 2, 4, or 8 bytes, must be passed by reference."
Reid Kleckner80944df2014-10-31 22:00:51 +00003929 if (Width > 64 || !llvm::isPowerOf2_64(Width))
John McCall7f416cc2015-09-08 08:05:57 +00003930 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003931
Reid Kleckner9005f412014-05-02 00:51:20 +00003932 // Otherwise, coerce it to a small integer.
Reid Kleckner80944df2014-10-31 22:00:51 +00003933 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003934 }
3935
Julien Lerouge10dcff82014-08-27 00:36:55 +00003936 // Bool type is always extended to the ABI, other builtin types are not
3937 // extended.
3938 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3939 if (BT && BT->getKind() == BuiltinType::Bool)
Alex Bradburye41a5e22018-01-12 20:08:16 +00003940 return ABIArgInfo::getExtend(Ty);
Julien Lerougee8d34fa2014-08-26 22:11:53 +00003941
Reid Kleckner11a17192015-10-28 22:29:52 +00003942 // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It
3943 // passes them indirectly through memory.
3944 if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) {
3945 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003946 if (LDF == &llvm::APFloat::x87DoubleExtended())
Reid Kleckner11a17192015-10-28 22:29:52 +00003947 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3948 }
3949
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003950 return ABIArgInfo::getDirect();
3951}
3952
Erich Keane521ed962017-01-05 00:20:51 +00003953void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI,
3954 unsigned FreeSSERegs,
3955 bool IsVectorCall,
3956 bool IsRegCall) const {
3957 unsigned Count = 0;
3958 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00003959 // Vectorcall in x64 only permits the first 6 arguments to be passed
3960 // as XMM/YMM registers.
Erich Keane521ed962017-01-05 00:20:51 +00003961 if (Count < VectorcallMaxParamNumAsReg)
3962 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
3963 else {
3964 // Since these cannot be passed in registers, pretend no registers
3965 // are left.
3966 unsigned ZeroSSERegsAvail = 0;
3967 I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false,
3968 IsVectorCall, IsRegCall);
3969 }
3970 ++Count;
3971 }
3972
Erich Keane521ed962017-01-05 00:20:51 +00003973 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00003974 I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info);
Erich Keane521ed962017-01-05 00:20:51 +00003975 }
3976}
3977
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003978void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner80944df2014-10-31 22:00:51 +00003979 bool IsVectorCall =
3980 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
Erich Keane757d3172016-11-02 18:29:35 +00003981 bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall;
Reid Kleckner37abaca2014-05-09 22:46:15 +00003982
Erich Keane757d3172016-11-02 18:29:35 +00003983 unsigned FreeSSERegs = 0;
3984 if (IsVectorCall) {
3985 // We can use up to 4 SSE return registers with vectorcall.
3986 FreeSSERegs = 4;
3987 } else if (IsRegCall) {
3988 // RegCall gives us 16 SSE registers.
3989 FreeSSERegs = 16;
3990 }
3991
Reid Kleckner80944df2014-10-31 22:00:51 +00003992 if (!getCXXABI().classifyReturnType(FI))
Erich Keane521ed962017-01-05 00:20:51 +00003993 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
3994 IsVectorCall, IsRegCall);
Reid Kleckner80944df2014-10-31 22:00:51 +00003995
Erich Keane757d3172016-11-02 18:29:35 +00003996 if (IsVectorCall) {
3997 // We can use up to 6 SSE register parameters with vectorcall.
3998 FreeSSERegs = 6;
3999 } else if (IsRegCall) {
Erich Keane521ed962017-01-05 00:20:51 +00004000 // RegCall gives us 16 SSE registers, we can reuse the return registers.
Erich Keane757d3172016-11-02 18:29:35 +00004001 FreeSSERegs = 16;
4002 }
4003
Erich Keane521ed962017-01-05 00:20:51 +00004004 if (IsVectorCall) {
4005 computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall);
4006 } else {
4007 for (auto &I : FI.arguments())
4008 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
4009 }
4010
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00004011}
4012
John McCall7f416cc2015-09-08 08:05:57 +00004013Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4014 QualType Ty) const {
Reid Klecknerb04449d2016-08-25 20:42:26 +00004015
4016 bool IsIndirect = false;
4017
4018 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4019 // not 1, 2, 4, or 8 bytes, must be passed by reference."
4020 if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) {
4021 uint64_t Width = getContext().getTypeSize(Ty);
4022 IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4023 }
4024
4025 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
John McCall7f416cc2015-09-08 08:05:57 +00004026 CGF.getContext().getTypeInfoInChars(Ty),
4027 CharUnits::fromQuantity(8),
4028 /*allowHigherAlign*/ false);
Chris Lattner04dc9572010-08-31 16:44:54 +00004029}
Chris Lattner0cf24192010-06-28 20:05:43 +00004030
John McCallea8d8bb2010-03-11 00:10:12 +00004031// PowerPC-32
John McCallea8d8bb2010-03-11 00:10:12 +00004032namespace {
Roman Divacky8a12d842014-11-03 18:32:54 +00004033/// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
4034class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
Saleem Abdulrasool2a5015b2017-10-25 17:56:50 +00004035 bool IsSoftFloatABI;
4036
4037 CharUnits getParamTypeAlignment(QualType Ty) const;
4038
John McCallea8d8bb2010-03-11 00:10:12 +00004039public:
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004040 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
4041 : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
Roman Divacky8a12d842014-11-03 18:32:54 +00004042
John McCall7f416cc2015-09-08 08:05:57 +00004043 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4044 QualType Ty) const override;
Roman Divacky8a12d842014-11-03 18:32:54 +00004045};
4046
4047class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
4048public:
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004049 PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
4050 : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00004051
Craig Topper4f12f102014-03-12 06:41:41 +00004052 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallea8d8bb2010-03-11 00:10:12 +00004053 // This is recovered from gcc output.
4054 return 1; // r1 is the dedicated stack pointer
4055 }
4056
4057 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004058 llvm::Value *Address) const override;
John McCallea8d8bb2010-03-11 00:10:12 +00004059};
Saleem Abdulrasool2a5015b2017-10-25 17:56:50 +00004060}
John McCallea8d8bb2010-03-11 00:10:12 +00004061
Saleem Abdulrasool2a5015b2017-10-25 17:56:50 +00004062CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
4063 // Complex types are passed just like their elements
4064 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4065 Ty = CTy->getElementType();
4066
4067 if (Ty->isVectorType())
4068 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
4069 : 4);
4070
4071 // For single-element float/vector structs, we consider the whole type
4072 // to have the same alignment requirements as its single element.
4073 const Type *AlignTy = nullptr;
4074 if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
4075 const BuiltinType *BT = EltType->getAs<BuiltinType>();
4076 if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
4077 (BT && BT->isFloatingPoint()))
4078 AlignTy = EltType;
4079 }
4080
4081 if (AlignTy)
4082 return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
4083 return CharUnits::fromQuantity(4);
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004084}
John McCallea8d8bb2010-03-11 00:10:12 +00004085
James Y Knight29b5f082016-02-24 02:59:33 +00004086// TODO: this implementation is now likely redundant with
4087// DefaultABIInfo::EmitVAArg.
John McCall7f416cc2015-09-08 08:05:57 +00004088Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4089 QualType Ty) const {
Saleem Abdulrasool2a5015b2017-10-25 17:56:50 +00004090 if (getTarget().getTriple().isOSDarwin()) {
4091 auto TI = getContext().getTypeInfoInChars(Ty);
4092 TI.second = getParamTypeAlignment(Ty);
4093
4094 CharUnits SlotSize = CharUnits::fromQuantity(4);
4095 return emitVoidPtrVAArg(CGF, VAList, Ty,
4096 classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
4097 /*AllowHigherAlign=*/true);
4098 }
4099
Roman Divacky039b9702016-02-20 08:31:24 +00004100 const unsigned OverflowLimit = 8;
Roman Divacky8a12d842014-11-03 18:32:54 +00004101 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4102 // TODO: Implement this. For now ignore.
4103 (void)CTy;
James Y Knight29b5f082016-02-24 02:59:33 +00004104 return Address::invalid(); // FIXME?
Roman Divacky8a12d842014-11-03 18:32:54 +00004105 }
4106
John McCall7f416cc2015-09-08 08:05:57 +00004107 // struct __va_list_tag {
4108 // unsigned char gpr;
4109 // unsigned char fpr;
4110 // unsigned short reserved;
4111 // void *overflow_arg_area;
4112 // void *reg_save_area;
4113 // };
4114
Roman Divacky8a12d842014-11-03 18:32:54 +00004115 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
Eric Christopher7565e0d2015-05-29 23:09:49 +00004116 bool isInt =
4117 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004118 bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
John McCall7f416cc2015-09-08 08:05:57 +00004119
4120 // All aggregates are passed indirectly? That doesn't seem consistent
4121 // with the argument-lowering code.
4122 bool isIndirect = Ty->isAggregateType();
Roman Divacky8a12d842014-11-03 18:32:54 +00004123
4124 CGBuilderTy &Builder = CGF.Builder;
John McCall7f416cc2015-09-08 08:05:57 +00004125
4126 // The calling convention either uses 1-2 GPRs or 1 FPR.
4127 Address NumRegsAddr = Address::invalid();
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004128 if (isInt || IsSoftFloatABI) {
John McCall7f416cc2015-09-08 08:05:57 +00004129 NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr");
4130 } else {
4131 NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr");
Roman Divacky8a12d842014-11-03 18:32:54 +00004132 }
John McCall7f416cc2015-09-08 08:05:57 +00004133
4134 llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4135
4136 // "Align" the register count when TY is i64.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004137 if (isI64 || (isF64 && IsSoftFloatABI)) {
John McCall7f416cc2015-09-08 08:05:57 +00004138 NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4139 NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4140 }
Roman Divacky8a12d842014-11-03 18:32:54 +00004141
Eric Christopher7565e0d2015-05-29 23:09:49 +00004142 llvm::Value *CC =
Roman Divacky039b9702016-02-20 08:31:24 +00004143 Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
Roman Divacky8a12d842014-11-03 18:32:54 +00004144
4145 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4146 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4147 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4148
4149 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4150
John McCall7f416cc2015-09-08 08:05:57 +00004151 llvm::Type *DirectTy = CGF.ConvertType(Ty);
4152 if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
Roman Divacky8a12d842014-11-03 18:32:54 +00004153
John McCall7f416cc2015-09-08 08:05:57 +00004154 // Case 1: consume registers.
4155 Address RegAddr = Address::invalid();
4156 {
4157 CGF.EmitBlock(UsingRegs);
4158
4159 Address RegSaveAreaPtr =
4160 Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8));
4161 RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
4162 CharUnits::fromQuantity(8));
4163 assert(RegAddr.getElementType() == CGF.Int8Ty);
4164
4165 // Floating-point registers start after the general-purpose registers.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004166 if (!(isInt || IsSoftFloatABI)) {
John McCall7f416cc2015-09-08 08:05:57 +00004167 RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4168 CharUnits::fromQuantity(32));
4169 }
4170
4171 // Get the address of the saved value by scaling the number of
4172 // registers we've used by the number of
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004173 CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
John McCall7f416cc2015-09-08 08:05:57 +00004174 llvm::Value *RegOffset =
4175 Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4176 RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
4177 RegAddr.getPointer(), RegOffset),
4178 RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4179 RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4180
4181 // Increase the used-register count.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004182 NumRegs =
4183 Builder.CreateAdd(NumRegs,
4184 Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
John McCall7f416cc2015-09-08 08:05:57 +00004185 Builder.CreateStore(NumRegs, NumRegsAddr);
4186
4187 CGF.EmitBranch(Cont);
Roman Divacky8a12d842014-11-03 18:32:54 +00004188 }
Roman Divacky8a12d842014-11-03 18:32:54 +00004189
John McCall7f416cc2015-09-08 08:05:57 +00004190 // Case 2: consume space in the overflow area.
4191 Address MemAddr = Address::invalid();
4192 {
4193 CGF.EmitBlock(UsingOverflow);
Roman Divacky8a12d842014-11-03 18:32:54 +00004194
Roman Divacky039b9702016-02-20 08:31:24 +00004195 Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4196
John McCall7f416cc2015-09-08 08:05:57 +00004197 // Everything in the overflow area is rounded up to a size of at least 4.
4198 CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4199
4200 CharUnits Size;
4201 if (!isIndirect) {
4202 auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
Rui Ueyama83aa9792016-01-14 21:00:27 +00004203 Size = TypeInfo.first.alignTo(OverflowAreaAlign);
John McCall7f416cc2015-09-08 08:05:57 +00004204 } else {
4205 Size = CGF.getPointerSize();
4206 }
4207
4208 Address OverflowAreaAddr =
4209 Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4));
Petar Jovanovic402257b2015-12-04 00:26:47 +00004210 Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
John McCall7f416cc2015-09-08 08:05:57 +00004211 OverflowAreaAlign);
Petar Jovanovic402257b2015-12-04 00:26:47 +00004212 // Round up address of argument to alignment
4213 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4214 if (Align > OverflowAreaAlign) {
4215 llvm::Value *Ptr = OverflowArea.getPointer();
4216 OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4217 Align);
4218 }
4219
John McCall7f416cc2015-09-08 08:05:57 +00004220 MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4221
4222 // Increase the overflow area.
4223 OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4224 Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4225 CGF.EmitBranch(Cont);
4226 }
Roman Divacky8a12d842014-11-03 18:32:54 +00004227
4228 CGF.EmitBlock(Cont);
4229
John McCall7f416cc2015-09-08 08:05:57 +00004230 // Merge the cases with a phi.
4231 Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4232 "vaarg.addr");
Roman Divacky8a12d842014-11-03 18:32:54 +00004233
John McCall7f416cc2015-09-08 08:05:57 +00004234 // Load the pointer if the argument was passed indirectly.
4235 if (isIndirect) {
4236 Result = Address(Builder.CreateLoad(Result, "aggr"),
4237 getContext().getTypeAlignInChars(Ty));
Roman Divacky8a12d842014-11-03 18:32:54 +00004238 }
4239
4240 return Result;
4241}
4242
John McCallea8d8bb2010-03-11 00:10:12 +00004243bool
4244PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4245 llvm::Value *Address) const {
4246 // This is calculated from the LLVM and GCC tables and verified
4247 // against gcc output. AFAIK all ABIs use the same encoding.
4248
4249 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallea8d8bb2010-03-11 00:10:12 +00004250
Chris Lattnerece04092012-02-07 00:39:47 +00004251 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallea8d8bb2010-03-11 00:10:12 +00004252 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4253 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4254 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4255
4256 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00004257 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00004258
4259 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00004260 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00004261
4262 // 64-76 are various 4-byte special-purpose registers:
4263 // 64: mq
4264 // 65: lr
4265 // 66: ctr
4266 // 67: ap
4267 // 68-75 cr0-7
4268 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00004269 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00004270
4271 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00004272 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00004273
4274 // 109: vrsave
4275 // 110: vscr
4276 // 111: spe_acc
4277 // 112: spefscr
4278 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00004279 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00004280
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00004281 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00004282}
4283
Roman Divackyd966e722012-05-09 18:22:46 +00004284// PowerPC-64
4285
4286namespace {
Bill Schmidt25cb3492012-10-03 19:18:57 +00004287/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
James Y Knight29b5f082016-02-24 02:59:33 +00004288class PPC64_SVR4_ABIInfo : public ABIInfo {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004289public:
4290 enum ABIKind {
4291 ELFv1 = 0,
4292 ELFv2
4293 };
4294
4295private:
4296 static const unsigned GPRBits = 64;
4297 ABIKind Kind;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004298 bool HasQPX;
Hal Finkel415c2a32016-10-02 02:10:45 +00004299 bool IsSoftFloatABI;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004300
4301 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
4302 // will be passed in a QPX register.
4303 bool IsQPXVectorTy(const Type *Ty) const {
4304 if (!HasQPX)
4305 return false;
4306
4307 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4308 unsigned NumElements = VT->getNumElements();
4309 if (NumElements == 1)
4310 return false;
4311
4312 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
4313 if (getContext().getTypeSize(Ty) <= 256)
4314 return true;
4315 } else if (VT->getElementType()->
4316 isSpecificBuiltinType(BuiltinType::Float)) {
4317 if (getContext().getTypeSize(Ty) <= 128)
4318 return true;
4319 }
4320 }
4321
4322 return false;
4323 }
4324
4325 bool IsQPXVectorTy(QualType Ty) const {
4326 return IsQPXVectorTy(Ty.getTypePtr());
4327 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00004328
4329public:
Hal Finkel415c2a32016-10-02 02:10:45 +00004330 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX,
4331 bool SoftFloatABI)
4332 : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX),
4333 IsSoftFloatABI(SoftFloatABI) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00004334
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004335 bool isPromotableTypeForABI(QualType Ty) const;
John McCall7f416cc2015-09-08 08:05:57 +00004336 CharUnits getParamTypeAlignment(QualType Ty) const;
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004337
4338 ABIArgInfo classifyReturnType(QualType RetTy) const;
4339 ABIArgInfo classifyArgumentType(QualType Ty) const;
4340
Reid Klecknere9f6a712014-10-31 17:10:41 +00004341 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4342 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4343 uint64_t Members) const override;
4344
Bill Schmidt84d37792012-10-12 19:26:17 +00004345 // TODO: We can add more logic to computeInfo to improve performance.
4346 // Example: For aggregate arguments that fit in a register, we could
4347 // use getDirectInReg (as is done below for structs containing a single
4348 // floating-point value) to avoid pushing them to memory on function
4349 // entry. This would require changing the logic in PPCISelLowering
4350 // when lowering the parameters in the caller and args in the callee.
Craig Topper4f12f102014-03-12 06:41:41 +00004351 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00004352 if (!getCXXABI().classifyReturnType(FI))
4353 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004354 for (auto &I : FI.arguments()) {
Bill Schmidt84d37792012-10-12 19:26:17 +00004355 // We rely on the default argument classification for the most part.
4356 // One exception: An aggregate containing a single floating-point
Bill Schmidt179afae2013-07-23 22:15:57 +00004357 // or vector item must be passed in a register if one is available.
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004358 const Type *T = isSingleElementStruct(I.type, getContext());
Bill Schmidt84d37792012-10-12 19:26:17 +00004359 if (T) {
4360 const BuiltinType *BT = T->getAs<BuiltinType>();
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004361 if (IsQPXVectorTy(T) ||
4362 (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004363 (BT && BT->isFloatingPoint())) {
Bill Schmidt84d37792012-10-12 19:26:17 +00004364 QualType QT(T, 0);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004365 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
Bill Schmidt84d37792012-10-12 19:26:17 +00004366 continue;
4367 }
4368 }
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004369 I.info = classifyArgumentType(I.type);
Bill Schmidt84d37792012-10-12 19:26:17 +00004370 }
4371 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00004372
John McCall7f416cc2015-09-08 08:05:57 +00004373 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4374 QualType Ty) const override;
Bill Schmidt25cb3492012-10-03 19:18:57 +00004375};
4376
4377class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004378
Bill Schmidt25cb3492012-10-03 19:18:57 +00004379public:
Ulrich Weigandb7122372014-07-21 00:48:09 +00004380 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
Hal Finkel415c2a32016-10-02 02:10:45 +00004381 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX,
4382 bool SoftFloatABI)
4383 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX,
4384 SoftFloatABI)) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00004385
Craig Topper4f12f102014-03-12 06:41:41 +00004386 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Bill Schmidt25cb3492012-10-03 19:18:57 +00004387 // This is recovered from gcc output.
4388 return 1; // r1 is the dedicated stack pointer
4389 }
4390
4391 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004392 llvm::Value *Address) const override;
Bill Schmidt25cb3492012-10-03 19:18:57 +00004393};
4394
Roman Divackyd966e722012-05-09 18:22:46 +00004395class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4396public:
4397 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
4398
Craig Topper4f12f102014-03-12 06:41:41 +00004399 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyd966e722012-05-09 18:22:46 +00004400 // This is recovered from gcc output.
4401 return 1; // r1 is the dedicated stack pointer
4402 }
4403
4404 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004405 llvm::Value *Address) const override;
Roman Divackyd966e722012-05-09 18:22:46 +00004406};
4407
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004408}
Roman Divackyd966e722012-05-09 18:22:46 +00004409
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004410// Return true if the ABI requires Ty to be passed sign- or zero-
4411// extended to 64 bits.
4412bool
4413PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
4414 // Treat an enum type as its underlying type.
4415 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4416 Ty = EnumTy->getDecl()->getIntegerType();
4417
4418 // Promotable integer types are required to be promoted by the ABI.
4419 if (Ty->isPromotableIntegerType())
4420 return true;
4421
4422 // In addition to the usual promotable integer types, we also need to
4423 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
4424 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4425 switch (BT->getKind()) {
4426 case BuiltinType::Int:
4427 case BuiltinType::UInt:
4428 return true;
4429 default:
4430 break;
4431 }
4432
4433 return false;
4434}
4435
John McCall7f416cc2015-09-08 08:05:57 +00004436/// isAlignedParamType - Determine whether a type requires 16-byte or
4437/// higher alignment in the parameter area. Always returns at least 8.
4438CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
Ulrich Weigand581badc2014-07-10 17:20:07 +00004439 // Complex types are passed just like their elements.
4440 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4441 Ty = CTy->getElementType();
4442
4443 // Only vector types of size 16 bytes need alignment (larger types are
4444 // passed via reference, smaller types are not aligned).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004445 if (IsQPXVectorTy(Ty)) {
4446 if (getContext().getTypeSize(Ty) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004447 return CharUnits::fromQuantity(32);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004448
John McCall7f416cc2015-09-08 08:05:57 +00004449 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004450 } else if (Ty->isVectorType()) {
John McCall7f416cc2015-09-08 08:05:57 +00004451 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004452 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00004453
4454 // For single-element float/vector structs, we consider the whole type
4455 // to have the same alignment requirements as its single element.
4456 const Type *AlignAsType = nullptr;
4457 const Type *EltType = isSingleElementStruct(Ty, getContext());
4458 if (EltType) {
4459 const BuiltinType *BT = EltType->getAs<BuiltinType>();
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004460 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
Ulrich Weigand581badc2014-07-10 17:20:07 +00004461 getContext().getTypeSize(EltType) == 128) ||
4462 (BT && BT->isFloatingPoint()))
4463 AlignAsType = EltType;
4464 }
4465
Ulrich Weigandb7122372014-07-21 00:48:09 +00004466 // Likewise for ELFv2 homogeneous aggregates.
4467 const Type *Base = nullptr;
4468 uint64_t Members = 0;
4469 if (!AlignAsType && Kind == ELFv2 &&
4470 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
4471 AlignAsType = Base;
4472
Ulrich Weigand581badc2014-07-10 17:20:07 +00004473 // With special case aggregates, only vector base types need alignment.
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004474 if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
4475 if (getContext().getTypeSize(AlignAsType) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004476 return CharUnits::fromQuantity(32);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004477
John McCall7f416cc2015-09-08 08:05:57 +00004478 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004479 } else if (AlignAsType) {
John McCall7f416cc2015-09-08 08:05:57 +00004480 return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004481 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00004482
4483 // Otherwise, we only need alignment for any aggregate type that
4484 // has an alignment requirement of >= 16 bytes.
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004485 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
4486 if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
John McCall7f416cc2015-09-08 08:05:57 +00004487 return CharUnits::fromQuantity(32);
4488 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004489 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00004490
John McCall7f416cc2015-09-08 08:05:57 +00004491 return CharUnits::fromQuantity(8);
Ulrich Weigand581badc2014-07-10 17:20:07 +00004492}
4493
Ulrich Weigandb7122372014-07-21 00:48:09 +00004494/// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
4495/// aggregate. Base is set to the base element type, and Members is set
4496/// to the number of base elements.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004497bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
4498 uint64_t &Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004499 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
4500 uint64_t NElements = AT->getSize().getZExtValue();
4501 if (NElements == 0)
4502 return false;
4503 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
4504 return false;
4505 Members *= NElements;
4506 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
4507 const RecordDecl *RD = RT->getDecl();
4508 if (RD->hasFlexibleArrayMember())
4509 return false;
4510
4511 Members = 0;
Ulrich Weiganda094f042014-10-29 13:23:20 +00004512
4513 // If this is a C++ record, check the bases first.
4514 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4515 for (const auto &I : CXXRD->bases()) {
4516 // Ignore empty records.
4517 if (isEmptyRecord(getContext(), I.getType(), true))
4518 continue;
4519
4520 uint64_t FldMembers;
4521 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
4522 return false;
4523
4524 Members += FldMembers;
4525 }
4526 }
4527
Ulrich Weigandb7122372014-07-21 00:48:09 +00004528 for (const auto *FD : RD->fields()) {
4529 // Ignore (non-zero arrays of) empty records.
4530 QualType FT = FD->getType();
4531 while (const ConstantArrayType *AT =
4532 getContext().getAsConstantArrayType(FT)) {
4533 if (AT->getSize().getZExtValue() == 0)
4534 return false;
4535 FT = AT->getElementType();
4536 }
4537 if (isEmptyRecord(getContext(), FT, true))
4538 continue;
4539
4540 // For compatibility with GCC, ignore empty bitfields in C++ mode.
4541 if (getContext().getLangOpts().CPlusPlus &&
4542 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4543 continue;
4544
4545 uint64_t FldMembers;
4546 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
4547 return false;
4548
4549 Members = (RD->isUnion() ?
4550 std::max(Members, FldMembers) : Members + FldMembers);
4551 }
4552
4553 if (!Base)
4554 return false;
4555
4556 // Ensure there is no padding.
4557 if (getContext().getTypeSize(Base) * Members !=
4558 getContext().getTypeSize(Ty))
4559 return false;
4560 } else {
4561 Members = 1;
4562 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
4563 Members = 2;
4564 Ty = CT->getElementType();
4565 }
4566
Reid Klecknere9f6a712014-10-31 17:10:41 +00004567 // Most ABIs only support float, double, and some vector type widths.
4568 if (!isHomogeneousAggregateBaseType(Ty))
Ulrich Weigandb7122372014-07-21 00:48:09 +00004569 return false;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004570
4571 // The base type must be the same for all members. Types that
4572 // agree in both total size and mode (float vs. vector) are
4573 // treated as being equivalent here.
4574 const Type *TyPtr = Ty.getTypePtr();
Ahmed Bougacha40a34c22016-04-19 17:54:29 +00004575 if (!Base) {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004576 Base = TyPtr;
Ahmed Bougacha40a34c22016-04-19 17:54:29 +00004577 // If it's a non-power-of-2 vector, its size is already a power-of-2,
4578 // so make sure to widen it explicitly.
4579 if (const VectorType *VT = Base->getAs<VectorType>()) {
4580 QualType EltTy = VT->getElementType();
4581 unsigned NumElements =
4582 getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
4583 Base = getContext()
4584 .getVectorType(EltTy, NumElements, VT->getVectorKind())
4585 .getTypePtr();
4586 }
4587 }
Ulrich Weigandb7122372014-07-21 00:48:09 +00004588
4589 if (Base->isVectorType() != TyPtr->isVectorType() ||
4590 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
4591 return false;
4592 }
Reid Klecknere9f6a712014-10-31 17:10:41 +00004593 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
4594}
Ulrich Weigandb7122372014-07-21 00:48:09 +00004595
Reid Klecknere9f6a712014-10-31 17:10:41 +00004596bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4597 // Homogeneous aggregates for ELFv2 must have base types of float,
4598 // double, long double, or 128-bit vectors.
4599 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4600 if (BT->getKind() == BuiltinType::Float ||
4601 BT->getKind() == BuiltinType::Double ||
Hal Finkel415c2a32016-10-02 02:10:45 +00004602 BT->getKind() == BuiltinType::LongDouble) {
4603 if (IsSoftFloatABI)
4604 return false;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004605 return true;
Hal Finkel415c2a32016-10-02 02:10:45 +00004606 }
Reid Klecknere9f6a712014-10-31 17:10:41 +00004607 }
4608 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004609 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
Reid Klecknere9f6a712014-10-31 17:10:41 +00004610 return true;
4611 }
4612 return false;
4613}
4614
4615bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
4616 const Type *Base, uint64_t Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004617 // Vector types require one register, floating point types require one
4618 // or two registers depending on their size.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004619 uint32_t NumRegs =
4620 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004621
4622 // Homogeneous Aggregates may occupy at most 8 registers.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004623 return Members * NumRegs <= 8;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004624}
4625
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004626ABIArgInfo
4627PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00004628 Ty = useFirstFieldIfTransparentUnion(Ty);
4629
Bill Schmidt90b22c92012-11-27 02:46:43 +00004630 if (Ty->isAnyComplexType())
4631 return ABIArgInfo::getDirect();
4632
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004633 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
4634 // or via reference (larger than 16 bytes).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004635 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004636 uint64_t Size = getContext().getTypeSize(Ty);
4637 if (Size > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004638 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004639 else if (Size < 128) {
4640 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4641 return ABIArgInfo::getDirect(CoerceTy);
4642 }
4643 }
4644
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004645 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +00004646 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00004647 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004648
John McCall7f416cc2015-09-08 08:05:57 +00004649 uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
4650 uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
Ulrich Weigandb7122372014-07-21 00:48:09 +00004651
4652 // ELFv2 homogeneous aggregates are passed as array types.
4653 const Type *Base = nullptr;
4654 uint64_t Members = 0;
4655 if (Kind == ELFv2 &&
4656 isHomogeneousAggregate(Ty, Base, Members)) {
4657 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4658 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4659 return ABIArgInfo::getDirect(CoerceTy);
4660 }
4661
Ulrich Weigand601957f2014-07-21 00:56:36 +00004662 // If an aggregate may end up fully in registers, we do not
4663 // use the ByVal method, but pass the aggregate as array.
4664 // This is usually beneficial since we avoid forcing the
4665 // back-end to store the argument to memory.
4666 uint64_t Bits = getContext().getTypeSize(Ty);
4667 if (Bits > 0 && Bits <= 8 * GPRBits) {
4668 llvm::Type *CoerceTy;
4669
4670 // Types up to 8 bytes are passed as integer type (which will be
4671 // properly aligned in the argument save area doubleword).
4672 if (Bits <= GPRBits)
Rui Ueyama83aa9792016-01-14 21:00:27 +00004673 CoerceTy =
4674 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Ulrich Weigand601957f2014-07-21 00:56:36 +00004675 // Larger types are passed as arrays, with the base type selected
4676 // according to the required alignment in the save area.
4677 else {
4678 uint64_t RegBits = ABIAlign * 8;
Rui Ueyama83aa9792016-01-14 21:00:27 +00004679 uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
Ulrich Weigand601957f2014-07-21 00:56:36 +00004680 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
4681 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
4682 }
4683
4684 return ABIArgInfo::getDirect(CoerceTy);
4685 }
4686
Ulrich Weigandb7122372014-07-21 00:48:09 +00004687 // All other aggregates are passed ByVal.
John McCall7f416cc2015-09-08 08:05:57 +00004688 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
4689 /*ByVal=*/true,
Ulrich Weigand581badc2014-07-10 17:20:07 +00004690 /*Realign=*/TyAlign > ABIAlign);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004691 }
4692
Alex Bradburye41a5e22018-01-12 20:08:16 +00004693 return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
4694 : ABIArgInfo::getDirect());
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004695}
4696
4697ABIArgInfo
4698PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4699 if (RetTy->isVoidType())
4700 return ABIArgInfo::getIgnore();
4701
Bill Schmidta3d121c2012-12-17 04:20:17 +00004702 if (RetTy->isAnyComplexType())
4703 return ABIArgInfo::getDirect();
4704
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004705 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
4706 // or via reference (larger than 16 bytes).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004707 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004708 uint64_t Size = getContext().getTypeSize(RetTy);
4709 if (Size > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004710 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004711 else if (Size < 128) {
4712 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4713 return ABIArgInfo::getDirect(CoerceTy);
4714 }
4715 }
4716
Ulrich Weigandb7122372014-07-21 00:48:09 +00004717 if (isAggregateTypeForABI(RetTy)) {
4718 // ELFv2 homogeneous aggregates are returned as array types.
4719 const Type *Base = nullptr;
4720 uint64_t Members = 0;
4721 if (Kind == ELFv2 &&
4722 isHomogeneousAggregate(RetTy, Base, Members)) {
4723 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4724 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4725 return ABIArgInfo::getDirect(CoerceTy);
4726 }
4727
4728 // ELFv2 small aggregates are returned in up to two registers.
4729 uint64_t Bits = getContext().getTypeSize(RetTy);
4730 if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
4731 if (Bits == 0)
4732 return ABIArgInfo::getIgnore();
4733
4734 llvm::Type *CoerceTy;
4735 if (Bits > GPRBits) {
4736 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
Serge Guelton1d993272017-05-09 19:31:30 +00004737 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
Ulrich Weigandb7122372014-07-21 00:48:09 +00004738 } else
Rui Ueyama83aa9792016-01-14 21:00:27 +00004739 CoerceTy =
4740 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Ulrich Weigandb7122372014-07-21 00:48:09 +00004741 return ABIArgInfo::getDirect(CoerceTy);
4742 }
4743
4744 // All other aggregates are returned indirectly.
John McCall7f416cc2015-09-08 08:05:57 +00004745 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandb7122372014-07-21 00:48:09 +00004746 }
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004747
Alex Bradburye41a5e22018-01-12 20:08:16 +00004748 return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
4749 : ABIArgInfo::getDirect());
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004750}
4751
Bill Schmidt25cb3492012-10-03 19:18:57 +00004752// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
John McCall7f416cc2015-09-08 08:05:57 +00004753Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4754 QualType Ty) const {
4755 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4756 TypeInfo.second = getParamTypeAlignment(Ty);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004757
John McCall7f416cc2015-09-08 08:05:57 +00004758 CharUnits SlotSize = CharUnits::fromQuantity(8);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004759
Bill Schmidt924c4782013-01-14 17:45:36 +00004760 // If we have a complex type and the base type is smaller than 8 bytes,
4761 // the ABI calls for the real and imaginary parts to be right-adjusted
4762 // in separate doublewords. However, Clang expects us to produce a
4763 // pointer to a structure with the two parts packed tightly. So generate
4764 // loads of the real and imaginary parts relative to the va_list pointer,
4765 // and store them to a temporary structure.
John McCall7f416cc2015-09-08 08:05:57 +00004766 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4767 CharUnits EltSize = TypeInfo.first / 2;
4768 if (EltSize < SlotSize) {
4769 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
4770 SlotSize * 2, SlotSize,
4771 SlotSize, /*AllowHigher*/ true);
4772
4773 Address RealAddr = Addr;
4774 Address ImagAddr = RealAddr;
4775 if (CGF.CGM.getDataLayout().isBigEndian()) {
4776 RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
4777 SlotSize - EltSize);
4778 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
4779 2 * SlotSize - EltSize);
4780 } else {
4781 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
4782 }
4783
4784 llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
4785 RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
4786 ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
4787 llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
4788 llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
4789
4790 Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
4791 CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
4792 /*init*/ true);
4793 return Temp;
Ulrich Weigandbebc55b2014-06-20 16:37:40 +00004794 }
Bill Schmidt924c4782013-01-14 17:45:36 +00004795 }
4796
John McCall7f416cc2015-09-08 08:05:57 +00004797 // Otherwise, just use the general rule.
4798 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
4799 TypeInfo, SlotSize, /*AllowHigher*/ true);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004800}
4801
4802static bool
4803PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4804 llvm::Value *Address) {
Roman Divackyd966e722012-05-09 18:22:46 +00004805 // This is calculated from the LLVM and GCC tables and verified
4806 // against gcc output. AFAIK all ABIs use the same encoding.
4807
4808 CodeGen::CGBuilderTy &Builder = CGF.Builder;
4809
4810 llvm::IntegerType *i8 = CGF.Int8Ty;
4811 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4812 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4813 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4814
4815 // 0-31: r0-31, the 8-byte general-purpose registers
4816 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
4817
4818 // 32-63: fp0-31, the 8-byte floating-point registers
4819 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4820
Hal Finkel84832a72016-08-30 02:38:34 +00004821 // 64-67 are various 8-byte special-purpose registers:
Roman Divackyd966e722012-05-09 18:22:46 +00004822 // 64: mq
4823 // 65: lr
4824 // 66: ctr
4825 // 67: ap
Hal Finkel84832a72016-08-30 02:38:34 +00004826 AssignToArrayRange(Builder, Address, Eight8, 64, 67);
4827
4828 // 68-76 are various 4-byte special-purpose registers:
Roman Divackyd966e722012-05-09 18:22:46 +00004829 // 68-75 cr0-7
4830 // 76: xer
Hal Finkel84832a72016-08-30 02:38:34 +00004831 AssignToArrayRange(Builder, Address, Four8, 68, 76);
Roman Divackyd966e722012-05-09 18:22:46 +00004832
4833 // 77-108: v0-31, the 16-byte vector registers
4834 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4835
4836 // 109: vrsave
4837 // 110: vscr
4838 // 111: spe_acc
4839 // 112: spefscr
4840 // 113: sfp
Hal Finkel84832a72016-08-30 02:38:34 +00004841 // 114: tfhar
4842 // 115: tfiar
4843 // 116: texasr
4844 AssignToArrayRange(Builder, Address, Eight8, 109, 116);
Roman Divackyd966e722012-05-09 18:22:46 +00004845
4846 return false;
4847}
John McCallea8d8bb2010-03-11 00:10:12 +00004848
Bill Schmidt25cb3492012-10-03 19:18:57 +00004849bool
4850PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
4851 CodeGen::CodeGenFunction &CGF,
4852 llvm::Value *Address) const {
4853
4854 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4855}
4856
4857bool
4858PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4859 llvm::Value *Address) const {
4860
4861 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4862}
4863
Chris Lattner0cf24192010-06-28 20:05:43 +00004864//===----------------------------------------------------------------------===//
Tim Northover573cbee2014-05-24 12:52:07 +00004865// AArch64 ABI Implementation
Tim Northovera2ee4332014-03-29 15:09:45 +00004866//===----------------------------------------------------------------------===//
4867
4868namespace {
4869
John McCall12f23522016-04-04 18:33:08 +00004870class AArch64ABIInfo : public SwiftABIInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00004871public:
4872 enum ABIKind {
4873 AAPCS = 0,
Martin Storsjo502de222017-07-13 17:59:14 +00004874 DarwinPCS,
4875 Win64
Tim Northovera2ee4332014-03-29 15:09:45 +00004876 };
4877
4878private:
4879 ABIKind Kind;
4880
4881public:
John McCall12f23522016-04-04 18:33:08 +00004882 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
4883 : SwiftABIInfo(CGT), Kind(Kind) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00004884
4885private:
4886 ABIKind getABIKind() const { return Kind; }
4887 bool isDarwinPCS() const { return Kind == DarwinPCS; }
4888
4889 ABIArgInfo classifyReturnType(QualType RetTy) const;
Tim Northoverb047bfa2014-11-27 21:02:49 +00004890 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004891 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4892 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4893 uint64_t Members) const override;
4894
Tim Northovera2ee4332014-03-29 15:09:45 +00004895 bool isIllegalVectorType(QualType Ty) const;
4896
David Blaikie1cbb9712014-11-14 19:09:44 +00004897 void computeInfo(CGFunctionInfo &FI) const override {
Akira Hatanakabe7daa32018-03-12 17:05:06 +00004898 if (!getCXXABI().classifyReturnType(FI))
Reid Kleckner40ca9132014-05-13 22:05:45 +00004899 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Tim Northover5ffc0922014-04-17 10:20:38 +00004900
Tim Northoverb047bfa2014-11-27 21:02:49 +00004901 for (auto &it : FI.arguments())
4902 it.info = classifyArgumentType(it.type);
Tim Northovera2ee4332014-03-29 15:09:45 +00004903 }
4904
John McCall7f416cc2015-09-08 08:05:57 +00004905 Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4906 CodeGenFunction &CGF) const;
Tim Northovera2ee4332014-03-29 15:09:45 +00004907
John McCall7f416cc2015-09-08 08:05:57 +00004908 Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
4909 CodeGenFunction &CGF) const;
Tim Northovera2ee4332014-03-29 15:09:45 +00004910
John McCall7f416cc2015-09-08 08:05:57 +00004911 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4912 QualType Ty) const override {
Martin Storsjo502de222017-07-13 17:59:14 +00004913 return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
4914 : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
4915 : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
Tim Northovera2ee4332014-03-29 15:09:45 +00004916 }
John McCall12f23522016-04-04 18:33:08 +00004917
Martin Storsjo502de222017-07-13 17:59:14 +00004918 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
4919 QualType Ty) const override;
4920
John McCall56331e22018-01-07 06:28:49 +00004921 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
John McCall12f23522016-04-04 18:33:08 +00004922 bool asReturnValue) const override {
4923 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4924 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00004925 bool isSwiftErrorInRegister() const override {
4926 return true;
4927 }
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00004928
4929 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
4930 unsigned elts) const override;
Tim Northovera2ee4332014-03-29 15:09:45 +00004931};
4932
Tim Northover573cbee2014-05-24 12:52:07 +00004933class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00004934public:
Tim Northover573cbee2014-05-24 12:52:07 +00004935 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
4936 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00004937
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004938 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
Oliver Stannard7f188642017-08-21 09:54:46 +00004939 return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
Tim Northovera2ee4332014-03-29 15:09:45 +00004940 }
4941
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004942 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4943 return 31;
4944 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004945
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004946 bool doesReturnSlotInterfereWithArgs() const override { return false; }
Tim Northovera2ee4332014-03-29 15:09:45 +00004947};
Martin Storsjo1c8af272017-07-20 05:47:06 +00004948
4949class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
4950public:
4951 WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
4952 : AArch64TargetCodeGenInfo(CGT, K) {}
4953
4954 void getDependentLibraryOption(llvm::StringRef Lib,
4955 llvm::SmallString<24> &Opt) const override {
4956 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
4957 }
4958
4959 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
4960 llvm::SmallString<32> &Opt) const override {
4961 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
4962 }
4963};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004964}
Tim Northovera2ee4332014-03-29 15:09:45 +00004965
Tim Northoverb047bfa2014-11-27 21:02:49 +00004966ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00004967 Ty = useFirstFieldIfTransparentUnion(Ty);
4968
Tim Northovera2ee4332014-03-29 15:09:45 +00004969 // Handle illegal vector types here.
4970 if (isIllegalVectorType(Ty)) {
4971 uint64_t Size = getContext().getTypeSize(Ty);
Nirav Dave9a8f97e2016-02-22 16:48:42 +00004972 // Android promotes <2 x i8> to i16, not i32
Ahmed Bougacha8862cae2016-04-19 17:54:24 +00004973 if (isAndroid() && (Size <= 16)) {
Nirav Dave9a8f97e2016-02-22 16:48:42 +00004974 llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
4975 return ABIArgInfo::getDirect(ResType);
4976 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004977 if (Size <= 32) {
4978 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
Tim Northovera2ee4332014-03-29 15:09:45 +00004979 return ABIArgInfo::getDirect(ResType);
4980 }
4981 if (Size == 64) {
4982 llvm::Type *ResType =
4983 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
Tim Northovera2ee4332014-03-29 15:09:45 +00004984 return ABIArgInfo::getDirect(ResType);
4985 }
4986 if (Size == 128) {
4987 llvm::Type *ResType =
4988 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
Tim Northovera2ee4332014-03-29 15:09:45 +00004989 return ABIArgInfo::getDirect(ResType);
4990 }
John McCall7f416cc2015-09-08 08:05:57 +00004991 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Tim Northovera2ee4332014-03-29 15:09:45 +00004992 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004993
4994 if (!isAggregateTypeForABI(Ty)) {
4995 // Treat an enum type as its underlying type.
4996 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4997 Ty = EnumTy->getDecl()->getIntegerType();
4998
Tim Northovera2ee4332014-03-29 15:09:45 +00004999 return (Ty->isPromotableIntegerType() && isDarwinPCS()
Alex Bradburye41a5e22018-01-12 20:08:16 +00005000 ? ABIArgInfo::getExtend(Ty)
Tim Northovera2ee4332014-03-29 15:09:45 +00005001 : ABIArgInfo::getDirect());
5002 }
5003
5004 // Structures with either a non-trivial destructor or a non-trivial
5005 // copy constructor are always indirect.
Reid Kleckner40ca9132014-05-13 22:05:45 +00005006 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
John McCall7f416cc2015-09-08 08:05:57 +00005007 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
5008 CGCXXABI::RAA_DirectInMemory);
Tim Northovera2ee4332014-03-29 15:09:45 +00005009 }
5010
5011 // Empty records are always ignored on Darwin, but actually passed in C++ mode
5012 // elsewhere for GNU compatibility.
Tim Northover23bcad22017-05-05 22:36:06 +00005013 uint64_t Size = getContext().getTypeSize(Ty);
5014 bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
5015 if (IsEmpty || Size == 0) {
Tim Northovera2ee4332014-03-29 15:09:45 +00005016 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
5017 return ABIArgInfo::getIgnore();
5018
Tim Northover23bcad22017-05-05 22:36:06 +00005019 // GNU C mode. The only argument that gets ignored is an empty one with size
5020 // 0.
5021 if (IsEmpty && Size == 0)
5022 return ABIArgInfo::getIgnore();
Tim Northovera2ee4332014-03-29 15:09:45 +00005023 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5024 }
5025
5026 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
Craig Topper8a13c412014-05-21 05:09:00 +00005027 const Type *Base = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005028 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005029 if (isHomogeneousAggregate(Ty, Base, Members)) {
Tim Northoverb047bfa2014-11-27 21:02:49 +00005030 return ABIArgInfo::getDirect(
5031 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
Tim Northovera2ee4332014-03-29 15:09:45 +00005032 }
5033
5034 // Aggregates <= 16 bytes are passed directly in registers or on the stack.
Tim Northovera2ee4332014-03-29 15:09:45 +00005035 if (Size <= 128) {
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005036 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5037 // same size and alignment.
5038 if (getTarget().isRenderScriptTarget()) {
5039 return coerceToIntArray(Ty, getContext(), getVMContext());
5040 }
Tim Northoverc801b4a2014-04-15 14:55:11 +00005041 unsigned Alignment = getContext().getTypeAlign(Ty);
Davide Italiano7a3b69d2017-04-03 16:51:39 +00005042 Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
Tim Northoverb047bfa2014-11-27 21:02:49 +00005043
Tim Northovera2ee4332014-03-29 15:09:45 +00005044 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5045 // For aggregates with 16-byte alignment, we use i128.
Tim Northoverc801b4a2014-04-15 14:55:11 +00005046 if (Alignment < 128 && Size == 128) {
Tim Northovera2ee4332014-03-29 15:09:45 +00005047 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5048 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5049 }
5050 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5051 }
5052
John McCall7f416cc2015-09-08 08:05:57 +00005053 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Tim Northovera2ee4332014-03-29 15:09:45 +00005054}
5055
Tim Northover573cbee2014-05-24 12:52:07 +00005056ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00005057 if (RetTy->isVoidType())
5058 return ABIArgInfo::getIgnore();
5059
5060 // Large vector types should be returned via memory.
5061 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00005062 return getNaturalAlignIndirect(RetTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005063
5064 if (!isAggregateTypeForABI(RetTy)) {
5065 // Treat an enum type as its underlying type.
5066 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5067 RetTy = EnumTy->getDecl()->getIntegerType();
5068
Tim Northover4dab6982014-04-18 13:46:08 +00005069 return (RetTy->isPromotableIntegerType() && isDarwinPCS()
Alex Bradburye41a5e22018-01-12 20:08:16 +00005070 ? ABIArgInfo::getExtend(RetTy)
Tim Northover4dab6982014-04-18 13:46:08 +00005071 : ABIArgInfo::getDirect());
Tim Northovera2ee4332014-03-29 15:09:45 +00005072 }
5073
Tim Northover23bcad22017-05-05 22:36:06 +00005074 uint64_t Size = getContext().getTypeSize(RetTy);
5075 if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
Tim Northovera2ee4332014-03-29 15:09:45 +00005076 return ABIArgInfo::getIgnore();
5077
Craig Topper8a13c412014-05-21 05:09:00 +00005078 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005079 uint64_t Members = 0;
5080 if (isHomogeneousAggregate(RetTy, Base, Members))
Tim Northovera2ee4332014-03-29 15:09:45 +00005081 // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
5082 return ABIArgInfo::getDirect();
5083
5084 // Aggregates <= 16 bytes are returned directly in registers or on the stack.
Tim Northovera2ee4332014-03-29 15:09:45 +00005085 if (Size <= 128) {
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005086 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5087 // same size and alignment.
5088 if (getTarget().isRenderScriptTarget()) {
5089 return coerceToIntArray(RetTy, getContext(), getVMContext());
5090 }
Pete Cooper635b5092015-04-17 22:16:24 +00005091 unsigned Alignment = getContext().getTypeAlign(RetTy);
Davide Italiano7a3b69d2017-04-03 16:51:39 +00005092 Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
Pete Cooper635b5092015-04-17 22:16:24 +00005093
5094 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5095 // For aggregates with 16-byte alignment, we use i128.
5096 if (Alignment < 128 && Size == 128) {
5097 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5098 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5099 }
Tim Northovera2ee4332014-03-29 15:09:45 +00005100 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5101 }
5102
John McCall7f416cc2015-09-08 08:05:57 +00005103 return getNaturalAlignIndirect(RetTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005104}
5105
Tim Northover573cbee2014-05-24 12:52:07 +00005106/// isIllegalVectorType - check whether the vector type is legal for AArch64.
5107bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00005108 if (const VectorType *VT = Ty->getAs<VectorType>()) {
5109 // Check whether VT is legal.
5110 unsigned NumElements = VT->getNumElements();
5111 uint64_t Size = getContext().getTypeSize(VT);
Tim Northover34fd4fb2016-05-03 19:24:47 +00005112 // NumElements should be power of 2.
Tim Northover360d2b32016-05-03 19:22:41 +00005113 if (!llvm::isPowerOf2_32(NumElements))
Tim Northovera2ee4332014-03-29 15:09:45 +00005114 return true;
5115 return Size != 64 && (Size != 128 || NumElements == 1);
5116 }
5117 return false;
5118}
5119
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00005120bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
5121 llvm::Type *eltTy,
5122 unsigned elts) const {
5123 if (!llvm::isPowerOf2_32(elts))
5124 return false;
5125 if (totalSize.getQuantity() != 8 &&
5126 (totalSize.getQuantity() != 16 || elts == 1))
5127 return false;
5128 return true;
5129}
5130
Reid Klecknere9f6a712014-10-31 17:10:41 +00005131bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5132 // Homogeneous aggregates for AAPCS64 must have base types of a floating
5133 // point type or a short-vector type. This is the same as the 32-bit ABI,
5134 // but with the difference that any floating-point type is allowed,
5135 // including __fp16.
5136 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5137 if (BT->isFloatingPoint())
5138 return true;
5139 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5140 unsigned VecSize = getContext().getTypeSize(VT);
5141 if (VecSize == 64 || VecSize == 128)
5142 return true;
5143 }
5144 return false;
5145}
5146
5147bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5148 uint64_t Members) const {
5149 return Members <= 4;
5150}
5151
John McCall7f416cc2015-09-08 08:05:57 +00005152Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
Tim Northoverb047bfa2014-11-27 21:02:49 +00005153 QualType Ty,
5154 CodeGenFunction &CGF) const {
5155 ABIArgInfo AI = classifyArgumentType(Ty);
Reid Klecknere9f6a712014-10-31 17:10:41 +00005156 bool IsIndirect = AI.isIndirect();
5157
Tim Northoverb047bfa2014-11-27 21:02:49 +00005158 llvm::Type *BaseTy = CGF.ConvertType(Ty);
5159 if (IsIndirect)
5160 BaseTy = llvm::PointerType::getUnqual(BaseTy);
5161 else if (AI.getCoerceToType())
5162 BaseTy = AI.getCoerceToType();
5163
5164 unsigned NumRegs = 1;
5165 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
5166 BaseTy = ArrTy->getElementType();
5167 NumRegs = ArrTy->getNumElements();
5168 }
5169 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
5170
Tim Northovera2ee4332014-03-29 15:09:45 +00005171 // The AArch64 va_list type and handling is specified in the Procedure Call
5172 // Standard, section B.4:
5173 //
5174 // struct {
5175 // void *__stack;
5176 // void *__gr_top;
5177 // void *__vr_top;
5178 // int __gr_offs;
5179 // int __vr_offs;
5180 // };
5181
5182 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
5183 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5184 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
5185 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
Tim Northovera2ee4332014-03-29 15:09:45 +00005186
John McCall7f416cc2015-09-08 08:05:57 +00005187 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5188 CharUnits TyAlign = TyInfo.second;
5189
5190 Address reg_offs_p = Address::invalid();
5191 llvm::Value *reg_offs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005192 int reg_top_index;
John McCall7f416cc2015-09-08 08:05:57 +00005193 CharUnits reg_top_offset;
5194 int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity();
Tim Northoverb047bfa2014-11-27 21:02:49 +00005195 if (!IsFPR) {
Tim Northovera2ee4332014-03-29 15:09:45 +00005196 // 3 is the field number of __gr_offs
David Blaikie2e804282015-04-05 22:47:07 +00005197 reg_offs_p =
John McCall7f416cc2015-09-08 08:05:57 +00005198 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
5199 "gr_offs_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00005200 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
5201 reg_top_index = 1; // field number for __gr_top
John McCall7f416cc2015-09-08 08:05:57 +00005202 reg_top_offset = CharUnits::fromQuantity(8);
Rui Ueyama83aa9792016-01-14 21:00:27 +00005203 RegSize = llvm::alignTo(RegSize, 8);
Tim Northovera2ee4332014-03-29 15:09:45 +00005204 } else {
Tim Northovera2ee4332014-03-29 15:09:45 +00005205 // 4 is the field number of __vr_offs.
David Blaikie2e804282015-04-05 22:47:07 +00005206 reg_offs_p =
John McCall7f416cc2015-09-08 08:05:57 +00005207 CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28),
5208 "vr_offs_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00005209 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
5210 reg_top_index = 2; // field number for __vr_top
John McCall7f416cc2015-09-08 08:05:57 +00005211 reg_top_offset = CharUnits::fromQuantity(16);
Tim Northoverb047bfa2014-11-27 21:02:49 +00005212 RegSize = 16 * NumRegs;
Tim Northovera2ee4332014-03-29 15:09:45 +00005213 }
5214
5215 //=======================================
5216 // Find out where argument was passed
5217 //=======================================
5218
5219 // If reg_offs >= 0 we're already using the stack for this type of
5220 // argument. We don't want to keep updating reg_offs (in case it overflows,
5221 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
5222 // whatever they get).
Craig Topper8a13c412014-05-21 05:09:00 +00005223 llvm::Value *UsingStack = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005224 UsingStack = CGF.Builder.CreateICmpSGE(
5225 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
5226
5227 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
5228
5229 // Otherwise, at least some kind of argument could go in these registers, the
Bob Wilson3abf1692014-04-21 01:23:36 +00005230 // question is whether this particular type is too big.
Tim Northovera2ee4332014-03-29 15:09:45 +00005231 CGF.EmitBlock(MaybeRegBlock);
5232
5233 // Integer arguments may need to correct register alignment (for example a
5234 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
5235 // align __gr_offs to calculate the potential address.
John McCall7f416cc2015-09-08 08:05:57 +00005236 if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
5237 int Align = TyAlign.getQuantity();
Tim Northovera2ee4332014-03-29 15:09:45 +00005238
5239 reg_offs = CGF.Builder.CreateAdd(
5240 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
5241 "align_regoffs");
5242 reg_offs = CGF.Builder.CreateAnd(
5243 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
5244 "aligned_regoffs");
5245 }
5246
5247 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
John McCall7f416cc2015-09-08 08:05:57 +00005248 // The fact that this is done unconditionally reflects the fact that
5249 // allocating an argument to the stack also uses up all the remaining
5250 // registers of the appropriate kind.
Craig Topper8a13c412014-05-21 05:09:00 +00005251 llvm::Value *NewOffset = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005252 NewOffset = CGF.Builder.CreateAdd(
5253 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
5254 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
5255
5256 // Now we're in a position to decide whether this argument really was in
5257 // registers or not.
Craig Topper8a13c412014-05-21 05:09:00 +00005258 llvm::Value *InRegs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005259 InRegs = CGF.Builder.CreateICmpSLE(
5260 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
5261
5262 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
5263
5264 //=======================================
5265 // Argument was in registers
5266 //=======================================
5267
5268 // Now we emit the code for if the argument was originally passed in
5269 // registers. First start the appropriate block:
5270 CGF.EmitBlock(InRegBlock);
5271
John McCall7f416cc2015-09-08 08:05:57 +00005272 llvm::Value *reg_top = nullptr;
5273 Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index,
5274 reg_top_offset, "reg_top_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00005275 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
John McCall7f416cc2015-09-08 08:05:57 +00005276 Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
5277 CharUnits::fromQuantity(IsFPR ? 16 : 8));
5278 Address RegAddr = Address::invalid();
5279 llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00005280
5281 if (IsIndirect) {
5282 // If it's been passed indirectly (actually a struct), whatever we find from
5283 // stored registers or on the stack will actually be a struct **.
5284 MemTy = llvm::PointerType::getUnqual(MemTy);
5285 }
5286
Craig Topper8a13c412014-05-21 05:09:00 +00005287 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005288 uint64_t NumMembers = 0;
5289 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
James Molloy467be602014-05-07 14:45:55 +00005290 if (IsHFA && NumMembers > 1) {
Tim Northovera2ee4332014-03-29 15:09:45 +00005291 // Homogeneous aggregates passed in registers will have their elements split
5292 // and stored 16-bytes apart regardless of size (they're notionally in qN,
5293 // qN+1, ...). We reload and store into a temporary local variable
5294 // contiguously.
5295 assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
John McCall7f416cc2015-09-08 08:05:57 +00005296 auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
Tim Northovera2ee4332014-03-29 15:09:45 +00005297 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
5298 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
John McCall7f416cc2015-09-08 08:05:57 +00005299 Address Tmp = CGF.CreateTempAlloca(HFATy,
5300 std::max(TyAlign, BaseTyInfo.second));
Tim Northovera2ee4332014-03-29 15:09:45 +00005301
John McCall7f416cc2015-09-08 08:05:57 +00005302 // On big-endian platforms, the value will be right-aligned in its slot.
5303 int Offset = 0;
5304 if (CGF.CGM.getDataLayout().isBigEndian() &&
5305 BaseTyInfo.first.getQuantity() < 16)
5306 Offset = 16 - BaseTyInfo.first.getQuantity();
5307
Tim Northovera2ee4332014-03-29 15:09:45 +00005308 for (unsigned i = 0; i < NumMembers; ++i) {
John McCall7f416cc2015-09-08 08:05:57 +00005309 CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
5310 Address LoadAddr =
5311 CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
5312 LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
5313
5314 Address StoreAddr =
5315 CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first);
Tim Northovera2ee4332014-03-29 15:09:45 +00005316
5317 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
5318 CGF.Builder.CreateStore(Elem, StoreAddr);
5319 }
5320
John McCall7f416cc2015-09-08 08:05:57 +00005321 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005322 } else {
John McCall7f416cc2015-09-08 08:05:57 +00005323 // Otherwise the object is contiguous in memory.
5324
5325 // It might be right-aligned in its slot.
5326 CharUnits SlotSize = BaseAddr.getAlignment();
5327 if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
James Molloy467be602014-05-07 14:45:55 +00005328 (IsHFA || !isAggregateTypeForABI(Ty)) &&
John McCall7f416cc2015-09-08 08:05:57 +00005329 TyInfo.first < SlotSize) {
5330 CharUnits Offset = SlotSize - TyInfo.first;
5331 BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
Tim Northovera2ee4332014-03-29 15:09:45 +00005332 }
5333
John McCall7f416cc2015-09-08 08:05:57 +00005334 RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005335 }
5336
5337 CGF.EmitBranch(ContBlock);
5338
5339 //=======================================
5340 // Argument was on the stack
5341 //=======================================
5342 CGF.EmitBlock(OnStackBlock);
5343
John McCall7f416cc2015-09-08 08:05:57 +00005344 Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0,
5345 CharUnits::Zero(), "stack_p");
5346 llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
Tim Northovera2ee4332014-03-29 15:09:45 +00005347
John McCall7f416cc2015-09-08 08:05:57 +00005348 // Again, stack arguments may need realignment. In this case both integer and
Tim Northovera2ee4332014-03-29 15:09:45 +00005349 // floating-point ones might be affected.
John McCall7f416cc2015-09-08 08:05:57 +00005350 if (!IsIndirect && TyAlign.getQuantity() > 8) {
5351 int Align = TyAlign.getQuantity();
Tim Northovera2ee4332014-03-29 15:09:45 +00005352
John McCall7f416cc2015-09-08 08:05:57 +00005353 OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00005354
John McCall7f416cc2015-09-08 08:05:57 +00005355 OnStackPtr = CGF.Builder.CreateAdd(
5356 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
Tim Northovera2ee4332014-03-29 15:09:45 +00005357 "align_stack");
John McCall7f416cc2015-09-08 08:05:57 +00005358 OnStackPtr = CGF.Builder.CreateAnd(
5359 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
Tim Northovera2ee4332014-03-29 15:09:45 +00005360 "align_stack");
5361
John McCall7f416cc2015-09-08 08:05:57 +00005362 OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005363 }
John McCall7f416cc2015-09-08 08:05:57 +00005364 Address OnStackAddr(OnStackPtr,
5365 std::max(CharUnits::fromQuantity(8), TyAlign));
Tim Northovera2ee4332014-03-29 15:09:45 +00005366
John McCall7f416cc2015-09-08 08:05:57 +00005367 // All stack slots are multiples of 8 bytes.
5368 CharUnits StackSlotSize = CharUnits::fromQuantity(8);
5369 CharUnits StackSize;
Tim Northovera2ee4332014-03-29 15:09:45 +00005370 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00005371 StackSize = StackSlotSize;
Tim Northovera2ee4332014-03-29 15:09:45 +00005372 else
Rui Ueyama83aa9792016-01-14 21:00:27 +00005373 StackSize = TyInfo.first.alignTo(StackSlotSize);
Tim Northovera2ee4332014-03-29 15:09:45 +00005374
John McCall7f416cc2015-09-08 08:05:57 +00005375 llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
Tim Northovera2ee4332014-03-29 15:09:45 +00005376 llvm::Value *NewStack =
John McCall7f416cc2015-09-08 08:05:57 +00005377 CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
Tim Northovera2ee4332014-03-29 15:09:45 +00005378
5379 // Write the new value of __stack for the next call to va_arg
5380 CGF.Builder.CreateStore(NewStack, stack_p);
5381
5382 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
John McCall7f416cc2015-09-08 08:05:57 +00005383 TyInfo.first < StackSlotSize) {
5384 CharUnits Offset = StackSlotSize - TyInfo.first;
5385 OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
Tim Northovera2ee4332014-03-29 15:09:45 +00005386 }
5387
John McCall7f416cc2015-09-08 08:05:57 +00005388 OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005389
5390 CGF.EmitBranch(ContBlock);
5391
5392 //=======================================
5393 // Tidy up
5394 //=======================================
5395 CGF.EmitBlock(ContBlock);
5396
John McCall7f416cc2015-09-08 08:05:57 +00005397 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
5398 OnStackAddr, OnStackBlock, "vaargs.addr");
Tim Northovera2ee4332014-03-29 15:09:45 +00005399
5400 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00005401 return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
5402 TyInfo.second);
Tim Northovera2ee4332014-03-29 15:09:45 +00005403
5404 return ResAddr;
5405}
5406
John McCall7f416cc2015-09-08 08:05:57 +00005407Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5408 CodeGenFunction &CGF) const {
5409 // The backend's lowering doesn't support va_arg for aggregates or
5410 // illegal vector types. Lower VAArg here for these cases and use
5411 // the LLVM va_arg instruction for everything else.
Tim Northovera2ee4332014-03-29 15:09:45 +00005412 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
James Y Knight29b5f082016-02-24 02:59:33 +00005413 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Tim Northovera2ee4332014-03-29 15:09:45 +00005414
John McCall7f416cc2015-09-08 08:05:57 +00005415 CharUnits SlotSize = CharUnits::fromQuantity(8);
Tim Northovera2ee4332014-03-29 15:09:45 +00005416
John McCall7f416cc2015-09-08 08:05:57 +00005417 // Empty records are ignored for parameter passing purposes.
Tim Northovera2ee4332014-03-29 15:09:45 +00005418 if (isEmptyRecord(getContext(), Ty, true)) {
John McCall7f416cc2015-09-08 08:05:57 +00005419 Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
5420 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5421 return Addr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005422 }
5423
John McCall7f416cc2015-09-08 08:05:57 +00005424 // The size of the actual thing passed, which might end up just
5425 // being a pointer for indirect types.
5426 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5427
5428 // Arguments bigger than 16 bytes which aren't homogeneous
5429 // aggregates should be passed indirectly.
5430 bool IsIndirect = false;
5431 if (TyInfo.first.getQuantity() > 16) {
5432 const Type *Base = nullptr;
5433 uint64_t Members = 0;
5434 IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
Tim Northovera2ee4332014-03-29 15:09:45 +00005435 }
5436
John McCall7f416cc2015-09-08 08:05:57 +00005437 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
5438 TyInfo, SlotSize, /*AllowHigherAlign*/ true);
Tim Northovera2ee4332014-03-29 15:09:45 +00005439}
5440
Martin Storsjo502de222017-07-13 17:59:14 +00005441Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5442 QualType Ty) const {
5443 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
5444 CGF.getContext().getTypeInfoInChars(Ty),
5445 CharUnits::fromQuantity(8),
5446 /*allowHigherAlign*/ false);
5447}
5448
Tim Northovera2ee4332014-03-29 15:09:45 +00005449//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00005450// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00005451//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00005452
5453namespace {
5454
John McCall12f23522016-04-04 18:33:08 +00005455class ARMABIInfo : public SwiftABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00005456public:
5457 enum ABIKind {
5458 APCS = 0,
5459 AAPCS = 1,
Tim Northover5627d392015-10-30 16:30:45 +00005460 AAPCS_VFP = 2,
5461 AAPCS16_VFP = 3,
Daniel Dunbar020daa92009-09-12 01:00:39 +00005462 };
5463
5464private:
5465 ABIKind Kind;
5466
5467public:
John McCall12f23522016-04-04 18:33:08 +00005468 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
5469 : SwiftABIInfo(CGT), Kind(_Kind) {
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005470 setCCs();
John McCall882987f2013-02-28 19:01:20 +00005471 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00005472
John McCall3480ef22011-08-30 01:42:09 +00005473 bool isEABI() const {
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00005474 switch (getTarget().getTriple().getEnvironment()) {
5475 case llvm::Triple::Android:
5476 case llvm::Triple::EABI:
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00005477 case llvm::Triple::EABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00005478 case llvm::Triple::GNUEABI:
Joerg Sonnenberger0c1652d2013-12-16 18:30:28 +00005479 case llvm::Triple::GNUEABIHF:
Rafael Espindola0fa66802016-06-24 21:35:06 +00005480 case llvm::Triple::MuslEABI:
5481 case llvm::Triple::MuslEABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00005482 return true;
5483 default:
5484 return false;
5485 }
John McCall3480ef22011-08-30 01:42:09 +00005486 }
5487
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00005488 bool isEABIHF() const {
5489 switch (getTarget().getTriple().getEnvironment()) {
5490 case llvm::Triple::EABIHF:
5491 case llvm::Triple::GNUEABIHF:
Rafael Espindola0fa66802016-06-24 21:35:06 +00005492 case llvm::Triple::MuslEABIHF:
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00005493 return true;
5494 default:
5495 return false;
5496 }
5497 }
5498
Daniel Dunbar020daa92009-09-12 01:00:39 +00005499 ABIKind getABIKind() const { return Kind; }
5500
Tim Northovera484bc02013-10-01 14:34:25 +00005501private:
Amara Emerson9dc78782014-01-28 10:56:36 +00005502 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
Tim Northoverbc784d12015-02-24 17:22:40 +00005503 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
Manman Renfef9e312012-10-16 19:18:39 +00005504 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005505
Reid Klecknere9f6a712014-10-31 17:10:41 +00005506 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5507 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5508 uint64_t Members) const override;
5509
Craig Topper4f12f102014-03-12 06:41:41 +00005510 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005511
John McCall7f416cc2015-09-08 08:05:57 +00005512 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5513 QualType Ty) const override;
John McCall882987f2013-02-28 19:01:20 +00005514
5515 llvm::CallingConv::ID getLLVMDefaultCC() const;
5516 llvm::CallingConv::ID getABIDefaultCC() const;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005517 void setCCs();
John McCall12f23522016-04-04 18:33:08 +00005518
John McCall56331e22018-01-07 06:28:49 +00005519 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
John McCall12f23522016-04-04 18:33:08 +00005520 bool asReturnValue) const override {
5521 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5522 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00005523 bool isSwiftErrorInRegister() const override {
5524 return true;
5525 }
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00005526 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5527 unsigned elts) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005528};
5529
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005530class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
5531public:
Chris Lattner2b037972010-07-29 02:01:43 +00005532 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5533 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00005534
John McCall3480ef22011-08-30 01:42:09 +00005535 const ARMABIInfo &getABIInfo() const {
5536 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
5537 }
5538
Craig Topper4f12f102014-03-12 06:41:41 +00005539 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallbeec5a02010-03-06 00:35:14 +00005540 return 13;
5541 }
Roman Divackyc1617352011-05-18 19:36:54 +00005542
Craig Topper4f12f102014-03-12 06:41:41 +00005543 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
Oliver Stannard7f188642017-08-21 09:54:46 +00005544 return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
John McCall31168b02011-06-15 23:02:42 +00005545 }
5546
Roman Divackyc1617352011-05-18 19:36:54 +00005547 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00005548 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00005549 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divackyc1617352011-05-18 19:36:54 +00005550
5551 // 0-15 are the 16 integer registers.
Chris Lattnerece04092012-02-07 00:39:47 +00005552 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divackyc1617352011-05-18 19:36:54 +00005553 return false;
5554 }
John McCall3480ef22011-08-30 01:42:09 +00005555
Craig Topper4f12f102014-03-12 06:41:41 +00005556 unsigned getSizeOfUnwindException() const override {
John McCall3480ef22011-08-30 01:42:09 +00005557 if (getABIInfo().isEABI()) return 88;
5558 return TargetCodeGenInfo::getSizeOfUnwindException();
5559 }
Tim Northovera484bc02013-10-01 14:34:25 +00005560
Eric Christopher162c91c2015-06-05 22:03:00 +00005561 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00005562 CodeGen::CodeGenModule &CGM) const override {
5563 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005564 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00005565 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Tim Northovera484bc02013-10-01 14:34:25 +00005566 if (!FD)
5567 return;
5568
5569 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
5570 if (!Attr)
5571 return;
5572
5573 const char *Kind;
5574 switch (Attr->getInterrupt()) {
5575 case ARMInterruptAttr::Generic: Kind = ""; break;
5576 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
5577 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
5578 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
5579 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
5580 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
5581 }
5582
5583 llvm::Function *Fn = cast<llvm::Function>(GV);
5584
5585 Fn->addFnAttr("interrupt", Kind);
5586
Tim Northover5627d392015-10-30 16:30:45 +00005587 ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
5588 if (ABI == ARMABIInfo::APCS)
Tim Northovera484bc02013-10-01 14:34:25 +00005589 return;
5590
5591 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
5592 // however this is not necessarily true on taking any interrupt. Instruct
5593 // the backend to perform a realignment as part of the function prologue.
5594 llvm::AttrBuilder B;
5595 B.addStackAlignmentAttr(8);
Reid Kleckneree4930b2017-05-02 22:07:37 +00005596 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
Tim Northovera484bc02013-10-01 14:34:25 +00005597 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005598};
5599
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005600class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005601public:
5602 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5603 : ARMTargetCodeGenInfo(CGT, K) {}
5604
Eric Christopher162c91c2015-06-05 22:03:00 +00005605 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00005606 CodeGen::CodeGenModule &CGM) const override;
Saleem Abdulrasool6e9e88b2016-06-23 13:45:33 +00005607
5608 void getDependentLibraryOption(llvm::StringRef Lib,
5609 llvm::SmallString<24> &Opt) const override {
5610 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5611 }
5612
5613 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5614 llvm::SmallString<32> &Opt) const override {
5615 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5616 }
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005617};
5618
Eric Christopher162c91c2015-06-05 22:03:00 +00005619void WindowsARMTargetCodeGenInfo::setTargetAttributes(
Rafael Espindoladeb10be2018-02-07 19:04:41 +00005620 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
5621 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
5622 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005623 return;
Hans Wennborgd43f40d2018-02-23 13:47:36 +00005624 addStackProbeTargetAttributes(D, GV, CGM);
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005625}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005626}
Daniel Dunbard59655c2009-09-12 00:59:49 +00005627
Chris Lattner22326a12010-07-29 02:31:05 +00005628void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakabe7daa32018-03-12 17:05:06 +00005629 if (!getCXXABI().classifyReturnType(FI))
Eric Christopher7565e0d2015-05-29 23:09:49 +00005630 FI.getReturnInfo() =
5631 classifyReturnType(FI.getReturnType(), FI.isVariadic());
Oliver Stannard405bded2014-02-11 09:25:50 +00005632
Tim Northoverbc784d12015-02-24 17:22:40 +00005633 for (auto &I : FI.arguments())
5634 I.info = classifyArgumentType(I.type, FI.isVariadic());
Daniel Dunbar020daa92009-09-12 01:00:39 +00005635
Anton Korobeynikov231e8752011-04-14 20:06:49 +00005636 // Always honor user-specified calling convention.
5637 if (FI.getCallingConvention() != llvm::CallingConv::C)
5638 return;
5639
John McCall882987f2013-02-28 19:01:20 +00005640 llvm::CallingConv::ID cc = getRuntimeCC();
5641 if (cc != llvm::CallingConv::C)
Tim Northoverbc784d12015-02-24 17:22:40 +00005642 FI.setEffectiveCallingConvention(cc);
John McCall882987f2013-02-28 19:01:20 +00005643}
Rafael Espindolaa92c4422010-06-16 16:13:39 +00005644
John McCall882987f2013-02-28 19:01:20 +00005645/// Return the default calling convention that LLVM will use.
5646llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
5647 // The default calling convention that LLVM will infer.
Tim Northoverd88ecb32016-01-27 19:32:40 +00005648 if (isEABIHF() || getTarget().getTriple().isWatchABI())
John McCall882987f2013-02-28 19:01:20 +00005649 return llvm::CallingConv::ARM_AAPCS_VFP;
5650 else if (isEABI())
5651 return llvm::CallingConv::ARM_AAPCS;
5652 else
5653 return llvm::CallingConv::ARM_APCS;
5654}
5655
5656/// Return the calling convention that our ABI would like us to use
5657/// as the C calling convention.
5658llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar020daa92009-09-12 01:00:39 +00005659 switch (getABIKind()) {
John McCall882987f2013-02-28 19:01:20 +00005660 case APCS: return llvm::CallingConv::ARM_APCS;
5661 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
5662 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Tim Northover5627d392015-10-30 16:30:45 +00005663 case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar020daa92009-09-12 01:00:39 +00005664 }
John McCall882987f2013-02-28 19:01:20 +00005665 llvm_unreachable("bad ABI kind");
5666}
5667
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005668void ARMABIInfo::setCCs() {
John McCall882987f2013-02-28 19:01:20 +00005669 assert(getRuntimeCC() == llvm::CallingConv::C);
5670
5671 // Don't muddy up the IR with a ton of explicit annotations if
5672 // they'd just match what LLVM will infer from the triple.
5673 llvm::CallingConv::ID abiCC = getABIDefaultCC();
5674 if (abiCC != getLLVMDefaultCC())
5675 RuntimeCC = abiCC;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005676
Tim Northover5627d392015-10-30 16:30:45 +00005677 // AAPCS apparently requires runtime support functions to be soft-float, but
5678 // that's almost certainly for historic reasons (Thumb1 not supporting VFP
5679 // most likely). It's more convenient for AAPCS16_VFP to be hard-float.
Peter Smith32e26752017-07-27 10:43:53 +00005680
5681 // The Run-time ABI for the ARM Architecture section 4.1.2 requires
5682 // AEABI-complying FP helper functions to use the base AAPCS.
5683 // These AEABI functions are expanded in the ARM llvm backend, all the builtin
5684 // support functions emitted by clang such as the _Complex helpers follow the
5685 // abiCC.
5686 if (abiCC != getLLVMDefaultCC())
Tim Northover5627d392015-10-30 16:30:45 +00005687 BuiltinCC = abiCC;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005688}
5689
Tim Northoverbc784d12015-02-24 17:22:40 +00005690ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
5691 bool isVariadic) const {
Manman Ren2a523d82012-10-30 23:21:41 +00005692 // 6.1.2.1 The following argument types are VFP CPRCs:
5693 // A single-precision floating-point type (including promoted
5694 // half-precision types); A double-precision floating-point type;
5695 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
5696 // with a Base Type of a single- or double-precision floating-point type,
5697 // 64-bit containerized vectors or 128-bit containerized vectors with one
5698 // to four Elements.
Tim Northover5a1558e2014-11-07 22:30:50 +00005699 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005700
Reid Klecknerb1be6832014-11-15 01:41:41 +00005701 Ty = useFirstFieldIfTransparentUnion(Ty);
5702
Manman Renfef9e312012-10-16 19:18:39 +00005703 // Handle illegal vector types here.
5704 if (isIllegalVectorType(Ty)) {
5705 uint64_t Size = getContext().getTypeSize(Ty);
5706 if (Size <= 32) {
5707 llvm::Type *ResType =
5708 llvm::Type::getInt32Ty(getVMContext());
Tim Northover5a1558e2014-11-07 22:30:50 +00005709 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005710 }
5711 if (Size == 64) {
5712 llvm::Type *ResType = llvm::VectorType::get(
5713 llvm::Type::getInt32Ty(getVMContext()), 2);
Tim Northover5a1558e2014-11-07 22:30:50 +00005714 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005715 }
5716 if (Size == 128) {
5717 llvm::Type *ResType = llvm::VectorType::get(
5718 llvm::Type::getInt32Ty(getVMContext()), 4);
Tim Northover5a1558e2014-11-07 22:30:50 +00005719 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005720 }
John McCall7f416cc2015-09-08 08:05:57 +00005721 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Manman Renfef9e312012-10-16 19:18:39 +00005722 }
5723
Sjoerd Meijerca8f4e72018-01-23 10:13:49 +00005724 // _Float16 and __fp16 get passed as if it were an int or float, but with
5725 // the top 16 bits unspecified. This is not done for OpenCL as it handles the
5726 // half type natively, and does not need to interwork with AAPCS code.
5727 if ((Ty->isFloat16Type() || Ty->isHalfType()) &&
5728 !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005729 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5730 llvm::Type::getFloatTy(getVMContext()) :
5731 llvm::Type::getInt32Ty(getVMContext());
5732 return ABIArgInfo::getDirect(ResType);
5733 }
5734
John McCalla1dee5302010-08-22 10:59:02 +00005735 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005736 // Treat an enum type as its underlying type.
Oliver Stannard405bded2014-02-11 09:25:50 +00005737 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005738 Ty = EnumTy->getDecl()->getIntegerType();
Oliver Stannard405bded2014-02-11 09:25:50 +00005739 }
Douglas Gregora71cc152010-02-02 20:10:50 +00005740
Alex Bradburye41a5e22018-01-12 20:08:16 +00005741 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
Tim Northover5a1558e2014-11-07 22:30:50 +00005742 : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00005743 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005744
Oliver Stannard405bded2014-02-11 09:25:50 +00005745 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
John McCall7f416cc2015-09-08 08:05:57 +00005746 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Oliver Stannard405bded2014-02-11 09:25:50 +00005747 }
Tim Northover1060eae2013-06-21 22:49:34 +00005748
Daniel Dunbar09d33622009-09-14 21:54:03 +00005749 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005750 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00005751 return ABIArgInfo::getIgnore();
5752
Tim Northover5a1558e2014-11-07 22:30:50 +00005753 if (IsEffectivelyAAPCS_VFP) {
Manman Ren2a523d82012-10-30 23:21:41 +00005754 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
5755 // into VFP registers.
Craig Topper8a13c412014-05-21 05:09:00 +00005756 const Type *Base = nullptr;
Manman Ren2a523d82012-10-30 23:21:41 +00005757 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005758 if (isHomogeneousAggregate(Ty, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005759 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Ren2a523d82012-10-30 23:21:41 +00005760 // Base can be a floating-point or a vector.
Tim Northover5a1558e2014-11-07 22:30:50 +00005761 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005762 }
Tim Northover5627d392015-10-30 16:30:45 +00005763 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5764 // WatchOS does have homogeneous aggregates. Note that we intentionally use
5765 // this convention even for a variadic function: the backend will use GPRs
5766 // if needed.
5767 const Type *Base = nullptr;
5768 uint64_t Members = 0;
5769 if (isHomogeneousAggregate(Ty, Base, Members)) {
5770 assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
5771 llvm::Type *Ty =
5772 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
5773 return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5774 }
5775 }
5776
5777 if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5778 getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
5779 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
5780 // bigger than 128-bits, they get placed in space allocated by the caller,
5781 // and a pointer is passed.
5782 return ABIArgInfo::getIndirect(
5783 CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
Bob Wilsone826a2a2011-08-03 05:58:22 +00005784 }
5785
Manman Ren6c30e132012-08-13 21:23:55 +00005786 // Support byval for ARM.
Manman Ren77b02382012-11-06 19:05:29 +00005787 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
5788 // most 8-byte. We realign the indirect argument if type alignment is bigger
5789 // than ABI alignment.
Manman Ren505d68f2012-11-05 22:42:46 +00005790 uint64_t ABIAlign = 4;
5791 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
5792 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
Tim Northoverd157e192015-03-09 21:40:42 +00005793 getABIKind() == ARMABIInfo::AAPCS)
Manman Ren505d68f2012-11-05 22:42:46 +00005794 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Tim Northoverd157e192015-03-09 21:40:42 +00005795
Manman Ren8cd99812012-11-06 04:58:01 +00005796 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
Tim Northover5627d392015-10-30 16:30:45 +00005797 assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
John McCall7f416cc2015-09-08 08:05:57 +00005798 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5799 /*ByVal=*/true,
5800 /*Realign=*/TyAlign > ABIAlign);
Eli Friedmane66abda2012-08-09 00:31:40 +00005801 }
5802
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005803 // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
5804 // same size and alignment.
5805 if (getTarget().isRenderScriptTarget()) {
5806 return coerceToIntArray(Ty, getContext(), getVMContext());
5807 }
5808
Daniel Dunbarb34b0802010-09-23 01:54:28 +00005809 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2192fe52011-07-18 04:24:23 +00005810 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005811 unsigned SizeRegs;
Eli Friedmane66abda2012-08-09 00:31:40 +00005812 // FIXME: Try to match the types of the arguments more accurately where
5813 // we can.
5814 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson8e2b75d2011-08-01 23:39:04 +00005815 ElemTy = llvm::Type::getInt32Ty(getVMContext());
5816 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren6fdb1582012-06-25 22:04:00 +00005817 } else {
Manman Ren6fdb1582012-06-25 22:04:00 +00005818 ElemTy = llvm::Type::getInt64Ty(getVMContext());
5819 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00005820 }
Stuart Hastings4b214952011-04-28 18:16:06 +00005821
Tim Northover5a1558e2014-11-07 22:30:50 +00005822 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005823}
5824
Chris Lattner458b2aa2010-07-29 02:16:43 +00005825static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005826 llvm::LLVMContext &VMContext) {
5827 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
5828 // is called integer-like if its size is less than or equal to one word, and
5829 // the offset of each of its addressable sub-fields is zero.
5830
5831 uint64_t Size = Context.getTypeSize(Ty);
5832
5833 // Check that the type fits in a word.
5834 if (Size > 32)
5835 return false;
5836
5837 // FIXME: Handle vector types!
5838 if (Ty->isVectorType())
5839 return false;
5840
Daniel Dunbard53bac72009-09-14 02:20:34 +00005841 // Float types are never treated as "integer like".
5842 if (Ty->isRealFloatingType())
5843 return false;
5844
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005845 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00005846 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005847 return true;
5848
Daniel Dunbar96ebba52010-02-01 23:31:26 +00005849 // Small complex integer types are "integer like".
5850 if (const ComplexType *CT = Ty->getAs<ComplexType>())
5851 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005852
5853 // Single element and zero sized arrays should be allowed, by the definition
5854 // above, but they are not.
5855
5856 // Otherwise, it must be a record type.
5857 const RecordType *RT = Ty->getAs<RecordType>();
5858 if (!RT) return false;
5859
5860 // Ignore records with flexible arrays.
5861 const RecordDecl *RD = RT->getDecl();
5862 if (RD->hasFlexibleArrayMember())
5863 return false;
5864
5865 // Check that all sub-fields are at offset 0, and are themselves "integer
5866 // like".
5867 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
5868
5869 bool HadField = false;
5870 unsigned idx = 0;
5871 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5872 i != e; ++i, ++idx) {
David Blaikie40ed2972012-06-06 20:45:41 +00005873 const FieldDecl *FD = *i;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005874
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005875 // Bit-fields are not addressable, we only need to verify they are "integer
5876 // like". We still have to disallow a subsequent non-bitfield, for example:
5877 // struct { int : 0; int x }
5878 // is non-integer like according to gcc.
5879 if (FD->isBitField()) {
5880 if (!RD->isUnion())
5881 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005882
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005883 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5884 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005885
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005886 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005887 }
5888
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005889 // Check if this field is at offset 0.
5890 if (Layout.getFieldOffset(idx) != 0)
5891 return false;
5892
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005893 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5894 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00005895
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005896 // Only allow at most one field in a structure. This doesn't match the
5897 // wording above, but follows gcc in situations with a field following an
5898 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005899 if (!RD->isUnion()) {
5900 if (HadField)
5901 return false;
5902
5903 HadField = true;
5904 }
5905 }
5906
5907 return true;
5908}
5909
Oliver Stannard405bded2014-02-11 09:25:50 +00005910ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
5911 bool isVariadic) const {
Tim Northover5627d392015-10-30 16:30:45 +00005912 bool IsEffectivelyAAPCS_VFP =
5913 (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005914
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005915 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005916 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005917
Daniel Dunbar19964db2010-09-23 01:54:32 +00005918 // Large vector types should be returned via memory.
Oliver Stannard405bded2014-02-11 09:25:50 +00005919 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
John McCall7f416cc2015-09-08 08:05:57 +00005920 return getNaturalAlignIndirect(RetTy);
Oliver Stannard405bded2014-02-11 09:25:50 +00005921 }
Daniel Dunbar19964db2010-09-23 01:54:32 +00005922
Sjoerd Meijerca8f4e72018-01-23 10:13:49 +00005923 // _Float16 and __fp16 get returned as if it were an int or float, but with
5924 // the top 16 bits unspecified. This is not done for OpenCL as it handles the
5925 // half type natively, and does not need to interwork with AAPCS code.
5926 if ((RetTy->isFloat16Type() || RetTy->isHalfType()) &&
5927 !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005928 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5929 llvm::Type::getFloatTy(getVMContext()) :
5930 llvm::Type::getInt32Ty(getVMContext());
5931 return ABIArgInfo::getDirect(ResType);
5932 }
5933
John McCalla1dee5302010-08-22 10:59:02 +00005934 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005935 // Treat an enum type as its underlying type.
5936 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5937 RetTy = EnumTy->getDecl()->getIntegerType();
5938
Alex Bradburye41a5e22018-01-12 20:08:16 +00005939 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
Tim Northover5a1558e2014-11-07 22:30:50 +00005940 : ABIArgInfo::getDirect();
Douglas Gregora71cc152010-02-02 20:10:50 +00005941 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005942
5943 // Are we following APCS?
5944 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00005945 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005946 return ABIArgInfo::getIgnore();
5947
Daniel Dunbareedf1512010-02-01 23:31:19 +00005948 // Complex types are all returned as packed integers.
5949 //
5950 // FIXME: Consider using 2 x vector types if the back end handles them
5951 // correctly.
5952 if (RetTy->isAnyComplexType())
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005953 return ABIArgInfo::getDirect(llvm::IntegerType::get(
5954 getVMContext(), getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00005955
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005956 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005957 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005958 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005959 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005960 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00005961 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005962 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00005963 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5964 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005965 }
5966
5967 // Otherwise return in memory.
John McCall7f416cc2015-09-08 08:05:57 +00005968 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005969 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005970
5971 // Otherwise this is an AAPCS variant.
5972
Chris Lattner458b2aa2010-07-29 02:16:43 +00005973 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005974 return ABIArgInfo::getIgnore();
5975
Bob Wilson1d9269a2011-11-02 04:51:36 +00005976 // Check for homogeneous aggregates with AAPCS-VFP.
Tim Northover5a1558e2014-11-07 22:30:50 +00005977 if (IsEffectivelyAAPCS_VFP) {
Craig Topper8a13c412014-05-21 05:09:00 +00005978 const Type *Base = nullptr;
Tim Northover5627d392015-10-30 16:30:45 +00005979 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005980 if (isHomogeneousAggregate(RetTy, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005981 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson1d9269a2011-11-02 04:51:36 +00005982 // Homogeneous Aggregates are returned directly.
Tim Northover5a1558e2014-11-07 22:30:50 +00005983 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005984 }
Bob Wilson1d9269a2011-11-02 04:51:36 +00005985 }
5986
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005987 // Aggregates <= 4 bytes are returned in r0; other aggregates
5988 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005989 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005990 if (Size <= 32) {
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005991 // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
5992 // same size and alignment.
5993 if (getTarget().isRenderScriptTarget()) {
5994 return coerceToIntArray(RetTy, getContext(), getVMContext());
5995 }
Christian Pirkerc3d32172014-07-03 09:28:12 +00005996 if (getDataLayout().isBigEndian())
5997 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
Tim Northover5a1558e2014-11-07 22:30:50 +00005998 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Christian Pirkerc3d32172014-07-03 09:28:12 +00005999
Daniel Dunbar1ce72512009-09-14 00:56:55 +00006000 // Return in the smallest viable integer type.
6001 if (Size <= 8)
Tim Northover5a1558e2014-11-07 22:30:50 +00006002 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00006003 if (Size <= 16)
Tim Northover5a1558e2014-11-07 22:30:50 +00006004 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6005 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Tim Northover5627d392015-10-30 16:30:45 +00006006 } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
6007 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
6008 llvm::Type *CoerceTy =
Rui Ueyama83aa9792016-01-14 21:00:27 +00006009 llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
Tim Northover5627d392015-10-30 16:30:45 +00006010 return ABIArgInfo::getDirect(CoerceTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00006011 }
6012
John McCall7f416cc2015-09-08 08:05:57 +00006013 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006014}
6015
Manman Renfef9e312012-10-16 19:18:39 +00006016/// isIllegalVector - check whether Ty is an illegal vector type.
6017bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
Stephen Hines8267e7d2015-12-04 01:39:30 +00006018 if (const VectorType *VT = Ty->getAs<VectorType> ()) {
6019 if (isAndroid()) {
6020 // Android shipped using Clang 3.1, which supported a slightly different
6021 // vector ABI. The primary differences were that 3-element vector types
6022 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
6023 // accepts that legacy behavior for Android only.
6024 // Check whether VT is legal.
6025 unsigned NumElements = VT->getNumElements();
6026 // NumElements should be power of 2 or equal to 3.
6027 if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
6028 return true;
6029 } else {
6030 // Check whether VT is legal.
6031 unsigned NumElements = VT->getNumElements();
6032 uint64_t Size = getContext().getTypeSize(VT);
6033 // NumElements should be power of 2.
6034 if (!llvm::isPowerOf2_32(NumElements))
6035 return true;
6036 // Size should be greater than 32 bits.
6037 return Size <= 32;
6038 }
Manman Renfef9e312012-10-16 19:18:39 +00006039 }
6040 return false;
6041}
6042
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00006043bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
6044 llvm::Type *eltTy,
6045 unsigned numElts) const {
6046 if (!llvm::isPowerOf2_32(numElts))
6047 return false;
6048 unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
6049 if (size > 64)
6050 return false;
6051 if (vectorSize.getQuantity() != 8 &&
6052 (vectorSize.getQuantity() != 16 || numElts == 1))
6053 return false;
6054 return true;
6055}
6056
Reid Klecknere9f6a712014-10-31 17:10:41 +00006057bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
6058 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
6059 // double, or 64-bit or 128-bit vectors.
6060 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
6061 if (BT->getKind() == BuiltinType::Float ||
6062 BT->getKind() == BuiltinType::Double ||
6063 BT->getKind() == BuiltinType::LongDouble)
6064 return true;
6065 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
6066 unsigned VecSize = getContext().getTypeSize(VT);
6067 if (VecSize == 64 || VecSize == 128)
6068 return true;
6069 }
6070 return false;
6071}
6072
6073bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
6074 uint64_t Members) const {
6075 return Members <= 4;
6076}
6077
John McCall7f416cc2015-09-08 08:05:57 +00006078Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6079 QualType Ty) const {
6080 CharUnits SlotSize = CharUnits::fromQuantity(4);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006081
John McCall7f416cc2015-09-08 08:05:57 +00006082 // Empty records are ignored for parameter passing purposes.
Tim Northover1711cc92013-06-21 23:05:33 +00006083 if (isEmptyRecord(getContext(), Ty, true)) {
John McCall7f416cc2015-09-08 08:05:57 +00006084 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
6085 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6086 return Addr;
Tim Northover1711cc92013-06-21 23:05:33 +00006087 }
6088
John McCall7f416cc2015-09-08 08:05:57 +00006089 auto TyInfo = getContext().getTypeInfoInChars(Ty);
6090 CharUnits TyAlignForABI = TyInfo.second;
Manman Rencca54d02012-10-16 19:01:37 +00006091
John McCall7f416cc2015-09-08 08:05:57 +00006092 // Use indirect if size of the illegal vector is bigger than 16 bytes.
6093 bool IsIndirect = false;
Tim Northover5627d392015-10-30 16:30:45 +00006094 const Type *Base = nullptr;
6095 uint64_t Members = 0;
John McCall7f416cc2015-09-08 08:05:57 +00006096 if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
6097 IsIndirect = true;
6098
Tim Northover5627d392015-10-30 16:30:45 +00006099 // ARMv7k passes structs bigger than 16 bytes indirectly, in space
6100 // allocated by the caller.
6101 } else if (TyInfo.first > CharUnits::fromQuantity(16) &&
6102 getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6103 !isHomogeneousAggregate(Ty, Base, Members)) {
6104 IsIndirect = true;
6105
John McCall7f416cc2015-09-08 08:05:57 +00006106 // Otherwise, bound the type's ABI alignment.
Manman Rencca54d02012-10-16 19:01:37 +00006107 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
6108 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
John McCall7f416cc2015-09-08 08:05:57 +00006109 // Our callers should be prepared to handle an under-aligned address.
6110 } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6111 getABIKind() == ARMABIInfo::AAPCS) {
6112 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6113 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
Tim Northover4c5cb9c2015-11-02 19:32:23 +00006114 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6115 // ARMv7k allows type alignment up to 16 bytes.
6116 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6117 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
John McCall7f416cc2015-09-08 08:05:57 +00006118 } else {
6119 TyAlignForABI = CharUnits::fromQuantity(4);
Manman Renfef9e312012-10-16 19:18:39 +00006120 }
John McCall7f416cc2015-09-08 08:05:57 +00006121 TyInfo.second = TyAlignForABI;
Manman Rencca54d02012-10-16 19:01:37 +00006122
John McCall7f416cc2015-09-08 08:05:57 +00006123 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
6124 SlotSize, /*AllowHigherAlign*/ true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006125}
6126
Chris Lattner0cf24192010-06-28 20:05:43 +00006127//===----------------------------------------------------------------------===//
Justin Holewinski83e96682012-05-24 17:43:12 +00006128// NVPTX ABI Implementation
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006129//===----------------------------------------------------------------------===//
6130
6131namespace {
6132
Justin Holewinski83e96682012-05-24 17:43:12 +00006133class NVPTXABIInfo : public ABIInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006134public:
Justin Holewinski36837432013-03-30 14:38:24 +00006135 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006136
6137 ABIArgInfo classifyReturnType(QualType RetTy) const;
6138 ABIArgInfo classifyArgumentType(QualType Ty) const;
6139
Craig Topper4f12f102014-03-12 06:41:41 +00006140 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00006141 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6142 QualType Ty) const override;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006143};
6144
Justin Holewinski83e96682012-05-24 17:43:12 +00006145class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006146public:
Justin Holewinski83e96682012-05-24 17:43:12 +00006147 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
6148 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Craig Topper4f12f102014-03-12 06:41:41 +00006149
Eric Christopher162c91c2015-06-05 22:03:00 +00006150 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00006151 CodeGen::CodeGenModule &M) const override;
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006152
Justin Holewinski36837432013-03-30 14:38:24 +00006153private:
Eli Benderskye06a2c42014-04-15 16:57:05 +00006154 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
6155 // resulting MDNode to the nvvm.annotations MDNode.
6156 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006157};
6158
Justin Holewinski83e96682012-05-24 17:43:12 +00006159ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006160 if (RetTy->isVoidType())
6161 return ABIArgInfo::getIgnore();
Justin Holewinskif9329ff2013-11-20 20:35:34 +00006162
6163 // note: this is different from default ABI
6164 if (!RetTy->isScalarType())
6165 return ABIArgInfo::getDirect();
6166
6167 // Treat an enum type as its underlying type.
6168 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6169 RetTy = EnumTy->getDecl()->getIntegerType();
6170
Alex Bradburye41a5e22018-01-12 20:08:16 +00006171 return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
6172 : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006173}
6174
Justin Holewinski83e96682012-05-24 17:43:12 +00006175ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinskif9329ff2013-11-20 20:35:34 +00006176 // Treat an enum type as its underlying type.
6177 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6178 Ty = EnumTy->getDecl()->getIntegerType();
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006179
Eli Bendersky95338a02014-10-29 13:43:21 +00006180 // Return aggregates type as indirect by value
6181 if (isAggregateTypeForABI(Ty))
John McCall7f416cc2015-09-08 08:05:57 +00006182 return getNaturalAlignIndirect(Ty, /* byval */ true);
Eli Bendersky95338a02014-10-29 13:43:21 +00006183
Alex Bradburye41a5e22018-01-12 20:08:16 +00006184 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
6185 : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006186}
6187
Justin Holewinski83e96682012-05-24 17:43:12 +00006188void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00006189 if (!getCXXABI().classifyReturnType(FI))
6190 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006191 for (auto &I : FI.arguments())
6192 I.info = classifyArgumentType(I.type);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006193
6194 // Always honor user-specified calling convention.
6195 if (FI.getCallingConvention() != llvm::CallingConv::C)
6196 return;
6197
John McCall882987f2013-02-28 19:01:20 +00006198 FI.setEffectiveCallingConvention(getRuntimeCC());
6199}
6200
John McCall7f416cc2015-09-08 08:05:57 +00006201Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6202 QualType Ty) const {
Justin Holewinski83e96682012-05-24 17:43:12 +00006203 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006204}
6205
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006206void NVPTXTargetCodeGenInfo::setTargetAttributes(
Rafael Espindoladeb10be2018-02-07 19:04:41 +00006207 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
6208 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006209 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006210 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Justin Holewinski38031972011-10-05 17:58:44 +00006211 if (!FD) return;
6212
6213 llvm::Function *F = cast<llvm::Function>(GV);
6214
6215 // Perform special handling in OpenCL mode
David Blaikiebbafb8a2012-03-11 07:00:24 +00006216 if (M.getLangOpts().OpenCL) {
Justin Holewinski36837432013-03-30 14:38:24 +00006217 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski38031972011-10-05 17:58:44 +00006218 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00006219 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinski36837432013-03-30 14:38:24 +00006220 // OpenCL __kernel functions get kernel metadata
Eli Benderskye06a2c42014-04-15 16:57:05 +00006221 // Create !{<func-ref>, metadata !"kernel", i32 1} node
6222 addNVVMMetadata(F, "kernel", 1);
Justin Holewinski38031972011-10-05 17:58:44 +00006223 // And kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00006224 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski38031972011-10-05 17:58:44 +00006225 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00006226 }
Justin Holewinski38031972011-10-05 17:58:44 +00006227
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00006228 // Perform special handling in CUDA mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00006229 if (M.getLangOpts().CUDA) {
Justin Holewinski36837432013-03-30 14:38:24 +00006230 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00006231 // __global__ functions cannot be called from the device, we do not
6232 // need to set the noinline attribute.
Eli Benderskye06a2c42014-04-15 16:57:05 +00006233 if (FD->hasAttr<CUDAGlobalAttr>()) {
6234 // Create !{<func-ref>, metadata !"kernel", i32 1} node
6235 addNVVMMetadata(F, "kernel", 1);
6236 }
Artem Belevich7093e402015-04-21 22:55:54 +00006237 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
Eli Benderskye06a2c42014-04-15 16:57:05 +00006238 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
Artem Belevich7093e402015-04-21 22:55:54 +00006239 llvm::APSInt MaxThreads(32);
6240 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
6241 if (MaxThreads > 0)
6242 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
6243
6244 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
6245 // not specified in __launch_bounds__ or if the user specified a 0 value,
6246 // we don't have to add a PTX directive.
6247 if (Attr->getMinBlocks()) {
6248 llvm::APSInt MinBlocks(32);
6249 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
6250 if (MinBlocks > 0)
6251 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
6252 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
Eli Benderskye06a2c42014-04-15 16:57:05 +00006253 }
6254 }
Justin Holewinski38031972011-10-05 17:58:44 +00006255 }
6256}
6257
Eli Benderskye06a2c42014-04-15 16:57:05 +00006258void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
6259 int Operand) {
Justin Holewinski36837432013-03-30 14:38:24 +00006260 llvm::Module *M = F->getParent();
6261 llvm::LLVMContext &Ctx = M->getContext();
6262
6263 // Get "nvvm.annotations" metadata node
6264 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
6265
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00006266 llvm::Metadata *MDVals[] = {
6267 llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
6268 llvm::ConstantAsMetadata::get(
6269 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
Justin Holewinski36837432013-03-30 14:38:24 +00006270 // Append metadata to nvvm.annotations
6271 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
6272}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006273}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006274
6275//===----------------------------------------------------------------------===//
Ulrich Weigand47445072013-05-06 16:26:41 +00006276// SystemZ ABI Implementation
6277//===----------------------------------------------------------------------===//
6278
6279namespace {
6280
Bryan Chane3f1ed52016-04-28 13:56:43 +00006281class SystemZABIInfo : public SwiftABIInfo {
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006282 bool HasVector;
6283
Ulrich Weigand47445072013-05-06 16:26:41 +00006284public:
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006285 SystemZABIInfo(CodeGenTypes &CGT, bool HV)
Bryan Chane3f1ed52016-04-28 13:56:43 +00006286 : SwiftABIInfo(CGT), HasVector(HV) {}
Ulrich Weigand47445072013-05-06 16:26:41 +00006287
6288 bool isPromotableIntegerType(QualType Ty) const;
6289 bool isCompoundType(QualType Ty) const;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006290 bool isVectorArgumentType(QualType Ty) const;
Ulrich Weigand47445072013-05-06 16:26:41 +00006291 bool isFPArgumentType(QualType Ty) const;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006292 QualType GetSingleElementType(QualType Ty) const;
Ulrich Weigand47445072013-05-06 16:26:41 +00006293
6294 ABIArgInfo classifyReturnType(QualType RetTy) const;
6295 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
6296
Craig Topper4f12f102014-03-12 06:41:41 +00006297 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00006298 if (!getCXXABI().classifyReturnType(FI))
6299 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006300 for (auto &I : FI.arguments())
6301 I.info = classifyArgumentType(I.type);
Ulrich Weigand47445072013-05-06 16:26:41 +00006302 }
6303
John McCall7f416cc2015-09-08 08:05:57 +00006304 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6305 QualType Ty) const override;
Bryan Chane3f1ed52016-04-28 13:56:43 +00006306
John McCall56331e22018-01-07 06:28:49 +00006307 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
Bryan Chane3f1ed52016-04-28 13:56:43 +00006308 bool asReturnValue) const override {
6309 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
6310 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00006311 bool isSwiftErrorInRegister() const override {
Arnold Schwaighofer612d6932017-11-07 16:40:51 +00006312 return false;
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00006313 }
Ulrich Weigand47445072013-05-06 16:26:41 +00006314};
6315
6316class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
6317public:
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006318 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
6319 : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
Ulrich Weigand47445072013-05-06 16:26:41 +00006320};
6321
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006322}
Ulrich Weigand47445072013-05-06 16:26:41 +00006323
6324bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
6325 // Treat an enum type as its underlying type.
6326 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6327 Ty = EnumTy->getDecl()->getIntegerType();
6328
6329 // Promotable integer types are required to be promoted by the ABI.
6330 if (Ty->isPromotableIntegerType())
6331 return true;
6332
6333 // 32-bit values must also be promoted.
6334 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6335 switch (BT->getKind()) {
6336 case BuiltinType::Int:
6337 case BuiltinType::UInt:
6338 return true;
6339 default:
6340 return false;
6341 }
6342 return false;
6343}
6344
6345bool SystemZABIInfo::isCompoundType(QualType Ty) const {
Ulrich Weigand759449c2015-03-30 13:49:01 +00006346 return (Ty->isAnyComplexType() ||
6347 Ty->isVectorType() ||
6348 isAggregateTypeForABI(Ty));
Ulrich Weigand47445072013-05-06 16:26:41 +00006349}
6350
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006351bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
6352 return (HasVector &&
6353 Ty->isVectorType() &&
6354 getContext().getTypeSize(Ty) <= 128);
6355}
6356
Ulrich Weigand47445072013-05-06 16:26:41 +00006357bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
6358 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6359 switch (BT->getKind()) {
6360 case BuiltinType::Float:
6361 case BuiltinType::Double:
6362 return true;
6363 default:
6364 return false;
6365 }
6366
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006367 return false;
6368}
6369
6370QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
Ulrich Weigand47445072013-05-06 16:26:41 +00006371 if (const RecordType *RT = Ty->getAsStructureType()) {
6372 const RecordDecl *RD = RT->getDecl();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006373 QualType Found;
Ulrich Weigand47445072013-05-06 16:26:41 +00006374
6375 // If this is a C++ record, check the bases first.
6376 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +00006377 for (const auto &I : CXXRD->bases()) {
6378 QualType Base = I.getType();
Ulrich Weigand47445072013-05-06 16:26:41 +00006379
6380 // Empty bases don't affect things either way.
6381 if (isEmptyRecord(getContext(), Base, true))
6382 continue;
6383
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006384 if (!Found.isNull())
6385 return Ty;
6386 Found = GetSingleElementType(Base);
Ulrich Weigand47445072013-05-06 16:26:41 +00006387 }
6388
6389 // Check the fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00006390 for (const auto *FD : RD->fields()) {
Ulrich Weigand759449c2015-03-30 13:49:01 +00006391 // For compatibility with GCC, ignore empty bitfields in C++ mode.
Ulrich Weigand47445072013-05-06 16:26:41 +00006392 // Unlike isSingleElementStruct(), empty structure and array fields
6393 // do count. So do anonymous bitfields that aren't zero-sized.
Ulrich Weigand759449c2015-03-30 13:49:01 +00006394 if (getContext().getLangOpts().CPlusPlus &&
6395 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
6396 continue;
Ulrich Weigand47445072013-05-06 16:26:41 +00006397
6398 // Unlike isSingleElementStruct(), arrays do not count.
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006399 // Nested structures still do though.
6400 if (!Found.isNull())
6401 return Ty;
6402 Found = GetSingleElementType(FD->getType());
Ulrich Weigand47445072013-05-06 16:26:41 +00006403 }
6404
6405 // Unlike isSingleElementStruct(), trailing padding is allowed.
6406 // An 8-byte aligned struct s { float f; } is passed as a double.
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006407 if (!Found.isNull())
6408 return Found;
Ulrich Weigand47445072013-05-06 16:26:41 +00006409 }
6410
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006411 return Ty;
Ulrich Weigand47445072013-05-06 16:26:41 +00006412}
6413
John McCall7f416cc2015-09-08 08:05:57 +00006414Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6415 QualType Ty) const {
Ulrich Weigand47445072013-05-06 16:26:41 +00006416 // Assume that va_list type is correct; should be pointer to LLVM type:
6417 // struct {
6418 // i64 __gpr;
6419 // i64 __fpr;
6420 // i8 *__overflow_arg_area;
6421 // i8 *__reg_save_area;
6422 // };
6423
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006424 // Every non-vector argument occupies 8 bytes and is passed by preference
6425 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
6426 // always passed on the stack.
John McCall7f416cc2015-09-08 08:05:57 +00006427 Ty = getContext().getCanonicalType(Ty);
6428 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Ulrich Weigand759449c2015-03-30 13:49:01 +00006429 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00006430 llvm::Type *DirectTy = ArgTy;
Ulrich Weigand47445072013-05-06 16:26:41 +00006431 ABIArgInfo AI = classifyArgumentType(Ty);
Ulrich Weigand47445072013-05-06 16:26:41 +00006432 bool IsIndirect = AI.isIndirect();
Ulrich Weigand759449c2015-03-30 13:49:01 +00006433 bool InFPRs = false;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006434 bool IsVector = false;
John McCall7f416cc2015-09-08 08:05:57 +00006435 CharUnits UnpaddedSize;
6436 CharUnits DirectAlign;
Ulrich Weigand47445072013-05-06 16:26:41 +00006437 if (IsIndirect) {
John McCall7f416cc2015-09-08 08:05:57 +00006438 DirectTy = llvm::PointerType::getUnqual(DirectTy);
6439 UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
Ulrich Weigand759449c2015-03-30 13:49:01 +00006440 } else {
6441 if (AI.getCoerceToType())
6442 ArgTy = AI.getCoerceToType();
6443 InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006444 IsVector = ArgTy->isVectorTy();
John McCall7f416cc2015-09-08 08:05:57 +00006445 UnpaddedSize = TyInfo.first;
6446 DirectAlign = TyInfo.second;
Ulrich Weigand759449c2015-03-30 13:49:01 +00006447 }
John McCall7f416cc2015-09-08 08:05:57 +00006448 CharUnits PaddedSize = CharUnits::fromQuantity(8);
6449 if (IsVector && UnpaddedSize > PaddedSize)
6450 PaddedSize = CharUnits::fromQuantity(16);
6451 assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
Ulrich Weigand47445072013-05-06 16:26:41 +00006452
John McCall7f416cc2015-09-08 08:05:57 +00006453 CharUnits Padding = (PaddedSize - UnpaddedSize);
Ulrich Weigand47445072013-05-06 16:26:41 +00006454
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006455 llvm::Type *IndexTy = CGF.Int64Ty;
John McCall7f416cc2015-09-08 08:05:57 +00006456 llvm::Value *PaddedSizeV =
6457 llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006458
6459 if (IsVector) {
6460 // Work out the address of a vector argument on the stack.
6461 // Vector arguments are always passed in the high bits of a
6462 // single (8 byte) or double (16 byte) stack slot.
John McCall7f416cc2015-09-08 08:05:57 +00006463 Address OverflowArgAreaPtr =
6464 CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16),
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006465 "overflow_arg_area_ptr");
John McCall7f416cc2015-09-08 08:05:57 +00006466 Address OverflowArgArea =
6467 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6468 TyInfo.second);
6469 Address MemAddr =
6470 CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006471
6472 // Update overflow_arg_area_ptr pointer
6473 llvm::Value *NewOverflowArgArea =
John McCall7f416cc2015-09-08 08:05:57 +00006474 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6475 "overflow_arg_area");
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006476 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6477
6478 return MemAddr;
6479 }
6480
John McCall7f416cc2015-09-08 08:05:57 +00006481 assert(PaddedSize.getQuantity() == 8);
6482
6483 unsigned MaxRegs, RegCountField, RegSaveIndex;
6484 CharUnits RegPadding;
Ulrich Weigand47445072013-05-06 16:26:41 +00006485 if (InFPRs) {
6486 MaxRegs = 4; // Maximum of 4 FPR arguments
6487 RegCountField = 1; // __fpr
6488 RegSaveIndex = 16; // save offset for f0
John McCall7f416cc2015-09-08 08:05:57 +00006489 RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
Ulrich Weigand47445072013-05-06 16:26:41 +00006490 } else {
6491 MaxRegs = 5; // Maximum of 5 GPR arguments
6492 RegCountField = 0; // __gpr
6493 RegSaveIndex = 2; // save offset for r2
6494 RegPadding = Padding; // values are passed in the low bits of a GPR
6495 }
6496
John McCall7f416cc2015-09-08 08:05:57 +00006497 Address RegCountPtr = CGF.Builder.CreateStructGEP(
6498 VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8),
6499 "reg_count_ptr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006500 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
Ulrich Weigand47445072013-05-06 16:26:41 +00006501 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
6502 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
Oliver Stannard405bded2014-02-11 09:25:50 +00006503 "fits_in_regs");
Ulrich Weigand47445072013-05-06 16:26:41 +00006504
6505 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
6506 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
6507 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
6508 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
6509
6510 // Emit code to load the value if it was passed in registers.
6511 CGF.EmitBlock(InRegBlock);
6512
6513 // Work out the address of an argument register.
Ulrich Weigand47445072013-05-06 16:26:41 +00006514 llvm::Value *ScaledRegCount =
6515 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
6516 llvm::Value *RegBase =
John McCall7f416cc2015-09-08 08:05:57 +00006517 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
6518 + RegPadding.getQuantity());
Ulrich Weigand47445072013-05-06 16:26:41 +00006519 llvm::Value *RegOffset =
6520 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
John McCall7f416cc2015-09-08 08:05:57 +00006521 Address RegSaveAreaPtr =
6522 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
6523 "reg_save_area_ptr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006524 llvm::Value *RegSaveArea =
6525 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
John McCall7f416cc2015-09-08 08:05:57 +00006526 Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
6527 "raw_reg_addr"),
6528 PaddedSize);
6529 Address RegAddr =
6530 CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006531
6532 // Update the register count
6533 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
6534 llvm::Value *NewRegCount =
6535 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
6536 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
6537 CGF.EmitBranch(ContBlock);
6538
6539 // Emit code to load the value if it was passed in memory.
6540 CGF.EmitBlock(InMemBlock);
6541
6542 // Work out the address of a stack argument.
John McCall7f416cc2015-09-08 08:05:57 +00006543 Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
6544 VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr");
6545 Address OverflowArgArea =
6546 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6547 PaddedSize);
6548 Address RawMemAddr =
6549 CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
6550 Address MemAddr =
6551 CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006552
6553 // Update overflow_arg_area_ptr pointer
6554 llvm::Value *NewOverflowArgArea =
John McCall7f416cc2015-09-08 08:05:57 +00006555 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6556 "overflow_arg_area");
Ulrich Weigand47445072013-05-06 16:26:41 +00006557 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6558 CGF.EmitBranch(ContBlock);
6559
6560 // Return the appropriate result.
6561 CGF.EmitBlock(ContBlock);
John McCall7f416cc2015-09-08 08:05:57 +00006562 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6563 MemAddr, InMemBlock, "va_arg.addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006564
6565 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00006566 ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
6567 TyInfo.second);
Ulrich Weigand47445072013-05-06 16:26:41 +00006568
6569 return ResAddr;
6570}
6571
Ulrich Weigand47445072013-05-06 16:26:41 +00006572ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
6573 if (RetTy->isVoidType())
6574 return ABIArgInfo::getIgnore();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006575 if (isVectorArgumentType(RetTy))
6576 return ABIArgInfo::getDirect();
Ulrich Weigand47445072013-05-06 16:26:41 +00006577 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
John McCall7f416cc2015-09-08 08:05:57 +00006578 return getNaturalAlignIndirect(RetTy);
Alex Bradburye41a5e22018-01-12 20:08:16 +00006579 return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy)
6580 : ABIArgInfo::getDirect());
Ulrich Weigand47445072013-05-06 16:26:41 +00006581}
6582
6583ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
6584 // Handle the generic C++ ABI.
Mark Lacey3825e832013-10-06 01:33:34 +00006585 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00006586 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand47445072013-05-06 16:26:41 +00006587
6588 // Integers and enums are extended to full register width.
6589 if (isPromotableIntegerType(Ty))
Alex Bradburye41a5e22018-01-12 20:08:16 +00006590 return ABIArgInfo::getExtend(Ty);
Ulrich Weigand47445072013-05-06 16:26:41 +00006591
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006592 // Handle vector types and vector-like structure types. Note that
6593 // as opposed to float-like structure types, we do not allow any
6594 // padding for vector-like structures, so verify the sizes match.
Ulrich Weigand47445072013-05-06 16:26:41 +00006595 uint64_t Size = getContext().getTypeSize(Ty);
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006596 QualType SingleElementTy = GetSingleElementType(Ty);
6597 if (isVectorArgumentType(SingleElementTy) &&
6598 getContext().getTypeSize(SingleElementTy) == Size)
6599 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
6600
6601 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
Ulrich Weigand47445072013-05-06 16:26:41 +00006602 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
John McCall7f416cc2015-09-08 08:05:57 +00006603 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00006604
6605 // Handle small structures.
6606 if (const RecordType *RT = Ty->getAs<RecordType>()) {
6607 // Structures with flexible arrays have variable length, so really
6608 // fail the size test above.
6609 const RecordDecl *RD = RT->getDecl();
6610 if (RD->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00006611 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00006612
6613 // The structure is passed as an unextended integer, a float, or a double.
6614 llvm::Type *PassTy;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006615 if (isFPArgumentType(SingleElementTy)) {
Ulrich Weigand47445072013-05-06 16:26:41 +00006616 assert(Size == 32 || Size == 64);
6617 if (Size == 32)
6618 PassTy = llvm::Type::getFloatTy(getVMContext());
6619 else
6620 PassTy = llvm::Type::getDoubleTy(getVMContext());
6621 } else
6622 PassTy = llvm::IntegerType::get(getVMContext(), Size);
6623 return ABIArgInfo::getDirect(PassTy);
6624 }
6625
6626 // Non-structure compounds are passed indirectly.
6627 if (isCompoundType(Ty))
John McCall7f416cc2015-09-08 08:05:57 +00006628 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00006629
Craig Topper8a13c412014-05-21 05:09:00 +00006630 return ABIArgInfo::getDirect(nullptr);
Ulrich Weigand47445072013-05-06 16:26:41 +00006631}
6632
6633//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006634// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00006635//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006636
6637namespace {
6638
6639class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
6640public:
Chris Lattner2b037972010-07-29 02:01:43 +00006641 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
6642 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Eric Christopher162c91c2015-06-05 22:03:00 +00006643 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00006644 CodeGen::CodeGenModule &M) const override;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006645};
6646
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006647}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006648
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006649void MSP430TargetCodeGenInfo::setTargetAttributes(
Rafael Espindoladeb10be2018-02-07 19:04:41 +00006650 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
6651 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006652 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006653 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006654 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
6655 // Handle 'interrupt' attribute:
6656 llvm::Function *F = cast<llvm::Function>(GV);
6657
6658 // Step 1: Set ISR calling convention.
6659 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
6660
6661 // Step 2: Add attributes goodness.
Bill Wendling207f0532012-12-20 19:27:06 +00006662 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006663
6664 // Step 3: Emit ISR vector alias.
Anton Korobeynikovc5a7f922012-11-26 18:59:10 +00006665 unsigned Num = attr->getNumber() / 2;
Rafael Espindola234405b2014-05-17 21:30:14 +00006666 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
6667 "__isr_" + Twine(Num), F);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006668 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006669 }
6670}
6671
Chris Lattner0cf24192010-06-28 20:05:43 +00006672//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00006673// MIPS ABI Implementation. This works for both little-endian and
6674// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00006675//===----------------------------------------------------------------------===//
6676
John McCall943fae92010-05-27 06:19:26 +00006677namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00006678class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00006679 bool IsO32;
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006680 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
6681 void CoerceToIntArgs(uint64_t TySize,
Craig Topper5603df42013-07-05 19:34:19 +00006682 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006683 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006684 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006685 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006686public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006687 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006688 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006689 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00006690
6691 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006692 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Craig Topper4f12f102014-03-12 06:41:41 +00006693 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00006694 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6695 QualType Ty) const override;
Alex Bradburye41a5e22018-01-12 20:08:16 +00006696 ABIArgInfo extendType(QualType Ty) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006697};
6698
John McCall943fae92010-05-27 06:19:26 +00006699class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00006700 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00006701public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006702 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
6703 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
Akira Hatanaka14378522011-11-02 23:14:57 +00006704 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00006705
Craig Topper4f12f102014-03-12 06:41:41 +00006706 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCall943fae92010-05-27 06:19:26 +00006707 return 29;
6708 }
6709
Eric Christopher162c91c2015-06-05 22:03:00 +00006710 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00006711 CodeGen::CodeGenModule &CGM) const override {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006712 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Reed Kotler3d5966f2013-03-13 20:40:30 +00006713 if (!FD) return;
Rafael Espindolaa0851a22013-03-19 14:32:23 +00006714 llvm::Function *Fn = cast<llvm::Function>(GV);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006715
6716 if (FD->hasAttr<MipsLongCallAttr>())
6717 Fn->addFnAttr("long-call");
6718 else if (FD->hasAttr<MipsShortCallAttr>())
6719 Fn->addFnAttr("short-call");
6720
6721 // Other attributes do not have a meaning for declarations.
Rafael Espindoladeb10be2018-02-07 19:04:41 +00006722 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006723 return;
6724
Reed Kotler3d5966f2013-03-13 20:40:30 +00006725 if (FD->hasAttr<Mips16Attr>()) {
6726 Fn->addFnAttr("mips16");
6727 }
6728 else if (FD->hasAttr<NoMips16Attr>()) {
6729 Fn->addFnAttr("nomips16");
6730 }
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006731
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006732 if (FD->hasAttr<MicroMipsAttr>())
6733 Fn->addFnAttr("micromips");
6734 else if (FD->hasAttr<NoMicroMipsAttr>())
6735 Fn->addFnAttr("nomicromips");
6736
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006737 const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
6738 if (!Attr)
6739 return;
6740
6741 const char *Kind;
6742 switch (Attr->getInterrupt()) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006743 case MipsInterruptAttr::eic: Kind = "eic"; break;
6744 case MipsInterruptAttr::sw0: Kind = "sw0"; break;
6745 case MipsInterruptAttr::sw1: Kind = "sw1"; break;
6746 case MipsInterruptAttr::hw0: Kind = "hw0"; break;
6747 case MipsInterruptAttr::hw1: Kind = "hw1"; break;
6748 case MipsInterruptAttr::hw2: Kind = "hw2"; break;
6749 case MipsInterruptAttr::hw3: Kind = "hw3"; break;
6750 case MipsInterruptAttr::hw4: Kind = "hw4"; break;
6751 case MipsInterruptAttr::hw5: Kind = "hw5"; break;
6752 }
6753
6754 Fn->addFnAttr("interrupt", Kind);
6755
Reed Kotler373feca2013-01-16 17:10:28 +00006756 }
Reed Kotler3d5966f2013-03-13 20:40:30 +00006757
John McCall943fae92010-05-27 06:19:26 +00006758 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00006759 llvm::Value *Address) const override;
John McCall3480ef22011-08-30 01:42:09 +00006760
Craig Topper4f12f102014-03-12 06:41:41 +00006761 unsigned getSizeOfUnwindException() const override {
Akira Hatanaka0486db02011-09-20 18:23:28 +00006762 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00006763 }
John McCall943fae92010-05-27 06:19:26 +00006764};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006765}
John McCall943fae92010-05-27 06:19:26 +00006766
Eric Christopher7565e0d2015-05-29 23:09:49 +00006767void MipsABIInfo::CoerceToIntArgs(
6768 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006769 llvm::IntegerType *IntTy =
6770 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006771
6772 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
6773 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
6774 ArgList.push_back(IntTy);
6775
6776 // If necessary, add one more integer type to ArgList.
6777 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
6778
6779 if (R)
6780 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006781}
6782
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006783// In N32/64, an aligned double precision floating point field is passed in
6784// a register.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006785llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006786 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
6787
6788 if (IsO32) {
6789 CoerceToIntArgs(TySize, ArgList);
6790 return llvm::StructType::get(getVMContext(), ArgList);
6791 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006792
Akira Hatanaka02e13e52012-01-12 00:52:17 +00006793 if (Ty->isComplexType())
6794 return CGT.ConvertType(Ty);
Akira Hatanaka79f04612012-01-10 23:12:19 +00006795
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00006796 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006797
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006798 // Unions/vectors are passed in integer registers.
6799 if (!RT || !RT->isStructureOrClassType()) {
6800 CoerceToIntArgs(TySize, ArgList);
6801 return llvm::StructType::get(getVMContext(), ArgList);
6802 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006803
6804 const RecordDecl *RD = RT->getDecl();
6805 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006806 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Eric Christopher7565e0d2015-05-29 23:09:49 +00006807
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006808 uint64_t LastOffset = 0;
6809 unsigned idx = 0;
6810 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
6811
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00006812 // Iterate over fields in the struct/class and check if there are any aligned
6813 // double fields.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006814 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6815 i != e; ++i, ++idx) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006816 const QualType Ty = i->getType();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006817 const BuiltinType *BT = Ty->getAs<BuiltinType>();
6818
6819 if (!BT || BT->getKind() != BuiltinType::Double)
6820 continue;
6821
6822 uint64_t Offset = Layout.getFieldOffset(idx);
6823 if (Offset % 64) // Ignore doubles that are not aligned.
6824 continue;
6825
6826 // Add ((Offset - LastOffset) / 64) args of type i64.
6827 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
6828 ArgList.push_back(I64);
6829
6830 // Add double type.
6831 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
6832 LastOffset = Offset + 64;
6833 }
6834
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006835 CoerceToIntArgs(TySize - LastOffset, IntArgList);
6836 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006837
6838 return llvm::StructType::get(getVMContext(), ArgList);
6839}
6840
Akira Hatanakaddd66342013-10-29 18:41:15 +00006841llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
6842 uint64_t Offset) const {
6843 if (OrigOffset + MinABIStackAlignInBytes > Offset)
Craig Topper8a13c412014-05-21 05:09:00 +00006844 return nullptr;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006845
Akira Hatanakaddd66342013-10-29 18:41:15 +00006846 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
Akira Hatanaka1632af62012-01-09 19:31:25 +00006847}
Akira Hatanaka21ee88c2012-01-10 22:44:52 +00006848
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006849ABIArgInfo
6850MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Daniel Sanders998c9102015-01-14 12:00:12 +00006851 Ty = useFirstFieldIfTransparentUnion(Ty);
6852
Akira Hatanaka1632af62012-01-09 19:31:25 +00006853 uint64_t OrigOffset = Offset;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006854 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanaka1632af62012-01-09 19:31:25 +00006855 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006856
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006857 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
6858 (uint64_t)StackAlignInBytes);
Rui Ueyama83aa9792016-01-14 21:00:27 +00006859 unsigned CurrOffset = llvm::alignTo(Offset, Align);
6860 Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006861
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006862 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanakab579fe52011-06-02 00:09:17 +00006863 // Ignore empty aggregates.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006864 if (TySize == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00006865 return ABIArgInfo::getIgnore();
6866
Mark Lacey3825e832013-10-06 01:33:34 +00006867 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006868 Offset = OrigOffset + MinABIStackAlignInBytes;
John McCall7f416cc2015-09-08 08:05:57 +00006869 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006870 }
Akira Hatanakadf425db2011-08-01 18:09:58 +00006871
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006872 // If we have reached here, aggregates are passed directly by coercing to
6873 // another structure type. Padding is inserted if the offset of the
6874 // aggregate is unaligned.
Daniel Sandersaa1b3552014-10-24 15:30:16 +00006875 ABIArgInfo ArgInfo =
6876 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
6877 getPaddingType(OrigOffset, CurrOffset));
6878 ArgInfo.setInReg(true);
6879 return ArgInfo;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006880 }
6881
6882 // Treat an enum type as its underlying type.
6883 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6884 Ty = EnumTy->getDecl()->getIntegerType();
6885
Daniel Sanders5b445b32014-10-24 14:42:42 +00006886 // All integral types are promoted to the GPR width.
6887 if (Ty->isIntegralOrEnumerationType())
Alex Bradburye41a5e22018-01-12 20:08:16 +00006888 return extendType(Ty);
Akira Hatanaka1632af62012-01-09 19:31:25 +00006889
Akira Hatanakaddd66342013-10-29 18:41:15 +00006890 return ABIArgInfo::getDirect(
Craig Topper8a13c412014-05-21 05:09:00 +00006891 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00006892}
6893
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006894llvm::Type*
6895MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakab6f74432012-02-09 18:49:26 +00006896 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006897 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006898
Akira Hatanakab6f74432012-02-09 18:49:26 +00006899 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006900 const RecordDecl *RD = RT->getDecl();
Akira Hatanakab6f74432012-02-09 18:49:26 +00006901 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6902 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006903
Akira Hatanakab6f74432012-02-09 18:49:26 +00006904 // N32/64 returns struct/classes in floating point registers if the
6905 // following conditions are met:
6906 // 1. The size of the struct/class is no larger than 128-bit.
6907 // 2. The struct/class has one or two fields all of which are floating
6908 // point types.
Eric Christopher7565e0d2015-05-29 23:09:49 +00006909 // 3. The offset of the first field is zero (this follows what gcc does).
Akira Hatanakab6f74432012-02-09 18:49:26 +00006910 //
6911 // Any other composite results are returned in integer registers.
6912 //
6913 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
6914 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
6915 for (; b != e; ++b) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006916 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006917
Akira Hatanakab6f74432012-02-09 18:49:26 +00006918 if (!BT || !BT->isFloatingPoint())
6919 break;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006920
David Blaikie2d7c57e2012-04-30 02:36:29 +00006921 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakab6f74432012-02-09 18:49:26 +00006922 }
6923
6924 if (b == e)
6925 return llvm::StructType::get(getVMContext(), RTList,
6926 RD->hasAttr<PackedAttr>());
6927
6928 RTList.clear();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006929 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006930 }
6931
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006932 CoerceToIntArgs(Size, RTList);
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006933 return llvm::StructType::get(getVMContext(), RTList);
6934}
6935
Akira Hatanakab579fe52011-06-02 00:09:17 +00006936ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanaka60f5fe62012-01-23 23:18:57 +00006937 uint64_t Size = getContext().getTypeSize(RetTy);
6938
Daniel Sandersed39f582014-09-04 13:28:14 +00006939 if (RetTy->isVoidType())
6940 return ABIArgInfo::getIgnore();
6941
6942 // O32 doesn't treat zero-sized structs differently from other structs.
6943 // However, N32/N64 ignores zero sized return values.
6944 if (!IsO32 && Size == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00006945 return ABIArgInfo::getIgnore();
6946
Akira Hatanakac37eddf2012-05-11 21:01:17 +00006947 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006948 if (Size <= 128) {
6949 if (RetTy->isAnyComplexType())
6950 return ABIArgInfo::getDirect();
6951
Daniel Sanderse5018b62014-09-04 15:05:39 +00006952 // O32 returns integer vectors in registers and N32/N64 returns all small
Daniel Sanders00a56ff2014-09-04 15:07:43 +00006953 // aggregates in registers.
Daniel Sanderse5018b62014-09-04 15:05:39 +00006954 if (!IsO32 ||
6955 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
6956 ABIArgInfo ArgInfo =
6957 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
6958 ArgInfo.setInReg(true);
6959 return ArgInfo;
6960 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006961 }
Akira Hatanakab579fe52011-06-02 00:09:17 +00006962
John McCall7f416cc2015-09-08 08:05:57 +00006963 return getNaturalAlignIndirect(RetTy);
Akira Hatanakab579fe52011-06-02 00:09:17 +00006964 }
6965
6966 // Treat an enum type as its underlying type.
6967 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6968 RetTy = EnumTy->getDecl()->getIntegerType();
6969
Alex Bradburye41a5e22018-01-12 20:08:16 +00006970 return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
6971 : ABIArgInfo::getDirect());
Akira Hatanakab579fe52011-06-02 00:09:17 +00006972}
6973
6974void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanaka32604a92012-01-12 01:10:09 +00006975 ABIArgInfo &RetInfo = FI.getReturnInfo();
Reid Kleckner40ca9132014-05-13 22:05:45 +00006976 if (!getCXXABI().classifyReturnType(FI))
6977 RetInfo = classifyReturnType(FI.getReturnType());
Akira Hatanaka32604a92012-01-12 01:10:09 +00006978
Eric Christopher7565e0d2015-05-29 23:09:49 +00006979 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006980 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanaka32604a92012-01-12 01:10:09 +00006981
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006982 for (auto &I : FI.arguments())
6983 I.info = classifyArgumentType(I.type, Offset);
Akira Hatanakab579fe52011-06-02 00:09:17 +00006984}
6985
John McCall7f416cc2015-09-08 08:05:57 +00006986Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6987 QualType OrigTy) const {
6988 QualType Ty = OrigTy;
Daniel Sanders59229dc2014-11-19 10:01:35 +00006989
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006990 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
6991 // Pointers are also promoted in the same way but this only matters for N32.
Daniel Sanders59229dc2014-11-19 10:01:35 +00006992 unsigned SlotSizeInBits = IsO32 ? 32 : 64;
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006993 unsigned PtrWidth = getTarget().getPointerWidth(0);
John McCall7f416cc2015-09-08 08:05:57 +00006994 bool DidPromote = false;
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006995 if ((Ty->isIntegerType() &&
John McCall7f416cc2015-09-08 08:05:57 +00006996 getContext().getIntWidth(Ty) < SlotSizeInBits) ||
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006997 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
John McCall7f416cc2015-09-08 08:05:57 +00006998 DidPromote = true;
6999 Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
7000 Ty->isSignedIntegerType());
Daniel Sanders59229dc2014-11-19 10:01:35 +00007001 }
Eric Christopher7565e0d2015-05-29 23:09:49 +00007002
John McCall7f416cc2015-09-08 08:05:57 +00007003 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00007004
John McCall7f416cc2015-09-08 08:05:57 +00007005 // The alignment of things in the argument area is never larger than
7006 // StackAlignInBytes.
7007 TyInfo.second =
7008 std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
7009
7010 // MinABIStackAlignInBytes is the size of argument slots on the stack.
7011 CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
7012
7013 Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
7014 TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
7015
7016
7017 // If there was a promotion, "unpromote" into a temporary.
7018 // TODO: can we just use a pointer into a subset of the original slot?
7019 if (DidPromote) {
7020 Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
7021 llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
7022
7023 // Truncate down to the right width.
7024 llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
7025 : CGF.IntPtrTy);
7026 llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
7027 if (OrigTy->isPointerType())
7028 V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
7029
7030 CGF.Builder.CreateStore(V, Temp);
7031 Addr = Temp;
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00007032 }
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00007033
John McCall7f416cc2015-09-08 08:05:57 +00007034 return Addr;
Akira Hatanakab579fe52011-06-02 00:09:17 +00007035}
7036
Alex Bradburye41a5e22018-01-12 20:08:16 +00007037ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00007038 int TySize = getContext().getTypeSize(Ty);
Eric Christopher7565e0d2015-05-29 23:09:49 +00007039
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00007040 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
7041 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
Alex Bradburye41a5e22018-01-12 20:08:16 +00007042 return ABIArgInfo::getSignExtend(Ty);
Eric Christopher7565e0d2015-05-29 23:09:49 +00007043
Alex Bradburye41a5e22018-01-12 20:08:16 +00007044 return ABIArgInfo::getExtend(Ty);
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00007045}
7046
John McCall943fae92010-05-27 06:19:26 +00007047bool
7048MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7049 llvm::Value *Address) const {
7050 // This information comes from gcc's implementation, which seems to
7051 // as canonical as it gets.
7052
John McCall943fae92010-05-27 06:19:26 +00007053 // Everything on MIPS is 4 bytes. Double-precision FP registers
7054 // are aliased to pairs of single-precision FP registers.
Chris Lattnerece04092012-02-07 00:39:47 +00007055 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCall943fae92010-05-27 06:19:26 +00007056
7057 // 0-31 are the general purpose registers, $0 - $31.
7058 // 32-63 are the floating-point registers, $f0 - $f31.
7059 // 64 and 65 are the multiply/divide registers, $hi and $lo.
7060 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattnerece04092012-02-07 00:39:47 +00007061 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCall943fae92010-05-27 06:19:26 +00007062
7063 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
7064 // They are one bit wide and ignored here.
7065
7066 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
7067 // (coprocessor 1 is the FP unit)
7068 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
7069 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
7070 // 176-181 are the DSP accumulator registers.
Chris Lattnerece04092012-02-07 00:39:47 +00007071 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCall943fae92010-05-27 06:19:26 +00007072 return false;
7073}
7074
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007075//===----------------------------------------------------------------------===//
Dylan McKaye8232d72017-02-08 05:09:26 +00007076// AVR ABI Implementation.
7077//===----------------------------------------------------------------------===//
7078
7079namespace {
7080class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
7081public:
7082 AVRTargetCodeGenInfo(CodeGenTypes &CGT)
7083 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { }
7084
7085 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00007086 CodeGen::CodeGenModule &CGM) const override {
7087 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00007088 return;
Dylan McKaye8232d72017-02-08 05:09:26 +00007089 const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
7090 if (!FD) return;
7091 auto *Fn = cast<llvm::Function>(GV);
7092
7093 if (FD->getAttr<AVRInterruptAttr>())
7094 Fn->addFnAttr("interrupt");
7095
7096 if (FD->getAttr<AVRSignalAttr>())
7097 Fn->addFnAttr("signal");
7098 }
7099};
7100}
7101
7102//===----------------------------------------------------------------------===//
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007103// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
Eric Christopher7565e0d2015-05-29 23:09:49 +00007104// Currently subclassed only to implement custom OpenCL C function attribute
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007105// handling.
7106//===----------------------------------------------------------------------===//
7107
7108namespace {
7109
7110class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
7111public:
7112 TCETargetCodeGenInfo(CodeGenTypes &CGT)
7113 : DefaultTargetCodeGenInfo(CGT) {}
7114
Eric Christopher162c91c2015-06-05 22:03:00 +00007115 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00007116 CodeGen::CodeGenModule &M) const override;
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007117};
7118
Eric Christopher162c91c2015-06-05 22:03:00 +00007119void TCETargetCodeGenInfo::setTargetAttributes(
Rafael Espindoladeb10be2018-02-07 19:04:41 +00007120 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7121 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00007122 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00007123 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007124 if (!FD) return;
7125
7126 llvm::Function *F = cast<llvm::Function>(GV);
Eric Christopher7565e0d2015-05-29 23:09:49 +00007127
David Blaikiebbafb8a2012-03-11 07:00:24 +00007128 if (M.getLangOpts().OpenCL) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007129 if (FD->hasAttr<OpenCLKernelAttr>()) {
7130 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00007131 F->addFnAttr(llvm::Attribute::NoInline);
Aaron Ballman36a18ff2013-12-19 13:16:35 +00007132 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
7133 if (Attr) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007134 // Convert the reqd_work_group_size() attributes to metadata.
7135 llvm::LLVMContext &Context = F->getContext();
Eric Christopher7565e0d2015-05-29 23:09:49 +00007136 llvm::NamedMDNode *OpenCLMetadata =
7137 M.getModule().getOrInsertNamedMetadata(
7138 "opencl.kernel_wg_size_info");
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007139
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007140 SmallVector<llvm::Metadata *, 5> Operands;
7141 Operands.push_back(llvm::ConstantAsMetadata::get(F));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007142
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007143 Operands.push_back(
7144 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7145 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
7146 Operands.push_back(
7147 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7148 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
7149 Operands.push_back(
7150 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7151 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007152
Eric Christopher7565e0d2015-05-29 23:09:49 +00007153 // Add a boolean constant operand for "required" (true) or "hint"
7154 // (false) for implementing the work_group_size_hint attr later.
7155 // Currently always true as the hint is not yet implemented.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007156 Operands.push_back(
7157 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007158 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
7159 }
7160 }
7161 }
7162}
7163
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007164}
John McCall943fae92010-05-27 06:19:26 +00007165
Tony Linthicum76329bf2011-12-12 21:14:55 +00007166//===----------------------------------------------------------------------===//
7167// Hexagon ABI Implementation
7168//===----------------------------------------------------------------------===//
7169
7170namespace {
7171
7172class HexagonABIInfo : public ABIInfo {
7173
7174
7175public:
7176 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
7177
7178private:
7179
7180 ABIArgInfo classifyReturnType(QualType RetTy) const;
7181 ABIArgInfo classifyArgumentType(QualType RetTy) const;
7182
Craig Topper4f12f102014-03-12 06:41:41 +00007183 void computeInfo(CGFunctionInfo &FI) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00007184
John McCall7f416cc2015-09-08 08:05:57 +00007185 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7186 QualType Ty) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00007187};
7188
7189class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
7190public:
7191 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
7192 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
7193
Craig Topper4f12f102014-03-12 06:41:41 +00007194 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Tony Linthicum76329bf2011-12-12 21:14:55 +00007195 return 29;
7196 }
7197};
7198
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007199}
Tony Linthicum76329bf2011-12-12 21:14:55 +00007200
7201void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00007202 if (!getCXXABI().classifyReturnType(FI))
7203 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00007204 for (auto &I : FI.arguments())
7205 I.info = classifyArgumentType(I.type);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007206}
7207
7208ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
7209 if (!isAggregateTypeForABI(Ty)) {
7210 // Treat an enum type as its underlying type.
7211 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7212 Ty = EnumTy->getDecl()->getIntegerType();
7213
Alex Bradburye41a5e22018-01-12 20:08:16 +00007214 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
7215 : ABIArgInfo::getDirect());
Tony Linthicum76329bf2011-12-12 21:14:55 +00007216 }
7217
Krzysztof Parzyszek408b2722017-05-12 13:18:07 +00007218 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7219 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7220
Tony Linthicum76329bf2011-12-12 21:14:55 +00007221 // Ignore empty records.
7222 if (isEmptyRecord(getContext(), Ty, true))
7223 return ABIArgInfo::getIgnore();
7224
Tony Linthicum76329bf2011-12-12 21:14:55 +00007225 uint64_t Size = getContext().getTypeSize(Ty);
7226 if (Size > 64)
John McCall7f416cc2015-09-08 08:05:57 +00007227 return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007228 // Pass in the smallest viable integer type.
7229 else if (Size > 32)
7230 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7231 else if (Size > 16)
7232 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7233 else if (Size > 8)
7234 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7235 else
7236 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7237}
7238
7239ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
7240 if (RetTy->isVoidType())
7241 return ABIArgInfo::getIgnore();
7242
7243 // Large vector types should be returned via memory.
7244 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
John McCall7f416cc2015-09-08 08:05:57 +00007245 return getNaturalAlignIndirect(RetTy);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007246
7247 if (!isAggregateTypeForABI(RetTy)) {
7248 // Treat an enum type as its underlying type.
7249 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7250 RetTy = EnumTy->getDecl()->getIntegerType();
7251
Alex Bradburye41a5e22018-01-12 20:08:16 +00007252 return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
7253 : ABIArgInfo::getDirect());
Tony Linthicum76329bf2011-12-12 21:14:55 +00007254 }
7255
Tony Linthicum76329bf2011-12-12 21:14:55 +00007256 if (isEmptyRecord(getContext(), RetTy, true))
7257 return ABIArgInfo::getIgnore();
7258
7259 // Aggregates <= 8 bytes are returned in r0; other aggregates
7260 // are returned indirectly.
7261 uint64_t Size = getContext().getTypeSize(RetTy);
7262 if (Size <= 64) {
7263 // Return in the smallest viable integer type.
7264 if (Size <= 8)
7265 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7266 if (Size <= 16)
7267 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7268 if (Size <= 32)
7269 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7270 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7271 }
7272
John McCall7f416cc2015-09-08 08:05:57 +00007273 return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007274}
7275
John McCall7f416cc2015-09-08 08:05:57 +00007276Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7277 QualType Ty) const {
7278 // FIXME: Someone needs to audit that this handle alignment correctly.
7279 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
7280 getContext().getTypeInfoInChars(Ty),
7281 CharUnits::fromQuantity(4),
7282 /*AllowHigherAlign*/ true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007283}
7284
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007285//===----------------------------------------------------------------------===//
Jacques Pienaard964cc22016-03-28 21:02:54 +00007286// Lanai ABI Implementation
7287//===----------------------------------------------------------------------===//
7288
Benjamin Kramer5d28c7f2016-04-07 10:14:54 +00007289namespace {
Jacques Pienaard964cc22016-03-28 21:02:54 +00007290class LanaiABIInfo : public DefaultABIInfo {
7291public:
7292 LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7293
7294 bool shouldUseInReg(QualType Ty, CCState &State) const;
7295
7296 void computeInfo(CGFunctionInfo &FI) const override {
7297 CCState State(FI.getCallingConvention());
7298 // Lanai uses 4 registers to pass arguments unless the function has the
7299 // regparm attribute set.
7300 if (FI.getHasRegParm()) {
7301 State.FreeRegs = FI.getRegParm();
7302 } else {
7303 State.FreeRegs = 4;
7304 }
7305
7306 if (!getCXXABI().classifyReturnType(FI))
7307 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7308 for (auto &I : FI.arguments())
7309 I.info = classifyArgumentType(I.type, State);
7310 }
7311
Jacques Pienaare74d9132016-04-26 00:09:29 +00007312 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
Jacques Pienaard964cc22016-03-28 21:02:54 +00007313 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
7314};
Benjamin Kramer5d28c7f2016-04-07 10:14:54 +00007315} // end anonymous namespace
Jacques Pienaard964cc22016-03-28 21:02:54 +00007316
7317bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
7318 unsigned Size = getContext().getTypeSize(Ty);
7319 unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
7320
7321 if (SizeInRegs == 0)
7322 return false;
7323
7324 if (SizeInRegs > State.FreeRegs) {
7325 State.FreeRegs = 0;
7326 return false;
7327 }
7328
7329 State.FreeRegs -= SizeInRegs;
7330
7331 return true;
7332}
7333
Jacques Pienaare74d9132016-04-26 00:09:29 +00007334ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
7335 CCState &State) const {
7336 if (!ByVal) {
7337 if (State.FreeRegs) {
7338 --State.FreeRegs; // Non-byval indirects just use one pointer.
7339 return getNaturalAlignIndirectInReg(Ty);
7340 }
7341 return getNaturalAlignIndirect(Ty, false);
7342 }
7343
7344 // Compute the byval alignment.
Kostya Serebryany0da44422016-04-26 01:53:49 +00007345 const unsigned MinABIStackAlignInBytes = 4;
Jacques Pienaare74d9132016-04-26 00:09:29 +00007346 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
7347 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
7348 /*Realign=*/TypeAlign >
7349 MinABIStackAlignInBytes);
7350}
7351
Jacques Pienaard964cc22016-03-28 21:02:54 +00007352ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
7353 CCState &State) const {
Jacques Pienaare74d9132016-04-26 00:09:29 +00007354 // Check with the C++ ABI first.
7355 const RecordType *RT = Ty->getAs<RecordType>();
7356 if (RT) {
7357 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
7358 if (RAA == CGCXXABI::RAA_Indirect) {
7359 return getIndirectResult(Ty, /*ByVal=*/false, State);
7360 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
7361 return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
7362 }
7363 }
7364
7365 if (isAggregateTypeForABI(Ty)) {
7366 // Structures with flexible arrays are always indirect.
7367 if (RT && RT->getDecl()->hasFlexibleArrayMember())
7368 return getIndirectResult(Ty, /*ByVal=*/true, State);
7369
7370 // Ignore empty structs/unions.
7371 if (isEmptyRecord(getContext(), Ty, true))
7372 return ABIArgInfo::getIgnore();
7373
7374 llvm::LLVMContext &LLVMContext = getVMContext();
7375 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
7376 if (SizeInRegs <= State.FreeRegs) {
7377 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
7378 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
7379 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
7380 State.FreeRegs -= SizeInRegs;
7381 return ABIArgInfo::getDirectInReg(Result);
7382 } else {
7383 State.FreeRegs = 0;
7384 }
7385 return getIndirectResult(Ty, true, State);
7386 }
Jacques Pienaard964cc22016-03-28 21:02:54 +00007387
7388 // Treat an enum type as its underlying type.
7389 if (const auto *EnumTy = Ty->getAs<EnumType>())
7390 Ty = EnumTy->getDecl()->getIntegerType();
7391
Jacques Pienaare74d9132016-04-26 00:09:29 +00007392 bool InReg = shouldUseInReg(Ty, State);
7393 if (Ty->isPromotableIntegerType()) {
7394 if (InReg)
7395 return ABIArgInfo::getDirectInReg();
Alex Bradburye41a5e22018-01-12 20:08:16 +00007396 return ABIArgInfo::getExtend(Ty);
Jacques Pienaare74d9132016-04-26 00:09:29 +00007397 }
7398 if (InReg)
7399 return ABIArgInfo::getDirectInReg();
Jacques Pienaard964cc22016-03-28 21:02:54 +00007400 return ABIArgInfo::getDirect();
7401}
7402
7403namespace {
7404class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
7405public:
7406 LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
7407 : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {}
7408};
7409}
7410
7411//===----------------------------------------------------------------------===//
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007412// AMDGPU ABI Implementation
7413//===----------------------------------------------------------------------===//
7414
7415namespace {
7416
Matt Arsenault88d7da02016-08-22 19:25:59 +00007417class AMDGPUABIInfo final : public DefaultABIInfo {
Matt Arsenault88d7da02016-08-22 19:25:59 +00007418private:
Matt Arsenault3fe73952017-08-09 21:44:58 +00007419 static const unsigned MaxNumRegsForArgsRet = 16;
7420
Matt Arsenault3fe73952017-08-09 21:44:58 +00007421 unsigned numRegsForType(QualType Ty) const;
7422
7423 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
7424 bool isHomogeneousAggregateSmallEnough(const Type *Base,
7425 uint64_t Members) const override;
7426
7427public:
7428 explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
7429 DefaultABIInfo(CGT) {}
7430
7431 ABIArgInfo classifyReturnType(QualType RetTy) const;
7432 ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
7433 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
Matt Arsenault88d7da02016-08-22 19:25:59 +00007434
7435 void computeInfo(CGFunctionInfo &FI) const override;
7436};
7437
Matt Arsenault3fe73952017-08-09 21:44:58 +00007438bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
7439 return true;
7440}
7441
7442bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
7443 const Type *Base, uint64_t Members) const {
7444 uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
7445
7446 // Homogeneous Aggregates may occupy at most 16 registers.
7447 return Members * NumRegs <= MaxNumRegsForArgsRet;
7448}
7449
Matt Arsenault3fe73952017-08-09 21:44:58 +00007450/// Estimate number of registers the type will use when passed in registers.
7451unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
7452 unsigned NumRegs = 0;
7453
7454 if (const VectorType *VT = Ty->getAs<VectorType>()) {
7455 // Compute from the number of elements. The reported size is based on the
7456 // in-memory size, which includes the padding 4th element for 3-vectors.
7457 QualType EltTy = VT->getElementType();
7458 unsigned EltSize = getContext().getTypeSize(EltTy);
7459
7460 // 16-bit element vectors should be passed as packed.
7461 if (EltSize == 16)
7462 return (VT->getNumElements() + 1) / 2;
7463
7464 unsigned EltNumRegs = (EltSize + 31) / 32;
7465 return EltNumRegs * VT->getNumElements();
7466 }
7467
7468 if (const RecordType *RT = Ty->getAs<RecordType>()) {
7469 const RecordDecl *RD = RT->getDecl();
7470 assert(!RD->hasFlexibleArrayMember());
7471
7472 for (const FieldDecl *Field : RD->fields()) {
7473 QualType FieldTy = Field->getType();
7474 NumRegs += numRegsForType(FieldTy);
7475 }
7476
7477 return NumRegs;
7478 }
7479
7480 return (getContext().getTypeSize(Ty) + 31) / 32;
7481}
7482
Matt Arsenault88d7da02016-08-22 19:25:59 +00007483void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
Matt Arsenault3fe73952017-08-09 21:44:58 +00007484 llvm::CallingConv::ID CC = FI.getCallingConvention();
7485
Matt Arsenault88d7da02016-08-22 19:25:59 +00007486 if (!getCXXABI().classifyReturnType(FI))
7487 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7488
Matt Arsenault3fe73952017-08-09 21:44:58 +00007489 unsigned NumRegsLeft = MaxNumRegsForArgsRet;
7490 for (auto &Arg : FI.arguments()) {
7491 if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
7492 Arg.info = classifyKernelArgumentType(Arg.type);
7493 } else {
7494 Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
7495 }
7496 }
Matt Arsenault88d7da02016-08-22 19:25:59 +00007497}
7498
Matt Arsenault3fe73952017-08-09 21:44:58 +00007499ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
7500 if (isAggregateTypeForABI(RetTy)) {
7501 // Records with non-trivial destructors/copy-constructors should not be
7502 // returned by value.
7503 if (!getRecordArgABI(RetTy, getCXXABI())) {
7504 // Ignore empty structs/unions.
7505 if (isEmptyRecord(getContext(), RetTy, true))
7506 return ABIArgInfo::getIgnore();
7507
7508 // Lower single-element structs to just return a regular value.
7509 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
7510 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
7511
7512 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
7513 const RecordDecl *RD = RT->getDecl();
7514 if (RD->hasFlexibleArrayMember())
7515 return DefaultABIInfo::classifyReturnType(RetTy);
7516 }
7517
7518 // Pack aggregates <= 4 bytes into single VGPR or pair.
7519 uint64_t Size = getContext().getTypeSize(RetTy);
7520 if (Size <= 16)
7521 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7522
7523 if (Size <= 32)
7524 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7525
7526 if (Size <= 64) {
7527 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
7528 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
7529 }
7530
7531 if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
7532 return ABIArgInfo::getDirect();
7533 }
Matt Arsenault88d7da02016-08-22 19:25:59 +00007534 }
7535
Matt Arsenault3fe73952017-08-09 21:44:58 +00007536 // Otherwise just do the default thing.
7537 return DefaultABIInfo::classifyReturnType(RetTy);
7538}
7539
7540/// For kernels all parameters are really passed in a special buffer. It doesn't
7541/// make sense to pass anything byval, so everything must be direct.
7542ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
7543 Ty = useFirstFieldIfTransparentUnion(Ty);
7544
7545 // TODO: Can we omit empty structs?
7546
Matt Arsenault88d7da02016-08-22 19:25:59 +00007547 // Coerce single element structs to its element.
Matt Arsenault3fe73952017-08-09 21:44:58 +00007548 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
7549 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
Matt Arsenault88d7da02016-08-22 19:25:59 +00007550
7551 // If we set CanBeFlattened to true, CodeGen will expand the struct to its
7552 // individual elements, which confuses the Clover OpenCL backend; therefore we
7553 // have to set it to false here. Other args of getDirect() are just defaults.
7554 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
7555}
7556
Matt Arsenault3fe73952017-08-09 21:44:58 +00007557ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
7558 unsigned &NumRegsLeft) const {
7559 assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
7560
7561 Ty = useFirstFieldIfTransparentUnion(Ty);
7562
7563 if (isAggregateTypeForABI(Ty)) {
7564 // Records with non-trivial destructors/copy-constructors should not be
7565 // passed by value.
7566 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
7567 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7568
7569 // Ignore empty structs/unions.
7570 if (isEmptyRecord(getContext(), Ty, true))
7571 return ABIArgInfo::getIgnore();
7572
7573 // Lower single-element structs to just pass a regular value. TODO: We
7574 // could do reasonable-size multiple-element structs too, using getExpand(),
7575 // though watch out for things like bitfields.
7576 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
7577 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
7578
7579 if (const RecordType *RT = Ty->getAs<RecordType>()) {
7580 const RecordDecl *RD = RT->getDecl();
7581 if (RD->hasFlexibleArrayMember())
7582 return DefaultABIInfo::classifyArgumentType(Ty);
7583 }
7584
7585 // Pack aggregates <= 8 bytes into single VGPR or pair.
7586 uint64_t Size = getContext().getTypeSize(Ty);
7587 if (Size <= 64) {
7588 unsigned NumRegs = (Size + 31) / 32;
7589 NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
7590
7591 if (Size <= 16)
7592 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7593
7594 if (Size <= 32)
7595 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7596
7597 // XXX: Should this be i64 instead, and should the limit increase?
7598 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
7599 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
7600 }
7601
7602 if (NumRegsLeft > 0) {
7603 unsigned NumRegs = numRegsForType(Ty);
7604 if (NumRegsLeft >= NumRegs) {
7605 NumRegsLeft -= NumRegs;
7606 return ABIArgInfo::getDirect();
7607 }
7608 }
7609 }
7610
7611 // Otherwise just do the default thing.
7612 ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
7613 if (!ArgInfo.isIndirect()) {
7614 unsigned NumRegs = numRegsForType(Ty);
7615 NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
7616 }
7617
7618 return ArgInfo;
7619}
7620
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007621class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
7622public:
7623 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
Matt Arsenault88d7da02016-08-22 19:25:59 +00007624 : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {}
Eric Christopher162c91c2015-06-05 22:03:00 +00007625 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +00007626 CodeGen::CodeGenModule &M) const override;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00007627 unsigned getOpenCLKernelCallingConv() const override;
Nico Weber7849eeb2016-12-14 21:38:18 +00007628
Yaxun Liu402804b2016-12-15 08:09:08 +00007629 llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
7630 llvm::PointerType *T, QualType QT) const override;
Yaxun Liu6d96f1632017-05-18 18:51:09 +00007631
Alexander Richardson6d989432017-10-15 18:48:14 +00007632 LangAS getASTAllocaAddressSpace() const override {
7633 return getLangASFromTargetAS(
7634 getABIInfo().getDataLayout().getAllocaAddrSpace());
Yaxun Liu6d96f1632017-05-18 18:51:09 +00007635 }
Alexander Richardson6d989432017-10-15 18:48:14 +00007636 LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
7637 const VarDecl *D) const override;
Yaxun Liu39195062017-08-04 18:16:31 +00007638 llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S,
7639 llvm::LLVMContext &C) const override;
Yaxun Liuc2a87a02017-10-14 12:23:50 +00007640 llvm::Function *
7641 createEnqueuedBlockKernel(CodeGenFunction &CGF,
7642 llvm::Function *BlockInvokeFunc,
7643 llvm::Value *BlockLiteral) const override;
Yaxun Liu402804b2016-12-15 08:09:08 +00007644};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007645}
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007646
Eric Christopher162c91c2015-06-05 22:03:00 +00007647void AMDGPUTargetCodeGenInfo::setTargetAttributes(
Rafael Espindoladeb10be2018-02-07 19:04:41 +00007648 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7649 if (GV->isDeclaration())
Simon Atanasyan1a116db2017-07-20 20:34:18 +00007650 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00007651 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007652 if (!FD)
7653 return;
7654
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007655 llvm::Function *F = cast<llvm::Function>(GV);
7656
Stanislav Mekhanoshin921a4232017-04-06 18:15:44 +00007657 const auto *ReqdWGS = M.getLangOpts().OpenCL ?
7658 FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
7659 const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
7660 if (ReqdWGS || FlatWGS) {
7661 unsigned Min = FlatWGS ? FlatWGS->getMin() : 0;
7662 unsigned Max = FlatWGS ? FlatWGS->getMax() : 0;
7663 if (ReqdWGS && Min == 0 && Max == 0)
7664 Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007665
7666 if (Min != 0) {
7667 assert(Min <= Max && "Min must be less than or equal Max");
7668
7669 std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
7670 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
7671 } else
7672 assert(Max == 0 && "Max must be zero");
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007673 }
7674
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007675 if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
7676 unsigned Min = Attr->getMin();
7677 unsigned Max = Attr->getMax();
7678
7679 if (Min != 0) {
7680 assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
7681
7682 std::string AttrVal = llvm::utostr(Min);
7683 if (Max != 0)
7684 AttrVal = AttrVal + "," + llvm::utostr(Max);
7685 F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
7686 } else
7687 assert(Max == 0 && "Max must be zero");
7688 }
7689
7690 if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007691 unsigned NumSGPR = Attr->getNumSGPR();
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007692
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007693 if (NumSGPR != 0)
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007694 F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
7695 }
7696
7697 if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
7698 uint32_t NumVGPR = Attr->getNumVGPR();
7699
7700 if (NumVGPR != 0)
7701 F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007702 }
Yaxun Liuf2e8ab22016-07-19 19:39:45 +00007703}
Tony Linthicum76329bf2011-12-12 21:14:55 +00007704
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00007705unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
7706 return llvm::CallingConv::AMDGPU_KERNEL;
7707}
7708
Yaxun Liu402804b2016-12-15 08:09:08 +00007709// Currently LLVM assumes null pointers always have value 0,
7710// which results in incorrectly transformed IR. Therefore, instead of
7711// emitting null pointers in private and local address spaces, a null
7712// pointer in generic address space is emitted which is casted to a
7713// pointer in local or private address space.
7714llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
7715 const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
7716 QualType QT) const {
7717 if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
7718 return llvm::ConstantPointerNull::get(PT);
7719
7720 auto &Ctx = CGM.getContext();
7721 auto NPT = llvm::PointerType::get(PT->getElementType(),
7722 Ctx.getTargetAddressSpace(LangAS::opencl_generic));
7723 return llvm::ConstantExpr::getAddrSpaceCast(
7724 llvm::ConstantPointerNull::get(NPT), PT);
7725}
7726
Alexander Richardson6d989432017-10-15 18:48:14 +00007727LangAS
Yaxun Liucbf647c2017-07-08 13:24:52 +00007728AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
7729 const VarDecl *D) const {
7730 assert(!CGM.getLangOpts().OpenCL &&
7731 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
7732 "Address space agnostic languages only");
Alexander Richardson6d989432017-10-15 18:48:14 +00007733 LangAS DefaultGlobalAS = getLangASFromTargetAS(
7734 CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
Yaxun Liucbf647c2017-07-08 13:24:52 +00007735 if (!D)
7736 return DefaultGlobalAS;
7737
Alexander Richardson6d989432017-10-15 18:48:14 +00007738 LangAS AddrSpace = D->getType().getAddressSpace();
7739 assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
Yaxun Liucbf647c2017-07-08 13:24:52 +00007740 if (AddrSpace != LangAS::Default)
7741 return AddrSpace;
7742
7743 if (CGM.isTypeConstant(D->getType(), false)) {
7744 if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
7745 return ConstAS.getValue();
7746 }
7747 return DefaultGlobalAS;
7748}
7749
Yaxun Liu39195062017-08-04 18:16:31 +00007750llvm::SyncScope::ID
7751AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S,
7752 llvm::LLVMContext &C) const {
7753 StringRef Name;
7754 switch (S) {
7755 case SyncScope::OpenCLWorkGroup:
7756 Name = "workgroup";
7757 break;
7758 case SyncScope::OpenCLDevice:
7759 Name = "agent";
7760 break;
7761 case SyncScope::OpenCLAllSVMDevices:
7762 Name = "";
7763 break;
7764 case SyncScope::OpenCLSubGroup:
7765 Name = "subgroup";
7766 }
7767 return C.getOrInsertSyncScopeID(Name);
7768}
7769
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007770//===----------------------------------------------------------------------===//
Chris Dewhurst7e7ee962016-06-08 14:47:25 +00007771// SPARC v8 ABI Implementation.
7772// Based on the SPARC Compliance Definition version 2.4.1.
7773//
7774// Ensures that complex values are passed in registers.
7775//
7776namespace {
7777class SparcV8ABIInfo : public DefaultABIInfo {
7778public:
7779 SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7780
7781private:
7782 ABIArgInfo classifyReturnType(QualType RetTy) const;
7783 void computeInfo(CGFunctionInfo &FI) const override;
7784};
7785} // end anonymous namespace
7786
7787
7788ABIArgInfo
7789SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
7790 if (Ty->isAnyComplexType()) {
7791 return ABIArgInfo::getDirect();
7792 }
7793 else {
7794 return DefaultABIInfo::classifyReturnType(Ty);
7795 }
7796}
7797
7798void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
7799
7800 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7801 for (auto &Arg : FI.arguments())
7802 Arg.info = classifyArgumentType(Arg.type);
7803}
7804
7805namespace {
7806class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
7807public:
7808 SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
7809 : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {}
7810};
7811} // end anonymous namespace
7812
7813//===----------------------------------------------------------------------===//
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007814// SPARC v9 ABI Implementation.
7815// Based on the SPARC Compliance Definition version 2.4.1.
7816//
7817// Function arguments a mapped to a nominal "parameter array" and promoted to
7818// registers depending on their type. Each argument occupies 8 or 16 bytes in
7819// the array, structs larger than 16 bytes are passed indirectly.
7820//
7821// One case requires special care:
7822//
7823// struct mixed {
7824// int i;
7825// float f;
7826// };
7827//
7828// When a struct mixed is passed by value, it only occupies 8 bytes in the
7829// parameter array, but the int is passed in an integer register, and the float
7830// is passed in a floating point register. This is represented as two arguments
7831// with the LLVM IR inreg attribute:
7832//
7833// declare void f(i32 inreg %i, float inreg %f)
7834//
7835// The code generator will only allocate 4 bytes from the parameter array for
7836// the inreg arguments. All other arguments are allocated a multiple of 8
7837// bytes.
7838//
7839namespace {
7840class SparcV9ABIInfo : public ABIInfo {
7841public:
7842 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
7843
7844private:
7845 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
Craig Topper4f12f102014-03-12 06:41:41 +00007846 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00007847 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7848 QualType Ty) const override;
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007849
7850 // Coercion type builder for structs passed in registers. The coercion type
7851 // serves two purposes:
7852 //
7853 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
7854 // in registers.
7855 // 2. Expose aligned floating point elements as first-level elements, so the
7856 // code generator knows to pass them in floating point registers.
7857 //
7858 // We also compute the InReg flag which indicates that the struct contains
7859 // aligned 32-bit floats.
7860 //
7861 struct CoerceBuilder {
7862 llvm::LLVMContext &Context;
7863 const llvm::DataLayout &DL;
7864 SmallVector<llvm::Type*, 8> Elems;
7865 uint64_t Size;
7866 bool InReg;
7867
7868 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
7869 : Context(c), DL(dl), Size(0), InReg(false) {}
7870
7871 // Pad Elems with integers until Size is ToSize.
7872 void pad(uint64_t ToSize) {
7873 assert(ToSize >= Size && "Cannot remove elements");
7874 if (ToSize == Size)
7875 return;
7876
7877 // Finish the current 64-bit word.
Rui Ueyama83aa9792016-01-14 21:00:27 +00007878 uint64_t Aligned = llvm::alignTo(Size, 64);
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007879 if (Aligned > Size && Aligned <= ToSize) {
7880 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
7881 Size = Aligned;
7882 }
7883
7884 // Add whole 64-bit words.
7885 while (Size + 64 <= ToSize) {
7886 Elems.push_back(llvm::Type::getInt64Ty(Context));
7887 Size += 64;
7888 }
7889
7890 // Final in-word padding.
7891 if (Size < ToSize) {
7892 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
7893 Size = ToSize;
7894 }
7895 }
7896
7897 // Add a floating point element at Offset.
7898 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
7899 // Unaligned floats are treated as integers.
7900 if (Offset % Bits)
7901 return;
7902 // The InReg flag is only required if there are any floats < 64 bits.
7903 if (Bits < 64)
7904 InReg = true;
7905 pad(Offset);
7906 Elems.push_back(Ty);
7907 Size = Offset + Bits;
7908 }
7909
7910 // Add a struct type to the coercion type, starting at Offset (in bits).
7911 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
7912 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
7913 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
7914 llvm::Type *ElemTy = StrTy->getElementType(i);
7915 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
7916 switch (ElemTy->getTypeID()) {
7917 case llvm::Type::StructTyID:
7918 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
7919 break;
7920 case llvm::Type::FloatTyID:
7921 addFloat(ElemOffset, ElemTy, 32);
7922 break;
7923 case llvm::Type::DoubleTyID:
7924 addFloat(ElemOffset, ElemTy, 64);
7925 break;
7926 case llvm::Type::FP128TyID:
7927 addFloat(ElemOffset, ElemTy, 128);
7928 break;
7929 case llvm::Type::PointerTyID:
7930 if (ElemOffset % 64 == 0) {
7931 pad(ElemOffset);
7932 Elems.push_back(ElemTy);
7933 Size += 64;
7934 }
7935 break;
7936 default:
7937 break;
7938 }
7939 }
7940 }
7941
7942 // Check if Ty is a usable substitute for the coercion type.
7943 bool isUsableType(llvm::StructType *Ty) const {
Benjamin Kramer39ccabe2015-03-02 11:57:06 +00007944 return llvm::makeArrayRef(Elems) == Ty->elements();
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007945 }
7946
7947 // Get the coercion type as a literal struct type.
7948 llvm::Type *getType() const {
7949 if (Elems.size() == 1)
7950 return Elems.front();
7951 else
7952 return llvm::StructType::get(Context, Elems);
7953 }
7954 };
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007955};
7956} // end anonymous namespace
7957
7958ABIArgInfo
7959SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
7960 if (Ty->isVoidType())
7961 return ABIArgInfo::getIgnore();
7962
7963 uint64_t Size = getContext().getTypeSize(Ty);
7964
7965 // Anything too big to fit in registers is passed with an explicit indirect
7966 // pointer / sret pointer.
7967 if (Size > SizeLimit)
John McCall7f416cc2015-09-08 08:05:57 +00007968 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007969
7970 // Treat an enum type as its underlying type.
7971 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7972 Ty = EnumTy->getDecl()->getIntegerType();
7973
7974 // Integer types smaller than a register are extended.
7975 if (Size < 64 && Ty->isIntegerType())
Alex Bradburye41a5e22018-01-12 20:08:16 +00007976 return ABIArgInfo::getExtend(Ty);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007977
7978 // Other non-aggregates go in registers.
7979 if (!isAggregateTypeForABI(Ty))
7980 return ABIArgInfo::getDirect();
7981
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00007982 // If a C++ object has either a non-trivial copy constructor or a non-trivial
7983 // destructor, it is passed with an explicit indirect pointer / sret pointer.
7984 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00007985 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00007986
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007987 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007988 // Build a coercion type from the LLVM struct type.
7989 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
7990 if (!StrTy)
7991 return ABIArgInfo::getDirect();
7992
7993 CoerceBuilder CB(getVMContext(), getDataLayout());
7994 CB.addStruct(0, StrTy);
Rui Ueyama83aa9792016-01-14 21:00:27 +00007995 CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007996
7997 // Try to use the original type for coercion.
7998 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
7999
8000 if (CB.InReg)
8001 return ABIArgInfo::getDirectInReg(CoerceTy);
8002 else
8003 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008004}
8005
John McCall7f416cc2015-09-08 08:05:57 +00008006Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8007 QualType Ty) const {
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008008 ABIArgInfo AI = classifyType(Ty, 16 * 8);
8009 llvm::Type *ArgTy = CGT.ConvertType(Ty);
8010 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
8011 AI.setCoerceToType(ArgTy);
8012
John McCall7f416cc2015-09-08 08:05:57 +00008013 CharUnits SlotSize = CharUnits::fromQuantity(8);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008014
John McCall7f416cc2015-09-08 08:05:57 +00008015 CGBuilderTy &Builder = CGF.Builder;
8016 Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
8017 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
8018
8019 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
8020
8021 Address ArgAddr = Address::invalid();
8022 CharUnits Stride;
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008023 switch (AI.getKind()) {
8024 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00008025 case ABIArgInfo::CoerceAndExpand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00008026 case ABIArgInfo::InAlloca:
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008027 llvm_unreachable("Unsupported ABI kind for va_arg");
8028
John McCall7f416cc2015-09-08 08:05:57 +00008029 case ABIArgInfo::Extend: {
8030 Stride = SlotSize;
8031 CharUnits Offset = SlotSize - TypeInfo.first;
8032 ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008033 break;
John McCall7f416cc2015-09-08 08:05:57 +00008034 }
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008035
John McCall7f416cc2015-09-08 08:05:57 +00008036 case ABIArgInfo::Direct: {
8037 auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
Rui Ueyama83aa9792016-01-14 21:00:27 +00008038 Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008039 ArgAddr = Addr;
8040 break;
John McCall7f416cc2015-09-08 08:05:57 +00008041 }
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008042
8043 case ABIArgInfo::Indirect:
John McCall7f416cc2015-09-08 08:05:57 +00008044 Stride = SlotSize;
8045 ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
8046 ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
8047 TypeInfo.second);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008048 break;
8049
8050 case ABIArgInfo::Ignore:
John McCall7f416cc2015-09-08 08:05:57 +00008051 return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008052 }
8053
8054 // Update VAList.
John McCall7f416cc2015-09-08 08:05:57 +00008055 llvm::Value *NextPtr =
8056 Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next");
8057 Builder.CreateStore(NextPtr, VAListAddr);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008058
John McCall7f416cc2015-09-08 08:05:57 +00008059 return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008060}
8061
8062void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
8063 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00008064 for (auto &I : FI.arguments())
8065 I.info = classifyType(I.type, 16 * 8);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008066}
8067
8068namespace {
8069class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
8070public:
8071 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
8072 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
Roman Divackyf02c9942014-02-24 18:46:27 +00008073
Craig Topper4f12f102014-03-12 06:41:41 +00008074 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyf02c9942014-02-24 18:46:27 +00008075 return 14;
8076 }
8077
8078 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00008079 llvm::Value *Address) const override;
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008080};
8081} // end anonymous namespace
8082
Roman Divackyf02c9942014-02-24 18:46:27 +00008083bool
8084SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8085 llvm::Value *Address) const {
8086 // This is calculated from the LLVM and GCC tables and verified
8087 // against gcc output. AFAIK all ABIs use the same encoding.
8088
8089 CodeGen::CGBuilderTy &Builder = CGF.Builder;
8090
8091 llvm::IntegerType *i8 = CGF.Int8Ty;
8092 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
8093 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
8094
8095 // 0-31: the 8-byte general-purpose registers
8096 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
8097
8098 // 32-63: f0-31, the 4-byte floating-point registers
8099 AssignToArrayRange(Builder, Address, Four8, 32, 63);
8100
8101 // Y = 64
8102 // PSR = 65
8103 // WIM = 66
8104 // TBR = 67
8105 // PC = 68
8106 // NPC = 69
8107 // FSR = 70
8108 // CSR = 71
8109 AssignToArrayRange(Builder, Address, Eight8, 64, 71);
Eric Christopher7565e0d2015-05-29 23:09:49 +00008110
Roman Divackyf02c9942014-02-24 18:46:27 +00008111 // 72-87: d0-15, the 8-byte floating-point registers
8112 AssignToArrayRange(Builder, Address, Eight8, 72, 87);
8113
8114 return false;
8115}
8116
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008117
Robert Lytton0e076492013-08-13 09:43:10 +00008118//===----------------------------------------------------------------------===//
Robert Lyttond21e2d72014-03-03 13:45:29 +00008119// XCore ABI Implementation
Robert Lytton0e076492013-08-13 09:43:10 +00008120//===----------------------------------------------------------------------===//
Robert Lytton844aeeb2014-05-02 09:33:20 +00008121
Robert Lytton0e076492013-08-13 09:43:10 +00008122namespace {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008123
8124/// A SmallStringEnc instance is used to build up the TypeString by passing
8125/// it by reference between functions that append to it.
8126typedef llvm::SmallString<128> SmallStringEnc;
8127
8128/// TypeStringCache caches the meta encodings of Types.
8129///
8130/// The reason for caching TypeStrings is two fold:
8131/// 1. To cache a type's encoding for later uses;
8132/// 2. As a means to break recursive member type inclusion.
8133///
8134/// A cache Entry can have a Status of:
8135/// NonRecursive: The type encoding is not recursive;
8136/// Recursive: The type encoding is recursive;
8137/// Incomplete: An incomplete TypeString;
8138/// IncompleteUsed: An incomplete TypeString that has been used in a
8139/// Recursive type encoding.
8140///
8141/// A NonRecursive entry will have all of its sub-members expanded as fully
8142/// as possible. Whilst it may contain types which are recursive, the type
8143/// itself is not recursive and thus its encoding may be safely used whenever
8144/// the type is encountered.
8145///
8146/// A Recursive entry will have all of its sub-members expanded as fully as
8147/// possible. The type itself is recursive and it may contain other types which
8148/// are recursive. The Recursive encoding must not be used during the expansion
8149/// of a recursive type's recursive branch. For simplicity the code uses
8150/// IncompleteCount to reject all usage of Recursive encodings for member types.
8151///
8152/// An Incomplete entry is always a RecordType and only encodes its
8153/// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
8154/// are placed into the cache during type expansion as a means to identify and
8155/// handle recursive inclusion of types as sub-members. If there is recursion
8156/// the entry becomes IncompleteUsed.
8157///
8158/// During the expansion of a RecordType's members:
8159///
8160/// If the cache contains a NonRecursive encoding for the member type, the
8161/// cached encoding is used;
8162///
8163/// If the cache contains a Recursive encoding for the member type, the
8164/// cached encoding is 'Swapped' out, as it may be incorrect, and...
8165///
8166/// If the member is a RecordType, an Incomplete encoding is placed into the
8167/// cache to break potential recursive inclusion of itself as a sub-member;
8168///
8169/// Once a member RecordType has been expanded, its temporary incomplete
8170/// entry is removed from the cache. If a Recursive encoding was swapped out
8171/// it is swapped back in;
8172///
8173/// If an incomplete entry is used to expand a sub-member, the incomplete
8174/// entry is marked as IncompleteUsed. The cache keeps count of how many
8175/// IncompleteUsed entries it currently contains in IncompleteUsedCount;
8176///
8177/// If a member's encoding is found to be a NonRecursive or Recursive viz:
8178/// IncompleteUsedCount==0, the member's encoding is added to the cache.
8179/// Else the member is part of a recursive type and thus the recursion has
8180/// been exited too soon for the encoding to be correct for the member.
8181///
8182class TypeStringCache {
8183 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
8184 struct Entry {
8185 std::string Str; // The encoded TypeString for the type.
8186 enum Status State; // Information about the encoding in 'Str'.
8187 std::string Swapped; // A temporary place holder for a Recursive encoding
8188 // during the expansion of RecordType's members.
8189 };
8190 std::map<const IdentifierInfo *, struct Entry> Map;
8191 unsigned IncompleteCount; // Number of Incomplete entries in the Map.
8192 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
8193public:
Hans Wennborg4afe5042015-07-22 20:46:26 +00008194 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
Robert Lytton844aeeb2014-05-02 09:33:20 +00008195 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
8196 bool removeIncomplete(const IdentifierInfo *ID);
8197 void addIfComplete(const IdentifierInfo *ID, StringRef Str,
8198 bool IsRecursive);
8199 StringRef lookupStr(const IdentifierInfo *ID);
8200};
8201
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008202/// TypeString encodings for enum & union fields must be order.
Robert Lytton844aeeb2014-05-02 09:33:20 +00008203/// FieldEncoding is a helper for this ordering process.
8204class FieldEncoding {
8205 bool HasName;
8206 std::string Enc;
8207public:
Hans Wennborg4afe5042015-07-22 20:46:26 +00008208 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
Malcolm Parsonsf76f6502016-11-02 10:39:27 +00008209 StringRef str() { return Enc; }
Robert Lytton844aeeb2014-05-02 09:33:20 +00008210 bool operator<(const FieldEncoding &rhs) const {
8211 if (HasName != rhs.HasName) return HasName;
8212 return Enc < rhs.Enc;
8213 }
8214};
8215
Robert Lytton7d1db152013-08-19 09:46:39 +00008216class XCoreABIInfo : public DefaultABIInfo {
8217public:
8218 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
John McCall7f416cc2015-09-08 08:05:57 +00008219 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8220 QualType Ty) const override;
Robert Lytton7d1db152013-08-19 09:46:39 +00008221};
8222
Robert Lyttond21e2d72014-03-03 13:45:29 +00008223class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008224 mutable TypeStringCache TSC;
Robert Lytton0e076492013-08-13 09:43:10 +00008225public:
Robert Lyttond21e2d72014-03-03 13:45:29 +00008226 XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
Robert Lytton7d1db152013-08-19 09:46:39 +00008227 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
Rafael Espindola8dcd6e72014-05-08 15:01:48 +00008228 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
8229 CodeGen::CodeGenModule &M) const override;
Robert Lytton0e076492013-08-13 09:43:10 +00008230};
Robert Lytton844aeeb2014-05-02 09:33:20 +00008231
Robert Lytton2d196952013-10-11 10:29:34 +00008232} // End anonymous namespace.
Robert Lytton0e076492013-08-13 09:43:10 +00008233
James Y Knight29b5f082016-02-24 02:59:33 +00008234// TODO: this implementation is likely now redundant with the default
8235// EmitVAArg.
John McCall7f416cc2015-09-08 08:05:57 +00008236Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8237 QualType Ty) const {
Robert Lytton7d1db152013-08-19 09:46:39 +00008238 CGBuilderTy &Builder = CGF.Builder;
Robert Lytton7d1db152013-08-19 09:46:39 +00008239
Robert Lytton2d196952013-10-11 10:29:34 +00008240 // Get the VAList.
John McCall7f416cc2015-09-08 08:05:57 +00008241 CharUnits SlotSize = CharUnits::fromQuantity(4);
8242 Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
Robert Lytton7d1db152013-08-19 09:46:39 +00008243
Robert Lytton2d196952013-10-11 10:29:34 +00008244 // Handle the argument.
8245 ABIArgInfo AI = classifyArgumentType(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00008246 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
Robert Lytton2d196952013-10-11 10:29:34 +00008247 llvm::Type *ArgTy = CGT.ConvertType(Ty);
8248 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
8249 AI.setCoerceToType(ArgTy);
Robert Lytton7d1db152013-08-19 09:46:39 +00008250 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
John McCall7f416cc2015-09-08 08:05:57 +00008251
8252 Address Val = Address::invalid();
8253 CharUnits ArgSize = CharUnits::Zero();
Robert Lytton7d1db152013-08-19 09:46:39 +00008254 switch (AI.getKind()) {
Robert Lytton7d1db152013-08-19 09:46:39 +00008255 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00008256 case ABIArgInfo::CoerceAndExpand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00008257 case ABIArgInfo::InAlloca:
Robert Lytton7d1db152013-08-19 09:46:39 +00008258 llvm_unreachable("Unsupported ABI kind for va_arg");
8259 case ABIArgInfo::Ignore:
John McCall7f416cc2015-09-08 08:05:57 +00008260 Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
8261 ArgSize = CharUnits::Zero();
Robert Lytton2d196952013-10-11 10:29:34 +00008262 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00008263 case ABIArgInfo::Extend:
8264 case ABIArgInfo::Direct:
John McCall7f416cc2015-09-08 08:05:57 +00008265 Val = Builder.CreateBitCast(AP, ArgPtrTy);
8266 ArgSize = CharUnits::fromQuantity(
8267 getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
Rui Ueyama83aa9792016-01-14 21:00:27 +00008268 ArgSize = ArgSize.alignTo(SlotSize);
Robert Lytton2d196952013-10-11 10:29:34 +00008269 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00008270 case ABIArgInfo::Indirect:
John McCall7f416cc2015-09-08 08:05:57 +00008271 Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
8272 Val = Address(Builder.CreateLoad(Val), TypeAlign);
8273 ArgSize = SlotSize;
Robert Lytton2d196952013-10-11 10:29:34 +00008274 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00008275 }
Robert Lytton2d196952013-10-11 10:29:34 +00008276
8277 // Increment the VAList.
John McCall7f416cc2015-09-08 08:05:57 +00008278 if (!ArgSize.isZero()) {
8279 llvm::Value *APN =
8280 Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize);
8281 Builder.CreateStore(APN, VAListAddr);
Robert Lytton2d196952013-10-11 10:29:34 +00008282 }
John McCall7f416cc2015-09-08 08:05:57 +00008283
Robert Lytton2d196952013-10-11 10:29:34 +00008284 return Val;
Robert Lytton7d1db152013-08-19 09:46:39 +00008285}
Robert Lytton0e076492013-08-13 09:43:10 +00008286
Robert Lytton844aeeb2014-05-02 09:33:20 +00008287/// During the expansion of a RecordType, an incomplete TypeString is placed
8288/// into the cache as a means to identify and break recursion.
8289/// If there is a Recursive encoding in the cache, it is swapped out and will
8290/// be reinserted by removeIncomplete().
8291/// All other types of encoding should have been used rather than arriving here.
8292void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
8293 std::string StubEnc) {
8294 if (!ID)
8295 return;
8296 Entry &E = Map[ID];
8297 assert( (E.Str.empty() || E.State == Recursive) &&
8298 "Incorrectly use of addIncomplete");
8299 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
8300 E.Swapped.swap(E.Str); // swap out the Recursive
8301 E.Str.swap(StubEnc);
8302 E.State = Incomplete;
8303 ++IncompleteCount;
8304}
8305
8306/// Once the RecordType has been expanded, the temporary incomplete TypeString
8307/// must be removed from the cache.
8308/// If a Recursive was swapped out by addIncomplete(), it will be replaced.
8309/// Returns true if the RecordType was defined recursively.
8310bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
8311 if (!ID)
8312 return false;
8313 auto I = Map.find(ID);
8314 assert(I != Map.end() && "Entry not present");
8315 Entry &E = I->second;
8316 assert( (E.State == Incomplete ||
8317 E.State == IncompleteUsed) &&
8318 "Entry must be an incomplete type");
8319 bool IsRecursive = false;
8320 if (E.State == IncompleteUsed) {
8321 // We made use of our Incomplete encoding, thus we are recursive.
8322 IsRecursive = true;
8323 --IncompleteUsedCount;
8324 }
8325 if (E.Swapped.empty())
8326 Map.erase(I);
8327 else {
8328 // Swap the Recursive back.
8329 E.Swapped.swap(E.Str);
8330 E.Swapped.clear();
8331 E.State = Recursive;
8332 }
8333 --IncompleteCount;
8334 return IsRecursive;
8335}
8336
8337/// Add the encoded TypeString to the cache only if it is NonRecursive or
8338/// Recursive (viz: all sub-members were expanded as fully as possible).
8339void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
8340 bool IsRecursive) {
8341 if (!ID || IncompleteUsedCount)
8342 return; // No key or it is is an incomplete sub-type so don't add.
8343 Entry &E = Map[ID];
8344 if (IsRecursive && !E.Str.empty()) {
8345 assert(E.State==Recursive && E.Str.size() == Str.size() &&
8346 "This is not the same Recursive entry");
8347 // The parent container was not recursive after all, so we could have used
8348 // this Recursive sub-member entry after all, but we assumed the worse when
8349 // we started viz: IncompleteCount!=0.
8350 return;
8351 }
8352 assert(E.Str.empty() && "Entry already present");
8353 E.Str = Str.str();
8354 E.State = IsRecursive? Recursive : NonRecursive;
8355}
8356
8357/// Return a cached TypeString encoding for the ID. If there isn't one, or we
8358/// are recursively expanding a type (IncompleteCount != 0) and the cached
8359/// encoding is Recursive, return an empty StringRef.
8360StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
8361 if (!ID)
8362 return StringRef(); // We have no key.
8363 auto I = Map.find(ID);
8364 if (I == Map.end())
8365 return StringRef(); // We have no encoding.
8366 Entry &E = I->second;
8367 if (E.State == Recursive && IncompleteCount)
8368 return StringRef(); // We don't use Recursive encodings for member types.
8369
8370 if (E.State == Incomplete) {
8371 // The incomplete type is being used to break out of recursion.
8372 E.State = IncompleteUsed;
8373 ++IncompleteUsedCount;
8374 }
Malcolm Parsonsf76f6502016-11-02 10:39:27 +00008375 return E.Str;
Robert Lytton844aeeb2014-05-02 09:33:20 +00008376}
8377
8378/// The XCore ABI includes a type information section that communicates symbol
8379/// type information to the linker. The linker uses this information to verify
8380/// safety/correctness of things such as array bound and pointers et al.
8381/// The ABI only requires C (and XC) language modules to emit TypeStrings.
8382/// This type information (TypeString) is emitted into meta data for all global
8383/// symbols: definitions, declarations, functions & variables.
8384///
8385/// The TypeString carries type, qualifier, name, size & value details.
8386/// Please see 'Tools Development Guide' section 2.16.2 for format details:
Eric Christopher7565e0d2015-05-29 23:09:49 +00008387/// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
Robert Lytton844aeeb2014-05-02 09:33:20 +00008388/// The output is tested by test/CodeGen/xcore-stringtype.c.
8389///
8390static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
8391 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
8392
8393/// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
8394void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
8395 CodeGen::CodeGenModule &CGM) const {
8396 SmallStringEnc Enc;
8397 if (getTypeString(Enc, D, CGM, TSC)) {
8398 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
Benjamin Kramer30934732016-07-02 11:41:41 +00008399 llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
8400 llvm::MDString::get(Ctx, Enc.str())};
Robert Lytton844aeeb2014-05-02 09:33:20 +00008401 llvm::NamedMDNode *MD =
8402 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
8403 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
8404 }
8405}
8406
Xiuli Pan972bea82016-03-24 03:57:17 +00008407//===----------------------------------------------------------------------===//
8408// SPIR ABI Implementation
8409//===----------------------------------------------------------------------===//
8410
8411namespace {
8412class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
8413public:
8414 SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
8415 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00008416 unsigned getOpenCLKernelCallingConv() const override;
Xiuli Pan972bea82016-03-24 03:57:17 +00008417};
Pekka Jaaskelainenfc2629a2017-06-01 07:18:49 +00008418
Xiuli Pan972bea82016-03-24 03:57:17 +00008419} // End anonymous namespace.
8420
Pekka Jaaskelainenfc2629a2017-06-01 07:18:49 +00008421namespace clang {
8422namespace CodeGen {
8423void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
8424 DefaultABIInfo SPIRABI(CGM.getTypes());
8425 SPIRABI.computeInfo(FI);
8426}
8427}
8428}
8429
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00008430unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
8431 return llvm::CallingConv::SPIR_KERNEL;
8432}
8433
Robert Lytton844aeeb2014-05-02 09:33:20 +00008434static bool appendType(SmallStringEnc &Enc, QualType QType,
8435 const CodeGen::CodeGenModule &CGM,
8436 TypeStringCache &TSC);
8437
8438/// Helper function for appendRecordType().
Eric Christopher7565e0d2015-05-29 23:09:49 +00008439/// Builds a SmallVector containing the encoded field types in declaration
8440/// order.
Robert Lytton844aeeb2014-05-02 09:33:20 +00008441static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
8442 const RecordDecl *RD,
8443 const CodeGen::CodeGenModule &CGM,
8444 TypeStringCache &TSC) {
Hans Wennborga302cd92014-08-21 16:06:57 +00008445 for (const auto *Field : RD->fields()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008446 SmallStringEnc Enc;
8447 Enc += "m(";
Hans Wennborga302cd92014-08-21 16:06:57 +00008448 Enc += Field->getName();
Robert Lytton844aeeb2014-05-02 09:33:20 +00008449 Enc += "){";
Hans Wennborga302cd92014-08-21 16:06:57 +00008450 if (Field->isBitField()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008451 Enc += "b(";
8452 llvm::raw_svector_ostream OS(Enc);
Hans Wennborga302cd92014-08-21 16:06:57 +00008453 OS << Field->getBitWidthValue(CGM.getContext());
Robert Lytton844aeeb2014-05-02 09:33:20 +00008454 Enc += ':';
8455 }
Hans Wennborga302cd92014-08-21 16:06:57 +00008456 if (!appendType(Enc, Field->getType(), CGM, TSC))
Robert Lytton844aeeb2014-05-02 09:33:20 +00008457 return false;
Hans Wennborga302cd92014-08-21 16:06:57 +00008458 if (Field->isBitField())
Robert Lytton844aeeb2014-05-02 09:33:20 +00008459 Enc += ')';
8460 Enc += '}';
Benjamin Kramer3204b152015-05-29 19:42:19 +00008461 FE.emplace_back(!Field->getName().empty(), Enc);
Robert Lytton844aeeb2014-05-02 09:33:20 +00008462 }
8463 return true;
8464}
8465
8466/// Appends structure and union types to Enc and adds encoding to cache.
8467/// Recursively calls appendType (via extractFieldType) for each field.
8468/// Union types have their fields ordered according to the ABI.
8469static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
8470 const CodeGen::CodeGenModule &CGM,
8471 TypeStringCache &TSC, const IdentifierInfo *ID) {
8472 // Append the cached TypeString if we have one.
8473 StringRef TypeString = TSC.lookupStr(ID);
8474 if (!TypeString.empty()) {
8475 Enc += TypeString;
8476 return true;
8477 }
8478
8479 // Start to emit an incomplete TypeString.
8480 size_t Start = Enc.size();
8481 Enc += (RT->isUnionType()? 'u' : 's');
8482 Enc += '(';
8483 if (ID)
8484 Enc += ID->getName();
8485 Enc += "){";
8486
8487 // We collect all encoded fields and order as necessary.
8488 bool IsRecursive = false;
Robert Lytton844aeeb2014-05-02 09:33:20 +00008489 const RecordDecl *RD = RT->getDecl()->getDefinition();
8490 if (RD && !RD->field_empty()) {
8491 // An incomplete TypeString stub is placed in the cache for this RecordType
8492 // so that recursive calls to this RecordType will use it whilst building a
8493 // complete TypeString for this RecordType.
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008494 SmallVector<FieldEncoding, 16> FE;
Robert Lytton844aeeb2014-05-02 09:33:20 +00008495 std::string StubEnc(Enc.substr(Start).str());
8496 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
8497 TSC.addIncomplete(ID, std::move(StubEnc));
8498 if (!extractFieldType(FE, RD, CGM, TSC)) {
8499 (void) TSC.removeIncomplete(ID);
8500 return false;
8501 }
8502 IsRecursive = TSC.removeIncomplete(ID);
8503 // The ABI requires unions to be sorted but not structures.
8504 // See FieldEncoding::operator< for sort algorithm.
8505 if (RT->isUnionType())
8506 std::sort(FE.begin(), FE.end());
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008507 // We can now complete the TypeString.
8508 unsigned E = FE.size();
Robert Lytton844aeeb2014-05-02 09:33:20 +00008509 for (unsigned I = 0; I != E; ++I) {
8510 if (I)
8511 Enc += ',';
8512 Enc += FE[I].str();
8513 }
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008514 }
Robert Lytton844aeeb2014-05-02 09:33:20 +00008515 Enc += '}';
8516 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
8517 return true;
8518}
8519
8520/// Appends enum types to Enc and adds the encoding to the cache.
8521static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
8522 TypeStringCache &TSC,
8523 const IdentifierInfo *ID) {
8524 // Append the cached TypeString if we have one.
8525 StringRef TypeString = TSC.lookupStr(ID);
8526 if (!TypeString.empty()) {
8527 Enc += TypeString;
8528 return true;
8529 }
8530
8531 size_t Start = Enc.size();
8532 Enc += "e(";
8533 if (ID)
8534 Enc += ID->getName();
8535 Enc += "){";
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008536
8537 // We collect all encoded enumerations and order them alphanumerically.
Robert Lytton844aeeb2014-05-02 09:33:20 +00008538 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008539 SmallVector<FieldEncoding, 16> FE;
8540 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
8541 ++I) {
8542 SmallStringEnc EnumEnc;
8543 EnumEnc += "m(";
8544 EnumEnc += I->getName();
8545 EnumEnc += "){";
8546 I->getInitVal().toString(EnumEnc);
8547 EnumEnc += '}';
8548 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
8549 }
8550 std::sort(FE.begin(), FE.end());
8551 unsigned E = FE.size();
8552 for (unsigned I = 0; I != E; ++I) {
8553 if (I)
Robert Lytton844aeeb2014-05-02 09:33:20 +00008554 Enc += ',';
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008555 Enc += FE[I].str();
Robert Lytton844aeeb2014-05-02 09:33:20 +00008556 }
8557 }
8558 Enc += '}';
8559 TSC.addIfComplete(ID, Enc.substr(Start), false);
8560 return true;
8561}
8562
8563/// Appends type's qualifier to Enc.
8564/// This is done prior to appending the type's encoding.
8565static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
8566 // Qualifiers are emitted in alphabetical order.
Craig Topper273dbc62015-10-18 05:29:26 +00008567 static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
Robert Lytton844aeeb2014-05-02 09:33:20 +00008568 int Lookup = 0;
8569 if (QT.isConstQualified())
8570 Lookup += 1<<0;
8571 if (QT.isRestrictQualified())
8572 Lookup += 1<<1;
8573 if (QT.isVolatileQualified())
8574 Lookup += 1<<2;
8575 Enc += Table[Lookup];
8576}
8577
8578/// Appends built-in types to Enc.
8579static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
8580 const char *EncType;
8581 switch (BT->getKind()) {
8582 case BuiltinType::Void:
8583 EncType = "0";
8584 break;
8585 case BuiltinType::Bool:
8586 EncType = "b";
8587 break;
8588 case BuiltinType::Char_U:
8589 EncType = "uc";
8590 break;
8591 case BuiltinType::UChar:
8592 EncType = "uc";
8593 break;
8594 case BuiltinType::SChar:
8595 EncType = "sc";
8596 break;
8597 case BuiltinType::UShort:
8598 EncType = "us";
8599 break;
8600 case BuiltinType::Short:
8601 EncType = "ss";
8602 break;
8603 case BuiltinType::UInt:
8604 EncType = "ui";
8605 break;
8606 case BuiltinType::Int:
8607 EncType = "si";
8608 break;
8609 case BuiltinType::ULong:
8610 EncType = "ul";
8611 break;
8612 case BuiltinType::Long:
8613 EncType = "sl";
8614 break;
8615 case BuiltinType::ULongLong:
8616 EncType = "ull";
8617 break;
8618 case BuiltinType::LongLong:
8619 EncType = "sll";
8620 break;
8621 case BuiltinType::Float:
8622 EncType = "ft";
8623 break;
8624 case BuiltinType::Double:
8625 EncType = "d";
8626 break;
8627 case BuiltinType::LongDouble:
8628 EncType = "ld";
8629 break;
8630 default:
8631 return false;
8632 }
8633 Enc += EncType;
8634 return true;
8635}
8636
8637/// Appends a pointer encoding to Enc before calling appendType for the pointee.
8638static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
8639 const CodeGen::CodeGenModule &CGM,
8640 TypeStringCache &TSC) {
8641 Enc += "p(";
8642 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
8643 return false;
8644 Enc += ')';
8645 return true;
8646}
8647
8648/// Appends array encoding to Enc before calling appendType for the element.
Robert Lytton6adb20f2014-06-05 09:06:21 +00008649static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
8650 const ArrayType *AT,
Robert Lytton844aeeb2014-05-02 09:33:20 +00008651 const CodeGen::CodeGenModule &CGM,
8652 TypeStringCache &TSC, StringRef NoSizeEnc) {
8653 if (AT->getSizeModifier() != ArrayType::Normal)
8654 return false;
8655 Enc += "a(";
8656 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
8657 CAT->getSize().toStringUnsigned(Enc);
8658 else
8659 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
8660 Enc += ':';
Robert Lytton6adb20f2014-06-05 09:06:21 +00008661 // The Qualifiers should be attached to the type rather than the array.
8662 appendQualifier(Enc, QT);
Robert Lytton844aeeb2014-05-02 09:33:20 +00008663 if (!appendType(Enc, AT->getElementType(), CGM, TSC))
8664 return false;
8665 Enc += ')';
8666 return true;
8667}
8668
8669/// Appends a function encoding to Enc, calling appendType for the return type
8670/// and the arguments.
8671static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
8672 const CodeGen::CodeGenModule &CGM,
8673 TypeStringCache &TSC) {
8674 Enc += "f{";
8675 if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
8676 return false;
8677 Enc += "}(";
8678 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
8679 // N.B. we are only interested in the adjusted param types.
8680 auto I = FPT->param_type_begin();
8681 auto E = FPT->param_type_end();
8682 if (I != E) {
8683 do {
8684 if (!appendType(Enc, *I, CGM, TSC))
8685 return false;
8686 ++I;
8687 if (I != E)
8688 Enc += ',';
8689 } while (I != E);
8690 if (FPT->isVariadic())
8691 Enc += ",va";
8692 } else {
8693 if (FPT->isVariadic())
8694 Enc += "va";
8695 else
8696 Enc += '0';
8697 }
8698 }
8699 Enc += ')';
8700 return true;
8701}
8702
8703/// Handles the type's qualifier before dispatching a call to handle specific
8704/// type encodings.
8705static bool appendType(SmallStringEnc &Enc, QualType QType,
8706 const CodeGen::CodeGenModule &CGM,
8707 TypeStringCache &TSC) {
8708
8709 QualType QT = QType.getCanonicalType();
8710
Robert Lytton6adb20f2014-06-05 09:06:21 +00008711 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
8712 // The Qualifiers should be attached to the type rather than the array.
8713 // Thus we don't call appendQualifier() here.
8714 return appendArrayType(Enc, QT, AT, CGM, TSC, "");
8715
Robert Lytton844aeeb2014-05-02 09:33:20 +00008716 appendQualifier(Enc, QT);
8717
8718 if (const BuiltinType *BT = QT->getAs<BuiltinType>())
8719 return appendBuiltinType(Enc, BT);
8720
Robert Lytton844aeeb2014-05-02 09:33:20 +00008721 if (const PointerType *PT = QT->getAs<PointerType>())
8722 return appendPointerType(Enc, PT, CGM, TSC);
8723
8724 if (const EnumType *ET = QT->getAs<EnumType>())
8725 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
8726
8727 if (const RecordType *RT = QT->getAsStructureType())
8728 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
8729
8730 if (const RecordType *RT = QT->getAsUnionType())
8731 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
8732
8733 if (const FunctionType *FT = QT->getAs<FunctionType>())
8734 return appendFunctionType(Enc, FT, CGM, TSC);
8735
8736 return false;
8737}
8738
8739static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
8740 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
8741 if (!D)
8742 return false;
8743
8744 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
8745 if (FD->getLanguageLinkage() != CLanguageLinkage)
8746 return false;
8747 return appendType(Enc, FD->getType(), CGM, TSC);
8748 }
8749
8750 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
8751 if (VD->getLanguageLinkage() != CLanguageLinkage)
8752 return false;
8753 QualType QT = VD->getType().getCanonicalType();
8754 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
8755 // Global ArrayTypes are given a size of '*' if the size is unknown.
Robert Lytton6adb20f2014-06-05 09:06:21 +00008756 // The Qualifiers should be attached to the type rather than the array.
8757 // Thus we don't call appendQualifier() here.
8758 return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
Robert Lytton844aeeb2014-05-02 09:33:20 +00008759 }
8760 return appendType(Enc, QT, CGM, TSC);
8761 }
8762 return false;
8763}
8764
Alex Bradbury8cbdd482018-01-15 17:54:52 +00008765//===----------------------------------------------------------------------===//
8766// RISCV ABI Implementation
8767//===----------------------------------------------------------------------===//
8768
8769namespace {
8770class RISCVABIInfo : public DefaultABIInfo {
8771private:
8772 unsigned XLen; // Size of the integer ('x') registers in bits.
8773 static const int NumArgGPRs = 8;
8774
8775public:
8776 RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen)
8777 : DefaultABIInfo(CGT), XLen(XLen) {}
8778
8779 // DefaultABIInfo's classifyReturnType and classifyArgumentType are
8780 // non-virtual, but computeInfo is virtual, so we overload it.
8781 void computeInfo(CGFunctionInfo &FI) const override;
8782
8783 ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed,
8784 int &ArgGPRsLeft) const;
8785 ABIArgInfo classifyReturnType(QualType RetTy) const;
8786
8787 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8788 QualType Ty) const override;
8789
8790 ABIArgInfo extendType(QualType Ty) const;
8791};
8792} // end anonymous namespace
8793
8794void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
8795 QualType RetTy = FI.getReturnType();
8796 if (!getCXXABI().classifyReturnType(FI))
8797 FI.getReturnInfo() = classifyReturnType(RetTy);
8798
8799 // IsRetIndirect is true if classifyArgumentType indicated the value should
8800 // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128
8801 // is passed direct in LLVM IR, relying on the backend lowering code to
8802 // rewrite the argument list and pass indirectly on RV32.
8803 bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect ||
8804 getContext().getTypeSize(RetTy) > (2 * XLen);
8805
8806 // We must track the number of GPRs used in order to conform to the RISC-V
8807 // ABI, as integer scalars passed in registers should have signext/zeroext
8808 // when promoted, but are anyext if passed on the stack. As GPR usage is
8809 // different for variadic arguments, we must also track whether we are
8810 // examining a vararg or not.
8811 int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
8812 int NumFixedArgs = FI.getNumRequiredArgs();
8813
8814 int ArgNum = 0;
8815 for (auto &ArgInfo : FI.arguments()) {
8816 bool IsFixed = ArgNum < NumFixedArgs;
8817 ArgInfo.info = classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft);
8818 ArgNum++;
8819 }
8820}
8821
8822ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
8823 int &ArgGPRsLeft) const {
8824 assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
8825 Ty = useFirstFieldIfTransparentUnion(Ty);
8826
8827 // Structures with either a non-trivial destructor or a non-trivial
8828 // copy constructor are always passed indirectly.
8829 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
8830 if (ArgGPRsLeft)
8831 ArgGPRsLeft -= 1;
8832 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
8833 CGCXXABI::RAA_DirectInMemory);
8834 }
8835
8836 // Ignore empty structs/unions.
8837 if (isEmptyRecord(getContext(), Ty, true))
8838 return ABIArgInfo::getIgnore();
8839
8840 uint64_t Size = getContext().getTypeSize(Ty);
8841 uint64_t NeededAlign = getContext().getTypeAlign(Ty);
8842 bool MustUseStack = false;
8843 // Determine the number of GPRs needed to pass the current argument
8844 // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
8845 // register pairs, so may consume 3 registers.
8846 int NeededArgGPRs = 1;
8847 if (!IsFixed && NeededAlign == 2 * XLen)
8848 NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
8849 else if (Size > XLen && Size <= 2 * XLen)
8850 NeededArgGPRs = 2;
8851
8852 if (NeededArgGPRs > ArgGPRsLeft) {
8853 MustUseStack = true;
8854 NeededArgGPRs = ArgGPRsLeft;
8855 }
8856
8857 ArgGPRsLeft -= NeededArgGPRs;
8858
8859 if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
8860 // Treat an enum type as its underlying type.
8861 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8862 Ty = EnumTy->getDecl()->getIntegerType();
8863
8864 // All integral types are promoted to XLen width, unless passed on the
8865 // stack.
8866 if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) {
8867 return extendType(Ty);
8868 }
8869
8870 return ABIArgInfo::getDirect();
8871 }
8872
8873 // Aggregates which are <= 2*XLen will be passed in registers if possible,
8874 // so coerce to integers.
8875 if (Size <= 2 * XLen) {
8876 unsigned Alignment = getContext().getTypeAlign(Ty);
8877
8878 // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
8879 // required, and a 2-element XLen array if only XLen alignment is required.
8880 if (Size <= XLen) {
8881 return ABIArgInfo::getDirect(
8882 llvm::IntegerType::get(getVMContext(), XLen));
8883 } else if (Alignment == 2 * XLen) {
8884 return ABIArgInfo::getDirect(
8885 llvm::IntegerType::get(getVMContext(), 2 * XLen));
8886 } else {
8887 return ABIArgInfo::getDirect(llvm::ArrayType::get(
8888 llvm::IntegerType::get(getVMContext(), XLen), 2));
8889 }
8890 }
8891 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
8892}
8893
8894ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
8895 if (RetTy->isVoidType())
8896 return ABIArgInfo::getIgnore();
8897
8898 int ArgGPRsLeft = 2;
8899
8900 // The rules for return and argument types are the same, so defer to
8901 // classifyArgumentType.
8902 return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft);
8903}
8904
8905Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8906 QualType Ty) const {
8907 CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
8908
8909 // Empty records are ignored for parameter passing purposes.
8910 if (isEmptyRecord(getContext(), Ty, true)) {
8911 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
8912 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
8913 return Addr;
8914 }
8915
8916 std::pair<CharUnits, CharUnits> SizeAndAlign =
8917 getContext().getTypeInfoInChars(Ty);
8918
8919 // Arguments bigger than 2*Xlen bytes are passed indirectly.
8920 bool IsIndirect = SizeAndAlign.first > 2 * SlotSize;
8921
8922 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign,
8923 SlotSize, /*AllowHigherAlign=*/true);
8924}
8925
8926ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
8927 int TySize = getContext().getTypeSize(Ty);
8928 // RV64 ABI requires unsigned 32 bit integers to be sign extended.
8929 if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
8930 return ABIArgInfo::getSignExtend(Ty);
8931 return ABIArgInfo::getExtend(Ty);
8932}
8933
8934namespace {
8935class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
8936public:
8937 RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen)
8938 : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen)) {}
8939};
8940} // namespace
Robert Lytton844aeeb2014-05-02 09:33:20 +00008941
Robert Lytton0e076492013-08-13 09:43:10 +00008942//===----------------------------------------------------------------------===//
8943// Driver code
8944//===----------------------------------------------------------------------===//
8945
Rafael Espindola9f834732014-09-19 01:54:22 +00008946bool CodeGenModule::supportsCOMDAT() const {
Xinliang David Li865cfdd2016-05-25 17:25:57 +00008947 return getTriple().supportsCOMDAT();
Rafael Espindola9f834732014-09-19 01:54:22 +00008948}
8949
Chris Lattner2b037972010-07-29 02:01:43 +00008950const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00008951 if (TheTargetCodeGenInfo)
8952 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00008953
Reid Kleckner9305fd12016-04-13 23:37:17 +00008954 // Helper to set the unique_ptr while still keeping the return value.
8955 auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
8956 this->TheTargetCodeGenInfo.reset(P);
8957 return *P;
8958 };
8959
John McCallc8e01702013-04-16 22:48:15 +00008960 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00008961 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00008962 default:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008963 return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00008964
Derek Schuff09338a22012-09-06 17:37:28 +00008965 case llvm::Triple::le32:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008966 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00008967 case llvm::Triple::mips:
8968 case llvm::Triple::mipsel:
Petar Jovanovic26a4a402015-07-08 13:07:31 +00008969 if (Triple.getOS() == llvm::Triple::NaCl)
Reid Kleckner9305fd12016-04-13 23:37:17 +00008970 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
8971 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
Akira Hatanakac4baedd2013-11-11 22:10:46 +00008972
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00008973 case llvm::Triple::mips64:
8974 case llvm::Triple::mips64el:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008975 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanakac4baedd2013-11-11 22:10:46 +00008976
Dylan McKaye8232d72017-02-08 05:09:26 +00008977 case llvm::Triple::avr:
8978 return SetCGInfo(new AVRTargetCodeGenInfo(Types));
8979
Tim Northover25e8a672014-05-24 12:51:25 +00008980 case llvm::Triple::aarch64:
Tim Northover40956e62014-07-23 12:32:58 +00008981 case llvm::Triple::aarch64_be: {
Tim Northover573cbee2014-05-24 12:52:07 +00008982 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
Alp Toker4925ba72014-06-07 23:30:42 +00008983 if (getTarget().getABI() == "darwinpcs")
Tim Northover573cbee2014-05-24 12:52:07 +00008984 Kind = AArch64ABIInfo::DarwinPCS;
Martin Storsjo502de222017-07-13 17:59:14 +00008985 else if (Triple.isOSWindows())
Martin Storsjo1c8af272017-07-20 05:47:06 +00008986 return SetCGInfo(
8987 new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
Tim Northovera2ee4332014-03-29 15:09:45 +00008988
Reid Kleckner9305fd12016-04-13 23:37:17 +00008989 return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
Tim Northovera2ee4332014-03-29 15:09:45 +00008990 }
8991
Dan Gohmanc2853072015-09-03 22:51:53 +00008992 case llvm::Triple::wasm32:
8993 case llvm::Triple::wasm64:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008994 return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types));
Dan Gohmanc2853072015-09-03 22:51:53 +00008995
Daniel Dunbard59655c2009-09-12 00:59:49 +00008996 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +00008997 case llvm::Triple::armeb:
Daniel Dunbard59655c2009-09-12 00:59:49 +00008998 case llvm::Triple::thumb:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008999 case llvm::Triple::thumbeb: {
9000 if (Triple.getOS() == llvm::Triple::Win32) {
9001 return SetCGInfo(
9002 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
Sandeep Patel45df3dd2011-04-05 00:23:47 +00009003 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00009004
Reid Kleckner9305fd12016-04-13 23:37:17 +00009005 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
9006 StringRef ABIStr = getTarget().getABI();
9007 if (ABIStr == "apcs-gnu")
9008 Kind = ARMABIInfo::APCS;
9009 else if (ABIStr == "aapcs16")
9010 Kind = ARMABIInfo::AAPCS16_VFP;
9011 else if (CodeGenOpts.FloatABI == "hard" ||
9012 (CodeGenOpts.FloatABI != "soft" &&
Oleg Ranevskyy7232f662016-05-13 14:45:57 +00009013 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
Rafael Espindola0fa66802016-06-24 21:35:06 +00009014 Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
Oleg Ranevskyy7232f662016-05-13 14:45:57 +00009015 Triple.getEnvironment() == llvm::Triple::EABIHF)))
Reid Kleckner9305fd12016-04-13 23:37:17 +00009016 Kind = ARMABIInfo::AAPCS_VFP;
9017
9018 return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
9019 }
9020
John McCallea8d8bb2010-03-11 00:10:12 +00009021 case llvm::Triple::ppc:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009022 return SetCGInfo(
9023 new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft"));
Roman Divackyd966e722012-05-09 18:22:46 +00009024 case llvm::Triple::ppc64:
Ulrich Weigandb7122372014-07-21 00:48:09 +00009025 if (Triple.isOSBinFormatELF()) {
Ulrich Weigandb7122372014-07-21 00:48:09 +00009026 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
Ulrich Weigand8afad612014-07-28 13:17:52 +00009027 if (getTarget().getABI() == "elfv2")
9028 Kind = PPC64_SVR4_ABIInfo::ELFv2;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00009029 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Hal Finkel415c2a32016-10-02 02:10:45 +00009030 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
Ulrich Weigand8afad612014-07-28 13:17:52 +00009031
Hal Finkel415c2a32016-10-02 02:10:45 +00009032 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
9033 IsSoftFloat));
Ulrich Weigandb7122372014-07-21 00:48:09 +00009034 } else
Reid Kleckner9305fd12016-04-13 23:37:17 +00009035 return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
Ulrich Weigandb7122372014-07-21 00:48:09 +00009036 case llvm::Triple::ppc64le: {
Bill Schmidt778d3872013-07-26 01:36:11 +00009037 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
Ulrich Weigandb7122372014-07-21 00:48:09 +00009038 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00009039 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
Ulrich Weigand8afad612014-07-28 13:17:52 +00009040 Kind = PPC64_SVR4_ABIInfo::ELFv1;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00009041 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Hal Finkel415c2a32016-10-02 02:10:45 +00009042 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
Ulrich Weigand8afad612014-07-28 13:17:52 +00009043
Hal Finkel415c2a32016-10-02 02:10:45 +00009044 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
9045 IsSoftFloat));
Ulrich Weigandb7122372014-07-21 00:48:09 +00009046 }
John McCallea8d8bb2010-03-11 00:10:12 +00009047
Peter Collingbournec947aae2012-05-20 23:28:41 +00009048 case llvm::Triple::nvptx:
9049 case llvm::Triple::nvptx64:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009050 return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00009051
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00009052 case llvm::Triple::msp430:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009053 return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00009054
Alex Bradbury8cbdd482018-01-15 17:54:52 +00009055 case llvm::Triple::riscv32:
9056 return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 32));
9057 case llvm::Triple::riscv64:
9058 return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 64));
9059
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00009060 case llvm::Triple::systemz: {
9061 bool HasVector = getTarget().getABI() == "vector";
Reid Kleckner9305fd12016-04-13 23:37:17 +00009062 return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector));
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00009063 }
Ulrich Weigand47445072013-05-06 16:26:41 +00009064
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00009065 case llvm::Triple::tce:
Pekka Jaaskelainen67354482016-11-16 15:22:31 +00009066 case llvm::Triple::tcele:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009067 return SetCGInfo(new TCETargetCodeGenInfo(Types));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00009068
Eli Friedman33465822011-07-08 23:31:17 +00009069 case llvm::Triple::x86: {
John McCall1fe2a8c2013-06-18 02:46:29 +00009070 bool IsDarwinVectorABI = Triple.isOSDarwin();
Michael Kupersteindc745202015-10-19 07:52:25 +00009071 bool RetSmallStructInRegABI =
John McCall1fe2a8c2013-06-18 02:46:29 +00009072 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
Saleem Abdulrasoolec5c6242014-11-23 02:16:24 +00009073 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00009074
John McCall1fe2a8c2013-06-18 02:46:29 +00009075 if (Triple.getOS() == llvm::Triple::Win32) {
Reid Kleckner9305fd12016-04-13 23:37:17 +00009076 return SetCGInfo(new WinX86_32TargetCodeGenInfo(
9077 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
9078 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
John McCall1fe2a8c2013-06-18 02:46:29 +00009079 } else {
Reid Kleckner9305fd12016-04-13 23:37:17 +00009080 return SetCGInfo(new X86_32TargetCodeGenInfo(
9081 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
9082 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
9083 CodeGenOpts.FloatABI == "soft"));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00009084 }
Eli Friedman33465822011-07-08 23:31:17 +00009085 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00009086
Eli Friedmanbfd5add2011-12-02 00:11:43 +00009087 case llvm::Triple::x86_64: {
Ahmed Bougachad39a4152015-06-22 21:30:39 +00009088 StringRef ABI = getTarget().getABI();
Reid Kleckner9305fd12016-04-13 23:37:17 +00009089 X86AVXABILevel AVXLevel =
9090 (ABI == "avx512"
9091 ? X86AVXABILevel::AVX512
9092 : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
Ahmed Bougachad39a4152015-06-22 21:30:39 +00009093
Chris Lattner04dc9572010-08-31 16:44:54 +00009094 switch (Triple.getOS()) {
9095 case llvm::Triple::Win32:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009096 return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
Alex Rosenberg12207fa2015-01-27 14:47:44 +00009097 case llvm::Triple::PS4:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009098 return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel));
Chris Lattner04dc9572010-08-31 16:44:54 +00009099 default:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009100 return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
Chris Lattner04dc9572010-08-31 16:44:54 +00009101 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00009102 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00009103 case llvm::Triple::hexagon:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009104 return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
Jacques Pienaard964cc22016-03-28 21:02:54 +00009105 case llvm::Triple::lanai:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009106 return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00009107 case llvm::Triple::r600:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009108 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
Tom Stellardd8e38a32015-01-06 20:34:47 +00009109 case llvm::Triple::amdgcn:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009110 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
Chris Dewhurst7e7ee962016-06-08 14:47:25 +00009111 case llvm::Triple::sparc:
9112 return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00009113 case llvm::Triple::sparcv9:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009114 return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
Robert Lytton0e076492013-08-13 09:43:10 +00009115 case llvm::Triple::xcore:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009116 return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
Xiuli Pan972bea82016-03-24 03:57:17 +00009117 case llvm::Triple::spir:
9118 case llvm::Triple::spir64:
Reid Kleckner9305fd12016-04-13 23:37:17 +00009119 return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
Eli Friedmanbfd5add2011-12-02 00:11:43 +00009120 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00009121}
Yaxun Liuc2a87a02017-10-14 12:23:50 +00009122
9123/// Create an OpenCL kernel for an enqueued block.
9124///
9125/// The kernel has the same function type as the block invoke function. Its
9126/// name is the name of the block invoke function postfixed with "_kernel".
9127/// It simply calls the block invoke function then returns.
9128llvm::Function *
9129TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
9130 llvm::Function *Invoke,
9131 llvm::Value *BlockLiteral) const {
9132 auto *InvokeFT = Invoke->getFunctionType();
9133 llvm::SmallVector<llvm::Type *, 2> ArgTys;
9134 for (auto &P : InvokeFT->params())
9135 ArgTys.push_back(P);
9136 auto &C = CGF.getLLVMContext();
9137 std::string Name = Invoke->getName().str() + "_kernel";
9138 auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
9139 auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
9140 &CGF.CGM.getModule());
9141 auto IP = CGF.Builder.saveIP();
9142 auto *BB = llvm::BasicBlock::Create(C, "entry", F);
9143 auto &Builder = CGF.Builder;
9144 Builder.SetInsertPoint(BB);
9145 llvm::SmallVector<llvm::Value *, 2> Args;
9146 for (auto &A : F->args())
9147 Args.push_back(&A);
9148 Builder.CreateCall(Invoke, Args);
9149 Builder.CreateRetVoid();
9150 Builder.restoreIP(IP);
9151 return F;
9152}
9153
9154/// Create an OpenCL kernel for an enqueued block.
9155///
9156/// The type of the first argument (the block literal) is the struct type
9157/// of the block literal instead of a pointer type. The first argument
9158/// (block literal) is passed directly by value to the kernel. The kernel
9159/// allocates the same type of struct on stack and stores the block literal
9160/// to it and passes its pointer to the block invoke function. The kernel
9161/// has "enqueued-block" function attribute and kernel argument metadata.
9162llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
9163 CodeGenFunction &CGF, llvm::Function *Invoke,
9164 llvm::Value *BlockLiteral) const {
9165 auto &Builder = CGF.Builder;
9166 auto &C = CGF.getLLVMContext();
9167
9168 auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
9169 auto *InvokeFT = Invoke->getFunctionType();
9170 llvm::SmallVector<llvm::Type *, 2> ArgTys;
9171 llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
9172 llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
9173 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
9174 llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
9175 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
9176 llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
9177
9178 ArgTys.push_back(BlockTy);
9179 ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
9180 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
9181 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
9182 ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
9183 AccessQuals.push_back(llvm::MDString::get(C, "none"));
9184 ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
9185 for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
9186 ArgTys.push_back(InvokeFT->getParamType(I));
Yaxun Liuc2a87a02017-10-14 12:23:50 +00009187 ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
9188 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
9189 AccessQuals.push_back(llvm::MDString::get(C, "none"));
9190 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
9191 ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
9192 ArgNames.push_back(
Yaxun Liu98f0c432017-10-14 12:51:52 +00009193 llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
Yaxun Liuc2a87a02017-10-14 12:23:50 +00009194 }
9195 std::string Name = Invoke->getName().str() + "_kernel";
9196 auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
9197 auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
9198 &CGF.CGM.getModule());
9199 F->addFnAttr("enqueued-block");
9200 auto IP = CGF.Builder.saveIP();
9201 auto *BB = llvm::BasicBlock::Create(C, "entry", F);
9202 Builder.SetInsertPoint(BB);
9203 unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy);
9204 auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
9205 BlockPtr->setAlignment(BlockAlign);
9206 Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
9207 auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
9208 llvm::SmallVector<llvm::Value *, 2> Args;
9209 Args.push_back(Cast);
9210 for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
9211 Args.push_back(I);
9212 Builder.CreateCall(Invoke, Args);
9213 Builder.CreateRetVoid();
9214 Builder.restoreIP(IP);
9215
9216 F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
9217 F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
9218 F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
9219 F->setMetadata("kernel_arg_base_type",
9220 llvm::MDNode::get(C, ArgBaseTypeNames));
9221 F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
9222 if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
9223 F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
9224
9225 return F;
9226}