blob: 412bd98390e5f7c86c2e5c2a1936211723b5329d [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000017#include "CGCXXABI.h"
Stephen Hines176edba2014-12-01 14:53:08 -080018#include "CGValue.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Mark Lacey8b549992013-10-30 21:53:58 +000021#include "clang/CodeGen/CGFunctionInfo.h"
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070022#include "clang/CodeGen/SwiftCallingConv.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000023#include "clang/Frontend/CodeGenOptions.h"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070024#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000025#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/Type.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000028#include "llvm/Support/raw_ostream.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070029#include <algorithm> // std::sort
30
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000031using namespace clang;
32using namespace CodeGen;
33
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070034// Helper for coercing an aggregate argument or return value into an integer
35// array of the same size (including padding) and alignment. This alternate
36// coercion happens only for the RenderScript ABI and can be removed after
37// runtimes that rely on it are no longer supported.
Matt Wala1d151512015-08-10 15:58:40 -070038//
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070039// RenderScript assumes that the size of the argument / return value in the IR
40// is the same as the size of the corresponding qualified type. This helper
41// coerces the aggregate type into an array of the same size (including
42// padding). This coercion is used in lieu of expansion of struct members or
43// other canonical coercions that return a coerced-type of larger size.
Matt Wala1d151512015-08-10 15:58:40 -070044//
45// Ty - The argument / return value type
46// Context - The associated ASTContext
47// LLVMContext - The associated LLVMContext
48static ABIArgInfo coerceToIntArray(QualType Ty,
49 ASTContext &Context,
50 llvm::LLVMContext &LLVMContext) {
51 // Alignment and Size are measured in bits.
52 const uint64_t Size = Context.getTypeSize(Ty);
53 const uint64_t Alignment = Context.getTypeAlign(Ty);
54 llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
55 const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
56 return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
57}
58
John McCallaeeb7012010-05-27 06:19:26 +000059static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
60 llvm::Value *Array,
61 llvm::Value *Value,
62 unsigned FirstIndex,
63 unsigned LastIndex) {
64 // Alternatively, we could emit this as a loop in the source.
65 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -070066 llvm::Value *Cell =
67 Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080068 Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
John McCallaeeb7012010-05-27 06:19:26 +000069 }
70}
71
John McCalld608cdb2010-08-22 10:59:02 +000072static bool isAggregateTypeForABI(QualType T) {
John McCall9d232c82013-03-07 21:37:08 +000073 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalld608cdb2010-08-22 10:59:02 +000074 T->isMemberFunctionPointerType();
75}
76
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080077ABIArgInfo
78ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
79 llvm::Type *Padding) const {
80 return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
81 ByRef, Realign, Padding);
82}
83
84ABIArgInfo
85ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
86 return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
87 /*ByRef*/ false, Realign);
88}
89
90Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
91 QualType Ty) const {
92 return Address::invalid();
93}
94
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000095ABIInfo::~ABIInfo() {}
96
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070097/// Does the given lowering require more than the given number of
98/// registers when expanded?
99///
100/// This is intended to be the basis of a reasonable basic implementation
101/// of should{Pass,Return}IndirectlyForSwift.
102///
103/// For most targets, a limit of four total registers is reasonable; this
104/// limits the amount of code required in order to move around the value
105/// in case it wasn't produced immediately prior to the call by the caller
106/// (or wasn't produced in exactly the right registers) or isn't used
107/// immediately within the callee. But some targets may need to further
108/// limit the register count due to an inability to support that many
109/// return registers.
110static bool occupiesMoreThan(CodeGenTypes &cgt,
111 ArrayRef<llvm::Type*> scalarTypes,
112 unsigned maxAllRegisters) {
113 unsigned intCount = 0, fpCount = 0;
114 for (llvm::Type *type : scalarTypes) {
115 if (type->isPointerTy()) {
116 intCount++;
117 } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
118 auto ptrWidth = cgt.getTarget().getPointerWidth(0);
119 intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
120 } else {
121 assert(type->isVectorTy() || type->isFloatingPointTy());
122 fpCount++;
123 }
124 }
125
126 return (intCount + fpCount > maxAllRegisters);
127}
128
129bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
130 llvm::Type *eltTy,
131 unsigned numElts) const {
132 // The default implementation of this assumes that the target guarantees
133 // 128-bit SIMD support but nothing more.
134 return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
135}
136
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000137static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
Mark Lacey23630722013-10-06 01:33:34 +0000138 CGCXXABI &CXXABI) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000139 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
140 if (!RD)
141 return CGCXXABI::RAA_Default;
Mark Lacey23630722013-10-06 01:33:34 +0000142 return CXXABI.getRecordArgABI(RD);
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000143}
144
145static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
Mark Lacey23630722013-10-06 01:33:34 +0000146 CGCXXABI &CXXABI) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000147 const RecordType *RT = T->getAs<RecordType>();
148 if (!RT)
149 return CGCXXABI::RAA_Default;
Mark Lacey23630722013-10-06 01:33:34 +0000150 return getRecordArgABI(RT, CXXABI);
151}
152
Stephen Hines176edba2014-12-01 14:53:08 -0800153/// Pass transparent unions as if they were the type of the first element. Sema
154/// should ensure that all elements of the union have the same "machine type".
155static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
156 if (const RecordType *UT = Ty->getAsUnionType()) {
157 const RecordDecl *UD = UT->getDecl();
158 if (UD->hasAttr<TransparentUnionAttr>()) {
159 assert(!UD->field_empty() && "sema created an empty transparent union");
160 return UD->field_begin()->getType();
161 }
162 }
163 return Ty;
164}
165
Mark Lacey23630722013-10-06 01:33:34 +0000166CGCXXABI &ABIInfo::getCXXABI() const {
167 return CGT.getCXXABI();
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000168}
169
Chris Lattnerea044322010-07-29 02:01:43 +0000170ASTContext &ABIInfo::getContext() const {
171 return CGT.getContext();
172}
173
174llvm::LLVMContext &ABIInfo::getVMContext() const {
175 return CGT.getLLVMContext();
176}
177
Micah Villmow25a6a842012-10-08 16:25:52 +0000178const llvm::DataLayout &ABIInfo::getDataLayout() const {
179 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +0000180}
181
John McCall64aa4b32013-04-16 22:48:15 +0000182const TargetInfo &ABIInfo::getTarget() const {
183 return CGT.getTarget();
184}
Chris Lattnerea044322010-07-29 02:01:43 +0000185
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700186bool ABIInfo:: isAndroid() const { return getTarget().getTriple().isAndroid(); }
187
Stephen Hines176edba2014-12-01 14:53:08 -0800188bool 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
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700197bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
198 return false;
199}
200
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700201LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000202 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000203 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000204 switch (TheKind) {
205 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000206 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +0000207 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +0000208 Ty->print(OS);
209 else
210 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000211 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000212 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000213 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000214 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000215 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000216 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000217 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700218 case InAlloca:
219 OS << "InAlloca Offset=" << getInAllocaFieldIndex();
220 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000221 case Indirect:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800222 OS << "Indirect Align=" << getIndirectAlign().getQuantity()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +0000223 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000224 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000225 break;
226 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000227 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000228 break;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700229 case CoerceAndExpand:
230 OS << "CoerceAndExpand Type=";
231 getCoerceAndExpandType()->print(OS);
232 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000233 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000234 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000235}
236
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800237// 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
254/// 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) {
285 Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
286 DirectAlign);
287 } else {
288 Addr = Address(Ptr, SlotSize);
289 }
290
291 // Advance the pointer past the argument, then store that back.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700292 CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800293 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.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700300 if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
301 !DirectTy->isStructTy()) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800302 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 Korobeynikov82d0a412010-01-10 12:58:08 +0000367TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
368
John McCall49e34be2011-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 Northoverc264e162013-01-31 12:13:10 +0000377 // AArch64 Linux
John McCall49e34be2011-08-30 01:42:09 +0000378 return 32;
379}
380
John McCallde5d3c72012-02-17 03:33:10 +0000381bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
382 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-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 Kleckner3190ca92013-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
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700400unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
401 return llvm::CallingConv::C;
402}
Daniel Dunbar98303b92009-09-13 08:03:58 +0000403static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000404
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000405/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000406/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000407static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
408 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000409 if (FD->isUnnamedBitfield())
410 return true;
411
412 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000413
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000414 // Constant arrays of empty records count as empty, strip them off.
415 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000416 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000417 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
418 if (AT->getSize() == 0)
419 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000420 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000421 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000422
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000423 const RecordType *RT = FT->getAs<RecordType>();
424 if (!RT)
425 return false;
426
427 // C++ record fields are never empty, at least in the Itanium ABI.
428 //
429 // FIXME: We should use a predicate for whether this behavior is true in the
430 // current ABI.
431 if (isa<CXXRecordDecl>(RT->getDecl()))
432 return false;
433
Daniel Dunbar98303b92009-09-13 08:03:58 +0000434 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000435}
436
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000437/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000438/// fields. Note that a structure with a flexible array member is not
439/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000440static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000441 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000442 if (!RT)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700443 return false;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000444 const RecordDecl *RD = RT->getDecl();
445 if (RD->hasFlexibleArrayMember())
446 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000447
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000448 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000449 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Stephen Hines651f13c2014-04-23 16:59:28 -0700450 for (const auto &I : CXXRD->bases())
451 if (!isEmptyRecord(Context, I.getType(), true))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000452 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000453
Stephen Hines651f13c2014-04-23 16:59:28 -0700454 for (const auto *I : RD->fields())
455 if (!isEmptyField(Context, I, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000456 return false;
457 return true;
458}
459
460/// isSingleElementStruct - Determine if a structure is a "single
461/// element struct", i.e. it has exactly one non-empty field or
462/// exactly one field which is itself a single element
463/// struct. Structures with flexible array members are never
464/// considered single element structs.
465///
466/// \return The field declaration for the single non-empty field, if
467/// it exists.
468static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700469 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000470 if (!RT)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700471 return nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000472
473 const RecordDecl *RD = RT->getDecl();
474 if (RD->hasFlexibleArrayMember())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700475 return nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000476
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700477 const Type *Found = nullptr;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000478
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000479 // If this is a C++ record, check the bases first.
480 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700481 for (const auto &I : CXXRD->bases()) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000482 // Ignore empty records.
Stephen Hines651f13c2014-04-23 16:59:28 -0700483 if (isEmptyRecord(Context, I.getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000484 continue;
485
486 // If we already found an element then this isn't a single-element struct.
487 if (Found)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700488 return nullptr;
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000489
490 // If this is non-empty and not a single element struct, the composite
491 // cannot be a single element struct.
Stephen Hines651f13c2014-04-23 16:59:28 -0700492 Found = isSingleElementStruct(I.getType(), Context);
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000493 if (!Found)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700494 return nullptr;
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000495 }
496 }
497
498 // Check for single element.
Stephen Hines651f13c2014-04-23 16:59:28 -0700499 for (const auto *FD : RD->fields()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000500 QualType FT = FD->getType();
501
502 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000503 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000504 continue;
505
506 // If we already found an element then this isn't a single-element
507 // struct.
508 if (Found)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700509 return nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000510
511 // Treat single element arrays as the element.
512 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
513 if (AT->getSize().getZExtValue() != 1)
514 break;
515 FT = AT->getElementType();
516 }
517
John McCalld608cdb2010-08-22 10:59:02 +0000518 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519 Found = FT.getTypePtr();
520 } else {
521 Found = isSingleElementStruct(FT, Context);
522 if (!Found)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700523 return nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000524 }
525 }
526
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000527 // We don't consider a struct a single-element struct if it has
528 // padding beyond the element type.
529 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700530 return nullptr;
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000531
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000532 return Found;
533}
534
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000535namespace {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700536Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
537 const ABIArgInfo &AI) {
538 // This default implementation defers to the llvm backend's va_arg
539 // instruction. It can handle only passing arguments directly
540 // (typically only handled in the backend for primitive types), or
541 // aggregates passed indirectly by pointer (NOTE: if the "byval"
542 // flag has ABI impact in the callee, this implementation cannot
543 // work.)
544
545 // Only a few cases are covered here at the moment -- those needed
546 // by the default abi.
547 llvm::Value *Val;
548
549 if (AI.isIndirect()) {
550 assert(!AI.getPaddingType() &&
551 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
552 assert(
553 !AI.getIndirectRealign() &&
554 "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
555
556 auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
557 CharUnits TyAlignForABI = TyInfo.second;
558
559 llvm::Type *BaseTy =
560 llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
561 llvm::Value *Addr =
562 CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
563 return Address(Addr, TyAlignForABI);
564 } else {
565 assert((AI.isDirect() || AI.isExtend()) &&
566 "Unexpected ArgInfo Kind in generic VAArg emitter!");
567
568 assert(!AI.getInReg() &&
569 "Unexpected InReg seen in arginfo in generic VAArg emitter!");
570 assert(!AI.getPaddingType() &&
571 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
572 assert(!AI.getDirectOffset() &&
573 "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
574 assert(!AI.getCoerceToType() &&
575 "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
576
577 Address Temp = CGF.CreateMemTemp(Ty, "varet");
578 Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
579 CGF.Builder.CreateStore(Val, Temp);
580 return Temp;
581 }
582}
583
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000584/// DefaultABIInfo - The default implementation for ABI specific
585/// details. This implementation provides information which results in
586/// self-consistent and sensible LLVM IR generation, but does not
587/// conform to any particular ABI.
588class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000589public:
590 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000591
Chris Lattnera3c109b2010-07-29 02:16:43 +0000592 ABIArgInfo classifyReturnType(QualType RetTy) const;
593 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000594
Stephen Hines651f13c2014-04-23 16:59:28 -0700595 void computeInfo(CGFunctionInfo &FI) const override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700596 if (!getCXXABI().classifyReturnType(FI))
597 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -0700598 for (auto &I : FI.arguments())
599 I.info = classifyArgumentType(I.type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000600 }
601
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800602 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700603 QualType Ty) const override {
604 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
605 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000606};
607
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000608class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
609public:
Chris Lattnerea044322010-07-29 02:01:43 +0000610 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
611 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000612};
613
Chris Lattnera3c109b2010-07-29 02:16:43 +0000614ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700615 Ty = useFirstFieldIfTransparentUnion(Ty);
616
617 if (isAggregateTypeForABI(Ty)) {
618 // Records with non-trivial destructors/copy-constructors should not be
619 // passed by value.
620 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800621 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700622
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800623 return getNaturalAlignIndirect(Ty);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700624 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000625
Chris Lattnera14db752010-03-11 18:19:55 +0000626 // Treat an enum type as its underlying type.
627 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
628 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000629
Chris Lattnera14db752010-03-11 18:19:55 +0000630 return (Ty->isPromotableIntegerType() ?
631 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000632}
633
Bob Wilson0024f942011-01-10 23:54:17 +0000634ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
635 if (RetTy->isVoidType())
636 return ABIArgInfo::getIgnore();
637
638 if (isAggregateTypeForABI(RetTy))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800639 return getNaturalAlignIndirect(RetTy);
Bob Wilson0024f942011-01-10 23:54:17 +0000640
641 // Treat an enum type as its underlying type.
642 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
643 RetTy = EnumTy->getDecl()->getIntegerType();
644
645 return (RetTy->isPromotableIntegerType() ?
646 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
647}
648
Derek Schuff9ed63f82012-09-06 17:37:28 +0000649//===----------------------------------------------------------------------===//
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800650// WebAssembly ABI Implementation
651//
652// This is a very simple ABI that relies a lot on DefaultABIInfo.
653//===----------------------------------------------------------------------===//
654
655class WebAssemblyABIInfo final : public DefaultABIInfo {
656public:
657 explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
658 : DefaultABIInfo(CGT) {}
659
660private:
661 ABIArgInfo classifyReturnType(QualType RetTy) const;
662 ABIArgInfo classifyArgumentType(QualType Ty) const;
663
664 // DefaultABIInfo's classifyReturnType and classifyArgumentType are
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700665 // non-virtual, but computeInfo and EmitVAArg are virtual, so we
666 // overload them.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800667 void computeInfo(CGFunctionInfo &FI) const override {
668 if (!getCXXABI().classifyReturnType(FI))
669 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
670 for (auto &Arg : FI.arguments())
671 Arg.info = classifyArgumentType(Arg.type);
672 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700673
674 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
675 QualType Ty) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800676};
677
678class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
679public:
680 explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
681 : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
682};
683
684/// \brief Classify argument of given type \p Ty.
685ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
686 Ty = useFirstFieldIfTransparentUnion(Ty);
687
688 if (isAggregateTypeForABI(Ty)) {
689 // Records with non-trivial destructors/copy-constructors should not be
690 // passed by value.
691 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
692 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
693 // Ignore empty structs/unions.
694 if (isEmptyRecord(getContext(), Ty, true))
695 return ABIArgInfo::getIgnore();
696 // Lower single-element structs to just pass a regular value. TODO: We
697 // could do reasonable-size multiple-element structs too, using getExpand(),
698 // though watch out for things like bitfields.
699 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
700 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
701 }
702
703 // Otherwise just do the default thing.
704 return DefaultABIInfo::classifyArgumentType(Ty);
705}
706
707ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
708 if (isAggregateTypeForABI(RetTy)) {
709 // Records with non-trivial destructors/copy-constructors should not be
710 // returned by value.
711 if (!getRecordArgABI(RetTy, getCXXABI())) {
712 // Ignore empty structs/unions.
713 if (isEmptyRecord(getContext(), RetTy, true))
714 return ABIArgInfo::getIgnore();
715 // Lower single-element structs to just return a regular value. TODO: We
716 // could do reasonable-size multiple-element structs too, using
717 // ABIArgInfo::getDirect().
718 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
719 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
720 }
721 }
722
723 // Otherwise just do the default thing.
724 return DefaultABIInfo::classifyReturnType(RetTy);
725}
726
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700727Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
728 QualType Ty) const {
729 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false,
730 getContext().getTypeInfoInChars(Ty),
731 CharUnits::fromQuantity(4),
732 /*AllowHigherAlign=*/ true);
733}
734
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800735//===----------------------------------------------------------------------===//
Derek Schuff9ed63f82012-09-06 17:37:28 +0000736// le32/PNaCl bitcode ABI Implementation
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000737//
738// This is a simplified version of the x86_32 ABI. Arguments and return values
739// are always passed on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000740//===----------------------------------------------------------------------===//
741
742class PNaClABIInfo : public ABIInfo {
743 public:
744 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
745
746 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000747 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000748
Stephen Hines651f13c2014-04-23 16:59:28 -0700749 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800750 Address EmitVAArg(CodeGenFunction &CGF,
751 Address VAListAddr, QualType Ty) const override;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000752};
753
754class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
755 public:
756 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
757 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
758};
759
760void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700761 if (!getCXXABI().classifyReturnType(FI))
Derek Schuff9ed63f82012-09-06 17:37:28 +0000762 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
763
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700764 for (auto &I : FI.arguments())
765 I.info = classifyArgumentType(I.type);
766}
Derek Schuff9ed63f82012-09-06 17:37:28 +0000767
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800768Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
769 QualType Ty) const {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700770 // The PNaCL ABI is a bit odd, in that varargs don't use normal
771 // function classification. Structs get passed directly for varargs
772 // functions, through a rewriting transform in
773 // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
774 // this target to actually support a va_arg instructions with an
775 // aggregate type, unlike other targets.
776 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000777}
778
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000779/// \brief Classify argument of given type \p Ty.
780ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff9ed63f82012-09-06 17:37:28 +0000781 if (isAggregateTypeForABI(Ty)) {
Mark Lacey23630722013-10-06 01:33:34 +0000782 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800783 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
784 return getNaturalAlignIndirect(Ty);
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000785 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
786 // Treat an enum type as its underlying type.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000787 Ty = EnumTy->getDecl()->getIntegerType();
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000788 } else if (Ty->isFloatingType()) {
789 // Floating-point types don't go inreg.
790 return ABIArgInfo::getDirect();
Derek Schuff9ed63f82012-09-06 17:37:28 +0000791 }
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000792
793 return (Ty->isPromotableIntegerType() ?
794 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000795}
796
797ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
798 if (RetTy->isVoidType())
799 return ABIArgInfo::getIgnore();
800
Eli Benderskye45dfd12013-04-04 22:49:35 +0000801 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000802 if (isAggregateTypeForABI(RetTy))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800803 return getNaturalAlignIndirect(RetTy);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000804
805 // Treat an enum type as its underlying type.
806 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
807 RetTy = EnumTy->getDecl()->getIntegerType();
808
809 return (RetTy->isPromotableIntegerType() ?
810 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
811}
812
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000813/// IsX86_MMXType - Return true if this is an MMX type.
814bool IsX86_MMXType(llvm::Type *IRType) {
815 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
Bill Wendlingbb465d72010-10-18 03:41:31 +0000816 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
817 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
818 IRType->getScalarSizeInBits() != 64;
819}
820
Jay Foadef6de3d2011-07-11 09:56:20 +0000821static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000822 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000823 llvm::Type* Ty) {
Tim Northover1bea6532013-06-07 00:04:50 +0000824 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
825 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
826 // Invalid MMX constraint
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700827 return nullptr;
Tim Northover1bea6532013-06-07 00:04:50 +0000828 }
829
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000830 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover1bea6532013-06-07 00:04:50 +0000831 }
832
833 // No operation needed
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000834 return Ty;
835}
836
Stephen Hines176edba2014-12-01 14:53:08 -0800837/// Returns true if this type can be passed in SSE registers with the
838/// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
839static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
840 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
841 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half)
842 return true;
843 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
844 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
845 // registers specially.
846 unsigned VecSize = Context.getTypeSize(VT);
847 if (VecSize == 128 || VecSize == 256 || VecSize == 512)
848 return true;
849 }
850 return false;
851}
852
853/// Returns true if this aggregate is small enough to be passed in SSE registers
854/// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
855static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
856 return NumMembers <= 4;
857}
858
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000859//===----------------------------------------------------------------------===//
860// X86-32 ABI Implementation
861//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000862
Stephen Hines651f13c2014-04-23 16:59:28 -0700863/// \brief Similar to llvm::CCState, but for Clang.
864struct CCState {
Stephen Hines176edba2014-12-01 14:53:08 -0800865 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
Stephen Hines651f13c2014-04-23 16:59:28 -0700866
867 unsigned CC;
868 unsigned FreeRegs;
Stephen Hines176edba2014-12-01 14:53:08 -0800869 unsigned FreeSSERegs;
Stephen Hines651f13c2014-04-23 16:59:28 -0700870};
871
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000872/// X86_32ABIInfo - The X86-32 ABI information.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700873class X86_32ABIInfo : public SwiftABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000874 enum Class {
875 Integer,
876 Float
877 };
878
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000879 static const unsigned MinABIStackAlignInBytes = 4;
880
David Chisnall1e4249c2009-08-17 23:08:21 +0000881 bool IsDarwinVectorABI;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800882 bool IsRetSmallStructInRegABI;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000883 bool IsWin32StructABI;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800884 bool IsSoftFloatABI;
885 bool IsMCUABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000886 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000887
888 static bool isRegisterSize(unsigned Size) {
889 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
890 }
891
Stephen Hines176edba2014-12-01 14:53:08 -0800892 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
893 // FIXME: Assumes vectorcall is in use.
894 return isX86VectorTypeForVectorCall(getContext(), Ty);
895 }
896
897 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
898 uint64_t NumMembers) const override {
899 // FIXME: Assumes vectorcall is in use.
900 return isX86VectorCallAggregateSmallEnough(NumMembers);
901 }
902
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700903 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000904
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000905 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
906 /// such that the argument will be passed in memory.
Stephen Hines651f13c2014-04-23 16:59:28 -0700907 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
908
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800909 ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000910
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000911 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000912 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000913
Rafael Espindolab48280b2012-07-31 02:44:24 +0000914 Class classify(QualType Ty) const;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700915 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
Stephen Hines651f13c2014-04-23 16:59:28 -0700916 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700917 /// \brief Updates the number of available free registers, returns
918 /// true if any registers were allocated.
919 bool updateFreeRegs(QualType Ty, CCState &State) const;
920
921 bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
922 bool &NeedsPadding) const;
923 bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
924
925 bool canExpandIndirectArgument(QualType Ty) const;
Stephen Hines651f13c2014-04-23 16:59:28 -0700926
927 /// \brief Rewrite the function info so that all memory arguments use
928 /// inalloca.
929 void rewriteWithInAlloca(CGFunctionInfo &FI) const;
930
931 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800932 CharUnits &StackOffset, ABIArgInfo &Info,
Stephen Hines651f13c2014-04-23 16:59:28 -0700933 QualType Type) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000934
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000935public:
936
Stephen Hines651f13c2014-04-23 16:59:28 -0700937 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800938 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
939 QualType Ty) const override;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000940
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800941 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
942 bool RetSmallStructInRegABI, bool Win32StructABI,
943 unsigned NumRegisterParameters, bool SoftFloatABI)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700944 : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800945 IsRetSmallStructInRegABI(RetSmallStructInRegABI),
946 IsWin32StructABI(Win32StructABI),
947 IsSoftFloatABI(SoftFloatABI),
948 IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
949 DefaultNumRegisterParameters(NumRegisterParameters) {}
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700950
951 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
952 ArrayRef<llvm::Type*> scalars,
953 bool asReturnValue) const override {
954 // LLVM's x86-32 lowering currently only assigns up to three
955 // integer registers and three fp registers. Oddly, it'll use up to
956 // four vector registers for vectors, but those can overlap with the
957 // scalar registers.
958 return occupiesMoreThan(CGT, scalars, /*total*/ 3);
959 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000960};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000961
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000962class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
963public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800964 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
965 bool RetSmallStructInRegABI, bool Win32StructABI,
966 unsigned NumRegisterParameters, bool SoftFloatABI)
967 : TargetCodeGenInfo(new X86_32ABIInfo(
968 CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
969 NumRegisterParameters, SoftFloatABI)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000970
John McCallb8b52972013-06-18 02:46:29 +0000971 static bool isStructReturnInRegABI(
972 const llvm::Triple &Triple, const CodeGenOptions &Opts);
973
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700974 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -0700975 CodeGen::CodeGenModule &CGM) const override;
John McCall6374c332010-03-06 00:35:14 +0000976
Stephen Hines651f13c2014-04-23 16:59:28 -0700977 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCall6374c332010-03-06 00:35:14 +0000978 // Darwin uses different dwarf register numbers for EH.
John McCall64aa4b32013-04-16 22:48:15 +0000979 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCall6374c332010-03-06 00:35:14 +0000980 return 4;
981 }
982
983 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -0700984 llvm::Value *Address) const override;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000985
Jay Foadef6de3d2011-07-11 09:56:20 +0000986 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000987 StringRef Constraint,
Stephen Hines651f13c2014-04-23 16:59:28 -0700988 llvm::Type* Ty) const override {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000989 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
990 }
991
Stephen Hines176edba2014-12-01 14:53:08 -0800992 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
993 std::string &Constraints,
994 std::vector<llvm::Type *> &ResultRegTypes,
995 std::vector<llvm::Type *> &ResultTruncRegTypes,
996 std::vector<LValue> &ResultRegDests,
997 std::string &AsmString,
998 unsigned NumOutputs) const override;
999
Stephen Hines651f13c2014-04-23 16:59:28 -07001000 llvm::Constant *
1001 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourneb914e872013-10-20 21:29:19 +00001002 unsigned Sig = (0xeb << 0) | // jmp rel8
1003 (0x06 << 8) | // .+0x08
1004 ('F' << 16) |
1005 ('T' << 24);
1006 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1007 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001008
1009 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1010 return "movl\t%ebp, %ebp"
1011 "\t\t## marker for objc_retainAutoreleaseReturnValue";
1012 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001013};
1014
1015}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001016
Stephen Hines176edba2014-12-01 14:53:08 -08001017/// Rewrite input constraint references after adding some output constraints.
1018/// In the case where there is one output and one input and we add one output,
1019/// we need to replace all operand references greater than or equal to 1:
1020/// mov $0, $1
1021/// mov eax, $1
1022/// The result will be:
1023/// mov $0, $2
1024/// mov eax, $2
1025static void rewriteInputConstraintReferences(unsigned FirstIn,
1026 unsigned NumNewOuts,
1027 std::string &AsmString) {
1028 std::string Buf;
1029 llvm::raw_string_ostream OS(Buf);
1030 size_t Pos = 0;
1031 while (Pos < AsmString.size()) {
1032 size_t DollarStart = AsmString.find('$', Pos);
1033 if (DollarStart == std::string::npos)
1034 DollarStart = AsmString.size();
1035 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1036 if (DollarEnd == std::string::npos)
1037 DollarEnd = AsmString.size();
1038 OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1039 Pos = DollarEnd;
1040 size_t NumDollars = DollarEnd - DollarStart;
1041 if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1042 // We have an operand reference.
1043 size_t DigitStart = Pos;
1044 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1045 if (DigitEnd == std::string::npos)
1046 DigitEnd = AsmString.size();
1047 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1048 unsigned OperandIndex;
1049 if (!OperandStr.getAsInteger(10, OperandIndex)) {
1050 if (OperandIndex >= FirstIn)
1051 OperandIndex += NumNewOuts;
1052 OS << OperandIndex;
1053 } else {
1054 OS << OperandStr;
1055 }
1056 Pos = DigitEnd;
1057 }
1058 }
1059 AsmString = std::move(OS.str());
1060}
1061
1062/// Add output constraints for EAX:EDX because they are return registers.
1063void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1064 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1065 std::vector<llvm::Type *> &ResultRegTypes,
1066 std::vector<llvm::Type *> &ResultTruncRegTypes,
1067 std::vector<LValue> &ResultRegDests, std::string &AsmString,
1068 unsigned NumOutputs) const {
1069 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1070
1071 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1072 // larger.
1073 if (!Constraints.empty())
1074 Constraints += ',';
1075 if (RetWidth <= 32) {
1076 Constraints += "={eax}";
1077 ResultRegTypes.push_back(CGF.Int32Ty);
1078 } else {
1079 // Use the 'A' constraint for EAX:EDX.
1080 Constraints += "=A";
1081 ResultRegTypes.push_back(CGF.Int64Ty);
1082 }
1083
1084 // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1085 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1086 ResultTruncRegTypes.push_back(CoerceTy);
1087
1088 // Coerce the integer by bitcasting the return slot pointer.
1089 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
1090 CoerceTy->getPointerTo()));
1091 ResultRegDests.push_back(ReturnSlot);
1092
1093 rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1094}
1095
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001096/// shouldReturnTypeInRegister - Determine if the given type should be
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001097/// returned in a register (for the Darwin and MCU ABI).
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001098bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1099 ASTContext &Context) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001100 uint64_t Size = Context.getTypeSize(Ty);
1101
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001102 // For i386, type must be register sized.
1103 // For the MCU ABI, it only needs to be <= 8-byte
1104 if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1105 return false;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001106
1107 if (Ty->isVectorType()) {
1108 // 64- and 128- bit vectors inside structures are not returned in
1109 // registers.
1110 if (Size == 64 || Size == 128)
1111 return false;
1112
1113 return true;
1114 }
1115
Daniel Dunbar77115232010-05-15 00:00:30 +00001116 // If this is a builtin, pointer, enum, complex type, member pointer, or
1117 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +00001118 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +00001119 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +00001120 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001121 return true;
1122
1123 // Arrays are treated like records.
1124 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001125 return shouldReturnTypeInRegister(AT->getElementType(), Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001126
1127 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +00001128 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001129 if (!RT) return false;
1130
Anders Carlssona8874232010-01-27 03:25:19 +00001131 // FIXME: Traverse bases here too.
1132
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001133 // Structure types are passed in register if all fields would be
1134 // passed in a register.
Stephen Hines651f13c2014-04-23 16:59:28 -07001135 for (const auto *FD : RT->getDecl()->fields()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001136 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001137 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001138 continue;
1139
1140 // Check fields recursively.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001141 if (!shouldReturnTypeInRegister(FD->getType(), Context))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001142 return false;
1143 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001144 return true;
1145}
1146
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001147static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1148 // Treat complex types as the element type.
1149 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1150 Ty = CTy->getElementType();
1151
1152 // Check for a type which we know has a simple scalar argument-passing
1153 // convention without any padding. (We're specifically looking for 32
1154 // and 64-bit integer and integer-equivalents, float, and double.)
1155 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1156 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1157 return false;
1158
1159 uint64_t Size = Context.getTypeSize(Ty);
1160 return Size == 32 || Size == 64;
1161}
1162
1163/// Test whether an argument type which is to be passed indirectly (on the
1164/// stack) would have the equivalent layout if it was expanded into separate
1165/// arguments. If so, we prefer to do the latter to avoid inhibiting
1166/// optimizations.
1167bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1168 // We can only expand structure types.
1169 const RecordType *RT = Ty->getAs<RecordType>();
1170 if (!RT)
1171 return false;
1172 const RecordDecl *RD = RT->getDecl();
1173 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1174 if (!IsWin32StructABI ) {
1175 // On non-Windows, we have to conservatively match our old bitcode
1176 // prototypes in order to be ABI-compatible at the bitcode level.
1177 if (!CXXRD->isCLike())
1178 return false;
1179 } else {
1180 // Don't do this for dynamic classes.
1181 if (CXXRD->isDynamicClass())
1182 return false;
1183 // Don't do this if there are any non-empty bases.
1184 for (const CXXBaseSpecifier &Base : CXXRD->bases()) {
1185 if (!isEmptyRecord(getContext(), Base.getType(), /*AllowArrays=*/true))
1186 return false;
1187 }
1188 }
1189 }
1190
1191 uint64_t Size = 0;
1192
1193 for (const auto *FD : RD->fields()) {
1194 // Scalar arguments on the stack get 4 byte alignment on x86. If the
1195 // argument is smaller than 32-bits, expanding the struct will create
1196 // alignment padding.
1197 if (!is32Or64BitBasicType(FD->getType(), getContext()))
1198 return false;
1199
1200 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1201 // how to expand them yet, and the predicate for telling if a bitfield still
1202 // counts as "basic" is more complicated than what we were doing previously.
1203 if (FD->isBitField())
1204 return false;
1205
1206 Size += getContext().getTypeSize(FD->getType());
1207 }
1208
1209 // We can do this if there was no alignment padding.
1210 return Size == getContext().getTypeSize(Ty);
1211}
1212
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001213ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001214 // If the return value is indirect, then the hidden argument is consuming one
1215 // integer register.
1216 if (State.FreeRegs) {
1217 --State.FreeRegs;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001218 if (!IsMCUABI)
1219 return getNaturalAlignIndirectInReg(RetTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07001220 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001221 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001222}
1223
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001224ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1225 CCState &State) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00001226 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001227 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001228
Stephen Hines176edba2014-12-01 14:53:08 -08001229 const Type *Base = nullptr;
1230 uint64_t NumElts = 0;
1231 if (State.CC == llvm::CallingConv::X86_VectorCall &&
1232 isHomogeneousAggregate(RetTy, Base, NumElts)) {
1233 // The LLVM struct type for such an aggregate should lower properly.
1234 return ABIArgInfo::getDirect();
1235 }
1236
Chris Lattnera3c109b2010-07-29 02:16:43 +00001237 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001238 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +00001239 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00001240 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001241
1242 // 128-bit vectors are a special case; they are returned in
1243 // registers and we need to make sure to pick a type the LLVM
1244 // backend will like.
1245 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +00001246 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +00001247 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001248
1249 // Always return in register if it fits in a general purpose
1250 // register, or if it is 64 bits and has a single element.
1251 if ((Size == 8 || Size == 16 || Size == 32) ||
1252 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +00001253 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00001254 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001255
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001256 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001257 }
1258
1259 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +00001260 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001261
John McCalld608cdb2010-08-22 10:59:02 +00001262 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +00001263 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +00001264 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001265 if (RT->getDecl()->hasFlexibleArrayMember())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001266 return getIndirectReturnResult(RetTy, State);
Anders Carlsson40092972009-10-20 22:07:59 +00001267 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001268
David Chisnall1e4249c2009-08-17 23:08:21 +00001269 // If specified, structs and unions are always indirect.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001270 if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
1271 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001272
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001273 // Ignore empty structs/unions.
1274 if (isEmptyRecord(getContext(), RetTy, true))
1275 return ABIArgInfo::getIgnore();
1276
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001277 // Small structures which are register sized are generally returned
1278 // in a register.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001279 if (shouldReturnTypeInRegister(RetTy, getContext())) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00001280 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +00001281
1282 // As a special-case, if the struct is a "single-element" struct, and
1283 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +00001284 // floating-point register. (MSVC does not apply this special case.)
1285 // We apply a similar transformation for pointer types to improve the
1286 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +00001287 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001288 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedman55fc7e22012-01-25 22:46:34 +00001289 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +00001290 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1291
1292 // FIXME: We should be able to narrow this integer in cases with dead
1293 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +00001294 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001295 }
1296
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001297 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001298 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001299
Chris Lattnera3c109b2010-07-29 02:16:43 +00001300 // Treat an enum type as its underlying type.
1301 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1302 RetTy = EnumTy->getDecl()->getIntegerType();
1303
1304 return (RetTy->isPromotableIntegerType() ?
1305 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001306}
1307
Eli Friedmanf4bd4d82012-06-05 19:40:46 +00001308static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
1309 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1310}
1311
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001312static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
1313 const RecordType *RT = Ty->getAs<RecordType>();
1314 if (!RT)
1315 return 0;
1316 const RecordDecl *RD = RT->getDecl();
1317
1318 // If this is a C++ record, check the bases first.
1319 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Stephen Hines651f13c2014-04-23 16:59:28 -07001320 for (const auto &I : CXXRD->bases())
1321 if (!isRecordWithSSEVectorType(Context, I.getType()))
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001322 return false;
1323
Stephen Hines651f13c2014-04-23 16:59:28 -07001324 for (const auto *i : RD->fields()) {
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001325 QualType FT = i->getType();
1326
Eli Friedmanf4bd4d82012-06-05 19:40:46 +00001327 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001328 return true;
1329
1330 if (isRecordWithSSEVectorType(Context, FT))
1331 return true;
1332 }
1333
1334 return false;
1335}
1336
Daniel Dunbare59d8582010-09-16 20:42:06 +00001337unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1338 unsigned Align) const {
1339 // Otherwise, if the alignment is less than or equal to the minimum ABI
1340 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +00001341 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +00001342 return 0; // Use default alignment.
1343
1344 // On non-Darwin, the stack type alignment is always 4.
1345 if (!IsDarwinVectorABI) {
1346 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +00001347 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +00001348 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +00001349
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001350 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +00001351 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
1352 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001353 return 16;
1354
1355 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +00001356}
1357
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001358ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
Stephen Hines651f13c2014-04-23 16:59:28 -07001359 CCState &State) const {
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001360 if (!ByVal) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001361 if (State.FreeRegs) {
1362 --State.FreeRegs; // Non-byval indirects just use one pointer.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001363 if (!IsMCUABI)
1364 return getNaturalAlignIndirectInReg(Ty);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001365 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001366 return getNaturalAlignIndirect(Ty, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001367 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001368
Daniel Dunbare59d8582010-09-16 20:42:06 +00001369 // Compute the byval alignment.
1370 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1371 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1372 if (StackAlign == 0)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001373 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
Daniel Dunbare59d8582010-09-16 20:42:06 +00001374
1375 // If the stack alignment is less than the type alignment, realign the
1376 // argument.
Stephen Hines651f13c2014-04-23 16:59:28 -07001377 bool Realign = TypeAlign > StackAlign;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001378 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1379 /*ByVal=*/true, Realign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +00001380}
1381
Rafael Espindolab48280b2012-07-31 02:44:24 +00001382X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1383 const Type *T = isSingleElementStruct(Ty, getContext());
1384 if (!T)
1385 T = Ty.getTypePtr();
1386
1387 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1388 BuiltinType::Kind K = BT->getKind();
1389 if (K == BuiltinType::Float || K == BuiltinType::Double)
1390 return Float;
1391 }
1392 return Integer;
1393}
1394
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001395bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001396 if (!IsSoftFloatABI) {
1397 Class C = classify(Ty);
1398 if (C == Float)
1399 return false;
1400 }
Rafael Espindolab48280b2012-07-31 02:44:24 +00001401
Rafael Espindolab6932692012-10-24 01:58:58 +00001402 unsigned Size = getContext().getTypeSize(Ty);
1403 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +00001404
1405 if (SizeInRegs == 0)
1406 return false;
1407
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001408 if (!IsMCUABI) {
1409 if (SizeInRegs > State.FreeRegs) {
1410 State.FreeRegs = 0;
1411 return false;
1412 }
1413 } else {
1414 // The MCU psABI allows passing parameters in-reg even if there are
1415 // earlier parameters that are passed on the stack. Also,
1416 // it does not allow passing >8-byte structs in-register,
1417 // even if there are 3 free registers available.
1418 if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1419 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +00001420 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001421
Stephen Hines651f13c2014-04-23 16:59:28 -07001422 State.FreeRegs -= SizeInRegs;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001423 return true;
1424}
1425
1426bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1427 bool &InReg,
1428 bool &NeedsPadding) const {
1429 // On Windows, aggregates other than HFAs are never passed in registers, and
1430 // they do not consume register slots. Homogenous floating-point aggregates
1431 // (HFAs) have already been dealt with at this point.
1432 if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1433 return false;
1434
1435 NeedsPadding = false;
1436 InReg = !IsMCUABI;
1437
1438 if (!updateFreeRegs(Ty, State))
1439 return false;
1440
1441 if (IsMCUABI)
1442 return true;
Rafael Espindolab6932692012-10-24 01:58:58 +00001443
Stephen Hines176edba2014-12-01 14:53:08 -08001444 if (State.CC == llvm::CallingConv::X86_FastCall ||
1445 State.CC == llvm::CallingConv::X86_VectorCall) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001446 if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +00001447 NeedsPadding = true;
1448
Rafael Espindolab6932692012-10-24 01:58:58 +00001449 return false;
1450 }
1451
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001452 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +00001453}
1454
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001455bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1456 if (!updateFreeRegs(Ty, State))
1457 return false;
1458
1459 if (IsMCUABI)
1460 return false;
1461
1462 if (State.CC == llvm::CallingConv::X86_FastCall ||
1463 State.CC == llvm::CallingConv::X86_VectorCall) {
1464 if (getContext().getTypeSize(Ty) > 32)
1465 return false;
1466
1467 return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1468 Ty->isReferenceType());
1469 }
1470
1471 return true;
1472}
1473
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001474ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Stephen Hines651f13c2014-04-23 16:59:28 -07001475 CCState &State) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001476 // FIXME: Set alignment on indirect arguments.
Daniel Dunbardc6d5742010-04-21 19:10:51 +00001477
Stephen Hines176edba2014-12-01 14:53:08 -08001478 Ty = useFirstFieldIfTransparentUnion(Ty);
1479
1480 // Check with the C++ ABI first.
1481 const RecordType *RT = Ty->getAs<RecordType>();
1482 if (RT) {
1483 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1484 if (RAA == CGCXXABI::RAA_Indirect) {
1485 return getIndirectResult(Ty, false, State);
1486 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1487 // The field index doesn't matter, we'll fix it up later.
1488 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1489 }
1490 }
1491
1492 // vectorcall adds the concept of a homogenous vector aggregate, similar
1493 // to other targets.
1494 const Type *Base = nullptr;
1495 uint64_t NumElts = 0;
1496 if (State.CC == llvm::CallingConv::X86_VectorCall &&
1497 isHomogeneousAggregate(Ty, Base, NumElts)) {
1498 if (State.FreeSSERegs >= NumElts) {
1499 State.FreeSSERegs -= NumElts;
1500 if (Ty->isBuiltinType() || Ty->isVectorType())
1501 return ABIArgInfo::getDirect();
1502 return ABIArgInfo::getExpand();
1503 }
1504 return getIndirectResult(Ty, /*ByVal=*/false, State);
1505 }
1506
1507 if (isAggregateTypeForABI(Ty)) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001508 // Structures with flexible arrays are always indirect.
1509 // FIXME: This should not be byval!
1510 if (RT && RT->getDecl()->hasFlexibleArrayMember())
1511 return getIndirectResult(Ty, true, State);
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001512
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001513 // Ignore empty structs/unions on non-Windows.
1514 if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001515 return ABIArgInfo::getIgnore();
1516
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +00001517 llvm::LLVMContext &LLVMContext = getVMContext();
1518 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001519 bool NeedsPadding = false;
1520 bool InReg;
1521 if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001522 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperb9bad792013-07-08 04:47:18 +00001523 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001524 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001525 if (InReg)
1526 return ABIArgInfo::getDirectInReg(Result);
1527 else
1528 return ABIArgInfo::getDirect(Result);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001529 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001530 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001531
Daniel Dunbar53012f42009-11-09 01:33:53 +00001532 // Expand small (<= 128-bit) record types when we know that the stack layout
1533 // of those arguments will match the struct. This is important because the
1534 // LLVM backend isn't smart enough to remove byval, which inhibits many
1535 // optimizations.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001536 // Don't do this for the MCU if there are still free integer registers
1537 // (see X86_64 ABI for full explanation).
1538 if (getContext().getTypeSize(Ty) <= 4 * 32 &&
1539 (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty))
Stephen Hines651f13c2014-04-23 16:59:28 -07001540 return ABIArgInfo::getExpandWithPadding(
Stephen Hines176edba2014-12-01 14:53:08 -08001541 State.CC == llvm::CallingConv::X86_FastCall ||
1542 State.CC == llvm::CallingConv::X86_VectorCall,
1543 PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001544
Stephen Hines651f13c2014-04-23 16:59:28 -07001545 return getIndirectResult(Ty, true, State);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001546 }
1547
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001548 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +00001549 // On Darwin, some vectors are passed in memory, we handle this by passing
1550 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001551 if (IsDarwinVectorABI) {
1552 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001553 if ((Size == 8 || Size == 16 || Size == 32) ||
1554 (Size == 64 && VT->getNumElements() == 1))
1555 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1556 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001557 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001558
Chad Rosier1f1df1f2013-03-25 21:00:27 +00001559 if (IsX86_MMXType(CGT.ConvertType(Ty)))
1560 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001561
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001562 return ABIArgInfo::getDirect();
1563 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001564
1565
Chris Lattnera3c109b2010-07-29 02:16:43 +00001566 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1567 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001568
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001569 bool InReg = shouldPrimitiveUseInReg(Ty, State);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001570
1571 if (Ty->isPromotableIntegerType()) {
1572 if (InReg)
1573 return ABIArgInfo::getExtendInReg();
1574 return ABIArgInfo::getExtend();
1575 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001576
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001577 if (InReg)
1578 return ABIArgInfo::getDirectInReg();
1579 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001580}
1581
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +00001582void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001583 CCState State(FI.getCallingConvention());
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001584 if (IsMCUABI)
1585 State.FreeRegs = 3;
1586 else if (State.CC == llvm::CallingConv::X86_FastCall)
Stephen Hines651f13c2014-04-23 16:59:28 -07001587 State.FreeRegs = 2;
Stephen Hines176edba2014-12-01 14:53:08 -08001588 else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1589 State.FreeRegs = 2;
1590 State.FreeSSERegs = 6;
1591 } else if (FI.getHasRegParm())
Stephen Hines651f13c2014-04-23 16:59:28 -07001592 State.FreeRegs = FI.getRegParm();
Rafael Espindolab6932692012-10-24 01:58:58 +00001593 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001594 State.FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +00001595
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001596 if (!getCXXABI().classifyReturnType(FI)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001597 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001598 } else if (FI.getReturnInfo().isIndirect()) {
1599 // The C++ ABI is not aware of register usage, so we have to check if the
1600 // return value was sret and put it in a register ourselves if appropriate.
1601 if (State.FreeRegs) {
1602 --State.FreeRegs; // The sret parameter consumes a register.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001603 if (!IsMCUABI)
1604 FI.getReturnInfo().setInReg(true);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001605 }
1606 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001607
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001608 // The chain argument effectively gives us another free register.
1609 if (FI.isChainCall())
1610 ++State.FreeRegs;
1611
Stephen Hines651f13c2014-04-23 16:59:28 -07001612 bool UsedInAlloca = false;
1613 for (auto &I : FI.arguments()) {
1614 I.info = classifyArgumentType(I.type, State);
1615 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
Rafael Espindolab48280b2012-07-31 02:44:24 +00001616 }
1617
Stephen Hines651f13c2014-04-23 16:59:28 -07001618 // If we needed to use inalloca for any argument, do a second pass and rewrite
1619 // all the memory arguments to use inalloca.
1620 if (UsedInAlloca)
1621 rewriteWithInAlloca(FI);
1622}
1623
1624void
1625X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001626 CharUnits &StackOffset, ABIArgInfo &Info,
1627 QualType Type) const {
1628 // Arguments are always 4-byte-aligned.
1629 CharUnits FieldAlign = CharUnits::fromQuantity(4);
1630
1631 assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001632 Info = ABIArgInfo::getInAlloca(FrameFields.size());
1633 FrameFields.push_back(CGT.ConvertTypeForMem(Type));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001634 StackOffset += getContext().getTypeSizeInChars(Type);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001635
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001636 // Insert padding bytes to respect alignment.
1637 CharUnits FieldEnd = StackOffset;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001638 StackOffset = FieldEnd.alignTo(FieldAlign);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001639 if (StackOffset != FieldEnd) {
1640 CharUnits NumBytes = StackOffset - FieldEnd;
Stephen Hines651f13c2014-04-23 16:59:28 -07001641 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001642 Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
Stephen Hines651f13c2014-04-23 16:59:28 -07001643 FrameFields.push_back(Ty);
1644 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001645}
1646
Stephen Hines176edba2014-12-01 14:53:08 -08001647static bool isArgInAlloca(const ABIArgInfo &Info) {
1648 // Leave ignored and inreg arguments alone.
1649 switch (Info.getKind()) {
1650 case ABIArgInfo::InAlloca:
1651 return true;
1652 case ABIArgInfo::Indirect:
1653 assert(Info.getIndirectByVal());
1654 return true;
1655 case ABIArgInfo::Ignore:
1656 return false;
1657 case ABIArgInfo::Direct:
1658 case ABIArgInfo::Extend:
Stephen Hines176edba2014-12-01 14:53:08 -08001659 if (Info.getInReg())
1660 return false;
1661 return true;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001662 case ABIArgInfo::Expand:
1663 case ABIArgInfo::CoerceAndExpand:
1664 // These are aggregate types which are never passed in registers when
1665 // inalloca is involved.
1666 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001667 }
1668 llvm_unreachable("invalid enum");
1669}
1670
Stephen Hines651f13c2014-04-23 16:59:28 -07001671void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1672 assert(IsWin32StructABI && "inalloca only supported on win32");
1673
1674 // Build a packed struct type for all of the arguments in memory.
1675 SmallVector<llvm::Type *, 6> FrameFields;
1676
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001677 // The stack alignment is always 4.
1678 CharUnits StackAlign = CharUnits::fromQuantity(4);
1679
1680 CharUnits StackOffset;
Stephen Hines176edba2014-12-01 14:53:08 -08001681 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1682
1683 // Put 'this' into the struct before 'sret', if necessary.
1684 bool IsThisCall =
1685 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1686 ABIArgInfo &Ret = FI.getReturnInfo();
1687 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1688 isArgInAlloca(I->info)) {
1689 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1690 ++I;
1691 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001692
1693 // Put the sret parameter into the inalloca struct if it's in memory.
Stephen Hines651f13c2014-04-23 16:59:28 -07001694 if (Ret.isIndirect() && !Ret.getInReg()) {
1695 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1696 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
1697 // On Windows, the hidden sret parameter is always returned in eax.
1698 Ret.setInAllocaSRet(IsWin32StructABI);
1699 }
1700
1701 // Skip the 'this' parameter in ecx.
Stephen Hines176edba2014-12-01 14:53:08 -08001702 if (IsThisCall)
Stephen Hines651f13c2014-04-23 16:59:28 -07001703 ++I;
1704
1705 // Put arguments passed in memory into the struct.
1706 for (; I != E; ++I) {
Stephen Hines176edba2014-12-01 14:53:08 -08001707 if (isArgInAlloca(I->info))
1708 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
Stephen Hines651f13c2014-04-23 16:59:28 -07001709 }
1710
1711 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001712 /*isPacked=*/true),
1713 StackAlign);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +00001714}
1715
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001716Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
1717 Address VAListAddr, QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001718
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001719 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
Eli Friedman7b1fb812011-11-18 02:12:09 +00001720
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001721 // x86-32 changes the alignment of certain arguments on the stack.
1722 //
1723 // Just messing with TypeInfo like this works because we never pass
1724 // anything indirectly.
1725 TypeInfo.second = CharUnits::fromQuantity(
1726 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
Eli Friedman7b1fb812011-11-18 02:12:09 +00001727
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001728 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
1729 TypeInfo, CharUnits::fromQuantity(4),
1730 /*AllowHigherAlign*/ true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001731}
1732
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001733bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1734 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1735 assert(Triple.getArch() == llvm::Triple::x86);
1736
1737 switch (Opts.getStructReturnConvention()) {
1738 case CodeGenOptions::SRCK_Default:
1739 break;
1740 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
1741 return false;
1742 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
1743 return true;
1744 }
1745
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001746 if (Triple.isOSDarwin() || Triple.isOSIAMCU())
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001747 return true;
1748
1749 switch (Triple.getOS()) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001750 case llvm::Triple::DragonFly:
1751 case llvm::Triple::FreeBSD:
1752 case llvm::Triple::OpenBSD:
1753 case llvm::Triple::Bitrig:
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001754 case llvm::Triple::Win32:
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001755 return true;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001756 default:
1757 return false;
1758 }
1759}
1760
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001761void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Charles Davis74f72932010-02-13 15:54:06 +00001762 llvm::GlobalValue *GV,
1763 CodeGen::CodeGenModule &CGM) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001764 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Charles Davis74f72932010-02-13 15:54:06 +00001765 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1766 // Get the LLVM function.
1767 llvm::Function *Fn = cast<llvm::Function>(GV);
1768
1769 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001770 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001771 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001772 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1773 llvm::AttributeSet::get(CGM.getLLVMContext(),
1774 llvm::AttributeSet::FunctionIndex,
1775 B));
Charles Davis74f72932010-02-13 15:54:06 +00001776 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001777 if (FD->hasAttr<AnyX86InterruptAttr>()) {
1778 llvm::Function *Fn = cast<llvm::Function>(GV);
1779 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
1780 }
Charles Davis74f72932010-02-13 15:54:06 +00001781 }
1782}
1783
John McCall6374c332010-03-06 00:35:14 +00001784bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1785 CodeGen::CodeGenFunction &CGF,
1786 llvm::Value *Address) const {
1787 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001788
Chris Lattner8b418682012-02-07 00:39:47 +00001789 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001790
John McCall6374c332010-03-06 00:35:14 +00001791 // 0-7 are the eight integer registers; the order is different
1792 // on Darwin (for EH), but the range is the same.
1793 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001794 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001795
John McCall64aa4b32013-04-16 22:48:15 +00001796 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCall6374c332010-03-06 00:35:14 +00001797 // 12-16 are st(0..4). Not sure why we stop at 4.
1798 // These have size 16, which is sizeof(long double) on
1799 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001800 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001801 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001802
John McCall6374c332010-03-06 00:35:14 +00001803 } else {
1804 // 9 is %eflags, which doesn't get a size on Darwin for some
1805 // reason.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001806 Builder.CreateAlignedStore(
1807 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
1808 CharUnits::One());
John McCall6374c332010-03-06 00:35:14 +00001809
1810 // 11-16 are st(0..5). Not sure why we stop at 5.
1811 // These have size 12, which is sizeof(long double) on
1812 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001813 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001814 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1815 }
John McCall6374c332010-03-06 00:35:14 +00001816
1817 return false;
1818}
1819
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001820//===----------------------------------------------------------------------===//
1821// X86-64 ABI Implementation
1822//===----------------------------------------------------------------------===//
1823
1824
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001825namespace {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001826/// The AVX ABI level for X86 targets.
1827enum class X86AVXABILevel {
1828 None,
1829 AVX,
1830 AVX512
1831};
1832
1833/// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
1834static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
1835 switch (AVXLevel) {
1836 case X86AVXABILevel::AVX512:
1837 return 512;
1838 case X86AVXABILevel::AVX:
1839 return 256;
1840 case X86AVXABILevel::None:
1841 return 128;
1842 }
1843 llvm_unreachable("Unknown AVXLevel");
1844}
1845
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001846/// X86_64ABIInfo - The X86_64 ABI information.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001847class X86_64ABIInfo : public SwiftABIInfo {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001848 enum Class {
1849 Integer = 0,
1850 SSE,
1851 SSEUp,
1852 X87,
1853 X87Up,
1854 ComplexX87,
1855 NoClass,
1856 Memory
1857 };
1858
1859 /// merge - Implement the X86_64 ABI merging algorithm.
1860 ///
1861 /// Merge an accumulating classification \arg Accum with a field
1862 /// classification \arg Field.
1863 ///
1864 /// \param Accum - The accumulating classification. This should
1865 /// always be either NoClass or the result of a previous merge
1866 /// call. In addition, this should never be Memory (the caller
1867 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001868 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001869
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001870 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1871 ///
1872 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1873 /// final MEMORY or SSE classes when necessary.
1874 ///
1875 /// \param AggregateSize - The size of the current aggregate in
1876 /// the classification process.
1877 ///
1878 /// \param Lo - The classification for the parts of the type
1879 /// residing in the low word of the containing object.
1880 ///
1881 /// \param Hi - The classification for the parts of the type
1882 /// residing in the higher words of the containing object.
1883 ///
1884 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1885
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001886 /// classify - Determine the x86_64 register classes in which the
1887 /// given type T should be passed.
1888 ///
1889 /// \param Lo - The classification for the parts of the type
1890 /// residing in the low word of the containing object.
1891 ///
1892 /// \param Hi - The classification for the parts of the type
1893 /// residing in the high word of the containing object.
1894 ///
1895 /// \param OffsetBase - The bit offset of this type in the
1896 /// containing object. Some parameters are classified different
1897 /// depending on whether they straddle an eightbyte boundary.
1898 ///
Eli Friedman7a1b5862013-06-12 00:13:45 +00001899 /// \param isNamedArg - Whether the argument in question is a "named"
1900 /// argument, as used in AMD64-ABI 3.5.7.
1901 ///
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001902 /// If a word is unused its result will be NoClass; if a type should
1903 /// be passed in Memory then at least the classification of \arg Lo
1904 /// will be Memory.
1905 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001906 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001907 ///
1908 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1909 /// also be ComplexX87.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001910 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1911 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001912
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001913 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001914 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1915 unsigned IROffset, QualType SourceTy,
1916 unsigned SourceOffset) const;
1917 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1918 unsigned IROffset, QualType SourceTy,
1919 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001920
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001921 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001922 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001923 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001924
1925 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001926 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001927 ///
1928 /// \param freeIntRegs - The number of free integer registers remaining
1929 /// available.
1930 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001931
Chris Lattnera3c109b2010-07-29 02:16:43 +00001932 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001933
Bill Wendlingbb465d72010-10-18 03:41:31 +00001934 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001935 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001936 unsigned &neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001937 unsigned &neededSSE,
1938 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001939
Eli Friedmanee1ad992011-12-02 00:11:43 +00001940 bool IsIllegalVectorType(QualType Ty) const;
1941
John McCall67a57732011-04-21 01:20:55 +00001942 /// The 0.98 ABI revision clarified a lot of ambiguities,
1943 /// unfortunately in ways that were not always consistent with
1944 /// certain previous compilers. In particular, platforms which
1945 /// required strict binary compatibility with older versions of GCC
1946 /// may need to exempt themselves.
1947 bool honorsRevision0_98() const {
John McCall64aa4b32013-04-16 22:48:15 +00001948 return !getTarget().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001949 }
1950
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001951 /// GCC classifies <1 x long long> as SSE but compatibility with older clang
1952 // compilers require us to classify it as INTEGER.
1953 bool classifyIntegerMMXAsSSE() const {
1954 const llvm::Triple &Triple = getTarget().getTriple();
1955 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
1956 return false;
1957 if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
1958 return false;
1959 return true;
1960 }
1961
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001962 X86AVXABILevel AVXLevel;
Derek Schuffbabaf312012-10-11 15:52:22 +00001963 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1964 // 64-bit hardware.
1965 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001966
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001967public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001968 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001969 SwiftABIInfo(CGT), AVXLevel(AVXLevel),
Derek Schuff90da80c2012-10-11 18:21:13 +00001970 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001971 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001972
John McCallde5d3c72012-02-17 03:33:10 +00001973 bool isPassedUsingAVXType(QualType type) const {
1974 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001975 // The freeIntRegs argument doesn't matter here.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001976 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1977 /*isNamedArg*/true);
John McCallde5d3c72012-02-17 03:33:10 +00001978 if (info.isDirect()) {
1979 llvm::Type *ty = info.getCoerceToType();
1980 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1981 return (vectorTy->getBitWidth() > 128);
1982 }
1983 return false;
1984 }
1985
Stephen Hines651f13c2014-04-23 16:59:28 -07001986 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001987
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001988 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1989 QualType Ty) const override;
1990 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
1991 QualType Ty) const override;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001992
1993 bool has64BitPointers() const {
1994 return Has64BitPointers;
1995 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001996
1997 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
1998 ArrayRef<llvm::Type*> scalars,
1999 bool asReturnValue) const override {
2000 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2001 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002002};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002003
Chris Lattnerf13721d2010-08-31 16:44:54 +00002004/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002005class WinX86_64ABIInfo : public ABIInfo {
Chris Lattnerf13721d2010-08-31 16:44:54 +00002006public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002007 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT)
2008 : ABIInfo(CGT),
2009 IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002010
Stephen Hines651f13c2014-04-23 16:59:28 -07002011 void computeInfo(CGFunctionInfo &FI) const override;
Chris Lattnerf13721d2010-08-31 16:44:54 +00002012
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002013 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2014 QualType Ty) const override;
Stephen Hines176edba2014-12-01 14:53:08 -08002015
2016 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2017 // FIXME: Assumes vectorcall is in use.
2018 return isX86VectorTypeForVectorCall(getContext(), Ty);
2019 }
2020
2021 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2022 uint64_t NumMembers) const override {
2023 // FIXME: Assumes vectorcall is in use.
2024 return isX86VectorCallAggregateSmallEnough(NumMembers);
2025 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002026
2027private:
2028 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs,
2029 bool IsReturnType) const;
2030
2031 bool IsMingw64;
Chris Lattnerf13721d2010-08-31 16:44:54 +00002032};
2033
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002034class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2035public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002036 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2037 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
John McCall6374c332010-03-06 00:35:14 +00002038
John McCallde5d3c72012-02-17 03:33:10 +00002039 const X86_64ABIInfo &getABIInfo() const {
2040 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2041 }
2042
Stephen Hines651f13c2014-04-23 16:59:28 -07002043 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCall6374c332010-03-06 00:35:14 +00002044 return 7;
2045 }
2046
2047 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07002048 llvm::Value *Address) const override {
Chris Lattner8b418682012-02-07 00:39:47 +00002049 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002050
John McCallaeeb7012010-05-27 06:19:26 +00002051 // 0-15 are the 16 integer registers.
2052 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00002053 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00002054 return false;
2055 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00002056
Jay Foadef6de3d2011-07-11 09:56:20 +00002057 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002058 StringRef Constraint,
Stephen Hines651f13c2014-04-23 16:59:28 -07002059 llvm::Type* Ty) const override {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00002060 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2061 }
2062
John McCallde5d3c72012-02-17 03:33:10 +00002063 bool isNoProtoCallVariadic(const CallArgList &args,
Stephen Hines651f13c2014-04-23 16:59:28 -07002064 const FunctionNoProtoType *fnType) const override {
John McCall01f151e2011-09-21 08:08:30 +00002065 // The default CC on x86-64 sets %al to the number of SSA
2066 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00002067 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00002068 // that when AVX types are involved: the ABI explicitly states it is
2069 // undefined, and it doesn't work in practice because of how the ABI
2070 // defines varargs anyway.
Reid Kleckneref072032013-08-27 23:08:25 +00002071 if (fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00002072 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00002073 for (CallArgList::const_iterator
2074 it = args.begin(), ie = args.end(); it != ie; ++it) {
2075 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2076 HasAVXType = true;
2077 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00002078 }
2079 }
John McCallde5d3c72012-02-17 03:33:10 +00002080
Eli Friedman3ed79032011-12-01 04:53:19 +00002081 if (!HasAVXType)
2082 return true;
2083 }
John McCall01f151e2011-09-21 08:08:30 +00002084
John McCallde5d3c72012-02-17 03:33:10 +00002085 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00002086 }
2087
Stephen Hines651f13c2014-04-23 16:59:28 -07002088 llvm::Constant *
2089 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002090 unsigned Sig;
2091 if (getABIInfo().has64BitPointers())
2092 Sig = (0xeb << 0) | // jmp rel8
2093 (0x0a << 8) | // .+0x0c
2094 ('F' << 16) |
2095 ('T' << 24);
2096 else
2097 Sig = (0xeb << 0) | // jmp rel8
2098 (0x06 << 8) | // .+0x08
2099 ('F' << 16) |
2100 ('T' << 24);
Peter Collingbourneb914e872013-10-20 21:29:19 +00002101 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2102 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002103
2104 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2105 CodeGen::CodeGenModule &CGM) const override {
2106 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2107 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2108 llvm::Function *Fn = cast<llvm::Function>(GV);
2109 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2110 }
2111 }
2112 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002113};
2114
2115class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
2116public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002117 PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2118 : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002119
2120 void getDependentLibraryOption(llvm::StringRef Lib,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002121 llvm::SmallString<24> &Opt) const override {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002122 Opt = "\01";
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002123 // If the argument contains a space, enclose it in quotes.
2124 if (Lib.find(" ") != StringRef::npos)
2125 Opt += "\"" + Lib.str() + "\"";
2126 else
2127 Opt += Lib;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002128 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002129};
2130
Aaron Ballman89735b92013-05-24 15:06:56 +00002131static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002132 // If the argument does not end in .lib, automatically add the suffix.
2133 // If the argument contains a space, enclose it in quotes.
2134 // This matches the behavior of MSVC.
2135 bool Quote = (Lib.find(" ") != StringRef::npos);
2136 std::string ArgStr = Quote ? "\"" : "";
2137 ArgStr += Lib;
Rui Ueyama723cead2013-10-31 19:12:53 +00002138 if (!Lib.endswith_lower(".lib"))
Aaron Ballman89735b92013-05-24 15:06:56 +00002139 ArgStr += ".lib";
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002140 ArgStr += Quote ? "\"" : "";
Aaron Ballman89735b92013-05-24 15:06:56 +00002141 return ArgStr;
2142}
2143
Reid Kleckner3190ca92013-05-08 13:44:39 +00002144class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2145public:
John McCallb8b52972013-06-18 02:46:29 +00002146 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002147 bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2148 unsigned NumRegisterParameters)
2149 : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
2150 Win32StructABI, NumRegisterParameters, false) {}
Reid Kleckner3190ca92013-05-08 13:44:39 +00002151
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002152 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002153 CodeGen::CodeGenModule &CGM) const override;
2154
Reid Kleckner3190ca92013-05-08 13:44:39 +00002155 void getDependentLibraryOption(llvm::StringRef Lib,
Stephen Hines651f13c2014-04-23 16:59:28 -07002156 llvm::SmallString<24> &Opt) const override {
Reid Kleckner3190ca92013-05-08 13:44:39 +00002157 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00002158 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00002159 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00002160
2161 void getDetectMismatchOption(llvm::StringRef Name,
2162 llvm::StringRef Value,
Stephen Hines651f13c2014-04-23 16:59:28 -07002163 llvm::SmallString<32> &Opt) const override {
Eli Friedman572ac322013-06-07 22:42:22 +00002164 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00002165 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00002166};
2167
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002168static void addStackProbeSizeTargetAttribute(const Decl *D,
2169 llvm::GlobalValue *GV,
2170 CodeGen::CodeGenModule &CGM) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002171 if (D && isa<FunctionDecl>(D)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002172 if (CGM.getCodeGenOpts().StackProbeSize != 4096) {
2173 llvm::Function *Fn = cast<llvm::Function>(GV);
2174
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002175 Fn->addFnAttr("stack-probe-size",
2176 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002177 }
2178 }
2179}
2180
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002181void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002182 llvm::GlobalValue *GV,
2183 CodeGen::CodeGenModule &CGM) const {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002184 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002185
2186 addStackProbeSizeTargetAttribute(D, GV, CGM);
2187}
2188
Chris Lattnerf13721d2010-08-31 16:44:54 +00002189class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002190public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002191 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2192 X86AVXABILevel AVXLevel)
2193 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002194
2195 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002196 CodeGen::CodeGenModule &CGM) const override;
2197
Stephen Hines651f13c2014-04-23 16:59:28 -07002198 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
Chris Lattnerf13721d2010-08-31 16:44:54 +00002199 return 7;
2200 }
2201
2202 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07002203 llvm::Value *Address) const override {
Chris Lattner8b418682012-02-07 00:39:47 +00002204 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002205
Chris Lattnerf13721d2010-08-31 16:44:54 +00002206 // 0-15 are the 16 integer registers.
2207 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00002208 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00002209 return false;
2210 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00002211
2212 void getDependentLibraryOption(llvm::StringRef Lib,
Stephen Hines651f13c2014-04-23 16:59:28 -07002213 llvm::SmallString<24> &Opt) const override {
Reid Kleckner3190ca92013-05-08 13:44:39 +00002214 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00002215 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00002216 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00002217
2218 void getDetectMismatchOption(llvm::StringRef Name,
2219 llvm::StringRef Value,
Stephen Hines651f13c2014-04-23 16:59:28 -07002220 llvm::SmallString<32> &Opt) const override {
Eli Friedman572ac322013-06-07 22:42:22 +00002221 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00002222 }
Chris Lattnerf13721d2010-08-31 16:44:54 +00002223};
2224
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002225void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002226 llvm::GlobalValue *GV,
2227 CodeGen::CodeGenModule &CGM) const {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002228 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002229
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002230 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2231 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2232 llvm::Function *Fn = cast<llvm::Function>(GV);
2233 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2234 }
2235 }
2236
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002237 addStackProbeSizeTargetAttribute(D, GV, CGM);
2238}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002239}
2240
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002241void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2242 Class &Hi) const {
2243 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2244 //
2245 // (a) If one of the classes is Memory, the whole argument is passed in
2246 // memory.
2247 //
2248 // (b) If X87UP is not preceded by X87, the whole argument is passed in
2249 // memory.
2250 //
2251 // (c) If the size of the aggregate exceeds two eightbytes and the first
2252 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2253 // argument is passed in memory. NOTE: This is necessary to keep the
2254 // ABI working for processors that don't support the __m256 type.
2255 //
2256 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2257 //
2258 // Some of these are enforced by the merging logic. Others can arise
2259 // only with unions; for example:
2260 // union { _Complex double; unsigned; }
2261 //
2262 // Note that clauses (b) and (c) were added in 0.98.
2263 //
2264 if (Hi == Memory)
2265 Lo = Memory;
2266 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2267 Lo = Memory;
2268 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2269 Lo = Memory;
2270 if (Hi == SSEUp && Lo != SSE)
2271 Hi = SSE;
2272}
2273
Chris Lattner1090a9b2010-06-28 21:43:59 +00002274X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002275 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2276 // classified recursively so that always two fields are
2277 // considered. The resulting class is calculated according to
2278 // the classes of the fields in the eightbyte:
2279 //
2280 // (a) If both classes are equal, this is the resulting class.
2281 //
2282 // (b) If one of the classes is NO_CLASS, the resulting class is
2283 // the other class.
2284 //
2285 // (c) If one of the classes is MEMORY, the result is the MEMORY
2286 // class.
2287 //
2288 // (d) If one of the classes is INTEGER, the result is the
2289 // INTEGER.
2290 //
2291 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2292 // MEMORY is used as class.
2293 //
2294 // (f) Otherwise class SSE is used.
2295
2296 // Accum should never be memory (we should have returned) or
2297 // ComplexX87 (because this cannot be passed in a structure).
2298 assert((Accum != Memory && Accum != ComplexX87) &&
2299 "Invalid accumulated classification during merge.");
2300 if (Accum == Field || Field == NoClass)
2301 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002302 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002303 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002304 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002305 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002306 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002307 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002308 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2309 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002310 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002311 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002312}
2313
Chris Lattnerbcaedae2010-06-30 19:14:05 +00002314void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman7a1b5862013-06-12 00:13:45 +00002315 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002316 // FIXME: This code can be simplified by introducing a simple value class for
2317 // Class pairs with appropriate constructor methods for the various
2318 // situations.
2319
2320 // FIXME: Some of the split computations are wrong; unaligned vectors
2321 // shouldn't be passed in registers for example, so there is no chance they
2322 // can straddle an eightbyte. Verify & simplify.
2323
2324 Lo = Hi = NoClass;
2325
2326 Class &Current = OffsetBase < 64 ? Lo : Hi;
2327 Current = Memory;
2328
John McCall183700f2009-09-21 23:43:11 +00002329 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002330 BuiltinType::Kind k = BT->getKind();
2331
2332 if (k == BuiltinType::Void) {
2333 Current = NoClass;
2334 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2335 Lo = Integer;
2336 Hi = Integer;
2337 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2338 Current = Integer;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002339 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002340 Current = SSE;
2341 } else if (k == BuiltinType::LongDouble) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002342 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2343 if (LDF == &llvm::APFloat::IEEEquad) {
2344 Lo = SSE;
2345 Hi = SSEUp;
2346 } else if (LDF == &llvm::APFloat::x87DoubleExtended) {
2347 Lo = X87;
2348 Hi = X87Up;
2349 } else if (LDF == &llvm::APFloat::IEEEdouble) {
2350 Current = SSE;
2351 } else
2352 llvm_unreachable("unexpected long double representation!");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002353 }
2354 // FIXME: _Decimal32 and _Decimal64 are SSE.
2355 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00002356 return;
2357 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002358
Chris Lattner1090a9b2010-06-28 21:43:59 +00002359 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002360 // Classify the underlying integer type.
Eli Friedman7a1b5862013-06-12 00:13:45 +00002361 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002362 return;
2363 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002364
Chris Lattner1090a9b2010-06-28 21:43:59 +00002365 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002366 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002367 return;
2368 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002369
Chris Lattner1090a9b2010-06-28 21:43:59 +00002370 if (Ty->isMemberPointerType()) {
Stephen Hines176edba2014-12-01 14:53:08 -08002371 if (Ty->isMemberFunctionPointerType()) {
2372 if (Has64BitPointers) {
2373 // If Has64BitPointers, this is an {i64, i64}, so classify both
2374 // Lo and Hi now.
2375 Lo = Hi = Integer;
2376 } else {
2377 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2378 // straddles an eightbyte boundary, Hi should be classified as well.
2379 uint64_t EB_FuncPtr = (OffsetBase) / 64;
2380 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2381 if (EB_FuncPtr != EB_ThisAdj) {
2382 Lo = Hi = Integer;
2383 } else {
2384 Current = Integer;
2385 }
2386 }
2387 } else {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00002388 Current = Integer;
Stephen Hines176edba2014-12-01 14:53:08 -08002389 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00002390 return;
2391 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002392
Chris Lattner1090a9b2010-06-28 21:43:59 +00002393 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00002394 uint64_t Size = getContext().getTypeSize(VT);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002395 if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2396 // gcc passes the following as integer:
2397 // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2398 // 2 bytes - <2 x char>, <1 x short>
2399 // 1 byte - <1 x char>
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002400 Current = Integer;
2401
2402 // If this type crosses an eightbyte boundary, it should be
2403 // split.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002404 uint64_t EB_Lo = (OffsetBase) / 64;
2405 uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2406 if (EB_Lo != EB_Hi)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002407 Hi = Lo;
2408 } else if (Size == 64) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002409 QualType ElementType = VT->getElementType();
2410
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002411 // gcc passes <1 x double> in memory. :(
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002412 if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002413 return;
2414
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002415 // gcc passes <1 x long long> as SSE but clang used to unconditionally
2416 // pass them as integer. For platforms where clang is the de facto
2417 // platform compiler, we must continue to use integer.
2418 if (!classifyIntegerMMXAsSSE() &&
2419 (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2420 ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2421 ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2422 ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002423 Current = Integer;
2424 else
2425 Current = SSE;
2426
2427 // If this type crosses an eightbyte boundary, it should be
2428 // split.
2429 if (OffsetBase && OffsetBase != 64)
2430 Hi = Lo;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002431 } else if (Size == 128 ||
2432 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002433 // Arguments of 256-bits are split into four eightbyte chunks. The
2434 // least significant one belongs to class SSE and all the others to class
2435 // SSEUP. The original Lo and Hi design considers that types can't be
2436 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2437 // This design isn't correct for 256-bits, but since there're no cases
2438 // where the upper parts would need to be inspected, avoid adding
2439 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman7a1b5862013-06-12 00:13:45 +00002440 //
2441 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2442 // registers if they are "named", i.e. not part of the "..." of a
2443 // variadic function.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002444 //
2445 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2446 // split into eight eightbyte chunks, one SSE and seven SSEUP.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002447 Lo = SSE;
2448 Hi = SSEUp;
2449 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00002450 return;
2451 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002452
Chris Lattner1090a9b2010-06-28 21:43:59 +00002453 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00002454 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002455
Chris Lattnerea044322010-07-29 02:01:43 +00002456 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002457 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002458 if (Size <= 64)
2459 Current = Integer;
2460 else if (Size <= 128)
2461 Lo = Hi = Integer;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002462 } else if (ET == getContext().FloatTy) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002463 Current = SSE;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002464 } else if (ET == getContext().DoubleTy) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002465 Lo = Hi = SSE;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002466 } else if (ET == getContext().LongDoubleTy) {
2467 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2468 if (LDF == &llvm::APFloat::IEEEquad)
2469 Current = Memory;
2470 else if (LDF == &llvm::APFloat::x87DoubleExtended)
2471 Current = ComplexX87;
2472 else if (LDF == &llvm::APFloat::IEEEdouble)
2473 Lo = Hi = SSE;
2474 else
2475 llvm_unreachable("unexpected long double representation!");
2476 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002477
2478 // If this complex type crosses an eightbyte boundary then it
2479 // should be split.
2480 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00002481 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002482 if (Hi == NoClass && EB_Real != EB_Imag)
2483 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002484
Chris Lattner1090a9b2010-06-28 21:43:59 +00002485 return;
2486 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002487
Chris Lattnerea044322010-07-29 02:01:43 +00002488 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002489 // Arrays are treated like structures.
2490
Chris Lattnerea044322010-07-29 02:01:43 +00002491 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002492
2493 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002494 // than four eightbytes, ..., it has class MEMORY.
2495 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002496 return;
2497
2498 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2499 // fields, it has class MEMORY.
2500 //
2501 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00002502 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002503 return;
2504
2505 // Otherwise implement simplified merge. We could be smarter about
2506 // this, but it isn't worth it and would be harder to verify.
2507 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00002508 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002509 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00002510
2511 // The only case a 256-bit wide vector could be used is when the array
2512 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2513 // to work for sizes wider than 128, early check and fallback to memory.
2514 if (Size > 128 && EltSize != 256)
2515 return;
2516
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002517 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2518 Class FieldLo, FieldHi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002519 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002520 Lo = merge(Lo, FieldLo);
2521 Hi = merge(Hi, FieldHi);
2522 if (Lo == Memory || Hi == Memory)
2523 break;
2524 }
2525
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002526 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002527 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002528 return;
2529 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002530
Chris Lattner1090a9b2010-06-28 21:43:59 +00002531 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00002532 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002533
2534 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002535 // than four eightbytes, ..., it has class MEMORY.
2536 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002537 return;
2538
Anders Carlsson0a8f8472009-09-16 15:53:40 +00002539 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2540 // copy constructor or a non-trivial destructor, it is passed by invisible
2541 // reference.
Mark Lacey23630722013-10-06 01:33:34 +00002542 if (getRecordArgABI(RT, getCXXABI()))
Anders Carlsson0a8f8472009-09-16 15:53:40 +00002543 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002544
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002545 const RecordDecl *RD = RT->getDecl();
2546
2547 // Assume variable sized types are passed in memory.
2548 if (RD->hasFlexibleArrayMember())
2549 return;
2550
Chris Lattnerea044322010-07-29 02:01:43 +00002551 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002552
2553 // Reset Lo class, this will be recomputed.
2554 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002555
2556 // If this is a C++ record, classify the bases first.
2557 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002558 for (const auto &I : CXXRD->bases()) {
2559 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002560 "Unexpected base class!");
2561 const CXXRecordDecl *Base =
Stephen Hines651f13c2014-04-23 16:59:28 -07002562 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002563
2564 // Classify this field.
2565 //
2566 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2567 // single eightbyte, each is classified separately. Each eightbyte gets
2568 // initialized to class NO_CLASS.
2569 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00002570 uint64_t Offset =
2571 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Stephen Hines651f13c2014-04-23 16:59:28 -07002572 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002573 Lo = merge(Lo, FieldLo);
2574 Hi = merge(Hi, FieldHi);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002575 if (Lo == Memory || Hi == Memory) {
2576 postMerge(Size, Lo, Hi);
2577 return;
2578 }
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002579 }
2580 }
2581
2582 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002583 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00002584 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002585 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002586 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2587 bool BitField = i->isBitField();
2588
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00002589 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2590 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002591 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00002592 // The only case a 256-bit wide vector could be used is when the struct
2593 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2594 // to work for sizes wider than 128, early check and fallback to memory.
2595 //
2596 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
2597 Lo = Memory;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002598 postMerge(Size, Lo, Hi);
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00002599 return;
2600 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002601 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00002602 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002603 Lo = Memory;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002604 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002605 return;
2606 }
2607
2608 // Classify this field.
2609 //
2610 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2611 // exceeds a single eightbyte, each is classified
2612 // separately. Each eightbyte gets initialized to class
2613 // NO_CLASS.
2614 Class FieldLo, FieldHi;
2615
2616 // Bit-fields require special handling, they do not force the
2617 // structure to be passed in memory even if unaligned, and
2618 // therefore they can straddle an eightbyte.
2619 if (BitField) {
2620 // Ignore padding bit-fields.
2621 if (i->isUnnamedBitfield())
2622 continue;
2623
2624 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002625 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002626
2627 uint64_t EB_Lo = Offset / 64;
2628 uint64_t EB_Hi = (Offset + Size - 1) / 64;
Sylvestre Ledru9a6002a2013-10-06 09:54:18 +00002629
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002630 if (EB_Lo) {
2631 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2632 FieldLo = NoClass;
2633 FieldHi = Integer;
2634 } else {
2635 FieldLo = Integer;
2636 FieldHi = EB_Hi ? Integer : NoClass;
2637 }
2638 } else
Eli Friedman7a1b5862013-06-12 00:13:45 +00002639 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002640 Lo = merge(Lo, FieldLo);
2641 Hi = merge(Hi, FieldHi);
2642 if (Lo == Memory || Hi == Memory)
2643 break;
2644 }
2645
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002646 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002647 }
2648}
2649
Chris Lattner9c254f02010-06-29 06:01:59 +00002650ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00002651 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2652 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00002653 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00002654 // Treat an enum type as its underlying type.
2655 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2656 Ty = EnumTy->getDecl()->getIntegerType();
2657
2658 return (Ty->isPromotableIntegerType() ?
2659 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2660 }
2661
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002662 return getNaturalAlignIndirect(Ty);
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00002663}
2664
Eli Friedmanee1ad992011-12-02 00:11:43 +00002665bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2666 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2667 uint64_t Size = getContext().getTypeSize(VecTy);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002668 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
Eli Friedmanee1ad992011-12-02 00:11:43 +00002669 if (Size <= 64 || Size > LargestVector)
2670 return true;
2671 }
2672
2673 return false;
2674}
2675
Daniel Dunbaredfac032012-03-10 01:03:58 +00002676ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2677 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002678 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2679 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00002680 //
2681 // This assumption is optimistic, as there could be free registers available
2682 // when we need to pass this argument in memory, and LLVM could try to pass
2683 // the argument in the free register. This does not seem to happen currently,
2684 // but this code would be much safer if we could mark the argument with
2685 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00002686 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002687 // Treat an enum type as its underlying type.
2688 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2689 Ty = EnumTy->getDecl()->getIntegerType();
2690
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002691 return (Ty->isPromotableIntegerType() ?
2692 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002693 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002694
Mark Lacey23630722013-10-06 01:33:34 +00002695 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002696 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00002697
Chris Lattner855d2272011-05-22 23:21:23 +00002698 // Compute the byval alignment. We specify the alignment of the byval in all
2699 // cases so that the mid-level optimizer knows the alignment of the byval.
2700 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002701
2702 // Attempt to avoid passing indirect results using byval when possible. This
2703 // is important for good codegen.
2704 //
2705 // We do this by coercing the value into a scalar type which the backend can
2706 // handle naturally (i.e., without using byval).
2707 //
2708 // For simplicity, we currently only do this when we have exhausted all of the
2709 // free integer registers. Doing this when there are free integer registers
2710 // would require more care, as we would have to ensure that the coerced value
2711 // did not claim the unused register. That would require either reording the
2712 // arguments to the function (so that any subsequent inreg values came first),
2713 // or only doing this optimization when there were no following arguments that
2714 // might be inreg.
2715 //
2716 // We currently expect it to be rare (particularly in well written code) for
2717 // arguments to be passed on the stack when there are still free integer
2718 // registers available (this would typically imply large structs being passed
2719 // by value), so this seems like a fair tradeoff for now.
2720 //
2721 // We can revisit this if the backend grows support for 'onstack' parameter
2722 // attributes. See PR12193.
2723 if (freeIntRegs == 0) {
2724 uint64_t Size = getContext().getTypeSize(Ty);
2725
2726 // If this type fits in an eightbyte, coerce it into the matching integral
2727 // type, which will end up on the stack (with alignment 8).
2728 if (Align == 8 && Size <= 64)
2729 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2730 Size));
2731 }
2732
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002733 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002734}
2735
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002736/// The ABI specifies that a value should be passed in a full vector XMM/YMM
2737/// register. Pick an LLVM IR type that will be passed as a vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002738llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002739 // Wrapper structs/arrays that only contain vectors are passed just like
2740 // vectors; strip them off if present.
2741 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
2742 Ty = QualType(InnerTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002743
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002744 llvm::Type *IRType = CGT.ConvertType(Ty);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002745 if (isa<llvm::VectorType>(IRType) ||
2746 IRType->getTypeID() == llvm::Type::FP128TyID)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002747 return IRType;
2748
2749 // We couldn't find the preferred IR vector type for 'Ty'.
2750 uint64_t Size = getContext().getTypeSize(Ty);
2751 assert((Size == 128 || Size == 256) && "Invalid type found!");
2752
2753 // Return a LLVM IR vector type based on the size of 'Ty'.
2754 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
2755 Size / 64);
Chris Lattner0f408f52010-07-29 04:56:46 +00002756}
2757
Chris Lattnere2962be2010-07-29 07:30:00 +00002758/// BitsContainNoUserData - Return true if the specified [start,end) bit range
2759/// is known to either be off the end of the specified type or being in
2760/// alignment padding. The user type specified is known to be at most 128 bits
2761/// in size, and have passed through X86_64ABIInfo::classify with a successful
2762/// classification that put one of the two halves in the INTEGER class.
2763///
2764/// It is conservatively correct to return false.
2765static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2766 unsigned EndBit, ASTContext &Context) {
2767 // If the bytes being queried are off the end of the type, there is no user
2768 // data hiding here. This handles analysis of builtins, vectors and other
2769 // types that don't contain interesting padding.
2770 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2771 if (TySize <= StartBit)
2772 return true;
2773
Chris Lattner021c3a32010-07-29 07:43:55 +00002774 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2775 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2776 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2777
2778 // Check each element to see if the element overlaps with the queried range.
2779 for (unsigned i = 0; i != NumElts; ++i) {
2780 // If the element is after the span we care about, then we're done..
2781 unsigned EltOffset = i*EltSize;
2782 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002783
Chris Lattner021c3a32010-07-29 07:43:55 +00002784 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2785 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
2786 EndBit-EltOffset, Context))
2787 return false;
2788 }
2789 // If it overlaps no elements, then it is safe to process as padding.
2790 return true;
2791 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002792
Chris Lattnere2962be2010-07-29 07:30:00 +00002793 if (const RecordType *RT = Ty->getAs<RecordType>()) {
2794 const RecordDecl *RD = RT->getDecl();
2795 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002796
Chris Lattnere2962be2010-07-29 07:30:00 +00002797 // If this is a C++ record, check the bases first.
2798 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002799 for (const auto &I : CXXRD->bases()) {
2800 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Chris Lattnere2962be2010-07-29 07:30:00 +00002801 "Unexpected base class!");
2802 const CXXRecordDecl *Base =
Stephen Hines651f13c2014-04-23 16:59:28 -07002803 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002804
Chris Lattnere2962be2010-07-29 07:30:00 +00002805 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00002806 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00002807 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002808
Chris Lattnere2962be2010-07-29 07:30:00 +00002809 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
Stephen Hines651f13c2014-04-23 16:59:28 -07002810 if (!BitsContainNoUserData(I.getType(), BaseStart,
Chris Lattnere2962be2010-07-29 07:30:00 +00002811 EndBit-BaseOffset, Context))
2812 return false;
2813 }
2814 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002815
Chris Lattnere2962be2010-07-29 07:30:00 +00002816 // Verify that no field has data that overlaps the region of interest. Yes
2817 // this could be sped up a lot by being smarter about queried fields,
2818 // however we're only looking at structs up to 16 bytes, so we don't care
2819 // much.
2820 unsigned idx = 0;
2821 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2822 i != e; ++i, ++idx) {
2823 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002824
Chris Lattnere2962be2010-07-29 07:30:00 +00002825 // If we found a field after the region we care about, then we're done.
2826 if (FieldOffset >= EndBit) break;
2827
2828 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
2829 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
2830 Context))
2831 return false;
2832 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002833
Chris Lattnere2962be2010-07-29 07:30:00 +00002834 // If nothing in this record overlapped the area of interest, then we're
2835 // clean.
2836 return true;
2837 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002838
Chris Lattnere2962be2010-07-29 07:30:00 +00002839 return false;
2840}
2841
Chris Lattner0b362002010-07-29 18:39:32 +00002842/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
2843/// float member at the specified offset. For example, {int,{float}} has a
2844/// float at offset 4. It is conservatively correct for this routine to return
2845/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002846static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00002847 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00002848 // Base case if we find a float.
2849 if (IROffset == 0 && IRType->isFloatTy())
2850 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002851
Chris Lattner0b362002010-07-29 18:39:32 +00002852 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002853 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00002854 const llvm::StructLayout *SL = TD.getStructLayout(STy);
2855 unsigned Elt = SL->getElementContainingOffset(IROffset);
2856 IROffset -= SL->getElementOffset(Elt);
2857 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
2858 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002859
Chris Lattner0b362002010-07-29 18:39:32 +00002860 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002861 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2862 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00002863 unsigned EltSize = TD.getTypeAllocSize(EltTy);
2864 IROffset -= IROffset/EltSize*EltSize;
2865 return ContainsFloatAtOffset(EltTy, IROffset, TD);
2866 }
2867
2868 return false;
2869}
2870
Chris Lattnerf47c9442010-07-29 18:13:09 +00002871
2872/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
2873/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002874llvm::Type *X86_64ABIInfo::
2875GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00002876 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00002877 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00002878 // pass as float if the last 4 bytes is just padding. This happens for
2879 // structs that contain 3 floats.
2880 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
2881 SourceOffset*8+64, getContext()))
2882 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002883
Chris Lattner0b362002010-07-29 18:39:32 +00002884 // We want to pass as <2 x float> if the LLVM IR type contains a float at
2885 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
2886 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00002887 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
2888 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00002889 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002890
Chris Lattnerf47c9442010-07-29 18:13:09 +00002891 return llvm::Type::getDoubleTy(getVMContext());
2892}
2893
2894
Chris Lattner0d2656d2010-07-29 17:40:35 +00002895/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
2896/// an 8-byte GPR. This means that we either have a scalar or we are talking
2897/// about the high or low part of an up-to-16-byte struct. This routine picks
2898/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00002899/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
2900/// etc).
2901///
2902/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
2903/// the source type. IROffset is an offset in bytes into the LLVM IR type that
2904/// the 8-byte value references. PrefType may be null.
2905///
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002906/// SourceTy is the source-level type for the entire argument. SourceOffset is
Chris Lattner49382de2010-07-28 22:44:07 +00002907/// an offset into this that we're processing (which is always either 0 or 8).
2908///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002909llvm::Type *X86_64ABIInfo::
2910GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00002911 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00002912 // If we're dealing with an un-offset LLVM IR type, then it means that we're
2913 // returning an 8-byte unit starting with it. See if we can safely use it.
2914 if (IROffset == 0) {
2915 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00002916 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
2917 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00002918 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00002919
Chris Lattnere2962be2010-07-29 07:30:00 +00002920 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
2921 // goodness in the source type is just tail padding. This is allowed to
2922 // kick in for struct {double,int} on the int, but not on
2923 // struct{double,int,int} because we wouldn't return the second int. We
2924 // have to do this analysis on the source type because we can't depend on
2925 // unions being lowered a specific way etc.
2926 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00002927 IRType->isIntegerTy(32) ||
2928 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2929 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2930 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002931
Chris Lattnere2962be2010-07-29 07:30:00 +00002932 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2933 SourceOffset*8+64, getContext()))
2934 return IRType;
2935 }
2936 }
Chris Lattner49382de2010-07-28 22:44:07 +00002937
Chris Lattner2acc6e32011-07-18 04:24:23 +00002938 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00002939 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00002940 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00002941 if (IROffset < SL->getSizeInBytes()) {
2942 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2943 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002944
Chris Lattner0d2656d2010-07-29 17:40:35 +00002945 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2946 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002947 }
Chris Lattner49382de2010-07-28 22:44:07 +00002948 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002949
Chris Lattner2acc6e32011-07-18 04:24:23 +00002950 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002951 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00002952 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00002953 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00002954 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2955 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00002956 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002957
Chris Lattner49382de2010-07-28 22:44:07 +00002958 // Okay, we don't have any better idea of what to pass, so we pass this in an
2959 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002960 unsigned TySizeInBytes =
2961 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00002962
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002963 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002964
Chris Lattner49382de2010-07-28 22:44:07 +00002965 // It is always safe to classify this as an integer type up to i64 that
2966 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002967 return llvm::IntegerType::get(getVMContext(),
2968 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00002969}
2970
Chris Lattner66e7b682010-09-01 00:50:20 +00002971
2972/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2973/// be used as elements of a two register pair to pass or return, return a
2974/// first class aggregate to represent them. For example, if the low part of
2975/// a by-value argument should be passed as i32* and the high part as float,
2976/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002977static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00002978GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00002979 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00002980 // In order to correctly satisfy the ABI, we need to the high part to start
2981 // at offset 8. If the high and low parts we inferred are both 4-byte types
2982 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2983 // the second element at offset 8. Check for this:
2984 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2985 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002986 unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002987 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002988
Chris Lattner66e7b682010-09-01 00:50:20 +00002989 // To handle this, we have to increase the size of the low part so that the
2990 // second element will start at an 8 byte offset. We can't increase the size
2991 // of the second element because it might make us access off the end of the
2992 // struct.
2993 if (HiStart != 8) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002994 // There are usually two sorts of types the ABI generation code can produce
2995 // for the low part of a pair that aren't 8 bytes in size: float or
2996 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
2997 // NaCl).
Chris Lattner66e7b682010-09-01 00:50:20 +00002998 // Promote these to a larger type.
2999 if (Lo->isFloatTy())
3000 Lo = llvm::Type::getDoubleTy(Lo->getContext());
3001 else {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003002 assert((Lo->isIntegerTy() || Lo->isPointerTy())
3003 && "Invalid/unknown lo type");
Chris Lattner66e7b682010-09-01 00:50:20 +00003004 Lo = llvm::Type::getInt64Ty(Lo->getContext());
3005 }
3006 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003007
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003008 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003009
3010
Chris Lattner66e7b682010-09-01 00:50:20 +00003011 // Verify that the second element is at an 8-byte offset.
3012 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3013 "Invalid x86-64 argument pair!");
3014 return Result;
3015}
3016
Chris Lattner519f68c2010-07-28 23:06:14 +00003017ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00003018classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00003019 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3020 // classification algorithm.
3021 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00003022 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner519f68c2010-07-28 23:06:14 +00003023
3024 // Check some invariants.
3025 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00003026 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3027
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003028 llvm::Type *ResType = nullptr;
Chris Lattner519f68c2010-07-28 23:06:14 +00003029 switch (Lo) {
3030 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00003031 if (Hi == NoClass)
3032 return ABIArgInfo::getIgnore();
3033 // If the low part is just padding, it takes no register, leave ResType
3034 // null.
3035 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3036 "Unknown missing lo part");
3037 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00003038
3039 case SSEUp:
3040 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00003041 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00003042
3043 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3044 // hidden argument.
3045 case Memory:
3046 return getIndirectReturnResult(RetTy);
3047
3048 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3049 // available register of the sequence %rax, %rdx is used.
3050 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003051 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003052
Chris Lattnereb518b42010-07-29 21:42:50 +00003053 // If we have a sign or zero extended integer, make sure to return Extend
3054 // so that the parameter gets the right LLVM IR attributes.
3055 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3056 // Treat an enum type as its underlying type.
3057 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3058 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003059
Chris Lattnereb518b42010-07-29 21:42:50 +00003060 if (RetTy->isIntegralOrEnumerationType() &&
3061 RetTy->isPromotableIntegerType())
3062 return ABIArgInfo::getExtend();
3063 }
Chris Lattner519f68c2010-07-28 23:06:14 +00003064 break;
3065
3066 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3067 // available SSE register of the sequence %xmm0, %xmm1 is used.
3068 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003069 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00003070 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00003071
3072 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3073 // returned on the X87 stack in %st0 as 80-bit x87 number.
3074 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00003075 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00003076 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00003077
3078 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3079 // part of the value is returned in %st0 and the imaginary part in
3080 // %st1.
3081 case ComplexX87:
3082 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00003083 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00003084 llvm::Type::getX86_FP80Ty(getVMContext()),
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003085 nullptr);
Chris Lattner519f68c2010-07-28 23:06:14 +00003086 break;
3087 }
3088
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003089 llvm::Type *HighPart = nullptr;
Chris Lattner519f68c2010-07-28 23:06:14 +00003090 switch (Hi) {
3091 // Memory was handled previously and X87 should
3092 // never occur as a hi class.
3093 case Memory:
3094 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00003095 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00003096
3097 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00003098 case NoClass:
3099 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00003100
Chris Lattner3db4dde2010-09-01 00:20:33 +00003101 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003102 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00003103 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3104 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00003105 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00003106 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003107 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00003108 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3109 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00003110 break;
3111
3112 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00003113 // is passed in the next available eightbyte chunk if the last used
3114 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00003115 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003116 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00003117 case SSEUp:
3118 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00003119 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00003120 break;
3121
3122 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3123 // returned together with the previous X87 value in %st0.
3124 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003125 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00003126 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003127 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00003128 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00003129 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003130 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00003131 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3132 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00003133 }
Chris Lattner519f68c2010-07-28 23:06:14 +00003134 break;
3135 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003136
Chris Lattner3db4dde2010-09-01 00:20:33 +00003137 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00003138 // known to pass in the high eightbyte of the result. We do this by forming a
3139 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00003140 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00003141 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00003142
Chris Lattnereb518b42010-07-29 21:42:50 +00003143 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00003144}
3145
Daniel Dunbaredfac032012-03-10 01:03:58 +00003146ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman7a1b5862013-06-12 00:13:45 +00003147 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3148 bool isNamedArg)
Daniel Dunbaredfac032012-03-10 01:03:58 +00003149 const
3150{
Stephen Hines176edba2014-12-01 14:53:08 -08003151 Ty = useFirstFieldIfTransparentUnion(Ty);
3152
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003153 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00003154 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003155
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003156 // Check some invariants.
3157 // FIXME: Enforce these by construction.
3158 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003159 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3160
3161 neededInt = 0;
3162 neededSSE = 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003163 llvm::Type *ResType = nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003164 switch (Lo) {
3165 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00003166 if (Hi == NoClass)
3167 return ABIArgInfo::getIgnore();
3168 // If the low part is just padding, it takes no register, leave ResType
3169 // null.
3170 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3171 "Unknown missing lo part");
3172 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003173
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003174 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3175 // on the stack.
3176 case Memory:
3177
3178 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3179 // COMPLEX_X87, it is passed in memory.
3180 case X87:
3181 case ComplexX87:
Mark Lacey23630722013-10-06 01:33:34 +00003182 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
Eli Friedmanded137f2011-06-29 07:04:55 +00003183 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00003184 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003185
3186 case SSEUp:
3187 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00003188 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003189
3190 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3191 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3192 // and %r9 is used.
3193 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00003194 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003195
Chris Lattner49382de2010-07-28 22:44:07 +00003196 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003197 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00003198
3199 // If we have a sign or zero extended integer, make sure to return Extend
3200 // so that the parameter gets the right LLVM IR attributes.
3201 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3202 // Treat an enum type as its underlying type.
3203 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3204 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003205
Chris Lattnereb518b42010-07-29 21:42:50 +00003206 if (Ty->isIntegralOrEnumerationType() &&
3207 Ty->isPromotableIntegerType())
3208 return ABIArgInfo::getExtend();
3209 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003210
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003211 break;
3212
3213 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3214 // available SSE register is used, the registers are taken in the
3215 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00003216 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003217 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00003218 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00003219 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003220 break;
3221 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00003222 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003223
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003224 llvm::Type *HighPart = nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003225 switch (Hi) {
3226 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003227 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003228 // which is passed in memory.
3229 case Memory:
3230 case X87:
3231 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00003232 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003233
3234 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003235
Chris Lattner645406a2010-09-01 00:24:35 +00003236 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003237 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00003238 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003239 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003240
Chris Lattner645406a2010-09-01 00:24:35 +00003241 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3242 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003243 break;
3244
3245 // X87Up generally doesn't occur here (long double is passed in
3246 // memory), except in situations involving unions.
3247 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00003248 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003249 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003250
Chris Lattner645406a2010-09-01 00:24:35 +00003251 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3252 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00003253
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003254 ++neededSSE;
3255 break;
3256
3257 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3258 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003259 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003260 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00003261 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00003262 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003263 break;
3264 }
3265
Chris Lattner645406a2010-09-01 00:24:35 +00003266 // If a high part was specified, merge it together with the low part. It is
3267 // known to pass in the high eightbyte of the result. We do this by forming a
3268 // first class struct aggregate with the high and low part: {low, high}
3269 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00003270 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003271
Chris Lattnereb518b42010-07-29 21:42:50 +00003272 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003273}
3274
Chris Lattneree5dcd02010-07-29 02:31:05 +00003275void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003276
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003277 if (!getCXXABI().classifyReturnType(FI))
3278 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003279
3280 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00003281 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003282
3283 // If the return value is indirect, then the hidden argument is consuming one
3284 // integer register.
3285 if (FI.getReturnInfo().isIndirect())
3286 --freeIntRegs;
3287
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003288 // The chain argument effectively gives us another free register.
3289 if (FI.isChainCall())
3290 ++freeIntRegs;
3291
Stephen Hines176edba2014-12-01 14:53:08 -08003292 unsigned NumRequiredArgs = FI.getNumRequiredArgs();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003293 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3294 // get assigned (in left-to-right order) for passing as follows...
Stephen Hines176edba2014-12-01 14:53:08 -08003295 unsigned ArgNo = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003296 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Stephen Hines176edba2014-12-01 14:53:08 -08003297 it != ie; ++it, ++ArgNo) {
3298 bool IsNamedArg = ArgNo < NumRequiredArgs;
Eli Friedman7a1b5862013-06-12 00:13:45 +00003299
Bill Wendling99aaae82010-10-18 23:51:38 +00003300 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00003301 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
Stephen Hines176edba2014-12-01 14:53:08 -08003302 neededSSE, IsNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003303
3304 // AMD64-ABI 3.2.3p3: If there are no registers available for any
3305 // eightbyte of an argument, the whole argument is passed on the
3306 // stack. If registers have already been assigned for some
3307 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00003308 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003309 freeIntRegs -= neededInt;
3310 freeSSERegs -= neededSSE;
3311 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00003312 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003313 }
3314 }
3315}
3316
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003317static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3318 Address VAListAddr, QualType Ty) {
3319 Address overflow_arg_area_p = CGF.Builder.CreateStructGEP(
3320 VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003321 llvm::Value *overflow_arg_area =
3322 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3323
3324 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3325 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00003326 // It isn't stated explicitly in the standard, but in practice we use
3327 // alignment greater than 16 where necessary.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003328 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3329 if (Align > CharUnits::fromQuantity(8)) {
3330 overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3331 Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003332 }
3333
3334 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003335 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003336 llvm::Value *Res =
3337 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003338 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003339
3340 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3341 // l->overflow_arg_area + sizeof(type).
3342 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3343 // an 8 byte boundary.
3344
3345 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00003346 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00003347 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003348 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3349 "overflow_arg_area.next");
3350 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3351
3352 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003353 return Address(Res, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003354}
3355
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003356Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3357 QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003358 // Assume that va_list type is correct; should be pointer to LLVM type:
3359 // struct {
3360 // i32 gp_offset;
3361 // i32 fp_offset;
3362 // i8* overflow_arg_area;
3363 // i8* reg_save_area;
3364 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00003365 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003366
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003367 Ty = getContext().getCanonicalType(Ty);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003368 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
Eli Friedman7a1b5862013-06-12 00:13:45 +00003369 /*isNamedArg*/false);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003370
3371 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3372 // in the registers. If not go to step 7.
3373 if (!neededInt && !neededSSE)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003374 return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003375
3376 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3377 // general purpose registers needed to pass type and num_fp to hold
3378 // the number of floating point registers needed.
3379
3380 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3381 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3382 // l->fp_offset > 304 - num_fp * 16 go to step 7.
3383 //
3384 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3385 // register save space).
3386
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003387 llvm::Value *InRegs = nullptr;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003388 Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3389 llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003390 if (neededInt) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003391 gp_offset_p =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003392 CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(),
3393 "gp_offset_p");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003394 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00003395 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3396 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003397 }
3398
3399 if (neededSSE) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003400 fp_offset_p =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003401 CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4),
3402 "fp_offset_p");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003403 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3404 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00003405 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3406 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003407 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3408 }
3409
3410 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3411 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3412 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3413 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3414
3415 // Emit code to load the value if it was passed in registers.
3416
3417 CGF.EmitBlock(InRegBlock);
3418
3419 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3420 // an offset of l->gp_offset and/or l->fp_offset. This may require
3421 // copying to a temporary location in case the parameter is passed
3422 // in different register classes or requires an alignment greater
3423 // than 8 for general purpose registers and 16 for XMM registers.
3424 //
3425 // FIXME: This really results in shameful code when we end up needing to
3426 // collect arguments from different places; often what should result in a
3427 // simple assembling of a structure from scattered addresses has many more
3428 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00003429 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003430 llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
3431 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)),
3432 "reg_save_area");
3433
3434 Address RegAddr = Address::invalid();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003435 if (neededInt && neededSSE) {
3436 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00003437 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003438 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003439 Address Tmp = CGF.CreateMemTemp(Ty);
3440 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003441 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003442 llvm::Type *TyLo = ST->getElementType(0);
3443 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00003444 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003445 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003446 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
3447 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003448 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
3449 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003450 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
3451 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003452
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003453 // Copy the first element.
3454 llvm::Value *V =
3455 CGF.Builder.CreateDefaultAlignedLoad(
3456 CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
3457 CGF.Builder.CreateStore(V,
3458 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3459
3460 // Copy the second element.
3461 V = CGF.Builder.CreateDefaultAlignedLoad(
3462 CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
3463 CharUnits Offset = CharUnits::fromQuantity(
3464 getDataLayout().getStructLayout(ST)->getElementOffset(1));
3465 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset));
3466
3467 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003468 } else if (neededInt) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003469 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
3470 CharUnits::fromQuantity(8));
3471 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Eli Friedmaneeb00622013-06-07 23:20:55 +00003472
3473 // Copy to a temporary if necessary to ensure the appropriate alignment.
3474 std::pair<CharUnits, CharUnits> SizeAlign =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003475 getContext().getTypeInfoInChars(Ty);
Eli Friedmaneeb00622013-06-07 23:20:55 +00003476 uint64_t TySize = SizeAlign.first.getQuantity();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003477 CharUnits TyAlign = SizeAlign.second;
3478
3479 // Copy into a temporary if the type is more aligned than the
3480 // register save area.
3481 if (TyAlign.getQuantity() > 8) {
3482 Address Tmp = CGF.CreateMemTemp(Ty);
3483 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
Eli Friedmaneeb00622013-06-07 23:20:55 +00003484 RegAddr = Tmp;
3485 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003486
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003487 } else if (neededSSE == 1) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003488 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3489 CharUnits::fromQuantity(16));
3490 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003491 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003492 assert(neededSSE == 2 && "Invalid number of needed registers!");
3493 // SSE registers are spaced 16 bytes apart in the register save
3494 // area, we need to collect the two eightbytes together.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003495 // The ABI isn't explicit about this, but it seems reasonable
3496 // to assume that the slots are 16-byte aligned, since the stack is
3497 // naturally 16-byte aligned and the prologue is expected to store
3498 // all the SSE registers to the RSA.
3499 Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3500 CharUnits::fromQuantity(16));
3501 Address RegAddrHi =
3502 CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
3503 CharUnits::fromQuantity(16));
Chris Lattner8b418682012-02-07 00:39:47 +00003504 llvm::Type *DoubleTy = CGF.DoubleTy;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003505 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003506 llvm::Value *V;
3507 Address Tmp = CGF.CreateMemTemp(Ty);
3508 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3509 V = CGF.Builder.CreateLoad(
3510 CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy));
3511 CGF.Builder.CreateStore(V,
3512 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3513 V = CGF.Builder.CreateLoad(
3514 CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy));
3515 CGF.Builder.CreateStore(V,
3516 CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8)));
3517
3518 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003519 }
3520
3521 // AMD64-ABI 3.5.7p5: Step 5. Set:
3522 // l->gp_offset = l->gp_offset + num_gp * 8
3523 // l->fp_offset = l->fp_offset + num_fp * 16.
3524 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00003525 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003526 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3527 gp_offset_p);
3528 }
3529 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00003530 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003531 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3532 fp_offset_p);
3533 }
3534 CGF.EmitBranch(ContBlock);
3535
3536 // Emit code to load the value if it was passed in memory.
3537
3538 CGF.EmitBlock(InMemBlock);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003539 Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003540
3541 // Return the appropriate result.
3542
3543 CGF.EmitBlock(ContBlock);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003544 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
3545 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003546 return ResAddr;
3547}
3548
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003549Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
3550 QualType Ty) const {
3551 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3552 CGF.getContext().getTypeInfoInChars(Ty),
3553 CharUnits::fromQuantity(8),
3554 /*allowHigherAlign*/ false);
3555}
3556
Stephen Hines176edba2014-12-01 14:53:08 -08003557ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
3558 bool IsReturnType) const {
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003559
3560 if (Ty->isVoidType())
3561 return ABIArgInfo::getIgnore();
3562
3563 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3564 Ty = EnumTy->getDecl()->getIntegerType();
3565
Stephen Hines176edba2014-12-01 14:53:08 -08003566 TypeInfo Info = getContext().getTypeInfo(Ty);
3567 uint64_t Width = Info.Width;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003568 CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003569
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003570 const RecordType *RT = Ty->getAs<RecordType>();
3571 if (RT) {
3572 if (!IsReturnType) {
Mark Lacey23630722013-10-06 01:33:34 +00003573 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003574 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003575 }
3576
3577 if (RT->getDecl()->hasFlexibleArrayMember())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003578 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003579
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003580 }
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00003581
Stephen Hines176edba2014-12-01 14:53:08 -08003582 // vectorcall adds the concept of a homogenous vector aggregate, similar to
3583 // other targets.
3584 const Type *Base = nullptr;
3585 uint64_t NumElts = 0;
3586 if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) {
3587 if (FreeSSERegs >= NumElts) {
3588 FreeSSERegs -= NumElts;
3589 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3590 return ABIArgInfo::getDirect();
3591 return ABIArgInfo::getExpand();
3592 }
3593 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3594 }
3595
3596
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003597 if (Ty->isMemberPointerType()) {
3598 // If the member pointer is represented by an LLVM int or ptr, pass it
3599 // directly.
3600 llvm::Type *LLTy = CGT.ConvertType(Ty);
3601 if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3602 return ABIArgInfo::getDirect();
3603 }
3604
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003605 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00003606 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3607 // not 1, 2, 4, or 8 bytes, must be passed by reference."
Stephen Hines176edba2014-12-01 14:53:08 -08003608 if (Width > 64 || !llvm::isPowerOf2_64(Width))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003609 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003610
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003611 // Otherwise, coerce it to a small integer.
Stephen Hines176edba2014-12-01 14:53:08 -08003612 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003613 }
3614
Stephen Hines176edba2014-12-01 14:53:08 -08003615 // Bool type is always extended to the ABI, other builtin types are not
3616 // extended.
3617 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3618 if (BT && BT->getKind() == BuiltinType::Bool)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003619 return ABIArgInfo::getExtend();
3620
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003621 // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It
3622 // passes them indirectly through memory.
3623 if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) {
3624 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
3625 if (LDF == &llvm::APFloat::x87DoubleExtended)
3626 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3627 }
3628
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003629 return ABIArgInfo::getDirect();
3630}
3631
3632void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines176edba2014-12-01 14:53:08 -08003633 bool IsVectorCall =
3634 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003635
Stephen Hines176edba2014-12-01 14:53:08 -08003636 // We can use up to 4 SSE return registers with vectorcall.
3637 unsigned FreeSSERegs = IsVectorCall ? 4 : 0;
3638 if (!getCXXABI().classifyReturnType(FI))
3639 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true);
3640
3641 // We can use up to 6 SSE register parameters with vectorcall.
3642 FreeSSERegs = IsVectorCall ? 6 : 0;
Stephen Hines651f13c2014-04-23 16:59:28 -07003643 for (auto &I : FI.arguments())
Stephen Hines176edba2014-12-01 14:53:08 -08003644 I.info = classify(I.type, FreeSSERegs, false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003645}
3646
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003647Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3648 QualType Ty) const {
3649 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3650 CGF.getContext().getTypeInfoInChars(Ty),
3651 CharUnits::fromQuantity(8),
3652 /*allowHigherAlign*/ false);
Chris Lattnerf13721d2010-08-31 16:44:54 +00003653}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003654
John McCallec853ba2010-03-11 00:10:12 +00003655// PowerPC-32
John McCallec853ba2010-03-11 00:10:12 +00003656namespace {
Stephen Hines176edba2014-12-01 14:53:08 -08003657/// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
3658class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003659bool IsSoftFloatABI;
John McCallec853ba2010-03-11 00:10:12 +00003660public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003661 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
3662 : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
Stephen Hines176edba2014-12-01 14:53:08 -08003663
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003664 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3665 QualType Ty) const override;
Stephen Hines176edba2014-12-01 14:53:08 -08003666};
3667
3668class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
3669public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003670 PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
3671 : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003672
Stephen Hines651f13c2014-04-23 16:59:28 -07003673 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallec853ba2010-03-11 00:10:12 +00003674 // This is recovered from gcc output.
3675 return 1; // r1 is the dedicated stack pointer
3676 }
3677
3678 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07003679 llvm::Value *Address) const override;
John McCallec853ba2010-03-11 00:10:12 +00003680};
3681
3682}
3683
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003684// TODO: this implementation is now likely redundant with
3685// DefaultABIInfo::EmitVAArg.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003686Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
3687 QualType Ty) const {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003688 const unsigned OverflowLimit = 8;
Stephen Hines176edba2014-12-01 14:53:08 -08003689 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
3690 // TODO: Implement this. For now ignore.
3691 (void)CTy;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003692 return Address::invalid(); // FIXME?
Stephen Hines176edba2014-12-01 14:53:08 -08003693 }
3694
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003695 // struct __va_list_tag {
3696 // unsigned char gpr;
3697 // unsigned char fpr;
3698 // unsigned short reserved;
3699 // void *overflow_arg_area;
3700 // void *reg_save_area;
3701 // };
3702
Stephen Hines176edba2014-12-01 14:53:08 -08003703 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003704 bool isInt =
3705 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003706 bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
3707
3708 // All aggregates are passed indirectly? That doesn't seem consistent
3709 // with the argument-lowering code.
3710 bool isIndirect = Ty->isAggregateType();
Stephen Hines176edba2014-12-01 14:53:08 -08003711
3712 CGBuilderTy &Builder = CGF.Builder;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003713
3714 // The calling convention either uses 1-2 GPRs or 1 FPR.
3715 Address NumRegsAddr = Address::invalid();
3716 if (isInt || IsSoftFloatABI) {
3717 NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr");
3718 } else {
3719 NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr");
Stephen Hines176edba2014-12-01 14:53:08 -08003720 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003721
3722 llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
3723
3724 // "Align" the register count when TY is i64.
3725 if (isI64 || (isF64 && IsSoftFloatABI)) {
3726 NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
3727 NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
3728 }
Stephen Hines176edba2014-12-01 14:53:08 -08003729
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003730 llvm::Value *CC =
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003731 Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
Stephen Hines176edba2014-12-01 14:53:08 -08003732
3733 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
3734 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
3735 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
3736
3737 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
3738
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003739 llvm::Type *DirectTy = CGF.ConvertType(Ty);
3740 if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
Stephen Hines176edba2014-12-01 14:53:08 -08003741
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003742 // Case 1: consume registers.
3743 Address RegAddr = Address::invalid();
3744 {
3745 CGF.EmitBlock(UsingRegs);
3746
3747 Address RegSaveAreaPtr =
3748 Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8));
3749 RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
3750 CharUnits::fromQuantity(8));
3751 assert(RegAddr.getElementType() == CGF.Int8Ty);
3752
3753 // Floating-point registers start after the general-purpose registers.
3754 if (!(isInt || IsSoftFloatABI)) {
3755 RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
3756 CharUnits::fromQuantity(32));
3757 }
3758
3759 // Get the address of the saved value by scaling the number of
3760 // registers we've used by the number of
3761 CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
3762 llvm::Value *RegOffset =
3763 Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
3764 RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
3765 RegAddr.getPointer(), RegOffset),
3766 RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
3767 RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
3768
3769 // Increase the used-register count.
3770 NumRegs =
3771 Builder.CreateAdd(NumRegs,
3772 Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
3773 Builder.CreateStore(NumRegs, NumRegsAddr);
3774
3775 CGF.EmitBranch(Cont);
Stephen Hines176edba2014-12-01 14:53:08 -08003776 }
Stephen Hines176edba2014-12-01 14:53:08 -08003777
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003778 // Case 2: consume space in the overflow area.
3779 Address MemAddr = Address::invalid();
3780 {
3781 CGF.EmitBlock(UsingOverflow);
Stephen Hines176edba2014-12-01 14:53:08 -08003782
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003783 Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
3784
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003785 // Everything in the overflow area is rounded up to a size of at least 4.
3786 CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
3787
3788 CharUnits Size;
3789 if (!isIndirect) {
3790 auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003791 Size = TypeInfo.first.alignTo(OverflowAreaAlign);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003792 } else {
3793 Size = CGF.getPointerSize();
3794 }
3795
3796 Address OverflowAreaAddr =
3797 Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4));
3798 Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
3799 OverflowAreaAlign);
3800 // Round up address of argument to alignment
3801 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3802 if (Align > OverflowAreaAlign) {
3803 llvm::Value *Ptr = OverflowArea.getPointer();
3804 OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
3805 Align);
3806 }
3807
3808 MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
3809
3810 // Increase the overflow area.
3811 OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
3812 Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
3813 CGF.EmitBranch(Cont);
3814 }
Stephen Hines176edba2014-12-01 14:53:08 -08003815
3816 CGF.EmitBlock(Cont);
3817
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003818 // Merge the cases with a phi.
3819 Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
3820 "vaarg.addr");
Stephen Hines176edba2014-12-01 14:53:08 -08003821
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003822 // Load the pointer if the argument was passed indirectly.
3823 if (isIndirect) {
3824 Result = Address(Builder.CreateLoad(Result, "aggr"),
3825 getContext().getTypeAlignInChars(Ty));
Stephen Hines176edba2014-12-01 14:53:08 -08003826 }
3827
3828 return Result;
3829}
3830
John McCallec853ba2010-03-11 00:10:12 +00003831bool
3832PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3833 llvm::Value *Address) const {
3834 // This is calculated from the LLVM and GCC tables and verified
3835 // against gcc output. AFAIK all ABIs use the same encoding.
3836
3837 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00003838
Chris Lattner8b418682012-02-07 00:39:47 +00003839 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00003840 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3841 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
3842 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
3843
3844 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00003845 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00003846
3847 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00003848 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00003849
3850 // 64-76 are various 4-byte special-purpose registers:
3851 // 64: mq
3852 // 65: lr
3853 // 66: ctr
3854 // 67: ap
3855 // 68-75 cr0-7
3856 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00003857 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00003858
3859 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00003860 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00003861
3862 // 109: vrsave
3863 // 110: vscr
3864 // 111: spe_acc
3865 // 112: spefscr
3866 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00003867 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00003868
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003869 return false;
John McCallec853ba2010-03-11 00:10:12 +00003870}
3871
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003872// PowerPC-64
3873
3874namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003875/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003876class PPC64_SVR4_ABIInfo : public ABIInfo {
Stephen Hines176edba2014-12-01 14:53:08 -08003877public:
3878 enum ABIKind {
3879 ELFv1 = 0,
3880 ELFv2
3881 };
3882
3883private:
3884 static const unsigned GPRBits = 64;
3885 ABIKind Kind;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003886 bool HasQPX;
3887
3888 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
3889 // will be passed in a QPX register.
3890 bool IsQPXVectorTy(const Type *Ty) const {
3891 if (!HasQPX)
3892 return false;
3893
3894 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3895 unsigned NumElements = VT->getNumElements();
3896 if (NumElements == 1)
3897 return false;
3898
3899 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
3900 if (getContext().getTypeSize(Ty) <= 256)
3901 return true;
3902 } else if (VT->getElementType()->
3903 isSpecificBuiltinType(BuiltinType::Float)) {
3904 if (getContext().getTypeSize(Ty) <= 128)
3905 return true;
3906 }
3907 }
3908
3909 return false;
3910 }
3911
3912 bool IsQPXVectorTy(QualType Ty) const {
3913 return IsQPXVectorTy(Ty.getTypePtr());
3914 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003915
3916public:
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003917 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003918 : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {}
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003919
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00003920 bool isPromotableTypeForABI(QualType Ty) const;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003921 CharUnits getParamTypeAlignment(QualType Ty) const;
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00003922
3923 ABIArgInfo classifyReturnType(QualType RetTy) const;
3924 ABIArgInfo classifyArgumentType(QualType Ty) const;
3925
Stephen Hines176edba2014-12-01 14:53:08 -08003926 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
3927 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
3928 uint64_t Members) const override;
3929
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003930 // TODO: We can add more logic to computeInfo to improve performance.
3931 // Example: For aggregate arguments that fit in a register, we could
3932 // use getDirectInReg (as is done below for structs containing a single
3933 // floating-point value) to avoid pushing them to memory on function
3934 // entry. This would require changing the logic in PPCISelLowering
3935 // when lowering the parameters in the caller and args in the callee.
Stephen Hines651f13c2014-04-23 16:59:28 -07003936 void computeInfo(CGFunctionInfo &FI) const override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003937 if (!getCXXABI().classifyReturnType(FI))
3938 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -07003939 for (auto &I : FI.arguments()) {
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003940 // We rely on the default argument classification for the most part.
3941 // One exception: An aggregate containing a single floating-point
Bill Schmidtb1993102013-07-23 22:15:57 +00003942 // or vector item must be passed in a register if one is available.
Stephen Hines651f13c2014-04-23 16:59:28 -07003943 const Type *T = isSingleElementStruct(I.type, getContext());
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003944 if (T) {
3945 const BuiltinType *BT = T->getAs<BuiltinType>();
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003946 if (IsQPXVectorTy(T) ||
3947 (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003948 (BT && BT->isFloatingPoint())) {
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003949 QualType QT(T, 0);
Stephen Hines651f13c2014-04-23 16:59:28 -07003950 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003951 continue;
3952 }
3953 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003954 I.info = classifyArgumentType(I.type);
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003955 }
3956 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003957
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003958 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3959 QualType Ty) const override;
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003960};
3961
3962class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003963
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003964public:
Stephen Hines176edba2014-12-01 14:53:08 -08003965 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003966 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003967 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {}
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003968
Stephen Hines651f13c2014-04-23 16:59:28 -07003969 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003970 // This is recovered from gcc output.
3971 return 1; // r1 is the dedicated stack pointer
3972 }
3973
3974 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07003975 llvm::Value *Address) const override;
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003976};
3977
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003978class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3979public:
3980 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
3981
Stephen Hines651f13c2014-04-23 16:59:28 -07003982 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003983 // This is recovered from gcc output.
3984 return 1; // r1 is the dedicated stack pointer
3985 }
3986
3987 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07003988 llvm::Value *Address) const override;
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003989};
3990
3991}
3992
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00003993// Return true if the ABI requires Ty to be passed sign- or zero-
3994// extended to 64 bits.
3995bool
3996PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
3997 // Treat an enum type as its underlying type.
3998 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3999 Ty = EnumTy->getDecl()->getIntegerType();
4000
4001 // Promotable integer types are required to be promoted by the ABI.
4002 if (Ty->isPromotableIntegerType())
4003 return true;
4004
4005 // In addition to the usual promotable integer types, we also need to
4006 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
4007 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4008 switch (BT->getKind()) {
4009 case BuiltinType::Int:
4010 case BuiltinType::UInt:
4011 return true;
4012 default:
4013 break;
4014 }
4015
4016 return false;
4017}
4018
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004019/// isAlignedParamType - Determine whether a type requires 16-byte or
4020/// higher alignment in the parameter area. Always returns at least 8.
4021CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004022 // Complex types are passed just like their elements.
4023 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4024 Ty = CTy->getElementType();
4025
4026 // Only vector types of size 16 bytes need alignment (larger types are
4027 // passed via reference, smaller types are not aligned).
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004028 if (IsQPXVectorTy(Ty)) {
4029 if (getContext().getTypeSize(Ty) > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004030 return CharUnits::fromQuantity(32);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004031
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004032 return CharUnits::fromQuantity(16);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004033 } else if (Ty->isVectorType()) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004034 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004035 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004036
4037 // For single-element float/vector structs, we consider the whole type
4038 // to have the same alignment requirements as its single element.
4039 const Type *AlignAsType = nullptr;
4040 const Type *EltType = isSingleElementStruct(Ty, getContext());
4041 if (EltType) {
4042 const BuiltinType *BT = EltType->getAs<BuiltinType>();
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004043 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004044 getContext().getTypeSize(EltType) == 128) ||
4045 (BT && BT->isFloatingPoint()))
4046 AlignAsType = EltType;
4047 }
4048
Stephen Hines176edba2014-12-01 14:53:08 -08004049 // Likewise for ELFv2 homogeneous aggregates.
4050 const Type *Base = nullptr;
4051 uint64_t Members = 0;
4052 if (!AlignAsType && Kind == ELFv2 &&
4053 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
4054 AlignAsType = Base;
4055
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004056 // With special case aggregates, only vector base types need alignment.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004057 if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
4058 if (getContext().getTypeSize(AlignAsType) > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004059 return CharUnits::fromQuantity(32);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004060
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004061 return CharUnits::fromQuantity(16);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004062 } else if (AlignAsType) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004063 return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004064 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004065
4066 // Otherwise, we only need alignment for any aggregate type that
4067 // has an alignment requirement of >= 16 bytes.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004068 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
4069 if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004070 return CharUnits::fromQuantity(32);
4071 return CharUnits::fromQuantity(16);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004072 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004073
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004074 return CharUnits::fromQuantity(8);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004075}
4076
Stephen Hines176edba2014-12-01 14:53:08 -08004077/// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
4078/// aggregate. Base is set to the base element type, and Members is set
4079/// to the number of base elements.
4080bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
4081 uint64_t &Members) const {
4082 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
4083 uint64_t NElements = AT->getSize().getZExtValue();
4084 if (NElements == 0)
4085 return false;
4086 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
4087 return false;
4088 Members *= NElements;
4089 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
4090 const RecordDecl *RD = RT->getDecl();
4091 if (RD->hasFlexibleArrayMember())
4092 return false;
4093
4094 Members = 0;
4095
4096 // If this is a C++ record, check the bases first.
4097 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4098 for (const auto &I : CXXRD->bases()) {
4099 // Ignore empty records.
4100 if (isEmptyRecord(getContext(), I.getType(), true))
4101 continue;
4102
4103 uint64_t FldMembers;
4104 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
4105 return false;
4106
4107 Members += FldMembers;
4108 }
4109 }
4110
4111 for (const auto *FD : RD->fields()) {
4112 // Ignore (non-zero arrays of) empty records.
4113 QualType FT = FD->getType();
4114 while (const ConstantArrayType *AT =
4115 getContext().getAsConstantArrayType(FT)) {
4116 if (AT->getSize().getZExtValue() == 0)
4117 return false;
4118 FT = AT->getElementType();
4119 }
4120 if (isEmptyRecord(getContext(), FT, true))
4121 continue;
4122
4123 // For compatibility with GCC, ignore empty bitfields in C++ mode.
4124 if (getContext().getLangOpts().CPlusPlus &&
4125 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4126 continue;
4127
4128 uint64_t FldMembers;
4129 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
4130 return false;
4131
4132 Members = (RD->isUnion() ?
4133 std::max(Members, FldMembers) : Members + FldMembers);
4134 }
4135
4136 if (!Base)
4137 return false;
4138
4139 // Ensure there is no padding.
4140 if (getContext().getTypeSize(Base) * Members !=
4141 getContext().getTypeSize(Ty))
4142 return false;
4143 } else {
4144 Members = 1;
4145 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
4146 Members = 2;
4147 Ty = CT->getElementType();
4148 }
4149
4150 // Most ABIs only support float, double, and some vector type widths.
4151 if (!isHomogeneousAggregateBaseType(Ty))
4152 return false;
4153
4154 // The base type must be the same for all members. Types that
4155 // agree in both total size and mode (float vs. vector) are
4156 // treated as being equivalent here.
4157 const Type *TyPtr = Ty.getTypePtr();
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004158 if (!Base) {
Stephen Hines176edba2014-12-01 14:53:08 -08004159 Base = TyPtr;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004160 // If it's a non-power-of-2 vector, its size is already a power-of-2,
4161 // so make sure to widen it explicitly.
4162 if (const VectorType *VT = Base->getAs<VectorType>()) {
4163 QualType EltTy = VT->getElementType();
4164 unsigned NumElements =
4165 getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
4166 Base = getContext()
4167 .getVectorType(EltTy, NumElements, VT->getVectorKind())
4168 .getTypePtr();
4169 }
4170 }
Stephen Hines176edba2014-12-01 14:53:08 -08004171
4172 if (Base->isVectorType() != TyPtr->isVectorType() ||
4173 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
4174 return false;
4175 }
4176 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
4177}
4178
4179bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4180 // Homogeneous aggregates for ELFv2 must have base types of float,
4181 // double, long double, or 128-bit vectors.
4182 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4183 if (BT->getKind() == BuiltinType::Float ||
4184 BT->getKind() == BuiltinType::Double ||
4185 BT->getKind() == BuiltinType::LongDouble)
4186 return true;
4187 }
4188 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004189 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
Stephen Hines176edba2014-12-01 14:53:08 -08004190 return true;
4191 }
4192 return false;
4193}
4194
4195bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
4196 const Type *Base, uint64_t Members) const {
4197 // Vector types require one register, floating point types require one
4198 // or two registers depending on their size.
4199 uint32_t NumRegs =
4200 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
4201
4202 // Homogeneous Aggregates may occupy at most 8 registers.
4203 return Members * NumRegs <= 8;
4204}
4205
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004206ABIArgInfo
4207PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Stephen Hines176edba2014-12-01 14:53:08 -08004208 Ty = useFirstFieldIfTransparentUnion(Ty);
4209
Bill Schmidtc9715fc2012-11-27 02:46:43 +00004210 if (Ty->isAnyComplexType())
4211 return ABIArgInfo::getDirect();
4212
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004213 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
4214 // or via reference (larger than 16 bytes).
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004215 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004216 uint64_t Size = getContext().getTypeSize(Ty);
4217 if (Size > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004218 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004219 else if (Size < 128) {
4220 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4221 return ABIArgInfo::getDirect(CoerceTy);
4222 }
4223 }
4224
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004225 if (isAggregateTypeForABI(Ty)) {
Mark Lacey23630722013-10-06 01:33:34 +00004226 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004227 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004228
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004229 uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
4230 uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
Stephen Hines176edba2014-12-01 14:53:08 -08004231
4232 // ELFv2 homogeneous aggregates are passed as array types.
4233 const Type *Base = nullptr;
4234 uint64_t Members = 0;
4235 if (Kind == ELFv2 &&
4236 isHomogeneousAggregate(Ty, Base, Members)) {
4237 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4238 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4239 return ABIArgInfo::getDirect(CoerceTy);
4240 }
4241
4242 // If an aggregate may end up fully in registers, we do not
4243 // use the ByVal method, but pass the aggregate as array.
4244 // This is usually beneficial since we avoid forcing the
4245 // back-end to store the argument to memory.
4246 uint64_t Bits = getContext().getTypeSize(Ty);
4247 if (Bits > 0 && Bits <= 8 * GPRBits) {
4248 llvm::Type *CoerceTy;
4249
4250 // Types up to 8 bytes are passed as integer type (which will be
4251 // properly aligned in the argument save area doubleword).
4252 if (Bits <= GPRBits)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004253 CoerceTy =
4254 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Stephen Hines176edba2014-12-01 14:53:08 -08004255 // Larger types are passed as arrays, with the base type selected
4256 // according to the required alignment in the save area.
4257 else {
4258 uint64_t RegBits = ABIAlign * 8;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004259 uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
Stephen Hines176edba2014-12-01 14:53:08 -08004260 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
4261 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
4262 }
4263
4264 return ABIArgInfo::getDirect(CoerceTy);
4265 }
4266
4267 // All other aggregates are passed ByVal.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004268 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
4269 /*ByVal=*/true,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004270 /*Realign=*/TyAlign > ABIAlign);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004271 }
4272
4273 return (isPromotableTypeForABI(Ty) ?
4274 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4275}
4276
4277ABIArgInfo
4278PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4279 if (RetTy->isVoidType())
4280 return ABIArgInfo::getIgnore();
4281
Bill Schmidt9e6111a2012-12-17 04:20:17 +00004282 if (RetTy->isAnyComplexType())
4283 return ABIArgInfo::getDirect();
4284
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004285 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
4286 // or via reference (larger than 16 bytes).
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004287 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004288 uint64_t Size = getContext().getTypeSize(RetTy);
4289 if (Size > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004290 return getNaturalAlignIndirect(RetTy);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004291 else if (Size < 128) {
4292 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4293 return ABIArgInfo::getDirect(CoerceTy);
4294 }
4295 }
4296
Stephen Hines176edba2014-12-01 14:53:08 -08004297 if (isAggregateTypeForABI(RetTy)) {
4298 // ELFv2 homogeneous aggregates are returned as array types.
4299 const Type *Base = nullptr;
4300 uint64_t Members = 0;
4301 if (Kind == ELFv2 &&
4302 isHomogeneousAggregate(RetTy, Base, Members)) {
4303 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4304 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4305 return ABIArgInfo::getDirect(CoerceTy);
4306 }
4307
4308 // ELFv2 small aggregates are returned in up to two registers.
4309 uint64_t Bits = getContext().getTypeSize(RetTy);
4310 if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
4311 if (Bits == 0)
4312 return ABIArgInfo::getIgnore();
4313
4314 llvm::Type *CoerceTy;
4315 if (Bits > GPRBits) {
4316 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004317 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr);
Stephen Hines176edba2014-12-01 14:53:08 -08004318 } else
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004319 CoerceTy =
4320 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Stephen Hines176edba2014-12-01 14:53:08 -08004321 return ABIArgInfo::getDirect(CoerceTy);
4322 }
4323
4324 // All other aggregates are returned indirectly.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004325 return getNaturalAlignIndirect(RetTy);
Stephen Hines176edba2014-12-01 14:53:08 -08004326 }
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004327
4328 return (isPromotableTypeForABI(RetTy) ?
4329 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4330}
4331
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004332// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004333Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4334 QualType Ty) const {
4335 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4336 TypeInfo.second = getParamTypeAlignment(Ty);
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004337
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004338 CharUnits SlotSize = CharUnits::fromQuantity(8);
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004339
Bill Schmidt19f8e852013-01-14 17:45:36 +00004340 // If we have a complex type and the base type is smaller than 8 bytes,
4341 // the ABI calls for the real and imaginary parts to be right-adjusted
4342 // in separate doublewords. However, Clang expects us to produce a
4343 // pointer to a structure with the two parts packed tightly. So generate
4344 // loads of the real and imaginary parts relative to the va_list pointer,
4345 // and store them to a temporary structure.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004346 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4347 CharUnits EltSize = TypeInfo.first / 2;
4348 if (EltSize < SlotSize) {
4349 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
4350 SlotSize * 2, SlotSize,
4351 SlotSize, /*AllowHigher*/ true);
4352
4353 Address RealAddr = Addr;
4354 Address ImagAddr = RealAddr;
4355 if (CGF.CGM.getDataLayout().isBigEndian()) {
4356 RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
4357 SlotSize - EltSize);
4358 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
4359 2 * SlotSize - EltSize);
4360 } else {
4361 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
4362 }
4363
4364 llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
4365 RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
4366 ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
4367 llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
4368 llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
4369
4370 Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
4371 CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
4372 /*init*/ true);
4373 return Temp;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004374 }
Bill Schmidt19f8e852013-01-14 17:45:36 +00004375 }
4376
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004377 // Otherwise, just use the general rule.
4378 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
4379 TypeInfo, SlotSize, /*AllowHigher*/ true);
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004380}
4381
4382static bool
4383PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4384 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004385 // This is calculated from the LLVM and GCC tables and verified
4386 // against gcc output. AFAIK all ABIs use the same encoding.
4387
4388 CodeGen::CGBuilderTy &Builder = CGF.Builder;
4389
4390 llvm::IntegerType *i8 = CGF.Int8Ty;
4391 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4392 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4393 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4394
4395 // 0-31: r0-31, the 8-byte general-purpose registers
4396 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
4397
4398 // 32-63: fp0-31, the 8-byte floating-point registers
4399 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4400
4401 // 64-76 are various 4-byte special-purpose registers:
4402 // 64: mq
4403 // 65: lr
4404 // 66: ctr
4405 // 67: ap
4406 // 68-75 cr0-7
4407 // 76: xer
4408 AssignToArrayRange(Builder, Address, Four8, 64, 76);
4409
4410 // 77-108: v0-31, the 16-byte vector registers
4411 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4412
4413 // 109: vrsave
4414 // 110: vscr
4415 // 111: spe_acc
4416 // 112: spefscr
4417 // 113: sfp
4418 AssignToArrayRange(Builder, Address, Four8, 109, 113);
4419
4420 return false;
4421}
John McCallec853ba2010-03-11 00:10:12 +00004422
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004423bool
4424PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
4425 CodeGen::CodeGenFunction &CGF,
4426 llvm::Value *Address) const {
4427
4428 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4429}
4430
4431bool
4432PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4433 llvm::Value *Address) const {
4434
4435 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4436}
4437
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004438//===----------------------------------------------------------------------===//
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004439// AArch64 ABI Implementation
Stephen Hines651f13c2014-04-23 16:59:28 -07004440//===----------------------------------------------------------------------===//
4441
4442namespace {
4443
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004444class AArch64ABIInfo : public SwiftABIInfo {
Stephen Hines651f13c2014-04-23 16:59:28 -07004445public:
4446 enum ABIKind {
4447 AAPCS = 0,
4448 DarwinPCS
4449 };
4450
4451private:
4452 ABIKind Kind;
4453
4454public:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004455 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
4456 : SwiftABIInfo(CGT), Kind(Kind) {}
Stephen Hines651f13c2014-04-23 16:59:28 -07004457
4458private:
4459 ABIKind getABIKind() const { return Kind; }
4460 bool isDarwinPCS() const { return Kind == DarwinPCS; }
4461
4462 ABIArgInfo classifyReturnType(QualType RetTy) const;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004463 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Stephen Hines176edba2014-12-01 14:53:08 -08004464 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4465 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4466 uint64_t Members) const override;
4467
Stephen Hines651f13c2014-04-23 16:59:28 -07004468 bool isIllegalVectorType(QualType Ty) const;
4469
Stephen Hines176edba2014-12-01 14:53:08 -08004470 void computeInfo(CGFunctionInfo &FI) const override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004471 if (!getCXXABI().classifyReturnType(FI))
4472 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004473
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004474 for (auto &it : FI.arguments())
4475 it.info = classifyArgumentType(it.type);
Stephen Hines651f13c2014-04-23 16:59:28 -07004476 }
4477
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004478 Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4479 CodeGenFunction &CGF) const;
Stephen Hines651f13c2014-04-23 16:59:28 -07004480
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004481 Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
4482 CodeGenFunction &CGF) const;
Stephen Hines651f13c2014-04-23 16:59:28 -07004483
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004484 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4485 QualType Ty) const override {
Stephen Hines651f13c2014-04-23 16:59:28 -07004486 return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
4487 : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
4488 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004489
4490 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
4491 ArrayRef<llvm::Type*> scalars,
4492 bool asReturnValue) const override {
4493 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4494 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004495};
4496
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004497class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
Stephen Hines651f13c2014-04-23 16:59:28 -07004498public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004499 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
4500 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
Stephen Hines651f13c2014-04-23 16:59:28 -07004501
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004502 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
Stephen Hines651f13c2014-04-23 16:59:28 -07004503 return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue";
4504 }
4505
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004506 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4507 return 31;
4508 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004509
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004510 bool doesReturnSlotInterfereWithArgs() const override { return false; }
Stephen Hines651f13c2014-04-23 16:59:28 -07004511};
4512}
4513
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004514ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
Stephen Hines176edba2014-12-01 14:53:08 -08004515 Ty = useFirstFieldIfTransparentUnion(Ty);
4516
Stephen Hines651f13c2014-04-23 16:59:28 -07004517 // Handle illegal vector types here.
4518 if (isIllegalVectorType(Ty)) {
4519 uint64_t Size = getContext().getTypeSize(Ty);
Tim Murray9212d4f2014-08-15 16:00:15 -07004520 // Android promotes <2 x i8> to i16, not i32
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004521 if (isAndroid() && (Size <= 16)) {
Tim Murray9212d4f2014-08-15 16:00:15 -07004522 llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
Tim Murray9212d4f2014-08-15 16:00:15 -07004523 return ABIArgInfo::getDirect(ResType);
4524 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004525 if (Size <= 32) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004526 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
Stephen Hines651f13c2014-04-23 16:59:28 -07004527 return ABIArgInfo::getDirect(ResType);
4528 }
4529 if (Size == 64) {
4530 llvm::Type *ResType =
4531 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
Stephen Hines651f13c2014-04-23 16:59:28 -07004532 return ABIArgInfo::getDirect(ResType);
4533 }
4534 if (Size == 128) {
4535 llvm::Type *ResType =
4536 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
Stephen Hines651f13c2014-04-23 16:59:28 -07004537 return ABIArgInfo::getDirect(ResType);
4538 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004539 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07004540 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004541
4542 if (!isAggregateTypeForABI(Ty)) {
4543 // Treat an enum type as its underlying type.
4544 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4545 Ty = EnumTy->getDecl()->getIntegerType();
4546
Stephen Hines651f13c2014-04-23 16:59:28 -07004547 return (Ty->isPromotableIntegerType() && isDarwinPCS()
4548 ? ABIArgInfo::getExtend()
4549 : ABIArgInfo::getDirect());
4550 }
4551
4552 // Structures with either a non-trivial destructor or a non-trivial
4553 // copy constructor are always indirect.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004554 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004555 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
4556 CGCXXABI::RAA_DirectInMemory);
Stephen Hines651f13c2014-04-23 16:59:28 -07004557 }
4558
4559 // Empty records are always ignored on Darwin, but actually passed in C++ mode
4560 // elsewhere for GNU compatibility.
4561 if (isEmptyRecord(getContext(), Ty, true)) {
4562 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
4563 return ABIArgInfo::getIgnore();
4564
Stephen Hines651f13c2014-04-23 16:59:28 -07004565 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4566 }
4567
4568 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004569 const Type *Base = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004570 uint64_t Members = 0;
Stephen Hines176edba2014-12-01 14:53:08 -08004571 if (isHomogeneousAggregate(Ty, Base, Members)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004572 return ABIArgInfo::getDirect(
4573 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
Stephen Hines651f13c2014-04-23 16:59:28 -07004574 }
4575
4576 // Aggregates <= 16 bytes are passed directly in registers or on the stack.
4577 uint64_t Size = getContext().getTypeSize(Ty);
4578 if (Size <= 128) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004579 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
4580 // same size and alignment.
4581 if (getTarget().isRenderScriptTarget()) {
Matt Wala1d151512015-08-10 15:58:40 -07004582 return coerceToIntArray(Ty, getContext(), getVMContext());
4583 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004584 unsigned Alignment = getContext().getTypeAlign(Ty);
Stephen Hines651f13c2014-04-23 16:59:28 -07004585 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004586
Stephen Hines651f13c2014-04-23 16:59:28 -07004587 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4588 // For aggregates with 16-byte alignment, we use i128.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004589 if (Alignment < 128 && Size == 128) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004590 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4591 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4592 }
4593 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4594 }
4595
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004596 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07004597}
4598
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004599ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07004600 if (RetTy->isVoidType())
4601 return ABIArgInfo::getIgnore();
4602
4603 // Large vector types should be returned via memory.
4604 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004605 return getNaturalAlignIndirect(RetTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004606
4607 if (!isAggregateTypeForABI(RetTy)) {
4608 // Treat an enum type as its underlying type.
4609 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4610 RetTy = EnumTy->getDecl()->getIntegerType();
4611
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004612 return (RetTy->isPromotableIntegerType() && isDarwinPCS()
4613 ? ABIArgInfo::getExtend()
4614 : ABIArgInfo::getDirect());
Stephen Hines651f13c2014-04-23 16:59:28 -07004615 }
4616
Stephen Hines651f13c2014-04-23 16:59:28 -07004617 if (isEmptyRecord(getContext(), RetTy, true))
4618 return ABIArgInfo::getIgnore();
4619
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004620 const Type *Base = nullptr;
Stephen Hines176edba2014-12-01 14:53:08 -08004621 uint64_t Members = 0;
4622 if (isHomogeneousAggregate(RetTy, Base, Members))
Stephen Hines651f13c2014-04-23 16:59:28 -07004623 // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
4624 return ABIArgInfo::getDirect();
4625
4626 // Aggregates <= 16 bytes are returned directly in registers or on the stack.
4627 uint64_t Size = getContext().getTypeSize(RetTy);
4628 if (Size <= 128) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004629 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
4630 // same size and alignment.
4631 if (getTarget().isRenderScriptTarget()) {
Matt Wala1d151512015-08-10 15:58:40 -07004632 return coerceToIntArray(RetTy, getContext(), getVMContext());
4633 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07004634 unsigned Alignment = getContext().getTypeAlign(RetTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004635 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07004636
4637 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4638 // For aggregates with 16-byte alignment, we use i128.
4639 if (Alignment < 128 && Size == 128) {
4640 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4641 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4642 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004643 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4644 }
4645
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004646 return getNaturalAlignIndirect(RetTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004647}
4648
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004649/// isIllegalVectorType - check whether the vector type is legal for AArch64.
4650bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07004651 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4652 // Check whether VT is legal.
4653 unsigned NumElements = VT->getNumElements();
4654 uint64_t Size = getContext().getTypeSize(VT);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004655 // NumElements should be power of 2.
4656 if (!llvm::isPowerOf2_32(NumElements))
Stephen Hines651f13c2014-04-23 16:59:28 -07004657 return true;
4658 return Size != 64 && (Size != 128 || NumElements == 1);
4659 }
4660 return false;
4661}
4662
Stephen Hines176edba2014-12-01 14:53:08 -08004663bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4664 // Homogeneous aggregates for AAPCS64 must have base types of a floating
4665 // point type or a short-vector type. This is the same as the 32-bit ABI,
4666 // but with the difference that any floating-point type is allowed,
4667 // including __fp16.
4668 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4669 if (BT->isFloatingPoint())
4670 return true;
4671 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
4672 unsigned VecSize = getContext().getTypeSize(VT);
4673 if (VecSize == 64 || VecSize == 128)
4674 return true;
4675 }
4676 return false;
4677}
4678
4679bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
4680 uint64_t Members) const {
4681 return Members <= 4;
4682}
4683
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004684Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004685 QualType Ty,
4686 CodeGenFunction &CGF) const {
4687 ABIArgInfo AI = classifyArgumentType(Ty);
Stephen Hines176edba2014-12-01 14:53:08 -08004688 bool IsIndirect = AI.isIndirect();
4689
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004690 llvm::Type *BaseTy = CGF.ConvertType(Ty);
4691 if (IsIndirect)
4692 BaseTy = llvm::PointerType::getUnqual(BaseTy);
4693 else if (AI.getCoerceToType())
4694 BaseTy = AI.getCoerceToType();
4695
4696 unsigned NumRegs = 1;
4697 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
4698 BaseTy = ArrTy->getElementType();
4699 NumRegs = ArrTy->getNumElements();
4700 }
4701 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
4702
Stephen Hines651f13c2014-04-23 16:59:28 -07004703 // The AArch64 va_list type and handling is specified in the Procedure Call
4704 // Standard, section B.4:
4705 //
4706 // struct {
4707 // void *__stack;
4708 // void *__gr_top;
4709 // void *__vr_top;
4710 // int __gr_offs;
4711 // int __vr_offs;
4712 // };
4713
4714 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
4715 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4716 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
4717 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
Stephen Hines651f13c2014-04-23 16:59:28 -07004718
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004719 auto TyInfo = getContext().getTypeInfoInChars(Ty);
4720 CharUnits TyAlign = TyInfo.second;
4721
4722 Address reg_offs_p = Address::invalid();
4723 llvm::Value *reg_offs = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004724 int reg_top_index;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004725 CharUnits reg_top_offset;
4726 int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004727 if (!IsFPR) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004728 // 3 is the field number of __gr_offs
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004729 reg_offs_p =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004730 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
4731 "gr_offs_p");
Stephen Hines651f13c2014-04-23 16:59:28 -07004732 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
4733 reg_top_index = 1; // field number for __gr_top
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004734 reg_top_offset = CharUnits::fromQuantity(8);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004735 RegSize = llvm::alignTo(RegSize, 8);
Stephen Hines651f13c2014-04-23 16:59:28 -07004736 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -07004737 // 4 is the field number of __vr_offs.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004738 reg_offs_p =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004739 CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28),
4740 "vr_offs_p");
Stephen Hines651f13c2014-04-23 16:59:28 -07004741 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
4742 reg_top_index = 2; // field number for __vr_top
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004743 reg_top_offset = CharUnits::fromQuantity(16);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004744 RegSize = 16 * NumRegs;
Stephen Hines651f13c2014-04-23 16:59:28 -07004745 }
4746
4747 //=======================================
4748 // Find out where argument was passed
4749 //=======================================
4750
4751 // If reg_offs >= 0 we're already using the stack for this type of
4752 // argument. We don't want to keep updating reg_offs (in case it overflows,
4753 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
4754 // whatever they get).
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004755 llvm::Value *UsingStack = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004756 UsingStack = CGF.Builder.CreateICmpSGE(
4757 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
4758
4759 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
4760
4761 // Otherwise, at least some kind of argument could go in these registers, the
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004762 // question is whether this particular type is too big.
Stephen Hines651f13c2014-04-23 16:59:28 -07004763 CGF.EmitBlock(MaybeRegBlock);
4764
4765 // Integer arguments may need to correct register alignment (for example a
4766 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
4767 // align __gr_offs to calculate the potential address.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004768 if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
4769 int Align = TyAlign.getQuantity();
Stephen Hines651f13c2014-04-23 16:59:28 -07004770
4771 reg_offs = CGF.Builder.CreateAdd(
4772 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
4773 "align_regoffs");
4774 reg_offs = CGF.Builder.CreateAnd(
4775 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
4776 "aligned_regoffs");
4777 }
4778
4779 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004780 // The fact that this is done unconditionally reflects the fact that
4781 // allocating an argument to the stack also uses up all the remaining
4782 // registers of the appropriate kind.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004783 llvm::Value *NewOffset = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004784 NewOffset = CGF.Builder.CreateAdd(
4785 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
4786 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
4787
4788 // Now we're in a position to decide whether this argument really was in
4789 // registers or not.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004790 llvm::Value *InRegs = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004791 InRegs = CGF.Builder.CreateICmpSLE(
4792 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
4793
4794 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
4795
4796 //=======================================
4797 // Argument was in registers
4798 //=======================================
4799
4800 // Now we emit the code for if the argument was originally passed in
4801 // registers. First start the appropriate block:
4802 CGF.EmitBlock(InRegBlock);
4803
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004804 llvm::Value *reg_top = nullptr;
4805 Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index,
4806 reg_top_offset, "reg_top_p");
Stephen Hines651f13c2014-04-23 16:59:28 -07004807 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004808 Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
4809 CharUnits::fromQuantity(IsFPR ? 16 : 8));
4810 Address RegAddr = Address::invalid();
4811 llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
Stephen Hines651f13c2014-04-23 16:59:28 -07004812
4813 if (IsIndirect) {
4814 // If it's been passed indirectly (actually a struct), whatever we find from
4815 // stored registers or on the stack will actually be a struct **.
4816 MemTy = llvm::PointerType::getUnqual(MemTy);
4817 }
4818
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004819 const Type *Base = nullptr;
Stephen Hines176edba2014-12-01 14:53:08 -08004820 uint64_t NumMembers = 0;
4821 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004822 if (IsHFA && NumMembers > 1) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004823 // Homogeneous aggregates passed in registers will have their elements split
4824 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4825 // qN+1, ...). We reload and store into a temporary local variable
4826 // contiguously.
4827 assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004828 auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
Stephen Hines651f13c2014-04-23 16:59:28 -07004829 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4830 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004831 Address Tmp = CGF.CreateTempAlloca(HFATy,
4832 std::max(TyAlign, BaseTyInfo.second));
Stephen Hines651f13c2014-04-23 16:59:28 -07004833
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004834 // On big-endian platforms, the value will be right-aligned in its slot.
4835 int Offset = 0;
4836 if (CGF.CGM.getDataLayout().isBigEndian() &&
4837 BaseTyInfo.first.getQuantity() < 16)
4838 Offset = 16 - BaseTyInfo.first.getQuantity();
4839
Stephen Hines651f13c2014-04-23 16:59:28 -07004840 for (unsigned i = 0; i < NumMembers; ++i) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004841 CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
4842 Address LoadAddr =
4843 CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
4844 LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
4845
4846 Address StoreAddr =
4847 CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first);
Stephen Hines651f13c2014-04-23 16:59:28 -07004848
4849 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4850 CGF.Builder.CreateStore(Elem, StoreAddr);
4851 }
4852
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004853 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004854 } else {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004855 // Otherwise the object is contiguous in memory.
4856
4857 // It might be right-aligned in its slot.
4858 CharUnits SlotSize = BaseAddr.getAlignment();
4859 if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004860 (IsHFA || !isAggregateTypeForABI(Ty)) &&
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004861 TyInfo.first < SlotSize) {
4862 CharUnits Offset = SlotSize - TyInfo.first;
4863 BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
Stephen Hines651f13c2014-04-23 16:59:28 -07004864 }
4865
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004866 RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004867 }
4868
4869 CGF.EmitBranch(ContBlock);
4870
4871 //=======================================
4872 // Argument was on the stack
4873 //=======================================
4874 CGF.EmitBlock(OnStackBlock);
4875
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004876 Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0,
4877 CharUnits::Zero(), "stack_p");
4878 llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
Stephen Hines651f13c2014-04-23 16:59:28 -07004879
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004880 // Again, stack arguments may need realignment. In this case both integer and
Stephen Hines651f13c2014-04-23 16:59:28 -07004881 // floating-point ones might be affected.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004882 if (!IsIndirect && TyAlign.getQuantity() > 8) {
4883 int Align = TyAlign.getQuantity();
Stephen Hines651f13c2014-04-23 16:59:28 -07004884
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004885 OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
Stephen Hines651f13c2014-04-23 16:59:28 -07004886
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004887 OnStackPtr = CGF.Builder.CreateAdd(
4888 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
Stephen Hines651f13c2014-04-23 16:59:28 -07004889 "align_stack");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004890 OnStackPtr = CGF.Builder.CreateAnd(
4891 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
Stephen Hines651f13c2014-04-23 16:59:28 -07004892 "align_stack");
4893
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004894 OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004895 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004896 Address OnStackAddr(OnStackPtr,
4897 std::max(CharUnits::fromQuantity(8), TyAlign));
Stephen Hines651f13c2014-04-23 16:59:28 -07004898
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004899 // All stack slots are multiples of 8 bytes.
4900 CharUnits StackSlotSize = CharUnits::fromQuantity(8);
4901 CharUnits StackSize;
Stephen Hines651f13c2014-04-23 16:59:28 -07004902 if (IsIndirect)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004903 StackSize = StackSlotSize;
Stephen Hines651f13c2014-04-23 16:59:28 -07004904 else
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004905 StackSize = TyInfo.first.alignTo(StackSlotSize);
Stephen Hines651f13c2014-04-23 16:59:28 -07004906
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004907 llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
Stephen Hines651f13c2014-04-23 16:59:28 -07004908 llvm::Value *NewStack =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004909 CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
Stephen Hines651f13c2014-04-23 16:59:28 -07004910
4911 // Write the new value of __stack for the next call to va_arg
4912 CGF.Builder.CreateStore(NewStack, stack_p);
4913
4914 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004915 TyInfo.first < StackSlotSize) {
4916 CharUnits Offset = StackSlotSize - TyInfo.first;
4917 OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
Stephen Hines651f13c2014-04-23 16:59:28 -07004918 }
4919
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004920 OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004921
4922 CGF.EmitBranch(ContBlock);
4923
4924 //=======================================
4925 // Tidy up
4926 //=======================================
4927 CGF.EmitBlock(ContBlock);
4928
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004929 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
4930 OnStackAddr, OnStackBlock, "vaargs.addr");
Stephen Hines651f13c2014-04-23 16:59:28 -07004931
4932 if (IsIndirect)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004933 return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
4934 TyInfo.second);
Stephen Hines651f13c2014-04-23 16:59:28 -07004935
4936 return ResAddr;
4937}
4938
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004939Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4940 CodeGenFunction &CGF) const {
4941 // The backend's lowering doesn't support va_arg for aggregates or
4942 // illegal vector types. Lower VAArg here for these cases and use
4943 // the LLVM va_arg instruction for everything else.
Stephen Hines651f13c2014-04-23 16:59:28 -07004944 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004945 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Stephen Hines651f13c2014-04-23 16:59:28 -07004946
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004947 CharUnits SlotSize = CharUnits::fromQuantity(8);
Stephen Hines651f13c2014-04-23 16:59:28 -07004948
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004949 // Empty records are ignored for parameter passing purposes.
Stephen Hines651f13c2014-04-23 16:59:28 -07004950 if (isEmptyRecord(getContext(), Ty, true)) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004951 Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
4952 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
4953 return Addr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004954 }
4955
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004956 // The size of the actual thing passed, which might end up just
4957 // being a pointer for indirect types.
4958 auto TyInfo = getContext().getTypeInfoInChars(Ty);
4959
4960 // Arguments bigger than 16 bytes which aren't homogeneous
4961 // aggregates should be passed indirectly.
4962 bool IsIndirect = false;
4963 if (TyInfo.first.getQuantity() > 16) {
4964 const Type *Base = nullptr;
4965 uint64_t Members = 0;
4966 IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
Stephen Hines651f13c2014-04-23 16:59:28 -07004967 }
4968
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004969 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4970 TyInfo, SlotSize, /*AllowHigherAlign*/ true);
Stephen Hines651f13c2014-04-23 16:59:28 -07004971}
4972
4973//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004974// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004975//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004976
4977namespace {
4978
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004979class ARMABIInfo : public SwiftABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00004980public:
4981 enum ABIKind {
4982 APCS = 0,
4983 AAPCS = 1,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004984 AAPCS_VFP = 2,
4985 AAPCS16_VFP = 3,
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00004986 };
4987
4988private:
4989 ABIKind Kind;
4990
4991public:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004992 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
4993 : SwiftABIInfo(CGT), Kind(_Kind) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004994 setCCs();
John McCallbd7370a2013-02-28 19:01:20 +00004995 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00004996
John McCall49e34be2011-08-30 01:42:09 +00004997 bool isEABI() const {
Stephen Hines651f13c2014-04-23 16:59:28 -07004998 switch (getTarget().getTriple().getEnvironment()) {
4999 case llvm::Triple::Android:
5000 case llvm::Triple::EABI:
5001 case llvm::Triple::EABIHF:
5002 case llvm::Triple::GNUEABI:
5003 case llvm::Triple::GNUEABIHF:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005004 case llvm::Triple::MuslEABI:
5005 case llvm::Triple::MuslEABIHF:
Stephen Hines651f13c2014-04-23 16:59:28 -07005006 return true;
5007 default:
5008 return false;
5009 }
5010 }
5011
5012 bool isEABIHF() const {
5013 switch (getTarget().getTriple().getEnvironment()) {
5014 case llvm::Triple::EABIHF:
5015 case llvm::Triple::GNUEABIHF:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005016 case llvm::Triple::MuslEABIHF:
Stephen Hines651f13c2014-04-23 16:59:28 -07005017 return true;
5018 default:
5019 return false;
5020 }
John McCall49e34be2011-08-30 01:42:09 +00005021 }
5022
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00005023 ABIKind getABIKind() const { return Kind; }
5024
Tim Northover64eac852013-10-01 14:34:25 +00005025private:
Stephen Hines651f13c2014-04-23 16:59:28 -07005026 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005027 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
Manman Ren97f81572012-10-16 19:18:39 +00005028 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005029
Stephen Hines176edba2014-12-01 14:53:08 -08005030 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5031 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5032 uint64_t Members) const override;
5033
Stephen Hines651f13c2014-04-23 16:59:28 -07005034 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005035
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005036 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5037 QualType Ty) const override;
John McCallbd7370a2013-02-28 19:01:20 +00005038
5039 llvm::CallingConv::ID getLLVMDefaultCC() const;
5040 llvm::CallingConv::ID getABIDefaultCC() const;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005041 void setCCs();
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005042
5043 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
5044 ArrayRef<llvm::Type*> scalars,
5045 bool asReturnValue) const override {
5046 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5047 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005048};
5049
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005050class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
5051public:
Chris Lattnerea044322010-07-29 02:01:43 +00005052 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5053 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00005054
John McCall49e34be2011-08-30 01:42:09 +00005055 const ARMABIInfo &getABIInfo() const {
5056 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
5057 }
5058
Stephen Hines651f13c2014-04-23 16:59:28 -07005059 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCall6374c332010-03-06 00:35:14 +00005060 return 13;
5061 }
Roman Divacky09345d12011-05-18 19:36:54 +00005062
Stephen Hines651f13c2014-04-23 16:59:28 -07005063 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
John McCallf85e1932011-06-15 23:02:42 +00005064 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
5065 }
5066
Roman Divacky09345d12011-05-18 19:36:54 +00005067 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07005068 llvm::Value *Address) const override {
Chris Lattner8b418682012-02-07 00:39:47 +00005069 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00005070
5071 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00005072 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00005073 return false;
5074 }
John McCall49e34be2011-08-30 01:42:09 +00005075
Stephen Hines651f13c2014-04-23 16:59:28 -07005076 unsigned getSizeOfUnwindException() const override {
John McCall49e34be2011-08-30 01:42:09 +00005077 if (getABIInfo().isEABI()) return 88;
5078 return TargetCodeGenInfo::getSizeOfUnwindException();
5079 }
Tim Northover64eac852013-10-01 14:34:25 +00005080
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005081 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07005082 CodeGen::CodeGenModule &CGM) const override {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005083 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Tim Northover64eac852013-10-01 14:34:25 +00005084 if (!FD)
5085 return;
5086
5087 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
5088 if (!Attr)
5089 return;
5090
5091 const char *Kind;
5092 switch (Attr->getInterrupt()) {
5093 case ARMInterruptAttr::Generic: Kind = ""; break;
5094 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
5095 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
5096 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
5097 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
5098 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
5099 }
5100
5101 llvm::Function *Fn = cast<llvm::Function>(GV);
5102
5103 Fn->addFnAttr("interrupt", Kind);
5104
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005105 ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
5106 if (ABI == ARMABIInfo::APCS)
Tim Northover64eac852013-10-01 14:34:25 +00005107 return;
5108
5109 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
5110 // however this is not necessarily true on taking any interrupt. Instruct
5111 // the backend to perform a realignment as part of the function prologue.
5112 llvm::AttrBuilder B;
5113 B.addStackAlignmentAttr(8);
5114 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
5115 llvm::AttributeSet::get(CGM.getLLVMContext(),
5116 llvm::AttributeSet::FunctionIndex,
5117 B));
5118 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005119};
5120
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005121class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005122public:
5123 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5124 : ARMTargetCodeGenInfo(CGT, K) {}
5125
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005126 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005127 CodeGen::CodeGenModule &CGM) const override;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005128
5129 void getDependentLibraryOption(llvm::StringRef Lib,
5130 llvm::SmallString<24> &Opt) const override {
5131 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5132 }
5133
5134 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5135 llvm::SmallString<32> &Opt) const override {
5136 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5137 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005138};
5139
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005140void WindowsARMTargetCodeGenInfo::setTargetAttributes(
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005141 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005142 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005143 addStackProbeSizeTargetAttribute(D, GV, CGM);
5144}
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005145}
5146
Chris Lattneree5dcd02010-07-29 02:31:05 +00005147void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005148 if (!getCXXABI().classifyReturnType(FI))
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005149 FI.getReturnInfo() =
5150 classifyReturnType(FI.getReturnType(), FI.isVariadic());
Stephen Hines651f13c2014-04-23 16:59:28 -07005151
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005152 for (auto &I : FI.arguments())
5153 I.info = classifyArgumentType(I.type, FI.isVariadic());
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00005154
Anton Korobeynikov414d8962011-04-14 20:06:49 +00005155 // Always honor user-specified calling convention.
5156 if (FI.getCallingConvention() != llvm::CallingConv::C)
5157 return;
5158
John McCallbd7370a2013-02-28 19:01:20 +00005159 llvm::CallingConv::ID cc = getRuntimeCC();
5160 if (cc != llvm::CallingConv::C)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005161 FI.setEffectiveCallingConvention(cc);
John McCallbd7370a2013-02-28 19:01:20 +00005162}
Rafael Espindola25117ab2010-06-16 16:13:39 +00005163
John McCallbd7370a2013-02-28 19:01:20 +00005164/// Return the default calling convention that LLVM will use.
5165llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
5166 // The default calling convention that LLVM will infer.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005167 if (isEABIHF() || getTarget().getTriple().isWatchABI())
John McCallbd7370a2013-02-28 19:01:20 +00005168 return llvm::CallingConv::ARM_AAPCS_VFP;
5169 else if (isEABI())
5170 return llvm::CallingConv::ARM_AAPCS;
5171 else
5172 return llvm::CallingConv::ARM_APCS;
5173}
5174
5175/// Return the calling convention that our ABI would like us to use
5176/// as the C calling convention.
5177llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00005178 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00005179 case APCS: return llvm::CallingConv::ARM_APCS;
5180 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
5181 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005182 case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00005183 }
John McCallbd7370a2013-02-28 19:01:20 +00005184 llvm_unreachable("bad ABI kind");
5185}
5186
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005187void ARMABIInfo::setCCs() {
John McCallbd7370a2013-02-28 19:01:20 +00005188 assert(getRuntimeCC() == llvm::CallingConv::C);
5189
5190 // Don't muddy up the IR with a ton of explicit annotations if
5191 // they'd just match what LLVM will infer from the triple.
5192 llvm::CallingConv::ID abiCC = getABIDefaultCC();
5193 if (abiCC != getLLVMDefaultCC())
5194 RuntimeCC = abiCC;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005195
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005196 // AAPCS apparently requires runtime support functions to be soft-float, but
5197 // that's almost certainly for historic reasons (Thumb1 not supporting VFP
5198 // most likely). It's more convenient for AAPCS16_VFP to be hard-float.
5199 switch (getABIKind()) {
5200 case APCS:
5201 case AAPCS16_VFP:
5202 if (abiCC != getLLVMDefaultCC())
5203 BuiltinCC = abiCC;
5204 break;
5205 case AAPCS:
5206 case AAPCS_VFP:
5207 BuiltinCC = llvm::CallingConv::ARM_AAPCS;
5208 break;
5209 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005210}
5211
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005212ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
5213 bool isVariadic) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00005214 // 6.1.2.1 The following argument types are VFP CPRCs:
5215 // A single-precision floating-point type (including promoted
5216 // half-precision types); A double-precision floating-point type;
5217 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
5218 // with a Base Type of a single- or double-precision floating-point type,
5219 // 64-bit containerized vectors or 128-bit containerized vectors with one
5220 // to four Elements.
Stephen Hines176edba2014-12-01 14:53:08 -08005221 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
5222
5223 Ty = useFirstFieldIfTransparentUnion(Ty);
Manman Renb3fa55f2012-10-30 23:21:41 +00005224
Manman Ren97f81572012-10-16 19:18:39 +00005225 // Handle illegal vector types here.
5226 if (isIllegalVectorType(Ty)) {
5227 uint64_t Size = getContext().getTypeSize(Ty);
5228 if (Size <= 32) {
5229 llvm::Type *ResType =
5230 llvm::Type::getInt32Ty(getVMContext());
5231 return ABIArgInfo::getDirect(ResType);
5232 }
5233 if (Size == 64) {
5234 llvm::Type *ResType = llvm::VectorType::get(
5235 llvm::Type::getInt32Ty(getVMContext()), 2);
5236 return ABIArgInfo::getDirect(ResType);
5237 }
5238 if (Size == 128) {
5239 llvm::Type *ResType = llvm::VectorType::get(
5240 llvm::Type::getInt32Ty(getVMContext()), 4);
5241 return ABIArgInfo::getDirect(ResType);
5242 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005243 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5244 }
5245
5246 // __fp16 gets passed as if it were an int or float, but with the top 16 bits
5247 // unspecified. This is not done for OpenCL as it handles the half type
5248 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar1567b302016-03-18 16:58:36 +00005249 if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005250 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5251 llvm::Type::getFloatTy(getVMContext()) :
5252 llvm::Type::getInt32Ty(getVMContext());
5253 return ABIArgInfo::getDirect(ResType);
Manman Ren97f81572012-10-16 19:18:39 +00005254 }
5255
John McCalld608cdb2010-08-22 10:59:02 +00005256 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005257 // Treat an enum type as its underlying type.
Stephen Hines651f13c2014-04-23 16:59:28 -07005258 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005259 Ty = EnumTy->getDecl()->getIntegerType();
Stephen Hines651f13c2014-04-23 16:59:28 -07005260 }
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005261
Stephen Hines176edba2014-12-01 14:53:08 -08005262 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5263 : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005264 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00005265
Stephen Hines651f13c2014-04-23 16:59:28 -07005266 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005267 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Stephen Hines651f13c2014-04-23 16:59:28 -07005268 }
Tim Northoverf5c3a252013-06-21 22:49:34 +00005269
Daniel Dunbar42025572009-09-14 21:54:03 +00005270 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00005271 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00005272 return ABIArgInfo::getIgnore();
5273
Stephen Hines176edba2014-12-01 14:53:08 -08005274 if (IsEffectivelyAAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00005275 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
5276 // into VFP registers.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005277 const Type *Base = nullptr;
Manman Renb3fa55f2012-10-30 23:21:41 +00005278 uint64_t Members = 0;
Stephen Hines176edba2014-12-01 14:53:08 -08005279 if (isHomogeneousAggregate(Ty, Base, Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00005280 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00005281 // Base can be a floating-point or a vector.
Stephen Hines176edba2014-12-01 14:53:08 -08005282 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00005283 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005284 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5285 // WatchOS does have homogeneous aggregates. Note that we intentionally use
5286 // this convention even for a variadic function: the backend will use GPRs
5287 // if needed.
5288 const Type *Base = nullptr;
5289 uint64_t Members = 0;
5290 if (isHomogeneousAggregate(Ty, Base, Members)) {
5291 assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
5292 llvm::Type *Ty =
5293 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
5294 return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5295 }
5296 }
5297
5298 if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5299 getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
5300 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
5301 // bigger than 128-bits, they get placed in space allocated by the caller,
5302 // and a pointer is passed.
5303 return ABIArgInfo::getIndirect(
5304 CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
Bob Wilson194f06a2011-08-03 05:58:22 +00005305 }
5306
Manman Ren634b3d22012-08-13 21:23:55 +00005307 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00005308 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
5309 // most 8-byte. We realign the indirect argument if type alignment is bigger
5310 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00005311 uint64_t ABIAlign = 4;
5312 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
5313 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07005314 getABIKind() == ARMABIInfo::AAPCS)
Manman Renfd1ba912012-11-05 22:42:46 +00005315 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07005316
Manman Ren885ad692012-11-06 04:58:01 +00005317 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005318 assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
5319 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5320 /*ByVal=*/true,
5321 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00005322 }
5323
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005324 // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
5325 // same size and alignment.
5326 if (getTarget().isRenderScriptTarget()) {
Matt Wala1d151512015-08-10 15:58:40 -07005327 return coerceToIntArray(Ty, getContext(), getVMContext());
5328 }
5329
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00005330 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00005331 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005332 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00005333 // FIXME: Try to match the types of the arguments more accurately where
5334 // we can.
5335 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00005336 ElemTy = llvm::Type::getInt32Ty(getVMContext());
5337 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00005338 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00005339 ElemTy = llvm::Type::getInt64Ty(getVMContext());
5340 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00005341 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00005342
Stephen Hines176edba2014-12-01 14:53:08 -08005343 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005344}
5345
Chris Lattnera3c109b2010-07-29 02:16:43 +00005346static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00005347 llvm::LLVMContext &VMContext) {
5348 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
5349 // is called integer-like if its size is less than or equal to one word, and
5350 // the offset of each of its addressable sub-fields is zero.
5351
5352 uint64_t Size = Context.getTypeSize(Ty);
5353
5354 // Check that the type fits in a word.
5355 if (Size > 32)
5356 return false;
5357
5358 // FIXME: Handle vector types!
5359 if (Ty->isVectorType())
5360 return false;
5361
Daniel Dunbarb0d58192009-09-14 02:20:34 +00005362 // Float types are never treated as "integer like".
5363 if (Ty->isRealFloatingType())
5364 return false;
5365
Daniel Dunbar98303b92009-09-13 08:03:58 +00005366 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00005367 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00005368 return true;
5369
Daniel Dunbar45815812010-02-01 23:31:26 +00005370 // Small complex integer types are "integer like".
5371 if (const ComplexType *CT = Ty->getAs<ComplexType>())
5372 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00005373
5374 // Single element and zero sized arrays should be allowed, by the definition
5375 // above, but they are not.
5376
5377 // Otherwise, it must be a record type.
5378 const RecordType *RT = Ty->getAs<RecordType>();
5379 if (!RT) return false;
5380
5381 // Ignore records with flexible arrays.
5382 const RecordDecl *RD = RT->getDecl();
5383 if (RD->hasFlexibleArrayMember())
5384 return false;
5385
5386 // Check that all sub-fields are at offset 0, and are themselves "integer
5387 // like".
5388 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
5389
5390 bool HadField = false;
5391 unsigned idx = 0;
5392 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5393 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00005394 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00005395
Daniel Dunbar679855a2010-01-29 03:22:29 +00005396 // Bit-fields are not addressable, we only need to verify they are "integer
5397 // like". We still have to disallow a subsequent non-bitfield, for example:
5398 // struct { int : 0; int x }
5399 // is non-integer like according to gcc.
5400 if (FD->isBitField()) {
5401 if (!RD->isUnion())
5402 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00005403
Daniel Dunbar679855a2010-01-29 03:22:29 +00005404 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5405 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00005406
Daniel Dunbar679855a2010-01-29 03:22:29 +00005407 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00005408 }
5409
Daniel Dunbar679855a2010-01-29 03:22:29 +00005410 // Check if this field is at offset 0.
5411 if (Layout.getFieldOffset(idx) != 0)
5412 return false;
5413
Daniel Dunbar98303b92009-09-13 08:03:58 +00005414 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5415 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00005416
Daniel Dunbar679855a2010-01-29 03:22:29 +00005417 // Only allow at most one field in a structure. This doesn't match the
5418 // wording above, but follows gcc in situations with a field following an
5419 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00005420 if (!RD->isUnion()) {
5421 if (HadField)
5422 return false;
5423
5424 HadField = true;
5425 }
5426 }
5427
5428 return true;
5429}
5430
Stephen Hines651f13c2014-04-23 16:59:28 -07005431ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
5432 bool isVariadic) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005433 bool IsEffectivelyAAPCS_VFP =
5434 (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic;
Stephen Hines176edba2014-12-01 14:53:08 -08005435
Daniel Dunbar98303b92009-09-13 08:03:58 +00005436 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005437 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00005438
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00005439 // Large vector types should be returned via memory.
Stephen Hines651f13c2014-04-23 16:59:28 -07005440 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005441 return getNaturalAlignIndirect(RetTy);
5442 }
5443
5444 // __fp16 gets returned as if it were an int or float, but with the top 16
5445 // bits unspecified. This is not done for OpenCL as it handles the half type
5446 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar1567b302016-03-18 16:58:36 +00005447 if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005448 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5449 llvm::Type::getFloatTy(getVMContext()) :
5450 llvm::Type::getInt32Ty(getVMContext());
5451 return ABIArgInfo::getDirect(ResType);
Stephen Hines651f13c2014-04-23 16:59:28 -07005452 }
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00005453
John McCalld608cdb2010-08-22 10:59:02 +00005454 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005455 // Treat an enum type as its underlying type.
5456 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5457 RetTy = EnumTy->getDecl()->getIntegerType();
5458
Stephen Hines176edba2014-12-01 14:53:08 -08005459 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5460 : ABIArgInfo::getDirect();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005461 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00005462
5463 // Are we following APCS?
5464 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00005465 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00005466 return ABIArgInfo::getIgnore();
5467
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00005468 // Complex types are all returned as packed integers.
5469 //
5470 // FIXME: Consider using 2 x vector types if the back end handles them
5471 // correctly.
5472 if (RetTy->isAnyComplexType())
Stephen Hines176edba2014-12-01 14:53:08 -08005473 return ABIArgInfo::getDirect(llvm::IntegerType::get(
5474 getVMContext(), getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00005475
Daniel Dunbar98303b92009-09-13 08:03:58 +00005476 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00005477 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00005478 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00005479 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00005480 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00005481 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00005482 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00005483 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5484 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00005485 }
5486
5487 // Otherwise return in memory.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005488 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005489 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00005490
5491 // Otherwise this is an AAPCS variant.
5492
Chris Lattnera3c109b2010-07-29 02:16:43 +00005493 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00005494 return ABIArgInfo::getIgnore();
5495
Bob Wilson3b694fa2011-11-02 04:51:36 +00005496 // Check for homogeneous aggregates with AAPCS-VFP.
Stephen Hines176edba2014-12-01 14:53:08 -08005497 if (IsEffectivelyAAPCS_VFP) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005498 const Type *Base = nullptr;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005499 uint64_t Members = 0;
Stephen Hines176edba2014-12-01 14:53:08 -08005500 if (isHomogeneousAggregate(RetTy, Base, Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00005501 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00005502 // Homogeneous Aggregates are returned directly.
Stephen Hines176edba2014-12-01 14:53:08 -08005503 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00005504 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00005505 }
5506
Daniel Dunbar98303b92009-09-13 08:03:58 +00005507 // Aggregates <= 4 bytes are returned in r0; other aggregates
5508 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00005509 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00005510 if (Size <= 32) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005511 // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
5512 // same size and alignment.
5513 if (getTarget().isRenderScriptTarget()) {
Matt Wala1d151512015-08-10 15:58:40 -07005514 return coerceToIntArray(RetTy, getContext(), getVMContext());
5515 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005516 if (getDataLayout().isBigEndian())
5517 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
5518 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5519
Daniel Dunbar16a08082009-09-14 00:56:55 +00005520 // Return in the smallest viable integer type.
5521 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00005522 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00005523 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00005524 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5525 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005526 } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
5527 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
5528 llvm::Type *CoerceTy =
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005529 llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005530 return ABIArgInfo::getDirect(CoerceTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00005531 }
5532
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005533 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005534}
5535
Manman Ren97f81572012-10-16 19:18:39 +00005536/// isIllegalVector - check whether Ty is an illegal vector type.
5537bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005538 if (const VectorType *VT = Ty->getAs<VectorType> ()) {
5539 if (isAndroid()) {
5540 // Android shipped using Clang 3.1, which supported a slightly different
5541 // vector ABI. The primary differences were that 3-element vector types
5542 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
5543 // accepts that legacy behavior for Android only.
5544 // Check whether VT is legal.
5545 unsigned NumElements = VT->getNumElements();
5546 // NumElements should be power of 2 or equal to 3.
5547 if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
5548 return true;
5549 } else {
5550 // Check whether VT is legal.
5551 unsigned NumElements = VT->getNumElements();
5552 uint64_t Size = getContext().getTypeSize(VT);
5553 // NumElements should be power of 2.
5554 if (!llvm::isPowerOf2_32(NumElements))
5555 return true;
5556 // Size should be greater than 32 bits.
5557 return Size <= 32;
5558 }
Manman Ren97f81572012-10-16 19:18:39 +00005559 }
5560 return false;
5561}
5562
Stephen Hines176edba2014-12-01 14:53:08 -08005563bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5564 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
5565 // double, or 64-bit or 128-bit vectors.
5566 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5567 if (BT->getKind() == BuiltinType::Float ||
5568 BT->getKind() == BuiltinType::Double ||
5569 BT->getKind() == BuiltinType::LongDouble)
5570 return true;
5571 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5572 unsigned VecSize = getContext().getTypeSize(VT);
5573 if (VecSize == 64 || VecSize == 128)
5574 return true;
5575 }
5576 return false;
5577}
5578
5579bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5580 uint64_t Members) const {
5581 return Members <= 4;
5582}
5583
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005584Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5585 QualType Ty) const {
5586 CharUnits SlotSize = CharUnits::fromQuantity(4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005587
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005588 // Empty records are ignored for parameter passing purposes.
Tim Northover373ac0a2013-06-21 23:05:33 +00005589 if (isEmptyRecord(getContext(), Ty, true)) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005590 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
5591 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5592 return Addr;
Tim Northover373ac0a2013-06-21 23:05:33 +00005593 }
5594
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005595 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5596 CharUnits TyAlignForABI = TyInfo.second;
Manman Rend105e732012-10-16 19:01:37 +00005597
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005598 // Use indirect if size of the illegal vector is bigger than 16 bytes.
5599 bool IsIndirect = false;
5600 const Type *Base = nullptr;
5601 uint64_t Members = 0;
5602 if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
5603 IsIndirect = true;
5604
5605 // ARMv7k passes structs bigger than 16 bytes indirectly, in space
5606 // allocated by the caller.
5607 } else if (TyInfo.first > CharUnits::fromQuantity(16) &&
5608 getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5609 !isHomogeneousAggregate(Ty, Base, Members)) {
5610 IsIndirect = true;
5611
5612 // Otherwise, bound the type's ABI alignment.
Manman Rend105e732012-10-16 19:01:37 +00005613 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
5614 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005615 // Our callers should be prepared to handle an under-aligned address.
5616 } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
5617 getABIKind() == ARMABIInfo::AAPCS) {
5618 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
5619 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
5620 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5621 // ARMv7k allows type alignment up to 16 bytes.
5622 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
5623 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
5624 } else {
5625 TyAlignForABI = CharUnits::fromQuantity(4);
Manman Ren97f81572012-10-16 19:18:39 +00005626 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005627 TyInfo.second = TyAlignForABI;
Manman Rend105e732012-10-16 19:01:37 +00005628
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005629 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
5630 SlotSize, /*AllowHigherAlign*/ true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005631}
5632
Chris Lattnerdce5ad02010-06-28 20:05:43 +00005633//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00005634// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005635//===----------------------------------------------------------------------===//
5636
5637namespace {
5638
Justin Holewinski2c585b92012-05-24 17:43:12 +00005639class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005640public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00005641 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005642
5643 ABIArgInfo classifyReturnType(QualType RetTy) const;
5644 ABIArgInfo classifyArgumentType(QualType Ty) const;
5645
Stephen Hines651f13c2014-04-23 16:59:28 -07005646 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005647 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5648 QualType Ty) const override;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005649};
5650
Justin Holewinski2c585b92012-05-24 17:43:12 +00005651class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005652public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00005653 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
5654 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Stephen Hines651f13c2014-04-23 16:59:28 -07005655
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005656 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07005657 CodeGen::CodeGenModule &M) const override;
Justin Holewinskidca8f332013-03-30 14:38:24 +00005658private:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005659 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
5660 // resulting MDNode to the nvvm.annotations MDNode.
5661 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005662};
5663
Justin Holewinski2c585b92012-05-24 17:43:12 +00005664ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005665 if (RetTy->isVoidType())
5666 return ABIArgInfo::getIgnore();
Bill Wendling846ff9f2013-11-21 23:31:45 +00005667
5668 // note: this is different from default ABI
5669 if (!RetTy->isScalarType())
5670 return ABIArgInfo::getDirect();
5671
5672 // Treat an enum type as its underlying type.
5673 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5674 RetTy = EnumTy->getDecl()->getIntegerType();
5675
5676 return (RetTy->isPromotableIntegerType() ?
5677 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005678}
5679
Justin Holewinski2c585b92012-05-24 17:43:12 +00005680ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Bill Wendling846ff9f2013-11-21 23:31:45 +00005681 // Treat an enum type as its underlying type.
5682 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5683 Ty = EnumTy->getDecl()->getIntegerType();
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005684
Stephen Hines176edba2014-12-01 14:53:08 -08005685 // Return aggregates type as indirect by value
5686 if (isAggregateTypeForABI(Ty))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005687 return getNaturalAlignIndirect(Ty, /* byval */ true);
Stephen Hines176edba2014-12-01 14:53:08 -08005688
Bill Wendling846ff9f2013-11-21 23:31:45 +00005689 return (Ty->isPromotableIntegerType() ?
5690 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005691}
5692
Justin Holewinski2c585b92012-05-24 17:43:12 +00005693void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005694 if (!getCXXABI().classifyReturnType(FI))
5695 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -07005696 for (auto &I : FI.arguments())
5697 I.info = classifyArgumentType(I.type);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005698
5699 // Always honor user-specified calling convention.
5700 if (FI.getCallingConvention() != llvm::CallingConv::C)
5701 return;
5702
John McCallbd7370a2013-02-28 19:01:20 +00005703 FI.setEffectiveCallingConvention(getRuntimeCC());
5704}
5705
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005706Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5707 QualType Ty) const {
Justin Holewinski2c585b92012-05-24 17:43:12 +00005708 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005709}
5710
Justin Holewinski2c585b92012-05-24 17:43:12 +00005711void NVPTXTargetCodeGenInfo::
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005712setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Justin Holewinski2c585b92012-05-24 17:43:12 +00005713 CodeGen::CodeGenModule &M) const{
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005714 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Justin Holewinski818eafb2011-10-05 17:58:44 +00005715 if (!FD) return;
5716
5717 llvm::Function *F = cast<llvm::Function>(GV);
5718
5719 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00005720 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00005721 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00005722 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00005723 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00005724 // OpenCL __kernel functions get kernel metadata
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005725 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5726 addNVVMMetadata(F, "kernel", 1);
Justin Holewinski818eafb2011-10-05 17:58:44 +00005727 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00005728 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00005729 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00005730 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00005731
Peter Collingbourne744d90b2011-10-06 16:49:54 +00005732 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00005733 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00005734 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00005735 // __global__ functions cannot be called from the device, we do not
5736 // need to set the noinline attribute.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005737 if (FD->hasAttr<CUDAGlobalAttr>()) {
5738 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5739 addNVVMMetadata(F, "kernel", 1);
5740 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005741 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005742 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005743 llvm::APSInt MaxThreads(32);
5744 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
5745 if (MaxThreads > 0)
5746 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
5747
5748 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
5749 // not specified in __launch_bounds__ or if the user specified a 0 value,
5750 // we don't have to add a PTX directive.
5751 if (Attr->getMinBlocks()) {
5752 llvm::APSInt MinBlocks(32);
5753 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
5754 if (MinBlocks > 0)
5755 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
5756 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005757 }
5758 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00005759 }
5760}
5761
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005762void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
5763 int Operand) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00005764 llvm::Module *M = F->getParent();
5765 llvm::LLVMContext &Ctx = M->getContext();
5766
5767 // Get "nvvm.annotations" metadata node
5768 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
5769
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005770 llvm::Metadata *MDVals[] = {
5771 llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
5772 llvm::ConstantAsMetadata::get(
5773 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
Justin Holewinskidca8f332013-03-30 14:38:24 +00005774 // Append metadata to nvvm.annotations
5775 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
5776}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005777}
5778
5779//===----------------------------------------------------------------------===//
Ulrich Weigandb8409212013-05-06 16:26:41 +00005780// SystemZ ABI Implementation
5781//===----------------------------------------------------------------------===//
5782
5783namespace {
5784
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005785class SystemZABIInfo : public SwiftABIInfo {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005786 bool HasVector;
5787
Ulrich Weigandb8409212013-05-06 16:26:41 +00005788public:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005789 SystemZABIInfo(CodeGenTypes &CGT, bool HV)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005790 : SwiftABIInfo(CGT), HasVector(HV) {}
Ulrich Weigandb8409212013-05-06 16:26:41 +00005791
5792 bool isPromotableIntegerType(QualType Ty) const;
5793 bool isCompoundType(QualType Ty) const;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005794 bool isVectorArgumentType(QualType Ty) const;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005795 bool isFPArgumentType(QualType Ty) const;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005796 QualType GetSingleElementType(QualType Ty) const;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005797
5798 ABIArgInfo classifyReturnType(QualType RetTy) const;
5799 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
5800
Stephen Hines651f13c2014-04-23 16:59:28 -07005801 void computeInfo(CGFunctionInfo &FI) const override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005802 if (!getCXXABI().classifyReturnType(FI))
5803 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -07005804 for (auto &I : FI.arguments())
5805 I.info = classifyArgumentType(I.type);
Ulrich Weigandb8409212013-05-06 16:26:41 +00005806 }
5807
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005808 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5809 QualType Ty) const override;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005810
5811 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
5812 ArrayRef<llvm::Type*> scalars,
5813 bool asReturnValue) const override {
5814 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5815 }
Ulrich Weigandb8409212013-05-06 16:26:41 +00005816};
5817
5818class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
5819public:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005820 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
5821 : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
Ulrich Weigandb8409212013-05-06 16:26:41 +00005822};
5823
5824}
5825
5826bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
5827 // Treat an enum type as its underlying type.
5828 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5829 Ty = EnumTy->getDecl()->getIntegerType();
5830
5831 // Promotable integer types are required to be promoted by the ABI.
5832 if (Ty->isPromotableIntegerType())
5833 return true;
5834
5835 // 32-bit values must also be promoted.
5836 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5837 switch (BT->getKind()) {
5838 case BuiltinType::Int:
5839 case BuiltinType::UInt:
5840 return true;
5841 default:
5842 return false;
5843 }
5844 return false;
5845}
5846
5847bool SystemZABIInfo::isCompoundType(QualType Ty) const {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005848 return (Ty->isAnyComplexType() ||
5849 Ty->isVectorType() ||
5850 isAggregateTypeForABI(Ty));
Ulrich Weigandb8409212013-05-06 16:26:41 +00005851}
5852
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005853bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
5854 return (HasVector &&
5855 Ty->isVectorType() &&
5856 getContext().getTypeSize(Ty) <= 128);
5857}
5858
Ulrich Weigandb8409212013-05-06 16:26:41 +00005859bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
5860 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5861 switch (BT->getKind()) {
5862 case BuiltinType::Float:
5863 case BuiltinType::Double:
5864 return true;
5865 default:
5866 return false;
5867 }
5868
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005869 return false;
5870}
5871
5872QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
Ulrich Weigandb8409212013-05-06 16:26:41 +00005873 if (const RecordType *RT = Ty->getAsStructureType()) {
5874 const RecordDecl *RD = RT->getDecl();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005875 QualType Found;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005876
5877 // If this is a C++ record, check the bases first.
5878 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Stephen Hines651f13c2014-04-23 16:59:28 -07005879 for (const auto &I : CXXRD->bases()) {
5880 QualType Base = I.getType();
Ulrich Weigandb8409212013-05-06 16:26:41 +00005881
5882 // Empty bases don't affect things either way.
5883 if (isEmptyRecord(getContext(), Base, true))
5884 continue;
5885
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005886 if (!Found.isNull())
5887 return Ty;
5888 Found = GetSingleElementType(Base);
Ulrich Weigandb8409212013-05-06 16:26:41 +00005889 }
5890
5891 // Check the fields.
Stephen Hines651f13c2014-04-23 16:59:28 -07005892 for (const auto *FD : RD->fields()) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005893 // For compatibility with GCC, ignore empty bitfields in C++ mode.
Ulrich Weigandb8409212013-05-06 16:26:41 +00005894 // Unlike isSingleElementStruct(), empty structure and array fields
5895 // do count. So do anonymous bitfields that aren't zero-sized.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005896 if (getContext().getLangOpts().CPlusPlus &&
5897 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
5898 continue;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005899
5900 // Unlike isSingleElementStruct(), arrays do not count.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005901 // Nested structures still do though.
5902 if (!Found.isNull())
5903 return Ty;
5904 Found = GetSingleElementType(FD->getType());
Ulrich Weigandb8409212013-05-06 16:26:41 +00005905 }
5906
5907 // Unlike isSingleElementStruct(), trailing padding is allowed.
5908 // An 8-byte aligned struct s { float f; } is passed as a double.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005909 if (!Found.isNull())
5910 return Found;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005911 }
5912
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005913 return Ty;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005914}
5915
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005916Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5917 QualType Ty) const {
Ulrich Weigandb8409212013-05-06 16:26:41 +00005918 // Assume that va_list type is correct; should be pointer to LLVM type:
5919 // struct {
5920 // i64 __gpr;
5921 // i64 __fpr;
5922 // i8 *__overflow_arg_area;
5923 // i8 *__reg_save_area;
5924 // };
5925
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005926 // Every non-vector argument occupies 8 bytes and is passed by preference
5927 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
5928 // always passed on the stack.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005929 Ty = getContext().getCanonicalType(Ty);
5930 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005931 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005932 llvm::Type *DirectTy = ArgTy;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005933 ABIArgInfo AI = classifyArgumentType(Ty);
Ulrich Weigandb8409212013-05-06 16:26:41 +00005934 bool IsIndirect = AI.isIndirect();
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005935 bool InFPRs = false;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005936 bool IsVector = false;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005937 CharUnits UnpaddedSize;
5938 CharUnits DirectAlign;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005939 if (IsIndirect) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005940 DirectTy = llvm::PointerType::getUnqual(DirectTy);
5941 UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005942 } else {
5943 if (AI.getCoerceToType())
5944 ArgTy = AI.getCoerceToType();
5945 InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005946 IsVector = ArgTy->isVectorTy();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005947 UnpaddedSize = TyInfo.first;
5948 DirectAlign = TyInfo.second;
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005949 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005950 CharUnits PaddedSize = CharUnits::fromQuantity(8);
5951 if (IsVector && UnpaddedSize > PaddedSize)
5952 PaddedSize = CharUnits::fromQuantity(16);
5953 assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
Ulrich Weigandb8409212013-05-06 16:26:41 +00005954
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005955 CharUnits Padding = (PaddedSize - UnpaddedSize);
Ulrich Weigandb8409212013-05-06 16:26:41 +00005956
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005957 llvm::Type *IndexTy = CGF.Int64Ty;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005958 llvm::Value *PaddedSizeV =
5959 llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005960
5961 if (IsVector) {
5962 // Work out the address of a vector argument on the stack.
5963 // Vector arguments are always passed in the high bits of a
5964 // single (8 byte) or double (16 byte) stack slot.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005965 Address OverflowArgAreaPtr =
5966 CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16),
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005967 "overflow_arg_area_ptr");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005968 Address OverflowArgArea =
5969 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
5970 TyInfo.second);
5971 Address MemAddr =
5972 CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005973
5974 // Update overflow_arg_area_ptr pointer
5975 llvm::Value *NewOverflowArgArea =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005976 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
5977 "overflow_arg_area");
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005978 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
5979
5980 return MemAddr;
5981 }
5982
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005983 assert(PaddedSize.getQuantity() == 8);
5984
5985 unsigned MaxRegs, RegCountField, RegSaveIndex;
5986 CharUnits RegPadding;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005987 if (InFPRs) {
5988 MaxRegs = 4; // Maximum of 4 FPR arguments
5989 RegCountField = 1; // __fpr
5990 RegSaveIndex = 16; // save offset for f0
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005991 RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
Ulrich Weigandb8409212013-05-06 16:26:41 +00005992 } else {
5993 MaxRegs = 5; // Maximum of 5 GPR arguments
5994 RegCountField = 0; // __gpr
5995 RegSaveIndex = 2; // save offset for r2
5996 RegPadding = Padding; // values are passed in the low bits of a GPR
5997 }
5998
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005999 Address RegCountPtr = CGF.Builder.CreateStructGEP(
6000 VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8),
6001 "reg_count_ptr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006002 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006003 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
6004 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
Stephen Hines651f13c2014-04-23 16:59:28 -07006005 "fits_in_regs");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006006
6007 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
6008 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
6009 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
6010 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
6011
6012 // Emit code to load the value if it was passed in registers.
6013 CGF.EmitBlock(InRegBlock);
6014
6015 // Work out the address of an argument register.
Ulrich Weigandb8409212013-05-06 16:26:41 +00006016 llvm::Value *ScaledRegCount =
6017 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
6018 llvm::Value *RegBase =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006019 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
6020 + RegPadding.getQuantity());
Ulrich Weigandb8409212013-05-06 16:26:41 +00006021 llvm::Value *RegOffset =
6022 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006023 Address RegSaveAreaPtr =
6024 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
6025 "reg_save_area_ptr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006026 llvm::Value *RegSaveArea =
6027 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006028 Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
6029 "raw_reg_addr"),
6030 PaddedSize);
6031 Address RegAddr =
6032 CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006033
6034 // Update the register count
6035 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
6036 llvm::Value *NewRegCount =
6037 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
6038 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
6039 CGF.EmitBranch(ContBlock);
6040
6041 // Emit code to load the value if it was passed in memory.
6042 CGF.EmitBlock(InMemBlock);
6043
6044 // Work out the address of a stack argument.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006045 Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
6046 VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr");
6047 Address OverflowArgArea =
6048 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6049 PaddedSize);
6050 Address RawMemAddr =
6051 CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
6052 Address MemAddr =
6053 CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006054
6055 // Update overflow_arg_area_ptr pointer
6056 llvm::Value *NewOverflowArgArea =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006057 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6058 "overflow_arg_area");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006059 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6060 CGF.EmitBranch(ContBlock);
6061
6062 // Return the appropriate result.
6063 CGF.EmitBlock(ContBlock);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006064 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6065 MemAddr, InMemBlock, "va_arg.addr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006066
6067 if (IsIndirect)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006068 ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
6069 TyInfo.second);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006070
6071 return ResAddr;
6072}
6073
Ulrich Weigandb8409212013-05-06 16:26:41 +00006074ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
6075 if (RetTy->isVoidType())
6076 return ABIArgInfo::getIgnore();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006077 if (isVectorArgumentType(RetTy))
6078 return ABIArgInfo::getDirect();
Ulrich Weigandb8409212013-05-06 16:26:41 +00006079 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006080 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006081 return (isPromotableIntegerType(RetTy) ?
6082 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6083}
6084
6085ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
6086 // Handle the generic C++ ABI.
Mark Lacey23630722013-10-06 01:33:34 +00006087 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006088 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006089
6090 // Integers and enums are extended to full register width.
6091 if (isPromotableIntegerType(Ty))
6092 return ABIArgInfo::getExtend();
6093
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006094 // Handle vector types and vector-like structure types. Note that
6095 // as opposed to float-like structure types, we do not allow any
6096 // padding for vector-like structures, so verify the sizes match.
Ulrich Weigandb8409212013-05-06 16:26:41 +00006097 uint64_t Size = getContext().getTypeSize(Ty);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006098 QualType SingleElementTy = GetSingleElementType(Ty);
6099 if (isVectorArgumentType(SingleElementTy) &&
6100 getContext().getTypeSize(SingleElementTy) == Size)
6101 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
6102
6103 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
Ulrich Weigandb8409212013-05-06 16:26:41 +00006104 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006105 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006106
6107 // Handle small structures.
6108 if (const RecordType *RT = Ty->getAs<RecordType>()) {
6109 // Structures with flexible arrays have variable length, so really
6110 // fail the size test above.
6111 const RecordDecl *RD = RT->getDecl();
6112 if (RD->hasFlexibleArrayMember())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006113 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006114
6115 // The structure is passed as an unextended integer, a float, or a double.
6116 llvm::Type *PassTy;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006117 if (isFPArgumentType(SingleElementTy)) {
Ulrich Weigandb8409212013-05-06 16:26:41 +00006118 assert(Size == 32 || Size == 64);
6119 if (Size == 32)
6120 PassTy = llvm::Type::getFloatTy(getVMContext());
6121 else
6122 PassTy = llvm::Type::getDoubleTy(getVMContext());
6123 } else
6124 PassTy = llvm::IntegerType::get(getVMContext(), Size);
6125 return ABIArgInfo::getDirect(PassTy);
6126 }
6127
6128 // Non-structure compounds are passed indirectly.
6129 if (isCompoundType(Ty))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006130 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006131
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006132 return ABIArgInfo::getDirect(nullptr);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006133}
6134
6135//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006136// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00006137//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006138
6139namespace {
6140
6141class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
6142public:
Chris Lattnerea044322010-07-29 02:01:43 +00006143 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
6144 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006145 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07006146 CodeGen::CodeGenModule &M) const override;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006147};
6148
6149}
6150
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006151void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006152 llvm::GlobalValue *GV,
6153 CodeGen::CodeGenModule &M) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006154 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006155 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
6156 // Handle 'interrupt' attribute:
6157 llvm::Function *F = cast<llvm::Function>(GV);
6158
6159 // Step 1: Set ISR calling convention.
6160 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
6161
6162 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00006163 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006164
6165 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00006166 unsigned Num = attr->getNumber() / 2;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006167 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
6168 "__isr_" + Twine(Num), F);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006169 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00006170 }
6171}
6172
Chris Lattnerdce5ad02010-06-28 20:05:43 +00006173//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00006174// MIPS ABI Implementation. This works for both little-endian and
6175// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00006176//===----------------------------------------------------------------------===//
6177
John McCallaeeb7012010-05-27 06:19:26 +00006178namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00006179class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00006180 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00006181 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
6182 void CoerceToIntArgs(uint64_t TySize,
Craig Topper6b9240e2013-07-05 19:34:19 +00006183 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006184 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006185 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006186 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00006187public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00006188 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00006189 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
6190 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00006191
6192 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00006193 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Stephen Hines651f13c2014-04-23 16:59:28 -07006194 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006195 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6196 QualType Ty) const override;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006197 bool shouldSignExtUnsignedType(QualType Ty) const override;
Akira Hatanaka619e8872011-06-02 00:09:17 +00006198};
6199
John McCallaeeb7012010-05-27 06:19:26 +00006200class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00006201 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00006202public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00006203 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
6204 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
6205 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00006206
Stephen Hines651f13c2014-04-23 16:59:28 -07006207 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallaeeb7012010-05-27 06:19:26 +00006208 return 29;
6209 }
6210
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006211 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07006212 CodeGen::CodeGenModule &CGM) const override {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006213 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00006214 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00006215 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00006216 if (FD->hasAttr<Mips16Attr>()) {
6217 Fn->addFnAttr("mips16");
6218 }
6219 else if (FD->hasAttr<NoMips16Attr>()) {
6220 Fn->addFnAttr("nomips16");
6221 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006222
6223 const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
6224 if (!Attr)
6225 return;
6226
6227 const char *Kind;
6228 switch (Attr->getInterrupt()) {
6229 case MipsInterruptAttr::eic: Kind = "eic"; break;
6230 case MipsInterruptAttr::sw0: Kind = "sw0"; break;
6231 case MipsInterruptAttr::sw1: Kind = "sw1"; break;
6232 case MipsInterruptAttr::hw0: Kind = "hw0"; break;
6233 case MipsInterruptAttr::hw1: Kind = "hw1"; break;
6234 case MipsInterruptAttr::hw2: Kind = "hw2"; break;
6235 case MipsInterruptAttr::hw3: Kind = "hw3"; break;
6236 case MipsInterruptAttr::hw4: Kind = "hw4"; break;
6237 case MipsInterruptAttr::hw5: Kind = "hw5"; break;
6238 }
6239
6240 Fn->addFnAttr("interrupt", Kind);
6241
Reed Kotler7dfd1822013-01-16 17:10:28 +00006242 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00006243
John McCallaeeb7012010-05-27 06:19:26 +00006244 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07006245 llvm::Value *Address) const override;
John McCall49e34be2011-08-30 01:42:09 +00006246
Stephen Hines651f13c2014-04-23 16:59:28 -07006247 unsigned getSizeOfUnwindException() const override {
Akira Hatanakae624fa02011-09-20 18:23:28 +00006248 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00006249 }
John McCallaeeb7012010-05-27 06:19:26 +00006250};
6251}
6252
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006253void MipsABIInfo::CoerceToIntArgs(
6254 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00006255 llvm::IntegerType *IntTy =
6256 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006257
6258 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
6259 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
6260 ArgList.push_back(IntTy);
6261
6262 // If necessary, add one more integer type to ArgList.
6263 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
6264
6265 if (R)
6266 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006267}
6268
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006269// In N32/64, an aligned double precision floating point field is passed in
6270// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006271llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00006272 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
6273
6274 if (IsO32) {
6275 CoerceToIntArgs(TySize, ArgList);
6276 return llvm::StructType::get(getVMContext(), ArgList);
6277 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006278
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00006279 if (Ty->isComplexType())
6280 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00006281
Akira Hatanakaa34e9212012-02-09 19:54:16 +00006282 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006283
Akira Hatanakac359f202012-07-03 19:24:06 +00006284 // Unions/vectors are passed in integer registers.
6285 if (!RT || !RT->isStructureOrClassType()) {
6286 CoerceToIntArgs(TySize, ArgList);
6287 return llvm::StructType::get(getVMContext(), ArgList);
6288 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006289
6290 const RecordDecl *RD = RT->getDecl();
6291 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006292 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006293
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006294 uint64_t LastOffset = 0;
6295 unsigned idx = 0;
6296 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
6297
Akira Hatanakaa34e9212012-02-09 19:54:16 +00006298 // Iterate over fields in the struct/class and check if there are any aligned
6299 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006300 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6301 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00006302 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006303 const BuiltinType *BT = Ty->getAs<BuiltinType>();
6304
6305 if (!BT || BT->getKind() != BuiltinType::Double)
6306 continue;
6307
6308 uint64_t Offset = Layout.getFieldOffset(idx);
6309 if (Offset % 64) // Ignore doubles that are not aligned.
6310 continue;
6311
6312 // Add ((Offset - LastOffset) / 64) args of type i64.
6313 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
6314 ArgList.push_back(I64);
6315
6316 // Add double type.
6317 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
6318 LastOffset = Offset + 64;
6319 }
6320
Akira Hatanakac359f202012-07-03 19:24:06 +00006321 CoerceToIntArgs(TySize - LastOffset, IntArgList);
6322 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006323
6324 return llvm::StructType::get(getVMContext(), ArgList);
6325}
6326
Akira Hatanaka7ebd9532013-10-29 18:41:15 +00006327llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
6328 uint64_t Offset) const {
6329 if (OrigOffset + MinABIStackAlignInBytes > Offset)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006330 return nullptr;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006331
Akira Hatanaka7ebd9532013-10-29 18:41:15 +00006332 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006333}
Akira Hatanaka9659d592012-01-10 22:44:52 +00006334
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00006335ABIArgInfo
6336MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006337 Ty = useFirstFieldIfTransparentUnion(Ty);
6338
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006339 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006340 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006341 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006342
Akira Hatanakac359f202012-07-03 19:24:06 +00006343 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
6344 (uint64_t)StackAlignInBytes);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07006345 unsigned CurrOffset = llvm::alignTo(Offset, Align);
6346 Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006347
Akira Hatanakac359f202012-07-03 19:24:06 +00006348 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00006349 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00006350 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00006351 return ABIArgInfo::getIgnore();
6352
Mark Lacey23630722013-10-06 01:33:34 +00006353 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006354 Offset = OrigOffset + MinABIStackAlignInBytes;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006355 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00006356 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00006357
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006358 // If we have reached here, aggregates are passed directly by coercing to
6359 // another structure type. Padding is inserted if the offset of the
6360 // aggregate is unaligned.
Stephen Hines176edba2014-12-01 14:53:08 -08006361 ABIArgInfo ArgInfo =
6362 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
6363 getPaddingType(OrigOffset, CurrOffset));
6364 ArgInfo.setInReg(true);
6365 return ArgInfo;
Akira Hatanaka619e8872011-06-02 00:09:17 +00006366 }
6367
6368 // Treat an enum type as its underlying type.
6369 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6370 Ty = EnumTy->getDecl()->getIntegerType();
6371
Stephen Hines176edba2014-12-01 14:53:08 -08006372 // All integral types are promoted to the GPR width.
6373 if (Ty->isIntegralOrEnumerationType())
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006374 return ABIArgInfo::getExtend();
6375
Akira Hatanaka7ebd9532013-10-29 18:41:15 +00006376 return ABIArgInfo::getDirect(
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006377 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00006378}
6379
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006380llvm::Type*
6381MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00006382 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00006383 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006384
Akira Hatanakada54ff32012-02-09 18:49:26 +00006385 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006386 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00006387 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6388 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006389
Akira Hatanakada54ff32012-02-09 18:49:26 +00006390 // N32/64 returns struct/classes in floating point registers if the
6391 // following conditions are met:
6392 // 1. The size of the struct/class is no larger than 128-bit.
6393 // 2. The struct/class has one or two fields all of which are floating
6394 // point types.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006395 // 3. The offset of the first field is zero (this follows what gcc does).
Akira Hatanakada54ff32012-02-09 18:49:26 +00006396 //
6397 // Any other composite results are returned in integer registers.
6398 //
6399 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
6400 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
6401 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00006402 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006403
Akira Hatanakada54ff32012-02-09 18:49:26 +00006404 if (!BT || !BT->isFloatingPoint())
6405 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006406
David Blaikie262bc182012-04-30 02:36:29 +00006407 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00006408 }
6409
6410 if (b == e)
6411 return llvm::StructType::get(getVMContext(), RTList,
6412 RD->hasAttr<PackedAttr>());
6413
6414 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006415 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006416 }
6417
Akira Hatanakac359f202012-07-03 19:24:06 +00006418 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006419 return llvm::StructType::get(getVMContext(), RTList);
6420}
6421
Akira Hatanaka619e8872011-06-02 00:09:17 +00006422ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00006423 uint64_t Size = getContext().getTypeSize(RetTy);
6424
Stephen Hines176edba2014-12-01 14:53:08 -08006425 if (RetTy->isVoidType())
6426 return ABIArgInfo::getIgnore();
6427
6428 // O32 doesn't treat zero-sized structs differently from other structs.
6429 // However, N32/N64 ignores zero sized return values.
6430 if (!IsO32 && Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00006431 return ABIArgInfo::getIgnore();
6432
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00006433 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006434 if (Size <= 128) {
6435 if (RetTy->isAnyComplexType())
6436 return ABIArgInfo::getDirect();
6437
Stephen Hines176edba2014-12-01 14:53:08 -08006438 // O32 returns integer vectors in registers and N32/N64 returns all small
6439 // aggregates in registers.
6440 if (!IsO32 ||
6441 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
6442 ABIArgInfo ArgInfo =
6443 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
6444 ArgInfo.setInReg(true);
6445 return ArgInfo;
6446 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006447 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00006448
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006449 return getNaturalAlignIndirect(RetTy);
Akira Hatanaka619e8872011-06-02 00:09:17 +00006450 }
6451
6452 // Treat an enum type as its underlying type.
6453 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6454 RetTy = EnumTy->getDecl()->getIntegerType();
6455
6456 return (RetTy->isPromotableIntegerType() ?
6457 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6458}
6459
6460void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00006461 ABIArgInfo &RetInfo = FI.getReturnInfo();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006462 if (!getCXXABI().classifyReturnType(FI))
6463 RetInfo = classifyReturnType(FI.getReturnType());
Akira Hatanakacc662542012-01-12 01:10:09 +00006464
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006465 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006466 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00006467
Stephen Hines651f13c2014-04-23 16:59:28 -07006468 for (auto &I : FI.arguments())
6469 I.info = classifyArgumentType(I.type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00006470}
6471
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006472Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6473 QualType OrigTy) const {
6474 QualType Ty = OrigTy;
Stephen Hines176edba2014-12-01 14:53:08 -08006475
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006476 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
6477 // Pointers are also promoted in the same way but this only matters for N32.
Stephen Hines176edba2014-12-01 14:53:08 -08006478 unsigned SlotSizeInBits = IsO32 ? 32 : 64;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006479 unsigned PtrWidth = getTarget().getPointerWidth(0);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006480 bool DidPromote = false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006481 if ((Ty->isIntegerType() &&
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006482 getContext().getIntWidth(Ty) < SlotSizeInBits) ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006483 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006484 DidPromote = true;
6485 Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
6486 Ty->isSignedIntegerType());
Stephen Hines176edba2014-12-01 14:53:08 -08006487 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006488
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006489 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00006490
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006491 // The alignment of things in the argument area is never larger than
6492 // StackAlignInBytes.
6493 TyInfo.second =
6494 std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
6495
6496 // MinABIStackAlignInBytes is the size of argument slots on the stack.
6497 CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
6498
6499 Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6500 TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
6501
6502
6503 // If there was a promotion, "unpromote" into a temporary.
6504 // TODO: can we just use a pointer into a subset of the original slot?
6505 if (DidPromote) {
6506 Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
6507 llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
6508
6509 // Truncate down to the right width.
6510 llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
6511 : CGF.IntPtrTy);
6512 llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
6513 if (OrigTy->isPointerType())
6514 V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
6515
6516 CGF.Builder.CreateStore(V, Temp);
6517 Addr = Temp;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00006518 }
Akira Hatanakac35e69d2011-08-01 20:48:01 +00006519
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006520 return Addr;
Akira Hatanaka619e8872011-06-02 00:09:17 +00006521}
6522
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006523bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
6524 int TySize = getContext().getTypeSize(Ty);
6525
6526 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
6527 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
6528 return true;
6529
6530 return false;
6531}
6532
John McCallaeeb7012010-05-27 06:19:26 +00006533bool
6534MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6535 llvm::Value *Address) const {
6536 // This information comes from gcc's implementation, which seems to
6537 // as canonical as it gets.
6538
John McCallaeeb7012010-05-27 06:19:26 +00006539 // Everything on MIPS is 4 bytes. Double-precision FP registers
6540 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00006541 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00006542
6543 // 0-31 are the general purpose registers, $0 - $31.
6544 // 32-63 are the floating-point registers, $f0 - $f31.
6545 // 64 and 65 are the multiply/divide registers, $hi and $lo.
6546 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00006547 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00006548
6549 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
6550 // They are one bit wide and ignored here.
6551
6552 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
6553 // (coprocessor 1 is the FP unit)
6554 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
6555 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
6556 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00006557 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00006558 return false;
6559}
6560
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006561//===----------------------------------------------------------------------===//
6562// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006563// Currently subclassed only to implement custom OpenCL C function attribute
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006564// handling.
6565//===----------------------------------------------------------------------===//
6566
6567namespace {
6568
6569class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
6570public:
6571 TCETargetCodeGenInfo(CodeGenTypes &CGT)
6572 : DefaultTargetCodeGenInfo(CGT) {}
6573
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006574 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07006575 CodeGen::CodeGenModule &M) const override;
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006576};
6577
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006578void TCETargetCodeGenInfo::setTargetAttributes(
6579 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006580 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006581 if (!FD) return;
6582
6583 llvm::Function *F = cast<llvm::Function>(GV);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006584
David Blaikie4e4d0842012-03-11 07:00:24 +00006585 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006586 if (FD->hasAttr<OpenCLKernelAttr>()) {
6587 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00006588 F->addFnAttr(llvm::Attribute::NoInline);
Stephen Hines651f13c2014-04-23 16:59:28 -07006589 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
6590 if (Attr) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006591 // Convert the reqd_work_group_size() attributes to metadata.
6592 llvm::LLVMContext &Context = F->getContext();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006593 llvm::NamedMDNode *OpenCLMetadata =
6594 M.getModule().getOrInsertNamedMetadata(
6595 "opencl.kernel_wg_size_info");
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006596
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006597 SmallVector<llvm::Metadata *, 5> Operands;
6598 Operands.push_back(llvm::ConstantAsMetadata::get(F));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006599
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006600 Operands.push_back(
6601 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6602 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
6603 Operands.push_back(
6604 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6605 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
6606 Operands.push_back(
6607 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6608 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006609
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006610 // Add a boolean constant operand for "required" (true) or "hint"
6611 // (false) for implementing the work_group_size_hint attr later.
6612 // Currently always true as the hint is not yet implemented.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006613 Operands.push_back(
6614 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006615 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
6616 }
6617 }
6618 }
6619}
6620
6621}
John McCallaeeb7012010-05-27 06:19:26 +00006622
Tony Linthicum96319392011-12-12 21:14:55 +00006623//===----------------------------------------------------------------------===//
6624// Hexagon ABI Implementation
6625//===----------------------------------------------------------------------===//
6626
6627namespace {
6628
6629class HexagonABIInfo : public ABIInfo {
6630
6631
6632public:
6633 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6634
6635private:
6636
6637 ABIArgInfo classifyReturnType(QualType RetTy) const;
6638 ABIArgInfo classifyArgumentType(QualType RetTy) const;
6639
Stephen Hines651f13c2014-04-23 16:59:28 -07006640 void computeInfo(CGFunctionInfo &FI) const override;
Tony Linthicum96319392011-12-12 21:14:55 +00006641
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006642 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6643 QualType Ty) const override;
Tony Linthicum96319392011-12-12 21:14:55 +00006644};
6645
6646class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
6647public:
6648 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
6649 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
6650
Stephen Hines651f13c2014-04-23 16:59:28 -07006651 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Tony Linthicum96319392011-12-12 21:14:55 +00006652 return 29;
6653 }
6654};
6655
6656}
6657
6658void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006659 if (!getCXXABI().classifyReturnType(FI))
6660 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -07006661 for (auto &I : FI.arguments())
6662 I.info = classifyArgumentType(I.type);
Tony Linthicum96319392011-12-12 21:14:55 +00006663}
6664
6665ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
6666 if (!isAggregateTypeForABI(Ty)) {
6667 // Treat an enum type as its underlying type.
6668 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6669 Ty = EnumTy->getDecl()->getIntegerType();
6670
6671 return (Ty->isPromotableIntegerType() ?
6672 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6673 }
6674
6675 // Ignore empty records.
6676 if (isEmptyRecord(getContext(), Ty, true))
6677 return ABIArgInfo::getIgnore();
6678
Mark Lacey23630722013-10-06 01:33:34 +00006679 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006680 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum96319392011-12-12 21:14:55 +00006681
6682 uint64_t Size = getContext().getTypeSize(Ty);
6683 if (Size > 64)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006684 return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
Tony Linthicum96319392011-12-12 21:14:55 +00006685 // Pass in the smallest viable integer type.
6686 else if (Size > 32)
6687 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6688 else if (Size > 16)
6689 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6690 else if (Size > 8)
6691 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6692 else
6693 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6694}
6695
6696ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
6697 if (RetTy->isVoidType())
6698 return ABIArgInfo::getIgnore();
6699
6700 // Large vector types should be returned via memory.
6701 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006702 return getNaturalAlignIndirect(RetTy);
Tony Linthicum96319392011-12-12 21:14:55 +00006703
6704 if (!isAggregateTypeForABI(RetTy)) {
6705 // Treat an enum type as its underlying type.
6706 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6707 RetTy = EnumTy->getDecl()->getIntegerType();
6708
6709 return (RetTy->isPromotableIntegerType() ?
6710 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6711 }
6712
Tony Linthicum96319392011-12-12 21:14:55 +00006713 if (isEmptyRecord(getContext(), RetTy, true))
6714 return ABIArgInfo::getIgnore();
6715
6716 // Aggregates <= 8 bytes are returned in r0; other aggregates
6717 // are returned indirectly.
6718 uint64_t Size = getContext().getTypeSize(RetTy);
6719 if (Size <= 64) {
6720 // Return in the smallest viable integer type.
6721 if (Size <= 8)
6722 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6723 if (Size <= 16)
6724 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6725 if (Size <= 32)
6726 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6727 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6728 }
6729
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006730 return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
Tony Linthicum96319392011-12-12 21:14:55 +00006731}
6732
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006733Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6734 QualType Ty) const {
6735 // FIXME: Someone needs to audit that this handle alignment correctly.
6736 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6737 getContext().getTypeInfoInChars(Ty),
6738 CharUnits::fromQuantity(4),
6739 /*AllowHigherAlign*/ true);
Tony Linthicum96319392011-12-12 21:14:55 +00006740}
6741
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006742//===----------------------------------------------------------------------===//
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07006743// Lanai ABI Implementation
6744//===----------------------------------------------------------------------===//
6745
6746namespace {
6747class LanaiABIInfo : public DefaultABIInfo {
6748public:
6749 LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
6750
6751 bool shouldUseInReg(QualType Ty, CCState &State) const;
6752
6753 void computeInfo(CGFunctionInfo &FI) const override {
6754 CCState State(FI.getCallingConvention());
6755 // Lanai uses 4 registers to pass arguments unless the function has the
6756 // regparm attribute set.
6757 if (FI.getHasRegParm()) {
6758 State.FreeRegs = FI.getRegParm();
6759 } else {
6760 State.FreeRegs = 4;
6761 }
6762
6763 if (!getCXXABI().classifyReturnType(FI))
6764 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6765 for (auto &I : FI.arguments())
6766 I.info = classifyArgumentType(I.type, State);
6767 }
6768
6769 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
6770 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
6771};
6772} // end anonymous namespace
6773
6774bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
6775 unsigned Size = getContext().getTypeSize(Ty);
6776 unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
6777
6778 if (SizeInRegs == 0)
6779 return false;
6780
6781 if (SizeInRegs > State.FreeRegs) {
6782 State.FreeRegs = 0;
6783 return false;
6784 }
6785
6786 State.FreeRegs -= SizeInRegs;
6787
6788 return true;
6789}
6790
6791ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
6792 CCState &State) const {
6793 if (!ByVal) {
6794 if (State.FreeRegs) {
6795 --State.FreeRegs; // Non-byval indirects just use one pointer.
6796 return getNaturalAlignIndirectInReg(Ty);
6797 }
6798 return getNaturalAlignIndirect(Ty, false);
6799 }
6800
6801 // Compute the byval alignment.
6802 const unsigned MinABIStackAlignInBytes = 4;
6803 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
6804 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
6805 /*Realign=*/TypeAlign >
6806 MinABIStackAlignInBytes);
6807}
6808
6809ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
6810 CCState &State) const {
6811 // Check with the C++ ABI first.
6812 const RecordType *RT = Ty->getAs<RecordType>();
6813 if (RT) {
6814 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
6815 if (RAA == CGCXXABI::RAA_Indirect) {
6816 return getIndirectResult(Ty, /*ByVal=*/false, State);
6817 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
6818 return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
6819 }
6820 }
6821
6822 if (isAggregateTypeForABI(Ty)) {
6823 // Structures with flexible arrays are always indirect.
6824 if (RT && RT->getDecl()->hasFlexibleArrayMember())
6825 return getIndirectResult(Ty, /*ByVal=*/true, State);
6826
6827 // Ignore empty structs/unions.
6828 if (isEmptyRecord(getContext(), Ty, true))
6829 return ABIArgInfo::getIgnore();
6830
6831 llvm::LLVMContext &LLVMContext = getVMContext();
6832 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
6833 if (SizeInRegs <= State.FreeRegs) {
6834 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
6835 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
6836 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
6837 State.FreeRegs -= SizeInRegs;
6838 return ABIArgInfo::getDirectInReg(Result);
6839 } else {
6840 State.FreeRegs = 0;
6841 }
6842 return getIndirectResult(Ty, true, State);
6843 }
6844
6845 // Treat an enum type as its underlying type.
6846 if (const auto *EnumTy = Ty->getAs<EnumType>())
6847 Ty = EnumTy->getDecl()->getIntegerType();
6848
6849 bool InReg = shouldUseInReg(Ty, State);
6850 if (Ty->isPromotableIntegerType()) {
6851 if (InReg)
6852 return ABIArgInfo::getDirectInReg();
6853 return ABIArgInfo::getExtend();
6854 }
6855 if (InReg)
6856 return ABIArgInfo::getDirectInReg();
6857 return ABIArgInfo::getDirect();
6858}
6859
6860namespace {
6861class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
6862public:
6863 LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
6864 : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {}
6865};
6866}
6867
6868//===----------------------------------------------------------------------===//
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006869// AMDGPU ABI Implementation
6870//===----------------------------------------------------------------------===//
6871
6872namespace {
6873
6874class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
6875public:
6876 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
6877 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006878 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006879 CodeGen::CodeGenModule &M) const override;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07006880 unsigned getOpenCLKernelCallingConv() const override;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006881};
6882
6883}
6884
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006885void AMDGPUTargetCodeGenInfo::setTargetAttributes(
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006886 const Decl *D,
6887 llvm::GlobalValue *GV,
6888 CodeGen::CodeGenModule &M) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006889 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006890 if (!FD)
6891 return;
6892
6893 if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
6894 llvm::Function *F = cast<llvm::Function>(GV);
6895 uint32_t NumVGPR = Attr->getNumVGPR();
6896 if (NumVGPR != 0)
6897 F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR));
6898 }
6899
6900 if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
6901 llvm::Function *F = cast<llvm::Function>(GV);
6902 unsigned NumSGPR = Attr->getNumSGPR();
6903 if (NumSGPR != 0)
6904 F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR));
6905 }
6906}
6907
Tony Linthicum96319392011-12-12 21:14:55 +00006908
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07006909unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
6910 return llvm::CallingConv::AMDGPU_KERNEL;
6911}
6912
6913//===----------------------------------------------------------------------===//
6914// SPARC v8 ABI Implementation.
6915// Based on the SPARC Compliance Definition version 2.4.1.
6916//
6917// Ensures that complex values are passed in registers.
6918//
6919namespace {
6920class SparcV8ABIInfo : public DefaultABIInfo {
6921public:
6922 SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
6923
6924private:
6925 ABIArgInfo classifyReturnType(QualType RetTy) const;
6926 void computeInfo(CGFunctionInfo &FI) const override;
6927};
6928} // end anonymous namespace
6929
6930
6931ABIArgInfo
6932SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
6933 if (Ty->isAnyComplexType()) {
6934 return ABIArgInfo::getDirect();
6935 }
6936 else {
6937 return DefaultABIInfo::classifyReturnType(Ty);
6938 }
6939}
6940
6941void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
6942
6943 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6944 for (auto &Arg : FI.arguments())
6945 Arg.info = classifyArgumentType(Arg.type);
6946}
6947
6948namespace {
6949class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
6950public:
6951 SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
6952 : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {}
6953};
6954} // end anonymous namespace
6955
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00006956//===----------------------------------------------------------------------===//
6957// SPARC v9 ABI Implementation.
6958// Based on the SPARC Compliance Definition version 2.4.1.
6959//
6960// Function arguments a mapped to a nominal "parameter array" and promoted to
6961// registers depending on their type. Each argument occupies 8 or 16 bytes in
6962// the array, structs larger than 16 bytes are passed indirectly.
6963//
6964// One case requires special care:
6965//
6966// struct mixed {
6967// int i;
6968// float f;
6969// };
6970//
6971// When a struct mixed is passed by value, it only occupies 8 bytes in the
6972// parameter array, but the int is passed in an integer register, and the float
6973// is passed in a floating point register. This is represented as two arguments
6974// with the LLVM IR inreg attribute:
6975//
6976// declare void f(i32 inreg %i, float inreg %f)
6977//
6978// The code generator will only allocate 4 bytes from the parameter array for
6979// the inreg arguments. All other arguments are allocated a multiple of 8
6980// bytes.
6981//
6982namespace {
6983class SparcV9ABIInfo : public ABIInfo {
6984public:
6985 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6986
6987private:
6988 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
Stephen Hines651f13c2014-04-23 16:59:28 -07006989 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006990 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6991 QualType Ty) const override;
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00006992
6993 // Coercion type builder for structs passed in registers. The coercion type
6994 // serves two purposes:
6995 //
6996 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
6997 // in registers.
6998 // 2. Expose aligned floating point elements as first-level elements, so the
6999 // code generator knows to pass them in floating point registers.
7000 //
7001 // We also compute the InReg flag which indicates that the struct contains
7002 // aligned 32-bit floats.
7003 //
7004 struct CoerceBuilder {
7005 llvm::LLVMContext &Context;
7006 const llvm::DataLayout &DL;
7007 SmallVector<llvm::Type*, 8> Elems;
7008 uint64_t Size;
7009 bool InReg;
7010
7011 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
7012 : Context(c), DL(dl), Size(0), InReg(false) {}
7013
7014 // Pad Elems with integers until Size is ToSize.
7015 void pad(uint64_t ToSize) {
7016 assert(ToSize >= Size && "Cannot remove elements");
7017 if (ToSize == Size)
7018 return;
7019
7020 // Finish the current 64-bit word.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007021 uint64_t Aligned = llvm::alignTo(Size, 64);
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00007022 if (Aligned > Size && Aligned <= ToSize) {
7023 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
7024 Size = Aligned;
7025 }
7026
7027 // Add whole 64-bit words.
7028 while (Size + 64 <= ToSize) {
7029 Elems.push_back(llvm::Type::getInt64Ty(Context));
7030 Size += 64;
7031 }
7032
7033 // Final in-word padding.
7034 if (Size < ToSize) {
7035 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
7036 Size = ToSize;
7037 }
7038 }
7039
7040 // Add a floating point element at Offset.
7041 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
7042 // Unaligned floats are treated as integers.
7043 if (Offset % Bits)
7044 return;
7045 // The InReg flag is only required if there are any floats < 64 bits.
7046 if (Bits < 64)
7047 InReg = true;
7048 pad(Offset);
7049 Elems.push_back(Ty);
7050 Size = Offset + Bits;
7051 }
7052
7053 // Add a struct type to the coercion type, starting at Offset (in bits).
7054 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
7055 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
7056 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
7057 llvm::Type *ElemTy = StrTy->getElementType(i);
7058 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
7059 switch (ElemTy->getTypeID()) {
7060 case llvm::Type::StructTyID:
7061 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
7062 break;
7063 case llvm::Type::FloatTyID:
7064 addFloat(ElemOffset, ElemTy, 32);
7065 break;
7066 case llvm::Type::DoubleTyID:
7067 addFloat(ElemOffset, ElemTy, 64);
7068 break;
7069 case llvm::Type::FP128TyID:
7070 addFloat(ElemOffset, ElemTy, 128);
7071 break;
7072 case llvm::Type::PointerTyID:
7073 if (ElemOffset % 64 == 0) {
7074 pad(ElemOffset);
7075 Elems.push_back(ElemTy);
7076 Size += 64;
7077 }
7078 break;
7079 default:
7080 break;
7081 }
7082 }
7083 }
7084
7085 // Check if Ty is a usable substitute for the coercion type.
7086 bool isUsableType(llvm::StructType *Ty) const {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07007087 return llvm::makeArrayRef(Elems) == Ty->elements();
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00007088 }
7089
7090 // Get the coercion type as a literal struct type.
7091 llvm::Type *getType() const {
7092 if (Elems.size() == 1)
7093 return Elems.front();
7094 else
7095 return llvm::StructType::get(Context, Elems);
7096 }
7097 };
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007098};
7099} // end anonymous namespace
7100
7101ABIArgInfo
7102SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
7103 if (Ty->isVoidType())
7104 return ABIArgInfo::getIgnore();
7105
7106 uint64_t Size = getContext().getTypeSize(Ty);
7107
7108 // Anything too big to fit in registers is passed with an explicit indirect
7109 // pointer / sret pointer.
7110 if (Size > SizeLimit)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007111 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007112
7113 // Treat an enum type as its underlying type.
7114 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7115 Ty = EnumTy->getDecl()->getIntegerType();
7116
7117 // Integer types smaller than a register are extended.
7118 if (Size < 64 && Ty->isIntegerType())
7119 return ABIArgInfo::getExtend();
7120
7121 // Other non-aggregates go in registers.
7122 if (!isAggregateTypeForABI(Ty))
7123 return ABIArgInfo::getDirect();
7124
Stephen Hines651f13c2014-04-23 16:59:28 -07007125 // If a C++ object has either a non-trivial copy constructor or a non-trivial
7126 // destructor, it is passed with an explicit indirect pointer / sret pointer.
7127 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007128 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Stephen Hines651f13c2014-04-23 16:59:28 -07007129
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007130 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00007131 // Build a coercion type from the LLVM struct type.
7132 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
7133 if (!StrTy)
7134 return ABIArgInfo::getDirect();
7135
7136 CoerceBuilder CB(getVMContext(), getDataLayout());
7137 CB.addStruct(0, StrTy);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007138 CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00007139
7140 // Try to use the original type for coercion.
7141 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
7142
7143 if (CB.InReg)
7144 return ABIArgInfo::getDirectInReg(CoerceTy);
7145 else
7146 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007147}
7148
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007149Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7150 QualType Ty) const {
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007151 ABIArgInfo AI = classifyType(Ty, 16 * 8);
7152 llvm::Type *ArgTy = CGT.ConvertType(Ty);
7153 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
7154 AI.setCoerceToType(ArgTy);
7155
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007156 CharUnits SlotSize = CharUnits::fromQuantity(8);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007157
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007158 CGBuilderTy &Builder = CGF.Builder;
7159 Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
7160 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
7161
7162 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
7163
7164 Address ArgAddr = Address::invalid();
7165 CharUnits Stride;
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007166 switch (AI.getKind()) {
7167 case ABIArgInfo::Expand:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007168 case ABIArgInfo::CoerceAndExpand:
Stephen Hines651f13c2014-04-23 16:59:28 -07007169 case ABIArgInfo::InAlloca:
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007170 llvm_unreachable("Unsupported ABI kind for va_arg");
7171
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007172 case ABIArgInfo::Extend: {
7173 Stride = SlotSize;
7174 CharUnits Offset = SlotSize - TypeInfo.first;
7175 ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007176 break;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007177 }
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007178
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007179 case ABIArgInfo::Direct: {
7180 auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007181 Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007182 ArgAddr = Addr;
7183 break;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007184 }
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007185
7186 case ABIArgInfo::Indirect:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007187 Stride = SlotSize;
7188 ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
7189 ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
7190 TypeInfo.second);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007191 break;
7192
7193 case ABIArgInfo::Ignore:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007194 return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007195 }
7196
7197 // Update VAList.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007198 llvm::Value *NextPtr =
7199 Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next");
7200 Builder.CreateStore(NextPtr, VAListAddr);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007201
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007202 return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007203}
7204
7205void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
7206 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
Stephen Hines651f13c2014-04-23 16:59:28 -07007207 for (auto &I : FI.arguments())
7208 I.info = classifyType(I.type, 16 * 8);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007209}
7210
7211namespace {
7212class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
7213public:
7214 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
7215 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
Stephen Hines651f13c2014-04-23 16:59:28 -07007216
7217 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
7218 return 14;
7219 }
7220
7221 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7222 llvm::Value *Address) const override;
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007223};
7224} // end anonymous namespace
7225
Stephen Hines651f13c2014-04-23 16:59:28 -07007226bool
7227SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7228 llvm::Value *Address) const {
7229 // This is calculated from the LLVM and GCC tables and verified
7230 // against gcc output. AFAIK all ABIs use the same encoding.
7231
7232 CodeGen::CGBuilderTy &Builder = CGF.Builder;
7233
7234 llvm::IntegerType *i8 = CGF.Int8Ty;
7235 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
7236 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
7237
7238 // 0-31: the 8-byte general-purpose registers
7239 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
7240
7241 // 32-63: f0-31, the 4-byte floating-point registers
7242 AssignToArrayRange(Builder, Address, Four8, 32, 63);
7243
7244 // Y = 64
7245 // PSR = 65
7246 // WIM = 66
7247 // TBR = 67
7248 // PC = 68
7249 // NPC = 69
7250 // FSR = 70
7251 // CSR = 71
7252 AssignToArrayRange(Builder, Address, Eight8, 64, 71);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07007253
Stephen Hines651f13c2014-04-23 16:59:28 -07007254 // 72-87: d0-15, the 8-byte floating-point registers
7255 AssignToArrayRange(Builder, Address, Eight8, 72, 87);
7256
7257 return false;
7258}
7259
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007260
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007261//===----------------------------------------------------------------------===//
Stephen Hines651f13c2014-04-23 16:59:28 -07007262// XCore ABI Implementation
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007263//===----------------------------------------------------------------------===//
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007264
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007265namespace {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007266
7267/// A SmallStringEnc instance is used to build up the TypeString by passing
7268/// it by reference between functions that append to it.
7269typedef llvm::SmallString<128> SmallStringEnc;
7270
7271/// TypeStringCache caches the meta encodings of Types.
7272///
7273/// The reason for caching TypeStrings is two fold:
7274/// 1. To cache a type's encoding for later uses;
7275/// 2. As a means to break recursive member type inclusion.
7276///
7277/// A cache Entry can have a Status of:
7278/// NonRecursive: The type encoding is not recursive;
7279/// Recursive: The type encoding is recursive;
7280/// Incomplete: An incomplete TypeString;
7281/// IncompleteUsed: An incomplete TypeString that has been used in a
7282/// Recursive type encoding.
7283///
7284/// A NonRecursive entry will have all of its sub-members expanded as fully
7285/// as possible. Whilst it may contain types which are recursive, the type
7286/// itself is not recursive and thus its encoding may be safely used whenever
7287/// the type is encountered.
7288///
7289/// A Recursive entry will have all of its sub-members expanded as fully as
7290/// possible. The type itself is recursive and it may contain other types which
7291/// are recursive. The Recursive encoding must not be used during the expansion
7292/// of a recursive type's recursive branch. For simplicity the code uses
7293/// IncompleteCount to reject all usage of Recursive encodings for member types.
7294///
7295/// An Incomplete entry is always a RecordType and only encodes its
7296/// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
7297/// are placed into the cache during type expansion as a means to identify and
7298/// handle recursive inclusion of types as sub-members. If there is recursion
7299/// the entry becomes IncompleteUsed.
7300///
7301/// During the expansion of a RecordType's members:
7302///
7303/// If the cache contains a NonRecursive encoding for the member type, the
7304/// cached encoding is used;
7305///
7306/// If the cache contains a Recursive encoding for the member type, the
7307/// cached encoding is 'Swapped' out, as it may be incorrect, and...
7308///
7309/// If the member is a RecordType, an Incomplete encoding is placed into the
7310/// cache to break potential recursive inclusion of itself as a sub-member;
7311///
7312/// Once a member RecordType has been expanded, its temporary incomplete
7313/// entry is removed from the cache. If a Recursive encoding was swapped out
7314/// it is swapped back in;
7315///
7316/// If an incomplete entry is used to expand a sub-member, the incomplete
7317/// entry is marked as IncompleteUsed. The cache keeps count of how many
7318/// IncompleteUsed entries it currently contains in IncompleteUsedCount;
7319///
7320/// If a member's encoding is found to be a NonRecursive or Recursive viz:
7321/// IncompleteUsedCount==0, the member's encoding is added to the cache.
7322/// Else the member is part of a recursive type and thus the recursion has
7323/// been exited too soon for the encoding to be correct for the member.
7324///
7325class TypeStringCache {
7326 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
7327 struct Entry {
7328 std::string Str; // The encoded TypeString for the type.
7329 enum Status State; // Information about the encoding in 'Str'.
7330 std::string Swapped; // A temporary place holder for a Recursive encoding
7331 // during the expansion of RecordType's members.
7332 };
7333 std::map<const IdentifierInfo *, struct Entry> Map;
7334 unsigned IncompleteCount; // Number of Incomplete entries in the Map.
7335 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
7336public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007337 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007338 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
7339 bool removeIncomplete(const IdentifierInfo *ID);
7340 void addIfComplete(const IdentifierInfo *ID, StringRef Str,
7341 bool IsRecursive);
7342 StringRef lookupStr(const IdentifierInfo *ID);
7343};
7344
7345/// TypeString encodings for enum & union fields must be order.
7346/// FieldEncoding is a helper for this ordering process.
7347class FieldEncoding {
7348 bool HasName;
7349 std::string Enc;
7350public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007351 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
7352 StringRef str() {return Enc.c_str();}
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007353 bool operator<(const FieldEncoding &rhs) const {
7354 if (HasName != rhs.HasName) return HasName;
7355 return Enc < rhs.Enc;
7356 }
7357};
7358
Robert Lytton276c2892013-08-19 09:46:39 +00007359class XCoreABIInfo : public DefaultABIInfo {
7360public:
7361 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007362 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7363 QualType Ty) const override;
Robert Lytton276c2892013-08-19 09:46:39 +00007364};
7365
Stephen Hines651f13c2014-04-23 16:59:28 -07007366class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007367 mutable TypeStringCache TSC;
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007368public:
Stephen Hines651f13c2014-04-23 16:59:28 -07007369 XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
Robert Lytton276c2892013-08-19 09:46:39 +00007370 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007371 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7372 CodeGen::CodeGenModule &M) const override;
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007373};
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007374
Robert Lytton645e6fd2013-10-11 10:29:34 +00007375} // End anonymous namespace.
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007376
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007377// TODO: this implementation is likely now redundant with the default
7378// EmitVAArg.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007379Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7380 QualType Ty) const {
Robert Lytton276c2892013-08-19 09:46:39 +00007381 CGBuilderTy &Builder = CGF.Builder;
Robert Lytton276c2892013-08-19 09:46:39 +00007382
Robert Lytton645e6fd2013-10-11 10:29:34 +00007383 // Get the VAList.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007384 CharUnits SlotSize = CharUnits::fromQuantity(4);
7385 Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
Robert Lytton276c2892013-08-19 09:46:39 +00007386
Robert Lytton645e6fd2013-10-11 10:29:34 +00007387 // Handle the argument.
7388 ABIArgInfo AI = classifyArgumentType(Ty);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007389 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
Robert Lytton645e6fd2013-10-11 10:29:34 +00007390 llvm::Type *ArgTy = CGT.ConvertType(Ty);
7391 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
7392 AI.setCoerceToType(ArgTy);
Robert Lytton276c2892013-08-19 09:46:39 +00007393 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007394
7395 Address Val = Address::invalid();
7396 CharUnits ArgSize = CharUnits::Zero();
Robert Lytton276c2892013-08-19 09:46:39 +00007397 switch (AI.getKind()) {
Robert Lytton276c2892013-08-19 09:46:39 +00007398 case ABIArgInfo::Expand:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007399 case ABIArgInfo::CoerceAndExpand:
Stephen Hines651f13c2014-04-23 16:59:28 -07007400 case ABIArgInfo::InAlloca:
Robert Lytton276c2892013-08-19 09:46:39 +00007401 llvm_unreachable("Unsupported ABI kind for va_arg");
7402 case ABIArgInfo::Ignore:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007403 Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
7404 ArgSize = CharUnits::Zero();
Robert Lytton645e6fd2013-10-11 10:29:34 +00007405 break;
Robert Lytton276c2892013-08-19 09:46:39 +00007406 case ABIArgInfo::Extend:
7407 case ABIArgInfo::Direct:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007408 Val = Builder.CreateBitCast(AP, ArgPtrTy);
7409 ArgSize = CharUnits::fromQuantity(
7410 getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007411 ArgSize = ArgSize.alignTo(SlotSize);
Robert Lytton645e6fd2013-10-11 10:29:34 +00007412 break;
Robert Lytton276c2892013-08-19 09:46:39 +00007413 case ABIArgInfo::Indirect:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007414 Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
7415 Val = Address(Builder.CreateLoad(Val), TypeAlign);
7416 ArgSize = SlotSize;
Robert Lytton645e6fd2013-10-11 10:29:34 +00007417 break;
Robert Lytton276c2892013-08-19 09:46:39 +00007418 }
Robert Lytton645e6fd2013-10-11 10:29:34 +00007419
7420 // Increment the VAList.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007421 if (!ArgSize.isZero()) {
7422 llvm::Value *APN =
7423 Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize);
7424 Builder.CreateStore(APN, VAListAddr);
Robert Lytton645e6fd2013-10-11 10:29:34 +00007425 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007426
Robert Lytton645e6fd2013-10-11 10:29:34 +00007427 return Val;
Robert Lytton276c2892013-08-19 09:46:39 +00007428}
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007429
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007430/// During the expansion of a RecordType, an incomplete TypeString is placed
7431/// into the cache as a means to identify and break recursion.
7432/// If there is a Recursive encoding in the cache, it is swapped out and will
7433/// be reinserted by removeIncomplete().
7434/// All other types of encoding should have been used rather than arriving here.
7435void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
7436 std::string StubEnc) {
7437 if (!ID)
7438 return;
7439 Entry &E = Map[ID];
7440 assert( (E.Str.empty() || E.State == Recursive) &&
7441 "Incorrectly use of addIncomplete");
7442 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
7443 E.Swapped.swap(E.Str); // swap out the Recursive
7444 E.Str.swap(StubEnc);
7445 E.State = Incomplete;
7446 ++IncompleteCount;
7447}
7448
7449/// Once the RecordType has been expanded, the temporary incomplete TypeString
7450/// must be removed from the cache.
7451/// If a Recursive was swapped out by addIncomplete(), it will be replaced.
7452/// Returns true if the RecordType was defined recursively.
7453bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
7454 if (!ID)
7455 return false;
7456 auto I = Map.find(ID);
7457 assert(I != Map.end() && "Entry not present");
7458 Entry &E = I->second;
7459 assert( (E.State == Incomplete ||
7460 E.State == IncompleteUsed) &&
7461 "Entry must be an incomplete type");
7462 bool IsRecursive = false;
7463 if (E.State == IncompleteUsed) {
7464 // We made use of our Incomplete encoding, thus we are recursive.
7465 IsRecursive = true;
7466 --IncompleteUsedCount;
7467 }
7468 if (E.Swapped.empty())
7469 Map.erase(I);
7470 else {
7471 // Swap the Recursive back.
7472 E.Swapped.swap(E.Str);
7473 E.Swapped.clear();
7474 E.State = Recursive;
7475 }
7476 --IncompleteCount;
7477 return IsRecursive;
7478}
7479
7480/// Add the encoded TypeString to the cache only if it is NonRecursive or
7481/// Recursive (viz: all sub-members were expanded as fully as possible).
7482void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
7483 bool IsRecursive) {
7484 if (!ID || IncompleteUsedCount)
7485 return; // No key or it is is an incomplete sub-type so don't add.
7486 Entry &E = Map[ID];
7487 if (IsRecursive && !E.Str.empty()) {
7488 assert(E.State==Recursive && E.Str.size() == Str.size() &&
7489 "This is not the same Recursive entry");
7490 // The parent container was not recursive after all, so we could have used
7491 // this Recursive sub-member entry after all, but we assumed the worse when
7492 // we started viz: IncompleteCount!=0.
7493 return;
7494 }
7495 assert(E.Str.empty() && "Entry already present");
7496 E.Str = Str.str();
7497 E.State = IsRecursive? Recursive : NonRecursive;
7498}
7499
7500/// Return a cached TypeString encoding for the ID. If there isn't one, or we
7501/// are recursively expanding a type (IncompleteCount != 0) and the cached
7502/// encoding is Recursive, return an empty StringRef.
7503StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
7504 if (!ID)
7505 return StringRef(); // We have no key.
7506 auto I = Map.find(ID);
7507 if (I == Map.end())
7508 return StringRef(); // We have no encoding.
7509 Entry &E = I->second;
7510 if (E.State == Recursive && IncompleteCount)
7511 return StringRef(); // We don't use Recursive encodings for member types.
7512
7513 if (E.State == Incomplete) {
7514 // The incomplete type is being used to break out of recursion.
7515 E.State = IncompleteUsed;
7516 ++IncompleteUsedCount;
7517 }
7518 return E.Str.c_str();
7519}
7520
7521/// The XCore ABI includes a type information section that communicates symbol
7522/// type information to the linker. The linker uses this information to verify
7523/// safety/correctness of things such as array bound and pointers et al.
7524/// The ABI only requires C (and XC) language modules to emit TypeStrings.
7525/// This type information (TypeString) is emitted into meta data for all global
7526/// symbols: definitions, declarations, functions & variables.
7527///
7528/// The TypeString carries type, qualifier, name, size & value details.
7529/// Please see 'Tools Development Guide' section 2.16.2 for format details:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07007530/// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007531/// The output is tested by test/CodeGen/xcore-stringtype.c.
7532///
7533static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7534 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
7535
7536/// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
7537void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7538 CodeGen::CodeGenModule &CGM) const {
7539 SmallStringEnc Enc;
7540 if (getTypeString(Enc, D, CGM, TSC)) {
7541 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007542 llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
7543 llvm::MDString::get(Ctx, Enc.str())};
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007544 llvm::NamedMDNode *MD =
7545 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
7546 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
7547 }
7548}
7549
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007550//===----------------------------------------------------------------------===//
7551// SPIR ABI Implementation
7552//===----------------------------------------------------------------------===//
7553
7554namespace {
7555class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
7556public:
7557 SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
7558 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
7559 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7560 CodeGen::CodeGenModule &M) const override;
7561 unsigned getOpenCLKernelCallingConv() const override;
7562};
7563} // End anonymous namespace.
7564
7565/// Emit SPIR specific metadata: OpenCL and SPIR version.
7566void SPIRTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7567 CodeGen::CodeGenModule &CGM) const {
7568 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
7569 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx);
7570 llvm::Module &M = CGM.getModule();
7571 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
7572 // opencl.spir.version named metadata.
7573 llvm::Metadata *SPIRVerElts[] = {
7574 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 2)),
7575 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 0))};
7576 llvm::NamedMDNode *SPIRVerMD =
7577 M.getOrInsertNamedMetadata("opencl.spir.version");
7578 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
7579 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
7580 // opencl.ocl.version named metadata node.
7581 llvm::Metadata *OCLVerElts[] = {
7582 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
7583 Int32Ty, CGM.getLangOpts().OpenCLVersion / 100)),
7584 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
7585 Int32Ty, (CGM.getLangOpts().OpenCLVersion % 100) / 10))};
7586 llvm::NamedMDNode *OCLVerMD =
7587 M.getOrInsertNamedMetadata("opencl.ocl.version");
7588 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
7589}
7590
7591unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
7592 return llvm::CallingConv::SPIR_KERNEL;
7593}
7594
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007595static bool appendType(SmallStringEnc &Enc, QualType QType,
7596 const CodeGen::CodeGenModule &CGM,
7597 TypeStringCache &TSC);
7598
7599/// Helper function for appendRecordType().
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07007600/// Builds a SmallVector containing the encoded field types in declaration
7601/// order.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007602static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
7603 const RecordDecl *RD,
7604 const CodeGen::CodeGenModule &CGM,
7605 TypeStringCache &TSC) {
Stephen Hines176edba2014-12-01 14:53:08 -08007606 for (const auto *Field : RD->fields()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007607 SmallStringEnc Enc;
7608 Enc += "m(";
Stephen Hines176edba2014-12-01 14:53:08 -08007609 Enc += Field->getName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007610 Enc += "){";
Stephen Hines176edba2014-12-01 14:53:08 -08007611 if (Field->isBitField()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007612 Enc += "b(";
7613 llvm::raw_svector_ostream OS(Enc);
Stephen Hines176edba2014-12-01 14:53:08 -08007614 OS << Field->getBitWidthValue(CGM.getContext());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007615 Enc += ':';
7616 }
Stephen Hines176edba2014-12-01 14:53:08 -08007617 if (!appendType(Enc, Field->getType(), CGM, TSC))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007618 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08007619 if (Field->isBitField())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007620 Enc += ')';
7621 Enc += '}';
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07007622 FE.emplace_back(!Field->getName().empty(), Enc);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007623 }
7624 return true;
7625}
7626
7627/// Appends structure and union types to Enc and adds encoding to cache.
7628/// Recursively calls appendType (via extractFieldType) for each field.
7629/// Union types have their fields ordered according to the ABI.
7630static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
7631 const CodeGen::CodeGenModule &CGM,
7632 TypeStringCache &TSC, const IdentifierInfo *ID) {
7633 // Append the cached TypeString if we have one.
7634 StringRef TypeString = TSC.lookupStr(ID);
7635 if (!TypeString.empty()) {
7636 Enc += TypeString;
7637 return true;
7638 }
7639
7640 // Start to emit an incomplete TypeString.
7641 size_t Start = Enc.size();
7642 Enc += (RT->isUnionType()? 'u' : 's');
7643 Enc += '(';
7644 if (ID)
7645 Enc += ID->getName();
7646 Enc += "){";
7647
7648 // We collect all encoded fields and order as necessary.
7649 bool IsRecursive = false;
7650 const RecordDecl *RD = RT->getDecl()->getDefinition();
7651 if (RD && !RD->field_empty()) {
7652 // An incomplete TypeString stub is placed in the cache for this RecordType
7653 // so that recursive calls to this RecordType will use it whilst building a
7654 // complete TypeString for this RecordType.
7655 SmallVector<FieldEncoding, 16> FE;
7656 std::string StubEnc(Enc.substr(Start).str());
7657 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
7658 TSC.addIncomplete(ID, std::move(StubEnc));
7659 if (!extractFieldType(FE, RD, CGM, TSC)) {
7660 (void) TSC.removeIncomplete(ID);
7661 return false;
7662 }
7663 IsRecursive = TSC.removeIncomplete(ID);
7664 // The ABI requires unions to be sorted but not structures.
7665 // See FieldEncoding::operator< for sort algorithm.
7666 if (RT->isUnionType())
7667 std::sort(FE.begin(), FE.end());
7668 // We can now complete the TypeString.
7669 unsigned E = FE.size();
7670 for (unsigned I = 0; I != E; ++I) {
7671 if (I)
7672 Enc += ',';
7673 Enc += FE[I].str();
7674 }
7675 }
7676 Enc += '}';
7677 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
7678 return true;
7679}
7680
7681/// Appends enum types to Enc and adds the encoding to the cache.
7682static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
7683 TypeStringCache &TSC,
7684 const IdentifierInfo *ID) {
7685 // Append the cached TypeString if we have one.
7686 StringRef TypeString = TSC.lookupStr(ID);
7687 if (!TypeString.empty()) {
7688 Enc += TypeString;
7689 return true;
7690 }
7691
7692 size_t Start = Enc.size();
7693 Enc += "e(";
7694 if (ID)
7695 Enc += ID->getName();
7696 Enc += "){";
7697
7698 // We collect all encoded enumerations and order them alphanumerically.
7699 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
7700 SmallVector<FieldEncoding, 16> FE;
7701 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
7702 ++I) {
7703 SmallStringEnc EnumEnc;
7704 EnumEnc += "m(";
7705 EnumEnc += I->getName();
7706 EnumEnc += "){";
7707 I->getInitVal().toString(EnumEnc);
7708 EnumEnc += '}';
7709 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
7710 }
7711 std::sort(FE.begin(), FE.end());
7712 unsigned E = FE.size();
7713 for (unsigned I = 0; I != E; ++I) {
7714 if (I)
7715 Enc += ',';
7716 Enc += FE[I].str();
7717 }
7718 }
7719 Enc += '}';
7720 TSC.addIfComplete(ID, Enc.substr(Start), false);
7721 return true;
7722}
7723
7724/// Appends type's qualifier to Enc.
7725/// This is done prior to appending the type's encoding.
7726static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
7727 // Qualifiers are emitted in alphabetical order.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007728 static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007729 int Lookup = 0;
7730 if (QT.isConstQualified())
7731 Lookup += 1<<0;
7732 if (QT.isRestrictQualified())
7733 Lookup += 1<<1;
7734 if (QT.isVolatileQualified())
7735 Lookup += 1<<2;
7736 Enc += Table[Lookup];
7737}
7738
7739/// Appends built-in types to Enc.
7740static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
7741 const char *EncType;
7742 switch (BT->getKind()) {
7743 case BuiltinType::Void:
7744 EncType = "0";
7745 break;
7746 case BuiltinType::Bool:
7747 EncType = "b";
7748 break;
7749 case BuiltinType::Char_U:
7750 EncType = "uc";
7751 break;
7752 case BuiltinType::UChar:
7753 EncType = "uc";
7754 break;
7755 case BuiltinType::SChar:
7756 EncType = "sc";
7757 break;
7758 case BuiltinType::UShort:
7759 EncType = "us";
7760 break;
7761 case BuiltinType::Short:
7762 EncType = "ss";
7763 break;
7764 case BuiltinType::UInt:
7765 EncType = "ui";
7766 break;
7767 case BuiltinType::Int:
7768 EncType = "si";
7769 break;
7770 case BuiltinType::ULong:
7771 EncType = "ul";
7772 break;
7773 case BuiltinType::Long:
7774 EncType = "sl";
7775 break;
7776 case BuiltinType::ULongLong:
7777 EncType = "ull";
7778 break;
7779 case BuiltinType::LongLong:
7780 EncType = "sll";
7781 break;
7782 case BuiltinType::Float:
7783 EncType = "ft";
7784 break;
7785 case BuiltinType::Double:
7786 EncType = "d";
7787 break;
7788 case BuiltinType::LongDouble:
7789 EncType = "ld";
7790 break;
7791 default:
7792 return false;
7793 }
7794 Enc += EncType;
7795 return true;
7796}
7797
7798/// Appends a pointer encoding to Enc before calling appendType for the pointee.
7799static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
7800 const CodeGen::CodeGenModule &CGM,
7801 TypeStringCache &TSC) {
7802 Enc += "p(";
7803 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
7804 return false;
7805 Enc += ')';
7806 return true;
7807}
7808
7809/// Appends array encoding to Enc before calling appendType for the element.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007810static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
7811 const ArrayType *AT,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007812 const CodeGen::CodeGenModule &CGM,
7813 TypeStringCache &TSC, StringRef NoSizeEnc) {
7814 if (AT->getSizeModifier() != ArrayType::Normal)
7815 return false;
7816 Enc += "a(";
7817 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
7818 CAT->getSize().toStringUnsigned(Enc);
7819 else
7820 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
7821 Enc += ':';
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007822 // The Qualifiers should be attached to the type rather than the array.
7823 appendQualifier(Enc, QT);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007824 if (!appendType(Enc, AT->getElementType(), CGM, TSC))
7825 return false;
7826 Enc += ')';
7827 return true;
7828}
7829
7830/// Appends a function encoding to Enc, calling appendType for the return type
7831/// and the arguments.
7832static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
7833 const CodeGen::CodeGenModule &CGM,
7834 TypeStringCache &TSC) {
7835 Enc += "f{";
7836 if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
7837 return false;
7838 Enc += "}(";
7839 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
7840 // N.B. we are only interested in the adjusted param types.
7841 auto I = FPT->param_type_begin();
7842 auto E = FPT->param_type_end();
7843 if (I != E) {
7844 do {
7845 if (!appendType(Enc, *I, CGM, TSC))
7846 return false;
7847 ++I;
7848 if (I != E)
7849 Enc += ',';
7850 } while (I != E);
7851 if (FPT->isVariadic())
7852 Enc += ",va";
7853 } else {
7854 if (FPT->isVariadic())
7855 Enc += "va";
7856 else
7857 Enc += '0';
7858 }
7859 }
7860 Enc += ')';
7861 return true;
7862}
7863
7864/// Handles the type's qualifier before dispatching a call to handle specific
7865/// type encodings.
7866static bool appendType(SmallStringEnc &Enc, QualType QType,
7867 const CodeGen::CodeGenModule &CGM,
7868 TypeStringCache &TSC) {
7869
7870 QualType QT = QType.getCanonicalType();
7871
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007872 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
7873 // The Qualifiers should be attached to the type rather than the array.
7874 // Thus we don't call appendQualifier() here.
7875 return appendArrayType(Enc, QT, AT, CGM, TSC, "");
7876
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007877 appendQualifier(Enc, QT);
7878
7879 if (const BuiltinType *BT = QT->getAs<BuiltinType>())
7880 return appendBuiltinType(Enc, BT);
7881
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007882 if (const PointerType *PT = QT->getAs<PointerType>())
7883 return appendPointerType(Enc, PT, CGM, TSC);
7884
7885 if (const EnumType *ET = QT->getAs<EnumType>())
7886 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
7887
7888 if (const RecordType *RT = QT->getAsStructureType())
7889 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7890
7891 if (const RecordType *RT = QT->getAsUnionType())
7892 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7893
7894 if (const FunctionType *FT = QT->getAs<FunctionType>())
7895 return appendFunctionType(Enc, FT, CGM, TSC);
7896
7897 return false;
7898}
7899
7900static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7901 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
7902 if (!D)
7903 return false;
7904
7905 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7906 if (FD->getLanguageLinkage() != CLanguageLinkage)
7907 return false;
7908 return appendType(Enc, FD->getType(), CGM, TSC);
7909 }
7910
7911 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
7912 if (VD->getLanguageLinkage() != CLanguageLinkage)
7913 return false;
7914 QualType QT = VD->getType().getCanonicalType();
7915 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
7916 // Global ArrayTypes are given a size of '*' if the size is unknown.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007917 // The Qualifiers should be attached to the type rather than the array.
7918 // Thus we don't call appendQualifier() here.
7919 return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007920 }
7921 return appendType(Enc, QT, CGM, TSC);
7922 }
7923 return false;
7924}
7925
7926
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007927//===----------------------------------------------------------------------===//
7928// Driver code
7929//===----------------------------------------------------------------------===//
7930
Stephen Hines176edba2014-12-01 14:53:08 -08007931const llvm::Triple &CodeGenModule::getTriple() const {
7932 return getTarget().getTriple();
7933}
7934
7935bool CodeGenModule::supportsCOMDAT() const {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007936 return getTriple().supportsCOMDAT();
Stephen Hines176edba2014-12-01 14:53:08 -08007937}
7938
Chris Lattnerea044322010-07-29 02:01:43 +00007939const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00007940 if (TheTargetCodeGenInfo)
7941 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00007942
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007943 // Helper to set the unique_ptr while still keeping the return value.
7944 auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
7945 this->TheTargetCodeGenInfo.reset(P);
7946 return *P;
7947 };
7948
John McCall64aa4b32013-04-16 22:48:15 +00007949 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00007950 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00007951 default:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007952 return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00007953
Derek Schuff9ed63f82012-09-06 17:37:28 +00007954 case llvm::Triple::le32:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007955 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00007956 case llvm::Triple::mips:
7957 case llvm::Triple::mipsel:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007958 if (Triple.getOS() == llvm::Triple::NaCl)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007959 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
7960 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00007961
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00007962 case llvm::Triple::mips64:
7963 case llvm::Triple::mips64el:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007964 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00007965
Tim Northoverc264e162013-01-31 12:13:10 +00007966 case llvm::Triple::aarch64:
Stephen Hines176edba2014-12-01 14:53:08 -08007967 case llvm::Triple::aarch64_be: {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007968 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007969 if (getTarget().getABI() == "darwinpcs")
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007970 Kind = AArch64ABIInfo::DarwinPCS;
7971
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007972 return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007973 }
Tim Northoverc264e162013-01-31 12:13:10 +00007974
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007975 case llvm::Triple::wasm32:
7976 case llvm::Triple::wasm64:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007977 return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007978
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00007979 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07007980 case llvm::Triple::armeb:
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00007981 case llvm::Triple::thumb:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007982 case llvm::Triple::thumbeb: {
7983 if (Triple.getOS() == llvm::Triple::Win32) {
7984 return SetCGInfo(
7985 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
Sandeep Patel34c1af82011-04-05 00:23:47 +00007986 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00007987
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007988 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
7989 StringRef ABIStr = getTarget().getABI();
7990 if (ABIStr == "apcs-gnu")
7991 Kind = ARMABIInfo::APCS;
7992 else if (ABIStr == "aapcs16")
7993 Kind = ARMABIInfo::AAPCS16_VFP;
7994 else if (CodeGenOpts.FloatABI == "hard" ||
7995 (CodeGenOpts.FloatABI != "soft" &&
7996 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
7997 Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
7998 Triple.getEnvironment() == llvm::Triple::EABIHF)))
7999 Kind = ARMABIInfo::AAPCS_VFP;
8000
8001 return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
8002 }
8003
John McCallec853ba2010-03-11 00:10:12 +00008004 case llvm::Triple::ppc:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008005 return SetCGInfo(
8006 new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft"));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00008007 case llvm::Triple::ppc64:
Stephen Hines176edba2014-12-01 14:53:08 -08008008 if (Triple.isOSBinFormatELF()) {
8009 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
8010 if (getTarget().getABI() == "elfv2")
8011 Kind = PPC64_SVR4_ABIInfo::ELFv2;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07008012 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Stephen Hines176edba2014-12-01 14:53:08 -08008013
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008014 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
Stephen Hines176edba2014-12-01 14:53:08 -08008015 } else
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008016 return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
Stephen Hines176edba2014-12-01 14:53:08 -08008017 case llvm::Triple::ppc64le: {
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00008018 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
Stephen Hines176edba2014-12-01 14:53:08 -08008019 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07008020 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
Stephen Hines176edba2014-12-01 14:53:08 -08008021 Kind = PPC64_SVR4_ABIInfo::ELFv1;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07008022 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Stephen Hines176edba2014-12-01 14:53:08 -08008023
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008024 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
Stephen Hines176edba2014-12-01 14:53:08 -08008025 }
John McCallec853ba2010-03-11 00:10:12 +00008026
Peter Collingbourneedb66f32012-05-20 23:28:41 +00008027 case llvm::Triple::nvptx:
8028 case llvm::Triple::nvptx64:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008029 return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00008030
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00008031 case llvm::Triple::msp430:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008032 return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00008033
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07008034 case llvm::Triple::systemz: {
8035 bool HasVector = getTarget().getABI() == "vector";
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008036 return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector));
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07008037 }
Ulrich Weigandb8409212013-05-06 16:26:41 +00008038
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00008039 case llvm::Triple::tce:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008040 return SetCGInfo(new TCETargetCodeGenInfo(Types));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00008041
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00008042 case llvm::Triple::x86: {
John McCallb8b52972013-06-18 02:46:29 +00008043 bool IsDarwinVectorABI = Triple.isOSDarwin();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08008044 bool RetSmallStructInRegABI =
John McCallb8b52972013-06-18 02:46:29 +00008045 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008046 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00008047
John McCallb8b52972013-06-18 02:46:29 +00008048 if (Triple.getOS() == llvm::Triple::Win32) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008049 return SetCGInfo(new WinX86_32TargetCodeGenInfo(
8050 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8051 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
John McCallb8b52972013-06-18 02:46:29 +00008052 } else {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008053 return SetCGInfo(new X86_32TargetCodeGenInfo(
8054 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8055 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
8056 CodeGenOpts.FloatABI == "soft"));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00008057 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00008058 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00008059
Eli Friedmanee1ad992011-12-02 00:11:43 +00008060 case llvm::Triple::x86_64: {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08008061 StringRef ABI = getTarget().getABI();
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008062 X86AVXABILevel AVXLevel =
8063 (ABI == "avx512"
8064 ? X86AVXABILevel::AVX512
8065 : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08008066
Chris Lattnerf13721d2010-08-31 16:44:54 +00008067 switch (Triple.getOS()) {
8068 case llvm::Triple::Win32:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008069 return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008070 case llvm::Triple::PS4:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008071 return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel));
Chris Lattnerf13721d2010-08-31 16:44:54 +00008072 default:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008073 return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
Chris Lattnerf13721d2010-08-31 16:44:54 +00008074 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00008075 }
Tony Linthicum96319392011-12-12 21:14:55 +00008076 case llvm::Triple::hexagon:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008077 return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
8078 case llvm::Triple::lanai:
8079 return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008080 case llvm::Triple::r600:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008081 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008082 case llvm::Triple::amdgcn:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008083 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
8084 case llvm::Triple::sparc:
8085 return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00008086 case llvm::Triple::sparcv9:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008087 return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
Robert Lytton5f15f4d2013-08-13 09:43:10 +00008088 case llvm::Triple::xcore:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008089 return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
8090 case llvm::Triple::spir:
8091 case llvm::Triple::spir64:
8092 return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00008093 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00008094}