blob: eeebd60a2d20ec051accc48e154834b8e004ff4c [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"
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000017#include "CGCXXABI.h"
Reid Kleckner9b3e3df2014-09-04 20:04:38 +000018#include "CGValue.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000021#include "clang/CodeGen/CGFunctionInfo.h"
John McCall12f23522016-04-04 18:33:08 +000022#include "clang/CodeGen/SwiftCallingConv.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000023#include "clang/Frontend/CodeGenOptions.h"
Matt Arsenault43fae6c2014-12-04 20:38:18 +000024#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000025#include "llvm/ADT/Triple.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/Type.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000028#include "llvm/Support/raw_ostream.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000029#include <algorithm> // std::sort
Robert Lytton844aeeb2014-05-02 09:33:20 +000030
Anton Korobeynikov244360d2009-06-05 22:08:42 +000031using namespace clang;
32using namespace CodeGen;
33
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +000034// Helper for coercing an aggregate argument or return value into an integer
35// array of the same size (including padding) and alignment. This alternate
36// coercion happens only for the RenderScript ABI and can be removed after
37// runtimes that rely on it are no longer supported.
38//
39// RenderScript assumes that the size of the argument / return value in the IR
40// is the same as the size of the corresponding qualified type. This helper
41// coerces the aggregate type into an array of the same size (including
42// padding). This coercion is used in lieu of expansion of struct members or
43// other canonical coercions that return a coerced-type of larger size.
44//
45// Ty - The argument / return value type
46// Context - The associated ASTContext
47// LLVMContext - The associated LLVMContext
48static ABIArgInfo coerceToIntArray(QualType Ty,
49 ASTContext &Context,
50 llvm::LLVMContext &LLVMContext) {
51 // Alignment and Size are measured in bits.
52 const uint64_t Size = Context.getTypeSize(Ty);
53 const uint64_t Alignment = Context.getTypeAlign(Ty);
54 llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
55 const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
56 return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
57}
58
John McCall943fae92010-05-27 06:19:26 +000059static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
60 llvm::Value *Array,
61 llvm::Value *Value,
62 unsigned FirstIndex,
63 unsigned LastIndex) {
64 // Alternatively, we could emit this as a loop in the source.
65 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
David Blaikiefb901c7a2015-04-04 15:12:29 +000066 llvm::Value *Cell =
67 Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
John McCall7f416cc2015-09-08 08:05:57 +000068 Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
John McCall943fae92010-05-27 06:19:26 +000069 }
70}
71
John McCalla1dee5302010-08-22 10:59:02 +000072static bool isAggregateTypeForABI(QualType T) {
John McCall47fb9502013-03-07 21:37:08 +000073 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalla1dee5302010-08-22 10:59:02 +000074 T->isMemberFunctionPointerType();
75}
76
John McCall7f416cc2015-09-08 08:05:57 +000077ABIArgInfo
78ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
79 llvm::Type *Padding) const {
80 return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
81 ByRef, Realign, Padding);
82}
83
84ABIArgInfo
85ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
86 return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
87 /*ByRef*/ false, Realign);
88}
89
Charles Davisc7d5c942015-09-17 20:55:33 +000090Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
91 QualType Ty) const {
92 return Address::invalid();
93}
94
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000095ABIInfo::~ABIInfo() {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +000096
John McCall12f23522016-04-04 18:33:08 +000097/// Does the given lowering require more than the given number of
98/// registers when expanded?
99///
100/// This is intended to be the basis of a reasonable basic implementation
101/// of should{Pass,Return}IndirectlyForSwift.
102///
103/// For most targets, a limit of four total registers is reasonable; this
104/// limits the amount of code required in order to move around the value
105/// in case it wasn't produced immediately prior to the call by the caller
106/// (or wasn't produced in exactly the right registers) or isn't used
107/// immediately within the callee. But some targets may need to further
108/// limit the register count due to an inability to support that many
109/// return registers.
110static bool occupiesMoreThan(CodeGenTypes &cgt,
111 ArrayRef<llvm::Type*> scalarTypes,
112 unsigned maxAllRegisters) {
113 unsigned intCount = 0, fpCount = 0;
114 for (llvm::Type *type : scalarTypes) {
115 if (type->isPointerTy()) {
116 intCount++;
117 } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
118 auto ptrWidth = cgt.getTarget().getPointerWidth(0);
119 intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
120 } else {
121 assert(type->isVectorTy() || type->isFloatingPointTy());
122 fpCount++;
123 }
124 }
125
126 return (intCount + fpCount > maxAllRegisters);
127}
128
129bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
130 llvm::Type *eltTy,
131 unsigned numElts) const {
132 // The default implementation of this assumes that the target guarantees
133 // 128-bit SIMD support but nothing more.
134 return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
135}
136
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000137static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
Mark Lacey3825e832013-10-06 01:33:34 +0000138 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000139 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
140 if (!RD)
141 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +0000142 return CXXABI.getRecordArgABI(RD);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000143}
144
145static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
Mark Lacey3825e832013-10-06 01:33:34 +0000146 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000147 const RecordType *RT = T->getAs<RecordType>();
148 if (!RT)
149 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +0000150 return getRecordArgABI(RT, CXXABI);
151}
152
Reid Klecknerb1be6832014-11-15 01:41:41 +0000153/// Pass transparent unions as if they were the type of the first element. Sema
154/// should ensure that all elements of the union have the same "machine type".
155static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
156 if (const RecordType *UT = Ty->getAsUnionType()) {
157 const RecordDecl *UD = UT->getDecl();
158 if (UD->hasAttr<TransparentUnionAttr>()) {
159 assert(!UD->field_empty() && "sema created an empty transparent union");
160 return UD->field_begin()->getType();
161 }
162 }
163 return Ty;
164}
165
Mark Lacey3825e832013-10-06 01:33:34 +0000166CGCXXABI &ABIInfo::getCXXABI() const {
167 return CGT.getCXXABI();
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000168}
169
Chris Lattner2b037972010-07-29 02:01:43 +0000170ASTContext &ABIInfo::getContext() const {
171 return CGT.getContext();
172}
173
174llvm::LLVMContext &ABIInfo::getVMContext() const {
175 return CGT.getLLVMContext();
176}
177
Micah Villmowdd31ca12012-10-08 16:25:52 +0000178const llvm::DataLayout &ABIInfo::getDataLayout() const {
179 return CGT.getDataLayout();
Chris Lattner2b037972010-07-29 02:01:43 +0000180}
181
John McCallc8e01702013-04-16 22:48:15 +0000182const TargetInfo &ABIInfo::getTarget() const {
183 return CGT.getTarget();
184}
Chris Lattner2b037972010-07-29 02:01:43 +0000185
Nirav Dave9a8f97e2016-02-22 16:48:42 +0000186bool ABIInfo:: isAndroid() const { return getTarget().getTriple().isAndroid(); }
187
Reid Klecknere9f6a712014-10-31 17:10:41 +0000188bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
189 return false;
190}
191
192bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
193 uint64_t Members) const {
194 return false;
195}
196
Petar Jovanovic1a3f9652015-05-26 21:07:19 +0000197bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
198 return false;
199}
200
Yaron Kerencdae9412016-01-29 19:38:18 +0000201LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000202 raw_ostream &OS = llvm::errs();
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000203 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000204 switch (TheKind) {
205 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000206 OS << "Direct Type=";
Chris Lattner2192fe52011-07-18 04:24:23 +0000207 if (llvm::Type *Ty = getCoerceToType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000208 Ty->print(OS);
209 else
210 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000211 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000212 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000213 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000214 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000215 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000216 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000217 break;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000218 case InAlloca:
219 OS << "InAlloca Offset=" << getInAllocaFieldIndex();
220 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000221 case Indirect:
John McCall7f416cc2015-09-08 08:05:57 +0000222 OS << "Indirect Align=" << getIndirectAlign().getQuantity()
Joerg Sonnenberger4921fe22011-07-15 18:23:44 +0000223 << " ByVal=" << getIndirectByVal()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000224 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000225 break;
226 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000227 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000228 break;
John McCallf26e73d2016-03-11 04:30:43 +0000229 case CoerceAndExpand:
230 OS << "CoerceAndExpand Type=";
231 getCoerceAndExpandType()->print(OS);
232 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000233 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000234 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000235}
236
Petar Jovanovic402257b2015-12-04 00:26:47 +0000237// Dynamically round a pointer up to a multiple of the given alignment.
238static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
239 llvm::Value *Ptr,
240 CharUnits Align) {
241 llvm::Value *PtrAsInt = Ptr;
242 // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
243 PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
244 PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
245 llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
246 PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
247 llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
248 PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
249 Ptr->getType(),
250 Ptr->getName() + ".aligned");
251 return PtrAsInt;
252}
253
John McCall7f416cc2015-09-08 08:05:57 +0000254/// Emit va_arg for a platform using the common void* representation,
255/// where arguments are simply emitted in an array of slots on the stack.
256///
257/// This version implements the core direct-value passing rules.
258///
259/// \param SlotSize - The size and alignment of a stack slot.
260/// Each argument will be allocated to a multiple of this number of
261/// slots, and all the slots will be aligned to this value.
262/// \param AllowHigherAlign - The slot alignment is not a cap;
263/// an argument type with an alignment greater than the slot size
264/// will be emitted on a higher-alignment address, potentially
265/// leaving one or more empty slots behind as padding. If this
266/// is false, the returned address might be less-aligned than
267/// DirectAlign.
268static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
269 Address VAListAddr,
270 llvm::Type *DirectTy,
271 CharUnits DirectSize,
272 CharUnits DirectAlign,
273 CharUnits SlotSize,
274 bool AllowHigherAlign) {
275 // Cast the element type to i8* if necessary. Some platforms define
276 // va_list as a struct containing an i8* instead of just an i8*.
277 if (VAListAddr.getElementType() != CGF.Int8PtrTy)
278 VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
279
280 llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
281
282 // If the CC aligns values higher than the slot size, do so if needed.
283 Address Addr = Address::invalid();
284 if (AllowHigherAlign && DirectAlign > SlotSize) {
Petar Jovanovic402257b2015-12-04 00:26:47 +0000285 Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
286 DirectAlign);
John McCall7f416cc2015-09-08 08:05:57 +0000287 } else {
Petar Jovanovic402257b2015-12-04 00:26:47 +0000288 Addr = Address(Ptr, SlotSize);
John McCall7f416cc2015-09-08 08:05:57 +0000289 }
290
291 // Advance the pointer past the argument, then store that back.
Rui Ueyama83aa9792016-01-14 21:00:27 +0000292 CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
John McCall7f416cc2015-09-08 08:05:57 +0000293 llvm::Value *NextPtr =
294 CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize,
295 "argp.next");
296 CGF.Builder.CreateStore(NextPtr, VAListAddr);
297
298 // If the argument is smaller than a slot, and this is a big-endian
299 // target, the argument will be right-adjusted in its slot.
Strahinja Petrovic515a1eb2016-06-24 12:12:41 +0000300 if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
301 !DirectTy->isStructTy()) {
John McCall7f416cc2015-09-08 08:05:57 +0000302 Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
303 }
304
305 Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
306 return Addr;
307}
308
309/// Emit va_arg for a platform using the common void* representation,
310/// where arguments are simply emitted in an array of slots on the stack.
311///
312/// \param IsIndirect - Values of this type are passed indirectly.
313/// \param ValueInfo - The size and alignment of this type, generally
314/// computed with getContext().getTypeInfoInChars(ValueTy).
315/// \param SlotSizeAndAlign - The size and alignment of a stack slot.
316/// Each argument will be allocated to a multiple of this number of
317/// slots, and all the slots will be aligned to this value.
318/// \param AllowHigherAlign - The slot alignment is not a cap;
319/// an argument type with an alignment greater than the slot size
320/// will be emitted on a higher-alignment address, potentially
321/// leaving one or more empty slots behind as padding.
322static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
323 QualType ValueTy, bool IsIndirect,
324 std::pair<CharUnits, CharUnits> ValueInfo,
325 CharUnits SlotSizeAndAlign,
326 bool AllowHigherAlign) {
327 // The size and alignment of the value that was passed directly.
328 CharUnits DirectSize, DirectAlign;
329 if (IsIndirect) {
330 DirectSize = CGF.getPointerSize();
331 DirectAlign = CGF.getPointerAlign();
332 } else {
333 DirectSize = ValueInfo.first;
334 DirectAlign = ValueInfo.second;
335 }
336
337 // Cast the address we've calculated to the right type.
338 llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
339 if (IsIndirect)
340 DirectTy = DirectTy->getPointerTo(0);
341
342 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
343 DirectSize, DirectAlign,
344 SlotSizeAndAlign,
345 AllowHigherAlign);
346
347 if (IsIndirect) {
348 Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
349 }
350
351 return Addr;
352
353}
354
355static Address emitMergePHI(CodeGenFunction &CGF,
356 Address Addr1, llvm::BasicBlock *Block1,
357 Address Addr2, llvm::BasicBlock *Block2,
358 const llvm::Twine &Name = "") {
359 assert(Addr1.getType() == Addr2.getType());
360 llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
361 PHI->addIncoming(Addr1.getPointer(), Block1);
362 PHI->addIncoming(Addr2.getPointer(), Block2);
363 CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
364 return Address(PHI, Align);
365}
366
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000367TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
368
John McCall3480ef22011-08-30 01:42:09 +0000369// If someone can figure out a general rule for this, that would be great.
370// It's probably just doomed to be platform-dependent, though.
371unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
372 // Verified for:
373 // x86-64 FreeBSD, Linux, Darwin
374 // x86-32 FreeBSD, Linux, Darwin
375 // PowerPC Linux, Darwin
376 // ARM Darwin (*not* EABI)
Tim Northover9bb857a2013-01-31 12:13:10 +0000377 // AArch64 Linux
John McCall3480ef22011-08-30 01:42:09 +0000378 return 32;
379}
380
John McCalla729c622012-02-17 03:33:10 +0000381bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
382 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-09-21 08:08:30 +0000383 // The following conventions are known to require this to be false:
384 // x86_stdcall
385 // MIPS
386 // For everything else, we just prefer false unless we opt out.
387 return false;
388}
389
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000390void
391TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
392 llvm::SmallString<24> &Opt) const {
393 // This assumes the user is passing a library name like "rt" instead of a
394 // filename like "librt.a/so", and that they don't care whether it's static or
395 // dynamic.
396 Opt = "-l";
397 Opt += Lib;
398}
399
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000400unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
Pekka Jaaskelainenfc2629a2017-06-01 07:18:49 +0000401 // OpenCL kernels are called via an explicit runtime API with arguments
402 // set with clSetKernelArg(), not as normal sub-functions.
403 // Return SPIR_KERNEL by default as the kernel calling convention to
404 // ensure the fingerprint is fixed such way that each OpenCL argument
405 // gets one matching argument in the produced kernel function argument
406 // list to enable feasible implementation of clSetKernelArg() with
407 // aggregates etc. In case we would use the default C calling conv here,
408 // clSetKernelArg() might break depending on the target-specific
409 // conventions; different targets might split structs passed as values
410 // to multiple function arguments etc.
411 return llvm::CallingConv::SPIR_KERNEL;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000412}
Yaxun Liu37ceede2016-07-20 19:21:11 +0000413
Yaxun Liu402804b2016-12-15 08:09:08 +0000414llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
415 llvm::PointerType *T, QualType QT) const {
416 return llvm::ConstantPointerNull::get(T);
417}
418
Yaxun Liucbf647c2017-07-08 13:24:52 +0000419unsigned TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
420 const VarDecl *D) const {
421 assert(!CGM.getLangOpts().OpenCL &&
422 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
423 "Address space agnostic languages only");
Yaxun Liu7bce6422017-07-08 19:13:41 +0000424 return D ? D->getType().getAddressSpace()
425 : static_cast<unsigned>(LangAS::Default);
Yaxun Liucbf647c2017-07-08 13:24:52 +0000426}
427
Yaxun Liu402804b2016-12-15 08:09:08 +0000428llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000429 CodeGen::CodeGenFunction &CGF, llvm::Value *Src, unsigned SrcAddr,
430 unsigned DestAddr, llvm::Type *DestTy, bool isNonNull) const {
Yaxun Liu402804b2016-12-15 08:09:08 +0000431 // Since target may map different address spaces in AST to the same address
432 // space, an address space conversion may end up as a bitcast.
Yaxun Liucbf647c2017-07-08 13:24:52 +0000433 if (auto *C = dyn_cast<llvm::Constant>(Src))
434 return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000435 return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy);
Yaxun Liu402804b2016-12-15 08:09:08 +0000436}
437
Yaxun Liucbf647c2017-07-08 13:24:52 +0000438llvm::Constant *
439TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
440 unsigned SrcAddr, unsigned DestAddr,
441 llvm::Type *DestTy) const {
442 // Since target may map different address spaces in AST to the same address
443 // space, an address space conversion may end up as a bitcast.
444 return llvm::ConstantExpr::getPointerCast(Src, DestTy);
445}
446
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000447static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000448
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000449/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000450/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000451static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
452 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000453 if (FD->isUnnamedBitfield())
454 return true;
455
456 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000457
Eli Friedman0b3f2012011-11-18 03:47:20 +0000458 // Constant arrays of empty records count as empty, strip them off.
459 // Constant arrays of zero length always count as empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000460 if (AllowArrays)
Eli Friedman0b3f2012011-11-18 03:47:20 +0000461 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
462 if (AT->getSize() == 0)
463 return true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000464 FT = AT->getElementType();
Eli Friedman0b3f2012011-11-18 03:47:20 +0000465 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000466
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000467 const RecordType *RT = FT->getAs<RecordType>();
468 if (!RT)
469 return false;
470
471 // C++ record fields are never empty, at least in the Itanium ABI.
472 //
473 // FIXME: We should use a predicate for whether this behavior is true in the
474 // current ABI.
475 if (isa<CXXRecordDecl>(RT->getDecl()))
476 return false;
477
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000478 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000479}
480
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000481/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000482/// fields. Note that a structure with a flexible array member is not
483/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000484static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000485 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000486 if (!RT)
Denis Zobnin380b2242016-02-11 11:26:03 +0000487 return false;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000488 const RecordDecl *RD = RT->getDecl();
489 if (RD->hasFlexibleArrayMember())
490 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000491
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000492 // If this is a C++ record, check the bases first.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000493 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +0000494 for (const auto &I : CXXRD->bases())
495 if (!isEmptyRecord(Context, I.getType(), true))
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000496 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000497
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000498 for (const auto *I : RD->fields())
499 if (!isEmptyField(Context, I, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000500 return false;
501 return true;
502}
503
504/// isSingleElementStruct - Determine if a structure is a "single
505/// element struct", i.e. it has exactly one non-empty field or
506/// exactly one field which is itself a single element
507/// struct. Structures with flexible array members are never
508/// considered single element structs.
509///
510/// \return The field declaration for the single non-empty field, if
511/// it exists.
512static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Benjamin Kramer83b1bf32015-03-02 16:09:24 +0000513 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000514 if (!RT)
Craig Topper8a13c412014-05-21 05:09:00 +0000515 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000516
517 const RecordDecl *RD = RT->getDecl();
518 if (RD->hasFlexibleArrayMember())
Craig Topper8a13c412014-05-21 05:09:00 +0000519 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000520
Craig Topper8a13c412014-05-21 05:09:00 +0000521 const Type *Found = nullptr;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000522
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000523 // If this is a C++ record, check the bases first.
524 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000525 for (const auto &I : CXXRD->bases()) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000526 // Ignore empty records.
Aaron Ballman574705e2014-03-13 15:41:46 +0000527 if (isEmptyRecord(Context, I.getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000528 continue;
529
530 // If we already found an element then this isn't a single-element struct.
531 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000532 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000533
534 // If this is non-empty and not a single element struct, the composite
535 // cannot be a single element struct.
Aaron Ballman574705e2014-03-13 15:41:46 +0000536 Found = isSingleElementStruct(I.getType(), Context);
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000537 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000538 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000539 }
540 }
541
542 // Check for single element.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000543 for (const auto *FD : RD->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000544 QualType FT = FD->getType();
545
546 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000547 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000548 continue;
549
550 // If we already found an element then this isn't a single-element
551 // struct.
552 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000553 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000554
555 // Treat single element arrays as the element.
556 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
557 if (AT->getSize().getZExtValue() != 1)
558 break;
559 FT = AT->getElementType();
560 }
561
John McCalla1dee5302010-08-22 10:59:02 +0000562 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000563 Found = FT.getTypePtr();
564 } else {
565 Found = isSingleElementStruct(FT, Context);
566 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000567 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000568 }
569 }
570
Eli Friedmanee945342011-11-18 01:25:50 +0000571 // We don't consider a struct a single-element struct if it has
572 // padding beyond the element type.
573 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
Craig Topper8a13c412014-05-21 05:09:00 +0000574 return nullptr;
Eli Friedmanee945342011-11-18 01:25:50 +0000575
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000576 return Found;
577}
578
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000579namespace {
James Y Knight29b5f082016-02-24 02:59:33 +0000580Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
581 const ABIArgInfo &AI) {
582 // This default implementation defers to the llvm backend's va_arg
583 // instruction. It can handle only passing arguments directly
584 // (typically only handled in the backend for primitive types), or
585 // aggregates passed indirectly by pointer (NOTE: if the "byval"
586 // flag has ABI impact in the callee, this implementation cannot
587 // work.)
588
589 // Only a few cases are covered here at the moment -- those needed
590 // by the default abi.
591 llvm::Value *Val;
592
593 if (AI.isIndirect()) {
594 assert(!AI.getPaddingType() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000595 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000596 assert(
597 !AI.getIndirectRealign() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000598 "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000599
600 auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
601 CharUnits TyAlignForABI = TyInfo.second;
602
603 llvm::Type *BaseTy =
604 llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
605 llvm::Value *Addr =
606 CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
607 return Address(Addr, TyAlignForABI);
608 } else {
609 assert((AI.isDirect() || AI.isExtend()) &&
610 "Unexpected ArgInfo Kind in generic VAArg emitter!");
611
612 assert(!AI.getInReg() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000613 "Unexpected InReg seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000614 assert(!AI.getPaddingType() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000615 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000616 assert(!AI.getDirectOffset() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000617 "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000618 assert(!AI.getCoerceToType() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000619 "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000620
621 Address Temp = CGF.CreateMemTemp(Ty, "varet");
622 Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
623 CGF.Builder.CreateStore(Val, Temp);
624 return Temp;
625 }
626}
627
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000628/// DefaultABIInfo - The default implementation for ABI specific
629/// details. This implementation provides information which results in
630/// self-consistent and sensible LLVM IR generation, but does not
631/// conform to any particular ABI.
632class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000633public:
634 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000635
Chris Lattner458b2aa2010-07-29 02:16:43 +0000636 ABIArgInfo classifyReturnType(QualType RetTy) const;
637 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000638
Craig Topper4f12f102014-03-12 06:41:41 +0000639 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000640 if (!getCXXABI().classifyReturnType(FI))
641 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +0000642 for (auto &I : FI.arguments())
643 I.info = classifyArgumentType(I.type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000644 }
645
John McCall7f416cc2015-09-08 08:05:57 +0000646 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
James Y Knight29b5f082016-02-24 02:59:33 +0000647 QualType Ty) const override {
648 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
649 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000650};
651
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000652class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
653public:
Chris Lattner2b037972010-07-29 02:01:43 +0000654 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
655 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000656};
657
Chris Lattner458b2aa2010-07-29 02:16:43 +0000658ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerac385062015-05-18 22:46:30 +0000659 Ty = useFirstFieldIfTransparentUnion(Ty);
660
661 if (isAggregateTypeForABI(Ty)) {
662 // Records with non-trivial destructors/copy-constructors should not be
663 // passed by value.
664 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000665 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Reid Klecknerac385062015-05-18 22:46:30 +0000666
John McCall7f416cc2015-09-08 08:05:57 +0000667 return getNaturalAlignIndirect(Ty);
Reid Klecknerac385062015-05-18 22:46:30 +0000668 }
Daniel Dunbar557893d2010-04-21 19:10:51 +0000669
Chris Lattner9723d6c2010-03-11 18:19:55 +0000670 // Treat an enum type as its underlying type.
671 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
672 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000673
Chris Lattner9723d6c2010-03-11 18:19:55 +0000674 return (Ty->isPromotableIntegerType() ?
675 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000676}
677
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000678ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
679 if (RetTy->isVoidType())
680 return ABIArgInfo::getIgnore();
681
682 if (isAggregateTypeForABI(RetTy))
John McCall7f416cc2015-09-08 08:05:57 +0000683 return getNaturalAlignIndirect(RetTy);
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000684
685 // Treat an enum type as its underlying type.
686 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
687 RetTy = EnumTy->getDecl()->getIntegerType();
688
689 return (RetTy->isPromotableIntegerType() ?
690 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
691}
692
Derek Schuff09338a22012-09-06 17:37:28 +0000693//===----------------------------------------------------------------------===//
Dan Gohmanc2853072015-09-03 22:51:53 +0000694// WebAssembly ABI Implementation
695//
696// This is a very simple ABI that relies a lot on DefaultABIInfo.
697//===----------------------------------------------------------------------===//
698
699class WebAssemblyABIInfo final : public DefaultABIInfo {
700public:
701 explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
702 : DefaultABIInfo(CGT) {}
703
704private:
705 ABIArgInfo classifyReturnType(QualType RetTy) const;
706 ABIArgInfo classifyArgumentType(QualType Ty) const;
707
708 // DefaultABIInfo's classifyReturnType and classifyArgumentType are
Richard Smith81ef0e12016-05-14 01:21:40 +0000709 // non-virtual, but computeInfo and EmitVAArg are virtual, so we
James Y Knight29b5f082016-02-24 02:59:33 +0000710 // overload them.
Dan Gohmanc2853072015-09-03 22:51:53 +0000711 void computeInfo(CGFunctionInfo &FI) const override {
712 if (!getCXXABI().classifyReturnType(FI))
713 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
714 for (auto &Arg : FI.arguments())
715 Arg.info = classifyArgumentType(Arg.type);
716 }
Dan Gohman1fcd10c2016-02-22 19:17:40 +0000717
718 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
719 QualType Ty) const override;
Dan Gohmanc2853072015-09-03 22:51:53 +0000720};
721
722class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
723public:
724 explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
725 : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
726};
727
728/// \brief Classify argument of given type \p Ty.
729ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
730 Ty = useFirstFieldIfTransparentUnion(Ty);
731
732 if (isAggregateTypeForABI(Ty)) {
733 // Records with non-trivial destructors/copy-constructors should not be
734 // passed by value.
Dan Gohmanc2853072015-09-03 22:51:53 +0000735 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000736 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Dan Gohmanc2853072015-09-03 22:51:53 +0000737 // Ignore empty structs/unions.
738 if (isEmptyRecord(getContext(), Ty, true))
739 return ABIArgInfo::getIgnore();
740 // Lower single-element structs to just pass a regular value. TODO: We
741 // could do reasonable-size multiple-element structs too, using getExpand(),
742 // though watch out for things like bitfields.
743 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
744 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
Dan Gohmanc2853072015-09-03 22:51:53 +0000745 }
746
747 // Otherwise just do the default thing.
748 return DefaultABIInfo::classifyArgumentType(Ty);
749}
750
751ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
752 if (isAggregateTypeForABI(RetTy)) {
753 // Records with non-trivial destructors/copy-constructors should not be
754 // returned by value.
755 if (!getRecordArgABI(RetTy, getCXXABI())) {
756 // Ignore empty structs/unions.
757 if (isEmptyRecord(getContext(), RetTy, true))
758 return ABIArgInfo::getIgnore();
759 // Lower single-element structs to just return a regular value. TODO: We
760 // could do reasonable-size multiple-element structs too, using
761 // ABIArgInfo::getDirect().
762 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
763 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
764 }
765 }
766
767 // Otherwise just do the default thing.
768 return DefaultABIInfo::classifyReturnType(RetTy);
769}
770
Dan Gohman1fcd10c2016-02-22 19:17:40 +0000771Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
772 QualType Ty) const {
773 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false,
774 getContext().getTypeInfoInChars(Ty),
775 CharUnits::fromQuantity(4),
776 /*AllowHigherAlign=*/ true);
777}
778
Dan Gohmanc2853072015-09-03 22:51:53 +0000779//===----------------------------------------------------------------------===//
Derek Schuff09338a22012-09-06 17:37:28 +0000780// le32/PNaCl bitcode ABI Implementation
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000781//
782// This is a simplified version of the x86_32 ABI. Arguments and return values
783// are always passed on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000784//===----------------------------------------------------------------------===//
785
786class PNaClABIInfo : public ABIInfo {
787 public:
788 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
789
790 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000791 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff09338a22012-09-06 17:37:28 +0000792
Craig Topper4f12f102014-03-12 06:41:41 +0000793 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +0000794 Address EmitVAArg(CodeGenFunction &CGF,
795 Address VAListAddr, QualType Ty) const override;
Derek Schuff09338a22012-09-06 17:37:28 +0000796};
797
798class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
799 public:
800 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
801 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
802};
803
804void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000805 if (!getCXXABI().classifyReturnType(FI))
Derek Schuff09338a22012-09-06 17:37:28 +0000806 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
807
Reid Kleckner40ca9132014-05-13 22:05:45 +0000808 for (auto &I : FI.arguments())
809 I.info = classifyArgumentType(I.type);
810}
Derek Schuff09338a22012-09-06 17:37:28 +0000811
John McCall7f416cc2015-09-08 08:05:57 +0000812Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
813 QualType Ty) const {
James Y Knight29b5f082016-02-24 02:59:33 +0000814 // The PNaCL ABI is a bit odd, in that varargs don't use normal
815 // function classification. Structs get passed directly for varargs
816 // functions, through a rewriting transform in
817 // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
818 // this target to actually support a va_arg instructions with an
819 // aggregate type, unlike other targets.
820 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000821}
822
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000823/// \brief Classify argument of given type \p Ty.
824ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff09338a22012-09-06 17:37:28 +0000825 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +0000826 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000827 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
828 return getNaturalAlignIndirect(Ty);
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000829 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
830 // Treat an enum type as its underlying type.
Derek Schuff09338a22012-09-06 17:37:28 +0000831 Ty = EnumTy->getDecl()->getIntegerType();
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000832 } else if (Ty->isFloatingType()) {
833 // Floating-point types don't go inreg.
834 return ABIArgInfo::getDirect();
Derek Schuff09338a22012-09-06 17:37:28 +0000835 }
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000836
837 return (Ty->isPromotableIntegerType() ?
838 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000839}
840
841ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
842 if (RetTy->isVoidType())
843 return ABIArgInfo::getIgnore();
844
Eli Benderskye20dad62013-04-04 22:49:35 +0000845 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000846 if (isAggregateTypeForABI(RetTy))
John McCall7f416cc2015-09-08 08:05:57 +0000847 return getNaturalAlignIndirect(RetTy);
Derek Schuff09338a22012-09-06 17:37:28 +0000848
849 // Treat an enum type as its underlying type.
850 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
851 RetTy = EnumTy->getDecl()->getIntegerType();
852
853 return (RetTy->isPromotableIntegerType() ?
854 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
855}
856
Chad Rosier651c1832013-03-25 21:00:27 +0000857/// IsX86_MMXType - Return true if this is an MMX type.
858bool IsX86_MMXType(llvm::Type *IRType) {
859 // 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 +0000860 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
861 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
862 IRType->getScalarSizeInBits() != 64;
863}
864
Jay Foad7c57be32011-07-11 09:56:20 +0000865static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000866 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000867 llvm::Type* Ty) {
Tim Northover0ae93912013-06-07 00:04:50 +0000868 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
869 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
870 // Invalid MMX constraint
Craig Topper8a13c412014-05-21 05:09:00 +0000871 return nullptr;
Tim Northover0ae93912013-06-07 00:04:50 +0000872 }
873
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000874 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover0ae93912013-06-07 00:04:50 +0000875 }
876
877 // No operation needed
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000878 return Ty;
879}
880
Reid Kleckner80944df2014-10-31 22:00:51 +0000881/// Returns true if this type can be passed in SSE registers with the
882/// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
883static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
884 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
885 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half)
886 return true;
887 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
888 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
889 // registers specially.
890 unsigned VecSize = Context.getTypeSize(VT);
891 if (VecSize == 128 || VecSize == 256 || VecSize == 512)
892 return true;
893 }
894 return false;
895}
896
897/// Returns true if this aggregate is small enough to be passed in SSE registers
898/// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
899static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
900 return NumMembers <= 4;
901}
902
Erich Keane521ed962017-01-05 00:20:51 +0000903/// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
904static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
905 auto AI = ABIArgInfo::getDirect(T);
906 AI.setInReg(true);
907 AI.setCanBeFlattened(false);
908 return AI;
909}
910
Chris Lattner0cf24192010-06-28 20:05:43 +0000911//===----------------------------------------------------------------------===//
912// X86-32 ABI Implementation
913//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000914
Reid Kleckner661f35b2014-01-18 01:12:41 +0000915/// \brief Similar to llvm::CCState, but for Clang.
916struct CCState {
Reid Kleckner80944df2014-10-31 22:00:51 +0000917 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
Reid Kleckner661f35b2014-01-18 01:12:41 +0000918
919 unsigned CC;
920 unsigned FreeRegs;
Reid Kleckner80944df2014-10-31 22:00:51 +0000921 unsigned FreeSSERegs;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000922};
923
Erich Keane521ed962017-01-05 00:20:51 +0000924enum {
925 // Vectorcall only allows the first 6 parameters to be passed in registers.
926 VectorcallMaxParamNumAsReg = 6
927};
928
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000929/// X86_32ABIInfo - The X86-32 ABI information.
John McCall12f23522016-04-04 18:33:08 +0000930class X86_32ABIInfo : public SwiftABIInfo {
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000931 enum Class {
932 Integer,
933 Float
934 };
935
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000936 static const unsigned MinABIStackAlignInBytes = 4;
937
David Chisnallde3a0692009-08-17 23:08:21 +0000938 bool IsDarwinVectorABI;
Michael Kupersteindc745202015-10-19 07:52:25 +0000939 bool IsRetSmallStructInRegABI;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000940 bool IsWin32StructABI;
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +0000941 bool IsSoftFloatABI;
Michael Kuperstein68901882015-10-25 08:18:20 +0000942 bool IsMCUABI;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000943 unsigned DefaultNumRegisterParameters;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000944
945 static bool isRegisterSize(unsigned Size) {
946 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
947 }
948
Reid Kleckner80944df2014-10-31 22:00:51 +0000949 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
950 // FIXME: Assumes vectorcall is in use.
951 return isX86VectorTypeForVectorCall(getContext(), Ty);
952 }
953
954 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
955 uint64_t NumMembers) const override {
956 // FIXME: Assumes vectorcall is in use.
957 return isX86VectorCallAggregateSmallEnough(NumMembers);
958 }
959
Reid Kleckner40ca9132014-05-13 22:05:45 +0000960 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000961
Daniel Dunbar557893d2010-04-21 19:10:51 +0000962 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
963 /// such that the argument will be passed in memory.
Reid Kleckner661f35b2014-01-18 01:12:41 +0000964 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
965
John McCall7f416cc2015-09-08 08:05:57 +0000966 ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000967
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000968 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000969 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000970
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000971 Class classify(QualType Ty) const;
Reid Kleckner40ca9132014-05-13 22:05:45 +0000972 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000973 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
Erich Keane4bd39302017-06-21 16:37:22 +0000974
Michael Kupersteinf3163dc2015-12-28 14:39:54 +0000975 /// \brief Updates the number of available free registers, returns
976 /// true if any registers were allocated.
977 bool updateFreeRegs(QualType Ty, CCState &State) const;
978
979 bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
980 bool &NeedsPadding) const;
981 bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000982
Reid Kleckner04046052016-05-02 17:41:07 +0000983 bool canExpandIndirectArgument(QualType Ty) const;
984
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000985 /// \brief Rewrite the function info so that all memory arguments use
986 /// inalloca.
987 void rewriteWithInAlloca(CGFunctionInfo &FI) const;
988
989 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +0000990 CharUnits &StackOffset, ABIArgInfo &Info,
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000991 QualType Type) const;
Erich Keane521ed962017-01-05 00:20:51 +0000992 void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
993 bool &UsedInAlloca) const;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000994
Rafael Espindola75419dc2012-07-23 23:30:29 +0000995public:
996
Craig Topper4f12f102014-03-12 06:41:41 +0000997 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +0000998 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
999 QualType Ty) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001000
Michael Kupersteindc745202015-10-19 07:52:25 +00001001 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1002 bool RetSmallStructInRegABI, bool Win32StructABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001003 unsigned NumRegisterParameters, bool SoftFloatABI)
John McCall12f23522016-04-04 18:33:08 +00001004 : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
Michael Kupersteindc745202015-10-19 07:52:25 +00001005 IsRetSmallStructInRegABI(RetSmallStructInRegABI),
1006 IsWin32StructABI(Win32StructABI),
Manuel Klimekab2e28e2015-10-19 08:43:46 +00001007 IsSoftFloatABI(SoftFloatABI),
Michael Kupersteind749f232015-10-27 07:46:22 +00001008 IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
Manuel Klimekab2e28e2015-10-19 08:43:46 +00001009 DefaultNumRegisterParameters(NumRegisterParameters) {}
John McCall12f23522016-04-04 18:33:08 +00001010
1011 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
1012 ArrayRef<llvm::Type*> scalars,
1013 bool asReturnValue) const override {
1014 // LLVM's x86-32 lowering currently only assigns up to three
1015 // integer registers and three fp registers. Oddly, it'll use up to
1016 // four vector registers for vectors, but those can overlap with the
1017 // scalar registers.
1018 return occupiesMoreThan(CGT, scalars, /*total*/ 3);
1019 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00001020
1021 bool isSwiftErrorInRegister() const override {
1022 // x86-32 lowering does not support passing swifterror in a register.
1023 return false;
1024 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001025};
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001026
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001027class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
1028public:
Michael Kupersteindc745202015-10-19 07:52:25 +00001029 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1030 bool RetSmallStructInRegABI, bool Win32StructABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001031 unsigned NumRegisterParameters, bool SoftFloatABI)
1032 : TargetCodeGenInfo(new X86_32ABIInfo(
1033 CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
1034 NumRegisterParameters, SoftFloatABI)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +00001035
John McCall1fe2a8c2013-06-18 02:46:29 +00001036 static bool isStructReturnInRegABI(
1037 const llvm::Triple &Triple, const CodeGenOptions &Opts);
1038
Eric Christopher162c91c2015-06-05 22:03:00 +00001039 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00001040 CodeGen::CodeGenModule &CGM) const override;
John McCallbeec5a02010-03-06 00:35:14 +00001041
Craig Topper4f12f102014-03-12 06:41:41 +00001042 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +00001043 // Darwin uses different dwarf register numbers for EH.
John McCallc8e01702013-04-16 22:48:15 +00001044 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCallbeec5a02010-03-06 00:35:14 +00001045 return 4;
1046 }
1047
1048 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00001049 llvm::Value *Address) const override;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001050
Jay Foad7c57be32011-07-11 09:56:20 +00001051 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001052 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +00001053 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001054 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1055 }
1056
Reid Kleckner9b3e3df2014-09-04 20:04:38 +00001057 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
1058 std::string &Constraints,
1059 std::vector<llvm::Type *> &ResultRegTypes,
1060 std::vector<llvm::Type *> &ResultTruncRegTypes,
1061 std::vector<LValue> &ResultRegDests,
1062 std::string &AsmString,
1063 unsigned NumOutputs) const override;
1064
Craig Topper4f12f102014-03-12 06:41:41 +00001065 llvm::Constant *
1066 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourneb453cd62013-10-20 21:29:19 +00001067 unsigned Sig = (0xeb << 0) | // jmp rel8
1068 (0x06 << 8) | // .+0x08
1069 ('F' << 16) |
1070 ('T' << 24);
1071 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1072 }
John McCall01391782016-02-05 21:37:38 +00001073
1074 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1075 return "movl\t%ebp, %ebp"
1076 "\t\t## marker for objc_retainAutoreleaseReturnValue";
1077 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001078};
1079
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001080}
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001081
Reid Kleckner9b3e3df2014-09-04 20:04:38 +00001082/// Rewrite input constraint references after adding some output constraints.
1083/// In the case where there is one output and one input and we add one output,
1084/// we need to replace all operand references greater than or equal to 1:
1085/// mov $0, $1
1086/// mov eax, $1
1087/// The result will be:
1088/// mov $0, $2
1089/// mov eax, $2
1090static void rewriteInputConstraintReferences(unsigned FirstIn,
1091 unsigned NumNewOuts,
1092 std::string &AsmString) {
1093 std::string Buf;
1094 llvm::raw_string_ostream OS(Buf);
1095 size_t Pos = 0;
1096 while (Pos < AsmString.size()) {
1097 size_t DollarStart = AsmString.find('$', Pos);
1098 if (DollarStart == std::string::npos)
1099 DollarStart = AsmString.size();
1100 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1101 if (DollarEnd == std::string::npos)
1102 DollarEnd = AsmString.size();
1103 OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1104 Pos = DollarEnd;
1105 size_t NumDollars = DollarEnd - DollarStart;
1106 if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1107 // We have an operand reference.
1108 size_t DigitStart = Pos;
1109 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1110 if (DigitEnd == std::string::npos)
1111 DigitEnd = AsmString.size();
1112 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1113 unsigned OperandIndex;
1114 if (!OperandStr.getAsInteger(10, OperandIndex)) {
1115 if (OperandIndex >= FirstIn)
1116 OperandIndex += NumNewOuts;
1117 OS << OperandIndex;
1118 } else {
1119 OS << OperandStr;
1120 }
1121 Pos = DigitEnd;
1122 }
1123 }
1124 AsmString = std::move(OS.str());
1125}
1126
1127/// Add output constraints for EAX:EDX because they are return registers.
1128void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1129 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1130 std::vector<llvm::Type *> &ResultRegTypes,
1131 std::vector<llvm::Type *> &ResultTruncRegTypes,
1132 std::vector<LValue> &ResultRegDests, std::string &AsmString,
1133 unsigned NumOutputs) const {
1134 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1135
1136 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1137 // larger.
1138 if (!Constraints.empty())
1139 Constraints += ',';
1140 if (RetWidth <= 32) {
1141 Constraints += "={eax}";
1142 ResultRegTypes.push_back(CGF.Int32Ty);
1143 } else {
1144 // Use the 'A' constraint for EAX:EDX.
1145 Constraints += "=A";
1146 ResultRegTypes.push_back(CGF.Int64Ty);
1147 }
1148
1149 // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1150 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1151 ResultTruncRegTypes.push_back(CoerceTy);
1152
1153 // Coerce the integer by bitcasting the return slot pointer.
1154 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
1155 CoerceTy->getPointerTo()));
1156 ResultRegDests.push_back(ReturnSlot);
1157
1158 rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1159}
1160
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001161/// shouldReturnTypeInRegister - Determine if the given type should be
Michael Kuperstein68901882015-10-25 08:18:20 +00001162/// returned in a register (for the Darwin and MCU ABI).
Reid Kleckner40ca9132014-05-13 22:05:45 +00001163bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1164 ASTContext &Context) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001165 uint64_t Size = Context.getTypeSize(Ty);
1166
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001167 // For i386, type must be register sized.
1168 // For the MCU ABI, it only needs to be <= 8-byte
1169 if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1170 return false;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001171
1172 if (Ty->isVectorType()) {
1173 // 64- and 128- bit vectors inside structures are not returned in
1174 // registers.
1175 if (Size == 64 || Size == 128)
1176 return false;
1177
1178 return true;
1179 }
1180
Daniel Dunbar4bd95c62010-05-15 00:00:30 +00001181 // If this is a builtin, pointer, enum, complex type, member pointer, or
1182 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +00001183 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +00001184 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +00001185 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001186 return true;
1187
1188 // Arrays are treated like records.
1189 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Reid Kleckner40ca9132014-05-13 22:05:45 +00001190 return shouldReturnTypeInRegister(AT->getElementType(), Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001191
1192 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001193 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001194 if (!RT) return false;
1195
Anders Carlsson40446e82010-01-27 03:25:19 +00001196 // FIXME: Traverse bases here too.
1197
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001198 // Structure types are passed in register if all fields would be
1199 // passed in a register.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001200 for (const auto *FD : RT->getDecl()->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001201 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001202 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001203 continue;
1204
1205 // Check fields recursively.
Reid Kleckner40ca9132014-05-13 22:05:45 +00001206 if (!shouldReturnTypeInRegister(FD->getType(), Context))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001207 return false;
1208 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001209 return true;
1210}
1211
Reid Kleckner04046052016-05-02 17:41:07 +00001212static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1213 // Treat complex types as the element type.
1214 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1215 Ty = CTy->getElementType();
1216
1217 // Check for a type which we know has a simple scalar argument-passing
1218 // convention without any padding. (We're specifically looking for 32
1219 // and 64-bit integer and integer-equivalents, float, and double.)
1220 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1221 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1222 return false;
1223
1224 uint64_t Size = Context.getTypeSize(Ty);
1225 return Size == 32 || Size == 64;
1226}
1227
Reid Kleckner791bbf62017-01-13 17:18:19 +00001228static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
1229 uint64_t &Size) {
1230 for (const auto *FD : RD->fields()) {
1231 // Scalar arguments on the stack get 4 byte alignment on x86. If the
1232 // argument is smaller than 32-bits, expanding the struct will create
1233 // alignment padding.
1234 if (!is32Or64BitBasicType(FD->getType(), Context))
1235 return false;
1236
1237 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1238 // how to expand them yet, and the predicate for telling if a bitfield still
1239 // counts as "basic" is more complicated than what we were doing previously.
1240 if (FD->isBitField())
1241 return false;
1242
1243 Size += Context.getTypeSize(FD->getType());
1244 }
1245 return true;
1246}
1247
1248static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
1249 uint64_t &Size) {
1250 // Don't do this if there are any non-empty bases.
1251 for (const CXXBaseSpecifier &Base : RD->bases()) {
1252 if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
1253 Size))
1254 return false;
1255 }
1256 if (!addFieldSizes(Context, RD, Size))
1257 return false;
1258 return true;
1259}
1260
Reid Kleckner04046052016-05-02 17:41:07 +00001261/// Test whether an argument type which is to be passed indirectly (on the
1262/// stack) would have the equivalent layout if it was expanded into separate
1263/// arguments. If so, we prefer to do the latter to avoid inhibiting
1264/// optimizations.
1265bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1266 // We can only expand structure types.
1267 const RecordType *RT = Ty->getAs<RecordType>();
1268 if (!RT)
1269 return false;
1270 const RecordDecl *RD = RT->getDecl();
Reid Kleckner791bbf62017-01-13 17:18:19 +00001271 uint64_t Size = 0;
Reid Kleckner04046052016-05-02 17:41:07 +00001272 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Reid Kleckner791bbf62017-01-13 17:18:19 +00001273 if (!IsWin32StructABI) {
Reid Kleckner04046052016-05-02 17:41:07 +00001274 // On non-Windows, we have to conservatively match our old bitcode
1275 // prototypes in order to be ABI-compatible at the bitcode level.
1276 if (!CXXRD->isCLike())
1277 return false;
1278 } else {
1279 // Don't do this for dynamic classes.
1280 if (CXXRD->isDynamicClass())
1281 return false;
Reid Kleckner04046052016-05-02 17:41:07 +00001282 }
Reid Kleckner791bbf62017-01-13 17:18:19 +00001283 if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
Reid Kleckner04046052016-05-02 17:41:07 +00001284 return false;
Reid Kleckner791bbf62017-01-13 17:18:19 +00001285 } else {
1286 if (!addFieldSizes(getContext(), RD, Size))
Reid Kleckner04046052016-05-02 17:41:07 +00001287 return false;
Reid Kleckner04046052016-05-02 17:41:07 +00001288 }
1289
1290 // We can do this if there was no alignment padding.
1291 return Size == getContext().getTypeSize(Ty);
1292}
1293
John McCall7f416cc2015-09-08 08:05:57 +00001294ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001295 // If the return value is indirect, then the hidden argument is consuming one
1296 // integer register.
1297 if (State.FreeRegs) {
1298 --State.FreeRegs;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001299 if (!IsMCUABI)
1300 return getNaturalAlignIndirectInReg(RetTy);
Reid Kleckner661f35b2014-01-18 01:12:41 +00001301 }
John McCall7f416cc2015-09-08 08:05:57 +00001302 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
Reid Kleckner661f35b2014-01-18 01:12:41 +00001303}
1304
Eric Christopher7565e0d2015-05-29 23:09:49 +00001305ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1306 CCState &State) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001307 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001308 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001309
Reid Kleckner80944df2014-10-31 22:00:51 +00001310 const Type *Base = nullptr;
1311 uint64_t NumElts = 0;
Erich Keane757d3172016-11-02 18:29:35 +00001312 if ((State.CC == llvm::CallingConv::X86_VectorCall ||
1313 State.CC == llvm::CallingConv::X86_RegCall) &&
Reid Kleckner80944df2014-10-31 22:00:51 +00001314 isHomogeneousAggregate(RetTy, Base, NumElts)) {
1315 // The LLVM struct type for such an aggregate should lower properly.
1316 return ABIArgInfo::getDirect();
1317 }
1318
Chris Lattner458b2aa2010-07-29 02:16:43 +00001319 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001320 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +00001321 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001322 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001323
1324 // 128-bit vectors are a special case; they are returned in
1325 // registers and we need to make sure to pick a type the LLVM
1326 // backend will like.
1327 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001328 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +00001329 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001330
1331 // Always return in register if it fits in a general purpose
1332 // register, or if it is 64 bits and has a single element.
1333 if ((Size == 8 || Size == 16 || Size == 32) ||
1334 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001335 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00001336 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001337
John McCall7f416cc2015-09-08 08:05:57 +00001338 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001339 }
1340
1341 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +00001342 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001343
John McCalla1dee5302010-08-22 10:59:02 +00001344 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +00001345 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +00001346 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001347 if (RT->getDecl()->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00001348 return getIndirectReturnResult(RetTy, State);
Anders Carlsson5789c492009-10-20 22:07:59 +00001349 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001350
David Chisnallde3a0692009-08-17 23:08:21 +00001351 // If specified, structs and unions are always indirect.
Michael Kupersteindc745202015-10-19 07:52:25 +00001352 if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
John McCall7f416cc2015-09-08 08:05:57 +00001353 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001354
Denis Zobnin380b2242016-02-11 11:26:03 +00001355 // Ignore empty structs/unions.
1356 if (isEmptyRecord(getContext(), RetTy, true))
1357 return ABIArgInfo::getIgnore();
1358
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001359 // Small structures which are register sized are generally returned
1360 // in a register.
Reid Kleckner40ca9132014-05-13 22:05:45 +00001361 if (shouldReturnTypeInRegister(RetTy, getContext())) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001362 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +00001363
1364 // As a special-case, if the struct is a "single-element" struct, and
1365 // the field is of type "float" or "double", return it in a
Eli Friedmana98d1f82012-01-25 22:46:34 +00001366 // floating-point register. (MSVC does not apply this special case.)
1367 // We apply a similar transformation for pointer types to improve the
1368 // quality of the generated IR.
Eli Friedmanee945342011-11-18 01:25:50 +00001369 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001370 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedmana98d1f82012-01-25 22:46:34 +00001371 || SeltTy->hasPointerRepresentation())
Eli Friedmanee945342011-11-18 01:25:50 +00001372 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1373
1374 // FIXME: We should be able to narrow this integer in cases with dead
1375 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001376 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001377 }
1378
John McCall7f416cc2015-09-08 08:05:57 +00001379 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001380 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001381
Chris Lattner458b2aa2010-07-29 02:16:43 +00001382 // Treat an enum type as its underlying type.
1383 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1384 RetTy = EnumTy->getDecl()->getIntegerType();
1385
1386 return (RetTy->isPromotableIntegerType() ?
1387 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001388}
1389
Eli Friedman7919bea2012-06-05 19:40:46 +00001390static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
1391 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1392}
1393
Daniel Dunbared23de32010-09-16 20:42:00 +00001394static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
1395 const RecordType *RT = Ty->getAs<RecordType>();
1396 if (!RT)
1397 return 0;
1398 const RecordDecl *RD = RT->getDecl();
1399
1400 // If this is a C++ record, check the bases first.
1401 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +00001402 for (const auto &I : CXXRD->bases())
1403 if (!isRecordWithSSEVectorType(Context, I.getType()))
Daniel Dunbared23de32010-09-16 20:42:00 +00001404 return false;
1405
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001406 for (const auto *i : RD->fields()) {
Daniel Dunbared23de32010-09-16 20:42:00 +00001407 QualType FT = i->getType();
1408
Eli Friedman7919bea2012-06-05 19:40:46 +00001409 if (isSSEVectorType(Context, FT))
Daniel Dunbared23de32010-09-16 20:42:00 +00001410 return true;
1411
1412 if (isRecordWithSSEVectorType(Context, FT))
1413 return true;
1414 }
1415
1416 return false;
1417}
1418
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001419unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1420 unsigned Align) const {
1421 // Otherwise, if the alignment is less than or equal to the minimum ABI
1422 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001423 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001424 return 0; // Use default alignment.
1425
1426 // On non-Darwin, the stack type alignment is always 4.
1427 if (!IsDarwinVectorABI) {
1428 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001429 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001430 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001431
Daniel Dunbared23de32010-09-16 20:42:00 +00001432 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7919bea2012-06-05 19:40:46 +00001433 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
1434 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbared23de32010-09-16 20:42:00 +00001435 return 16;
1436
1437 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001438}
1439
Rafael Espindola703c47f2012-10-19 05:04:37 +00001440ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
Reid Kleckner661f35b2014-01-18 01:12:41 +00001441 CCState &State) const {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001442 if (!ByVal) {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001443 if (State.FreeRegs) {
1444 --State.FreeRegs; // Non-byval indirects just use one pointer.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001445 if (!IsMCUABI)
1446 return getNaturalAlignIndirectInReg(Ty);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001447 }
John McCall7f416cc2015-09-08 08:05:57 +00001448 return getNaturalAlignIndirect(Ty, false);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001449 }
Daniel Dunbar53fac692010-04-21 19:49:55 +00001450
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001451 // Compute the byval alignment.
1452 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1453 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1454 if (StackAlign == 0)
John McCall7f416cc2015-09-08 08:05:57 +00001455 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001456
1457 // If the stack alignment is less than the type alignment, realign the
1458 // argument.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001459 bool Realign = TypeAlign > StackAlign;
John McCall7f416cc2015-09-08 08:05:57 +00001460 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1461 /*ByVal=*/true, Realign);
Daniel Dunbar557893d2010-04-21 19:10:51 +00001462}
1463
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001464X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1465 const Type *T = isSingleElementStruct(Ty, getContext());
1466 if (!T)
1467 T = Ty.getTypePtr();
1468
1469 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1470 BuiltinType::Kind K = BT->getKind();
1471 if (K == BuiltinType::Float || K == BuiltinType::Double)
1472 return Float;
1473 }
1474 return Integer;
1475}
1476
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001477bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001478 if (!IsSoftFloatABI) {
1479 Class C = classify(Ty);
1480 if (C == Float)
1481 return false;
1482 }
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001483
Rafael Espindola077dd592012-10-24 01:58:58 +00001484 unsigned Size = getContext().getTypeSize(Ty);
1485 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindolae2a9e902012-10-23 02:04:01 +00001486
1487 if (SizeInRegs == 0)
1488 return false;
1489
Michael Kuperstein68901882015-10-25 08:18:20 +00001490 if (!IsMCUABI) {
1491 if (SizeInRegs > State.FreeRegs) {
1492 State.FreeRegs = 0;
1493 return false;
1494 }
1495 } else {
1496 // The MCU psABI allows passing parameters in-reg even if there are
1497 // earlier parameters that are passed on the stack. Also,
1498 // it does not allow passing >8-byte structs in-register,
1499 // even if there are 3 free registers available.
1500 if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1501 return false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001502 }
Rafael Espindola703c47f2012-10-19 05:04:37 +00001503
Reid Kleckner661f35b2014-01-18 01:12:41 +00001504 State.FreeRegs -= SizeInRegs;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001505 return true;
1506}
1507
1508bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1509 bool &InReg,
1510 bool &NeedsPadding) const {
Reid Kleckner04046052016-05-02 17:41:07 +00001511 // On Windows, aggregates other than HFAs are never passed in registers, and
1512 // they do not consume register slots. Homogenous floating-point aggregates
1513 // (HFAs) have already been dealt with at this point.
1514 if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1515 return false;
1516
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001517 NeedsPadding = false;
1518 InReg = !IsMCUABI;
1519
1520 if (!updateFreeRegs(Ty, State))
1521 return false;
1522
1523 if (IsMCUABI)
1524 return true;
Rafael Espindola077dd592012-10-24 01:58:58 +00001525
Reid Kleckner80944df2014-10-31 22:00:51 +00001526 if (State.CC == llvm::CallingConv::X86_FastCall ||
Erich Keane757d3172016-11-02 18:29:35 +00001527 State.CC == llvm::CallingConv::X86_VectorCall ||
1528 State.CC == llvm::CallingConv::X86_RegCall) {
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001529 if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
Rafael Espindolafad28de2012-10-24 01:59:00 +00001530 NeedsPadding = true;
1531
Rafael Espindola077dd592012-10-24 01:58:58 +00001532 return false;
1533 }
1534
Rafael Espindola703c47f2012-10-19 05:04:37 +00001535 return true;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001536}
1537
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001538bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1539 if (!updateFreeRegs(Ty, State))
1540 return false;
1541
1542 if (IsMCUABI)
1543 return false;
1544
1545 if (State.CC == llvm::CallingConv::X86_FastCall ||
Erich Keane757d3172016-11-02 18:29:35 +00001546 State.CC == llvm::CallingConv::X86_VectorCall ||
1547 State.CC == llvm::CallingConv::X86_RegCall) {
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001548 if (getContext().getTypeSize(Ty) > 32)
1549 return false;
1550
1551 return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1552 Ty->isReferenceType());
1553 }
1554
1555 return true;
1556}
1557
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001558ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1559 CCState &State) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001560 // FIXME: Set alignment on indirect arguments.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001561
Reid Klecknerb1be6832014-11-15 01:41:41 +00001562 Ty = useFirstFieldIfTransparentUnion(Ty);
1563
Reid Kleckner80944df2014-10-31 22:00:51 +00001564 // Check with the C++ ABI first.
1565 const RecordType *RT = Ty->getAs<RecordType>();
1566 if (RT) {
1567 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1568 if (RAA == CGCXXABI::RAA_Indirect) {
1569 return getIndirectResult(Ty, false, State);
1570 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1571 // The field index doesn't matter, we'll fix it up later.
1572 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1573 }
1574 }
1575
Erich Keane4bd39302017-06-21 16:37:22 +00001576 // Regcall uses the concept of a homogenous vector aggregate, similar
1577 // to other targets.
Reid Kleckner80944df2014-10-31 22:00:51 +00001578 const Type *Base = nullptr;
1579 uint64_t NumElts = 0;
Erich Keane4bd39302017-06-21 16:37:22 +00001580 if (State.CC == llvm::CallingConv::X86_RegCall &&
Reid Kleckner80944df2014-10-31 22:00:51 +00001581 isHomogeneousAggregate(Ty, Base, NumElts)) {
Erich Keane521ed962017-01-05 00:20:51 +00001582
Erich Keane4bd39302017-06-21 16:37:22 +00001583 if (State.FreeSSERegs >= NumElts) {
1584 State.FreeSSERegs -= NumElts;
1585 if (Ty->isBuiltinType() || Ty->isVectorType())
Reid Kleckner80944df2014-10-31 22:00:51 +00001586 return ABIArgInfo::getDirect();
Erich Keane4bd39302017-06-21 16:37:22 +00001587 return ABIArgInfo::getExpand();
Reid Kleckner80944df2014-10-31 22:00:51 +00001588 }
Erich Keane4bd39302017-06-21 16:37:22 +00001589 return getIndirectResult(Ty, /*ByVal=*/false, State);
Reid Kleckner80944df2014-10-31 22:00:51 +00001590 }
1591
1592 if (isAggregateTypeForABI(Ty)) {
Reid Kleckner04046052016-05-02 17:41:07 +00001593 // Structures with flexible arrays are always indirect.
1594 // FIXME: This should not be byval!
1595 if (RT && RT->getDecl()->hasFlexibleArrayMember())
1596 return getIndirectResult(Ty, true, State);
Daniel Dunbar557893d2010-04-21 19:10:51 +00001597
Reid Kleckner04046052016-05-02 17:41:07 +00001598 // Ignore empty structs/unions on non-Windows.
1599 if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001600 return ABIArgInfo::getIgnore();
1601
Rafael Espindolafad28de2012-10-24 01:59:00 +00001602 llvm::LLVMContext &LLVMContext = getVMContext();
1603 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
Reid Kleckner04046052016-05-02 17:41:07 +00001604 bool NeedsPadding = false;
1605 bool InReg;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001606 if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001607 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperac9201a2013-07-08 04:47:18 +00001608 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001609 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001610 if (InReg)
1611 return ABIArgInfo::getDirectInReg(Result);
1612 else
1613 return ABIArgInfo::getDirect(Result);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001614 }
Craig Topper8a13c412014-05-21 05:09:00 +00001615 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
Rafael Espindola703c47f2012-10-19 05:04:37 +00001616
Daniel Dunbar11c08c82009-11-09 01:33:53 +00001617 // Expand small (<= 128-bit) record types when we know that the stack layout
1618 // of those arguments will match the struct. This is important because the
1619 // LLVM backend isn't smart enough to remove byval, which inhibits many
1620 // optimizations.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001621 // Don't do this for the MCU if there are still free integer registers
1622 // (see X86_64 ABI for full explanation).
Reid Kleckner04046052016-05-02 17:41:07 +00001623 if (getContext().getTypeSize(Ty) <= 4 * 32 &&
1624 (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty))
Reid Kleckner661f35b2014-01-18 01:12:41 +00001625 return ABIArgInfo::getExpandWithPadding(
Reid Kleckner80944df2014-10-31 22:00:51 +00001626 State.CC == llvm::CallingConv::X86_FastCall ||
Erich Keane757d3172016-11-02 18:29:35 +00001627 State.CC == llvm::CallingConv::X86_VectorCall ||
1628 State.CC == llvm::CallingConv::X86_RegCall,
Reid Kleckner80944df2014-10-31 22:00:51 +00001629 PaddingType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001630
Reid Kleckner661f35b2014-01-18 01:12:41 +00001631 return getIndirectResult(Ty, true, State);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001632 }
1633
Chris Lattnerd774ae92010-08-26 20:05:13 +00001634 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +00001635 // On Darwin, some vectors are passed in memory, we handle this by passing
1636 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +00001637 if (IsDarwinVectorABI) {
1638 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +00001639 if ((Size == 8 || Size == 16 || Size == 32) ||
1640 (Size == 64 && VT->getNumElements() == 1))
1641 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1642 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +00001643 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001644
Chad Rosier651c1832013-03-25 21:00:27 +00001645 if (IsX86_MMXType(CGT.ConvertType(Ty)))
1646 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001647
Chris Lattnerd774ae92010-08-26 20:05:13 +00001648 return ABIArgInfo::getDirect();
1649 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001650
1651
Chris Lattner458b2aa2010-07-29 02:16:43 +00001652 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1653 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +00001654
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001655 bool InReg = shouldPrimitiveUseInReg(Ty, State);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001656
1657 if (Ty->isPromotableIntegerType()) {
1658 if (InReg)
1659 return ABIArgInfo::getExtendInReg();
1660 return ABIArgInfo::getExtend();
1661 }
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001662
Rafael Espindola703c47f2012-10-19 05:04:37 +00001663 if (InReg)
1664 return ABIArgInfo::getDirectInReg();
1665 return ABIArgInfo::getDirect();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001666}
1667
Erich Keane521ed962017-01-05 00:20:51 +00001668void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
1669 bool &UsedInAlloca) const {
Erich Keane4bd39302017-06-21 16:37:22 +00001670 // Vectorcall x86 works subtly different than in x64, so the format is
1671 // a bit different than the x64 version. First, all vector types (not HVAs)
1672 // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers.
1673 // This differs from the x64 implementation, where the first 6 by INDEX get
1674 // registers.
1675 // After that, integers AND HVAs are assigned Left to Right in the same pass.
1676 // Integers are passed as ECX/EDX if one is available (in order). HVAs will
1677 // first take up the remaining YMM/XMM registers. If insufficient registers
1678 // remain but an integer register (ECX/EDX) is available, it will be passed
1679 // in that, else, on the stack.
Erich Keane521ed962017-01-05 00:20:51 +00001680 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00001681 // First pass do all the vector types.
1682 const Type *Base = nullptr;
1683 uint64_t NumElts = 0;
1684 const QualType& Ty = I.type;
1685 if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
1686 isHomogeneousAggregate(Ty, Base, NumElts)) {
1687 if (State.FreeSSERegs >= NumElts) {
1688 State.FreeSSERegs -= NumElts;
1689 I.info = ABIArgInfo::getDirect();
1690 } else {
1691 I.info = classifyArgumentType(Ty, State);
1692 }
1693 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1694 }
Erich Keane521ed962017-01-05 00:20:51 +00001695 }
Erich Keane4bd39302017-06-21 16:37:22 +00001696
Erich Keane521ed962017-01-05 00:20:51 +00001697 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00001698 // Second pass, do the rest!
1699 const Type *Base = nullptr;
1700 uint64_t NumElts = 0;
1701 const QualType& Ty = I.type;
1702 bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts);
1703
1704 if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) {
1705 // Assign true HVAs (non vector/native FP types).
1706 if (State.FreeSSERegs >= NumElts) {
1707 State.FreeSSERegs -= NumElts;
1708 I.info = getDirectX86Hva();
1709 } else {
1710 I.info = getIndirectResult(Ty, /*ByVal=*/false, State);
1711 }
1712 } else if (!IsHva) {
1713 // Assign all Non-HVAs, so this will exclude Vector/FP args.
1714 I.info = classifyArgumentType(Ty, State);
1715 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1716 }
Erich Keane521ed962017-01-05 00:20:51 +00001717 }
1718}
1719
Rafael Espindolaa6472962012-07-24 00:01:07 +00001720void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001721 CCState State(FI.getCallingConvention());
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001722 if (IsMCUABI)
1723 State.FreeRegs = 3;
1724 else if (State.CC == llvm::CallingConv::X86_FastCall)
Reid Kleckner661f35b2014-01-18 01:12:41 +00001725 State.FreeRegs = 2;
Reid Kleckner80944df2014-10-31 22:00:51 +00001726 else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1727 State.FreeRegs = 2;
1728 State.FreeSSERegs = 6;
1729 } else if (FI.getHasRegParm())
Reid Kleckner661f35b2014-01-18 01:12:41 +00001730 State.FreeRegs = FI.getRegParm();
Erich Keane757d3172016-11-02 18:29:35 +00001731 else if (State.CC == llvm::CallingConv::X86_RegCall) {
1732 State.FreeRegs = 5;
1733 State.FreeSSERegs = 8;
1734 } else
Reid Kleckner661f35b2014-01-18 01:12:41 +00001735 State.FreeRegs = DefaultNumRegisterParameters;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001736
Reid Kleckner677539d2014-07-10 01:58:55 +00001737 if (!getCXXABI().classifyReturnType(FI)) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00001738 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
Reid Kleckner677539d2014-07-10 01:58:55 +00001739 } else if (FI.getReturnInfo().isIndirect()) {
1740 // The C++ ABI is not aware of register usage, so we have to check if the
1741 // return value was sret and put it in a register ourselves if appropriate.
1742 if (State.FreeRegs) {
1743 --State.FreeRegs; // The sret parameter consumes a register.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001744 if (!IsMCUABI)
1745 FI.getReturnInfo().setInReg(true);
Reid Kleckner677539d2014-07-10 01:58:55 +00001746 }
1747 }
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001748
Peter Collingbournef7706832014-12-12 23:41:25 +00001749 // The chain argument effectively gives us another free register.
1750 if (FI.isChainCall())
1751 ++State.FreeRegs;
1752
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001753 bool UsedInAlloca = false;
Erich Keane521ed962017-01-05 00:20:51 +00001754 if (State.CC == llvm::CallingConv::X86_VectorCall) {
1755 computeVectorCallArgs(FI, State, UsedInAlloca);
1756 } else {
1757 // If not vectorcall, revert to normal behavior.
1758 for (auto &I : FI.arguments()) {
1759 I.info = classifyArgumentType(I.type, State);
1760 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1761 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001762 }
1763
1764 // If we needed to use inalloca for any argument, do a second pass and rewrite
1765 // all the memory arguments to use inalloca.
1766 if (UsedInAlloca)
1767 rewriteWithInAlloca(FI);
1768}
1769
1770void
1771X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001772 CharUnits &StackOffset, ABIArgInfo &Info,
1773 QualType Type) const {
1774 // Arguments are always 4-byte-aligned.
1775 CharUnits FieldAlign = CharUnits::fromQuantity(4);
1776
1777 assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
Reid Klecknerd378a712014-04-10 19:09:43 +00001778 Info = ABIArgInfo::getInAlloca(FrameFields.size());
1779 FrameFields.push_back(CGT.ConvertTypeForMem(Type));
John McCall7f416cc2015-09-08 08:05:57 +00001780 StackOffset += getContext().getTypeSizeInChars(Type);
Reid Klecknerd378a712014-04-10 19:09:43 +00001781
John McCall7f416cc2015-09-08 08:05:57 +00001782 // Insert padding bytes to respect alignment.
1783 CharUnits FieldEnd = StackOffset;
Rui Ueyama83aa9792016-01-14 21:00:27 +00001784 StackOffset = FieldEnd.alignTo(FieldAlign);
John McCall7f416cc2015-09-08 08:05:57 +00001785 if (StackOffset != FieldEnd) {
1786 CharUnits NumBytes = StackOffset - FieldEnd;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001787 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
John McCall7f416cc2015-09-08 08:05:57 +00001788 Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001789 FrameFields.push_back(Ty);
1790 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001791}
1792
Reid Kleckner852361d2014-07-26 00:12:26 +00001793static bool isArgInAlloca(const ABIArgInfo &Info) {
1794 // Leave ignored and inreg arguments alone.
1795 switch (Info.getKind()) {
1796 case ABIArgInfo::InAlloca:
1797 return true;
1798 case ABIArgInfo::Indirect:
1799 assert(Info.getIndirectByVal());
1800 return true;
1801 case ABIArgInfo::Ignore:
1802 return false;
1803 case ABIArgInfo::Direct:
1804 case ABIArgInfo::Extend:
Reid Kleckner852361d2014-07-26 00:12:26 +00001805 if (Info.getInReg())
1806 return false;
1807 return true;
Reid Kleckner04046052016-05-02 17:41:07 +00001808 case ABIArgInfo::Expand:
1809 case ABIArgInfo::CoerceAndExpand:
1810 // These are aggregate types which are never passed in registers when
1811 // inalloca is involved.
1812 return true;
Reid Kleckner852361d2014-07-26 00:12:26 +00001813 }
1814 llvm_unreachable("invalid enum");
1815}
1816
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001817void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1818 assert(IsWin32StructABI && "inalloca only supported on win32");
1819
1820 // Build a packed struct type for all of the arguments in memory.
1821 SmallVector<llvm::Type *, 6> FrameFields;
1822
John McCall7f416cc2015-09-08 08:05:57 +00001823 // The stack alignment is always 4.
1824 CharUnits StackAlign = CharUnits::fromQuantity(4);
1825
1826 CharUnits StackOffset;
Reid Kleckner852361d2014-07-26 00:12:26 +00001827 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1828
1829 // Put 'this' into the struct before 'sret', if necessary.
1830 bool IsThisCall =
1831 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1832 ABIArgInfo &Ret = FI.getReturnInfo();
1833 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1834 isArgInAlloca(I->info)) {
1835 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1836 ++I;
1837 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001838
1839 // Put the sret parameter into the inalloca struct if it's in memory.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001840 if (Ret.isIndirect() && !Ret.getInReg()) {
1841 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1842 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
Reid Klecknerfab1e892014-02-25 00:59:14 +00001843 // On Windows, the hidden sret parameter is always returned in eax.
1844 Ret.setInAllocaSRet(IsWin32StructABI);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001845 }
1846
1847 // Skip the 'this' parameter in ecx.
Reid Kleckner852361d2014-07-26 00:12:26 +00001848 if (IsThisCall)
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001849 ++I;
1850
1851 // Put arguments passed in memory into the struct.
1852 for (; I != E; ++I) {
Reid Kleckner852361d2014-07-26 00:12:26 +00001853 if (isArgInAlloca(I->info))
1854 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001855 }
1856
1857 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001858 /*isPacked=*/true),
1859 StackAlign);
Rafael Espindolaa6472962012-07-24 00:01:07 +00001860}
1861
John McCall7f416cc2015-09-08 08:05:57 +00001862Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
1863 Address VAListAddr, QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001864
John McCall7f416cc2015-09-08 08:05:57 +00001865 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001866
John McCall7f416cc2015-09-08 08:05:57 +00001867 // x86-32 changes the alignment of certain arguments on the stack.
1868 //
1869 // Just messing with TypeInfo like this works because we never pass
1870 // anything indirectly.
1871 TypeInfo.second = CharUnits::fromQuantity(
1872 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001873
John McCall7f416cc2015-09-08 08:05:57 +00001874 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
1875 TypeInfo, CharUnits::fromQuantity(4),
1876 /*AllowHigherAlign*/ true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001877}
1878
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001879bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1880 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1881 assert(Triple.getArch() == llvm::Triple::x86);
1882
1883 switch (Opts.getStructReturnConvention()) {
1884 case CodeGenOptions::SRCK_Default:
1885 break;
1886 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
1887 return false;
1888 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
1889 return true;
1890 }
1891
Michael Kupersteind749f232015-10-27 07:46:22 +00001892 if (Triple.isOSDarwin() || Triple.isOSIAMCU())
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001893 return true;
1894
1895 switch (Triple.getOS()) {
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001896 case llvm::Triple::DragonFly:
1897 case llvm::Triple::FreeBSD:
1898 case llvm::Triple::OpenBSD:
1899 case llvm::Triple::Bitrig:
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001900 case llvm::Triple::Win32:
Reid Kleckner2918fef2014-11-24 22:05:42 +00001901 return true;
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001902 default:
1903 return false;
1904 }
1905}
1906
Eric Christopher162c91c2015-06-05 22:03:00 +00001907void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Charles Davis4ea31ab2010-02-13 15:54:06 +00001908 llvm::GlobalValue *GV,
1909 CodeGen::CodeGenModule &CGM) const {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00001910 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Charles Davis4ea31ab2010-02-13 15:54:06 +00001911 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1912 // Get the LLVM function.
1913 llvm::Function *Fn = cast<llvm::Function>(GV);
1914
1915 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001916 llvm::AttrBuilder B;
Bill Wendlingccf94c92012-10-14 03:28:14 +00001917 B.addStackAlignmentAttr(16);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001918 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
Charles Davis4ea31ab2010-02-13 15:54:06 +00001919 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00001920 if (FD->hasAttr<AnyX86InterruptAttr>()) {
1921 llvm::Function *Fn = cast<llvm::Function>(GV);
1922 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
1923 }
Charles Davis4ea31ab2010-02-13 15:54:06 +00001924 }
1925}
1926
John McCallbeec5a02010-03-06 00:35:14 +00001927bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1928 CodeGen::CodeGenFunction &CGF,
1929 llvm::Value *Address) const {
1930 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallbeec5a02010-03-06 00:35:14 +00001931
Chris Lattnerece04092012-02-07 00:39:47 +00001932 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001933
John McCallbeec5a02010-03-06 00:35:14 +00001934 // 0-7 are the eight integer registers; the order is different
1935 // on Darwin (for EH), but the range is the same.
1936 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +00001937 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +00001938
John McCallc8e01702013-04-16 22:48:15 +00001939 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCallbeec5a02010-03-06 00:35:14 +00001940 // 12-16 are st(0..4). Not sure why we stop at 4.
1941 // These have size 16, which is sizeof(long double) on
1942 // platforms with 8-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001943 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCall943fae92010-05-27 06:19:26 +00001944 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001945
John McCallbeec5a02010-03-06 00:35:14 +00001946 } else {
1947 // 9 is %eflags, which doesn't get a size on Darwin for some
1948 // reason.
John McCall7f416cc2015-09-08 08:05:57 +00001949 Builder.CreateAlignedStore(
1950 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
1951 CharUnits::One());
John McCallbeec5a02010-03-06 00:35:14 +00001952
1953 // 11-16 are st(0..5). Not sure why we stop at 5.
1954 // These have size 12, which is sizeof(long double) on
1955 // platforms with 4-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001956 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCall943fae92010-05-27 06:19:26 +00001957 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1958 }
John McCallbeec5a02010-03-06 00:35:14 +00001959
1960 return false;
1961}
1962
Chris Lattner0cf24192010-06-28 20:05:43 +00001963//===----------------------------------------------------------------------===//
1964// X86-64 ABI Implementation
1965//===----------------------------------------------------------------------===//
1966
1967
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001968namespace {
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001969/// The AVX ABI level for X86 targets.
1970enum class X86AVXABILevel {
1971 None,
Ahmed Bougacha0b938282015-06-22 21:31:43 +00001972 AVX,
1973 AVX512
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001974};
1975
1976/// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
1977static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
1978 switch (AVXLevel) {
Ahmed Bougacha0b938282015-06-22 21:31:43 +00001979 case X86AVXABILevel::AVX512:
1980 return 512;
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001981 case X86AVXABILevel::AVX:
1982 return 256;
1983 case X86AVXABILevel::None:
1984 return 128;
1985 }
Yaron Kerenb76cb042015-06-23 09:45:42 +00001986 llvm_unreachable("Unknown AVXLevel");
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001987}
1988
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001989/// X86_64ABIInfo - The X86_64 ABI information.
John McCall12f23522016-04-04 18:33:08 +00001990class X86_64ABIInfo : public SwiftABIInfo {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001991 enum Class {
1992 Integer = 0,
1993 SSE,
1994 SSEUp,
1995 X87,
1996 X87Up,
1997 ComplexX87,
1998 NoClass,
1999 Memory
2000 };
2001
2002 /// merge - Implement the X86_64 ABI merging algorithm.
2003 ///
2004 /// Merge an accumulating classification \arg Accum with a field
2005 /// classification \arg Field.
2006 ///
2007 /// \param Accum - The accumulating classification. This should
2008 /// always be either NoClass or the result of a previous merge
2009 /// call. In addition, this should never be Memory (the caller
2010 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +00002011 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002012
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002013 /// postMerge - Implement the X86_64 ABI post merging algorithm.
2014 ///
2015 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
2016 /// final MEMORY or SSE classes when necessary.
2017 ///
2018 /// \param AggregateSize - The size of the current aggregate in
2019 /// the classification process.
2020 ///
2021 /// \param Lo - The classification for the parts of the type
2022 /// residing in the low word of the containing object.
2023 ///
2024 /// \param Hi - The classification for the parts of the type
2025 /// residing in the higher words of the containing object.
2026 ///
2027 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
2028
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002029 /// classify - Determine the x86_64 register classes in which the
2030 /// given type T should be passed.
2031 ///
2032 /// \param Lo - The classification for the parts of the type
2033 /// residing in the low word of the containing object.
2034 ///
2035 /// \param Hi - The classification for the parts of the type
2036 /// residing in the high word of the containing object.
2037 ///
2038 /// \param OffsetBase - The bit offset of this type in the
2039 /// containing object. Some parameters are classified different
2040 /// depending on whether they straddle an eightbyte boundary.
2041 ///
Eli Friedman96fd2642013-06-12 00:13:45 +00002042 /// \param isNamedArg - Whether the argument in question is a "named"
2043 /// argument, as used in AMD64-ABI 3.5.7.
2044 ///
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002045 /// If a word is unused its result will be NoClass; if a type should
2046 /// be passed in Memory then at least the classification of \arg Lo
2047 /// will be Memory.
2048 ///
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00002049 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002050 ///
2051 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
2052 /// also be ComplexX87.
Eli Friedman96fd2642013-06-12 00:13:45 +00002053 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
2054 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002055
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002056 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +00002057 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
2058 unsigned IROffset, QualType SourceTy,
2059 unsigned SourceOffset) const;
2060 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
2061 unsigned IROffset, QualType SourceTy,
2062 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002063
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002064 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +00002065 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +00002066 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +00002067
2068 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002069 /// such that the argument will be passed in memory.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002070 ///
2071 /// \param freeIntRegs - The number of free integer registers remaining
2072 /// available.
2073 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002074
Chris Lattner458b2aa2010-07-29 02:16:43 +00002075 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002076
Erich Keane757d3172016-11-02 18:29:35 +00002077 ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
2078 unsigned &neededInt, unsigned &neededSSE,
Eli Friedman96fd2642013-06-12 00:13:45 +00002079 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002080
Erich Keane757d3172016-11-02 18:29:35 +00002081 ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
2082 unsigned &NeededSSE) const;
2083
2084 ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
2085 unsigned &NeededSSE) const;
2086
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002087 bool IsIllegalVectorType(QualType Ty) const;
2088
John McCalle0fda732011-04-21 01:20:55 +00002089 /// The 0.98 ABI revision clarified a lot of ambiguities,
2090 /// unfortunately in ways that were not always consistent with
2091 /// certain previous compilers. In particular, platforms which
2092 /// required strict binary compatibility with older versions of GCC
2093 /// may need to exempt themselves.
2094 bool honorsRevision0_98() const {
John McCallc8e01702013-04-16 22:48:15 +00002095 return !getTarget().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +00002096 }
2097
David Majnemere2ae2282016-03-04 05:26:16 +00002098 /// GCC classifies <1 x long long> as SSE but compatibility with older clang
2099 // compilers require us to classify it as INTEGER.
2100 bool classifyIntegerMMXAsSSE() const {
2101 const llvm::Triple &Triple = getTarget().getTriple();
2102 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
2103 return false;
2104 if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
2105 return false;
2106 return true;
2107 }
2108
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002109 X86AVXABILevel AVXLevel;
Derek Schuffc7dd7222012-10-11 15:52:22 +00002110 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
2111 // 64-bit hardware.
2112 bool Has64BitPointers;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002113
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002114public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002115 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
John McCall12f23522016-04-04 18:33:08 +00002116 SwiftABIInfo(CGT), AVXLevel(AVXLevel),
Derek Schuff8a872f32012-10-11 18:21:13 +00002117 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffc7dd7222012-10-11 15:52:22 +00002118 }
Chris Lattner22a931e2010-06-29 06:01:59 +00002119
John McCalla729c622012-02-17 03:33:10 +00002120 bool isPassedUsingAVXType(QualType type) const {
2121 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002122 // The freeIntRegs argument doesn't matter here.
Eli Friedman96fd2642013-06-12 00:13:45 +00002123 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
2124 /*isNamedArg*/true);
John McCalla729c622012-02-17 03:33:10 +00002125 if (info.isDirect()) {
2126 llvm::Type *ty = info.getCoerceToType();
2127 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
2128 return (vectorTy->getBitWidth() > 128);
2129 }
2130 return false;
2131 }
2132
Craig Topper4f12f102014-03-12 06:41:41 +00002133 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002134
John McCall7f416cc2015-09-08 08:05:57 +00002135 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2136 QualType Ty) const override;
Charles Davisc7d5c942015-09-17 20:55:33 +00002137 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
2138 QualType Ty) const override;
Peter Collingbourne69b004d2015-02-25 23:18:42 +00002139
2140 bool has64BitPointers() const {
2141 return Has64BitPointers;
2142 }
John McCall12f23522016-04-04 18:33:08 +00002143
2144 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
2145 ArrayRef<llvm::Type*> scalars,
2146 bool asReturnValue) const override {
2147 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2148 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00002149 bool isSwiftErrorInRegister() const override {
2150 return true;
2151 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002152};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002153
Chris Lattner04dc9572010-08-31 16:44:54 +00002154/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
Arnold Schwaighofer4fc955e2016-10-12 18:59:24 +00002155class WinX86_64ABIInfo : public SwiftABIInfo {
Chris Lattner04dc9572010-08-31 16:44:54 +00002156public:
Reid Kleckner11a17192015-10-28 22:29:52 +00002157 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT)
Arnold Schwaighofer4fc955e2016-10-12 18:59:24 +00002158 : SwiftABIInfo(CGT),
Reid Kleckner11a17192015-10-28 22:29:52 +00002159 IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002160
Craig Topper4f12f102014-03-12 06:41:41 +00002161 void computeInfo(CGFunctionInfo &FI) const override;
Chris Lattner04dc9572010-08-31 16:44:54 +00002162
John McCall7f416cc2015-09-08 08:05:57 +00002163 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2164 QualType Ty) const override;
Reid Kleckner80944df2014-10-31 22:00:51 +00002165
2166 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2167 // FIXME: Assumes vectorcall is in use.
2168 return isX86VectorTypeForVectorCall(getContext(), Ty);
2169 }
2170
2171 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2172 uint64_t NumMembers) const override {
2173 // FIXME: Assumes vectorcall is in use.
2174 return isX86VectorCallAggregateSmallEnough(NumMembers);
2175 }
Reid Kleckner11a17192015-10-28 22:29:52 +00002176
Arnold Schwaighofer4fc955e2016-10-12 18:59:24 +00002177 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
2178 ArrayRef<llvm::Type *> scalars,
2179 bool asReturnValue) const override {
2180 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2181 }
2182
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00002183 bool isSwiftErrorInRegister() const override {
2184 return true;
2185 }
2186
Reid Kleckner11a17192015-10-28 22:29:52 +00002187private:
Erich Keane521ed962017-01-05 00:20:51 +00002188 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
2189 bool IsVectorCall, bool IsRegCall) const;
2190 ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
2191 const ABIArgInfo &current) const;
2192 void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs,
2193 bool IsVectorCall, bool IsRegCall) const;
Reid Kleckner11a17192015-10-28 22:29:52 +00002194
Erich Keane521ed962017-01-05 00:20:51 +00002195 bool IsMingw64;
Chris Lattner04dc9572010-08-31 16:44:54 +00002196};
2197
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002198class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2199public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002200 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
Alexey Bataev00396512015-07-02 03:40:19 +00002201 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002202
John McCalla729c622012-02-17 03:33:10 +00002203 const X86_64ABIInfo &getABIInfo() const {
2204 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2205 }
2206
Craig Topper4f12f102014-03-12 06:41:41 +00002207 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +00002208 return 7;
2209 }
2210
2211 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00002212 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00002213 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002214
John McCall943fae92010-05-27 06:19:26 +00002215 // 0-15 are the 16 integer registers.
2216 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00002217 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +00002218 return false;
2219 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00002220
Jay Foad7c57be32011-07-11 09:56:20 +00002221 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002222 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +00002223 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00002224 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2225 }
2226
John McCalla729c622012-02-17 03:33:10 +00002227 bool isNoProtoCallVariadic(const CallArgList &args,
Craig Topper4f12f102014-03-12 06:41:41 +00002228 const FunctionNoProtoType *fnType) const override {
John McCallcbc038a2011-09-21 08:08:30 +00002229 // The default CC on x86-64 sets %al to the number of SSA
2230 // registers used, and GCC sets this when calling an unprototyped
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002231 // function, so we override the default behavior. However, don't do
Eli Friedmanb8e45b22011-12-06 03:08:26 +00002232 // that when AVX types are involved: the ABI explicitly states it is
2233 // undefined, and it doesn't work in practice because of how the ABI
2234 // defines varargs anyway.
Reid Kleckner78af0702013-08-27 23:08:25 +00002235 if (fnType->getCallConv() == CC_C) {
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002236 bool HasAVXType = false;
John McCalla729c622012-02-17 03:33:10 +00002237 for (CallArgList::const_iterator
2238 it = args.begin(), ie = args.end(); it != ie; ++it) {
2239 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2240 HasAVXType = true;
2241 break;
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002242 }
2243 }
John McCalla729c622012-02-17 03:33:10 +00002244
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002245 if (!HasAVXType)
2246 return true;
2247 }
John McCallcbc038a2011-09-21 08:08:30 +00002248
John McCalla729c622012-02-17 03:33:10 +00002249 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCallcbc038a2011-09-21 08:08:30 +00002250 }
2251
Craig Topper4f12f102014-03-12 06:41:41 +00002252 llvm::Constant *
2253 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourne69b004d2015-02-25 23:18:42 +00002254 unsigned Sig;
2255 if (getABIInfo().has64BitPointers())
2256 Sig = (0xeb << 0) | // jmp rel8
2257 (0x0a << 8) | // .+0x0c
2258 ('F' << 16) |
2259 ('T' << 24);
2260 else
2261 Sig = (0xeb << 0) | // jmp rel8
2262 (0x06 << 8) | // .+0x08
2263 ('F' << 16) |
2264 ('T' << 24);
Peter Collingbourneb453cd62013-10-20 21:29:19 +00002265 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2266 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00002267
2268 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2269 CodeGen::CodeGenModule &CGM) const override {
2270 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2271 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2272 llvm::Function *Fn = cast<llvm::Function>(GV);
2273 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2274 }
2275 }
2276 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002277};
2278
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002279class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
2280public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002281 PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2282 : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002283
2284 void getDependentLibraryOption(llvm::StringRef Lib,
Alexander Kornienko34eb2072015-04-11 02:00:23 +00002285 llvm::SmallString<24> &Opt) const override {
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002286 Opt = "\01";
Yunzhong Gaod65200c2015-07-20 17:46:56 +00002287 // If the argument contains a space, enclose it in quotes.
2288 if (Lib.find(" ") != StringRef::npos)
2289 Opt += "\"" + Lib.str() + "\"";
2290 else
2291 Opt += Lib;
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002292 }
2293};
2294
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002295static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
Michael Kupersteinf0e4ccf2015-02-16 11:57:43 +00002296 // If the argument does not end in .lib, automatically add the suffix.
2297 // If the argument contains a space, enclose it in quotes.
2298 // This matches the behavior of MSVC.
2299 bool Quote = (Lib.find(" ") != StringRef::npos);
2300 std::string ArgStr = Quote ? "\"" : "";
2301 ArgStr += Lib;
Rui Ueyama727025a2013-10-31 19:12:53 +00002302 if (!Lib.endswith_lower(".lib"))
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002303 ArgStr += ".lib";
Michael Kupersteinf0e4ccf2015-02-16 11:57:43 +00002304 ArgStr += Quote ? "\"" : "";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002305 return ArgStr;
2306}
2307
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002308class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2309public:
John McCall1fe2a8c2013-06-18 02:46:29 +00002310 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Michael Kupersteindc745202015-10-19 07:52:25 +00002311 bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2312 unsigned NumRegisterParameters)
2313 : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00002314 Win32StructABI, NumRegisterParameters, false) {}
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002315
Eric Christopher162c91c2015-06-05 22:03:00 +00002316 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Hans Wennborg77dc2362015-01-20 19:45:50 +00002317 CodeGen::CodeGenModule &CGM) const override;
2318
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002319 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00002320 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002321 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002322 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002323 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00002324
2325 void getDetectMismatchOption(llvm::StringRef Name,
2326 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00002327 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00002328 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00002329 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002330};
2331
Hans Wennborg77dc2362015-01-20 19:45:50 +00002332static void addStackProbeSizeTargetAttribute(const Decl *D,
2333 llvm::GlobalValue *GV,
2334 CodeGen::CodeGenModule &CGM) {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00002335 if (D && isa<FunctionDecl>(D)) {
Hans Wennborg77dc2362015-01-20 19:45:50 +00002336 if (CGM.getCodeGenOpts().StackProbeSize != 4096) {
2337 llvm::Function *Fn = cast<llvm::Function>(GV);
2338
Eric Christopher7565e0d2015-05-29 23:09:49 +00002339 Fn->addFnAttr("stack-probe-size",
2340 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
Hans Wennborg77dc2362015-01-20 19:45:50 +00002341 }
2342 }
2343}
2344
Eric Christopher162c91c2015-06-05 22:03:00 +00002345void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Hans Wennborg77dc2362015-01-20 19:45:50 +00002346 llvm::GlobalValue *GV,
2347 CodeGen::CodeGenModule &CGM) const {
Eric Christopher162c91c2015-06-05 22:03:00 +00002348 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Hans Wennborg77dc2362015-01-20 19:45:50 +00002349
2350 addStackProbeSizeTargetAttribute(D, GV, CGM);
2351}
2352
Chris Lattner04dc9572010-08-31 16:44:54 +00002353class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2354public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002355 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2356 X86AVXABILevel AVXLevel)
Alexey Bataev00396512015-07-02 03:40:19 +00002357 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
Chris Lattner04dc9572010-08-31 16:44:54 +00002358
Eric Christopher162c91c2015-06-05 22:03:00 +00002359 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Hans Wennborg77dc2362015-01-20 19:45:50 +00002360 CodeGen::CodeGenModule &CGM) const override;
2361
Craig Topper4f12f102014-03-12 06:41:41 +00002362 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
Chris Lattner04dc9572010-08-31 16:44:54 +00002363 return 7;
2364 }
2365
2366 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00002367 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00002368 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002369
Chris Lattner04dc9572010-08-31 16:44:54 +00002370 // 0-15 are the 16 integer registers.
2371 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00002372 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattner04dc9572010-08-31 16:44:54 +00002373 return false;
2374 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002375
2376 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00002377 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002378 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002379 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002380 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00002381
2382 void getDetectMismatchOption(llvm::StringRef Name,
2383 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00002384 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00002385 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00002386 }
Chris Lattner04dc9572010-08-31 16:44:54 +00002387};
2388
Eric Christopher162c91c2015-06-05 22:03:00 +00002389void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Hans Wennborg77dc2362015-01-20 19:45:50 +00002390 llvm::GlobalValue *GV,
2391 CodeGen::CodeGenModule &CGM) const {
Eric Christopher162c91c2015-06-05 22:03:00 +00002392 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Hans Wennborg77dc2362015-01-20 19:45:50 +00002393
Alexey Bataevd51e9932016-01-15 04:06:31 +00002394 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2395 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2396 llvm::Function *Fn = cast<llvm::Function>(GV);
2397 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2398 }
2399 }
2400
Hans Wennborg77dc2362015-01-20 19:45:50 +00002401 addStackProbeSizeTargetAttribute(D, GV, CGM);
2402}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002403}
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002404
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002405void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2406 Class &Hi) const {
2407 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2408 //
2409 // (a) If one of the classes is Memory, the whole argument is passed in
2410 // memory.
2411 //
2412 // (b) If X87UP is not preceded by X87, the whole argument is passed in
2413 // memory.
2414 //
2415 // (c) If the size of the aggregate exceeds two eightbytes and the first
2416 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2417 // argument is passed in memory. NOTE: This is necessary to keep the
2418 // ABI working for processors that don't support the __m256 type.
2419 //
2420 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2421 //
2422 // Some of these are enforced by the merging logic. Others can arise
2423 // only with unions; for example:
2424 // union { _Complex double; unsigned; }
2425 //
2426 // Note that clauses (b) and (c) were added in 0.98.
2427 //
2428 if (Hi == Memory)
2429 Lo = Memory;
2430 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2431 Lo = Memory;
2432 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2433 Lo = Memory;
2434 if (Hi == SSEUp && Lo != SSE)
2435 Hi = SSE;
2436}
2437
Chris Lattnerd776fb12010-06-28 21:43:59 +00002438X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002439 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2440 // classified recursively so that always two fields are
2441 // considered. The resulting class is calculated according to
2442 // the classes of the fields in the eightbyte:
2443 //
2444 // (a) If both classes are equal, this is the resulting class.
2445 //
2446 // (b) If one of the classes is NO_CLASS, the resulting class is
2447 // the other class.
2448 //
2449 // (c) If one of the classes is MEMORY, the result is the MEMORY
2450 // class.
2451 //
2452 // (d) If one of the classes is INTEGER, the result is the
2453 // INTEGER.
2454 //
2455 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2456 // MEMORY is used as class.
2457 //
2458 // (f) Otherwise class SSE is used.
2459
2460 // Accum should never be memory (we should have returned) or
2461 // ComplexX87 (because this cannot be passed in a structure).
2462 assert((Accum != Memory && Accum != ComplexX87) &&
2463 "Invalid accumulated classification during merge.");
2464 if (Accum == Field || Field == NoClass)
2465 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002466 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002467 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002468 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002469 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002470 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002471 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002472 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2473 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002474 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002475 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002476}
2477
Chris Lattner5c740f12010-06-30 19:14:05 +00002478void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman96fd2642013-06-12 00:13:45 +00002479 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002480 // FIXME: This code can be simplified by introducing a simple value class for
2481 // Class pairs with appropriate constructor methods for the various
2482 // situations.
2483
2484 // FIXME: Some of the split computations are wrong; unaligned vectors
2485 // shouldn't be passed in registers for example, so there is no chance they
2486 // can straddle an eightbyte. Verify & simplify.
2487
2488 Lo = Hi = NoClass;
2489
2490 Class &Current = OffsetBase < 64 ? Lo : Hi;
2491 Current = Memory;
2492
John McCall9dd450b2009-09-21 23:43:11 +00002493 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002494 BuiltinType::Kind k = BT->getKind();
2495
2496 if (k == BuiltinType::Void) {
2497 Current = NoClass;
2498 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2499 Lo = Integer;
2500 Hi = Integer;
2501 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2502 Current = Integer;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002503 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002504 Current = SSE;
2505 } else if (k == BuiltinType::LongDouble) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002506 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002507 if (LDF == &llvm::APFloat::IEEEquad()) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002508 Lo = SSE;
2509 Hi = SSEUp;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002510 } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002511 Lo = X87;
2512 Hi = X87Up;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002513 } else if (LDF == &llvm::APFloat::IEEEdouble()) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002514 Current = SSE;
2515 } else
2516 llvm_unreachable("unexpected long double representation!");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002517 }
2518 // FIXME: _Decimal32 and _Decimal64 are SSE.
2519 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00002520 return;
2521 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002522
Chris Lattnerd776fb12010-06-28 21:43:59 +00002523 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002524 // Classify the underlying integer type.
Eli Friedman96fd2642013-06-12 00:13:45 +00002525 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002526 return;
2527 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002528
Chris Lattnerd776fb12010-06-28 21:43:59 +00002529 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002530 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002531 return;
2532 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002533
Chris Lattnerd776fb12010-06-28 21:43:59 +00002534 if (Ty->isMemberPointerType()) {
Jan Wen Voung01c21e82014-10-02 16:56:57 +00002535 if (Ty->isMemberFunctionPointerType()) {
2536 if (Has64BitPointers) {
2537 // If Has64BitPointers, this is an {i64, i64}, so classify both
2538 // Lo and Hi now.
2539 Lo = Hi = Integer;
2540 } else {
2541 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2542 // straddles an eightbyte boundary, Hi should be classified as well.
2543 uint64_t EB_FuncPtr = (OffsetBase) / 64;
2544 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2545 if (EB_FuncPtr != EB_ThisAdj) {
2546 Lo = Hi = Integer;
2547 } else {
2548 Current = Integer;
2549 }
2550 }
2551 } else {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00002552 Current = Integer;
Jan Wen Voung01c21e82014-10-02 16:56:57 +00002553 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00002554 return;
2555 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002556
Chris Lattnerd776fb12010-06-28 21:43:59 +00002557 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002558 uint64_t Size = getContext().getTypeSize(VT);
David Majnemerf8d14db2015-07-17 05:49:13 +00002559 if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2560 // gcc passes the following as integer:
2561 // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2562 // 2 bytes - <2 x char>, <1 x short>
2563 // 1 byte - <1 x char>
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002564 Current = Integer;
2565
2566 // If this type crosses an eightbyte boundary, it should be
2567 // split.
David Majnemerf8d14db2015-07-17 05:49:13 +00002568 uint64_t EB_Lo = (OffsetBase) / 64;
2569 uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2570 if (EB_Lo != EB_Hi)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002571 Hi = Lo;
2572 } else if (Size == 64) {
David Majnemere2ae2282016-03-04 05:26:16 +00002573 QualType ElementType = VT->getElementType();
2574
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002575 // gcc passes <1 x double> in memory. :(
David Majnemere2ae2282016-03-04 05:26:16 +00002576 if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002577 return;
2578
David Majnemere2ae2282016-03-04 05:26:16 +00002579 // gcc passes <1 x long long> as SSE but clang used to unconditionally
2580 // pass them as integer. For platforms where clang is the de facto
2581 // platform compiler, we must continue to use integer.
2582 if (!classifyIntegerMMXAsSSE() &&
2583 (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2584 ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2585 ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2586 ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002587 Current = Integer;
2588 else
2589 Current = SSE;
2590
2591 // If this type crosses an eightbyte boundary, it should be
2592 // split.
2593 if (OffsetBase && OffsetBase != 64)
2594 Hi = Lo;
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002595 } else if (Size == 128 ||
2596 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002597 // Arguments of 256-bits are split into four eightbyte chunks. The
2598 // least significant one belongs to class SSE and all the others to class
2599 // SSEUP. The original Lo and Hi design considers that types can't be
2600 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2601 // This design isn't correct for 256-bits, but since there're no cases
2602 // where the upper parts would need to be inspected, avoid adding
2603 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman96fd2642013-06-12 00:13:45 +00002604 //
2605 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2606 // registers if they are "named", i.e. not part of the "..." of a
2607 // variadic function.
Ahmed Bougacha0b938282015-06-22 21:31:43 +00002608 //
2609 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2610 // split into eight eightbyte chunks, one SSE and seven SSEUP.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002611 Lo = SSE;
2612 Hi = SSEUp;
2613 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00002614 return;
2615 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002616
Chris Lattnerd776fb12010-06-28 21:43:59 +00002617 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002618 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002619
Chris Lattner2b037972010-07-29 02:01:43 +00002620 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00002621 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002622 if (Size <= 64)
2623 Current = Integer;
2624 else if (Size <= 128)
2625 Lo = Hi = Integer;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002626 } else if (ET == getContext().FloatTy) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002627 Current = SSE;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002628 } else if (ET == getContext().DoubleTy) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002629 Lo = Hi = SSE;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002630 } else if (ET == getContext().LongDoubleTy) {
2631 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002632 if (LDF == &llvm::APFloat::IEEEquad())
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002633 Current = Memory;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002634 else if (LDF == &llvm::APFloat::x87DoubleExtended())
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002635 Current = ComplexX87;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002636 else if (LDF == &llvm::APFloat::IEEEdouble())
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002637 Lo = Hi = SSE;
2638 else
2639 llvm_unreachable("unexpected long double representation!");
2640 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002641
2642 // If this complex type crosses an eightbyte boundary then it
2643 // should be split.
2644 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00002645 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002646 if (Hi == NoClass && EB_Real != EB_Imag)
2647 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002648
Chris Lattnerd776fb12010-06-28 21:43:59 +00002649 return;
2650 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002651
Chris Lattner2b037972010-07-29 02:01:43 +00002652 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002653 // Arrays are treated like structures.
2654
Chris Lattner2b037972010-07-29 02:01:43 +00002655 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002656
2657 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
David Majnemerb229cb02016-08-15 06:39:18 +00002658 // than eight eightbytes, ..., it has class MEMORY.
2659 if (Size > 512)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002660 return;
2661
2662 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2663 // fields, it has class MEMORY.
2664 //
2665 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00002666 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002667 return;
2668
2669 // Otherwise implement simplified merge. We could be smarter about
2670 // this, but it isn't worth it and would be harder to verify.
2671 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00002672 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002673 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00002674
2675 // The only case a 256-bit wide vector could be used is when the array
2676 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2677 // to work for sizes wider than 128, early check and fallback to memory.
David Majnemerb229cb02016-08-15 06:39:18 +00002678 //
2679 if (Size > 128 &&
2680 (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00002681 return;
2682
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002683 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2684 Class FieldLo, FieldHi;
Eli Friedman96fd2642013-06-12 00:13:45 +00002685 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002686 Lo = merge(Lo, FieldLo);
2687 Hi = merge(Hi, FieldHi);
2688 if (Lo == Memory || Hi == Memory)
2689 break;
2690 }
2691
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002692 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002693 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002694 return;
2695 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002696
Chris Lattnerd776fb12010-06-28 21:43:59 +00002697 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002698 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002699
2700 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
David Majnemerb229cb02016-08-15 06:39:18 +00002701 // than eight eightbytes, ..., it has class MEMORY.
2702 if (Size > 512)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002703 return;
2704
Anders Carlsson20759ad2009-09-16 15:53:40 +00002705 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2706 // copy constructor or a non-trivial destructor, it is passed by invisible
2707 // reference.
Mark Lacey3825e832013-10-06 01:33:34 +00002708 if (getRecordArgABI(RT, getCXXABI()))
Anders Carlsson20759ad2009-09-16 15:53:40 +00002709 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002710
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002711 const RecordDecl *RD = RT->getDecl();
2712
2713 // Assume variable sized types are passed in memory.
2714 if (RD->hasFlexibleArrayMember())
2715 return;
2716
Chris Lattner2b037972010-07-29 02:01:43 +00002717 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002718
2719 // Reset Lo class, this will be recomputed.
2720 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002721
2722 // If this is a C++ record, classify the bases first.
2723 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00002724 for (const auto &I : CXXRD->bases()) {
2725 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002726 "Unexpected base class!");
2727 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00002728 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002729
2730 // Classify this field.
2731 //
2732 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2733 // single eightbyte, each is classified separately. Each eightbyte gets
2734 // initialized to class NO_CLASS.
2735 Class FieldLo, FieldHi;
Benjamin Kramer2ef30312012-07-04 18:45:14 +00002736 uint64_t Offset =
2737 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Aaron Ballman574705e2014-03-13 15:41:46 +00002738 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002739 Lo = merge(Lo, FieldLo);
2740 Hi = merge(Hi, FieldHi);
David Majnemercefbc7c2015-07-08 05:14:29 +00002741 if (Lo == Memory || Hi == Memory) {
2742 postMerge(Size, Lo, Hi);
2743 return;
2744 }
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002745 }
2746 }
2747
2748 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002749 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00002750 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002751 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002752 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2753 bool BitField = i->isBitField();
2754
David Majnemerb439dfe2016-08-15 07:20:40 +00002755 // Ignore padding bit-fields.
2756 if (BitField && i->isUnnamedBitfield())
2757 continue;
2758
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002759 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2760 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002761 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002762 // The only case a 256-bit wide vector could be used is when the struct
2763 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2764 // to work for sizes wider than 128, early check and fallback to memory.
2765 //
David Majnemerb229cb02016-08-15 06:39:18 +00002766 if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) ||
2767 Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002768 Lo = Memory;
David Majnemer699dd042015-07-08 05:07:05 +00002769 postMerge(Size, Lo, Hi);
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002770 return;
2771 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002772 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00002773 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002774 Lo = Memory;
David Majnemer699dd042015-07-08 05:07:05 +00002775 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002776 return;
2777 }
2778
2779 // Classify this field.
2780 //
2781 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2782 // exceeds a single eightbyte, each is classified
2783 // separately. Each eightbyte gets initialized to class
2784 // NO_CLASS.
2785 Class FieldLo, FieldHi;
2786
2787 // Bit-fields require special handling, they do not force the
2788 // structure to be passed in memory even if unaligned, and
2789 // therefore they can straddle an eightbyte.
2790 if (BitField) {
David Majnemerb439dfe2016-08-15 07:20:40 +00002791 assert(!i->isUnnamedBitfield());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002792 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00002793 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002794
2795 uint64_t EB_Lo = Offset / 64;
2796 uint64_t EB_Hi = (Offset + Size - 1) / 64;
Sylvestre Ledru0c4813e2013-10-06 09:54:18 +00002797
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002798 if (EB_Lo) {
2799 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2800 FieldLo = NoClass;
2801 FieldHi = Integer;
2802 } else {
2803 FieldLo = Integer;
2804 FieldHi = EB_Hi ? Integer : NoClass;
2805 }
2806 } else
Eli Friedman96fd2642013-06-12 00:13:45 +00002807 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002808 Lo = merge(Lo, FieldLo);
2809 Hi = merge(Hi, FieldHi);
2810 if (Lo == Memory || Hi == Memory)
2811 break;
2812 }
2813
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002814 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002815 }
2816}
2817
Chris Lattner22a931e2010-06-29 06:01:59 +00002818ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002819 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2820 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00002821 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002822 // Treat an enum type as its underlying type.
2823 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2824 Ty = EnumTy->getDecl()->getIntegerType();
2825
2826 return (Ty->isPromotableIntegerType() ?
2827 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2828 }
2829
John McCall7f416cc2015-09-08 08:05:57 +00002830 return getNaturalAlignIndirect(Ty);
Daniel Dunbar53fac692010-04-21 19:49:55 +00002831}
2832
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002833bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2834 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2835 uint64_t Size = getContext().getTypeSize(VecTy);
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002836 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002837 if (Size <= 64 || Size > LargestVector)
2838 return true;
2839 }
2840
2841 return false;
2842}
2843
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002844ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2845 unsigned freeIntRegs) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002846 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2847 // place naturally.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002848 //
2849 // This assumption is optimistic, as there could be free registers available
2850 // when we need to pass this argument in memory, and LLVM could try to pass
2851 // the argument in the free register. This does not seem to happen currently,
2852 // but this code would be much safer if we could mark the argument with
2853 // 'onstack'. See PR12193.
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002854 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002855 // Treat an enum type as its underlying type.
2856 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2857 Ty = EnumTy->getDecl()->getIntegerType();
2858
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002859 return (Ty->isPromotableIntegerType() ?
2860 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002861 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002862
Mark Lacey3825e832013-10-06 01:33:34 +00002863 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00002864 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson20759ad2009-09-16 15:53:40 +00002865
Chris Lattner44c2b902011-05-22 23:21:23 +00002866 // Compute the byval alignment. We specify the alignment of the byval in all
2867 // cases so that the mid-level optimizer knows the alignment of the byval.
2868 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002869
2870 // Attempt to avoid passing indirect results using byval when possible. This
2871 // is important for good codegen.
2872 //
2873 // We do this by coercing the value into a scalar type which the backend can
2874 // handle naturally (i.e., without using byval).
2875 //
2876 // For simplicity, we currently only do this when we have exhausted all of the
2877 // free integer registers. Doing this when there are free integer registers
2878 // would require more care, as we would have to ensure that the coerced value
2879 // did not claim the unused register. That would require either reording the
2880 // arguments to the function (so that any subsequent inreg values came first),
2881 // or only doing this optimization when there were no following arguments that
2882 // might be inreg.
2883 //
2884 // We currently expect it to be rare (particularly in well written code) for
2885 // arguments to be passed on the stack when there are still free integer
2886 // registers available (this would typically imply large structs being passed
2887 // by value), so this seems like a fair tradeoff for now.
2888 //
2889 // We can revisit this if the backend grows support for 'onstack' parameter
2890 // attributes. See PR12193.
2891 if (freeIntRegs == 0) {
2892 uint64_t Size = getContext().getTypeSize(Ty);
2893
2894 // If this type fits in an eightbyte, coerce it into the matching integral
2895 // type, which will end up on the stack (with alignment 8).
2896 if (Align == 8 && Size <= 64)
2897 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2898 Size));
2899 }
2900
John McCall7f416cc2015-09-08 08:05:57 +00002901 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002902}
2903
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002904/// The ABI specifies that a value should be passed in a full vector XMM/YMM
2905/// register. Pick an LLVM IR type that will be passed as a vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002906llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002907 // Wrapper structs/arrays that only contain vectors are passed just like
2908 // vectors; strip them off if present.
2909 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
2910 Ty = QualType(InnerTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002911
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002912 llvm::Type *IRType = CGT.ConvertType(Ty);
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002913 if (isa<llvm::VectorType>(IRType) ||
2914 IRType->getTypeID() == llvm::Type::FP128TyID)
Andrea Di Biagioe7347c62015-06-02 19:34:40 +00002915 return IRType;
2916
2917 // We couldn't find the preferred IR vector type for 'Ty'.
2918 uint64_t Size = getContext().getTypeSize(Ty);
David Majnemerb229cb02016-08-15 06:39:18 +00002919 assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
Andrea Di Biagioe7347c62015-06-02 19:34:40 +00002920
2921 // Return a LLVM IR vector type based on the size of 'Ty'.
2922 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
2923 Size / 64);
Chris Lattner4200fe42010-07-29 04:56:46 +00002924}
2925
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002926/// BitsContainNoUserData - Return true if the specified [start,end) bit range
2927/// is known to either be off the end of the specified type or being in
2928/// alignment padding. The user type specified is known to be at most 128 bits
2929/// in size, and have passed through X86_64ABIInfo::classify with a successful
2930/// classification that put one of the two halves in the INTEGER class.
2931///
2932/// It is conservatively correct to return false.
2933static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2934 unsigned EndBit, ASTContext &Context) {
2935 // If the bytes being queried are off the end of the type, there is no user
2936 // data hiding here. This handles analysis of builtins, vectors and other
2937 // types that don't contain interesting padding.
2938 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2939 if (TySize <= StartBit)
2940 return true;
2941
Chris Lattner98076a22010-07-29 07:43:55 +00002942 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2943 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2944 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2945
2946 // Check each element to see if the element overlaps with the queried range.
2947 for (unsigned i = 0; i != NumElts; ++i) {
2948 // If the element is after the span we care about, then we're done..
2949 unsigned EltOffset = i*EltSize;
2950 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002951
Chris Lattner98076a22010-07-29 07:43:55 +00002952 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2953 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
2954 EndBit-EltOffset, Context))
2955 return false;
2956 }
2957 // If it overlaps no elements, then it is safe to process as padding.
2958 return true;
2959 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002960
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002961 if (const RecordType *RT = Ty->getAs<RecordType>()) {
2962 const RecordDecl *RD = RT->getDecl();
2963 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002964
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002965 // If this is a C++ record, check the bases first.
2966 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00002967 for (const auto &I : CXXRD->bases()) {
2968 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002969 "Unexpected base class!");
2970 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00002971 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002972
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002973 // If the base is after the span we care about, ignore it.
Benjamin Kramer2ef30312012-07-04 18:45:14 +00002974 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002975 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002976
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002977 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
Aaron Ballman574705e2014-03-13 15:41:46 +00002978 if (!BitsContainNoUserData(I.getType(), BaseStart,
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002979 EndBit-BaseOffset, Context))
2980 return false;
2981 }
2982 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002983
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002984 // Verify that no field has data that overlaps the region of interest. Yes
2985 // this could be sped up a lot by being smarter about queried fields,
2986 // however we're only looking at structs up to 16 bytes, so we don't care
2987 // much.
2988 unsigned idx = 0;
2989 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2990 i != e; ++i, ++idx) {
2991 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002992
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002993 // If we found a field after the region we care about, then we're done.
2994 if (FieldOffset >= EndBit) break;
2995
2996 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
2997 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
2998 Context))
2999 return false;
3000 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003001
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003002 // If nothing in this record overlapped the area of interest, then we're
3003 // clean.
3004 return true;
3005 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003006
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003007 return false;
3008}
3009
Chris Lattnere556a712010-07-29 18:39:32 +00003010/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
3011/// float member at the specified offset. For example, {int,{float}} has a
3012/// float at offset 4. It is conservatively correct for this routine to return
3013/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00003014static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmowdd31ca12012-10-08 16:25:52 +00003015 const llvm::DataLayout &TD) {
Chris Lattnere556a712010-07-29 18:39:32 +00003016 // Base case if we find a float.
3017 if (IROffset == 0 && IRType->isFloatTy())
3018 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003019
Chris Lattnere556a712010-07-29 18:39:32 +00003020 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00003021 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00003022 const llvm::StructLayout *SL = TD.getStructLayout(STy);
3023 unsigned Elt = SL->getElementContainingOffset(IROffset);
3024 IROffset -= SL->getElementOffset(Elt);
3025 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
3026 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003027
Chris Lattnere556a712010-07-29 18:39:32 +00003028 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00003029 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3030 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00003031 unsigned EltSize = TD.getTypeAllocSize(EltTy);
3032 IROffset -= IROffset/EltSize*EltSize;
3033 return ContainsFloatAtOffset(EltTy, IROffset, TD);
3034 }
3035
3036 return false;
3037}
3038
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003039
3040/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3041/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003042llvm::Type *X86_64ABIInfo::
3043GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003044 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00003045 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003046 // pass as float if the last 4 bytes is just padding. This happens for
3047 // structs that contain 3 floats.
3048 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
3049 SourceOffset*8+64, getContext()))
3050 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003051
Chris Lattnere556a712010-07-29 18:39:32 +00003052 // We want to pass as <2 x float> if the LLVM IR type contains a float at
3053 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
3054 // case.
Micah Villmowdd31ca12012-10-08 16:25:52 +00003055 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
3056 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner9f8b4512010-08-25 23:39:14 +00003057 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003058
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003059 return llvm::Type::getDoubleTy(getVMContext());
3060}
3061
3062
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003063/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3064/// an 8-byte GPR. This means that we either have a scalar or we are talking
3065/// about the high or low part of an up-to-16-byte struct. This routine picks
3066/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003067/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3068/// etc).
3069///
3070/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3071/// the source type. IROffset is an offset in bytes into the LLVM IR type that
3072/// the 8-byte value references. PrefType may be null.
3073///
Alp Toker9907f082014-07-09 14:06:35 +00003074/// SourceTy is the source-level type for the entire argument. SourceOffset is
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003075/// an offset into this that we're processing (which is always either 0 or 8).
3076///
Chris Lattnera5f58b02011-07-09 17:41:47 +00003077llvm::Type *X86_64ABIInfo::
3078GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003079 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003080 // If we're dealing with an un-offset LLVM IR type, then it means that we're
3081 // returning an 8-byte unit starting with it. See if we can safely use it.
3082 if (IROffset == 0) {
3083 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffc7dd7222012-10-11 15:52:22 +00003084 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3085 IRType->isIntegerTy(64))
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003086 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003087
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003088 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3089 // goodness in the source type is just tail padding. This is allowed to
3090 // kick in for struct {double,int} on the int, but not on
3091 // struct{double,int,int} because we wouldn't return the second int. We
3092 // have to do this analysis on the source type because we can't depend on
3093 // unions being lowered a specific way etc.
3094 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffc7dd7222012-10-11 15:52:22 +00003095 IRType->isIntegerTy(32) ||
3096 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3097 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3098 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003099
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003100 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3101 SourceOffset*8+64, getContext()))
3102 return IRType;
3103 }
3104 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003105
Chris Lattner2192fe52011-07-18 04:24:23 +00003106 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003107 // If this is a struct, recurse into the field at the specified offset.
Micah Villmowdd31ca12012-10-08 16:25:52 +00003108 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003109 if (IROffset < SL->getSizeInBytes()) {
3110 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3111 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003112
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003113 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3114 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003115 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003116 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003117
Chris Lattner2192fe52011-07-18 04:24:23 +00003118 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003119 llvm::Type *EltTy = ATy->getElementType();
Micah Villmowdd31ca12012-10-08 16:25:52 +00003120 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner98076a22010-07-29 07:43:55 +00003121 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003122 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3123 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00003124 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003125
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003126 // Okay, we don't have any better idea of what to pass, so we pass this in an
3127 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00003128 unsigned TySizeInBytes =
3129 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003130
Chris Lattner3f763422010-07-29 17:34:39 +00003131 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003132
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003133 // It is always safe to classify this as an integer type up to i64 that
3134 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00003135 return llvm::IntegerType::get(getVMContext(),
3136 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00003137}
3138
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003139
3140/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3141/// be used as elements of a two register pair to pass or return, return a
3142/// first class aggregate to represent them. For example, if the low part of
3143/// a by-value argument should be passed as i32* and the high part as float,
3144/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003145static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00003146GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmowdd31ca12012-10-08 16:25:52 +00003147 const llvm::DataLayout &TD) {
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003148 // In order to correctly satisfy the ABI, we need to the high part to start
3149 // at offset 8. If the high and low parts we inferred are both 4-byte types
3150 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3151 // the second element at offset 8. Check for this:
3152 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3153 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003154 unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003155 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003156
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003157 // To handle this, we have to increase the size of the low part so that the
3158 // second element will start at an 8 byte offset. We can't increase the size
3159 // of the second element because it might make us access off the end of the
3160 // struct.
3161 if (HiStart != 8) {
Derek Schuff5ec51282015-06-24 22:36:38 +00003162 // There are usually two sorts of types the ABI generation code can produce
3163 // for the low part of a pair that aren't 8 bytes in size: float or
3164 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
3165 // NaCl).
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003166 // Promote these to a larger type.
3167 if (Lo->isFloatTy())
3168 Lo = llvm::Type::getDoubleTy(Lo->getContext());
3169 else {
Derek Schuff3c6a48d2015-06-24 22:36:36 +00003170 assert((Lo->isIntegerTy() || Lo->isPointerTy())
3171 && "Invalid/unknown lo type");
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003172 Lo = llvm::Type::getInt64Ty(Lo->getContext());
3173 }
3174 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003175
Serge Guelton1d993272017-05-09 19:31:30 +00003176 llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003177
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003178 // Verify that the second element is at an 8-byte offset.
3179 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3180 "Invalid x86-64 argument pair!");
3181 return Result;
3182}
3183
Chris Lattner31faff52010-07-28 23:06:14 +00003184ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00003185classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00003186 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3187 // classification algorithm.
3188 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00003189 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner31faff52010-07-28 23:06:14 +00003190
3191 // Check some invariants.
3192 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00003193 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3194
Craig Topper8a13c412014-05-21 05:09:00 +00003195 llvm::Type *ResType = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00003196 switch (Lo) {
3197 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003198 if (Hi == NoClass)
3199 return ABIArgInfo::getIgnore();
3200 // If the low part is just padding, it takes no register, leave ResType
3201 // null.
3202 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3203 "Unknown missing lo part");
3204 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003205
3206 case SSEUp:
3207 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00003208 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00003209
3210 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3211 // hidden argument.
3212 case Memory:
3213 return getIndirectReturnResult(RetTy);
3214
3215 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3216 // available register of the sequence %rax, %rdx is used.
3217 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003218 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003219
Chris Lattner1f3a0632010-07-29 21:42:50 +00003220 // If we have a sign or zero extended integer, make sure to return Extend
3221 // so that the parameter gets the right LLVM IR attributes.
3222 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3223 // Treat an enum type as its underlying type.
3224 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3225 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003226
Chris Lattner1f3a0632010-07-29 21:42:50 +00003227 if (RetTy->isIntegralOrEnumerationType() &&
3228 RetTy->isPromotableIntegerType())
3229 return ABIArgInfo::getExtend();
3230 }
Chris Lattner31faff52010-07-28 23:06:14 +00003231 break;
3232
3233 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3234 // available SSE register of the sequence %xmm0, %xmm1 is used.
3235 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003236 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003237 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003238
3239 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3240 // returned on the X87 stack in %st0 as 80-bit x87 number.
3241 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00003242 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003243 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003244
3245 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3246 // part of the value is returned in %st0 and the imaginary part in
3247 // %st1.
3248 case ComplexX87:
3249 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00003250 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Serge Guelton1d993272017-05-09 19:31:30 +00003251 llvm::Type::getX86_FP80Ty(getVMContext()));
Chris Lattner31faff52010-07-28 23:06:14 +00003252 break;
3253 }
3254
Craig Topper8a13c412014-05-21 05:09:00 +00003255 llvm::Type *HighPart = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00003256 switch (Hi) {
3257 // Memory was handled previously and X87 should
3258 // never occur as a hi class.
3259 case Memory:
3260 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00003261 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00003262
3263 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003264 case NoClass:
3265 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003266
Chris Lattner52b3c132010-09-01 00:20:33 +00003267 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003268 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003269 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3270 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00003271 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00003272 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003273 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003274 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3275 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00003276 break;
3277
3278 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003279 // is passed in the next available eightbyte chunk if the last used
3280 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00003281 //
Chris Lattner57540c52011-04-15 05:22:18 +00003282 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00003283 case SSEUp:
3284 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003285 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00003286 break;
3287
3288 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3289 // returned together with the previous X87 value in %st0.
3290 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00003291 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00003292 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00003293 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00003294 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00003295 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003296 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003297 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3298 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00003299 }
Chris Lattner31faff52010-07-28 23:06:14 +00003300 break;
3301 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003302
Chris Lattner52b3c132010-09-01 00:20:33 +00003303 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003304 // known to pass in the high eightbyte of the result. We do this by forming a
3305 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003306 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00003307 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner31faff52010-07-28 23:06:14 +00003308
Chris Lattner1f3a0632010-07-29 21:42:50 +00003309 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00003310}
3311
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003312ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman96fd2642013-06-12 00:13:45 +00003313 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3314 bool isNamedArg)
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003315 const
3316{
Reid Klecknerb1be6832014-11-15 01:41:41 +00003317 Ty = useFirstFieldIfTransparentUnion(Ty);
3318
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003319 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00003320 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003321
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003322 // Check some invariants.
3323 // FIXME: Enforce these by construction.
3324 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003325 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3326
3327 neededInt = 0;
3328 neededSSE = 0;
Craig Topper8a13c412014-05-21 05:09:00 +00003329 llvm::Type *ResType = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003330 switch (Lo) {
3331 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003332 if (Hi == NoClass)
3333 return ABIArgInfo::getIgnore();
3334 // If the low part is just padding, it takes no register, leave ResType
3335 // null.
3336 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3337 "Unknown missing lo part");
3338 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003339
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003340 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3341 // on the stack.
3342 case Memory:
3343
3344 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3345 // COMPLEX_X87, it is passed in memory.
3346 case X87:
3347 case ComplexX87:
Mark Lacey3825e832013-10-06 01:33:34 +00003348 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
Eli Friedman4774b7e2011-06-29 07:04:55 +00003349 ++neededInt;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003350 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003351
3352 case SSEUp:
3353 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00003354 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003355
3356 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3357 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3358 // and %r9 is used.
3359 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00003360 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003361
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003362 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003363 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00003364
3365 // If we have a sign or zero extended integer, make sure to return Extend
3366 // so that the parameter gets the right LLVM IR attributes.
3367 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3368 // Treat an enum type as its underlying type.
3369 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3370 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003371
Chris Lattner1f3a0632010-07-29 21:42:50 +00003372 if (Ty->isIntegralOrEnumerationType() &&
3373 Ty->isPromotableIntegerType())
3374 return ABIArgInfo::getExtend();
3375 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003376
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003377 break;
3378
3379 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3380 // available SSE register is used, the registers are taken in the
3381 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00003382 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003383 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00003384 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00003385 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003386 break;
3387 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00003388 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003389
Craig Topper8a13c412014-05-21 05:09:00 +00003390 llvm::Type *HighPart = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003391 switch (Hi) {
3392 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00003393 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003394 // which is passed in memory.
3395 case Memory:
3396 case X87:
3397 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00003398 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003399
3400 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003401
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003402 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003403 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003404 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003405 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003406
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003407 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3408 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003409 break;
3410
3411 // X87Up generally doesn't occur here (long double is passed in
3412 // memory), except in situations involving unions.
3413 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003414 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003415 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003416
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003417 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3418 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003419
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003420 ++neededSSE;
3421 break;
3422
3423 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3424 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003425 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003426 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00003427 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003428 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003429 break;
3430 }
3431
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003432 // If a high part was specified, merge it together with the low part. It is
3433 // known to pass in the high eightbyte of the result. We do this by forming a
3434 // first class struct aggregate with the high and low part: {low, high}
3435 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00003436 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003437
Chris Lattner1f3a0632010-07-29 21:42:50 +00003438 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003439}
3440
Erich Keane757d3172016-11-02 18:29:35 +00003441ABIArgInfo
3442X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3443 unsigned &NeededSSE) const {
3444 auto RT = Ty->getAs<RecordType>();
3445 assert(RT && "classifyRegCallStructType only valid with struct types");
3446
3447 if (RT->getDecl()->hasFlexibleArrayMember())
3448 return getIndirectReturnResult(Ty);
3449
3450 // Sum up bases
3451 if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3452 if (CXXRD->isDynamicClass()) {
3453 NeededInt = NeededSSE = 0;
3454 return getIndirectReturnResult(Ty);
3455 }
3456
3457 for (const auto &I : CXXRD->bases())
3458 if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
3459 .isIndirect()) {
3460 NeededInt = NeededSSE = 0;
3461 return getIndirectReturnResult(Ty);
3462 }
3463 }
3464
3465 // Sum up members
3466 for (const auto *FD : RT->getDecl()->fields()) {
3467 if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
3468 if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
3469 .isIndirect()) {
3470 NeededInt = NeededSSE = 0;
3471 return getIndirectReturnResult(Ty);
3472 }
3473 } else {
3474 unsigned LocalNeededInt, LocalNeededSSE;
3475 if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
3476 LocalNeededSSE, true)
3477 .isIndirect()) {
3478 NeededInt = NeededSSE = 0;
3479 return getIndirectReturnResult(Ty);
3480 }
3481 NeededInt += LocalNeededInt;
3482 NeededSSE += LocalNeededSSE;
3483 }
3484 }
3485
3486 return ABIArgInfo::getDirect();
3487}
3488
3489ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
3490 unsigned &NeededInt,
3491 unsigned &NeededSSE) const {
3492
3493 NeededInt = 0;
3494 NeededSSE = 0;
3495
3496 return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
3497}
3498
Chris Lattner22326a12010-07-29 02:31:05 +00003499void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003500
Erich Keane757d3172016-11-02 18:29:35 +00003501 bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003502
3503 // Keep track of the number of assigned registers.
Erich Keane757d3172016-11-02 18:29:35 +00003504 unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3505 unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3506 unsigned NeededInt, NeededSSE;
3507
3508 if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3509 !FI.getReturnType()->getTypePtr()->isUnionType()) {
3510 FI.getReturnInfo() =
3511 classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
3512 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3513 FreeIntRegs -= NeededInt;
3514 FreeSSERegs -= NeededSSE;
3515 } else {
3516 FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3517 }
3518 } else if (!getCXXABI().classifyReturnType(FI))
3519 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003520
3521 // If the return value is indirect, then the hidden argument is consuming one
3522 // integer register.
3523 if (FI.getReturnInfo().isIndirect())
Erich Keane757d3172016-11-02 18:29:35 +00003524 --FreeIntRegs;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003525
Peter Collingbournef7706832014-12-12 23:41:25 +00003526 // The chain argument effectively gives us another free register.
3527 if (FI.isChainCall())
Erich Keane757d3172016-11-02 18:29:35 +00003528 ++FreeIntRegs;
Peter Collingbournef7706832014-12-12 23:41:25 +00003529
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003530 unsigned NumRequiredArgs = FI.getNumRequiredArgs();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003531 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3532 // get assigned (in left-to-right order) for passing as follows...
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003533 unsigned ArgNo = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003534 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003535 it != ie; ++it, ++ArgNo) {
3536 bool IsNamedArg = ArgNo < NumRequiredArgs;
Eli Friedman96fd2642013-06-12 00:13:45 +00003537
Erich Keane757d3172016-11-02 18:29:35 +00003538 if (IsRegCall && it->type->isStructureOrClassType())
3539 it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
3540 else
3541 it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
3542 NeededSSE, IsNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003543
3544 // AMD64-ABI 3.2.3p3: If there are no registers available for any
3545 // eightbyte of an argument, the whole argument is passed on the
3546 // stack. If registers have already been assigned for some
3547 // eightbytes of such an argument, the assignments get reverted.
Erich Keane757d3172016-11-02 18:29:35 +00003548 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3549 FreeIntRegs -= NeededInt;
3550 FreeSSERegs -= NeededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003551 } else {
Erich Keane757d3172016-11-02 18:29:35 +00003552 it->info = getIndirectResult(it->type, FreeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003553 }
3554 }
3555}
3556
John McCall7f416cc2015-09-08 08:05:57 +00003557static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3558 Address VAListAddr, QualType Ty) {
3559 Address overflow_arg_area_p = CGF.Builder.CreateStructGEP(
3560 VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003561 llvm::Value *overflow_arg_area =
3562 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3563
3564 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3565 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00003566 // It isn't stated explicitly in the standard, but in practice we use
3567 // alignment greater than 16 where necessary.
Petar Jovanovic402257b2015-12-04 00:26:47 +00003568 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3569 if (Align > CharUnits::fromQuantity(8)) {
3570 overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3571 Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003572 }
3573
3574 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00003575 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003576 llvm::Value *Res =
3577 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003578 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003579
3580 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3581 // l->overflow_arg_area + sizeof(type).
3582 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3583 // an 8 byte boundary.
3584
3585 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00003586 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00003587 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003588 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3589 "overflow_arg_area.next");
3590 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3591
3592 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
Petar Jovanovic402257b2015-12-04 00:26:47 +00003593 return Address(Res, Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003594}
3595
John McCall7f416cc2015-09-08 08:05:57 +00003596Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3597 QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003598 // Assume that va_list type is correct; should be pointer to LLVM type:
3599 // struct {
3600 // i32 gp_offset;
3601 // i32 fp_offset;
3602 // i8* overflow_arg_area;
3603 // i8* reg_save_area;
3604 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00003605 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003606
John McCall7f416cc2015-09-08 08:05:57 +00003607 Ty = getContext().getCanonicalType(Ty);
Eric Christopher7565e0d2015-05-29 23:09:49 +00003608 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
Eli Friedman96fd2642013-06-12 00:13:45 +00003609 /*isNamedArg*/false);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003610
3611 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3612 // in the registers. If not go to step 7.
3613 if (!neededInt && !neededSSE)
John McCall7f416cc2015-09-08 08:05:57 +00003614 return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003615
3616 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3617 // general purpose registers needed to pass type and num_fp to hold
3618 // the number of floating point registers needed.
3619
3620 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3621 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3622 // l->fp_offset > 304 - num_fp * 16 go to step 7.
3623 //
3624 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3625 // register save space).
3626
Craig Topper8a13c412014-05-21 05:09:00 +00003627 llvm::Value *InRegs = nullptr;
John McCall7f416cc2015-09-08 08:05:57 +00003628 Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3629 llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003630 if (neededInt) {
David Blaikie1ed728c2015-04-05 22:45:47 +00003631 gp_offset_p =
John McCall7f416cc2015-09-08 08:05:57 +00003632 CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(),
3633 "gp_offset_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003634 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00003635 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3636 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003637 }
3638
3639 if (neededSSE) {
David Blaikie1ed728c2015-04-05 22:45:47 +00003640 fp_offset_p =
John McCall7f416cc2015-09-08 08:05:57 +00003641 CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4),
3642 "fp_offset_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003643 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3644 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00003645 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3646 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003647 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3648 }
3649
3650 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3651 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3652 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3653 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3654
3655 // Emit code to load the value if it was passed in registers.
3656
3657 CGF.EmitBlock(InRegBlock);
3658
3659 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3660 // an offset of l->gp_offset and/or l->fp_offset. This may require
3661 // copying to a temporary location in case the parameter is passed
3662 // in different register classes or requires an alignment greater
3663 // than 8 for general purpose registers and 16 for XMM registers.
3664 //
3665 // FIXME: This really results in shameful code when we end up needing to
3666 // collect arguments from different places; often what should result in a
3667 // simple assembling of a structure from scattered addresses has many more
3668 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00003669 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00003670 llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
3671 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)),
3672 "reg_save_area");
3673
3674 Address RegAddr = Address::invalid();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003675 if (neededInt && neededSSE) {
3676 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003677 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003678 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
John McCall7f416cc2015-09-08 08:05:57 +00003679 Address Tmp = CGF.CreateMemTemp(Ty);
3680 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003681 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003682 llvm::Type *TyLo = ST->getElementType(0);
3683 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00003684 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003685 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003686 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
3687 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
John McCall7f416cc2015-09-08 08:05:57 +00003688 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
3689 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
Rafael Espindola0a500af2014-06-24 20:01:50 +00003690 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
3691 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003692
John McCall7f416cc2015-09-08 08:05:57 +00003693 // Copy the first element.
Peter Collingbourneb367c562016-11-28 22:30:21 +00003694 // FIXME: Our choice of alignment here and below is probably pessimistic.
3695 llvm::Value *V = CGF.Builder.CreateAlignedLoad(
3696 TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
3697 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
John McCall7f416cc2015-09-08 08:05:57 +00003698 CGF.Builder.CreateStore(V,
3699 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3700
3701 // Copy the second element.
Peter Collingbourneb367c562016-11-28 22:30:21 +00003702 V = CGF.Builder.CreateAlignedLoad(
3703 TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
3704 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
John McCall7f416cc2015-09-08 08:05:57 +00003705 CharUnits Offset = CharUnits::fromQuantity(
3706 getDataLayout().getStructLayout(ST)->getElementOffset(1));
3707 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset));
3708
3709 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003710 } else if (neededInt) {
John McCall7f416cc2015-09-08 08:05:57 +00003711 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
3712 CharUnits::fromQuantity(8));
3713 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003714
3715 // Copy to a temporary if necessary to ensure the appropriate alignment.
3716 std::pair<CharUnits, CharUnits> SizeAlign =
John McCall7f416cc2015-09-08 08:05:57 +00003717 getContext().getTypeInfoInChars(Ty);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003718 uint64_t TySize = SizeAlign.first.getQuantity();
John McCall7f416cc2015-09-08 08:05:57 +00003719 CharUnits TyAlign = SizeAlign.second;
3720
3721 // Copy into a temporary if the type is more aligned than the
3722 // register save area.
3723 if (TyAlign.getQuantity() > 8) {
3724 Address Tmp = CGF.CreateMemTemp(Ty);
3725 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003726 RegAddr = Tmp;
3727 }
John McCall7f416cc2015-09-08 08:05:57 +00003728
Chris Lattner0cf24192010-06-28 20:05:43 +00003729 } else if (neededSSE == 1) {
John McCall7f416cc2015-09-08 08:05:57 +00003730 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3731 CharUnits::fromQuantity(16));
3732 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003733 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00003734 assert(neededSSE == 2 && "Invalid number of needed registers!");
3735 // SSE registers are spaced 16 bytes apart in the register save
3736 // area, we need to collect the two eightbytes together.
John McCall7f416cc2015-09-08 08:05:57 +00003737 // The ABI isn't explicit about this, but it seems reasonable
3738 // to assume that the slots are 16-byte aligned, since the stack is
3739 // naturally 16-byte aligned and the prologue is expected to store
3740 // all the SSE registers to the RSA.
3741 Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3742 CharUnits::fromQuantity(16));
3743 Address RegAddrHi =
3744 CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
3745 CharUnits::fromQuantity(16));
Chris Lattnerece04092012-02-07 00:39:47 +00003746 llvm::Type *DoubleTy = CGF.DoubleTy;
Serge Guelton1d993272017-05-09 19:31:30 +00003747 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy);
John McCall7f416cc2015-09-08 08:05:57 +00003748 llvm::Value *V;
3749 Address Tmp = CGF.CreateMemTemp(Ty);
3750 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3751 V = CGF.Builder.CreateLoad(
3752 CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy));
3753 CGF.Builder.CreateStore(V,
3754 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3755 V = CGF.Builder.CreateLoad(
3756 CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy));
3757 CGF.Builder.CreateStore(V,
3758 CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8)));
3759
3760 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003761 }
3762
3763 // AMD64-ABI 3.5.7p5: Step 5. Set:
3764 // l->gp_offset = l->gp_offset + num_gp * 8
3765 // l->fp_offset = l->fp_offset + num_fp * 16.
3766 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003767 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003768 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3769 gp_offset_p);
3770 }
3771 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003772 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003773 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3774 fp_offset_p);
3775 }
3776 CGF.EmitBranch(ContBlock);
3777
3778 // Emit code to load the value if it was passed in memory.
3779
3780 CGF.EmitBlock(InMemBlock);
John McCall7f416cc2015-09-08 08:05:57 +00003781 Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003782
3783 // Return the appropriate result.
3784
3785 CGF.EmitBlock(ContBlock);
John McCall7f416cc2015-09-08 08:05:57 +00003786 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
3787 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003788 return ResAddr;
3789}
3790
Charles Davisc7d5c942015-09-17 20:55:33 +00003791Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
3792 QualType Ty) const {
3793 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3794 CGF.getContext().getTypeInfoInChars(Ty),
3795 CharUnits::fromQuantity(8),
3796 /*allowHigherAlign*/ false);
3797}
3798
Erich Keane521ed962017-01-05 00:20:51 +00003799ABIArgInfo
3800WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
3801 const ABIArgInfo &current) const {
3802 // Assumes vectorCall calling convention.
3803 const Type *Base = nullptr;
3804 uint64_t NumElts = 0;
3805
3806 if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
3807 isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
3808 FreeSSERegs -= NumElts;
3809 return getDirectX86Hva();
3810 }
3811 return current;
3812}
3813
Reid Kleckner80944df2014-10-31 22:00:51 +00003814ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
Erich Keane521ed962017-01-05 00:20:51 +00003815 bool IsReturnType, bool IsVectorCall,
3816 bool IsRegCall) const {
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003817
3818 if (Ty->isVoidType())
3819 return ABIArgInfo::getIgnore();
3820
3821 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3822 Ty = EnumTy->getDecl()->getIntegerType();
3823
Reid Kleckner80944df2014-10-31 22:00:51 +00003824 TypeInfo Info = getContext().getTypeInfo(Ty);
3825 uint64_t Width = Info.Width;
Reid Kleckner11a17192015-10-28 22:29:52 +00003826 CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003827
Reid Kleckner9005f412014-05-02 00:51:20 +00003828 const RecordType *RT = Ty->getAs<RecordType>();
3829 if (RT) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00003830 if (!IsReturnType) {
Mark Lacey3825e832013-10-06 01:33:34 +00003831 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00003832 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00003833 }
3834
3835 if (RT->getDecl()->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00003836 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003837
Reid Kleckner9005f412014-05-02 00:51:20 +00003838 }
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00003839
Reid Kleckner80944df2014-10-31 22:00:51 +00003840 const Type *Base = nullptr;
3841 uint64_t NumElts = 0;
Erich Keane521ed962017-01-05 00:20:51 +00003842 // vectorcall adds the concept of a homogenous vector aggregate, similar to
3843 // other targets.
3844 if ((IsVectorCall || IsRegCall) &&
3845 isHomogeneousAggregate(Ty, Base, NumElts)) {
3846 if (IsRegCall) {
3847 if (FreeSSERegs >= NumElts) {
3848 FreeSSERegs -= NumElts;
3849 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3850 return ABIArgInfo::getDirect();
3851 return ABIArgInfo::getExpand();
3852 }
3853 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3854 } else if (IsVectorCall) {
3855 if (FreeSSERegs >= NumElts &&
3856 (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
3857 FreeSSERegs -= NumElts;
Reid Kleckner80944df2014-10-31 22:00:51 +00003858 return ABIArgInfo::getDirect();
Erich Keane521ed962017-01-05 00:20:51 +00003859 } else if (IsReturnType) {
3860 return ABIArgInfo::getExpand();
3861 } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
3862 // HVAs are delayed and reclassified in the 2nd step.
3863 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3864 }
Reid Kleckner80944df2014-10-31 22:00:51 +00003865 }
Reid Kleckner80944df2014-10-31 22:00:51 +00003866 }
3867
Reid Klecknerec87fec2014-05-02 01:17:12 +00003868 if (Ty->isMemberPointerType()) {
Reid Kleckner7f5f0f32014-05-02 01:14:59 +00003869 // If the member pointer is represented by an LLVM int or ptr, pass it
3870 // directly.
3871 llvm::Type *LLTy = CGT.ConvertType(Ty);
3872 if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3873 return ABIArgInfo::getDirect();
Reid Kleckner9005f412014-05-02 00:51:20 +00003874 }
3875
Michael Kuperstein4f818702015-02-24 09:35:58 +00003876 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00003877 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3878 // not 1, 2, 4, or 8 bytes, must be passed by reference."
Reid Kleckner80944df2014-10-31 22:00:51 +00003879 if (Width > 64 || !llvm::isPowerOf2_64(Width))
John McCall7f416cc2015-09-08 08:05:57 +00003880 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003881
Reid Kleckner9005f412014-05-02 00:51:20 +00003882 // Otherwise, coerce it to a small integer.
Reid Kleckner80944df2014-10-31 22:00:51 +00003883 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003884 }
3885
Julien Lerouge10dcff82014-08-27 00:36:55 +00003886 // Bool type is always extended to the ABI, other builtin types are not
3887 // extended.
3888 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3889 if (BT && BT->getKind() == BuiltinType::Bool)
Julien Lerougee8d34fa2014-08-26 22:11:53 +00003890 return ABIArgInfo::getExtend();
3891
Reid Kleckner11a17192015-10-28 22:29:52 +00003892 // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It
3893 // passes them indirectly through memory.
3894 if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) {
3895 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003896 if (LDF == &llvm::APFloat::x87DoubleExtended())
Reid Kleckner11a17192015-10-28 22:29:52 +00003897 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3898 }
3899
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003900 return ABIArgInfo::getDirect();
3901}
3902
Erich Keane521ed962017-01-05 00:20:51 +00003903void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI,
3904 unsigned FreeSSERegs,
3905 bool IsVectorCall,
3906 bool IsRegCall) const {
3907 unsigned Count = 0;
3908 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00003909 // Vectorcall in x64 only permits the first 6 arguments to be passed
3910 // as XMM/YMM registers.
Erich Keane521ed962017-01-05 00:20:51 +00003911 if (Count < VectorcallMaxParamNumAsReg)
3912 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
3913 else {
3914 // Since these cannot be passed in registers, pretend no registers
3915 // are left.
3916 unsigned ZeroSSERegsAvail = 0;
3917 I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false,
3918 IsVectorCall, IsRegCall);
3919 }
3920 ++Count;
3921 }
3922
Erich Keane521ed962017-01-05 00:20:51 +00003923 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00003924 I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info);
Erich Keane521ed962017-01-05 00:20:51 +00003925 }
3926}
3927
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003928void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner80944df2014-10-31 22:00:51 +00003929 bool IsVectorCall =
3930 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
Erich Keane757d3172016-11-02 18:29:35 +00003931 bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall;
Reid Kleckner37abaca2014-05-09 22:46:15 +00003932
Erich Keane757d3172016-11-02 18:29:35 +00003933 unsigned FreeSSERegs = 0;
3934 if (IsVectorCall) {
3935 // We can use up to 4 SSE return registers with vectorcall.
3936 FreeSSERegs = 4;
3937 } else if (IsRegCall) {
3938 // RegCall gives us 16 SSE registers.
3939 FreeSSERegs = 16;
3940 }
3941
Reid Kleckner80944df2014-10-31 22:00:51 +00003942 if (!getCXXABI().classifyReturnType(FI))
Erich Keane521ed962017-01-05 00:20:51 +00003943 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
3944 IsVectorCall, IsRegCall);
Reid Kleckner80944df2014-10-31 22:00:51 +00003945
Erich Keane757d3172016-11-02 18:29:35 +00003946 if (IsVectorCall) {
3947 // We can use up to 6 SSE register parameters with vectorcall.
3948 FreeSSERegs = 6;
3949 } else if (IsRegCall) {
Erich Keane521ed962017-01-05 00:20:51 +00003950 // RegCall gives us 16 SSE registers, we can reuse the return registers.
Erich Keane757d3172016-11-02 18:29:35 +00003951 FreeSSERegs = 16;
3952 }
3953
Erich Keane521ed962017-01-05 00:20:51 +00003954 if (IsVectorCall) {
3955 computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall);
3956 } else {
3957 for (auto &I : FI.arguments())
3958 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
3959 }
3960
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003961}
3962
John McCall7f416cc2015-09-08 08:05:57 +00003963Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3964 QualType Ty) const {
Reid Klecknerb04449d2016-08-25 20:42:26 +00003965
3966 bool IsIndirect = false;
3967
3968 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3969 // not 1, 2, 4, or 8 bytes, must be passed by reference."
3970 if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) {
3971 uint64_t Width = getContext().getTypeSize(Ty);
3972 IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
3973 }
3974
3975 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
John McCall7f416cc2015-09-08 08:05:57 +00003976 CGF.getContext().getTypeInfoInChars(Ty),
3977 CharUnits::fromQuantity(8),
3978 /*allowHigherAlign*/ false);
Chris Lattner04dc9572010-08-31 16:44:54 +00003979}
Chris Lattner0cf24192010-06-28 20:05:43 +00003980
John McCallea8d8bb2010-03-11 00:10:12 +00003981// PowerPC-32
John McCallea8d8bb2010-03-11 00:10:12 +00003982namespace {
Roman Divacky8a12d842014-11-03 18:32:54 +00003983/// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
3984class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003985bool IsSoftFloatABI;
John McCallea8d8bb2010-03-11 00:10:12 +00003986public:
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003987 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
3988 : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
Roman Divacky8a12d842014-11-03 18:32:54 +00003989
John McCall7f416cc2015-09-08 08:05:57 +00003990 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3991 QualType Ty) const override;
Roman Divacky8a12d842014-11-03 18:32:54 +00003992};
3993
3994class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
3995public:
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003996 PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
3997 : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003998
Craig Topper4f12f102014-03-12 06:41:41 +00003999 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallea8d8bb2010-03-11 00:10:12 +00004000 // This is recovered from gcc output.
4001 return 1; // r1 is the dedicated stack pointer
4002 }
4003
4004 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004005 llvm::Value *Address) const override;
John McCallea8d8bb2010-03-11 00:10:12 +00004006};
4007
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004008}
John McCallea8d8bb2010-03-11 00:10:12 +00004009
James Y Knight29b5f082016-02-24 02:59:33 +00004010// TODO: this implementation is now likely redundant with
4011// DefaultABIInfo::EmitVAArg.
John McCall7f416cc2015-09-08 08:05:57 +00004012Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4013 QualType Ty) const {
Roman Divacky039b9702016-02-20 08:31:24 +00004014 const unsigned OverflowLimit = 8;
Roman Divacky8a12d842014-11-03 18:32:54 +00004015 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4016 // TODO: Implement this. For now ignore.
4017 (void)CTy;
James Y Knight29b5f082016-02-24 02:59:33 +00004018 return Address::invalid(); // FIXME?
Roman Divacky8a12d842014-11-03 18:32:54 +00004019 }
4020
John McCall7f416cc2015-09-08 08:05:57 +00004021 // struct __va_list_tag {
4022 // unsigned char gpr;
4023 // unsigned char fpr;
4024 // unsigned short reserved;
4025 // void *overflow_arg_area;
4026 // void *reg_save_area;
4027 // };
4028
Roman Divacky8a12d842014-11-03 18:32:54 +00004029 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
Eric Christopher7565e0d2015-05-29 23:09:49 +00004030 bool isInt =
4031 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004032 bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
John McCall7f416cc2015-09-08 08:05:57 +00004033
4034 // All aggregates are passed indirectly? That doesn't seem consistent
4035 // with the argument-lowering code.
4036 bool isIndirect = Ty->isAggregateType();
Roman Divacky8a12d842014-11-03 18:32:54 +00004037
4038 CGBuilderTy &Builder = CGF.Builder;
John McCall7f416cc2015-09-08 08:05:57 +00004039
4040 // The calling convention either uses 1-2 GPRs or 1 FPR.
4041 Address NumRegsAddr = Address::invalid();
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004042 if (isInt || IsSoftFloatABI) {
John McCall7f416cc2015-09-08 08:05:57 +00004043 NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr");
4044 } else {
4045 NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr");
Roman Divacky8a12d842014-11-03 18:32:54 +00004046 }
John McCall7f416cc2015-09-08 08:05:57 +00004047
4048 llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4049
4050 // "Align" the register count when TY is i64.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004051 if (isI64 || (isF64 && IsSoftFloatABI)) {
John McCall7f416cc2015-09-08 08:05:57 +00004052 NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4053 NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4054 }
Roman Divacky8a12d842014-11-03 18:32:54 +00004055
Eric Christopher7565e0d2015-05-29 23:09:49 +00004056 llvm::Value *CC =
Roman Divacky039b9702016-02-20 08:31:24 +00004057 Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
Roman Divacky8a12d842014-11-03 18:32:54 +00004058
4059 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4060 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4061 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4062
4063 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4064
John McCall7f416cc2015-09-08 08:05:57 +00004065 llvm::Type *DirectTy = CGF.ConvertType(Ty);
4066 if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
Roman Divacky8a12d842014-11-03 18:32:54 +00004067
John McCall7f416cc2015-09-08 08:05:57 +00004068 // Case 1: consume registers.
4069 Address RegAddr = Address::invalid();
4070 {
4071 CGF.EmitBlock(UsingRegs);
4072
4073 Address RegSaveAreaPtr =
4074 Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8));
4075 RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
4076 CharUnits::fromQuantity(8));
4077 assert(RegAddr.getElementType() == CGF.Int8Ty);
4078
4079 // Floating-point registers start after the general-purpose registers.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004080 if (!(isInt || IsSoftFloatABI)) {
John McCall7f416cc2015-09-08 08:05:57 +00004081 RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4082 CharUnits::fromQuantity(32));
4083 }
4084
4085 // Get the address of the saved value by scaling the number of
4086 // registers we've used by the number of
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004087 CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
John McCall7f416cc2015-09-08 08:05:57 +00004088 llvm::Value *RegOffset =
4089 Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4090 RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
4091 RegAddr.getPointer(), RegOffset),
4092 RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4093 RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4094
4095 // Increase the used-register count.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004096 NumRegs =
4097 Builder.CreateAdd(NumRegs,
4098 Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
John McCall7f416cc2015-09-08 08:05:57 +00004099 Builder.CreateStore(NumRegs, NumRegsAddr);
4100
4101 CGF.EmitBranch(Cont);
Roman Divacky8a12d842014-11-03 18:32:54 +00004102 }
Roman Divacky8a12d842014-11-03 18:32:54 +00004103
John McCall7f416cc2015-09-08 08:05:57 +00004104 // Case 2: consume space in the overflow area.
4105 Address MemAddr = Address::invalid();
4106 {
4107 CGF.EmitBlock(UsingOverflow);
Roman Divacky8a12d842014-11-03 18:32:54 +00004108
Roman Divacky039b9702016-02-20 08:31:24 +00004109 Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4110
John McCall7f416cc2015-09-08 08:05:57 +00004111 // Everything in the overflow area is rounded up to a size of at least 4.
4112 CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4113
4114 CharUnits Size;
4115 if (!isIndirect) {
4116 auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
Rui Ueyama83aa9792016-01-14 21:00:27 +00004117 Size = TypeInfo.first.alignTo(OverflowAreaAlign);
John McCall7f416cc2015-09-08 08:05:57 +00004118 } else {
4119 Size = CGF.getPointerSize();
4120 }
4121
4122 Address OverflowAreaAddr =
4123 Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4));
Petar Jovanovic402257b2015-12-04 00:26:47 +00004124 Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
John McCall7f416cc2015-09-08 08:05:57 +00004125 OverflowAreaAlign);
Petar Jovanovic402257b2015-12-04 00:26:47 +00004126 // Round up address of argument to alignment
4127 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4128 if (Align > OverflowAreaAlign) {
4129 llvm::Value *Ptr = OverflowArea.getPointer();
4130 OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4131 Align);
4132 }
4133
John McCall7f416cc2015-09-08 08:05:57 +00004134 MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4135
4136 // Increase the overflow area.
4137 OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4138 Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4139 CGF.EmitBranch(Cont);
4140 }
Roman Divacky8a12d842014-11-03 18:32:54 +00004141
4142 CGF.EmitBlock(Cont);
4143
John McCall7f416cc2015-09-08 08:05:57 +00004144 // Merge the cases with a phi.
4145 Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4146 "vaarg.addr");
Roman Divacky8a12d842014-11-03 18:32:54 +00004147
John McCall7f416cc2015-09-08 08:05:57 +00004148 // Load the pointer if the argument was passed indirectly.
4149 if (isIndirect) {
4150 Result = Address(Builder.CreateLoad(Result, "aggr"),
4151 getContext().getTypeAlignInChars(Ty));
Roman Divacky8a12d842014-11-03 18:32:54 +00004152 }
4153
4154 return Result;
4155}
4156
John McCallea8d8bb2010-03-11 00:10:12 +00004157bool
4158PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4159 llvm::Value *Address) const {
4160 // This is calculated from the LLVM and GCC tables and verified
4161 // against gcc output. AFAIK all ABIs use the same encoding.
4162
4163 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallea8d8bb2010-03-11 00:10:12 +00004164
Chris Lattnerece04092012-02-07 00:39:47 +00004165 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallea8d8bb2010-03-11 00:10:12 +00004166 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4167 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4168 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4169
4170 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00004171 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00004172
4173 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00004174 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00004175
4176 // 64-76 are various 4-byte special-purpose registers:
4177 // 64: mq
4178 // 65: lr
4179 // 66: ctr
4180 // 67: ap
4181 // 68-75 cr0-7
4182 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00004183 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00004184
4185 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00004186 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00004187
4188 // 109: vrsave
4189 // 110: vscr
4190 // 111: spe_acc
4191 // 112: spefscr
4192 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00004193 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00004194
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00004195 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00004196}
4197
Roman Divackyd966e722012-05-09 18:22:46 +00004198// PowerPC-64
4199
4200namespace {
Bill Schmidt25cb3492012-10-03 19:18:57 +00004201/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
James Y Knight29b5f082016-02-24 02:59:33 +00004202class PPC64_SVR4_ABIInfo : public ABIInfo {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004203public:
4204 enum ABIKind {
4205 ELFv1 = 0,
4206 ELFv2
4207 };
4208
4209private:
4210 static const unsigned GPRBits = 64;
4211 ABIKind Kind;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004212 bool HasQPX;
Hal Finkel415c2a32016-10-02 02:10:45 +00004213 bool IsSoftFloatABI;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004214
4215 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
4216 // will be passed in a QPX register.
4217 bool IsQPXVectorTy(const Type *Ty) const {
4218 if (!HasQPX)
4219 return false;
4220
4221 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4222 unsigned NumElements = VT->getNumElements();
4223 if (NumElements == 1)
4224 return false;
4225
4226 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
4227 if (getContext().getTypeSize(Ty) <= 256)
4228 return true;
4229 } else if (VT->getElementType()->
4230 isSpecificBuiltinType(BuiltinType::Float)) {
4231 if (getContext().getTypeSize(Ty) <= 128)
4232 return true;
4233 }
4234 }
4235
4236 return false;
4237 }
4238
4239 bool IsQPXVectorTy(QualType Ty) const {
4240 return IsQPXVectorTy(Ty.getTypePtr());
4241 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00004242
4243public:
Hal Finkel415c2a32016-10-02 02:10:45 +00004244 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX,
4245 bool SoftFloatABI)
4246 : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX),
4247 IsSoftFloatABI(SoftFloatABI) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00004248
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004249 bool isPromotableTypeForABI(QualType Ty) const;
John McCall7f416cc2015-09-08 08:05:57 +00004250 CharUnits getParamTypeAlignment(QualType Ty) const;
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004251
4252 ABIArgInfo classifyReturnType(QualType RetTy) const;
4253 ABIArgInfo classifyArgumentType(QualType Ty) const;
4254
Reid Klecknere9f6a712014-10-31 17:10:41 +00004255 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4256 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4257 uint64_t Members) const override;
4258
Bill Schmidt84d37792012-10-12 19:26:17 +00004259 // TODO: We can add more logic to computeInfo to improve performance.
4260 // Example: For aggregate arguments that fit in a register, we could
4261 // use getDirectInReg (as is done below for structs containing a single
4262 // floating-point value) to avoid pushing them to memory on function
4263 // entry. This would require changing the logic in PPCISelLowering
4264 // when lowering the parameters in the caller and args in the callee.
Craig Topper4f12f102014-03-12 06:41:41 +00004265 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00004266 if (!getCXXABI().classifyReturnType(FI))
4267 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004268 for (auto &I : FI.arguments()) {
Bill Schmidt84d37792012-10-12 19:26:17 +00004269 // We rely on the default argument classification for the most part.
4270 // One exception: An aggregate containing a single floating-point
Bill Schmidt179afae2013-07-23 22:15:57 +00004271 // or vector item must be passed in a register if one is available.
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004272 const Type *T = isSingleElementStruct(I.type, getContext());
Bill Schmidt84d37792012-10-12 19:26:17 +00004273 if (T) {
4274 const BuiltinType *BT = T->getAs<BuiltinType>();
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004275 if (IsQPXVectorTy(T) ||
4276 (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004277 (BT && BT->isFloatingPoint())) {
Bill Schmidt84d37792012-10-12 19:26:17 +00004278 QualType QT(T, 0);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004279 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
Bill Schmidt84d37792012-10-12 19:26:17 +00004280 continue;
4281 }
4282 }
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004283 I.info = classifyArgumentType(I.type);
Bill Schmidt84d37792012-10-12 19:26:17 +00004284 }
4285 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00004286
John McCall7f416cc2015-09-08 08:05:57 +00004287 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4288 QualType Ty) const override;
Bill Schmidt25cb3492012-10-03 19:18:57 +00004289};
4290
4291class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004292
Bill Schmidt25cb3492012-10-03 19:18:57 +00004293public:
Ulrich Weigandb7122372014-07-21 00:48:09 +00004294 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
Hal Finkel415c2a32016-10-02 02:10:45 +00004295 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX,
4296 bool SoftFloatABI)
4297 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX,
4298 SoftFloatABI)) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00004299
Craig Topper4f12f102014-03-12 06:41:41 +00004300 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Bill Schmidt25cb3492012-10-03 19:18:57 +00004301 // This is recovered from gcc output.
4302 return 1; // r1 is the dedicated stack pointer
4303 }
4304
4305 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004306 llvm::Value *Address) const override;
Bill Schmidt25cb3492012-10-03 19:18:57 +00004307};
4308
Roman Divackyd966e722012-05-09 18:22:46 +00004309class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4310public:
4311 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
4312
Craig Topper4f12f102014-03-12 06:41:41 +00004313 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyd966e722012-05-09 18:22:46 +00004314 // This is recovered from gcc output.
4315 return 1; // r1 is the dedicated stack pointer
4316 }
4317
4318 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004319 llvm::Value *Address) const override;
Roman Divackyd966e722012-05-09 18:22:46 +00004320};
4321
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004322}
Roman Divackyd966e722012-05-09 18:22:46 +00004323
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004324// Return true if the ABI requires Ty to be passed sign- or zero-
4325// extended to 64 bits.
4326bool
4327PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
4328 // Treat an enum type as its underlying type.
4329 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4330 Ty = EnumTy->getDecl()->getIntegerType();
4331
4332 // Promotable integer types are required to be promoted by the ABI.
4333 if (Ty->isPromotableIntegerType())
4334 return true;
4335
4336 // In addition to the usual promotable integer types, we also need to
4337 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
4338 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4339 switch (BT->getKind()) {
4340 case BuiltinType::Int:
4341 case BuiltinType::UInt:
4342 return true;
4343 default:
4344 break;
4345 }
4346
4347 return false;
4348}
4349
John McCall7f416cc2015-09-08 08:05:57 +00004350/// isAlignedParamType - Determine whether a type requires 16-byte or
4351/// higher alignment in the parameter area. Always returns at least 8.
4352CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
Ulrich Weigand581badc2014-07-10 17:20:07 +00004353 // Complex types are passed just like their elements.
4354 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4355 Ty = CTy->getElementType();
4356
4357 // Only vector types of size 16 bytes need alignment (larger types are
4358 // passed via reference, smaller types are not aligned).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004359 if (IsQPXVectorTy(Ty)) {
4360 if (getContext().getTypeSize(Ty) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004361 return CharUnits::fromQuantity(32);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004362
John McCall7f416cc2015-09-08 08:05:57 +00004363 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004364 } else if (Ty->isVectorType()) {
John McCall7f416cc2015-09-08 08:05:57 +00004365 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004366 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00004367
4368 // For single-element float/vector structs, we consider the whole type
4369 // to have the same alignment requirements as its single element.
4370 const Type *AlignAsType = nullptr;
4371 const Type *EltType = isSingleElementStruct(Ty, getContext());
4372 if (EltType) {
4373 const BuiltinType *BT = EltType->getAs<BuiltinType>();
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004374 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
Ulrich Weigand581badc2014-07-10 17:20:07 +00004375 getContext().getTypeSize(EltType) == 128) ||
4376 (BT && BT->isFloatingPoint()))
4377 AlignAsType = EltType;
4378 }
4379
Ulrich Weigandb7122372014-07-21 00:48:09 +00004380 // Likewise for ELFv2 homogeneous aggregates.
4381 const Type *Base = nullptr;
4382 uint64_t Members = 0;
4383 if (!AlignAsType && Kind == ELFv2 &&
4384 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
4385 AlignAsType = Base;
4386
Ulrich Weigand581badc2014-07-10 17:20:07 +00004387 // With special case aggregates, only vector base types need alignment.
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004388 if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
4389 if (getContext().getTypeSize(AlignAsType) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004390 return CharUnits::fromQuantity(32);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004391
John McCall7f416cc2015-09-08 08:05:57 +00004392 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004393 } else if (AlignAsType) {
John McCall7f416cc2015-09-08 08:05:57 +00004394 return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004395 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00004396
4397 // Otherwise, we only need alignment for any aggregate type that
4398 // has an alignment requirement of >= 16 bytes.
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004399 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
4400 if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
John McCall7f416cc2015-09-08 08:05:57 +00004401 return CharUnits::fromQuantity(32);
4402 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004403 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00004404
John McCall7f416cc2015-09-08 08:05:57 +00004405 return CharUnits::fromQuantity(8);
Ulrich Weigand581badc2014-07-10 17:20:07 +00004406}
4407
Ulrich Weigandb7122372014-07-21 00:48:09 +00004408/// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
4409/// aggregate. Base is set to the base element type, and Members is set
4410/// to the number of base elements.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004411bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
4412 uint64_t &Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004413 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
4414 uint64_t NElements = AT->getSize().getZExtValue();
4415 if (NElements == 0)
4416 return false;
4417 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
4418 return false;
4419 Members *= NElements;
4420 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
4421 const RecordDecl *RD = RT->getDecl();
4422 if (RD->hasFlexibleArrayMember())
4423 return false;
4424
4425 Members = 0;
Ulrich Weiganda094f042014-10-29 13:23:20 +00004426
4427 // If this is a C++ record, check the bases first.
4428 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4429 for (const auto &I : CXXRD->bases()) {
4430 // Ignore empty records.
4431 if (isEmptyRecord(getContext(), I.getType(), true))
4432 continue;
4433
4434 uint64_t FldMembers;
4435 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
4436 return false;
4437
4438 Members += FldMembers;
4439 }
4440 }
4441
Ulrich Weigandb7122372014-07-21 00:48:09 +00004442 for (const auto *FD : RD->fields()) {
4443 // Ignore (non-zero arrays of) empty records.
4444 QualType FT = FD->getType();
4445 while (const ConstantArrayType *AT =
4446 getContext().getAsConstantArrayType(FT)) {
4447 if (AT->getSize().getZExtValue() == 0)
4448 return false;
4449 FT = AT->getElementType();
4450 }
4451 if (isEmptyRecord(getContext(), FT, true))
4452 continue;
4453
4454 // For compatibility with GCC, ignore empty bitfields in C++ mode.
4455 if (getContext().getLangOpts().CPlusPlus &&
4456 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4457 continue;
4458
4459 uint64_t FldMembers;
4460 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
4461 return false;
4462
4463 Members = (RD->isUnion() ?
4464 std::max(Members, FldMembers) : Members + FldMembers);
4465 }
4466
4467 if (!Base)
4468 return false;
4469
4470 // Ensure there is no padding.
4471 if (getContext().getTypeSize(Base) * Members !=
4472 getContext().getTypeSize(Ty))
4473 return false;
4474 } else {
4475 Members = 1;
4476 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
4477 Members = 2;
4478 Ty = CT->getElementType();
4479 }
4480
Reid Klecknere9f6a712014-10-31 17:10:41 +00004481 // Most ABIs only support float, double, and some vector type widths.
4482 if (!isHomogeneousAggregateBaseType(Ty))
Ulrich Weigandb7122372014-07-21 00:48:09 +00004483 return false;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004484
4485 // The base type must be the same for all members. Types that
4486 // agree in both total size and mode (float vs. vector) are
4487 // treated as being equivalent here.
4488 const Type *TyPtr = Ty.getTypePtr();
Ahmed Bougacha40a34c22016-04-19 17:54:29 +00004489 if (!Base) {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004490 Base = TyPtr;
Ahmed Bougacha40a34c22016-04-19 17:54:29 +00004491 // If it's a non-power-of-2 vector, its size is already a power-of-2,
4492 // so make sure to widen it explicitly.
4493 if (const VectorType *VT = Base->getAs<VectorType>()) {
4494 QualType EltTy = VT->getElementType();
4495 unsigned NumElements =
4496 getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
4497 Base = getContext()
4498 .getVectorType(EltTy, NumElements, VT->getVectorKind())
4499 .getTypePtr();
4500 }
4501 }
Ulrich Weigandb7122372014-07-21 00:48:09 +00004502
4503 if (Base->isVectorType() != TyPtr->isVectorType() ||
4504 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
4505 return false;
4506 }
Reid Klecknere9f6a712014-10-31 17:10:41 +00004507 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
4508}
Ulrich Weigandb7122372014-07-21 00:48:09 +00004509
Reid Klecknere9f6a712014-10-31 17:10:41 +00004510bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4511 // Homogeneous aggregates for ELFv2 must have base types of float,
4512 // double, long double, or 128-bit vectors.
4513 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4514 if (BT->getKind() == BuiltinType::Float ||
4515 BT->getKind() == BuiltinType::Double ||
Hal Finkel415c2a32016-10-02 02:10:45 +00004516 BT->getKind() == BuiltinType::LongDouble) {
4517 if (IsSoftFloatABI)
4518 return false;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004519 return true;
Hal Finkel415c2a32016-10-02 02:10:45 +00004520 }
Reid Klecknere9f6a712014-10-31 17:10:41 +00004521 }
4522 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004523 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
Reid Klecknere9f6a712014-10-31 17:10:41 +00004524 return true;
4525 }
4526 return false;
4527}
4528
4529bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
4530 const Type *Base, uint64_t Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004531 // Vector types require one register, floating point types require one
4532 // or two registers depending on their size.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004533 uint32_t NumRegs =
4534 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004535
4536 // Homogeneous Aggregates may occupy at most 8 registers.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004537 return Members * NumRegs <= 8;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004538}
4539
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004540ABIArgInfo
4541PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00004542 Ty = useFirstFieldIfTransparentUnion(Ty);
4543
Bill Schmidt90b22c92012-11-27 02:46:43 +00004544 if (Ty->isAnyComplexType())
4545 return ABIArgInfo::getDirect();
4546
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004547 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
4548 // or via reference (larger than 16 bytes).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004549 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004550 uint64_t Size = getContext().getTypeSize(Ty);
4551 if (Size > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004552 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004553 else if (Size < 128) {
4554 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4555 return ABIArgInfo::getDirect(CoerceTy);
4556 }
4557 }
4558
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004559 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +00004560 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00004561 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004562
John McCall7f416cc2015-09-08 08:05:57 +00004563 uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
4564 uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
Ulrich Weigandb7122372014-07-21 00:48:09 +00004565
4566 // ELFv2 homogeneous aggregates are passed as array types.
4567 const Type *Base = nullptr;
4568 uint64_t Members = 0;
4569 if (Kind == ELFv2 &&
4570 isHomogeneousAggregate(Ty, Base, Members)) {
4571 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4572 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4573 return ABIArgInfo::getDirect(CoerceTy);
4574 }
4575
Ulrich Weigand601957f2014-07-21 00:56:36 +00004576 // If an aggregate may end up fully in registers, we do not
4577 // use the ByVal method, but pass the aggregate as array.
4578 // This is usually beneficial since we avoid forcing the
4579 // back-end to store the argument to memory.
4580 uint64_t Bits = getContext().getTypeSize(Ty);
4581 if (Bits > 0 && Bits <= 8 * GPRBits) {
4582 llvm::Type *CoerceTy;
4583
4584 // Types up to 8 bytes are passed as integer type (which will be
4585 // properly aligned in the argument save area doubleword).
4586 if (Bits <= GPRBits)
Rui Ueyama83aa9792016-01-14 21:00:27 +00004587 CoerceTy =
4588 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Ulrich Weigand601957f2014-07-21 00:56:36 +00004589 // Larger types are passed as arrays, with the base type selected
4590 // according to the required alignment in the save area.
4591 else {
4592 uint64_t RegBits = ABIAlign * 8;
Rui Ueyama83aa9792016-01-14 21:00:27 +00004593 uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
Ulrich Weigand601957f2014-07-21 00:56:36 +00004594 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
4595 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
4596 }
4597
4598 return ABIArgInfo::getDirect(CoerceTy);
4599 }
4600
Ulrich Weigandb7122372014-07-21 00:48:09 +00004601 // All other aggregates are passed ByVal.
John McCall7f416cc2015-09-08 08:05:57 +00004602 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
4603 /*ByVal=*/true,
Ulrich Weigand581badc2014-07-10 17:20:07 +00004604 /*Realign=*/TyAlign > ABIAlign);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004605 }
4606
4607 return (isPromotableTypeForABI(Ty) ?
4608 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4609}
4610
4611ABIArgInfo
4612PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4613 if (RetTy->isVoidType())
4614 return ABIArgInfo::getIgnore();
4615
Bill Schmidta3d121c2012-12-17 04:20:17 +00004616 if (RetTy->isAnyComplexType())
4617 return ABIArgInfo::getDirect();
4618
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004619 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
4620 // or via reference (larger than 16 bytes).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004621 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004622 uint64_t Size = getContext().getTypeSize(RetTy);
4623 if (Size > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004624 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004625 else if (Size < 128) {
4626 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4627 return ABIArgInfo::getDirect(CoerceTy);
4628 }
4629 }
4630
Ulrich Weigandb7122372014-07-21 00:48:09 +00004631 if (isAggregateTypeForABI(RetTy)) {
4632 // ELFv2 homogeneous aggregates are returned as array types.
4633 const Type *Base = nullptr;
4634 uint64_t Members = 0;
4635 if (Kind == ELFv2 &&
4636 isHomogeneousAggregate(RetTy, Base, Members)) {
4637 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4638 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4639 return ABIArgInfo::getDirect(CoerceTy);
4640 }
4641
4642 // ELFv2 small aggregates are returned in up to two registers.
4643 uint64_t Bits = getContext().getTypeSize(RetTy);
4644 if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
4645 if (Bits == 0)
4646 return ABIArgInfo::getIgnore();
4647
4648 llvm::Type *CoerceTy;
4649 if (Bits > GPRBits) {
4650 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
Serge Guelton1d993272017-05-09 19:31:30 +00004651 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
Ulrich Weigandb7122372014-07-21 00:48:09 +00004652 } else
Rui Ueyama83aa9792016-01-14 21:00:27 +00004653 CoerceTy =
4654 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Ulrich Weigandb7122372014-07-21 00:48:09 +00004655 return ABIArgInfo::getDirect(CoerceTy);
4656 }
4657
4658 // All other aggregates are returned indirectly.
John McCall7f416cc2015-09-08 08:05:57 +00004659 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandb7122372014-07-21 00:48:09 +00004660 }
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004661
4662 return (isPromotableTypeForABI(RetTy) ?
4663 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4664}
4665
Bill Schmidt25cb3492012-10-03 19:18:57 +00004666// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
John McCall7f416cc2015-09-08 08:05:57 +00004667Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4668 QualType Ty) const {
4669 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4670 TypeInfo.second = getParamTypeAlignment(Ty);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004671
John McCall7f416cc2015-09-08 08:05:57 +00004672 CharUnits SlotSize = CharUnits::fromQuantity(8);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004673
Bill Schmidt924c4782013-01-14 17:45:36 +00004674 // If we have a complex type and the base type is smaller than 8 bytes,
4675 // the ABI calls for the real and imaginary parts to be right-adjusted
4676 // in separate doublewords. However, Clang expects us to produce a
4677 // pointer to a structure with the two parts packed tightly. So generate
4678 // loads of the real and imaginary parts relative to the va_list pointer,
4679 // and store them to a temporary structure.
John McCall7f416cc2015-09-08 08:05:57 +00004680 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4681 CharUnits EltSize = TypeInfo.first / 2;
4682 if (EltSize < SlotSize) {
4683 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
4684 SlotSize * 2, SlotSize,
4685 SlotSize, /*AllowHigher*/ true);
4686
4687 Address RealAddr = Addr;
4688 Address ImagAddr = RealAddr;
4689 if (CGF.CGM.getDataLayout().isBigEndian()) {
4690 RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
4691 SlotSize - EltSize);
4692 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
4693 2 * SlotSize - EltSize);
4694 } else {
4695 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
4696 }
4697
4698 llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
4699 RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
4700 ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
4701 llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
4702 llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
4703
4704 Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
4705 CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
4706 /*init*/ true);
4707 return Temp;
Ulrich Weigandbebc55b2014-06-20 16:37:40 +00004708 }
Bill Schmidt924c4782013-01-14 17:45:36 +00004709 }
4710
John McCall7f416cc2015-09-08 08:05:57 +00004711 // Otherwise, just use the general rule.
4712 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
4713 TypeInfo, SlotSize, /*AllowHigher*/ true);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004714}
4715
4716static bool
4717PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4718 llvm::Value *Address) {
Roman Divackyd966e722012-05-09 18:22:46 +00004719 // This is calculated from the LLVM and GCC tables and verified
4720 // against gcc output. AFAIK all ABIs use the same encoding.
4721
4722 CodeGen::CGBuilderTy &Builder = CGF.Builder;
4723
4724 llvm::IntegerType *i8 = CGF.Int8Ty;
4725 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4726 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4727 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4728
4729 // 0-31: r0-31, the 8-byte general-purpose registers
4730 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
4731
4732 // 32-63: fp0-31, the 8-byte floating-point registers
4733 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4734
Hal Finkel84832a72016-08-30 02:38:34 +00004735 // 64-67 are various 8-byte special-purpose registers:
Roman Divackyd966e722012-05-09 18:22:46 +00004736 // 64: mq
4737 // 65: lr
4738 // 66: ctr
4739 // 67: ap
Hal Finkel84832a72016-08-30 02:38:34 +00004740 AssignToArrayRange(Builder, Address, Eight8, 64, 67);
4741
4742 // 68-76 are various 4-byte special-purpose registers:
Roman Divackyd966e722012-05-09 18:22:46 +00004743 // 68-75 cr0-7
4744 // 76: xer
Hal Finkel84832a72016-08-30 02:38:34 +00004745 AssignToArrayRange(Builder, Address, Four8, 68, 76);
Roman Divackyd966e722012-05-09 18:22:46 +00004746
4747 // 77-108: v0-31, the 16-byte vector registers
4748 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4749
4750 // 109: vrsave
4751 // 110: vscr
4752 // 111: spe_acc
4753 // 112: spefscr
4754 // 113: sfp
Hal Finkel84832a72016-08-30 02:38:34 +00004755 // 114: tfhar
4756 // 115: tfiar
4757 // 116: texasr
4758 AssignToArrayRange(Builder, Address, Eight8, 109, 116);
Roman Divackyd966e722012-05-09 18:22:46 +00004759
4760 return false;
4761}
John McCallea8d8bb2010-03-11 00:10:12 +00004762
Bill Schmidt25cb3492012-10-03 19:18:57 +00004763bool
4764PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
4765 CodeGen::CodeGenFunction &CGF,
4766 llvm::Value *Address) const {
4767
4768 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4769}
4770
4771bool
4772PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4773 llvm::Value *Address) const {
4774
4775 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4776}
4777
Chris Lattner0cf24192010-06-28 20:05:43 +00004778//===----------------------------------------------------------------------===//
Tim Northover573cbee2014-05-24 12:52:07 +00004779// AArch64 ABI Implementation
Tim Northovera2ee4332014-03-29 15:09:45 +00004780//===----------------------------------------------------------------------===//
4781
4782namespace {
4783
John McCall12f23522016-04-04 18:33:08 +00004784class AArch64ABIInfo : public SwiftABIInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00004785public:
4786 enum ABIKind {
4787 AAPCS = 0,
4788 DarwinPCS
4789 };
4790
4791private:
4792 ABIKind Kind;
4793
4794public:
John McCall12f23522016-04-04 18:33:08 +00004795 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
4796 : SwiftABIInfo(CGT), Kind(Kind) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00004797
4798private:
4799 ABIKind getABIKind() const { return Kind; }
4800 bool isDarwinPCS() const { return Kind == DarwinPCS; }
4801
4802 ABIArgInfo classifyReturnType(QualType RetTy) const;
Tim Northoverb047bfa2014-11-27 21:02:49 +00004803 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004804 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4805 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4806 uint64_t Members) const override;
4807
Tim Northovera2ee4332014-03-29 15:09:45 +00004808 bool isIllegalVectorType(QualType Ty) const;
4809
David Blaikie1cbb9712014-11-14 19:09:44 +00004810 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00004811 if (!getCXXABI().classifyReturnType(FI))
4812 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Tim Northover5ffc0922014-04-17 10:20:38 +00004813
Tim Northoverb047bfa2014-11-27 21:02:49 +00004814 for (auto &it : FI.arguments())
4815 it.info = classifyArgumentType(it.type);
Tim Northovera2ee4332014-03-29 15:09:45 +00004816 }
4817
John McCall7f416cc2015-09-08 08:05:57 +00004818 Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4819 CodeGenFunction &CGF) const;
Tim Northovera2ee4332014-03-29 15:09:45 +00004820
John McCall7f416cc2015-09-08 08:05:57 +00004821 Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
4822 CodeGenFunction &CGF) const;
Tim Northovera2ee4332014-03-29 15:09:45 +00004823
John McCall7f416cc2015-09-08 08:05:57 +00004824 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4825 QualType Ty) const override {
Tim Northovera2ee4332014-03-29 15:09:45 +00004826 return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
4827 : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
4828 }
John McCall12f23522016-04-04 18:33:08 +00004829
4830 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
4831 ArrayRef<llvm::Type*> scalars,
4832 bool asReturnValue) const override {
4833 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4834 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00004835 bool isSwiftErrorInRegister() const override {
4836 return true;
4837 }
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00004838
4839 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
4840 unsigned elts) const override;
Tim Northovera2ee4332014-03-29 15:09:45 +00004841};
4842
Tim Northover573cbee2014-05-24 12:52:07 +00004843class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00004844public:
Tim Northover573cbee2014-05-24 12:52:07 +00004845 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
4846 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00004847
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004848 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
Saleem Abdulrasool40db4772017-02-11 23:03:13 +00004849 return "mov\tfp, fp\t\t# marker for objc_retainAutoreleaseReturnValue";
Tim Northovera2ee4332014-03-29 15:09:45 +00004850 }
4851
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004852 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4853 return 31;
4854 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004855
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004856 bool doesReturnSlotInterfereWithArgs() const override { return false; }
Tim Northovera2ee4332014-03-29 15:09:45 +00004857};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004858}
Tim Northovera2ee4332014-03-29 15:09:45 +00004859
Tim Northoverb047bfa2014-11-27 21:02:49 +00004860ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00004861 Ty = useFirstFieldIfTransparentUnion(Ty);
4862
Tim Northovera2ee4332014-03-29 15:09:45 +00004863 // Handle illegal vector types here.
4864 if (isIllegalVectorType(Ty)) {
4865 uint64_t Size = getContext().getTypeSize(Ty);
Nirav Dave9a8f97e2016-02-22 16:48:42 +00004866 // Android promotes <2 x i8> to i16, not i32
Ahmed Bougacha8862cae2016-04-19 17:54:24 +00004867 if (isAndroid() && (Size <= 16)) {
Nirav Dave9a8f97e2016-02-22 16:48:42 +00004868 llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
4869 return ABIArgInfo::getDirect(ResType);
4870 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004871 if (Size <= 32) {
4872 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
Tim Northovera2ee4332014-03-29 15:09:45 +00004873 return ABIArgInfo::getDirect(ResType);
4874 }
4875 if (Size == 64) {
4876 llvm::Type *ResType =
4877 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
Tim Northovera2ee4332014-03-29 15:09:45 +00004878 return ABIArgInfo::getDirect(ResType);
4879 }
4880 if (Size == 128) {
4881 llvm::Type *ResType =
4882 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
Tim Northovera2ee4332014-03-29 15:09:45 +00004883 return ABIArgInfo::getDirect(ResType);
4884 }
John McCall7f416cc2015-09-08 08:05:57 +00004885 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Tim Northovera2ee4332014-03-29 15:09:45 +00004886 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004887
4888 if (!isAggregateTypeForABI(Ty)) {
4889 // Treat an enum type as its underlying type.
4890 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4891 Ty = EnumTy->getDecl()->getIntegerType();
4892
Tim Northovera2ee4332014-03-29 15:09:45 +00004893 return (Ty->isPromotableIntegerType() && isDarwinPCS()
4894 ? ABIArgInfo::getExtend()
4895 : ABIArgInfo::getDirect());
4896 }
4897
4898 // Structures with either a non-trivial destructor or a non-trivial
4899 // copy constructor are always indirect.
Reid Kleckner40ca9132014-05-13 22:05:45 +00004900 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
John McCall7f416cc2015-09-08 08:05:57 +00004901 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
4902 CGCXXABI::RAA_DirectInMemory);
Tim Northovera2ee4332014-03-29 15:09:45 +00004903 }
4904
4905 // Empty records are always ignored on Darwin, but actually passed in C++ mode
4906 // elsewhere for GNU compatibility.
Tim Northover23bcad22017-05-05 22:36:06 +00004907 uint64_t Size = getContext().getTypeSize(Ty);
4908 bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
4909 if (IsEmpty || Size == 0) {
Tim Northovera2ee4332014-03-29 15:09:45 +00004910 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
4911 return ABIArgInfo::getIgnore();
4912
Tim Northover23bcad22017-05-05 22:36:06 +00004913 // GNU C mode. The only argument that gets ignored is an empty one with size
4914 // 0.
4915 if (IsEmpty && Size == 0)
4916 return ABIArgInfo::getIgnore();
Tim Northovera2ee4332014-03-29 15:09:45 +00004917 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4918 }
4919
4920 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
Craig Topper8a13c412014-05-21 05:09:00 +00004921 const Type *Base = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004922 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004923 if (isHomogeneousAggregate(Ty, Base, Members)) {
Tim Northoverb047bfa2014-11-27 21:02:49 +00004924 return ABIArgInfo::getDirect(
4925 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
Tim Northovera2ee4332014-03-29 15:09:45 +00004926 }
4927
4928 // Aggregates <= 16 bytes are passed directly in registers or on the stack.
Tim Northovera2ee4332014-03-29 15:09:45 +00004929 if (Size <= 128) {
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00004930 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
4931 // same size and alignment.
4932 if (getTarget().isRenderScriptTarget()) {
4933 return coerceToIntArray(Ty, getContext(), getVMContext());
4934 }
Tim Northoverc801b4a2014-04-15 14:55:11 +00004935 unsigned Alignment = getContext().getTypeAlign(Ty);
Davide Italiano7a3b69d2017-04-03 16:51:39 +00004936 Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
Tim Northoverb047bfa2014-11-27 21:02:49 +00004937
Tim Northovera2ee4332014-03-29 15:09:45 +00004938 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4939 // For aggregates with 16-byte alignment, we use i128.
Tim Northoverc801b4a2014-04-15 14:55:11 +00004940 if (Alignment < 128 && Size == 128) {
Tim Northovera2ee4332014-03-29 15:09:45 +00004941 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4942 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4943 }
4944 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4945 }
4946
John McCall7f416cc2015-09-08 08:05:57 +00004947 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Tim Northovera2ee4332014-03-29 15:09:45 +00004948}
4949
Tim Northover573cbee2014-05-24 12:52:07 +00004950ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00004951 if (RetTy->isVoidType())
4952 return ABIArgInfo::getIgnore();
4953
4954 // Large vector types should be returned via memory.
4955 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004956 return getNaturalAlignIndirect(RetTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00004957
4958 if (!isAggregateTypeForABI(RetTy)) {
4959 // Treat an enum type as its underlying type.
4960 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4961 RetTy = EnumTy->getDecl()->getIntegerType();
4962
Tim Northover4dab6982014-04-18 13:46:08 +00004963 return (RetTy->isPromotableIntegerType() && isDarwinPCS()
4964 ? ABIArgInfo::getExtend()
4965 : ABIArgInfo::getDirect());
Tim Northovera2ee4332014-03-29 15:09:45 +00004966 }
4967
Tim Northover23bcad22017-05-05 22:36:06 +00004968 uint64_t Size = getContext().getTypeSize(RetTy);
4969 if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
Tim Northovera2ee4332014-03-29 15:09:45 +00004970 return ABIArgInfo::getIgnore();
4971
Craig Topper8a13c412014-05-21 05:09:00 +00004972 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004973 uint64_t Members = 0;
4974 if (isHomogeneousAggregate(RetTy, Base, Members))
Tim Northovera2ee4332014-03-29 15:09:45 +00004975 // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
4976 return ABIArgInfo::getDirect();
4977
4978 // Aggregates <= 16 bytes are returned directly in registers or on the stack.
Tim Northovera2ee4332014-03-29 15:09:45 +00004979 if (Size <= 128) {
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00004980 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
4981 // same size and alignment.
4982 if (getTarget().isRenderScriptTarget()) {
4983 return coerceToIntArray(RetTy, getContext(), getVMContext());
4984 }
Pete Cooper635b5092015-04-17 22:16:24 +00004985 unsigned Alignment = getContext().getTypeAlign(RetTy);
Davide Italiano7a3b69d2017-04-03 16:51:39 +00004986 Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
Pete Cooper635b5092015-04-17 22:16:24 +00004987
4988 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4989 // For aggregates with 16-byte alignment, we use i128.
4990 if (Alignment < 128 && Size == 128) {
4991 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4992 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4993 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004994 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4995 }
4996
John McCall7f416cc2015-09-08 08:05:57 +00004997 return getNaturalAlignIndirect(RetTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00004998}
4999
Tim Northover573cbee2014-05-24 12:52:07 +00005000/// isIllegalVectorType - check whether the vector type is legal for AArch64.
5001bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00005002 if (const VectorType *VT = Ty->getAs<VectorType>()) {
5003 // Check whether VT is legal.
5004 unsigned NumElements = VT->getNumElements();
5005 uint64_t Size = getContext().getTypeSize(VT);
Tim Northover34fd4fb2016-05-03 19:24:47 +00005006 // NumElements should be power of 2.
Tim Northover360d2b32016-05-03 19:22:41 +00005007 if (!llvm::isPowerOf2_32(NumElements))
Tim Northovera2ee4332014-03-29 15:09:45 +00005008 return true;
5009 return Size != 64 && (Size != 128 || NumElements == 1);
5010 }
5011 return false;
5012}
5013
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00005014bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
5015 llvm::Type *eltTy,
5016 unsigned elts) const {
5017 if (!llvm::isPowerOf2_32(elts))
5018 return false;
5019 if (totalSize.getQuantity() != 8 &&
5020 (totalSize.getQuantity() != 16 || elts == 1))
5021 return false;
5022 return true;
5023}
5024
Reid Klecknere9f6a712014-10-31 17:10:41 +00005025bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5026 // Homogeneous aggregates for AAPCS64 must have base types of a floating
5027 // point type or a short-vector type. This is the same as the 32-bit ABI,
5028 // but with the difference that any floating-point type is allowed,
5029 // including __fp16.
5030 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5031 if (BT->isFloatingPoint())
5032 return true;
5033 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5034 unsigned VecSize = getContext().getTypeSize(VT);
5035 if (VecSize == 64 || VecSize == 128)
5036 return true;
5037 }
5038 return false;
5039}
5040
5041bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5042 uint64_t Members) const {
5043 return Members <= 4;
5044}
5045
John McCall7f416cc2015-09-08 08:05:57 +00005046Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
Tim Northoverb047bfa2014-11-27 21:02:49 +00005047 QualType Ty,
5048 CodeGenFunction &CGF) const {
5049 ABIArgInfo AI = classifyArgumentType(Ty);
Reid Klecknere9f6a712014-10-31 17:10:41 +00005050 bool IsIndirect = AI.isIndirect();
5051
Tim Northoverb047bfa2014-11-27 21:02:49 +00005052 llvm::Type *BaseTy = CGF.ConvertType(Ty);
5053 if (IsIndirect)
5054 BaseTy = llvm::PointerType::getUnqual(BaseTy);
5055 else if (AI.getCoerceToType())
5056 BaseTy = AI.getCoerceToType();
5057
5058 unsigned NumRegs = 1;
5059 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
5060 BaseTy = ArrTy->getElementType();
5061 NumRegs = ArrTy->getNumElements();
5062 }
5063 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
5064
Tim Northovera2ee4332014-03-29 15:09:45 +00005065 // The AArch64 va_list type and handling is specified in the Procedure Call
5066 // Standard, section B.4:
5067 //
5068 // struct {
5069 // void *__stack;
5070 // void *__gr_top;
5071 // void *__vr_top;
5072 // int __gr_offs;
5073 // int __vr_offs;
5074 // };
5075
5076 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
5077 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5078 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
5079 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
Tim Northovera2ee4332014-03-29 15:09:45 +00005080
John McCall7f416cc2015-09-08 08:05:57 +00005081 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5082 CharUnits TyAlign = TyInfo.second;
5083
5084 Address reg_offs_p = Address::invalid();
5085 llvm::Value *reg_offs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005086 int reg_top_index;
John McCall7f416cc2015-09-08 08:05:57 +00005087 CharUnits reg_top_offset;
5088 int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity();
Tim Northoverb047bfa2014-11-27 21:02:49 +00005089 if (!IsFPR) {
Tim Northovera2ee4332014-03-29 15:09:45 +00005090 // 3 is the field number of __gr_offs
David Blaikie2e804282015-04-05 22:47:07 +00005091 reg_offs_p =
John McCall7f416cc2015-09-08 08:05:57 +00005092 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
5093 "gr_offs_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00005094 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
5095 reg_top_index = 1; // field number for __gr_top
John McCall7f416cc2015-09-08 08:05:57 +00005096 reg_top_offset = CharUnits::fromQuantity(8);
Rui Ueyama83aa9792016-01-14 21:00:27 +00005097 RegSize = llvm::alignTo(RegSize, 8);
Tim Northovera2ee4332014-03-29 15:09:45 +00005098 } else {
Tim Northovera2ee4332014-03-29 15:09:45 +00005099 // 4 is the field number of __vr_offs.
David Blaikie2e804282015-04-05 22:47:07 +00005100 reg_offs_p =
John McCall7f416cc2015-09-08 08:05:57 +00005101 CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28),
5102 "vr_offs_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00005103 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
5104 reg_top_index = 2; // field number for __vr_top
John McCall7f416cc2015-09-08 08:05:57 +00005105 reg_top_offset = CharUnits::fromQuantity(16);
Tim Northoverb047bfa2014-11-27 21:02:49 +00005106 RegSize = 16 * NumRegs;
Tim Northovera2ee4332014-03-29 15:09:45 +00005107 }
5108
5109 //=======================================
5110 // Find out where argument was passed
5111 //=======================================
5112
5113 // If reg_offs >= 0 we're already using the stack for this type of
5114 // argument. We don't want to keep updating reg_offs (in case it overflows,
5115 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
5116 // whatever they get).
Craig Topper8a13c412014-05-21 05:09:00 +00005117 llvm::Value *UsingStack = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005118 UsingStack = CGF.Builder.CreateICmpSGE(
5119 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
5120
5121 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
5122
5123 // Otherwise, at least some kind of argument could go in these registers, the
Bob Wilson3abf1692014-04-21 01:23:36 +00005124 // question is whether this particular type is too big.
Tim Northovera2ee4332014-03-29 15:09:45 +00005125 CGF.EmitBlock(MaybeRegBlock);
5126
5127 // Integer arguments may need to correct register alignment (for example a
5128 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
5129 // align __gr_offs to calculate the potential address.
John McCall7f416cc2015-09-08 08:05:57 +00005130 if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
5131 int Align = TyAlign.getQuantity();
Tim Northovera2ee4332014-03-29 15:09:45 +00005132
5133 reg_offs = CGF.Builder.CreateAdd(
5134 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
5135 "align_regoffs");
5136 reg_offs = CGF.Builder.CreateAnd(
5137 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
5138 "aligned_regoffs");
5139 }
5140
5141 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
John McCall7f416cc2015-09-08 08:05:57 +00005142 // The fact that this is done unconditionally reflects the fact that
5143 // allocating an argument to the stack also uses up all the remaining
5144 // registers of the appropriate kind.
Craig Topper8a13c412014-05-21 05:09:00 +00005145 llvm::Value *NewOffset = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005146 NewOffset = CGF.Builder.CreateAdd(
5147 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
5148 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
5149
5150 // Now we're in a position to decide whether this argument really was in
5151 // registers or not.
Craig Topper8a13c412014-05-21 05:09:00 +00005152 llvm::Value *InRegs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005153 InRegs = CGF.Builder.CreateICmpSLE(
5154 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
5155
5156 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
5157
5158 //=======================================
5159 // Argument was in registers
5160 //=======================================
5161
5162 // Now we emit the code for if the argument was originally passed in
5163 // registers. First start the appropriate block:
5164 CGF.EmitBlock(InRegBlock);
5165
John McCall7f416cc2015-09-08 08:05:57 +00005166 llvm::Value *reg_top = nullptr;
5167 Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index,
5168 reg_top_offset, "reg_top_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00005169 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
John McCall7f416cc2015-09-08 08:05:57 +00005170 Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
5171 CharUnits::fromQuantity(IsFPR ? 16 : 8));
5172 Address RegAddr = Address::invalid();
5173 llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00005174
5175 if (IsIndirect) {
5176 // If it's been passed indirectly (actually a struct), whatever we find from
5177 // stored registers or on the stack will actually be a struct **.
5178 MemTy = llvm::PointerType::getUnqual(MemTy);
5179 }
5180
Craig Topper8a13c412014-05-21 05:09:00 +00005181 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005182 uint64_t NumMembers = 0;
5183 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
James Molloy467be602014-05-07 14:45:55 +00005184 if (IsHFA && NumMembers > 1) {
Tim Northovera2ee4332014-03-29 15:09:45 +00005185 // Homogeneous aggregates passed in registers will have their elements split
5186 // and stored 16-bytes apart regardless of size (they're notionally in qN,
5187 // qN+1, ...). We reload and store into a temporary local variable
5188 // contiguously.
5189 assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
John McCall7f416cc2015-09-08 08:05:57 +00005190 auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
Tim Northovera2ee4332014-03-29 15:09:45 +00005191 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
5192 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
John McCall7f416cc2015-09-08 08:05:57 +00005193 Address Tmp = CGF.CreateTempAlloca(HFATy,
5194 std::max(TyAlign, BaseTyInfo.second));
Tim Northovera2ee4332014-03-29 15:09:45 +00005195
John McCall7f416cc2015-09-08 08:05:57 +00005196 // On big-endian platforms, the value will be right-aligned in its slot.
5197 int Offset = 0;
5198 if (CGF.CGM.getDataLayout().isBigEndian() &&
5199 BaseTyInfo.first.getQuantity() < 16)
5200 Offset = 16 - BaseTyInfo.first.getQuantity();
5201
Tim Northovera2ee4332014-03-29 15:09:45 +00005202 for (unsigned i = 0; i < NumMembers; ++i) {
John McCall7f416cc2015-09-08 08:05:57 +00005203 CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
5204 Address LoadAddr =
5205 CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
5206 LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
5207
5208 Address StoreAddr =
5209 CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first);
Tim Northovera2ee4332014-03-29 15:09:45 +00005210
5211 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
5212 CGF.Builder.CreateStore(Elem, StoreAddr);
5213 }
5214
John McCall7f416cc2015-09-08 08:05:57 +00005215 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005216 } else {
John McCall7f416cc2015-09-08 08:05:57 +00005217 // Otherwise the object is contiguous in memory.
5218
5219 // It might be right-aligned in its slot.
5220 CharUnits SlotSize = BaseAddr.getAlignment();
5221 if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
James Molloy467be602014-05-07 14:45:55 +00005222 (IsHFA || !isAggregateTypeForABI(Ty)) &&
John McCall7f416cc2015-09-08 08:05:57 +00005223 TyInfo.first < SlotSize) {
5224 CharUnits Offset = SlotSize - TyInfo.first;
5225 BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
Tim Northovera2ee4332014-03-29 15:09:45 +00005226 }
5227
John McCall7f416cc2015-09-08 08:05:57 +00005228 RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005229 }
5230
5231 CGF.EmitBranch(ContBlock);
5232
5233 //=======================================
5234 // Argument was on the stack
5235 //=======================================
5236 CGF.EmitBlock(OnStackBlock);
5237
John McCall7f416cc2015-09-08 08:05:57 +00005238 Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0,
5239 CharUnits::Zero(), "stack_p");
5240 llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
Tim Northovera2ee4332014-03-29 15:09:45 +00005241
John McCall7f416cc2015-09-08 08:05:57 +00005242 // Again, stack arguments may need realignment. In this case both integer and
Tim Northovera2ee4332014-03-29 15:09:45 +00005243 // floating-point ones might be affected.
John McCall7f416cc2015-09-08 08:05:57 +00005244 if (!IsIndirect && TyAlign.getQuantity() > 8) {
5245 int Align = TyAlign.getQuantity();
Tim Northovera2ee4332014-03-29 15:09:45 +00005246
John McCall7f416cc2015-09-08 08:05:57 +00005247 OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00005248
John McCall7f416cc2015-09-08 08:05:57 +00005249 OnStackPtr = CGF.Builder.CreateAdd(
5250 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
Tim Northovera2ee4332014-03-29 15:09:45 +00005251 "align_stack");
John McCall7f416cc2015-09-08 08:05:57 +00005252 OnStackPtr = CGF.Builder.CreateAnd(
5253 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
Tim Northovera2ee4332014-03-29 15:09:45 +00005254 "align_stack");
5255
John McCall7f416cc2015-09-08 08:05:57 +00005256 OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005257 }
John McCall7f416cc2015-09-08 08:05:57 +00005258 Address OnStackAddr(OnStackPtr,
5259 std::max(CharUnits::fromQuantity(8), TyAlign));
Tim Northovera2ee4332014-03-29 15:09:45 +00005260
John McCall7f416cc2015-09-08 08:05:57 +00005261 // All stack slots are multiples of 8 bytes.
5262 CharUnits StackSlotSize = CharUnits::fromQuantity(8);
5263 CharUnits StackSize;
Tim Northovera2ee4332014-03-29 15:09:45 +00005264 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00005265 StackSize = StackSlotSize;
Tim Northovera2ee4332014-03-29 15:09:45 +00005266 else
Rui Ueyama83aa9792016-01-14 21:00:27 +00005267 StackSize = TyInfo.first.alignTo(StackSlotSize);
Tim Northovera2ee4332014-03-29 15:09:45 +00005268
John McCall7f416cc2015-09-08 08:05:57 +00005269 llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
Tim Northovera2ee4332014-03-29 15:09:45 +00005270 llvm::Value *NewStack =
John McCall7f416cc2015-09-08 08:05:57 +00005271 CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
Tim Northovera2ee4332014-03-29 15:09:45 +00005272
5273 // Write the new value of __stack for the next call to va_arg
5274 CGF.Builder.CreateStore(NewStack, stack_p);
5275
5276 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
John McCall7f416cc2015-09-08 08:05:57 +00005277 TyInfo.first < StackSlotSize) {
5278 CharUnits Offset = StackSlotSize - TyInfo.first;
5279 OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
Tim Northovera2ee4332014-03-29 15:09:45 +00005280 }
5281
John McCall7f416cc2015-09-08 08:05:57 +00005282 OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005283
5284 CGF.EmitBranch(ContBlock);
5285
5286 //=======================================
5287 // Tidy up
5288 //=======================================
5289 CGF.EmitBlock(ContBlock);
5290
John McCall7f416cc2015-09-08 08:05:57 +00005291 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
5292 OnStackAddr, OnStackBlock, "vaargs.addr");
Tim Northovera2ee4332014-03-29 15:09:45 +00005293
5294 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00005295 return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
5296 TyInfo.second);
Tim Northovera2ee4332014-03-29 15:09:45 +00005297
5298 return ResAddr;
5299}
5300
John McCall7f416cc2015-09-08 08:05:57 +00005301Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5302 CodeGenFunction &CGF) const {
5303 // The backend's lowering doesn't support va_arg for aggregates or
5304 // illegal vector types. Lower VAArg here for these cases and use
5305 // the LLVM va_arg instruction for everything else.
Tim Northovera2ee4332014-03-29 15:09:45 +00005306 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
James Y Knight29b5f082016-02-24 02:59:33 +00005307 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Tim Northovera2ee4332014-03-29 15:09:45 +00005308
John McCall7f416cc2015-09-08 08:05:57 +00005309 CharUnits SlotSize = CharUnits::fromQuantity(8);
Tim Northovera2ee4332014-03-29 15:09:45 +00005310
John McCall7f416cc2015-09-08 08:05:57 +00005311 // Empty records are ignored for parameter passing purposes.
Tim Northovera2ee4332014-03-29 15:09:45 +00005312 if (isEmptyRecord(getContext(), Ty, true)) {
John McCall7f416cc2015-09-08 08:05:57 +00005313 Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
5314 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5315 return Addr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005316 }
5317
John McCall7f416cc2015-09-08 08:05:57 +00005318 // The size of the actual thing passed, which might end up just
5319 // being a pointer for indirect types.
5320 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5321
5322 // Arguments bigger than 16 bytes which aren't homogeneous
5323 // aggregates should be passed indirectly.
5324 bool IsIndirect = false;
5325 if (TyInfo.first.getQuantity() > 16) {
5326 const Type *Base = nullptr;
5327 uint64_t Members = 0;
5328 IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
Tim Northovera2ee4332014-03-29 15:09:45 +00005329 }
5330
John McCall7f416cc2015-09-08 08:05:57 +00005331 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
5332 TyInfo, SlotSize, /*AllowHigherAlign*/ true);
Tim Northovera2ee4332014-03-29 15:09:45 +00005333}
5334
5335//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00005336// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00005337//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00005338
5339namespace {
5340
John McCall12f23522016-04-04 18:33:08 +00005341class ARMABIInfo : public SwiftABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00005342public:
5343 enum ABIKind {
5344 APCS = 0,
5345 AAPCS = 1,
Tim Northover5627d392015-10-30 16:30:45 +00005346 AAPCS_VFP = 2,
5347 AAPCS16_VFP = 3,
Daniel Dunbar020daa92009-09-12 01:00:39 +00005348 };
5349
5350private:
5351 ABIKind Kind;
5352
5353public:
John McCall12f23522016-04-04 18:33:08 +00005354 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
5355 : SwiftABIInfo(CGT), Kind(_Kind) {
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005356 setCCs();
John McCall882987f2013-02-28 19:01:20 +00005357 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00005358
John McCall3480ef22011-08-30 01:42:09 +00005359 bool isEABI() const {
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00005360 switch (getTarget().getTriple().getEnvironment()) {
5361 case llvm::Triple::Android:
5362 case llvm::Triple::EABI:
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00005363 case llvm::Triple::EABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00005364 case llvm::Triple::GNUEABI:
Joerg Sonnenberger0c1652d2013-12-16 18:30:28 +00005365 case llvm::Triple::GNUEABIHF:
Rafael Espindola0fa66802016-06-24 21:35:06 +00005366 case llvm::Triple::MuslEABI:
5367 case llvm::Triple::MuslEABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00005368 return true;
5369 default:
5370 return false;
5371 }
John McCall3480ef22011-08-30 01:42:09 +00005372 }
5373
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00005374 bool isEABIHF() const {
5375 switch (getTarget().getTriple().getEnvironment()) {
5376 case llvm::Triple::EABIHF:
5377 case llvm::Triple::GNUEABIHF:
Rafael Espindola0fa66802016-06-24 21:35:06 +00005378 case llvm::Triple::MuslEABIHF:
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00005379 return true;
5380 default:
5381 return false;
5382 }
5383 }
5384
Daniel Dunbar020daa92009-09-12 01:00:39 +00005385 ABIKind getABIKind() const { return Kind; }
5386
Tim Northovera484bc02013-10-01 14:34:25 +00005387private:
Amara Emerson9dc78782014-01-28 10:56:36 +00005388 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
Tim Northoverbc784d12015-02-24 17:22:40 +00005389 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
Manman Renfef9e312012-10-16 19:18:39 +00005390 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005391
Reid Klecknere9f6a712014-10-31 17:10:41 +00005392 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5393 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5394 uint64_t Members) const override;
5395
Craig Topper4f12f102014-03-12 06:41:41 +00005396 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005397
John McCall7f416cc2015-09-08 08:05:57 +00005398 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5399 QualType Ty) const override;
John McCall882987f2013-02-28 19:01:20 +00005400
5401 llvm::CallingConv::ID getLLVMDefaultCC() const;
5402 llvm::CallingConv::ID getABIDefaultCC() const;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005403 void setCCs();
John McCall12f23522016-04-04 18:33:08 +00005404
5405 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
5406 ArrayRef<llvm::Type*> scalars,
5407 bool asReturnValue) const override {
5408 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5409 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00005410 bool isSwiftErrorInRegister() const override {
5411 return true;
5412 }
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00005413 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5414 unsigned elts) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005415};
5416
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005417class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
5418public:
Chris Lattner2b037972010-07-29 02:01:43 +00005419 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5420 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00005421
John McCall3480ef22011-08-30 01:42:09 +00005422 const ARMABIInfo &getABIInfo() const {
5423 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
5424 }
5425
Craig Topper4f12f102014-03-12 06:41:41 +00005426 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallbeec5a02010-03-06 00:35:14 +00005427 return 13;
5428 }
Roman Divackyc1617352011-05-18 19:36:54 +00005429
Craig Topper4f12f102014-03-12 06:41:41 +00005430 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
John McCall31168b02011-06-15 23:02:42 +00005431 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
5432 }
5433
Roman Divackyc1617352011-05-18 19:36:54 +00005434 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00005435 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00005436 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divackyc1617352011-05-18 19:36:54 +00005437
5438 // 0-15 are the 16 integer registers.
Chris Lattnerece04092012-02-07 00:39:47 +00005439 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divackyc1617352011-05-18 19:36:54 +00005440 return false;
5441 }
John McCall3480ef22011-08-30 01:42:09 +00005442
Craig Topper4f12f102014-03-12 06:41:41 +00005443 unsigned getSizeOfUnwindException() const override {
John McCall3480ef22011-08-30 01:42:09 +00005444 if (getABIInfo().isEABI()) return 88;
5445 return TargetCodeGenInfo::getSizeOfUnwindException();
5446 }
Tim Northovera484bc02013-10-01 14:34:25 +00005447
Eric Christopher162c91c2015-06-05 22:03:00 +00005448 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00005449 CodeGen::CodeGenModule &CGM) const override {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00005450 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Tim Northovera484bc02013-10-01 14:34:25 +00005451 if (!FD)
5452 return;
5453
5454 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
5455 if (!Attr)
5456 return;
5457
5458 const char *Kind;
5459 switch (Attr->getInterrupt()) {
5460 case ARMInterruptAttr::Generic: Kind = ""; break;
5461 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
5462 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
5463 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
5464 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
5465 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
5466 }
5467
5468 llvm::Function *Fn = cast<llvm::Function>(GV);
5469
5470 Fn->addFnAttr("interrupt", Kind);
5471
Tim Northover5627d392015-10-30 16:30:45 +00005472 ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
5473 if (ABI == ARMABIInfo::APCS)
Tim Northovera484bc02013-10-01 14:34:25 +00005474 return;
5475
5476 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
5477 // however this is not necessarily true on taking any interrupt. Instruct
5478 // the backend to perform a realignment as part of the function prologue.
5479 llvm::AttrBuilder B;
5480 B.addStackAlignmentAttr(8);
Reid Kleckneree4930b2017-05-02 22:07:37 +00005481 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
Tim Northovera484bc02013-10-01 14:34:25 +00005482 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005483};
5484
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005485class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005486public:
5487 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5488 : ARMTargetCodeGenInfo(CGT, K) {}
5489
Eric Christopher162c91c2015-06-05 22:03:00 +00005490 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005491 CodeGen::CodeGenModule &CGM) const override;
Saleem Abdulrasool6e9e88b2016-06-23 13:45:33 +00005492
5493 void getDependentLibraryOption(llvm::StringRef Lib,
5494 llvm::SmallString<24> &Opt) const override {
5495 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5496 }
5497
5498 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5499 llvm::SmallString<32> &Opt) const override {
5500 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5501 }
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005502};
5503
Eric Christopher162c91c2015-06-05 22:03:00 +00005504void WindowsARMTargetCodeGenInfo::setTargetAttributes(
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005505 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
Eric Christopher162c91c2015-06-05 22:03:00 +00005506 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005507 addStackProbeSizeTargetAttribute(D, GV, CGM);
5508}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005509}
Daniel Dunbard59655c2009-09-12 00:59:49 +00005510
Chris Lattner22326a12010-07-29 02:31:05 +00005511void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Tim Northoverbc784d12015-02-24 17:22:40 +00005512 if (!getCXXABI().classifyReturnType(FI))
Eric Christopher7565e0d2015-05-29 23:09:49 +00005513 FI.getReturnInfo() =
5514 classifyReturnType(FI.getReturnType(), FI.isVariadic());
Oliver Stannard405bded2014-02-11 09:25:50 +00005515
Tim Northoverbc784d12015-02-24 17:22:40 +00005516 for (auto &I : FI.arguments())
5517 I.info = classifyArgumentType(I.type, FI.isVariadic());
Daniel Dunbar020daa92009-09-12 01:00:39 +00005518
Anton Korobeynikov231e8752011-04-14 20:06:49 +00005519 // Always honor user-specified calling convention.
5520 if (FI.getCallingConvention() != llvm::CallingConv::C)
5521 return;
5522
John McCall882987f2013-02-28 19:01:20 +00005523 llvm::CallingConv::ID cc = getRuntimeCC();
5524 if (cc != llvm::CallingConv::C)
Tim Northoverbc784d12015-02-24 17:22:40 +00005525 FI.setEffectiveCallingConvention(cc);
John McCall882987f2013-02-28 19:01:20 +00005526}
Rafael Espindolaa92c4422010-06-16 16:13:39 +00005527
John McCall882987f2013-02-28 19:01:20 +00005528/// Return the default calling convention that LLVM will use.
5529llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
5530 // The default calling convention that LLVM will infer.
Tim Northoverd88ecb32016-01-27 19:32:40 +00005531 if (isEABIHF() || getTarget().getTriple().isWatchABI())
John McCall882987f2013-02-28 19:01:20 +00005532 return llvm::CallingConv::ARM_AAPCS_VFP;
5533 else if (isEABI())
5534 return llvm::CallingConv::ARM_AAPCS;
5535 else
5536 return llvm::CallingConv::ARM_APCS;
5537}
5538
5539/// Return the calling convention that our ABI would like us to use
5540/// as the C calling convention.
5541llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar020daa92009-09-12 01:00:39 +00005542 switch (getABIKind()) {
John McCall882987f2013-02-28 19:01:20 +00005543 case APCS: return llvm::CallingConv::ARM_APCS;
5544 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
5545 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Tim Northover5627d392015-10-30 16:30:45 +00005546 case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar020daa92009-09-12 01:00:39 +00005547 }
John McCall882987f2013-02-28 19:01:20 +00005548 llvm_unreachable("bad ABI kind");
5549}
5550
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005551void ARMABIInfo::setCCs() {
John McCall882987f2013-02-28 19:01:20 +00005552 assert(getRuntimeCC() == llvm::CallingConv::C);
5553
5554 // Don't muddy up the IR with a ton of explicit annotations if
5555 // they'd just match what LLVM will infer from the triple.
5556 llvm::CallingConv::ID abiCC = getABIDefaultCC();
5557 if (abiCC != getLLVMDefaultCC())
5558 RuntimeCC = abiCC;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005559
Tim Northover5627d392015-10-30 16:30:45 +00005560 // AAPCS apparently requires runtime support functions to be soft-float, but
5561 // that's almost certainly for historic reasons (Thumb1 not supporting VFP
5562 // most likely). It's more convenient for AAPCS16_VFP to be hard-float.
5563 switch (getABIKind()) {
5564 case APCS:
5565 case AAPCS16_VFP:
5566 if (abiCC != getLLVMDefaultCC())
5567 BuiltinCC = abiCC;
5568 break;
5569 case AAPCS:
5570 case AAPCS_VFP:
5571 BuiltinCC = llvm::CallingConv::ARM_AAPCS;
5572 break;
5573 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005574}
5575
Tim Northoverbc784d12015-02-24 17:22:40 +00005576ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
5577 bool isVariadic) const {
Manman Ren2a523d82012-10-30 23:21:41 +00005578 // 6.1.2.1 The following argument types are VFP CPRCs:
5579 // A single-precision floating-point type (including promoted
5580 // half-precision types); A double-precision floating-point type;
5581 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
5582 // with a Base Type of a single- or double-precision floating-point type,
5583 // 64-bit containerized vectors or 128-bit containerized vectors with one
5584 // to four Elements.
Tim Northover5a1558e2014-11-07 22:30:50 +00005585 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005586
Reid Klecknerb1be6832014-11-15 01:41:41 +00005587 Ty = useFirstFieldIfTransparentUnion(Ty);
5588
Manman Renfef9e312012-10-16 19:18:39 +00005589 // Handle illegal vector types here.
5590 if (isIllegalVectorType(Ty)) {
5591 uint64_t Size = getContext().getTypeSize(Ty);
5592 if (Size <= 32) {
5593 llvm::Type *ResType =
5594 llvm::Type::getInt32Ty(getVMContext());
Tim Northover5a1558e2014-11-07 22:30:50 +00005595 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005596 }
5597 if (Size == 64) {
5598 llvm::Type *ResType = llvm::VectorType::get(
5599 llvm::Type::getInt32Ty(getVMContext()), 2);
Tim Northover5a1558e2014-11-07 22:30:50 +00005600 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005601 }
5602 if (Size == 128) {
5603 llvm::Type *ResType = llvm::VectorType::get(
5604 llvm::Type::getInt32Ty(getVMContext()), 4);
Tim Northover5a1558e2014-11-07 22:30:50 +00005605 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005606 }
John McCall7f416cc2015-09-08 08:05:57 +00005607 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Manman Renfef9e312012-10-16 19:18:39 +00005608 }
5609
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005610 // __fp16 gets passed as if it were an int or float, but with the top 16 bits
5611 // unspecified. This is not done for OpenCL as it handles the half type
5612 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar8e2e9d62016-03-18 16:58:36 +00005613 if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005614 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5615 llvm::Type::getFloatTy(getVMContext()) :
5616 llvm::Type::getInt32Ty(getVMContext());
5617 return ABIArgInfo::getDirect(ResType);
5618 }
5619
John McCalla1dee5302010-08-22 10:59:02 +00005620 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005621 // Treat an enum type as its underlying type.
Oliver Stannard405bded2014-02-11 09:25:50 +00005622 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005623 Ty = EnumTy->getDecl()->getIntegerType();
Oliver Stannard405bded2014-02-11 09:25:50 +00005624 }
Douglas Gregora71cc152010-02-02 20:10:50 +00005625
Tim Northover5a1558e2014-11-07 22:30:50 +00005626 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5627 : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00005628 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005629
Oliver Stannard405bded2014-02-11 09:25:50 +00005630 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
John McCall7f416cc2015-09-08 08:05:57 +00005631 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Oliver Stannard405bded2014-02-11 09:25:50 +00005632 }
Tim Northover1060eae2013-06-21 22:49:34 +00005633
Daniel Dunbar09d33622009-09-14 21:54:03 +00005634 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005635 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00005636 return ABIArgInfo::getIgnore();
5637
Tim Northover5a1558e2014-11-07 22:30:50 +00005638 if (IsEffectivelyAAPCS_VFP) {
Manman Ren2a523d82012-10-30 23:21:41 +00005639 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
5640 // into VFP registers.
Craig Topper8a13c412014-05-21 05:09:00 +00005641 const Type *Base = nullptr;
Manman Ren2a523d82012-10-30 23:21:41 +00005642 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005643 if (isHomogeneousAggregate(Ty, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005644 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Ren2a523d82012-10-30 23:21:41 +00005645 // Base can be a floating-point or a vector.
Tim Northover5a1558e2014-11-07 22:30:50 +00005646 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005647 }
Tim Northover5627d392015-10-30 16:30:45 +00005648 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5649 // WatchOS does have homogeneous aggregates. Note that we intentionally use
5650 // this convention even for a variadic function: the backend will use GPRs
5651 // if needed.
5652 const Type *Base = nullptr;
5653 uint64_t Members = 0;
5654 if (isHomogeneousAggregate(Ty, Base, Members)) {
5655 assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
5656 llvm::Type *Ty =
5657 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
5658 return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5659 }
5660 }
5661
5662 if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5663 getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
5664 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
5665 // bigger than 128-bits, they get placed in space allocated by the caller,
5666 // and a pointer is passed.
5667 return ABIArgInfo::getIndirect(
5668 CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
Bob Wilsone826a2a2011-08-03 05:58:22 +00005669 }
5670
Manman Ren6c30e132012-08-13 21:23:55 +00005671 // Support byval for ARM.
Manman Ren77b02382012-11-06 19:05:29 +00005672 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
5673 // most 8-byte. We realign the indirect argument if type alignment is bigger
5674 // than ABI alignment.
Manman Ren505d68f2012-11-05 22:42:46 +00005675 uint64_t ABIAlign = 4;
5676 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
5677 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
Tim Northoverd157e192015-03-09 21:40:42 +00005678 getABIKind() == ARMABIInfo::AAPCS)
Manman Ren505d68f2012-11-05 22:42:46 +00005679 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Tim Northoverd157e192015-03-09 21:40:42 +00005680
Manman Ren8cd99812012-11-06 04:58:01 +00005681 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
Tim Northover5627d392015-10-30 16:30:45 +00005682 assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
John McCall7f416cc2015-09-08 08:05:57 +00005683 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5684 /*ByVal=*/true,
5685 /*Realign=*/TyAlign > ABIAlign);
Eli Friedmane66abda2012-08-09 00:31:40 +00005686 }
5687
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005688 // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
5689 // same size and alignment.
5690 if (getTarget().isRenderScriptTarget()) {
5691 return coerceToIntArray(Ty, getContext(), getVMContext());
5692 }
5693
Daniel Dunbarb34b0802010-09-23 01:54:28 +00005694 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2192fe52011-07-18 04:24:23 +00005695 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005696 unsigned SizeRegs;
Eli Friedmane66abda2012-08-09 00:31:40 +00005697 // FIXME: Try to match the types of the arguments more accurately where
5698 // we can.
5699 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson8e2b75d2011-08-01 23:39:04 +00005700 ElemTy = llvm::Type::getInt32Ty(getVMContext());
5701 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren6fdb1582012-06-25 22:04:00 +00005702 } else {
Manman Ren6fdb1582012-06-25 22:04:00 +00005703 ElemTy = llvm::Type::getInt64Ty(getVMContext());
5704 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00005705 }
Stuart Hastings4b214952011-04-28 18:16:06 +00005706
Tim Northover5a1558e2014-11-07 22:30:50 +00005707 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005708}
5709
Chris Lattner458b2aa2010-07-29 02:16:43 +00005710static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005711 llvm::LLVMContext &VMContext) {
5712 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
5713 // is called integer-like if its size is less than or equal to one word, and
5714 // the offset of each of its addressable sub-fields is zero.
5715
5716 uint64_t Size = Context.getTypeSize(Ty);
5717
5718 // Check that the type fits in a word.
5719 if (Size > 32)
5720 return false;
5721
5722 // FIXME: Handle vector types!
5723 if (Ty->isVectorType())
5724 return false;
5725
Daniel Dunbard53bac72009-09-14 02:20:34 +00005726 // Float types are never treated as "integer like".
5727 if (Ty->isRealFloatingType())
5728 return false;
5729
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005730 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00005731 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005732 return true;
5733
Daniel Dunbar96ebba52010-02-01 23:31:26 +00005734 // Small complex integer types are "integer like".
5735 if (const ComplexType *CT = Ty->getAs<ComplexType>())
5736 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005737
5738 // Single element and zero sized arrays should be allowed, by the definition
5739 // above, but they are not.
5740
5741 // Otherwise, it must be a record type.
5742 const RecordType *RT = Ty->getAs<RecordType>();
5743 if (!RT) return false;
5744
5745 // Ignore records with flexible arrays.
5746 const RecordDecl *RD = RT->getDecl();
5747 if (RD->hasFlexibleArrayMember())
5748 return false;
5749
5750 // Check that all sub-fields are at offset 0, and are themselves "integer
5751 // like".
5752 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
5753
5754 bool HadField = false;
5755 unsigned idx = 0;
5756 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5757 i != e; ++i, ++idx) {
David Blaikie40ed2972012-06-06 20:45:41 +00005758 const FieldDecl *FD = *i;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005759
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005760 // Bit-fields are not addressable, we only need to verify they are "integer
5761 // like". We still have to disallow a subsequent non-bitfield, for example:
5762 // struct { int : 0; int x }
5763 // is non-integer like according to gcc.
5764 if (FD->isBitField()) {
5765 if (!RD->isUnion())
5766 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005767
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005768 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5769 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005770
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005771 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005772 }
5773
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005774 // Check if this field is at offset 0.
5775 if (Layout.getFieldOffset(idx) != 0)
5776 return false;
5777
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005778 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5779 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00005780
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005781 // Only allow at most one field in a structure. This doesn't match the
5782 // wording above, but follows gcc in situations with a field following an
5783 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005784 if (!RD->isUnion()) {
5785 if (HadField)
5786 return false;
5787
5788 HadField = true;
5789 }
5790 }
5791
5792 return true;
5793}
5794
Oliver Stannard405bded2014-02-11 09:25:50 +00005795ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
5796 bool isVariadic) const {
Tim Northover5627d392015-10-30 16:30:45 +00005797 bool IsEffectivelyAAPCS_VFP =
5798 (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005799
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005800 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005801 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005802
Daniel Dunbar19964db2010-09-23 01:54:32 +00005803 // Large vector types should be returned via memory.
Oliver Stannard405bded2014-02-11 09:25:50 +00005804 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
John McCall7f416cc2015-09-08 08:05:57 +00005805 return getNaturalAlignIndirect(RetTy);
Oliver Stannard405bded2014-02-11 09:25:50 +00005806 }
Daniel Dunbar19964db2010-09-23 01:54:32 +00005807
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005808 // __fp16 gets returned as if it were an int or float, but with the top 16
5809 // bits unspecified. This is not done for OpenCL as it handles the half type
5810 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar8e2e9d62016-03-18 16:58:36 +00005811 if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005812 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5813 llvm::Type::getFloatTy(getVMContext()) :
5814 llvm::Type::getInt32Ty(getVMContext());
5815 return ABIArgInfo::getDirect(ResType);
5816 }
5817
John McCalla1dee5302010-08-22 10:59:02 +00005818 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005819 // Treat an enum type as its underlying type.
5820 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5821 RetTy = EnumTy->getDecl()->getIntegerType();
5822
Tim Northover5a1558e2014-11-07 22:30:50 +00005823 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5824 : ABIArgInfo::getDirect();
Douglas Gregora71cc152010-02-02 20:10:50 +00005825 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005826
5827 // Are we following APCS?
5828 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00005829 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005830 return ABIArgInfo::getIgnore();
5831
Daniel Dunbareedf1512010-02-01 23:31:19 +00005832 // Complex types are all returned as packed integers.
5833 //
5834 // FIXME: Consider using 2 x vector types if the back end handles them
5835 // correctly.
5836 if (RetTy->isAnyComplexType())
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005837 return ABIArgInfo::getDirect(llvm::IntegerType::get(
5838 getVMContext(), getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00005839
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005840 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005841 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005842 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005843 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005844 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00005845 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005846 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00005847 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5848 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005849 }
5850
5851 // Otherwise return in memory.
John McCall7f416cc2015-09-08 08:05:57 +00005852 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005853 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005854
5855 // Otherwise this is an AAPCS variant.
5856
Chris Lattner458b2aa2010-07-29 02:16:43 +00005857 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005858 return ABIArgInfo::getIgnore();
5859
Bob Wilson1d9269a2011-11-02 04:51:36 +00005860 // Check for homogeneous aggregates with AAPCS-VFP.
Tim Northover5a1558e2014-11-07 22:30:50 +00005861 if (IsEffectivelyAAPCS_VFP) {
Craig Topper8a13c412014-05-21 05:09:00 +00005862 const Type *Base = nullptr;
Tim Northover5627d392015-10-30 16:30:45 +00005863 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005864 if (isHomogeneousAggregate(RetTy, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005865 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson1d9269a2011-11-02 04:51:36 +00005866 // Homogeneous Aggregates are returned directly.
Tim Northover5a1558e2014-11-07 22:30:50 +00005867 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005868 }
Bob Wilson1d9269a2011-11-02 04:51:36 +00005869 }
5870
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005871 // Aggregates <= 4 bytes are returned in r0; other aggregates
5872 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005873 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005874 if (Size <= 32) {
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005875 // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
5876 // same size and alignment.
5877 if (getTarget().isRenderScriptTarget()) {
5878 return coerceToIntArray(RetTy, getContext(), getVMContext());
5879 }
Christian Pirkerc3d32172014-07-03 09:28:12 +00005880 if (getDataLayout().isBigEndian())
5881 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
Tim Northover5a1558e2014-11-07 22:30:50 +00005882 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Christian Pirkerc3d32172014-07-03 09:28:12 +00005883
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005884 // Return in the smallest viable integer type.
5885 if (Size <= 8)
Tim Northover5a1558e2014-11-07 22:30:50 +00005886 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005887 if (Size <= 16)
Tim Northover5a1558e2014-11-07 22:30:50 +00005888 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5889 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Tim Northover5627d392015-10-30 16:30:45 +00005890 } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
5891 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
5892 llvm::Type *CoerceTy =
Rui Ueyama83aa9792016-01-14 21:00:27 +00005893 llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
Tim Northover5627d392015-10-30 16:30:45 +00005894 return ABIArgInfo::getDirect(CoerceTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005895 }
5896
John McCall7f416cc2015-09-08 08:05:57 +00005897 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005898}
5899
Manman Renfef9e312012-10-16 19:18:39 +00005900/// isIllegalVector - check whether Ty is an illegal vector type.
5901bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
Stephen Hines8267e7d2015-12-04 01:39:30 +00005902 if (const VectorType *VT = Ty->getAs<VectorType> ()) {
5903 if (isAndroid()) {
5904 // Android shipped using Clang 3.1, which supported a slightly different
5905 // vector ABI. The primary differences were that 3-element vector types
5906 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
5907 // accepts that legacy behavior for Android only.
5908 // Check whether VT is legal.
5909 unsigned NumElements = VT->getNumElements();
5910 // NumElements should be power of 2 or equal to 3.
5911 if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
5912 return true;
5913 } else {
5914 // Check whether VT is legal.
5915 unsigned NumElements = VT->getNumElements();
5916 uint64_t Size = getContext().getTypeSize(VT);
5917 // NumElements should be power of 2.
5918 if (!llvm::isPowerOf2_32(NumElements))
5919 return true;
5920 // Size should be greater than 32 bits.
5921 return Size <= 32;
5922 }
Manman Renfef9e312012-10-16 19:18:39 +00005923 }
5924 return false;
5925}
5926
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00005927bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
5928 llvm::Type *eltTy,
5929 unsigned numElts) const {
5930 if (!llvm::isPowerOf2_32(numElts))
5931 return false;
5932 unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
5933 if (size > 64)
5934 return false;
5935 if (vectorSize.getQuantity() != 8 &&
5936 (vectorSize.getQuantity() != 16 || numElts == 1))
5937 return false;
5938 return true;
5939}
5940
Reid Klecknere9f6a712014-10-31 17:10:41 +00005941bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5942 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
5943 // double, or 64-bit or 128-bit vectors.
5944 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5945 if (BT->getKind() == BuiltinType::Float ||
5946 BT->getKind() == BuiltinType::Double ||
5947 BT->getKind() == BuiltinType::LongDouble)
5948 return true;
5949 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5950 unsigned VecSize = getContext().getTypeSize(VT);
5951 if (VecSize == 64 || VecSize == 128)
5952 return true;
5953 }
5954 return false;
5955}
5956
5957bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5958 uint64_t Members) const {
5959 return Members <= 4;
5960}
5961
John McCall7f416cc2015-09-08 08:05:57 +00005962Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5963 QualType Ty) const {
5964 CharUnits SlotSize = CharUnits::fromQuantity(4);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005965
John McCall7f416cc2015-09-08 08:05:57 +00005966 // Empty records are ignored for parameter passing purposes.
Tim Northover1711cc92013-06-21 23:05:33 +00005967 if (isEmptyRecord(getContext(), Ty, true)) {
John McCall7f416cc2015-09-08 08:05:57 +00005968 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
5969 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5970 return Addr;
Tim Northover1711cc92013-06-21 23:05:33 +00005971 }
5972
John McCall7f416cc2015-09-08 08:05:57 +00005973 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5974 CharUnits TyAlignForABI = TyInfo.second;
Manman Rencca54d02012-10-16 19:01:37 +00005975
John McCall7f416cc2015-09-08 08:05:57 +00005976 // Use indirect if size of the illegal vector is bigger than 16 bytes.
5977 bool IsIndirect = false;
Tim Northover5627d392015-10-30 16:30:45 +00005978 const Type *Base = nullptr;
5979 uint64_t Members = 0;
John McCall7f416cc2015-09-08 08:05:57 +00005980 if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
5981 IsIndirect = true;
5982
Tim Northover5627d392015-10-30 16:30:45 +00005983 // ARMv7k passes structs bigger than 16 bytes indirectly, in space
5984 // allocated by the caller.
5985 } else if (TyInfo.first > CharUnits::fromQuantity(16) &&
5986 getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5987 !isHomogeneousAggregate(Ty, Base, Members)) {
5988 IsIndirect = true;
5989
John McCall7f416cc2015-09-08 08:05:57 +00005990 // Otherwise, bound the type's ABI alignment.
Manman Rencca54d02012-10-16 19:01:37 +00005991 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
5992 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
John McCall7f416cc2015-09-08 08:05:57 +00005993 // Our callers should be prepared to handle an under-aligned address.
5994 } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
5995 getABIKind() == ARMABIInfo::AAPCS) {
5996 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
5997 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
Tim Northover4c5cb9c2015-11-02 19:32:23 +00005998 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5999 // ARMv7k allows type alignment up to 16 bytes.
6000 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6001 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
John McCall7f416cc2015-09-08 08:05:57 +00006002 } else {
6003 TyAlignForABI = CharUnits::fromQuantity(4);
Manman Renfef9e312012-10-16 19:18:39 +00006004 }
John McCall7f416cc2015-09-08 08:05:57 +00006005 TyInfo.second = TyAlignForABI;
Manman Rencca54d02012-10-16 19:01:37 +00006006
John McCall7f416cc2015-09-08 08:05:57 +00006007 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
6008 SlotSize, /*AllowHigherAlign*/ true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006009}
6010
Chris Lattner0cf24192010-06-28 20:05:43 +00006011//===----------------------------------------------------------------------===//
Justin Holewinski83e96682012-05-24 17:43:12 +00006012// NVPTX ABI Implementation
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006013//===----------------------------------------------------------------------===//
6014
6015namespace {
6016
Justin Holewinski83e96682012-05-24 17:43:12 +00006017class NVPTXABIInfo : public ABIInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006018public:
Justin Holewinski36837432013-03-30 14:38:24 +00006019 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006020
6021 ABIArgInfo classifyReturnType(QualType RetTy) const;
6022 ABIArgInfo classifyArgumentType(QualType Ty) const;
6023
Craig Topper4f12f102014-03-12 06:41:41 +00006024 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00006025 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6026 QualType Ty) const override;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006027};
6028
Justin Holewinski83e96682012-05-24 17:43:12 +00006029class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006030public:
Justin Holewinski83e96682012-05-24 17:43:12 +00006031 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
6032 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Craig Topper4f12f102014-03-12 06:41:41 +00006033
Eric Christopher162c91c2015-06-05 22:03:00 +00006034 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00006035 CodeGen::CodeGenModule &M) const override;
Justin Holewinski36837432013-03-30 14:38:24 +00006036private:
Eli Benderskye06a2c42014-04-15 16:57:05 +00006037 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
6038 // resulting MDNode to the nvvm.annotations MDNode.
6039 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006040};
6041
Justin Holewinski83e96682012-05-24 17:43:12 +00006042ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006043 if (RetTy->isVoidType())
6044 return ABIArgInfo::getIgnore();
Justin Holewinskif9329ff2013-11-20 20:35:34 +00006045
6046 // note: this is different from default ABI
6047 if (!RetTy->isScalarType())
6048 return ABIArgInfo::getDirect();
6049
6050 // Treat an enum type as its underlying type.
6051 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6052 RetTy = EnumTy->getDecl()->getIntegerType();
6053
6054 return (RetTy->isPromotableIntegerType() ?
6055 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006056}
6057
Justin Holewinski83e96682012-05-24 17:43:12 +00006058ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinskif9329ff2013-11-20 20:35:34 +00006059 // Treat an enum type as its underlying type.
6060 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6061 Ty = EnumTy->getDecl()->getIntegerType();
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006062
Eli Bendersky95338a02014-10-29 13:43:21 +00006063 // Return aggregates type as indirect by value
6064 if (isAggregateTypeForABI(Ty))
John McCall7f416cc2015-09-08 08:05:57 +00006065 return getNaturalAlignIndirect(Ty, /* byval */ true);
Eli Bendersky95338a02014-10-29 13:43:21 +00006066
Justin Holewinskif9329ff2013-11-20 20:35:34 +00006067 return (Ty->isPromotableIntegerType() ?
6068 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006069}
6070
Justin Holewinski83e96682012-05-24 17:43:12 +00006071void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00006072 if (!getCXXABI().classifyReturnType(FI))
6073 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006074 for (auto &I : FI.arguments())
6075 I.info = classifyArgumentType(I.type);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006076
6077 // Always honor user-specified calling convention.
6078 if (FI.getCallingConvention() != llvm::CallingConv::C)
6079 return;
6080
John McCall882987f2013-02-28 19:01:20 +00006081 FI.setEffectiveCallingConvention(getRuntimeCC());
6082}
6083
John McCall7f416cc2015-09-08 08:05:57 +00006084Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6085 QualType Ty) const {
Justin Holewinski83e96682012-05-24 17:43:12 +00006086 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006087}
6088
Justin Holewinski83e96682012-05-24 17:43:12 +00006089void NVPTXTargetCodeGenInfo::
Eric Christopher162c91c2015-06-05 22:03:00 +00006090setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Justin Holewinski83e96682012-05-24 17:43:12 +00006091 CodeGen::CodeGenModule &M) const{
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006092 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Justin Holewinski38031972011-10-05 17:58:44 +00006093 if (!FD) return;
6094
6095 llvm::Function *F = cast<llvm::Function>(GV);
6096
6097 // Perform special handling in OpenCL mode
David Blaikiebbafb8a2012-03-11 07:00:24 +00006098 if (M.getLangOpts().OpenCL) {
Justin Holewinski36837432013-03-30 14:38:24 +00006099 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski38031972011-10-05 17:58:44 +00006100 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00006101 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinski36837432013-03-30 14:38:24 +00006102 // OpenCL __kernel functions get kernel metadata
Eli Benderskye06a2c42014-04-15 16:57:05 +00006103 // Create !{<func-ref>, metadata !"kernel", i32 1} node
6104 addNVVMMetadata(F, "kernel", 1);
Justin Holewinski38031972011-10-05 17:58:44 +00006105 // And kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00006106 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski38031972011-10-05 17:58:44 +00006107 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00006108 }
Justin Holewinski38031972011-10-05 17:58:44 +00006109
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00006110 // Perform special handling in CUDA mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00006111 if (M.getLangOpts().CUDA) {
Justin Holewinski36837432013-03-30 14:38:24 +00006112 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00006113 // __global__ functions cannot be called from the device, we do not
6114 // need to set the noinline attribute.
Eli Benderskye06a2c42014-04-15 16:57:05 +00006115 if (FD->hasAttr<CUDAGlobalAttr>()) {
6116 // Create !{<func-ref>, metadata !"kernel", i32 1} node
6117 addNVVMMetadata(F, "kernel", 1);
6118 }
Artem Belevich7093e402015-04-21 22:55:54 +00006119 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
Eli Benderskye06a2c42014-04-15 16:57:05 +00006120 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
Artem Belevich7093e402015-04-21 22:55:54 +00006121 llvm::APSInt MaxThreads(32);
6122 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
6123 if (MaxThreads > 0)
6124 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
6125
6126 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
6127 // not specified in __launch_bounds__ or if the user specified a 0 value,
6128 // we don't have to add a PTX directive.
6129 if (Attr->getMinBlocks()) {
6130 llvm::APSInt MinBlocks(32);
6131 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
6132 if (MinBlocks > 0)
6133 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
6134 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
Eli Benderskye06a2c42014-04-15 16:57:05 +00006135 }
6136 }
Justin Holewinski38031972011-10-05 17:58:44 +00006137 }
6138}
6139
Eli Benderskye06a2c42014-04-15 16:57:05 +00006140void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
6141 int Operand) {
Justin Holewinski36837432013-03-30 14:38:24 +00006142 llvm::Module *M = F->getParent();
6143 llvm::LLVMContext &Ctx = M->getContext();
6144
6145 // Get "nvvm.annotations" metadata node
6146 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
6147
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00006148 llvm::Metadata *MDVals[] = {
6149 llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
6150 llvm::ConstantAsMetadata::get(
6151 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
Justin Holewinski36837432013-03-30 14:38:24 +00006152 // Append metadata to nvvm.annotations
6153 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
6154}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006155}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006156
6157//===----------------------------------------------------------------------===//
Ulrich Weigand47445072013-05-06 16:26:41 +00006158// SystemZ ABI Implementation
6159//===----------------------------------------------------------------------===//
6160
6161namespace {
6162
Bryan Chane3f1ed52016-04-28 13:56:43 +00006163class SystemZABIInfo : public SwiftABIInfo {
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006164 bool HasVector;
6165
Ulrich Weigand47445072013-05-06 16:26:41 +00006166public:
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006167 SystemZABIInfo(CodeGenTypes &CGT, bool HV)
Bryan Chane3f1ed52016-04-28 13:56:43 +00006168 : SwiftABIInfo(CGT), HasVector(HV) {}
Ulrich Weigand47445072013-05-06 16:26:41 +00006169
6170 bool isPromotableIntegerType(QualType Ty) const;
6171 bool isCompoundType(QualType Ty) const;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006172 bool isVectorArgumentType(QualType Ty) const;
Ulrich Weigand47445072013-05-06 16:26:41 +00006173 bool isFPArgumentType(QualType Ty) const;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006174 QualType GetSingleElementType(QualType Ty) const;
Ulrich Weigand47445072013-05-06 16:26:41 +00006175
6176 ABIArgInfo classifyReturnType(QualType RetTy) const;
6177 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
6178
Craig Topper4f12f102014-03-12 06:41:41 +00006179 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00006180 if (!getCXXABI().classifyReturnType(FI))
6181 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006182 for (auto &I : FI.arguments())
6183 I.info = classifyArgumentType(I.type);
Ulrich Weigand47445072013-05-06 16:26:41 +00006184 }
6185
John McCall7f416cc2015-09-08 08:05:57 +00006186 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6187 QualType Ty) const override;
Bryan Chane3f1ed52016-04-28 13:56:43 +00006188
6189 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
6190 ArrayRef<llvm::Type*> scalars,
6191 bool asReturnValue) const override {
6192 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
6193 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00006194 bool isSwiftErrorInRegister() const override {
6195 return true;
6196 }
Ulrich Weigand47445072013-05-06 16:26:41 +00006197};
6198
6199class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
6200public:
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006201 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
6202 : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
Ulrich Weigand47445072013-05-06 16:26:41 +00006203};
6204
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006205}
Ulrich Weigand47445072013-05-06 16:26:41 +00006206
6207bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
6208 // Treat an enum type as its underlying type.
6209 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6210 Ty = EnumTy->getDecl()->getIntegerType();
6211
6212 // Promotable integer types are required to be promoted by the ABI.
6213 if (Ty->isPromotableIntegerType())
6214 return true;
6215
6216 // 32-bit values must also be promoted.
6217 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6218 switch (BT->getKind()) {
6219 case BuiltinType::Int:
6220 case BuiltinType::UInt:
6221 return true;
6222 default:
6223 return false;
6224 }
6225 return false;
6226}
6227
6228bool SystemZABIInfo::isCompoundType(QualType Ty) const {
Ulrich Weigand759449c2015-03-30 13:49:01 +00006229 return (Ty->isAnyComplexType() ||
6230 Ty->isVectorType() ||
6231 isAggregateTypeForABI(Ty));
Ulrich Weigand47445072013-05-06 16:26:41 +00006232}
6233
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006234bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
6235 return (HasVector &&
6236 Ty->isVectorType() &&
6237 getContext().getTypeSize(Ty) <= 128);
6238}
6239
Ulrich Weigand47445072013-05-06 16:26:41 +00006240bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
6241 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6242 switch (BT->getKind()) {
6243 case BuiltinType::Float:
6244 case BuiltinType::Double:
6245 return true;
6246 default:
6247 return false;
6248 }
6249
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006250 return false;
6251}
6252
6253QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
Ulrich Weigand47445072013-05-06 16:26:41 +00006254 if (const RecordType *RT = Ty->getAsStructureType()) {
6255 const RecordDecl *RD = RT->getDecl();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006256 QualType Found;
Ulrich Weigand47445072013-05-06 16:26:41 +00006257
6258 // If this is a C++ record, check the bases first.
6259 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +00006260 for (const auto &I : CXXRD->bases()) {
6261 QualType Base = I.getType();
Ulrich Weigand47445072013-05-06 16:26:41 +00006262
6263 // Empty bases don't affect things either way.
6264 if (isEmptyRecord(getContext(), Base, true))
6265 continue;
6266
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006267 if (!Found.isNull())
6268 return Ty;
6269 Found = GetSingleElementType(Base);
Ulrich Weigand47445072013-05-06 16:26:41 +00006270 }
6271
6272 // Check the fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00006273 for (const auto *FD : RD->fields()) {
Ulrich Weigand759449c2015-03-30 13:49:01 +00006274 // For compatibility with GCC, ignore empty bitfields in C++ mode.
Ulrich Weigand47445072013-05-06 16:26:41 +00006275 // Unlike isSingleElementStruct(), empty structure and array fields
6276 // do count. So do anonymous bitfields that aren't zero-sized.
Ulrich Weigand759449c2015-03-30 13:49:01 +00006277 if (getContext().getLangOpts().CPlusPlus &&
6278 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
6279 continue;
Ulrich Weigand47445072013-05-06 16:26:41 +00006280
6281 // Unlike isSingleElementStruct(), arrays do not count.
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006282 // Nested structures still do though.
6283 if (!Found.isNull())
6284 return Ty;
6285 Found = GetSingleElementType(FD->getType());
Ulrich Weigand47445072013-05-06 16:26:41 +00006286 }
6287
6288 // Unlike isSingleElementStruct(), trailing padding is allowed.
6289 // An 8-byte aligned struct s { float f; } is passed as a double.
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006290 if (!Found.isNull())
6291 return Found;
Ulrich Weigand47445072013-05-06 16:26:41 +00006292 }
6293
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006294 return Ty;
Ulrich Weigand47445072013-05-06 16:26:41 +00006295}
6296
John McCall7f416cc2015-09-08 08:05:57 +00006297Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6298 QualType Ty) const {
Ulrich Weigand47445072013-05-06 16:26:41 +00006299 // Assume that va_list type is correct; should be pointer to LLVM type:
6300 // struct {
6301 // i64 __gpr;
6302 // i64 __fpr;
6303 // i8 *__overflow_arg_area;
6304 // i8 *__reg_save_area;
6305 // };
6306
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006307 // Every non-vector argument occupies 8 bytes and is passed by preference
6308 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
6309 // always passed on the stack.
John McCall7f416cc2015-09-08 08:05:57 +00006310 Ty = getContext().getCanonicalType(Ty);
6311 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Ulrich Weigand759449c2015-03-30 13:49:01 +00006312 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00006313 llvm::Type *DirectTy = ArgTy;
Ulrich Weigand47445072013-05-06 16:26:41 +00006314 ABIArgInfo AI = classifyArgumentType(Ty);
Ulrich Weigand47445072013-05-06 16:26:41 +00006315 bool IsIndirect = AI.isIndirect();
Ulrich Weigand759449c2015-03-30 13:49:01 +00006316 bool InFPRs = false;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006317 bool IsVector = false;
John McCall7f416cc2015-09-08 08:05:57 +00006318 CharUnits UnpaddedSize;
6319 CharUnits DirectAlign;
Ulrich Weigand47445072013-05-06 16:26:41 +00006320 if (IsIndirect) {
John McCall7f416cc2015-09-08 08:05:57 +00006321 DirectTy = llvm::PointerType::getUnqual(DirectTy);
6322 UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
Ulrich Weigand759449c2015-03-30 13:49:01 +00006323 } else {
6324 if (AI.getCoerceToType())
6325 ArgTy = AI.getCoerceToType();
6326 InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006327 IsVector = ArgTy->isVectorTy();
John McCall7f416cc2015-09-08 08:05:57 +00006328 UnpaddedSize = TyInfo.first;
6329 DirectAlign = TyInfo.second;
Ulrich Weigand759449c2015-03-30 13:49:01 +00006330 }
John McCall7f416cc2015-09-08 08:05:57 +00006331 CharUnits PaddedSize = CharUnits::fromQuantity(8);
6332 if (IsVector && UnpaddedSize > PaddedSize)
6333 PaddedSize = CharUnits::fromQuantity(16);
6334 assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
Ulrich Weigand47445072013-05-06 16:26:41 +00006335
John McCall7f416cc2015-09-08 08:05:57 +00006336 CharUnits Padding = (PaddedSize - UnpaddedSize);
Ulrich Weigand47445072013-05-06 16:26:41 +00006337
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006338 llvm::Type *IndexTy = CGF.Int64Ty;
John McCall7f416cc2015-09-08 08:05:57 +00006339 llvm::Value *PaddedSizeV =
6340 llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006341
6342 if (IsVector) {
6343 // Work out the address of a vector argument on the stack.
6344 // Vector arguments are always passed in the high bits of a
6345 // single (8 byte) or double (16 byte) stack slot.
John McCall7f416cc2015-09-08 08:05:57 +00006346 Address OverflowArgAreaPtr =
6347 CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16),
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006348 "overflow_arg_area_ptr");
John McCall7f416cc2015-09-08 08:05:57 +00006349 Address OverflowArgArea =
6350 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6351 TyInfo.second);
6352 Address MemAddr =
6353 CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006354
6355 // Update overflow_arg_area_ptr pointer
6356 llvm::Value *NewOverflowArgArea =
John McCall7f416cc2015-09-08 08:05:57 +00006357 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6358 "overflow_arg_area");
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006359 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6360
6361 return MemAddr;
6362 }
6363
John McCall7f416cc2015-09-08 08:05:57 +00006364 assert(PaddedSize.getQuantity() == 8);
6365
6366 unsigned MaxRegs, RegCountField, RegSaveIndex;
6367 CharUnits RegPadding;
Ulrich Weigand47445072013-05-06 16:26:41 +00006368 if (InFPRs) {
6369 MaxRegs = 4; // Maximum of 4 FPR arguments
6370 RegCountField = 1; // __fpr
6371 RegSaveIndex = 16; // save offset for f0
John McCall7f416cc2015-09-08 08:05:57 +00006372 RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
Ulrich Weigand47445072013-05-06 16:26:41 +00006373 } else {
6374 MaxRegs = 5; // Maximum of 5 GPR arguments
6375 RegCountField = 0; // __gpr
6376 RegSaveIndex = 2; // save offset for r2
6377 RegPadding = Padding; // values are passed in the low bits of a GPR
6378 }
6379
John McCall7f416cc2015-09-08 08:05:57 +00006380 Address RegCountPtr = CGF.Builder.CreateStructGEP(
6381 VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8),
6382 "reg_count_ptr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006383 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
Ulrich Weigand47445072013-05-06 16:26:41 +00006384 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
6385 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
Oliver Stannard405bded2014-02-11 09:25:50 +00006386 "fits_in_regs");
Ulrich Weigand47445072013-05-06 16:26:41 +00006387
6388 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
6389 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
6390 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
6391 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
6392
6393 // Emit code to load the value if it was passed in registers.
6394 CGF.EmitBlock(InRegBlock);
6395
6396 // Work out the address of an argument register.
Ulrich Weigand47445072013-05-06 16:26:41 +00006397 llvm::Value *ScaledRegCount =
6398 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
6399 llvm::Value *RegBase =
John McCall7f416cc2015-09-08 08:05:57 +00006400 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
6401 + RegPadding.getQuantity());
Ulrich Weigand47445072013-05-06 16:26:41 +00006402 llvm::Value *RegOffset =
6403 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
John McCall7f416cc2015-09-08 08:05:57 +00006404 Address RegSaveAreaPtr =
6405 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
6406 "reg_save_area_ptr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006407 llvm::Value *RegSaveArea =
6408 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
John McCall7f416cc2015-09-08 08:05:57 +00006409 Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
6410 "raw_reg_addr"),
6411 PaddedSize);
6412 Address RegAddr =
6413 CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006414
6415 // Update the register count
6416 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
6417 llvm::Value *NewRegCount =
6418 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
6419 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
6420 CGF.EmitBranch(ContBlock);
6421
6422 // Emit code to load the value if it was passed in memory.
6423 CGF.EmitBlock(InMemBlock);
6424
6425 // Work out the address of a stack argument.
John McCall7f416cc2015-09-08 08:05:57 +00006426 Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
6427 VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr");
6428 Address OverflowArgArea =
6429 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6430 PaddedSize);
6431 Address RawMemAddr =
6432 CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
6433 Address MemAddr =
6434 CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006435
6436 // Update overflow_arg_area_ptr pointer
6437 llvm::Value *NewOverflowArgArea =
John McCall7f416cc2015-09-08 08:05:57 +00006438 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6439 "overflow_arg_area");
Ulrich Weigand47445072013-05-06 16:26:41 +00006440 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6441 CGF.EmitBranch(ContBlock);
6442
6443 // Return the appropriate result.
6444 CGF.EmitBlock(ContBlock);
John McCall7f416cc2015-09-08 08:05:57 +00006445 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6446 MemAddr, InMemBlock, "va_arg.addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006447
6448 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00006449 ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
6450 TyInfo.second);
Ulrich Weigand47445072013-05-06 16:26:41 +00006451
6452 return ResAddr;
6453}
6454
Ulrich Weigand47445072013-05-06 16:26:41 +00006455ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
6456 if (RetTy->isVoidType())
6457 return ABIArgInfo::getIgnore();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006458 if (isVectorArgumentType(RetTy))
6459 return ABIArgInfo::getDirect();
Ulrich Weigand47445072013-05-06 16:26:41 +00006460 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
John McCall7f416cc2015-09-08 08:05:57 +00006461 return getNaturalAlignIndirect(RetTy);
Ulrich Weigand47445072013-05-06 16:26:41 +00006462 return (isPromotableIntegerType(RetTy) ?
6463 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6464}
6465
6466ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
6467 // Handle the generic C++ ABI.
Mark Lacey3825e832013-10-06 01:33:34 +00006468 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00006469 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand47445072013-05-06 16:26:41 +00006470
6471 // Integers and enums are extended to full register width.
6472 if (isPromotableIntegerType(Ty))
6473 return ABIArgInfo::getExtend();
6474
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006475 // Handle vector types and vector-like structure types. Note that
6476 // as opposed to float-like structure types, we do not allow any
6477 // padding for vector-like structures, so verify the sizes match.
Ulrich Weigand47445072013-05-06 16:26:41 +00006478 uint64_t Size = getContext().getTypeSize(Ty);
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006479 QualType SingleElementTy = GetSingleElementType(Ty);
6480 if (isVectorArgumentType(SingleElementTy) &&
6481 getContext().getTypeSize(SingleElementTy) == Size)
6482 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
6483
6484 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
Ulrich Weigand47445072013-05-06 16:26:41 +00006485 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
John McCall7f416cc2015-09-08 08:05:57 +00006486 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00006487
6488 // Handle small structures.
6489 if (const RecordType *RT = Ty->getAs<RecordType>()) {
6490 // Structures with flexible arrays have variable length, so really
6491 // fail the size test above.
6492 const RecordDecl *RD = RT->getDecl();
6493 if (RD->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00006494 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00006495
6496 // The structure is passed as an unextended integer, a float, or a double.
6497 llvm::Type *PassTy;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006498 if (isFPArgumentType(SingleElementTy)) {
Ulrich Weigand47445072013-05-06 16:26:41 +00006499 assert(Size == 32 || Size == 64);
6500 if (Size == 32)
6501 PassTy = llvm::Type::getFloatTy(getVMContext());
6502 else
6503 PassTy = llvm::Type::getDoubleTy(getVMContext());
6504 } else
6505 PassTy = llvm::IntegerType::get(getVMContext(), Size);
6506 return ABIArgInfo::getDirect(PassTy);
6507 }
6508
6509 // Non-structure compounds are passed indirectly.
6510 if (isCompoundType(Ty))
John McCall7f416cc2015-09-08 08:05:57 +00006511 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00006512
Craig Topper8a13c412014-05-21 05:09:00 +00006513 return ABIArgInfo::getDirect(nullptr);
Ulrich Weigand47445072013-05-06 16:26:41 +00006514}
6515
6516//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006517// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00006518//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006519
6520namespace {
6521
6522class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
6523public:
Chris Lattner2b037972010-07-29 02:01:43 +00006524 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
6525 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Eric Christopher162c91c2015-06-05 22:03:00 +00006526 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00006527 CodeGen::CodeGenModule &M) const override;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006528};
6529
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006530}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006531
Eric Christopher162c91c2015-06-05 22:03:00 +00006532void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006533 llvm::GlobalValue *GV,
6534 CodeGen::CodeGenModule &M) const {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006535 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006536 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
6537 // Handle 'interrupt' attribute:
6538 llvm::Function *F = cast<llvm::Function>(GV);
6539
6540 // Step 1: Set ISR calling convention.
6541 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
6542
6543 // Step 2: Add attributes goodness.
Bill Wendling207f0532012-12-20 19:27:06 +00006544 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006545
6546 // Step 3: Emit ISR vector alias.
Anton Korobeynikovc5a7f922012-11-26 18:59:10 +00006547 unsigned Num = attr->getNumber() / 2;
Rafael Espindola234405b2014-05-17 21:30:14 +00006548 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
6549 "__isr_" + Twine(Num), F);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006550 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006551 }
6552}
6553
Chris Lattner0cf24192010-06-28 20:05:43 +00006554//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00006555// MIPS ABI Implementation. This works for both little-endian and
6556// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00006557//===----------------------------------------------------------------------===//
6558
John McCall943fae92010-05-27 06:19:26 +00006559namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00006560class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00006561 bool IsO32;
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006562 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
6563 void CoerceToIntArgs(uint64_t TySize,
Craig Topper5603df42013-07-05 19:34:19 +00006564 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006565 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006566 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006567 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006568public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006569 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006570 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006571 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00006572
6573 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006574 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Craig Topper4f12f102014-03-12 06:41:41 +00006575 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00006576 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6577 QualType Ty) const override;
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00006578 bool shouldSignExtUnsignedType(QualType Ty) const override;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006579};
6580
John McCall943fae92010-05-27 06:19:26 +00006581class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00006582 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00006583public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006584 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
6585 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
Akira Hatanaka14378522011-11-02 23:14:57 +00006586 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00006587
Craig Topper4f12f102014-03-12 06:41:41 +00006588 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCall943fae92010-05-27 06:19:26 +00006589 return 29;
6590 }
6591
Eric Christopher162c91c2015-06-05 22:03:00 +00006592 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00006593 CodeGen::CodeGenModule &CGM) const override {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006594 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Reed Kotler3d5966f2013-03-13 20:40:30 +00006595 if (!FD) return;
Rafael Espindolaa0851a22013-03-19 14:32:23 +00006596 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotler3d5966f2013-03-13 20:40:30 +00006597 if (FD->hasAttr<Mips16Attr>()) {
6598 Fn->addFnAttr("mips16");
6599 }
6600 else if (FD->hasAttr<NoMips16Attr>()) {
6601 Fn->addFnAttr("nomips16");
6602 }
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006603
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006604 if (FD->hasAttr<MicroMipsAttr>())
6605 Fn->addFnAttr("micromips");
6606 else if (FD->hasAttr<NoMicroMipsAttr>())
6607 Fn->addFnAttr("nomicromips");
6608
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006609 const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
6610 if (!Attr)
6611 return;
6612
6613 const char *Kind;
6614 switch (Attr->getInterrupt()) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006615 case MipsInterruptAttr::eic: Kind = "eic"; break;
6616 case MipsInterruptAttr::sw0: Kind = "sw0"; break;
6617 case MipsInterruptAttr::sw1: Kind = "sw1"; break;
6618 case MipsInterruptAttr::hw0: Kind = "hw0"; break;
6619 case MipsInterruptAttr::hw1: Kind = "hw1"; break;
6620 case MipsInterruptAttr::hw2: Kind = "hw2"; break;
6621 case MipsInterruptAttr::hw3: Kind = "hw3"; break;
6622 case MipsInterruptAttr::hw4: Kind = "hw4"; break;
6623 case MipsInterruptAttr::hw5: Kind = "hw5"; break;
6624 }
6625
6626 Fn->addFnAttr("interrupt", Kind);
6627
Reed Kotler373feca2013-01-16 17:10:28 +00006628 }
Reed Kotler3d5966f2013-03-13 20:40:30 +00006629
John McCall943fae92010-05-27 06:19:26 +00006630 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00006631 llvm::Value *Address) const override;
John McCall3480ef22011-08-30 01:42:09 +00006632
Craig Topper4f12f102014-03-12 06:41:41 +00006633 unsigned getSizeOfUnwindException() const override {
Akira Hatanaka0486db02011-09-20 18:23:28 +00006634 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00006635 }
John McCall943fae92010-05-27 06:19:26 +00006636};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006637}
John McCall943fae92010-05-27 06:19:26 +00006638
Eric Christopher7565e0d2015-05-29 23:09:49 +00006639void MipsABIInfo::CoerceToIntArgs(
6640 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006641 llvm::IntegerType *IntTy =
6642 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006643
6644 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
6645 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
6646 ArgList.push_back(IntTy);
6647
6648 // If necessary, add one more integer type to ArgList.
6649 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
6650
6651 if (R)
6652 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006653}
6654
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006655// In N32/64, an aligned double precision floating point field is passed in
6656// a register.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006657llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006658 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
6659
6660 if (IsO32) {
6661 CoerceToIntArgs(TySize, ArgList);
6662 return llvm::StructType::get(getVMContext(), ArgList);
6663 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006664
Akira Hatanaka02e13e52012-01-12 00:52:17 +00006665 if (Ty->isComplexType())
6666 return CGT.ConvertType(Ty);
Akira Hatanaka79f04612012-01-10 23:12:19 +00006667
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00006668 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006669
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006670 // Unions/vectors are passed in integer registers.
6671 if (!RT || !RT->isStructureOrClassType()) {
6672 CoerceToIntArgs(TySize, ArgList);
6673 return llvm::StructType::get(getVMContext(), ArgList);
6674 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006675
6676 const RecordDecl *RD = RT->getDecl();
6677 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006678 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Eric Christopher7565e0d2015-05-29 23:09:49 +00006679
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006680 uint64_t LastOffset = 0;
6681 unsigned idx = 0;
6682 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
6683
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00006684 // Iterate over fields in the struct/class and check if there are any aligned
6685 // double fields.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006686 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6687 i != e; ++i, ++idx) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006688 const QualType Ty = i->getType();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006689 const BuiltinType *BT = Ty->getAs<BuiltinType>();
6690
6691 if (!BT || BT->getKind() != BuiltinType::Double)
6692 continue;
6693
6694 uint64_t Offset = Layout.getFieldOffset(idx);
6695 if (Offset % 64) // Ignore doubles that are not aligned.
6696 continue;
6697
6698 // Add ((Offset - LastOffset) / 64) args of type i64.
6699 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
6700 ArgList.push_back(I64);
6701
6702 // Add double type.
6703 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
6704 LastOffset = Offset + 64;
6705 }
6706
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006707 CoerceToIntArgs(TySize - LastOffset, IntArgList);
6708 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006709
6710 return llvm::StructType::get(getVMContext(), ArgList);
6711}
6712
Akira Hatanakaddd66342013-10-29 18:41:15 +00006713llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
6714 uint64_t Offset) const {
6715 if (OrigOffset + MinABIStackAlignInBytes > Offset)
Craig Topper8a13c412014-05-21 05:09:00 +00006716 return nullptr;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006717
Akira Hatanakaddd66342013-10-29 18:41:15 +00006718 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
Akira Hatanaka1632af62012-01-09 19:31:25 +00006719}
Akira Hatanaka21ee88c2012-01-10 22:44:52 +00006720
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006721ABIArgInfo
6722MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Daniel Sanders998c9102015-01-14 12:00:12 +00006723 Ty = useFirstFieldIfTransparentUnion(Ty);
6724
Akira Hatanaka1632af62012-01-09 19:31:25 +00006725 uint64_t OrigOffset = Offset;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006726 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanaka1632af62012-01-09 19:31:25 +00006727 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006728
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006729 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
6730 (uint64_t)StackAlignInBytes);
Rui Ueyama83aa9792016-01-14 21:00:27 +00006731 unsigned CurrOffset = llvm::alignTo(Offset, Align);
6732 Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006733
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006734 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanakab579fe52011-06-02 00:09:17 +00006735 // Ignore empty aggregates.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006736 if (TySize == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00006737 return ABIArgInfo::getIgnore();
6738
Mark Lacey3825e832013-10-06 01:33:34 +00006739 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006740 Offset = OrigOffset + MinABIStackAlignInBytes;
John McCall7f416cc2015-09-08 08:05:57 +00006741 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006742 }
Akira Hatanakadf425db2011-08-01 18:09:58 +00006743
Petar Jovanovic6f4cdb82017-05-10 14:28:18 +00006744 // Use indirect if the aggregate cannot fit into registers for
6745 // passing arguments according to the ABI
6746 unsigned Threshold = IsO32 ? 16 : 64;
6747
6748 if(getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(Threshold))
6749 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align), true,
6750 getContext().getTypeAlign(Ty) / 8 > Align);
6751
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006752 // If we have reached here, aggregates are passed directly by coercing to
6753 // another structure type. Padding is inserted if the offset of the
6754 // aggregate is unaligned.
Daniel Sandersaa1b3552014-10-24 15:30:16 +00006755 ABIArgInfo ArgInfo =
6756 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
6757 getPaddingType(OrigOffset, CurrOffset));
6758 ArgInfo.setInReg(true);
6759 return ArgInfo;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006760 }
6761
6762 // Treat an enum type as its underlying type.
6763 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6764 Ty = EnumTy->getDecl()->getIntegerType();
6765
Daniel Sanders5b445b32014-10-24 14:42:42 +00006766 // All integral types are promoted to the GPR width.
6767 if (Ty->isIntegralOrEnumerationType())
Akira Hatanaka1632af62012-01-09 19:31:25 +00006768 return ABIArgInfo::getExtend();
6769
Akira Hatanakaddd66342013-10-29 18:41:15 +00006770 return ABIArgInfo::getDirect(
Craig Topper8a13c412014-05-21 05:09:00 +00006771 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00006772}
6773
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006774llvm::Type*
6775MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakab6f74432012-02-09 18:49:26 +00006776 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006777 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006778
Akira Hatanakab6f74432012-02-09 18:49:26 +00006779 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006780 const RecordDecl *RD = RT->getDecl();
Akira Hatanakab6f74432012-02-09 18:49:26 +00006781 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6782 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006783
Akira Hatanakab6f74432012-02-09 18:49:26 +00006784 // N32/64 returns struct/classes in floating point registers if the
6785 // following conditions are met:
6786 // 1. The size of the struct/class is no larger than 128-bit.
6787 // 2. The struct/class has one or two fields all of which are floating
6788 // point types.
Eric Christopher7565e0d2015-05-29 23:09:49 +00006789 // 3. The offset of the first field is zero (this follows what gcc does).
Akira Hatanakab6f74432012-02-09 18:49:26 +00006790 //
6791 // Any other composite results are returned in integer registers.
6792 //
6793 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
6794 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
6795 for (; b != e; ++b) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006796 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006797
Akira Hatanakab6f74432012-02-09 18:49:26 +00006798 if (!BT || !BT->isFloatingPoint())
6799 break;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006800
David Blaikie2d7c57e2012-04-30 02:36:29 +00006801 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakab6f74432012-02-09 18:49:26 +00006802 }
6803
6804 if (b == e)
6805 return llvm::StructType::get(getVMContext(), RTList,
6806 RD->hasAttr<PackedAttr>());
6807
6808 RTList.clear();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006809 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006810 }
6811
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006812 CoerceToIntArgs(Size, RTList);
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006813 return llvm::StructType::get(getVMContext(), RTList);
6814}
6815
Akira Hatanakab579fe52011-06-02 00:09:17 +00006816ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanaka60f5fe62012-01-23 23:18:57 +00006817 uint64_t Size = getContext().getTypeSize(RetTy);
6818
Daniel Sandersed39f582014-09-04 13:28:14 +00006819 if (RetTy->isVoidType())
6820 return ABIArgInfo::getIgnore();
6821
6822 // O32 doesn't treat zero-sized structs differently from other structs.
6823 // However, N32/N64 ignores zero sized return values.
6824 if (!IsO32 && Size == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00006825 return ABIArgInfo::getIgnore();
6826
Akira Hatanakac37eddf2012-05-11 21:01:17 +00006827 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006828 if (Size <= 128) {
6829 if (RetTy->isAnyComplexType())
6830 return ABIArgInfo::getDirect();
6831
Daniel Sanderse5018b62014-09-04 15:05:39 +00006832 // O32 returns integer vectors in registers and N32/N64 returns all small
Daniel Sanders00a56ff2014-09-04 15:07:43 +00006833 // aggregates in registers.
Daniel Sanderse5018b62014-09-04 15:05:39 +00006834 if (!IsO32 ||
6835 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
6836 ABIArgInfo ArgInfo =
6837 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
6838 ArgInfo.setInReg(true);
6839 return ArgInfo;
6840 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006841 }
Akira Hatanakab579fe52011-06-02 00:09:17 +00006842
John McCall7f416cc2015-09-08 08:05:57 +00006843 return getNaturalAlignIndirect(RetTy);
Akira Hatanakab579fe52011-06-02 00:09:17 +00006844 }
6845
6846 // Treat an enum type as its underlying type.
6847 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6848 RetTy = EnumTy->getDecl()->getIntegerType();
6849
6850 return (RetTy->isPromotableIntegerType() ?
6851 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6852}
6853
6854void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanaka32604a92012-01-12 01:10:09 +00006855 ABIArgInfo &RetInfo = FI.getReturnInfo();
Reid Kleckner40ca9132014-05-13 22:05:45 +00006856 if (!getCXXABI().classifyReturnType(FI))
6857 RetInfo = classifyReturnType(FI.getReturnType());
Akira Hatanaka32604a92012-01-12 01:10:09 +00006858
Eric Christopher7565e0d2015-05-29 23:09:49 +00006859 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006860 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanaka32604a92012-01-12 01:10:09 +00006861
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006862 for (auto &I : FI.arguments())
6863 I.info = classifyArgumentType(I.type, Offset);
Akira Hatanakab579fe52011-06-02 00:09:17 +00006864}
6865
John McCall7f416cc2015-09-08 08:05:57 +00006866Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6867 QualType OrigTy) const {
6868 QualType Ty = OrigTy;
Daniel Sanders59229dc2014-11-19 10:01:35 +00006869
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006870 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
6871 // Pointers are also promoted in the same way but this only matters for N32.
Daniel Sanders59229dc2014-11-19 10:01:35 +00006872 unsigned SlotSizeInBits = IsO32 ? 32 : 64;
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006873 unsigned PtrWidth = getTarget().getPointerWidth(0);
John McCall7f416cc2015-09-08 08:05:57 +00006874 bool DidPromote = false;
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006875 if ((Ty->isIntegerType() &&
John McCall7f416cc2015-09-08 08:05:57 +00006876 getContext().getIntWidth(Ty) < SlotSizeInBits) ||
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006877 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
John McCall7f416cc2015-09-08 08:05:57 +00006878 DidPromote = true;
6879 Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
6880 Ty->isSignedIntegerType());
Daniel Sanders59229dc2014-11-19 10:01:35 +00006881 }
Eric Christopher7565e0d2015-05-29 23:09:49 +00006882
John McCall7f416cc2015-09-08 08:05:57 +00006883 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00006884
John McCall7f416cc2015-09-08 08:05:57 +00006885 // The alignment of things in the argument area is never larger than
6886 // StackAlignInBytes.
6887 TyInfo.second =
6888 std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
6889
6890 // MinABIStackAlignInBytes is the size of argument slots on the stack.
6891 CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
6892
6893 Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6894 TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
6895
6896
6897 // If there was a promotion, "unpromote" into a temporary.
6898 // TODO: can we just use a pointer into a subset of the original slot?
6899 if (DidPromote) {
6900 Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
6901 llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
6902
6903 // Truncate down to the right width.
6904 llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
6905 : CGF.IntPtrTy);
6906 llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
6907 if (OrigTy->isPointerType())
6908 V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
6909
6910 CGF.Builder.CreateStore(V, Temp);
6911 Addr = Temp;
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00006912 }
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00006913
John McCall7f416cc2015-09-08 08:05:57 +00006914 return Addr;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006915}
6916
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00006917bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
6918 int TySize = getContext().getTypeSize(Ty);
Eric Christopher7565e0d2015-05-29 23:09:49 +00006919
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00006920 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
6921 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
6922 return true;
Eric Christopher7565e0d2015-05-29 23:09:49 +00006923
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00006924 return false;
6925}
6926
John McCall943fae92010-05-27 06:19:26 +00006927bool
6928MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6929 llvm::Value *Address) const {
6930 // This information comes from gcc's implementation, which seems to
6931 // as canonical as it gets.
6932
John McCall943fae92010-05-27 06:19:26 +00006933 // Everything on MIPS is 4 bytes. Double-precision FP registers
6934 // are aliased to pairs of single-precision FP registers.
Chris Lattnerece04092012-02-07 00:39:47 +00006935 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCall943fae92010-05-27 06:19:26 +00006936
6937 // 0-31 are the general purpose registers, $0 - $31.
6938 // 32-63 are the floating-point registers, $f0 - $f31.
6939 // 64 and 65 are the multiply/divide registers, $hi and $lo.
6940 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattnerece04092012-02-07 00:39:47 +00006941 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCall943fae92010-05-27 06:19:26 +00006942
6943 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
6944 // They are one bit wide and ignored here.
6945
6946 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
6947 // (coprocessor 1 is the FP unit)
6948 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
6949 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
6950 // 176-181 are the DSP accumulator registers.
Chris Lattnerece04092012-02-07 00:39:47 +00006951 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCall943fae92010-05-27 06:19:26 +00006952 return false;
6953}
6954
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006955//===----------------------------------------------------------------------===//
Dylan McKaye8232d72017-02-08 05:09:26 +00006956// AVR ABI Implementation.
6957//===----------------------------------------------------------------------===//
6958
6959namespace {
6960class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
6961public:
6962 AVRTargetCodeGenInfo(CodeGenTypes &CGT)
6963 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { }
6964
6965 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6966 CodeGen::CodeGenModule &CGM) const override {
6967 const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
6968 if (!FD) return;
6969 auto *Fn = cast<llvm::Function>(GV);
6970
6971 if (FD->getAttr<AVRInterruptAttr>())
6972 Fn->addFnAttr("interrupt");
6973
6974 if (FD->getAttr<AVRSignalAttr>())
6975 Fn->addFnAttr("signal");
6976 }
6977};
6978}
6979
6980//===----------------------------------------------------------------------===//
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006981// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
Eric Christopher7565e0d2015-05-29 23:09:49 +00006982// Currently subclassed only to implement custom OpenCL C function attribute
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006983// handling.
6984//===----------------------------------------------------------------------===//
6985
6986namespace {
6987
6988class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
6989public:
6990 TCETargetCodeGenInfo(CodeGenTypes &CGT)
6991 : DefaultTargetCodeGenInfo(CGT) {}
6992
Eric Christopher162c91c2015-06-05 22:03:00 +00006993 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00006994 CodeGen::CodeGenModule &M) const override;
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006995};
6996
Eric Christopher162c91c2015-06-05 22:03:00 +00006997void TCETargetCodeGenInfo::setTargetAttributes(
Eric Christopher7565e0d2015-05-29 23:09:49 +00006998 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006999 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007000 if (!FD) return;
7001
7002 llvm::Function *F = cast<llvm::Function>(GV);
Eric Christopher7565e0d2015-05-29 23:09:49 +00007003
David Blaikiebbafb8a2012-03-11 07:00:24 +00007004 if (M.getLangOpts().OpenCL) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007005 if (FD->hasAttr<OpenCLKernelAttr>()) {
7006 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00007007 F->addFnAttr(llvm::Attribute::NoInline);
Aaron Ballman36a18ff2013-12-19 13:16:35 +00007008 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
7009 if (Attr) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007010 // Convert the reqd_work_group_size() attributes to metadata.
7011 llvm::LLVMContext &Context = F->getContext();
Eric Christopher7565e0d2015-05-29 23:09:49 +00007012 llvm::NamedMDNode *OpenCLMetadata =
7013 M.getModule().getOrInsertNamedMetadata(
7014 "opencl.kernel_wg_size_info");
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007015
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007016 SmallVector<llvm::Metadata *, 5> Operands;
7017 Operands.push_back(llvm::ConstantAsMetadata::get(F));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007018
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007019 Operands.push_back(
7020 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7021 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
7022 Operands.push_back(
7023 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7024 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
7025 Operands.push_back(
7026 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7027 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007028
Eric Christopher7565e0d2015-05-29 23:09:49 +00007029 // Add a boolean constant operand for "required" (true) or "hint"
7030 // (false) for implementing the work_group_size_hint attr later.
7031 // Currently always true as the hint is not yet implemented.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007032 Operands.push_back(
7033 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007034 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
7035 }
7036 }
7037 }
7038}
7039
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007040}
John McCall943fae92010-05-27 06:19:26 +00007041
Tony Linthicum76329bf2011-12-12 21:14:55 +00007042//===----------------------------------------------------------------------===//
7043// Hexagon ABI Implementation
7044//===----------------------------------------------------------------------===//
7045
7046namespace {
7047
7048class HexagonABIInfo : public ABIInfo {
7049
7050
7051public:
7052 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
7053
7054private:
7055
7056 ABIArgInfo classifyReturnType(QualType RetTy) const;
7057 ABIArgInfo classifyArgumentType(QualType RetTy) const;
7058
Craig Topper4f12f102014-03-12 06:41:41 +00007059 void computeInfo(CGFunctionInfo &FI) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00007060
John McCall7f416cc2015-09-08 08:05:57 +00007061 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7062 QualType Ty) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00007063};
7064
7065class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
7066public:
7067 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
7068 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
7069
Craig Topper4f12f102014-03-12 06:41:41 +00007070 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Tony Linthicum76329bf2011-12-12 21:14:55 +00007071 return 29;
7072 }
7073};
7074
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007075}
Tony Linthicum76329bf2011-12-12 21:14:55 +00007076
7077void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00007078 if (!getCXXABI().classifyReturnType(FI))
7079 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00007080 for (auto &I : FI.arguments())
7081 I.info = classifyArgumentType(I.type);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007082}
7083
7084ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
7085 if (!isAggregateTypeForABI(Ty)) {
7086 // Treat an enum type as its underlying type.
7087 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7088 Ty = EnumTy->getDecl()->getIntegerType();
7089
7090 return (Ty->isPromotableIntegerType() ?
7091 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
7092 }
7093
Krzysztof Parzyszek408b2722017-05-12 13:18:07 +00007094 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7095 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7096
Tony Linthicum76329bf2011-12-12 21:14:55 +00007097 // Ignore empty records.
7098 if (isEmptyRecord(getContext(), Ty, true))
7099 return ABIArgInfo::getIgnore();
7100
Tony Linthicum76329bf2011-12-12 21:14:55 +00007101 uint64_t Size = getContext().getTypeSize(Ty);
7102 if (Size > 64)
John McCall7f416cc2015-09-08 08:05:57 +00007103 return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007104 // Pass in the smallest viable integer type.
7105 else if (Size > 32)
7106 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7107 else if (Size > 16)
7108 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7109 else if (Size > 8)
7110 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7111 else
7112 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7113}
7114
7115ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
7116 if (RetTy->isVoidType())
7117 return ABIArgInfo::getIgnore();
7118
7119 // Large vector types should be returned via memory.
7120 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
John McCall7f416cc2015-09-08 08:05:57 +00007121 return getNaturalAlignIndirect(RetTy);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007122
7123 if (!isAggregateTypeForABI(RetTy)) {
7124 // Treat an enum type as its underlying type.
7125 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7126 RetTy = EnumTy->getDecl()->getIntegerType();
7127
7128 return (RetTy->isPromotableIntegerType() ?
7129 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
7130 }
7131
Tony Linthicum76329bf2011-12-12 21:14:55 +00007132 if (isEmptyRecord(getContext(), RetTy, true))
7133 return ABIArgInfo::getIgnore();
7134
7135 // Aggregates <= 8 bytes are returned in r0; other aggregates
7136 // are returned indirectly.
7137 uint64_t Size = getContext().getTypeSize(RetTy);
7138 if (Size <= 64) {
7139 // Return in the smallest viable integer type.
7140 if (Size <= 8)
7141 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7142 if (Size <= 16)
7143 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7144 if (Size <= 32)
7145 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7146 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7147 }
7148
John McCall7f416cc2015-09-08 08:05:57 +00007149 return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007150}
7151
John McCall7f416cc2015-09-08 08:05:57 +00007152Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7153 QualType Ty) const {
7154 // FIXME: Someone needs to audit that this handle alignment correctly.
7155 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
7156 getContext().getTypeInfoInChars(Ty),
7157 CharUnits::fromQuantity(4),
7158 /*AllowHigherAlign*/ true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007159}
7160
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007161//===----------------------------------------------------------------------===//
Jacques Pienaard964cc22016-03-28 21:02:54 +00007162// Lanai ABI Implementation
7163//===----------------------------------------------------------------------===//
7164
Benjamin Kramer5d28c7f2016-04-07 10:14:54 +00007165namespace {
Jacques Pienaard964cc22016-03-28 21:02:54 +00007166class LanaiABIInfo : public DefaultABIInfo {
7167public:
7168 LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7169
7170 bool shouldUseInReg(QualType Ty, CCState &State) const;
7171
7172 void computeInfo(CGFunctionInfo &FI) const override {
7173 CCState State(FI.getCallingConvention());
7174 // Lanai uses 4 registers to pass arguments unless the function has the
7175 // regparm attribute set.
7176 if (FI.getHasRegParm()) {
7177 State.FreeRegs = FI.getRegParm();
7178 } else {
7179 State.FreeRegs = 4;
7180 }
7181
7182 if (!getCXXABI().classifyReturnType(FI))
7183 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7184 for (auto &I : FI.arguments())
7185 I.info = classifyArgumentType(I.type, State);
7186 }
7187
Jacques Pienaare74d9132016-04-26 00:09:29 +00007188 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
Jacques Pienaard964cc22016-03-28 21:02:54 +00007189 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
7190};
Benjamin Kramer5d28c7f2016-04-07 10:14:54 +00007191} // end anonymous namespace
Jacques Pienaard964cc22016-03-28 21:02:54 +00007192
7193bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
7194 unsigned Size = getContext().getTypeSize(Ty);
7195 unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
7196
7197 if (SizeInRegs == 0)
7198 return false;
7199
7200 if (SizeInRegs > State.FreeRegs) {
7201 State.FreeRegs = 0;
7202 return false;
7203 }
7204
7205 State.FreeRegs -= SizeInRegs;
7206
7207 return true;
7208}
7209
Jacques Pienaare74d9132016-04-26 00:09:29 +00007210ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
7211 CCState &State) const {
7212 if (!ByVal) {
7213 if (State.FreeRegs) {
7214 --State.FreeRegs; // Non-byval indirects just use one pointer.
7215 return getNaturalAlignIndirectInReg(Ty);
7216 }
7217 return getNaturalAlignIndirect(Ty, false);
7218 }
7219
7220 // Compute the byval alignment.
Kostya Serebryany0da44422016-04-26 01:53:49 +00007221 const unsigned MinABIStackAlignInBytes = 4;
Jacques Pienaare74d9132016-04-26 00:09:29 +00007222 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
7223 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
7224 /*Realign=*/TypeAlign >
7225 MinABIStackAlignInBytes);
7226}
7227
Jacques Pienaard964cc22016-03-28 21:02:54 +00007228ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
7229 CCState &State) const {
Jacques Pienaare74d9132016-04-26 00:09:29 +00007230 // Check with the C++ ABI first.
7231 const RecordType *RT = Ty->getAs<RecordType>();
7232 if (RT) {
7233 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
7234 if (RAA == CGCXXABI::RAA_Indirect) {
7235 return getIndirectResult(Ty, /*ByVal=*/false, State);
7236 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
7237 return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
7238 }
7239 }
7240
7241 if (isAggregateTypeForABI(Ty)) {
7242 // Structures with flexible arrays are always indirect.
7243 if (RT && RT->getDecl()->hasFlexibleArrayMember())
7244 return getIndirectResult(Ty, /*ByVal=*/true, State);
7245
7246 // Ignore empty structs/unions.
7247 if (isEmptyRecord(getContext(), Ty, true))
7248 return ABIArgInfo::getIgnore();
7249
7250 llvm::LLVMContext &LLVMContext = getVMContext();
7251 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
7252 if (SizeInRegs <= State.FreeRegs) {
7253 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
7254 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
7255 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
7256 State.FreeRegs -= SizeInRegs;
7257 return ABIArgInfo::getDirectInReg(Result);
7258 } else {
7259 State.FreeRegs = 0;
7260 }
7261 return getIndirectResult(Ty, true, State);
7262 }
Jacques Pienaard964cc22016-03-28 21:02:54 +00007263
7264 // Treat an enum type as its underlying type.
7265 if (const auto *EnumTy = Ty->getAs<EnumType>())
7266 Ty = EnumTy->getDecl()->getIntegerType();
7267
Jacques Pienaare74d9132016-04-26 00:09:29 +00007268 bool InReg = shouldUseInReg(Ty, State);
7269 if (Ty->isPromotableIntegerType()) {
7270 if (InReg)
7271 return ABIArgInfo::getDirectInReg();
Jacques Pienaard964cc22016-03-28 21:02:54 +00007272 return ABIArgInfo::getExtend();
Jacques Pienaare74d9132016-04-26 00:09:29 +00007273 }
7274 if (InReg)
7275 return ABIArgInfo::getDirectInReg();
Jacques Pienaard964cc22016-03-28 21:02:54 +00007276 return ABIArgInfo::getDirect();
7277}
7278
7279namespace {
7280class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
7281public:
7282 LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
7283 : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {}
7284};
7285}
7286
7287//===----------------------------------------------------------------------===//
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007288// AMDGPU ABI Implementation
7289//===----------------------------------------------------------------------===//
7290
7291namespace {
7292
Matt Arsenault88d7da02016-08-22 19:25:59 +00007293class AMDGPUABIInfo final : public DefaultABIInfo {
7294public:
7295 explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7296
7297private:
7298 ABIArgInfo classifyArgumentType(QualType Ty) const;
7299
7300 void computeInfo(CGFunctionInfo &FI) const override;
7301};
7302
7303void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
7304 if (!getCXXABI().classifyReturnType(FI))
7305 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7306
7307 unsigned CC = FI.getCallingConvention();
7308 for (auto &Arg : FI.arguments())
7309 if (CC == llvm::CallingConv::AMDGPU_KERNEL)
7310 Arg.info = classifyArgumentType(Arg.type);
7311 else
7312 Arg.info = DefaultABIInfo::classifyArgumentType(Arg.type);
7313}
7314
7315/// \brief Classify argument of given type \p Ty.
7316ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty) const {
7317 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
7318 if (!StrTy) {
7319 return DefaultABIInfo::classifyArgumentType(Ty);
7320 }
7321
7322 // Coerce single element structs to its element.
7323 if (StrTy->getNumElements() == 1) {
7324 return ABIArgInfo::getDirect();
7325 }
7326
7327 // If we set CanBeFlattened to true, CodeGen will expand the struct to its
7328 // individual elements, which confuses the Clover OpenCL backend; therefore we
7329 // have to set it to false here. Other args of getDirect() are just defaults.
7330 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
7331}
7332
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007333class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
7334public:
7335 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
Matt Arsenault88d7da02016-08-22 19:25:59 +00007336 : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {}
Eric Christopher162c91c2015-06-05 22:03:00 +00007337 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007338 CodeGen::CodeGenModule &M) const override;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00007339 unsigned getOpenCLKernelCallingConv() const override;
Nico Weber7849eeb2016-12-14 21:38:18 +00007340
Yaxun Liu402804b2016-12-15 08:09:08 +00007341 llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
7342 llvm::PointerType *T, QualType QT) const override;
Yaxun Liu6d96f1632017-05-18 18:51:09 +00007343
7344 unsigned getASTAllocaAddressSpace() const override {
7345 return LangAS::FirstTargetAddressSpace +
7346 getABIInfo().getDataLayout().getAllocaAddrSpace();
7347 }
Yaxun Liucbf647c2017-07-08 13:24:52 +00007348 unsigned getGlobalVarAddressSpace(CodeGenModule &CGM,
7349 const VarDecl *D) const override;
Yaxun Liu402804b2016-12-15 08:09:08 +00007350};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007351}
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007352
Eric Christopher162c91c2015-06-05 22:03:00 +00007353void AMDGPUTargetCodeGenInfo::setTargetAttributes(
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007354 const Decl *D,
7355 llvm::GlobalValue *GV,
7356 CodeGen::CodeGenModule &M) const {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00007357 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007358 if (!FD)
7359 return;
7360
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007361 llvm::Function *F = cast<llvm::Function>(GV);
7362
Stanislav Mekhanoshin921a4232017-04-06 18:15:44 +00007363 const auto *ReqdWGS = M.getLangOpts().OpenCL ?
7364 FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
7365 const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
7366 if (ReqdWGS || FlatWGS) {
7367 unsigned Min = FlatWGS ? FlatWGS->getMin() : 0;
7368 unsigned Max = FlatWGS ? FlatWGS->getMax() : 0;
7369 if (ReqdWGS && Min == 0 && Max == 0)
7370 Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007371
7372 if (Min != 0) {
7373 assert(Min <= Max && "Min must be less than or equal Max");
7374
7375 std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
7376 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
7377 } else
7378 assert(Max == 0 && "Max must be zero");
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007379 }
7380
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007381 if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
7382 unsigned Min = Attr->getMin();
7383 unsigned Max = Attr->getMax();
7384
7385 if (Min != 0) {
7386 assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
7387
7388 std::string AttrVal = llvm::utostr(Min);
7389 if (Max != 0)
7390 AttrVal = AttrVal + "," + llvm::utostr(Max);
7391 F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
7392 } else
7393 assert(Max == 0 && "Max must be zero");
7394 }
7395
7396 if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007397 unsigned NumSGPR = Attr->getNumSGPR();
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007398
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007399 if (NumSGPR != 0)
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007400 F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
7401 }
7402
7403 if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
7404 uint32_t NumVGPR = Attr->getNumVGPR();
7405
7406 if (NumVGPR != 0)
7407 F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007408 }
Yaxun Liuf2e8ab22016-07-19 19:39:45 +00007409}
Tony Linthicum76329bf2011-12-12 21:14:55 +00007410
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00007411unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
7412 return llvm::CallingConv::AMDGPU_KERNEL;
7413}
7414
Yaxun Liu402804b2016-12-15 08:09:08 +00007415// Currently LLVM assumes null pointers always have value 0,
7416// which results in incorrectly transformed IR. Therefore, instead of
7417// emitting null pointers in private and local address spaces, a null
7418// pointer in generic address space is emitted which is casted to a
7419// pointer in local or private address space.
7420llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
7421 const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
7422 QualType QT) const {
7423 if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
7424 return llvm::ConstantPointerNull::get(PT);
7425
7426 auto &Ctx = CGM.getContext();
7427 auto NPT = llvm::PointerType::get(PT->getElementType(),
7428 Ctx.getTargetAddressSpace(LangAS::opencl_generic));
7429 return llvm::ConstantExpr::getAddrSpaceCast(
7430 llvm::ConstantPointerNull::get(NPT), PT);
7431}
7432
Yaxun Liucbf647c2017-07-08 13:24:52 +00007433unsigned
7434AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
7435 const VarDecl *D) const {
7436 assert(!CGM.getLangOpts().OpenCL &&
7437 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
7438 "Address space agnostic languages only");
7439 unsigned DefaultGlobalAS =
7440 LangAS::FirstTargetAddressSpace +
7441 CGM.getContext().getTargetAddressSpace(LangAS::opencl_global);
7442 if (!D)
7443 return DefaultGlobalAS;
7444
7445 unsigned AddrSpace = D->getType().getAddressSpace();
7446 assert(AddrSpace == LangAS::Default ||
7447 AddrSpace >= LangAS::FirstTargetAddressSpace);
7448 if (AddrSpace != LangAS::Default)
7449 return AddrSpace;
7450
7451 if (CGM.isTypeConstant(D->getType(), false)) {
7452 if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
7453 return ConstAS.getValue();
7454 }
7455 return DefaultGlobalAS;
7456}
7457
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007458//===----------------------------------------------------------------------===//
Chris Dewhurst7e7ee962016-06-08 14:47:25 +00007459// SPARC v8 ABI Implementation.
7460// Based on the SPARC Compliance Definition version 2.4.1.
7461//
7462// Ensures that complex values are passed in registers.
7463//
7464namespace {
7465class SparcV8ABIInfo : public DefaultABIInfo {
7466public:
7467 SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7468
7469private:
7470 ABIArgInfo classifyReturnType(QualType RetTy) const;
7471 void computeInfo(CGFunctionInfo &FI) const override;
7472};
7473} // end anonymous namespace
7474
7475
7476ABIArgInfo
7477SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
7478 if (Ty->isAnyComplexType()) {
7479 return ABIArgInfo::getDirect();
7480 }
7481 else {
7482 return DefaultABIInfo::classifyReturnType(Ty);
7483 }
7484}
7485
7486void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
7487
7488 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7489 for (auto &Arg : FI.arguments())
7490 Arg.info = classifyArgumentType(Arg.type);
7491}
7492
7493namespace {
7494class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
7495public:
7496 SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
7497 : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {}
7498};
7499} // end anonymous namespace
7500
7501//===----------------------------------------------------------------------===//
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007502// SPARC v9 ABI Implementation.
7503// Based on the SPARC Compliance Definition version 2.4.1.
7504//
7505// Function arguments a mapped to a nominal "parameter array" and promoted to
7506// registers depending on their type. Each argument occupies 8 or 16 bytes in
7507// the array, structs larger than 16 bytes are passed indirectly.
7508//
7509// One case requires special care:
7510//
7511// struct mixed {
7512// int i;
7513// float f;
7514// };
7515//
7516// When a struct mixed is passed by value, it only occupies 8 bytes in the
7517// parameter array, but the int is passed in an integer register, and the float
7518// is passed in a floating point register. This is represented as two arguments
7519// with the LLVM IR inreg attribute:
7520//
7521// declare void f(i32 inreg %i, float inreg %f)
7522//
7523// The code generator will only allocate 4 bytes from the parameter array for
7524// the inreg arguments. All other arguments are allocated a multiple of 8
7525// bytes.
7526//
7527namespace {
7528class SparcV9ABIInfo : public ABIInfo {
7529public:
7530 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
7531
7532private:
7533 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
Craig Topper4f12f102014-03-12 06:41:41 +00007534 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00007535 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7536 QualType Ty) const override;
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007537
7538 // Coercion type builder for structs passed in registers. The coercion type
7539 // serves two purposes:
7540 //
7541 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
7542 // in registers.
7543 // 2. Expose aligned floating point elements as first-level elements, so the
7544 // code generator knows to pass them in floating point registers.
7545 //
7546 // We also compute the InReg flag which indicates that the struct contains
7547 // aligned 32-bit floats.
7548 //
7549 struct CoerceBuilder {
7550 llvm::LLVMContext &Context;
7551 const llvm::DataLayout &DL;
7552 SmallVector<llvm::Type*, 8> Elems;
7553 uint64_t Size;
7554 bool InReg;
7555
7556 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
7557 : Context(c), DL(dl), Size(0), InReg(false) {}
7558
7559 // Pad Elems with integers until Size is ToSize.
7560 void pad(uint64_t ToSize) {
7561 assert(ToSize >= Size && "Cannot remove elements");
7562 if (ToSize == Size)
7563 return;
7564
7565 // Finish the current 64-bit word.
Rui Ueyama83aa9792016-01-14 21:00:27 +00007566 uint64_t Aligned = llvm::alignTo(Size, 64);
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007567 if (Aligned > Size && Aligned <= ToSize) {
7568 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
7569 Size = Aligned;
7570 }
7571
7572 // Add whole 64-bit words.
7573 while (Size + 64 <= ToSize) {
7574 Elems.push_back(llvm::Type::getInt64Ty(Context));
7575 Size += 64;
7576 }
7577
7578 // Final in-word padding.
7579 if (Size < ToSize) {
7580 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
7581 Size = ToSize;
7582 }
7583 }
7584
7585 // Add a floating point element at Offset.
7586 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
7587 // Unaligned floats are treated as integers.
7588 if (Offset % Bits)
7589 return;
7590 // The InReg flag is only required if there are any floats < 64 bits.
7591 if (Bits < 64)
7592 InReg = true;
7593 pad(Offset);
7594 Elems.push_back(Ty);
7595 Size = Offset + Bits;
7596 }
7597
7598 // Add a struct type to the coercion type, starting at Offset (in bits).
7599 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
7600 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
7601 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
7602 llvm::Type *ElemTy = StrTy->getElementType(i);
7603 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
7604 switch (ElemTy->getTypeID()) {
7605 case llvm::Type::StructTyID:
7606 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
7607 break;
7608 case llvm::Type::FloatTyID:
7609 addFloat(ElemOffset, ElemTy, 32);
7610 break;
7611 case llvm::Type::DoubleTyID:
7612 addFloat(ElemOffset, ElemTy, 64);
7613 break;
7614 case llvm::Type::FP128TyID:
7615 addFloat(ElemOffset, ElemTy, 128);
7616 break;
7617 case llvm::Type::PointerTyID:
7618 if (ElemOffset % 64 == 0) {
7619 pad(ElemOffset);
7620 Elems.push_back(ElemTy);
7621 Size += 64;
7622 }
7623 break;
7624 default:
7625 break;
7626 }
7627 }
7628 }
7629
7630 // Check if Ty is a usable substitute for the coercion type.
7631 bool isUsableType(llvm::StructType *Ty) const {
Benjamin Kramer39ccabe2015-03-02 11:57:06 +00007632 return llvm::makeArrayRef(Elems) == Ty->elements();
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007633 }
7634
7635 // Get the coercion type as a literal struct type.
7636 llvm::Type *getType() const {
7637 if (Elems.size() == 1)
7638 return Elems.front();
7639 else
7640 return llvm::StructType::get(Context, Elems);
7641 }
7642 };
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007643};
7644} // end anonymous namespace
7645
7646ABIArgInfo
7647SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
7648 if (Ty->isVoidType())
7649 return ABIArgInfo::getIgnore();
7650
7651 uint64_t Size = getContext().getTypeSize(Ty);
7652
7653 // Anything too big to fit in registers is passed with an explicit indirect
7654 // pointer / sret pointer.
7655 if (Size > SizeLimit)
John McCall7f416cc2015-09-08 08:05:57 +00007656 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007657
7658 // Treat an enum type as its underlying type.
7659 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7660 Ty = EnumTy->getDecl()->getIntegerType();
7661
7662 // Integer types smaller than a register are extended.
7663 if (Size < 64 && Ty->isIntegerType())
7664 return ABIArgInfo::getExtend();
7665
7666 // Other non-aggregates go in registers.
7667 if (!isAggregateTypeForABI(Ty))
7668 return ABIArgInfo::getDirect();
7669
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00007670 // If a C++ object has either a non-trivial copy constructor or a non-trivial
7671 // destructor, it is passed with an explicit indirect pointer / sret pointer.
7672 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00007673 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00007674
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007675 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007676 // Build a coercion type from the LLVM struct type.
7677 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
7678 if (!StrTy)
7679 return ABIArgInfo::getDirect();
7680
7681 CoerceBuilder CB(getVMContext(), getDataLayout());
7682 CB.addStruct(0, StrTy);
Rui Ueyama83aa9792016-01-14 21:00:27 +00007683 CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007684
7685 // Try to use the original type for coercion.
7686 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
7687
7688 if (CB.InReg)
7689 return ABIArgInfo::getDirectInReg(CoerceTy);
7690 else
7691 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007692}
7693
John McCall7f416cc2015-09-08 08:05:57 +00007694Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7695 QualType Ty) const {
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007696 ABIArgInfo AI = classifyType(Ty, 16 * 8);
7697 llvm::Type *ArgTy = CGT.ConvertType(Ty);
7698 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
7699 AI.setCoerceToType(ArgTy);
7700
John McCall7f416cc2015-09-08 08:05:57 +00007701 CharUnits SlotSize = CharUnits::fromQuantity(8);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007702
John McCall7f416cc2015-09-08 08:05:57 +00007703 CGBuilderTy &Builder = CGF.Builder;
7704 Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
7705 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
7706
7707 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
7708
7709 Address ArgAddr = Address::invalid();
7710 CharUnits Stride;
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007711 switch (AI.getKind()) {
7712 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00007713 case ABIArgInfo::CoerceAndExpand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00007714 case ABIArgInfo::InAlloca:
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007715 llvm_unreachable("Unsupported ABI kind for va_arg");
7716
John McCall7f416cc2015-09-08 08:05:57 +00007717 case ABIArgInfo::Extend: {
7718 Stride = SlotSize;
7719 CharUnits Offset = SlotSize - TypeInfo.first;
7720 ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007721 break;
John McCall7f416cc2015-09-08 08:05:57 +00007722 }
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007723
John McCall7f416cc2015-09-08 08:05:57 +00007724 case ABIArgInfo::Direct: {
7725 auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
Rui Ueyama83aa9792016-01-14 21:00:27 +00007726 Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007727 ArgAddr = Addr;
7728 break;
John McCall7f416cc2015-09-08 08:05:57 +00007729 }
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007730
7731 case ABIArgInfo::Indirect:
John McCall7f416cc2015-09-08 08:05:57 +00007732 Stride = SlotSize;
7733 ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
7734 ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
7735 TypeInfo.second);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007736 break;
7737
7738 case ABIArgInfo::Ignore:
John McCall7f416cc2015-09-08 08:05:57 +00007739 return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007740 }
7741
7742 // Update VAList.
John McCall7f416cc2015-09-08 08:05:57 +00007743 llvm::Value *NextPtr =
7744 Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next");
7745 Builder.CreateStore(NextPtr, VAListAddr);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007746
John McCall7f416cc2015-09-08 08:05:57 +00007747 return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007748}
7749
7750void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
7751 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00007752 for (auto &I : FI.arguments())
7753 I.info = classifyType(I.type, 16 * 8);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007754}
7755
7756namespace {
7757class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
7758public:
7759 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
7760 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
Roman Divackyf02c9942014-02-24 18:46:27 +00007761
Craig Topper4f12f102014-03-12 06:41:41 +00007762 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyf02c9942014-02-24 18:46:27 +00007763 return 14;
7764 }
7765
7766 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00007767 llvm::Value *Address) const override;
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007768};
7769} // end anonymous namespace
7770
Roman Divackyf02c9942014-02-24 18:46:27 +00007771bool
7772SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7773 llvm::Value *Address) const {
7774 // This is calculated from the LLVM and GCC tables and verified
7775 // against gcc output. AFAIK all ABIs use the same encoding.
7776
7777 CodeGen::CGBuilderTy &Builder = CGF.Builder;
7778
7779 llvm::IntegerType *i8 = CGF.Int8Ty;
7780 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
7781 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
7782
7783 // 0-31: the 8-byte general-purpose registers
7784 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
7785
7786 // 32-63: f0-31, the 4-byte floating-point registers
7787 AssignToArrayRange(Builder, Address, Four8, 32, 63);
7788
7789 // Y = 64
7790 // PSR = 65
7791 // WIM = 66
7792 // TBR = 67
7793 // PC = 68
7794 // NPC = 69
7795 // FSR = 70
7796 // CSR = 71
7797 AssignToArrayRange(Builder, Address, Eight8, 64, 71);
Eric Christopher7565e0d2015-05-29 23:09:49 +00007798
Roman Divackyf02c9942014-02-24 18:46:27 +00007799 // 72-87: d0-15, the 8-byte floating-point registers
7800 AssignToArrayRange(Builder, Address, Eight8, 72, 87);
7801
7802 return false;
7803}
7804
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007805
Robert Lytton0e076492013-08-13 09:43:10 +00007806//===----------------------------------------------------------------------===//
Robert Lyttond21e2d72014-03-03 13:45:29 +00007807// XCore ABI Implementation
Robert Lytton0e076492013-08-13 09:43:10 +00007808//===----------------------------------------------------------------------===//
Robert Lytton844aeeb2014-05-02 09:33:20 +00007809
Robert Lytton0e076492013-08-13 09:43:10 +00007810namespace {
Robert Lytton844aeeb2014-05-02 09:33:20 +00007811
7812/// A SmallStringEnc instance is used to build up the TypeString by passing
7813/// it by reference between functions that append to it.
7814typedef llvm::SmallString<128> SmallStringEnc;
7815
7816/// TypeStringCache caches the meta encodings of Types.
7817///
7818/// The reason for caching TypeStrings is two fold:
7819/// 1. To cache a type's encoding for later uses;
7820/// 2. As a means to break recursive member type inclusion.
7821///
7822/// A cache Entry can have a Status of:
7823/// NonRecursive: The type encoding is not recursive;
7824/// Recursive: The type encoding is recursive;
7825/// Incomplete: An incomplete TypeString;
7826/// IncompleteUsed: An incomplete TypeString that has been used in a
7827/// Recursive type encoding.
7828///
7829/// A NonRecursive entry will have all of its sub-members expanded as fully
7830/// as possible. Whilst it may contain types which are recursive, the type
7831/// itself is not recursive and thus its encoding may be safely used whenever
7832/// the type is encountered.
7833///
7834/// A Recursive entry will have all of its sub-members expanded as fully as
7835/// possible. The type itself is recursive and it may contain other types which
7836/// are recursive. The Recursive encoding must not be used during the expansion
7837/// of a recursive type's recursive branch. For simplicity the code uses
7838/// IncompleteCount to reject all usage of Recursive encodings for member types.
7839///
7840/// An Incomplete entry is always a RecordType and only encodes its
7841/// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
7842/// are placed into the cache during type expansion as a means to identify and
7843/// handle recursive inclusion of types as sub-members. If there is recursion
7844/// the entry becomes IncompleteUsed.
7845///
7846/// During the expansion of a RecordType's members:
7847///
7848/// If the cache contains a NonRecursive encoding for the member type, the
7849/// cached encoding is used;
7850///
7851/// If the cache contains a Recursive encoding for the member type, the
7852/// cached encoding is 'Swapped' out, as it may be incorrect, and...
7853///
7854/// If the member is a RecordType, an Incomplete encoding is placed into the
7855/// cache to break potential recursive inclusion of itself as a sub-member;
7856///
7857/// Once a member RecordType has been expanded, its temporary incomplete
7858/// entry is removed from the cache. If a Recursive encoding was swapped out
7859/// it is swapped back in;
7860///
7861/// If an incomplete entry is used to expand a sub-member, the incomplete
7862/// entry is marked as IncompleteUsed. The cache keeps count of how many
7863/// IncompleteUsed entries it currently contains in IncompleteUsedCount;
7864///
7865/// If a member's encoding is found to be a NonRecursive or Recursive viz:
7866/// IncompleteUsedCount==0, the member's encoding is added to the cache.
7867/// Else the member is part of a recursive type and thus the recursion has
7868/// been exited too soon for the encoding to be correct for the member.
7869///
7870class TypeStringCache {
7871 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
7872 struct Entry {
7873 std::string Str; // The encoded TypeString for the type.
7874 enum Status State; // Information about the encoding in 'Str'.
7875 std::string Swapped; // A temporary place holder for a Recursive encoding
7876 // during the expansion of RecordType's members.
7877 };
7878 std::map<const IdentifierInfo *, struct Entry> Map;
7879 unsigned IncompleteCount; // Number of Incomplete entries in the Map.
7880 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
7881public:
Hans Wennborg4afe5042015-07-22 20:46:26 +00007882 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
Robert Lytton844aeeb2014-05-02 09:33:20 +00007883 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
7884 bool removeIncomplete(const IdentifierInfo *ID);
7885 void addIfComplete(const IdentifierInfo *ID, StringRef Str,
7886 bool IsRecursive);
7887 StringRef lookupStr(const IdentifierInfo *ID);
7888};
7889
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00007890/// TypeString encodings for enum & union fields must be order.
Robert Lytton844aeeb2014-05-02 09:33:20 +00007891/// FieldEncoding is a helper for this ordering process.
7892class FieldEncoding {
7893 bool HasName;
7894 std::string Enc;
7895public:
Hans Wennborg4afe5042015-07-22 20:46:26 +00007896 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
Malcolm Parsonsf76f6502016-11-02 10:39:27 +00007897 StringRef str() { return Enc; }
Robert Lytton844aeeb2014-05-02 09:33:20 +00007898 bool operator<(const FieldEncoding &rhs) const {
7899 if (HasName != rhs.HasName) return HasName;
7900 return Enc < rhs.Enc;
7901 }
7902};
7903
Robert Lytton7d1db152013-08-19 09:46:39 +00007904class XCoreABIInfo : public DefaultABIInfo {
7905public:
7906 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
John McCall7f416cc2015-09-08 08:05:57 +00007907 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7908 QualType Ty) const override;
Robert Lytton7d1db152013-08-19 09:46:39 +00007909};
7910
Robert Lyttond21e2d72014-03-03 13:45:29 +00007911class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
Robert Lytton844aeeb2014-05-02 09:33:20 +00007912 mutable TypeStringCache TSC;
Robert Lytton0e076492013-08-13 09:43:10 +00007913public:
Robert Lyttond21e2d72014-03-03 13:45:29 +00007914 XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
Robert Lytton7d1db152013-08-19 09:46:39 +00007915 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
Rafael Espindola8dcd6e72014-05-08 15:01:48 +00007916 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7917 CodeGen::CodeGenModule &M) const override;
Robert Lytton0e076492013-08-13 09:43:10 +00007918};
Robert Lytton844aeeb2014-05-02 09:33:20 +00007919
Robert Lytton2d196952013-10-11 10:29:34 +00007920} // End anonymous namespace.
Robert Lytton0e076492013-08-13 09:43:10 +00007921
James Y Knight29b5f082016-02-24 02:59:33 +00007922// TODO: this implementation is likely now redundant with the default
7923// EmitVAArg.
John McCall7f416cc2015-09-08 08:05:57 +00007924Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7925 QualType Ty) const {
Robert Lytton7d1db152013-08-19 09:46:39 +00007926 CGBuilderTy &Builder = CGF.Builder;
Robert Lytton7d1db152013-08-19 09:46:39 +00007927
Robert Lytton2d196952013-10-11 10:29:34 +00007928 // Get the VAList.
John McCall7f416cc2015-09-08 08:05:57 +00007929 CharUnits SlotSize = CharUnits::fromQuantity(4);
7930 Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
Robert Lytton7d1db152013-08-19 09:46:39 +00007931
Robert Lytton2d196952013-10-11 10:29:34 +00007932 // Handle the argument.
7933 ABIArgInfo AI = classifyArgumentType(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00007934 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
Robert Lytton2d196952013-10-11 10:29:34 +00007935 llvm::Type *ArgTy = CGT.ConvertType(Ty);
7936 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
7937 AI.setCoerceToType(ArgTy);
Robert Lytton7d1db152013-08-19 09:46:39 +00007938 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
John McCall7f416cc2015-09-08 08:05:57 +00007939
7940 Address Val = Address::invalid();
7941 CharUnits ArgSize = CharUnits::Zero();
Robert Lytton7d1db152013-08-19 09:46:39 +00007942 switch (AI.getKind()) {
Robert Lytton7d1db152013-08-19 09:46:39 +00007943 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00007944 case ABIArgInfo::CoerceAndExpand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00007945 case ABIArgInfo::InAlloca:
Robert Lytton7d1db152013-08-19 09:46:39 +00007946 llvm_unreachable("Unsupported ABI kind for va_arg");
7947 case ABIArgInfo::Ignore:
John McCall7f416cc2015-09-08 08:05:57 +00007948 Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
7949 ArgSize = CharUnits::Zero();
Robert Lytton2d196952013-10-11 10:29:34 +00007950 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00007951 case ABIArgInfo::Extend:
7952 case ABIArgInfo::Direct:
John McCall7f416cc2015-09-08 08:05:57 +00007953 Val = Builder.CreateBitCast(AP, ArgPtrTy);
7954 ArgSize = CharUnits::fromQuantity(
7955 getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
Rui Ueyama83aa9792016-01-14 21:00:27 +00007956 ArgSize = ArgSize.alignTo(SlotSize);
Robert Lytton2d196952013-10-11 10:29:34 +00007957 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00007958 case ABIArgInfo::Indirect:
John McCall7f416cc2015-09-08 08:05:57 +00007959 Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
7960 Val = Address(Builder.CreateLoad(Val), TypeAlign);
7961 ArgSize = SlotSize;
Robert Lytton2d196952013-10-11 10:29:34 +00007962 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00007963 }
Robert Lytton2d196952013-10-11 10:29:34 +00007964
7965 // Increment the VAList.
John McCall7f416cc2015-09-08 08:05:57 +00007966 if (!ArgSize.isZero()) {
7967 llvm::Value *APN =
7968 Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize);
7969 Builder.CreateStore(APN, VAListAddr);
Robert Lytton2d196952013-10-11 10:29:34 +00007970 }
John McCall7f416cc2015-09-08 08:05:57 +00007971
Robert Lytton2d196952013-10-11 10:29:34 +00007972 return Val;
Robert Lytton7d1db152013-08-19 09:46:39 +00007973}
Robert Lytton0e076492013-08-13 09:43:10 +00007974
Robert Lytton844aeeb2014-05-02 09:33:20 +00007975/// During the expansion of a RecordType, an incomplete TypeString is placed
7976/// into the cache as a means to identify and break recursion.
7977/// If there is a Recursive encoding in the cache, it is swapped out and will
7978/// be reinserted by removeIncomplete().
7979/// All other types of encoding should have been used rather than arriving here.
7980void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
7981 std::string StubEnc) {
7982 if (!ID)
7983 return;
7984 Entry &E = Map[ID];
7985 assert( (E.Str.empty() || E.State == Recursive) &&
7986 "Incorrectly use of addIncomplete");
7987 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
7988 E.Swapped.swap(E.Str); // swap out the Recursive
7989 E.Str.swap(StubEnc);
7990 E.State = Incomplete;
7991 ++IncompleteCount;
7992}
7993
7994/// Once the RecordType has been expanded, the temporary incomplete TypeString
7995/// must be removed from the cache.
7996/// If a Recursive was swapped out by addIncomplete(), it will be replaced.
7997/// Returns true if the RecordType was defined recursively.
7998bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
7999 if (!ID)
8000 return false;
8001 auto I = Map.find(ID);
8002 assert(I != Map.end() && "Entry not present");
8003 Entry &E = I->second;
8004 assert( (E.State == Incomplete ||
8005 E.State == IncompleteUsed) &&
8006 "Entry must be an incomplete type");
8007 bool IsRecursive = false;
8008 if (E.State == IncompleteUsed) {
8009 // We made use of our Incomplete encoding, thus we are recursive.
8010 IsRecursive = true;
8011 --IncompleteUsedCount;
8012 }
8013 if (E.Swapped.empty())
8014 Map.erase(I);
8015 else {
8016 // Swap the Recursive back.
8017 E.Swapped.swap(E.Str);
8018 E.Swapped.clear();
8019 E.State = Recursive;
8020 }
8021 --IncompleteCount;
8022 return IsRecursive;
8023}
8024
8025/// Add the encoded TypeString to the cache only if it is NonRecursive or
8026/// Recursive (viz: all sub-members were expanded as fully as possible).
8027void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
8028 bool IsRecursive) {
8029 if (!ID || IncompleteUsedCount)
8030 return; // No key or it is is an incomplete sub-type so don't add.
8031 Entry &E = Map[ID];
8032 if (IsRecursive && !E.Str.empty()) {
8033 assert(E.State==Recursive && E.Str.size() == Str.size() &&
8034 "This is not the same Recursive entry");
8035 // The parent container was not recursive after all, so we could have used
8036 // this Recursive sub-member entry after all, but we assumed the worse when
8037 // we started viz: IncompleteCount!=0.
8038 return;
8039 }
8040 assert(E.Str.empty() && "Entry already present");
8041 E.Str = Str.str();
8042 E.State = IsRecursive? Recursive : NonRecursive;
8043}
8044
8045/// Return a cached TypeString encoding for the ID. If there isn't one, or we
8046/// are recursively expanding a type (IncompleteCount != 0) and the cached
8047/// encoding is Recursive, return an empty StringRef.
8048StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
8049 if (!ID)
8050 return StringRef(); // We have no key.
8051 auto I = Map.find(ID);
8052 if (I == Map.end())
8053 return StringRef(); // We have no encoding.
8054 Entry &E = I->second;
8055 if (E.State == Recursive && IncompleteCount)
8056 return StringRef(); // We don't use Recursive encodings for member types.
8057
8058 if (E.State == Incomplete) {
8059 // The incomplete type is being used to break out of recursion.
8060 E.State = IncompleteUsed;
8061 ++IncompleteUsedCount;
8062 }
Malcolm Parsonsf76f6502016-11-02 10:39:27 +00008063 return E.Str;
Robert Lytton844aeeb2014-05-02 09:33:20 +00008064}
8065
8066/// The XCore ABI includes a type information section that communicates symbol
8067/// type information to the linker. The linker uses this information to verify
8068/// safety/correctness of things such as array bound and pointers et al.
8069/// The ABI only requires C (and XC) language modules to emit TypeStrings.
8070/// This type information (TypeString) is emitted into meta data for all global
8071/// symbols: definitions, declarations, functions & variables.
8072///
8073/// The TypeString carries type, qualifier, name, size & value details.
8074/// Please see 'Tools Development Guide' section 2.16.2 for format details:
Eric Christopher7565e0d2015-05-29 23:09:49 +00008075/// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
Robert Lytton844aeeb2014-05-02 09:33:20 +00008076/// The output is tested by test/CodeGen/xcore-stringtype.c.
8077///
8078static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
8079 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
8080
8081/// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
8082void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
8083 CodeGen::CodeGenModule &CGM) const {
8084 SmallStringEnc Enc;
8085 if (getTypeString(Enc, D, CGM, TSC)) {
8086 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
Benjamin Kramer30934732016-07-02 11:41:41 +00008087 llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
8088 llvm::MDString::get(Ctx, Enc.str())};
Robert Lytton844aeeb2014-05-02 09:33:20 +00008089 llvm::NamedMDNode *MD =
8090 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
8091 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
8092 }
8093}
8094
Xiuli Pan972bea82016-03-24 03:57:17 +00008095//===----------------------------------------------------------------------===//
8096// SPIR ABI Implementation
8097//===----------------------------------------------------------------------===//
8098
8099namespace {
8100class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
8101public:
8102 SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
8103 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00008104 unsigned getOpenCLKernelCallingConv() const override;
Xiuli Pan972bea82016-03-24 03:57:17 +00008105};
Pekka Jaaskelainenfc2629a2017-06-01 07:18:49 +00008106
Xiuli Pan972bea82016-03-24 03:57:17 +00008107} // End anonymous namespace.
8108
Pekka Jaaskelainenfc2629a2017-06-01 07:18:49 +00008109namespace clang {
8110namespace CodeGen {
8111void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
8112 DefaultABIInfo SPIRABI(CGM.getTypes());
8113 SPIRABI.computeInfo(FI);
8114}
8115}
8116}
8117
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00008118unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
8119 return llvm::CallingConv::SPIR_KERNEL;
8120}
8121
Robert Lytton844aeeb2014-05-02 09:33:20 +00008122static bool appendType(SmallStringEnc &Enc, QualType QType,
8123 const CodeGen::CodeGenModule &CGM,
8124 TypeStringCache &TSC);
8125
8126/// Helper function for appendRecordType().
Eric Christopher7565e0d2015-05-29 23:09:49 +00008127/// Builds a SmallVector containing the encoded field types in declaration
8128/// order.
Robert Lytton844aeeb2014-05-02 09:33:20 +00008129static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
8130 const RecordDecl *RD,
8131 const CodeGen::CodeGenModule &CGM,
8132 TypeStringCache &TSC) {
Hans Wennborga302cd92014-08-21 16:06:57 +00008133 for (const auto *Field : RD->fields()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008134 SmallStringEnc Enc;
8135 Enc += "m(";
Hans Wennborga302cd92014-08-21 16:06:57 +00008136 Enc += Field->getName();
Robert Lytton844aeeb2014-05-02 09:33:20 +00008137 Enc += "){";
Hans Wennborga302cd92014-08-21 16:06:57 +00008138 if (Field->isBitField()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008139 Enc += "b(";
8140 llvm::raw_svector_ostream OS(Enc);
Hans Wennborga302cd92014-08-21 16:06:57 +00008141 OS << Field->getBitWidthValue(CGM.getContext());
Robert Lytton844aeeb2014-05-02 09:33:20 +00008142 Enc += ':';
8143 }
Hans Wennborga302cd92014-08-21 16:06:57 +00008144 if (!appendType(Enc, Field->getType(), CGM, TSC))
Robert Lytton844aeeb2014-05-02 09:33:20 +00008145 return false;
Hans Wennborga302cd92014-08-21 16:06:57 +00008146 if (Field->isBitField())
Robert Lytton844aeeb2014-05-02 09:33:20 +00008147 Enc += ')';
8148 Enc += '}';
Benjamin Kramer3204b152015-05-29 19:42:19 +00008149 FE.emplace_back(!Field->getName().empty(), Enc);
Robert Lytton844aeeb2014-05-02 09:33:20 +00008150 }
8151 return true;
8152}
8153
8154/// Appends structure and union types to Enc and adds encoding to cache.
8155/// Recursively calls appendType (via extractFieldType) for each field.
8156/// Union types have their fields ordered according to the ABI.
8157static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
8158 const CodeGen::CodeGenModule &CGM,
8159 TypeStringCache &TSC, const IdentifierInfo *ID) {
8160 // Append the cached TypeString if we have one.
8161 StringRef TypeString = TSC.lookupStr(ID);
8162 if (!TypeString.empty()) {
8163 Enc += TypeString;
8164 return true;
8165 }
8166
8167 // Start to emit an incomplete TypeString.
8168 size_t Start = Enc.size();
8169 Enc += (RT->isUnionType()? 'u' : 's');
8170 Enc += '(';
8171 if (ID)
8172 Enc += ID->getName();
8173 Enc += "){";
8174
8175 // We collect all encoded fields and order as necessary.
8176 bool IsRecursive = false;
Robert Lytton844aeeb2014-05-02 09:33:20 +00008177 const RecordDecl *RD = RT->getDecl()->getDefinition();
8178 if (RD && !RD->field_empty()) {
8179 // An incomplete TypeString stub is placed in the cache for this RecordType
8180 // so that recursive calls to this RecordType will use it whilst building a
8181 // complete TypeString for this RecordType.
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008182 SmallVector<FieldEncoding, 16> FE;
Robert Lytton844aeeb2014-05-02 09:33:20 +00008183 std::string StubEnc(Enc.substr(Start).str());
8184 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
8185 TSC.addIncomplete(ID, std::move(StubEnc));
8186 if (!extractFieldType(FE, RD, CGM, TSC)) {
8187 (void) TSC.removeIncomplete(ID);
8188 return false;
8189 }
8190 IsRecursive = TSC.removeIncomplete(ID);
8191 // The ABI requires unions to be sorted but not structures.
8192 // See FieldEncoding::operator< for sort algorithm.
8193 if (RT->isUnionType())
8194 std::sort(FE.begin(), FE.end());
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008195 // We can now complete the TypeString.
8196 unsigned E = FE.size();
Robert Lytton844aeeb2014-05-02 09:33:20 +00008197 for (unsigned I = 0; I != E; ++I) {
8198 if (I)
8199 Enc += ',';
8200 Enc += FE[I].str();
8201 }
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008202 }
Robert Lytton844aeeb2014-05-02 09:33:20 +00008203 Enc += '}';
8204 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
8205 return true;
8206}
8207
8208/// Appends enum types to Enc and adds the encoding to the cache.
8209static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
8210 TypeStringCache &TSC,
8211 const IdentifierInfo *ID) {
8212 // Append the cached TypeString if we have one.
8213 StringRef TypeString = TSC.lookupStr(ID);
8214 if (!TypeString.empty()) {
8215 Enc += TypeString;
8216 return true;
8217 }
8218
8219 size_t Start = Enc.size();
8220 Enc += "e(";
8221 if (ID)
8222 Enc += ID->getName();
8223 Enc += "){";
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008224
8225 // We collect all encoded enumerations and order them alphanumerically.
Robert Lytton844aeeb2014-05-02 09:33:20 +00008226 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008227 SmallVector<FieldEncoding, 16> FE;
8228 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
8229 ++I) {
8230 SmallStringEnc EnumEnc;
8231 EnumEnc += "m(";
8232 EnumEnc += I->getName();
8233 EnumEnc += "){";
8234 I->getInitVal().toString(EnumEnc);
8235 EnumEnc += '}';
8236 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
8237 }
8238 std::sort(FE.begin(), FE.end());
8239 unsigned E = FE.size();
8240 for (unsigned I = 0; I != E; ++I) {
8241 if (I)
Robert Lytton844aeeb2014-05-02 09:33:20 +00008242 Enc += ',';
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008243 Enc += FE[I].str();
Robert Lytton844aeeb2014-05-02 09:33:20 +00008244 }
8245 }
8246 Enc += '}';
8247 TSC.addIfComplete(ID, Enc.substr(Start), false);
8248 return true;
8249}
8250
8251/// Appends type's qualifier to Enc.
8252/// This is done prior to appending the type's encoding.
8253static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
8254 // Qualifiers are emitted in alphabetical order.
Craig Topper273dbc62015-10-18 05:29:26 +00008255 static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
Robert Lytton844aeeb2014-05-02 09:33:20 +00008256 int Lookup = 0;
8257 if (QT.isConstQualified())
8258 Lookup += 1<<0;
8259 if (QT.isRestrictQualified())
8260 Lookup += 1<<1;
8261 if (QT.isVolatileQualified())
8262 Lookup += 1<<2;
8263 Enc += Table[Lookup];
8264}
8265
8266/// Appends built-in types to Enc.
8267static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
8268 const char *EncType;
8269 switch (BT->getKind()) {
8270 case BuiltinType::Void:
8271 EncType = "0";
8272 break;
8273 case BuiltinType::Bool:
8274 EncType = "b";
8275 break;
8276 case BuiltinType::Char_U:
8277 EncType = "uc";
8278 break;
8279 case BuiltinType::UChar:
8280 EncType = "uc";
8281 break;
8282 case BuiltinType::SChar:
8283 EncType = "sc";
8284 break;
8285 case BuiltinType::UShort:
8286 EncType = "us";
8287 break;
8288 case BuiltinType::Short:
8289 EncType = "ss";
8290 break;
8291 case BuiltinType::UInt:
8292 EncType = "ui";
8293 break;
8294 case BuiltinType::Int:
8295 EncType = "si";
8296 break;
8297 case BuiltinType::ULong:
8298 EncType = "ul";
8299 break;
8300 case BuiltinType::Long:
8301 EncType = "sl";
8302 break;
8303 case BuiltinType::ULongLong:
8304 EncType = "ull";
8305 break;
8306 case BuiltinType::LongLong:
8307 EncType = "sll";
8308 break;
8309 case BuiltinType::Float:
8310 EncType = "ft";
8311 break;
8312 case BuiltinType::Double:
8313 EncType = "d";
8314 break;
8315 case BuiltinType::LongDouble:
8316 EncType = "ld";
8317 break;
8318 default:
8319 return false;
8320 }
8321 Enc += EncType;
8322 return true;
8323}
8324
8325/// Appends a pointer encoding to Enc before calling appendType for the pointee.
8326static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
8327 const CodeGen::CodeGenModule &CGM,
8328 TypeStringCache &TSC) {
8329 Enc += "p(";
8330 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
8331 return false;
8332 Enc += ')';
8333 return true;
8334}
8335
8336/// Appends array encoding to Enc before calling appendType for the element.
Robert Lytton6adb20f2014-06-05 09:06:21 +00008337static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
8338 const ArrayType *AT,
Robert Lytton844aeeb2014-05-02 09:33:20 +00008339 const CodeGen::CodeGenModule &CGM,
8340 TypeStringCache &TSC, StringRef NoSizeEnc) {
8341 if (AT->getSizeModifier() != ArrayType::Normal)
8342 return false;
8343 Enc += "a(";
8344 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
8345 CAT->getSize().toStringUnsigned(Enc);
8346 else
8347 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
8348 Enc += ':';
Robert Lytton6adb20f2014-06-05 09:06:21 +00008349 // The Qualifiers should be attached to the type rather than the array.
8350 appendQualifier(Enc, QT);
Robert Lytton844aeeb2014-05-02 09:33:20 +00008351 if (!appendType(Enc, AT->getElementType(), CGM, TSC))
8352 return false;
8353 Enc += ')';
8354 return true;
8355}
8356
8357/// Appends a function encoding to Enc, calling appendType for the return type
8358/// and the arguments.
8359static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
8360 const CodeGen::CodeGenModule &CGM,
8361 TypeStringCache &TSC) {
8362 Enc += "f{";
8363 if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
8364 return false;
8365 Enc += "}(";
8366 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
8367 // N.B. we are only interested in the adjusted param types.
8368 auto I = FPT->param_type_begin();
8369 auto E = FPT->param_type_end();
8370 if (I != E) {
8371 do {
8372 if (!appendType(Enc, *I, CGM, TSC))
8373 return false;
8374 ++I;
8375 if (I != E)
8376 Enc += ',';
8377 } while (I != E);
8378 if (FPT->isVariadic())
8379 Enc += ",va";
8380 } else {
8381 if (FPT->isVariadic())
8382 Enc += "va";
8383 else
8384 Enc += '0';
8385 }
8386 }
8387 Enc += ')';
8388 return true;
8389}
8390
8391/// Handles the type's qualifier before dispatching a call to handle specific
8392/// type encodings.
8393static bool appendType(SmallStringEnc &Enc, QualType QType,
8394 const CodeGen::CodeGenModule &CGM,
8395 TypeStringCache &TSC) {
8396
8397 QualType QT = QType.getCanonicalType();
8398
Robert Lytton6adb20f2014-06-05 09:06:21 +00008399 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
8400 // The Qualifiers should be attached to the type rather than the array.
8401 // Thus we don't call appendQualifier() here.
8402 return appendArrayType(Enc, QT, AT, CGM, TSC, "");
8403
Robert Lytton844aeeb2014-05-02 09:33:20 +00008404 appendQualifier(Enc, QT);
8405
8406 if (const BuiltinType *BT = QT->getAs<BuiltinType>())
8407 return appendBuiltinType(Enc, BT);
8408
Robert Lytton844aeeb2014-05-02 09:33:20 +00008409 if (const PointerType *PT = QT->getAs<PointerType>())
8410 return appendPointerType(Enc, PT, CGM, TSC);
8411
8412 if (const EnumType *ET = QT->getAs<EnumType>())
8413 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
8414
8415 if (const RecordType *RT = QT->getAsStructureType())
8416 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
8417
8418 if (const RecordType *RT = QT->getAsUnionType())
8419 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
8420
8421 if (const FunctionType *FT = QT->getAs<FunctionType>())
8422 return appendFunctionType(Enc, FT, CGM, TSC);
8423
8424 return false;
8425}
8426
8427static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
8428 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
8429 if (!D)
8430 return false;
8431
8432 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
8433 if (FD->getLanguageLinkage() != CLanguageLinkage)
8434 return false;
8435 return appendType(Enc, FD->getType(), CGM, TSC);
8436 }
8437
8438 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
8439 if (VD->getLanguageLinkage() != CLanguageLinkage)
8440 return false;
8441 QualType QT = VD->getType().getCanonicalType();
8442 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
8443 // Global ArrayTypes are given a size of '*' if the size is unknown.
Robert Lytton6adb20f2014-06-05 09:06:21 +00008444 // The Qualifiers should be attached to the type rather than the array.
8445 // Thus we don't call appendQualifier() here.
8446 return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
Robert Lytton844aeeb2014-05-02 09:33:20 +00008447 }
8448 return appendType(Enc, QT, CGM, TSC);
8449 }
8450 return false;
8451}
8452
8453
Robert Lytton0e076492013-08-13 09:43:10 +00008454//===----------------------------------------------------------------------===//
8455// Driver code
8456//===----------------------------------------------------------------------===//
8457
Rafael Espindola9f834732014-09-19 01:54:22 +00008458bool CodeGenModule::supportsCOMDAT() const {
Xinliang David Li865cfdd2016-05-25 17:25:57 +00008459 return getTriple().supportsCOMDAT();
Rafael Espindola9f834732014-09-19 01:54:22 +00008460}
8461
Chris Lattner2b037972010-07-29 02:01:43 +00008462const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00008463 if (TheTargetCodeGenInfo)
8464 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00008465
Reid Kleckner9305fd12016-04-13 23:37:17 +00008466 // Helper to set the unique_ptr while still keeping the return value.
8467 auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
8468 this->TheTargetCodeGenInfo.reset(P);
8469 return *P;
8470 };
8471
John McCallc8e01702013-04-16 22:48:15 +00008472 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00008473 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00008474 default:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008475 return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00008476
Derek Schuff09338a22012-09-06 17:37:28 +00008477 case llvm::Triple::le32:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008478 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00008479 case llvm::Triple::mips:
8480 case llvm::Triple::mipsel:
Petar Jovanovic26a4a402015-07-08 13:07:31 +00008481 if (Triple.getOS() == llvm::Triple::NaCl)
Reid Kleckner9305fd12016-04-13 23:37:17 +00008482 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
8483 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
Akira Hatanakac4baedd2013-11-11 22:10:46 +00008484
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00008485 case llvm::Triple::mips64:
8486 case llvm::Triple::mips64el:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008487 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanakac4baedd2013-11-11 22:10:46 +00008488
Dylan McKaye8232d72017-02-08 05:09:26 +00008489 case llvm::Triple::avr:
8490 return SetCGInfo(new AVRTargetCodeGenInfo(Types));
8491
Tim Northover25e8a672014-05-24 12:51:25 +00008492 case llvm::Triple::aarch64:
Tim Northover40956e62014-07-23 12:32:58 +00008493 case llvm::Triple::aarch64_be: {
Tim Northover573cbee2014-05-24 12:52:07 +00008494 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
Alp Toker4925ba72014-06-07 23:30:42 +00008495 if (getTarget().getABI() == "darwinpcs")
Tim Northover573cbee2014-05-24 12:52:07 +00008496 Kind = AArch64ABIInfo::DarwinPCS;
Tim Northovera2ee4332014-03-29 15:09:45 +00008497
Reid Kleckner9305fd12016-04-13 23:37:17 +00008498 return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
Tim Northovera2ee4332014-03-29 15:09:45 +00008499 }
8500
Dan Gohmanc2853072015-09-03 22:51:53 +00008501 case llvm::Triple::wasm32:
8502 case llvm::Triple::wasm64:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008503 return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types));
Dan Gohmanc2853072015-09-03 22:51:53 +00008504
Daniel Dunbard59655c2009-09-12 00:59:49 +00008505 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +00008506 case llvm::Triple::armeb:
Daniel Dunbard59655c2009-09-12 00:59:49 +00008507 case llvm::Triple::thumb:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008508 case llvm::Triple::thumbeb: {
8509 if (Triple.getOS() == llvm::Triple::Win32) {
8510 return SetCGInfo(
8511 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
Sandeep Patel45df3dd2011-04-05 00:23:47 +00008512 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00008513
Reid Kleckner9305fd12016-04-13 23:37:17 +00008514 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
8515 StringRef ABIStr = getTarget().getABI();
8516 if (ABIStr == "apcs-gnu")
8517 Kind = ARMABIInfo::APCS;
8518 else if (ABIStr == "aapcs16")
8519 Kind = ARMABIInfo::AAPCS16_VFP;
8520 else if (CodeGenOpts.FloatABI == "hard" ||
8521 (CodeGenOpts.FloatABI != "soft" &&
Oleg Ranevskyy7232f662016-05-13 14:45:57 +00008522 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
Rafael Espindola0fa66802016-06-24 21:35:06 +00008523 Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
Oleg Ranevskyy7232f662016-05-13 14:45:57 +00008524 Triple.getEnvironment() == llvm::Triple::EABIHF)))
Reid Kleckner9305fd12016-04-13 23:37:17 +00008525 Kind = ARMABIInfo::AAPCS_VFP;
8526
8527 return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
8528 }
8529
John McCallea8d8bb2010-03-11 00:10:12 +00008530 case llvm::Triple::ppc:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008531 return SetCGInfo(
8532 new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft"));
Roman Divackyd966e722012-05-09 18:22:46 +00008533 case llvm::Triple::ppc64:
Ulrich Weigandb7122372014-07-21 00:48:09 +00008534 if (Triple.isOSBinFormatELF()) {
Ulrich Weigandb7122372014-07-21 00:48:09 +00008535 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
Ulrich Weigand8afad612014-07-28 13:17:52 +00008536 if (getTarget().getABI() == "elfv2")
8537 Kind = PPC64_SVR4_ABIInfo::ELFv2;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00008538 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Hal Finkel415c2a32016-10-02 02:10:45 +00008539 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
Ulrich Weigand8afad612014-07-28 13:17:52 +00008540
Hal Finkel415c2a32016-10-02 02:10:45 +00008541 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
8542 IsSoftFloat));
Ulrich Weigandb7122372014-07-21 00:48:09 +00008543 } else
Reid Kleckner9305fd12016-04-13 23:37:17 +00008544 return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
Ulrich Weigandb7122372014-07-21 00:48:09 +00008545 case llvm::Triple::ppc64le: {
Bill Schmidt778d3872013-07-26 01:36:11 +00008546 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
Ulrich Weigandb7122372014-07-21 00:48:09 +00008547 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00008548 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
Ulrich Weigand8afad612014-07-28 13:17:52 +00008549 Kind = PPC64_SVR4_ABIInfo::ELFv1;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00008550 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Hal Finkel415c2a32016-10-02 02:10:45 +00008551 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
Ulrich Weigand8afad612014-07-28 13:17:52 +00008552
Hal Finkel415c2a32016-10-02 02:10:45 +00008553 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
8554 IsSoftFloat));
Ulrich Weigandb7122372014-07-21 00:48:09 +00008555 }
John McCallea8d8bb2010-03-11 00:10:12 +00008556
Peter Collingbournec947aae2012-05-20 23:28:41 +00008557 case llvm::Triple::nvptx:
8558 case llvm::Triple::nvptx64:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008559 return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00008560
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00008561 case llvm::Triple::msp430:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008562 return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00008563
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00008564 case llvm::Triple::systemz: {
8565 bool HasVector = getTarget().getABI() == "vector";
Reid Kleckner9305fd12016-04-13 23:37:17 +00008566 return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector));
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00008567 }
Ulrich Weigand47445072013-05-06 16:26:41 +00008568
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00008569 case llvm::Triple::tce:
Pekka Jaaskelainen67354482016-11-16 15:22:31 +00008570 case llvm::Triple::tcele:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008571 return SetCGInfo(new TCETargetCodeGenInfo(Types));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00008572
Eli Friedman33465822011-07-08 23:31:17 +00008573 case llvm::Triple::x86: {
John McCall1fe2a8c2013-06-18 02:46:29 +00008574 bool IsDarwinVectorABI = Triple.isOSDarwin();
Michael Kupersteindc745202015-10-19 07:52:25 +00008575 bool RetSmallStructInRegABI =
John McCall1fe2a8c2013-06-18 02:46:29 +00008576 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
Saleem Abdulrasoolec5c6242014-11-23 02:16:24 +00008577 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00008578
John McCall1fe2a8c2013-06-18 02:46:29 +00008579 if (Triple.getOS() == llvm::Triple::Win32) {
Reid Kleckner9305fd12016-04-13 23:37:17 +00008580 return SetCGInfo(new WinX86_32TargetCodeGenInfo(
8581 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8582 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
John McCall1fe2a8c2013-06-18 02:46:29 +00008583 } else {
Reid Kleckner9305fd12016-04-13 23:37:17 +00008584 return SetCGInfo(new X86_32TargetCodeGenInfo(
8585 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8586 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
8587 CodeGenOpts.FloatABI == "soft"));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00008588 }
Eli Friedman33465822011-07-08 23:31:17 +00008589 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00008590
Eli Friedmanbfd5add2011-12-02 00:11:43 +00008591 case llvm::Triple::x86_64: {
Ahmed Bougachad39a4152015-06-22 21:30:39 +00008592 StringRef ABI = getTarget().getABI();
Reid Kleckner9305fd12016-04-13 23:37:17 +00008593 X86AVXABILevel AVXLevel =
8594 (ABI == "avx512"
8595 ? X86AVXABILevel::AVX512
8596 : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
Ahmed Bougachad39a4152015-06-22 21:30:39 +00008597
Chris Lattner04dc9572010-08-31 16:44:54 +00008598 switch (Triple.getOS()) {
8599 case llvm::Triple::Win32:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008600 return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
Alex Rosenberg12207fa2015-01-27 14:47:44 +00008601 case llvm::Triple::PS4:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008602 return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel));
Chris Lattner04dc9572010-08-31 16:44:54 +00008603 default:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008604 return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
Chris Lattner04dc9572010-08-31 16:44:54 +00008605 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00008606 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00008607 case llvm::Triple::hexagon:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008608 return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
Jacques Pienaard964cc22016-03-28 21:02:54 +00008609 case llvm::Triple::lanai:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008610 return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00008611 case llvm::Triple::r600:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008612 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
Tom Stellardd8e38a32015-01-06 20:34:47 +00008613 case llvm::Triple::amdgcn:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008614 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
Chris Dewhurst7e7ee962016-06-08 14:47:25 +00008615 case llvm::Triple::sparc:
8616 return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008617 case llvm::Triple::sparcv9:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008618 return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
Robert Lytton0e076492013-08-13 09:43:10 +00008619 case llvm::Triple::xcore:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008620 return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
Xiuli Pan972bea82016-03-24 03:57:17 +00008621 case llvm::Triple::spir:
8622 case llvm::Triple::spir64:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008623 return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
Eli Friedmanbfd5add2011-12-02 00:11:43 +00008624 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00008625}