blob: de568b51e0c7c74132c427b4e5b1f09b84642ba6 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000017#include "CGCXXABI.h"
Reid Kleckner9b3e3df2014-09-04 20:04:38 +000018#include "CGValue.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000021#include "clang/CodeGen/CGFunctionInfo.h"
Sandeep Patel45df3dd2011-04-05 00:23:47 +000022#include "clang/Frontend/CodeGenOptions.h"
Matt Arsenault43fae6c2014-12-04 20:38:18 +000023#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000024#include "llvm/ADT/Triple.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000025#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/Type.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000027#include "llvm/Support/raw_ostream.h"
Robert Lytton844aeeb2014-05-02 09:33:20 +000028#include <algorithm> // std::sort
29
Anton Korobeynikov244360d2009-06-05 22:08:42 +000030using namespace clang;
31using namespace CodeGen;
32
John McCall943fae92010-05-27 06:19:26 +000033static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
34 llvm::Value *Array,
35 llvm::Value *Value,
36 unsigned FirstIndex,
37 unsigned LastIndex) {
38 // Alternatively, we could emit this as a loop in the source.
39 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
David Blaikiefb901c7a2015-04-04 15:12:29 +000040 llvm::Value *Cell =
41 Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
John McCall7f416cc2015-09-08 08:05:57 +000042 Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
John McCall943fae92010-05-27 06:19:26 +000043 }
44}
45
John McCalla1dee5302010-08-22 10:59:02 +000046static bool isAggregateTypeForABI(QualType T) {
John McCall47fb9502013-03-07 21:37:08 +000047 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalla1dee5302010-08-22 10:59:02 +000048 T->isMemberFunctionPointerType();
49}
50
John McCall7f416cc2015-09-08 08:05:57 +000051ABIArgInfo
52ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
53 llvm::Type *Padding) const {
54 return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
55 ByRef, Realign, Padding);
56}
57
58ABIArgInfo
59ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
60 return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
61 /*ByRef*/ false, Realign);
62}
63
Charles Davisc7d5c942015-09-17 20:55:33 +000064Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
65 QualType Ty) const {
66 return Address::invalid();
67}
68
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000069ABIInfo::~ABIInfo() {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +000070
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000071static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
Mark Lacey3825e832013-10-06 01:33:34 +000072 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000073 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
74 if (!RD)
75 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +000076 return CXXABI.getRecordArgABI(RD);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000077}
78
79static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
Mark Lacey3825e832013-10-06 01:33:34 +000080 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000081 const RecordType *RT = T->getAs<RecordType>();
82 if (!RT)
83 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +000084 return getRecordArgABI(RT, CXXABI);
85}
86
Reid Klecknerb1be6832014-11-15 01:41:41 +000087/// Pass transparent unions as if they were the type of the first element. Sema
88/// should ensure that all elements of the union have the same "machine type".
89static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
90 if (const RecordType *UT = Ty->getAsUnionType()) {
91 const RecordDecl *UD = UT->getDecl();
92 if (UD->hasAttr<TransparentUnionAttr>()) {
93 assert(!UD->field_empty() && "sema created an empty transparent union");
94 return UD->field_begin()->getType();
95 }
96 }
97 return Ty;
98}
99
Mark Lacey3825e832013-10-06 01:33:34 +0000100CGCXXABI &ABIInfo::getCXXABI() const {
101 return CGT.getCXXABI();
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000102}
103
Chris Lattner2b037972010-07-29 02:01:43 +0000104ASTContext &ABIInfo::getContext() const {
105 return CGT.getContext();
106}
107
108llvm::LLVMContext &ABIInfo::getVMContext() const {
109 return CGT.getLLVMContext();
110}
111
Micah Villmowdd31ca12012-10-08 16:25:52 +0000112const llvm::DataLayout &ABIInfo::getDataLayout() const {
113 return CGT.getDataLayout();
Chris Lattner2b037972010-07-29 02:01:43 +0000114}
115
John McCallc8e01702013-04-16 22:48:15 +0000116const TargetInfo &ABIInfo::getTarget() const {
117 return CGT.getTarget();
118}
Chris Lattner2b037972010-07-29 02:01:43 +0000119
Nirav Dave9a8f97e2016-02-22 16:48:42 +0000120bool ABIInfo:: isAndroid() const { return getTarget().getTriple().isAndroid(); }
121
Reid Klecknere9f6a712014-10-31 17:10:41 +0000122bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
123 return false;
124}
125
126bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
127 uint64_t Members) const {
128 return false;
129}
130
Petar Jovanovic1a3f9652015-05-26 21:07:19 +0000131bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
132 return false;
133}
134
Yaron Kerencdae9412016-01-29 19:38:18 +0000135LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000136 raw_ostream &OS = llvm::errs();
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000137 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000138 switch (TheKind) {
139 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000140 OS << "Direct Type=";
Chris Lattner2192fe52011-07-18 04:24:23 +0000141 if (llvm::Type *Ty = getCoerceToType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000142 Ty->print(OS);
143 else
144 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000145 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000146 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000147 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000148 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000149 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000150 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000151 break;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000152 case InAlloca:
153 OS << "InAlloca Offset=" << getInAllocaFieldIndex();
154 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000155 case Indirect:
John McCall7f416cc2015-09-08 08:05:57 +0000156 OS << "Indirect Align=" << getIndirectAlign().getQuantity()
Joerg Sonnenberger4921fe22011-07-15 18:23:44 +0000157 << " ByVal=" << getIndirectByVal()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000158 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000159 break;
160 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000161 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000162 break;
John McCallf26e73d2016-03-11 04:30:43 +0000163 case CoerceAndExpand:
164 OS << "CoerceAndExpand Type=";
165 getCoerceAndExpandType()->print(OS);
166 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000167 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000168 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000169}
170
Petar Jovanovic402257b2015-12-04 00:26:47 +0000171// Dynamically round a pointer up to a multiple of the given alignment.
172static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
173 llvm::Value *Ptr,
174 CharUnits Align) {
175 llvm::Value *PtrAsInt = Ptr;
176 // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
177 PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
178 PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
179 llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
180 PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
181 llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
182 PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
183 Ptr->getType(),
184 Ptr->getName() + ".aligned");
185 return PtrAsInt;
186}
187
John McCall7f416cc2015-09-08 08:05:57 +0000188/// Emit va_arg for a platform using the common void* representation,
189/// where arguments are simply emitted in an array of slots on the stack.
190///
191/// This version implements the core direct-value passing rules.
192///
193/// \param SlotSize - The size and alignment of a stack slot.
194/// Each argument will be allocated to a multiple of this number of
195/// slots, and all the slots will be aligned to this value.
196/// \param AllowHigherAlign - The slot alignment is not a cap;
197/// an argument type with an alignment greater than the slot size
198/// will be emitted on a higher-alignment address, potentially
199/// leaving one or more empty slots behind as padding. If this
200/// is false, the returned address might be less-aligned than
201/// DirectAlign.
202static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
203 Address VAListAddr,
204 llvm::Type *DirectTy,
205 CharUnits DirectSize,
206 CharUnits DirectAlign,
207 CharUnits SlotSize,
208 bool AllowHigherAlign) {
209 // Cast the element type to i8* if necessary. Some platforms define
210 // va_list as a struct containing an i8* instead of just an i8*.
211 if (VAListAddr.getElementType() != CGF.Int8PtrTy)
212 VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
213
214 llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
215
216 // If the CC aligns values higher than the slot size, do so if needed.
217 Address Addr = Address::invalid();
218 if (AllowHigherAlign && DirectAlign > SlotSize) {
Petar Jovanovic402257b2015-12-04 00:26:47 +0000219 Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
220 DirectAlign);
John McCall7f416cc2015-09-08 08:05:57 +0000221 } else {
Petar Jovanovic402257b2015-12-04 00:26:47 +0000222 Addr = Address(Ptr, SlotSize);
John McCall7f416cc2015-09-08 08:05:57 +0000223 }
224
225 // Advance the pointer past the argument, then store that back.
Rui Ueyama83aa9792016-01-14 21:00:27 +0000226 CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
John McCall7f416cc2015-09-08 08:05:57 +0000227 llvm::Value *NextPtr =
228 CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize,
229 "argp.next");
230 CGF.Builder.CreateStore(NextPtr, VAListAddr);
231
232 // If the argument is smaller than a slot, and this is a big-endian
233 // target, the argument will be right-adjusted in its slot.
234 if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian()) {
235 Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
236 }
237
238 Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
239 return Addr;
240}
241
242/// Emit va_arg for a platform using the common void* representation,
243/// where arguments are simply emitted in an array of slots on the stack.
244///
245/// \param IsIndirect - Values of this type are passed indirectly.
246/// \param ValueInfo - The size and alignment of this type, generally
247/// computed with getContext().getTypeInfoInChars(ValueTy).
248/// \param SlotSizeAndAlign - The size and alignment of a stack slot.
249/// Each argument will be allocated to a multiple of this number of
250/// slots, and all the slots will be aligned to this value.
251/// \param AllowHigherAlign - The slot alignment is not a cap;
252/// an argument type with an alignment greater than the slot size
253/// will be emitted on a higher-alignment address, potentially
254/// leaving one or more empty slots behind as padding.
255static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
256 QualType ValueTy, bool IsIndirect,
257 std::pair<CharUnits, CharUnits> ValueInfo,
258 CharUnits SlotSizeAndAlign,
259 bool AllowHigherAlign) {
260 // The size and alignment of the value that was passed directly.
261 CharUnits DirectSize, DirectAlign;
262 if (IsIndirect) {
263 DirectSize = CGF.getPointerSize();
264 DirectAlign = CGF.getPointerAlign();
265 } else {
266 DirectSize = ValueInfo.first;
267 DirectAlign = ValueInfo.second;
268 }
269
270 // Cast the address we've calculated to the right type.
271 llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
272 if (IsIndirect)
273 DirectTy = DirectTy->getPointerTo(0);
274
275 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
276 DirectSize, DirectAlign,
277 SlotSizeAndAlign,
278 AllowHigherAlign);
279
280 if (IsIndirect) {
281 Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
282 }
283
284 return Addr;
285
286}
287
288static Address emitMergePHI(CodeGenFunction &CGF,
289 Address Addr1, llvm::BasicBlock *Block1,
290 Address Addr2, llvm::BasicBlock *Block2,
291 const llvm::Twine &Name = "") {
292 assert(Addr1.getType() == Addr2.getType());
293 llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
294 PHI->addIncoming(Addr1.getPointer(), Block1);
295 PHI->addIncoming(Addr2.getPointer(), Block2);
296 CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
297 return Address(PHI, Align);
298}
299
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000300TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
301
John McCall3480ef22011-08-30 01:42:09 +0000302// If someone can figure out a general rule for this, that would be great.
303// It's probably just doomed to be platform-dependent, though.
304unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
305 // Verified for:
306 // x86-64 FreeBSD, Linux, Darwin
307 // x86-32 FreeBSD, Linux, Darwin
308 // PowerPC Linux, Darwin
309 // ARM Darwin (*not* EABI)
Tim Northover9bb857a2013-01-31 12:13:10 +0000310 // AArch64 Linux
John McCall3480ef22011-08-30 01:42:09 +0000311 return 32;
312}
313
John McCalla729c622012-02-17 03:33:10 +0000314bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
315 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-09-21 08:08:30 +0000316 // The following conventions are known to require this to be false:
317 // x86_stdcall
318 // MIPS
319 // For everything else, we just prefer false unless we opt out.
320 return false;
321}
322
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000323void
324TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
325 llvm::SmallString<24> &Opt) const {
326 // This assumes the user is passing a library name like "rt" instead of a
327 // filename like "librt.a/so", and that they don't care whether it's static or
328 // dynamic.
329 Opt = "-l";
330 Opt += Lib;
331}
332
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000333static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000334
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000335/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000336/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000337static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
338 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000339 if (FD->isUnnamedBitfield())
340 return true;
341
342 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000343
Eli Friedman0b3f2012011-11-18 03:47:20 +0000344 // Constant arrays of empty records count as empty, strip them off.
345 // Constant arrays of zero length always count as empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000346 if (AllowArrays)
Eli Friedman0b3f2012011-11-18 03:47:20 +0000347 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
348 if (AT->getSize() == 0)
349 return true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000350 FT = AT->getElementType();
Eli Friedman0b3f2012011-11-18 03:47:20 +0000351 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000352
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000353 const RecordType *RT = FT->getAs<RecordType>();
354 if (!RT)
355 return false;
356
357 // C++ record fields are never empty, at least in the Itanium ABI.
358 //
359 // FIXME: We should use a predicate for whether this behavior is true in the
360 // current ABI.
361 if (isa<CXXRecordDecl>(RT->getDecl()))
362 return false;
363
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000364 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000365}
366
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000367/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000368/// fields. Note that a structure with a flexible array member is not
369/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000370static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000371 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000372 if (!RT)
Denis Zobnin380b2242016-02-11 11:26:03 +0000373 return false;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000374 const RecordDecl *RD = RT->getDecl();
375 if (RD->hasFlexibleArrayMember())
376 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000377
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000378 // If this is a C++ record, check the bases first.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000379 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +0000380 for (const auto &I : CXXRD->bases())
381 if (!isEmptyRecord(Context, I.getType(), true))
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000382 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000383
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000384 for (const auto *I : RD->fields())
385 if (!isEmptyField(Context, I, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000386 return false;
387 return true;
388}
389
390/// isSingleElementStruct - Determine if a structure is a "single
391/// element struct", i.e. it has exactly one non-empty field or
392/// exactly one field which is itself a single element
393/// struct. Structures with flexible array members are never
394/// considered single element structs.
395///
396/// \return The field declaration for the single non-empty field, if
397/// it exists.
398static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Benjamin Kramer83b1bf32015-03-02 16:09:24 +0000399 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000400 if (!RT)
Craig Topper8a13c412014-05-21 05:09:00 +0000401 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000402
403 const RecordDecl *RD = RT->getDecl();
404 if (RD->hasFlexibleArrayMember())
Craig Topper8a13c412014-05-21 05:09:00 +0000405 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000406
Craig Topper8a13c412014-05-21 05:09:00 +0000407 const Type *Found = nullptr;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000408
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000409 // If this is a C++ record, check the bases first.
410 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000411 for (const auto &I : CXXRD->bases()) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000412 // Ignore empty records.
Aaron Ballman574705e2014-03-13 15:41:46 +0000413 if (isEmptyRecord(Context, I.getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000414 continue;
415
416 // If we already found an element then this isn't a single-element struct.
417 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000418 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000419
420 // If this is non-empty and not a single element struct, the composite
421 // cannot be a single element struct.
Aaron Ballman574705e2014-03-13 15:41:46 +0000422 Found = isSingleElementStruct(I.getType(), Context);
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000423 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000424 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000425 }
426 }
427
428 // Check for single element.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000429 for (const auto *FD : RD->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000430 QualType FT = FD->getType();
431
432 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000433 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000434 continue;
435
436 // If we already found an element then this isn't a single-element
437 // struct.
438 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000439 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000440
441 // Treat single element arrays as the element.
442 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
443 if (AT->getSize().getZExtValue() != 1)
444 break;
445 FT = AT->getElementType();
446 }
447
John McCalla1dee5302010-08-22 10:59:02 +0000448 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000449 Found = FT.getTypePtr();
450 } else {
451 Found = isSingleElementStruct(FT, Context);
452 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000453 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000454 }
455 }
456
Eli Friedmanee945342011-11-18 01:25:50 +0000457 // We don't consider a struct a single-element struct if it has
458 // padding beyond the element type.
459 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
Craig Topper8a13c412014-05-21 05:09:00 +0000460 return nullptr;
Eli Friedmanee945342011-11-18 01:25:50 +0000461
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000462 return Found;
463}
464
465static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Eli Friedmana92db672012-11-29 23:21:04 +0000466 // Treat complex types as the element type.
467 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
468 Ty = CTy->getElementType();
469
470 // Check for a type which we know has a simple scalar argument-passing
471 // convention without any padding. (We're specifically looking for 32
472 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000473 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmana92db672012-11-29 23:21:04 +0000474 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000475 return false;
476
477 uint64_t Size = Context.getTypeSize(Ty);
478 return Size == 32 || Size == 64;
479}
480
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000481/// canExpandIndirectArgument - Test whether an argument type which is to be
482/// passed indirectly (on the stack) would have the equivalent layout if it was
483/// expanded into separate arguments. If so, we prefer to do the latter to avoid
484/// inhibiting optimizations.
485///
486// FIXME: This predicate is missing many cases, currently it just follows
487// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
488// should probably make this smarter, or better yet make the LLVM backend
489// capable of handling it.
490static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
491 // We can only expand structure types.
492 const RecordType *RT = Ty->getAs<RecordType>();
493 if (!RT)
494 return false;
495
496 // We can only expand (C) structures.
497 //
498 // FIXME: This needs to be generalized to handle classes as well.
499 const RecordDecl *RD = RT->getDecl();
Manman Ren27382782015-04-03 18:10:29 +0000500 if (!RD->isStruct())
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000501 return false;
502
Manman Ren27382782015-04-03 18:10:29 +0000503 // We try to expand CLike CXXRecordDecl.
504 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
505 if (!CXXRD->isCLike())
506 return false;
507 }
508
Eli Friedmane5c85622011-11-18 01:32:26 +0000509 uint64_t Size = 0;
510
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000511 for (const auto *FD : RD->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000512 if (!is32Or64BitBasicType(FD->getType(), Context))
513 return false;
514
515 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
516 // how to expand them yet, and the predicate for telling if a bitfield still
517 // counts as "basic" is more complicated than what we were doing previously.
518 if (FD->isBitField())
519 return false;
Eli Friedmane5c85622011-11-18 01:32:26 +0000520
521 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000522 }
523
Eli Friedmane5c85622011-11-18 01:32:26 +0000524 // Make sure there are not any holes in the struct.
525 if (Size != Context.getTypeSize(Ty))
526 return false;
527
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000528 return true;
529}
530
531namespace {
James Y Knight29b5f082016-02-24 02:59:33 +0000532Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
533 const ABIArgInfo &AI) {
534 // This default implementation defers to the llvm backend's va_arg
535 // instruction. It can handle only passing arguments directly
536 // (typically only handled in the backend for primitive types), or
537 // aggregates passed indirectly by pointer (NOTE: if the "byval"
538 // flag has ABI impact in the callee, this implementation cannot
539 // work.)
540
541 // Only a few cases are covered here at the moment -- those needed
542 // by the default abi.
543 llvm::Value *Val;
544
545 if (AI.isIndirect()) {
546 assert(!AI.getPaddingType() &&
547 "Unepxected PaddingType seen in arginfo in generic VAArg emitter!");
548 assert(
549 !AI.getIndirectRealign() &&
550 "Unepxected IndirectRealign seen in arginfo in generic VAArg emitter!");
551
552 auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
553 CharUnits TyAlignForABI = TyInfo.second;
554
555 llvm::Type *BaseTy =
556 llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
557 llvm::Value *Addr =
558 CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
559 return Address(Addr, TyAlignForABI);
560 } else {
561 assert((AI.isDirect() || AI.isExtend()) &&
562 "Unexpected ArgInfo Kind in generic VAArg emitter!");
563
564 assert(!AI.getInReg() &&
565 "Unepxected InReg seen in arginfo in generic VAArg emitter!");
566 assert(!AI.getPaddingType() &&
567 "Unepxected PaddingType seen in arginfo in generic VAArg emitter!");
568 assert(!AI.getDirectOffset() &&
569 "Unepxected DirectOffset seen in arginfo in generic VAArg emitter!");
570 assert(!AI.getCoerceToType() &&
571 "Unepxected CoerceToType seen in arginfo in generic VAArg emitter!");
572
573 Address Temp = CGF.CreateMemTemp(Ty, "varet");
574 Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
575 CGF.Builder.CreateStore(Val, Temp);
576 return Temp;
577 }
578}
579
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000580/// DefaultABIInfo - The default implementation for ABI specific
581/// details. This implementation provides information which results in
582/// self-consistent and sensible LLVM IR generation, but does not
583/// conform to any particular ABI.
584class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000585public:
586 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000587
Chris Lattner458b2aa2010-07-29 02:16:43 +0000588 ABIArgInfo classifyReturnType(QualType RetTy) const;
589 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000590
Craig Topper4f12f102014-03-12 06:41:41 +0000591 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000592 if (!getCXXABI().classifyReturnType(FI))
593 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +0000594 for (auto &I : FI.arguments())
595 I.info = classifyArgumentType(I.type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000596 }
597
John McCall7f416cc2015-09-08 08:05:57 +0000598 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
James Y Knight29b5f082016-02-24 02:59:33 +0000599 QualType Ty) const override {
600 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
601 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000602};
603
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000604class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
605public:
Chris Lattner2b037972010-07-29 02:01:43 +0000606 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
607 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000608};
609
Chris Lattner458b2aa2010-07-29 02:16:43 +0000610ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerac385062015-05-18 22:46:30 +0000611 Ty = useFirstFieldIfTransparentUnion(Ty);
612
613 if (isAggregateTypeForABI(Ty)) {
614 // Records with non-trivial destructors/copy-constructors should not be
615 // passed by value.
616 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000617 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Reid Klecknerac385062015-05-18 22:46:30 +0000618
John McCall7f416cc2015-09-08 08:05:57 +0000619 return getNaturalAlignIndirect(Ty);
Reid Klecknerac385062015-05-18 22:46:30 +0000620 }
Daniel Dunbar557893d2010-04-21 19:10:51 +0000621
Chris Lattner9723d6c2010-03-11 18:19:55 +0000622 // Treat an enum type as its underlying type.
623 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
624 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000625
Chris Lattner9723d6c2010-03-11 18:19:55 +0000626 return (Ty->isPromotableIntegerType() ?
627 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000628}
629
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000630ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
631 if (RetTy->isVoidType())
632 return ABIArgInfo::getIgnore();
633
634 if (isAggregateTypeForABI(RetTy))
John McCall7f416cc2015-09-08 08:05:57 +0000635 return getNaturalAlignIndirect(RetTy);
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000636
637 // Treat an enum type as its underlying type.
638 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
639 RetTy = EnumTy->getDecl()->getIntegerType();
640
641 return (RetTy->isPromotableIntegerType() ?
642 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
643}
644
Derek Schuff09338a22012-09-06 17:37:28 +0000645//===----------------------------------------------------------------------===//
Dan Gohmanc2853072015-09-03 22:51:53 +0000646// WebAssembly ABI Implementation
647//
648// This is a very simple ABI that relies a lot on DefaultABIInfo.
649//===----------------------------------------------------------------------===//
650
651class WebAssemblyABIInfo final : public DefaultABIInfo {
652public:
653 explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
654 : DefaultABIInfo(CGT) {}
655
656private:
657 ABIArgInfo classifyReturnType(QualType RetTy) const;
658 ABIArgInfo classifyArgumentType(QualType Ty) const;
659
660 // DefaultABIInfo's classifyReturnType and classifyArgumentType are
James Y Knight29b5f082016-02-24 02:59:33 +0000661 // non-virtual, but computeInfo and EmitVAArg is virtual, so we
662 // overload them.
Dan Gohmanc2853072015-09-03 22:51:53 +0000663 void computeInfo(CGFunctionInfo &FI) const override {
664 if (!getCXXABI().classifyReturnType(FI))
665 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
666 for (auto &Arg : FI.arguments())
667 Arg.info = classifyArgumentType(Arg.type);
668 }
Dan Gohman1fcd10c2016-02-22 19:17:40 +0000669
670 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
671 QualType Ty) const override;
Dan Gohmanc2853072015-09-03 22:51:53 +0000672};
673
674class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
675public:
676 explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
677 : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
678};
679
680/// \brief Classify argument of given type \p Ty.
681ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
682 Ty = useFirstFieldIfTransparentUnion(Ty);
683
684 if (isAggregateTypeForABI(Ty)) {
685 // Records with non-trivial destructors/copy-constructors should not be
686 // passed by value.
Dan Gohmanc2853072015-09-03 22:51:53 +0000687 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000688 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Dan Gohmanc2853072015-09-03 22:51:53 +0000689 // Ignore empty structs/unions.
690 if (isEmptyRecord(getContext(), Ty, true))
691 return ABIArgInfo::getIgnore();
692 // Lower single-element structs to just pass a regular value. TODO: We
693 // could do reasonable-size multiple-element structs too, using getExpand(),
694 // though watch out for things like bitfields.
695 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
696 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
Dan Gohmanc2853072015-09-03 22:51:53 +0000697 }
698
699 // Otherwise just do the default thing.
700 return DefaultABIInfo::classifyArgumentType(Ty);
701}
702
703ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
704 if (isAggregateTypeForABI(RetTy)) {
705 // Records with non-trivial destructors/copy-constructors should not be
706 // returned by value.
707 if (!getRecordArgABI(RetTy, getCXXABI())) {
708 // Ignore empty structs/unions.
709 if (isEmptyRecord(getContext(), RetTy, true))
710 return ABIArgInfo::getIgnore();
711 // Lower single-element structs to just return a regular value. TODO: We
712 // could do reasonable-size multiple-element structs too, using
713 // ABIArgInfo::getDirect().
714 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
715 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
716 }
717 }
718
719 // Otherwise just do the default thing.
720 return DefaultABIInfo::classifyReturnType(RetTy);
721}
722
Dan Gohman1fcd10c2016-02-22 19:17:40 +0000723Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
724 QualType Ty) const {
725 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false,
726 getContext().getTypeInfoInChars(Ty),
727 CharUnits::fromQuantity(4),
728 /*AllowHigherAlign=*/ true);
729}
730
Dan Gohmanc2853072015-09-03 22:51:53 +0000731//===----------------------------------------------------------------------===//
Derek Schuff09338a22012-09-06 17:37:28 +0000732// le32/PNaCl bitcode ABI Implementation
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000733//
734// This is a simplified version of the x86_32 ABI. Arguments and return values
735// are always passed on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000736//===----------------------------------------------------------------------===//
737
738class PNaClABIInfo : public ABIInfo {
739 public:
740 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
741
742 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000743 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff09338a22012-09-06 17:37:28 +0000744
Craig Topper4f12f102014-03-12 06:41:41 +0000745 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +0000746 Address EmitVAArg(CodeGenFunction &CGF,
747 Address VAListAddr, QualType Ty) const override;
Derek Schuff09338a22012-09-06 17:37:28 +0000748};
749
750class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
751 public:
752 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
753 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
754};
755
756void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000757 if (!getCXXABI().classifyReturnType(FI))
Derek Schuff09338a22012-09-06 17:37:28 +0000758 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
759
Reid Kleckner40ca9132014-05-13 22:05:45 +0000760 for (auto &I : FI.arguments())
761 I.info = classifyArgumentType(I.type);
762}
Derek Schuff09338a22012-09-06 17:37:28 +0000763
John McCall7f416cc2015-09-08 08:05:57 +0000764Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
765 QualType Ty) const {
James Y Knight29b5f082016-02-24 02:59:33 +0000766 // The PNaCL ABI is a bit odd, in that varargs don't use normal
767 // function classification. Structs get passed directly for varargs
768 // functions, through a rewriting transform in
769 // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
770 // this target to actually support a va_arg instructions with an
771 // aggregate type, unlike other targets.
772 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000773}
774
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000775/// \brief Classify argument of given type \p Ty.
776ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff09338a22012-09-06 17:37:28 +0000777 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +0000778 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000779 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
780 return getNaturalAlignIndirect(Ty);
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000781 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
782 // Treat an enum type as its underlying type.
Derek Schuff09338a22012-09-06 17:37:28 +0000783 Ty = EnumTy->getDecl()->getIntegerType();
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000784 } else if (Ty->isFloatingType()) {
785 // Floating-point types don't go inreg.
786 return ABIArgInfo::getDirect();
Derek Schuff09338a22012-09-06 17:37:28 +0000787 }
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000788
789 return (Ty->isPromotableIntegerType() ?
790 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000791}
792
793ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
794 if (RetTy->isVoidType())
795 return ABIArgInfo::getIgnore();
796
Eli Benderskye20dad62013-04-04 22:49:35 +0000797 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000798 if (isAggregateTypeForABI(RetTy))
John McCall7f416cc2015-09-08 08:05:57 +0000799 return getNaturalAlignIndirect(RetTy);
Derek Schuff09338a22012-09-06 17:37:28 +0000800
801 // Treat an enum type as its underlying type.
802 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
803 RetTy = EnumTy->getDecl()->getIntegerType();
804
805 return (RetTy->isPromotableIntegerType() ?
806 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
807}
808
Chad Rosier651c1832013-03-25 21:00:27 +0000809/// IsX86_MMXType - Return true if this is an MMX type.
810bool IsX86_MMXType(llvm::Type *IRType) {
811 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
Bill Wendling5cd41c42010-10-18 03:41:31 +0000812 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
813 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
814 IRType->getScalarSizeInBits() != 64;
815}
816
Jay Foad7c57be32011-07-11 09:56:20 +0000817static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000818 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000819 llvm::Type* Ty) {
Tim Northover0ae93912013-06-07 00:04:50 +0000820 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
821 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
822 // Invalid MMX constraint
Craig Topper8a13c412014-05-21 05:09:00 +0000823 return nullptr;
Tim Northover0ae93912013-06-07 00:04:50 +0000824 }
825
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000826 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover0ae93912013-06-07 00:04:50 +0000827 }
828
829 // No operation needed
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000830 return Ty;
831}
832
Reid Kleckner80944df2014-10-31 22:00:51 +0000833/// Returns true if this type can be passed in SSE registers with the
834/// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
835static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
836 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
837 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half)
838 return true;
839 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
840 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
841 // registers specially.
842 unsigned VecSize = Context.getTypeSize(VT);
843 if (VecSize == 128 || VecSize == 256 || VecSize == 512)
844 return true;
845 }
846 return false;
847}
848
849/// Returns true if this aggregate is small enough to be passed in SSE registers
850/// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
851static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
852 return NumMembers <= 4;
853}
854
Chris Lattner0cf24192010-06-28 20:05:43 +0000855//===----------------------------------------------------------------------===//
856// X86-32 ABI Implementation
857//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000858
Reid Kleckner661f35b2014-01-18 01:12:41 +0000859/// \brief Similar to llvm::CCState, but for Clang.
860struct CCState {
Reid Kleckner80944df2014-10-31 22:00:51 +0000861 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
Reid Kleckner661f35b2014-01-18 01:12:41 +0000862
863 unsigned CC;
864 unsigned FreeRegs;
Reid Kleckner80944df2014-10-31 22:00:51 +0000865 unsigned FreeSSERegs;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000866};
867
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000868/// X86_32ABIInfo - The X86-32 ABI information.
869class X86_32ABIInfo : public ABIInfo {
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000870 enum Class {
871 Integer,
872 Float
873 };
874
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000875 static const unsigned MinABIStackAlignInBytes = 4;
876
David Chisnallde3a0692009-08-17 23:08:21 +0000877 bool IsDarwinVectorABI;
Michael Kupersteindc745202015-10-19 07:52:25 +0000878 bool IsRetSmallStructInRegABI;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000879 bool IsWin32StructABI;
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +0000880 bool IsSoftFloatABI;
Michael Kuperstein68901882015-10-25 08:18:20 +0000881 bool IsMCUABI;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000882 unsigned DefaultNumRegisterParameters;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000883
884 static bool isRegisterSize(unsigned Size) {
885 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
886 }
887
Reid Kleckner80944df2014-10-31 22:00:51 +0000888 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
889 // FIXME: Assumes vectorcall is in use.
890 return isX86VectorTypeForVectorCall(getContext(), Ty);
891 }
892
893 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
894 uint64_t NumMembers) const override {
895 // FIXME: Assumes vectorcall is in use.
896 return isX86VectorCallAggregateSmallEnough(NumMembers);
897 }
898
Reid Kleckner40ca9132014-05-13 22:05:45 +0000899 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000900
Daniel Dunbar557893d2010-04-21 19:10:51 +0000901 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
902 /// such that the argument will be passed in memory.
Reid Kleckner661f35b2014-01-18 01:12:41 +0000903 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
904
John McCall7f416cc2015-09-08 08:05:57 +0000905 ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000906
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000907 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000908 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000909
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000910 Class classify(QualType Ty) const;
Reid Kleckner40ca9132014-05-13 22:05:45 +0000911 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000912 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +0000913 /// \brief Updates the number of available free registers, returns
914 /// true if any registers were allocated.
915 bool updateFreeRegs(QualType Ty, CCState &State) const;
916
917 bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
918 bool &NeedsPadding) const;
919 bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000920
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000921 /// \brief Rewrite the function info so that all memory arguments use
922 /// inalloca.
923 void rewriteWithInAlloca(CGFunctionInfo &FI) const;
924
925 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +0000926 CharUnits &StackOffset, ABIArgInfo &Info,
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000927 QualType Type) const;
928
Rafael Espindola75419dc2012-07-23 23:30:29 +0000929public:
930
Craig Topper4f12f102014-03-12 06:41:41 +0000931 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +0000932 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
933 QualType Ty) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000934
Michael Kupersteindc745202015-10-19 07:52:25 +0000935 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
936 bool RetSmallStructInRegABI, bool Win32StructABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +0000937 unsigned NumRegisterParameters, bool SoftFloatABI)
Michael Kupersteindc745202015-10-19 07:52:25 +0000938 : ABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
939 IsRetSmallStructInRegABI(RetSmallStructInRegABI),
940 IsWin32StructABI(Win32StructABI),
Manuel Klimekab2e28e2015-10-19 08:43:46 +0000941 IsSoftFloatABI(SoftFloatABI),
Michael Kupersteind749f232015-10-27 07:46:22 +0000942 IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
Manuel Klimekab2e28e2015-10-19 08:43:46 +0000943 DefaultNumRegisterParameters(NumRegisterParameters) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000944};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000945
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000946class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
947public:
Michael Kupersteindc745202015-10-19 07:52:25 +0000948 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
949 bool RetSmallStructInRegABI, bool Win32StructABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +0000950 unsigned NumRegisterParameters, bool SoftFloatABI)
951 : TargetCodeGenInfo(new X86_32ABIInfo(
952 CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
953 NumRegisterParameters, SoftFloatABI)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000954
John McCall1fe2a8c2013-06-18 02:46:29 +0000955 static bool isStructReturnInRegABI(
956 const llvm::Triple &Triple, const CodeGenOptions &Opts);
957
Eric Christopher162c91c2015-06-05 22:03:00 +0000958 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +0000959 CodeGen::CodeGenModule &CGM) const override;
John McCallbeec5a02010-03-06 00:35:14 +0000960
Craig Topper4f12f102014-03-12 06:41:41 +0000961 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +0000962 // Darwin uses different dwarf register numbers for EH.
John McCallc8e01702013-04-16 22:48:15 +0000963 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCallbeec5a02010-03-06 00:35:14 +0000964 return 4;
965 }
966
967 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +0000968 llvm::Value *Address) const override;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000969
Jay Foad7c57be32011-07-11 09:56:20 +0000970 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000971 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +0000972 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000973 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
974 }
975
Reid Kleckner9b3e3df2014-09-04 20:04:38 +0000976 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
977 std::string &Constraints,
978 std::vector<llvm::Type *> &ResultRegTypes,
979 std::vector<llvm::Type *> &ResultTruncRegTypes,
980 std::vector<LValue> &ResultRegDests,
981 std::string &AsmString,
982 unsigned NumOutputs) const override;
983
Craig Topper4f12f102014-03-12 06:41:41 +0000984 llvm::Constant *
985 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourneb453cd62013-10-20 21:29:19 +0000986 unsigned Sig = (0xeb << 0) | // jmp rel8
987 (0x06 << 8) | // .+0x08
988 ('F' << 16) |
989 ('T' << 24);
990 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
991 }
John McCall01391782016-02-05 21:37:38 +0000992
993 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
994 return "movl\t%ebp, %ebp"
995 "\t\t## marker for objc_retainAutoreleaseReturnValue";
996 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000997};
998
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000999}
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001000
Reid Kleckner9b3e3df2014-09-04 20:04:38 +00001001/// Rewrite input constraint references after adding some output constraints.
1002/// In the case where there is one output and one input and we add one output,
1003/// we need to replace all operand references greater than or equal to 1:
1004/// mov $0, $1
1005/// mov eax, $1
1006/// The result will be:
1007/// mov $0, $2
1008/// mov eax, $2
1009static void rewriteInputConstraintReferences(unsigned FirstIn,
1010 unsigned NumNewOuts,
1011 std::string &AsmString) {
1012 std::string Buf;
1013 llvm::raw_string_ostream OS(Buf);
1014 size_t Pos = 0;
1015 while (Pos < AsmString.size()) {
1016 size_t DollarStart = AsmString.find('$', Pos);
1017 if (DollarStart == std::string::npos)
1018 DollarStart = AsmString.size();
1019 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1020 if (DollarEnd == std::string::npos)
1021 DollarEnd = AsmString.size();
1022 OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1023 Pos = DollarEnd;
1024 size_t NumDollars = DollarEnd - DollarStart;
1025 if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1026 // We have an operand reference.
1027 size_t DigitStart = Pos;
1028 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1029 if (DigitEnd == std::string::npos)
1030 DigitEnd = AsmString.size();
1031 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1032 unsigned OperandIndex;
1033 if (!OperandStr.getAsInteger(10, OperandIndex)) {
1034 if (OperandIndex >= FirstIn)
1035 OperandIndex += NumNewOuts;
1036 OS << OperandIndex;
1037 } else {
1038 OS << OperandStr;
1039 }
1040 Pos = DigitEnd;
1041 }
1042 }
1043 AsmString = std::move(OS.str());
1044}
1045
1046/// Add output constraints for EAX:EDX because they are return registers.
1047void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1048 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1049 std::vector<llvm::Type *> &ResultRegTypes,
1050 std::vector<llvm::Type *> &ResultTruncRegTypes,
1051 std::vector<LValue> &ResultRegDests, std::string &AsmString,
1052 unsigned NumOutputs) const {
1053 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1054
1055 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1056 // larger.
1057 if (!Constraints.empty())
1058 Constraints += ',';
1059 if (RetWidth <= 32) {
1060 Constraints += "={eax}";
1061 ResultRegTypes.push_back(CGF.Int32Ty);
1062 } else {
1063 // Use the 'A' constraint for EAX:EDX.
1064 Constraints += "=A";
1065 ResultRegTypes.push_back(CGF.Int64Ty);
1066 }
1067
1068 // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1069 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1070 ResultTruncRegTypes.push_back(CoerceTy);
1071
1072 // Coerce the integer by bitcasting the return slot pointer.
1073 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
1074 CoerceTy->getPointerTo()));
1075 ResultRegDests.push_back(ReturnSlot);
1076
1077 rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1078}
1079
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001080/// shouldReturnTypeInRegister - Determine if the given type should be
Michael Kuperstein68901882015-10-25 08:18:20 +00001081/// returned in a register (for the Darwin and MCU ABI).
Reid Kleckner40ca9132014-05-13 22:05:45 +00001082bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1083 ASTContext &Context) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001084 uint64_t Size = Context.getTypeSize(Ty);
1085
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001086 // For i386, type must be register sized.
1087 // For the MCU ABI, it only needs to be <= 8-byte
1088 if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1089 return false;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001090
1091 if (Ty->isVectorType()) {
1092 // 64- and 128- bit vectors inside structures are not returned in
1093 // registers.
1094 if (Size == 64 || Size == 128)
1095 return false;
1096
1097 return true;
1098 }
1099
Daniel Dunbar4bd95c62010-05-15 00:00:30 +00001100 // If this is a builtin, pointer, enum, complex type, member pointer, or
1101 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +00001102 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +00001103 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +00001104 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001105 return true;
1106
1107 // Arrays are treated like records.
1108 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Reid Kleckner40ca9132014-05-13 22:05:45 +00001109 return shouldReturnTypeInRegister(AT->getElementType(), Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001110
1111 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001112 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001113 if (!RT) return false;
1114
Anders Carlsson40446e82010-01-27 03:25:19 +00001115 // FIXME: Traverse bases here too.
1116
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001117 // Structure types are passed in register if all fields would be
1118 // passed in a register.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001119 for (const auto *FD : RT->getDecl()->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001120 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001121 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001122 continue;
1123
1124 // Check fields recursively.
Reid Kleckner40ca9132014-05-13 22:05:45 +00001125 if (!shouldReturnTypeInRegister(FD->getType(), Context))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001126 return false;
1127 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001128 return true;
1129}
1130
John McCall7f416cc2015-09-08 08:05:57 +00001131ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001132 // If the return value is indirect, then the hidden argument is consuming one
1133 // integer register.
1134 if (State.FreeRegs) {
1135 --State.FreeRegs;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001136 if (!IsMCUABI)
1137 return getNaturalAlignIndirectInReg(RetTy);
Reid Kleckner661f35b2014-01-18 01:12:41 +00001138 }
John McCall7f416cc2015-09-08 08:05:57 +00001139 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
Reid Kleckner661f35b2014-01-18 01:12:41 +00001140}
1141
Eric Christopher7565e0d2015-05-29 23:09:49 +00001142ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1143 CCState &State) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001144 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001145 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001146
Reid Kleckner80944df2014-10-31 22:00:51 +00001147 const Type *Base = nullptr;
1148 uint64_t NumElts = 0;
1149 if (State.CC == llvm::CallingConv::X86_VectorCall &&
1150 isHomogeneousAggregate(RetTy, Base, NumElts)) {
1151 // The LLVM struct type for such an aggregate should lower properly.
1152 return ABIArgInfo::getDirect();
1153 }
1154
Chris Lattner458b2aa2010-07-29 02:16:43 +00001155 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001156 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +00001157 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001158 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001159
1160 // 128-bit vectors are a special case; they are returned in
1161 // registers and we need to make sure to pick a type the LLVM
1162 // backend will like.
1163 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001164 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +00001165 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001166
1167 // Always return in register if it fits in a general purpose
1168 // register, or if it is 64 bits and has a single element.
1169 if ((Size == 8 || Size == 16 || Size == 32) ||
1170 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001171 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00001172 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001173
John McCall7f416cc2015-09-08 08:05:57 +00001174 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001175 }
1176
1177 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +00001178 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001179
John McCalla1dee5302010-08-22 10:59:02 +00001180 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +00001181 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +00001182 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001183 if (RT->getDecl()->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00001184 return getIndirectReturnResult(RetTy, State);
Anders Carlsson5789c492009-10-20 22:07:59 +00001185 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001186
David Chisnallde3a0692009-08-17 23:08:21 +00001187 // If specified, structs and unions are always indirect.
Michael Kupersteindc745202015-10-19 07:52:25 +00001188 if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
John McCall7f416cc2015-09-08 08:05:57 +00001189 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001190
Denis Zobnin380b2242016-02-11 11:26:03 +00001191 // Ignore empty structs/unions.
1192 if (isEmptyRecord(getContext(), RetTy, true))
1193 return ABIArgInfo::getIgnore();
1194
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001195 // Small structures which are register sized are generally returned
1196 // in a register.
Reid Kleckner40ca9132014-05-13 22:05:45 +00001197 if (shouldReturnTypeInRegister(RetTy, getContext())) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001198 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +00001199
1200 // As a special-case, if the struct is a "single-element" struct, and
1201 // the field is of type "float" or "double", return it in a
Eli Friedmana98d1f82012-01-25 22:46:34 +00001202 // floating-point register. (MSVC does not apply this special case.)
1203 // We apply a similar transformation for pointer types to improve the
1204 // quality of the generated IR.
Eli Friedmanee945342011-11-18 01:25:50 +00001205 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001206 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedmana98d1f82012-01-25 22:46:34 +00001207 || SeltTy->hasPointerRepresentation())
Eli Friedmanee945342011-11-18 01:25:50 +00001208 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1209
1210 // FIXME: We should be able to narrow this integer in cases with dead
1211 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001212 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001213 }
1214
John McCall7f416cc2015-09-08 08:05:57 +00001215 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001216 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001217
Chris Lattner458b2aa2010-07-29 02:16:43 +00001218 // Treat an enum type as its underlying type.
1219 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1220 RetTy = EnumTy->getDecl()->getIntegerType();
1221
1222 return (RetTy->isPromotableIntegerType() ?
1223 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001224}
1225
Eli Friedman7919bea2012-06-05 19:40:46 +00001226static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
1227 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1228}
1229
Daniel Dunbared23de32010-09-16 20:42:00 +00001230static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
1231 const RecordType *RT = Ty->getAs<RecordType>();
1232 if (!RT)
1233 return 0;
1234 const RecordDecl *RD = RT->getDecl();
1235
1236 // If this is a C++ record, check the bases first.
1237 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +00001238 for (const auto &I : CXXRD->bases())
1239 if (!isRecordWithSSEVectorType(Context, I.getType()))
Daniel Dunbared23de32010-09-16 20:42:00 +00001240 return false;
1241
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001242 for (const auto *i : RD->fields()) {
Daniel Dunbared23de32010-09-16 20:42:00 +00001243 QualType FT = i->getType();
1244
Eli Friedman7919bea2012-06-05 19:40:46 +00001245 if (isSSEVectorType(Context, FT))
Daniel Dunbared23de32010-09-16 20:42:00 +00001246 return true;
1247
1248 if (isRecordWithSSEVectorType(Context, FT))
1249 return true;
1250 }
1251
1252 return false;
1253}
1254
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001255unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1256 unsigned Align) const {
1257 // Otherwise, if the alignment is less than or equal to the minimum ABI
1258 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001259 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001260 return 0; // Use default alignment.
1261
1262 // On non-Darwin, the stack type alignment is always 4.
1263 if (!IsDarwinVectorABI) {
1264 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001265 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001266 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001267
Daniel Dunbared23de32010-09-16 20:42:00 +00001268 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7919bea2012-06-05 19:40:46 +00001269 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
1270 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbared23de32010-09-16 20:42:00 +00001271 return 16;
1272
1273 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001274}
1275
Rafael Espindola703c47f2012-10-19 05:04:37 +00001276ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
Reid Kleckner661f35b2014-01-18 01:12:41 +00001277 CCState &State) const {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001278 if (!ByVal) {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001279 if (State.FreeRegs) {
1280 --State.FreeRegs; // Non-byval indirects just use one pointer.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001281 if (!IsMCUABI)
1282 return getNaturalAlignIndirectInReg(Ty);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001283 }
John McCall7f416cc2015-09-08 08:05:57 +00001284 return getNaturalAlignIndirect(Ty, false);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001285 }
Daniel Dunbar53fac692010-04-21 19:49:55 +00001286
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001287 // Compute the byval alignment.
1288 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1289 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1290 if (StackAlign == 0)
John McCall7f416cc2015-09-08 08:05:57 +00001291 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001292
1293 // If the stack alignment is less than the type alignment, realign the
1294 // argument.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001295 bool Realign = TypeAlign > StackAlign;
John McCall7f416cc2015-09-08 08:05:57 +00001296 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1297 /*ByVal=*/true, Realign);
Daniel Dunbar557893d2010-04-21 19:10:51 +00001298}
1299
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001300X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1301 const Type *T = isSingleElementStruct(Ty, getContext());
1302 if (!T)
1303 T = Ty.getTypePtr();
1304
1305 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1306 BuiltinType::Kind K = BT->getKind();
1307 if (K == BuiltinType::Float || K == BuiltinType::Double)
1308 return Float;
1309 }
1310 return Integer;
1311}
1312
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001313bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001314 if (!IsSoftFloatABI) {
1315 Class C = classify(Ty);
1316 if (C == Float)
1317 return false;
1318 }
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001319
Rafael Espindola077dd592012-10-24 01:58:58 +00001320 unsigned Size = getContext().getTypeSize(Ty);
1321 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindolae2a9e902012-10-23 02:04:01 +00001322
1323 if (SizeInRegs == 0)
1324 return false;
1325
Michael Kuperstein68901882015-10-25 08:18:20 +00001326 if (!IsMCUABI) {
1327 if (SizeInRegs > State.FreeRegs) {
1328 State.FreeRegs = 0;
1329 return false;
1330 }
1331 } else {
1332 // The MCU psABI allows passing parameters in-reg even if there are
1333 // earlier parameters that are passed on the stack. Also,
1334 // it does not allow passing >8-byte structs in-register,
1335 // even if there are 3 free registers available.
1336 if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1337 return false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001338 }
Rafael Espindola703c47f2012-10-19 05:04:37 +00001339
Reid Kleckner661f35b2014-01-18 01:12:41 +00001340 State.FreeRegs -= SizeInRegs;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001341 return true;
1342}
1343
1344bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1345 bool &InReg,
1346 bool &NeedsPadding) const {
1347 NeedsPadding = false;
1348 InReg = !IsMCUABI;
1349
1350 if (!updateFreeRegs(Ty, State))
1351 return false;
1352
1353 if (IsMCUABI)
1354 return true;
Rafael Espindola077dd592012-10-24 01:58:58 +00001355
Reid Kleckner80944df2014-10-31 22:00:51 +00001356 if (State.CC == llvm::CallingConv::X86_FastCall ||
1357 State.CC == llvm::CallingConv::X86_VectorCall) {
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001358 if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
Rafael Espindolafad28de2012-10-24 01:59:00 +00001359 NeedsPadding = true;
1360
Rafael Espindola077dd592012-10-24 01:58:58 +00001361 return false;
1362 }
1363
Rafael Espindola703c47f2012-10-19 05:04:37 +00001364 return true;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001365}
1366
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001367bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1368 if (!updateFreeRegs(Ty, State))
1369 return false;
1370
1371 if (IsMCUABI)
1372 return false;
1373
1374 if (State.CC == llvm::CallingConv::X86_FastCall ||
1375 State.CC == llvm::CallingConv::X86_VectorCall) {
1376 if (getContext().getTypeSize(Ty) > 32)
1377 return false;
1378
1379 return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1380 Ty->isReferenceType());
1381 }
1382
1383 return true;
1384}
1385
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001386ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1387 CCState &State) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001388 // FIXME: Set alignment on indirect arguments.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001389
Reid Klecknerb1be6832014-11-15 01:41:41 +00001390 Ty = useFirstFieldIfTransparentUnion(Ty);
1391
Reid Kleckner80944df2014-10-31 22:00:51 +00001392 // Check with the C++ ABI first.
1393 const RecordType *RT = Ty->getAs<RecordType>();
1394 if (RT) {
1395 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1396 if (RAA == CGCXXABI::RAA_Indirect) {
1397 return getIndirectResult(Ty, false, State);
1398 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1399 // The field index doesn't matter, we'll fix it up later.
1400 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1401 }
1402 }
1403
1404 // vectorcall adds the concept of a homogenous vector aggregate, similar
1405 // to other targets.
1406 const Type *Base = nullptr;
1407 uint64_t NumElts = 0;
1408 if (State.CC == llvm::CallingConv::X86_VectorCall &&
1409 isHomogeneousAggregate(Ty, Base, NumElts)) {
1410 if (State.FreeSSERegs >= NumElts) {
1411 State.FreeSSERegs -= NumElts;
1412 if (Ty->isBuiltinType() || Ty->isVectorType())
1413 return ABIArgInfo::getDirect();
1414 return ABIArgInfo::getExpand();
1415 }
1416 return getIndirectResult(Ty, /*ByVal=*/false, State);
1417 }
1418
1419 if (isAggregateTypeForABI(Ty)) {
1420 if (RT) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001421 // Structs are always byval on win32, regardless of what they contain.
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001422 if (IsWin32StructABI)
Reid Kleckner661f35b2014-01-18 01:12:41 +00001423 return getIndirectResult(Ty, true, State);
Daniel Dunbar557893d2010-04-21 19:10:51 +00001424
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001425 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001426 if (RT->getDecl()->hasFlexibleArrayMember())
Reid Kleckner661f35b2014-01-18 01:12:41 +00001427 return getIndirectResult(Ty, true, State);
Anders Carlsson40446e82010-01-27 03:25:19 +00001428 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001429
Eli Friedman9f061a32011-11-18 00:28:11 +00001430 // Ignore empty structs/unions.
Eli Friedmanf22fa9e2011-11-18 04:01:36 +00001431 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001432 return ABIArgInfo::getIgnore();
1433
Rafael Espindolafad28de2012-10-24 01:59:00 +00001434 llvm::LLVMContext &LLVMContext = getVMContext();
1435 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001436 bool NeedsPadding, InReg;
1437 if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001438 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperac9201a2013-07-08 04:47:18 +00001439 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001440 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001441 if (InReg)
1442 return ABIArgInfo::getDirectInReg(Result);
1443 else
1444 return ABIArgInfo::getDirect(Result);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001445 }
Craig Topper8a13c412014-05-21 05:09:00 +00001446 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
Rafael Espindola703c47f2012-10-19 05:04:37 +00001447
Daniel Dunbar11c08c82009-11-09 01:33:53 +00001448 // Expand small (<= 128-bit) record types when we know that the stack layout
1449 // of those arguments will match the struct. This is important because the
1450 // LLVM backend isn't smart enough to remove byval, which inhibits many
1451 // optimizations.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001452 // Don't do this for the MCU if there are still free integer registers
1453 // (see X86_64 ABI for full explanation).
Chris Lattner458b2aa2010-07-29 02:16:43 +00001454 if (getContext().getTypeSize(Ty) <= 4*32 &&
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001455 canExpandIndirectArgument(Ty, getContext()) &&
1456 (!IsMCUABI || State.FreeRegs == 0))
Reid Kleckner661f35b2014-01-18 01:12:41 +00001457 return ABIArgInfo::getExpandWithPadding(
Reid Kleckner80944df2014-10-31 22:00:51 +00001458 State.CC == llvm::CallingConv::X86_FastCall ||
1459 State.CC == llvm::CallingConv::X86_VectorCall,
1460 PaddingType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001461
Reid Kleckner661f35b2014-01-18 01:12:41 +00001462 return getIndirectResult(Ty, true, State);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001463 }
1464
Chris Lattnerd774ae92010-08-26 20:05:13 +00001465 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +00001466 // On Darwin, some vectors are passed in memory, we handle this by passing
1467 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +00001468 if (IsDarwinVectorABI) {
1469 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +00001470 if ((Size == 8 || Size == 16 || Size == 32) ||
1471 (Size == 64 && VT->getNumElements() == 1))
1472 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1473 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +00001474 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001475
Chad Rosier651c1832013-03-25 21:00:27 +00001476 if (IsX86_MMXType(CGT.ConvertType(Ty)))
1477 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001478
Chris Lattnerd774ae92010-08-26 20:05:13 +00001479 return ABIArgInfo::getDirect();
1480 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001481
1482
Chris Lattner458b2aa2010-07-29 02:16:43 +00001483 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1484 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +00001485
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001486 bool InReg = shouldPrimitiveUseInReg(Ty, State);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001487
1488 if (Ty->isPromotableIntegerType()) {
1489 if (InReg)
1490 return ABIArgInfo::getExtendInReg();
1491 return ABIArgInfo::getExtend();
1492 }
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001493
Rafael Espindola703c47f2012-10-19 05:04:37 +00001494 if (InReg)
1495 return ABIArgInfo::getDirectInReg();
1496 return ABIArgInfo::getDirect();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001497}
1498
Rafael Espindolaa6472962012-07-24 00:01:07 +00001499void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001500 CCState State(FI.getCallingConvention());
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001501 if (IsMCUABI)
1502 State.FreeRegs = 3;
1503 else if (State.CC == llvm::CallingConv::X86_FastCall)
Reid Kleckner661f35b2014-01-18 01:12:41 +00001504 State.FreeRegs = 2;
Reid Kleckner80944df2014-10-31 22:00:51 +00001505 else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1506 State.FreeRegs = 2;
1507 State.FreeSSERegs = 6;
1508 } else if (FI.getHasRegParm())
Reid Kleckner661f35b2014-01-18 01:12:41 +00001509 State.FreeRegs = FI.getRegParm();
Rafael Espindola077dd592012-10-24 01:58:58 +00001510 else
Reid Kleckner661f35b2014-01-18 01:12:41 +00001511 State.FreeRegs = DefaultNumRegisterParameters;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001512
Reid Kleckner677539d2014-07-10 01:58:55 +00001513 if (!getCXXABI().classifyReturnType(FI)) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00001514 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
Reid Kleckner677539d2014-07-10 01:58:55 +00001515 } else if (FI.getReturnInfo().isIndirect()) {
1516 // The C++ ABI is not aware of register usage, so we have to check if the
1517 // return value was sret and put it in a register ourselves if appropriate.
1518 if (State.FreeRegs) {
1519 --State.FreeRegs; // The sret parameter consumes a register.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001520 if (!IsMCUABI)
1521 FI.getReturnInfo().setInReg(true);
Reid Kleckner677539d2014-07-10 01:58:55 +00001522 }
1523 }
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001524
Peter Collingbournef7706832014-12-12 23:41:25 +00001525 // The chain argument effectively gives us another free register.
1526 if (FI.isChainCall())
1527 ++State.FreeRegs;
1528
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001529 bool UsedInAlloca = false;
Aaron Ballmanec47bc22014-03-17 18:10:01 +00001530 for (auto &I : FI.arguments()) {
1531 I.info = classifyArgumentType(I.type, State);
1532 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001533 }
1534
1535 // If we needed to use inalloca for any argument, do a second pass and rewrite
1536 // all the memory arguments to use inalloca.
1537 if (UsedInAlloca)
1538 rewriteWithInAlloca(FI);
1539}
1540
1541void
1542X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001543 CharUnits &StackOffset, ABIArgInfo &Info,
1544 QualType Type) const {
1545 // Arguments are always 4-byte-aligned.
1546 CharUnits FieldAlign = CharUnits::fromQuantity(4);
1547
1548 assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
Reid Klecknerd378a712014-04-10 19:09:43 +00001549 Info = ABIArgInfo::getInAlloca(FrameFields.size());
1550 FrameFields.push_back(CGT.ConvertTypeForMem(Type));
John McCall7f416cc2015-09-08 08:05:57 +00001551 StackOffset += getContext().getTypeSizeInChars(Type);
Reid Klecknerd378a712014-04-10 19:09:43 +00001552
John McCall7f416cc2015-09-08 08:05:57 +00001553 // Insert padding bytes to respect alignment.
1554 CharUnits FieldEnd = StackOffset;
Rui Ueyama83aa9792016-01-14 21:00:27 +00001555 StackOffset = FieldEnd.alignTo(FieldAlign);
John McCall7f416cc2015-09-08 08:05:57 +00001556 if (StackOffset != FieldEnd) {
1557 CharUnits NumBytes = StackOffset - FieldEnd;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001558 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
John McCall7f416cc2015-09-08 08:05:57 +00001559 Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001560 FrameFields.push_back(Ty);
1561 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001562}
1563
Reid Kleckner852361d2014-07-26 00:12:26 +00001564static bool isArgInAlloca(const ABIArgInfo &Info) {
1565 // Leave ignored and inreg arguments alone.
1566 switch (Info.getKind()) {
1567 case ABIArgInfo::InAlloca:
1568 return true;
1569 case ABIArgInfo::Indirect:
1570 assert(Info.getIndirectByVal());
1571 return true;
1572 case ABIArgInfo::Ignore:
1573 return false;
1574 case ABIArgInfo::Direct:
1575 case ABIArgInfo::Extend:
1576 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00001577 case ABIArgInfo::CoerceAndExpand:
Reid Kleckner852361d2014-07-26 00:12:26 +00001578 if (Info.getInReg())
1579 return false;
1580 return true;
1581 }
1582 llvm_unreachable("invalid enum");
1583}
1584
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001585void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1586 assert(IsWin32StructABI && "inalloca only supported on win32");
1587
1588 // Build a packed struct type for all of the arguments in memory.
1589 SmallVector<llvm::Type *, 6> FrameFields;
1590
John McCall7f416cc2015-09-08 08:05:57 +00001591 // The stack alignment is always 4.
1592 CharUnits StackAlign = CharUnits::fromQuantity(4);
1593
1594 CharUnits StackOffset;
Reid Kleckner852361d2014-07-26 00:12:26 +00001595 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1596
1597 // Put 'this' into the struct before 'sret', if necessary.
1598 bool IsThisCall =
1599 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1600 ABIArgInfo &Ret = FI.getReturnInfo();
1601 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1602 isArgInAlloca(I->info)) {
1603 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1604 ++I;
1605 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001606
1607 // Put the sret parameter into the inalloca struct if it's in memory.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001608 if (Ret.isIndirect() && !Ret.getInReg()) {
1609 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1610 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
Reid Klecknerfab1e892014-02-25 00:59:14 +00001611 // On Windows, the hidden sret parameter is always returned in eax.
1612 Ret.setInAllocaSRet(IsWin32StructABI);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001613 }
1614
1615 // Skip the 'this' parameter in ecx.
Reid Kleckner852361d2014-07-26 00:12:26 +00001616 if (IsThisCall)
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001617 ++I;
1618
1619 // Put arguments passed in memory into the struct.
1620 for (; I != E; ++I) {
Reid Kleckner852361d2014-07-26 00:12:26 +00001621 if (isArgInAlloca(I->info))
1622 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001623 }
1624
1625 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001626 /*isPacked=*/true),
1627 StackAlign);
Rafael Espindolaa6472962012-07-24 00:01:07 +00001628}
1629
John McCall7f416cc2015-09-08 08:05:57 +00001630Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
1631 Address VAListAddr, QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001632
John McCall7f416cc2015-09-08 08:05:57 +00001633 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001634
John McCall7f416cc2015-09-08 08:05:57 +00001635 // x86-32 changes the alignment of certain arguments on the stack.
1636 //
1637 // Just messing with TypeInfo like this works because we never pass
1638 // anything indirectly.
1639 TypeInfo.second = CharUnits::fromQuantity(
1640 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001641
John McCall7f416cc2015-09-08 08:05:57 +00001642 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
1643 TypeInfo, CharUnits::fromQuantity(4),
1644 /*AllowHigherAlign*/ true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001645}
1646
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001647bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1648 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1649 assert(Triple.getArch() == llvm::Triple::x86);
1650
1651 switch (Opts.getStructReturnConvention()) {
1652 case CodeGenOptions::SRCK_Default:
1653 break;
1654 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
1655 return false;
1656 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
1657 return true;
1658 }
1659
Michael Kupersteind749f232015-10-27 07:46:22 +00001660 if (Triple.isOSDarwin() || Triple.isOSIAMCU())
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001661 return true;
1662
1663 switch (Triple.getOS()) {
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001664 case llvm::Triple::DragonFly:
1665 case llvm::Triple::FreeBSD:
1666 case llvm::Triple::OpenBSD:
1667 case llvm::Triple::Bitrig:
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001668 case llvm::Triple::Win32:
Reid Kleckner2918fef2014-11-24 22:05:42 +00001669 return true;
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001670 default:
1671 return false;
1672 }
1673}
1674
Eric Christopher162c91c2015-06-05 22:03:00 +00001675void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Charles Davis4ea31ab2010-02-13 15:54:06 +00001676 llvm::GlobalValue *GV,
1677 CodeGen::CodeGenModule &CGM) const {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00001678 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Charles Davis4ea31ab2010-02-13 15:54:06 +00001679 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1680 // Get the LLVM function.
1681 llvm::Function *Fn = cast<llvm::Function>(GV);
1682
1683 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001684 llvm::AttrBuilder B;
Bill Wendlingccf94c92012-10-14 03:28:14 +00001685 B.addStackAlignmentAttr(16);
Bill Wendling9a677922013-01-23 00:21:06 +00001686 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1687 llvm::AttributeSet::get(CGM.getLLVMContext(),
1688 llvm::AttributeSet::FunctionIndex,
1689 B));
Charles Davis4ea31ab2010-02-13 15:54:06 +00001690 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00001691 if (FD->hasAttr<AnyX86InterruptAttr>()) {
1692 llvm::Function *Fn = cast<llvm::Function>(GV);
1693 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
1694 }
Charles Davis4ea31ab2010-02-13 15:54:06 +00001695 }
1696}
1697
John McCallbeec5a02010-03-06 00:35:14 +00001698bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1699 CodeGen::CodeGenFunction &CGF,
1700 llvm::Value *Address) const {
1701 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallbeec5a02010-03-06 00:35:14 +00001702
Chris Lattnerece04092012-02-07 00:39:47 +00001703 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001704
John McCallbeec5a02010-03-06 00:35:14 +00001705 // 0-7 are the eight integer registers; the order is different
1706 // on Darwin (for EH), but the range is the same.
1707 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +00001708 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +00001709
John McCallc8e01702013-04-16 22:48:15 +00001710 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCallbeec5a02010-03-06 00:35:14 +00001711 // 12-16 are st(0..4). Not sure why we stop at 4.
1712 // These have size 16, which is sizeof(long double) on
1713 // platforms with 8-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001714 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCall943fae92010-05-27 06:19:26 +00001715 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001716
John McCallbeec5a02010-03-06 00:35:14 +00001717 } else {
1718 // 9 is %eflags, which doesn't get a size on Darwin for some
1719 // reason.
John McCall7f416cc2015-09-08 08:05:57 +00001720 Builder.CreateAlignedStore(
1721 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
1722 CharUnits::One());
John McCallbeec5a02010-03-06 00:35:14 +00001723
1724 // 11-16 are st(0..5). Not sure why we stop at 5.
1725 // These have size 12, which is sizeof(long double) on
1726 // platforms with 4-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001727 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCall943fae92010-05-27 06:19:26 +00001728 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1729 }
John McCallbeec5a02010-03-06 00:35:14 +00001730
1731 return false;
1732}
1733
Chris Lattner0cf24192010-06-28 20:05:43 +00001734//===----------------------------------------------------------------------===//
1735// X86-64 ABI Implementation
1736//===----------------------------------------------------------------------===//
1737
1738
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001739namespace {
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001740/// The AVX ABI level for X86 targets.
1741enum class X86AVXABILevel {
1742 None,
Ahmed Bougacha0b938282015-06-22 21:31:43 +00001743 AVX,
1744 AVX512
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001745};
1746
1747/// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
1748static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
1749 switch (AVXLevel) {
Ahmed Bougacha0b938282015-06-22 21:31:43 +00001750 case X86AVXABILevel::AVX512:
1751 return 512;
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001752 case X86AVXABILevel::AVX:
1753 return 256;
1754 case X86AVXABILevel::None:
1755 return 128;
1756 }
Yaron Kerenb76cb042015-06-23 09:45:42 +00001757 llvm_unreachable("Unknown AVXLevel");
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001758}
1759
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001760/// X86_64ABIInfo - The X86_64 ABI information.
1761class X86_64ABIInfo : public ABIInfo {
1762 enum Class {
1763 Integer = 0,
1764 SSE,
1765 SSEUp,
1766 X87,
1767 X87Up,
1768 ComplexX87,
1769 NoClass,
1770 Memory
1771 };
1772
1773 /// merge - Implement the X86_64 ABI merging algorithm.
1774 ///
1775 /// Merge an accumulating classification \arg Accum with a field
1776 /// classification \arg Field.
1777 ///
1778 /// \param Accum - The accumulating classification. This should
1779 /// always be either NoClass or the result of a previous merge
1780 /// call. In addition, this should never be Memory (the caller
1781 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001782 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001783
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001784 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1785 ///
1786 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1787 /// final MEMORY or SSE classes when necessary.
1788 ///
1789 /// \param AggregateSize - The size of the current aggregate in
1790 /// the classification process.
1791 ///
1792 /// \param Lo - The classification for the parts of the type
1793 /// residing in the low word of the containing object.
1794 ///
1795 /// \param Hi - The classification for the parts of the type
1796 /// residing in the higher words of the containing object.
1797 ///
1798 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1799
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001800 /// classify - Determine the x86_64 register classes in which the
1801 /// given type T should be passed.
1802 ///
1803 /// \param Lo - The classification for the parts of the type
1804 /// residing in the low word of the containing object.
1805 ///
1806 /// \param Hi - The classification for the parts of the type
1807 /// residing in the high word of the containing object.
1808 ///
1809 /// \param OffsetBase - The bit offset of this type in the
1810 /// containing object. Some parameters are classified different
1811 /// depending on whether they straddle an eightbyte boundary.
1812 ///
Eli Friedman96fd2642013-06-12 00:13:45 +00001813 /// \param isNamedArg - Whether the argument in question is a "named"
1814 /// argument, as used in AMD64-ABI 3.5.7.
1815 ///
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001816 /// If a word is unused its result will be NoClass; if a type should
1817 /// be passed in Memory then at least the classification of \arg Lo
1818 /// will be Memory.
1819 ///
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00001820 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001821 ///
1822 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1823 /// also be ComplexX87.
Eli Friedman96fd2642013-06-12 00:13:45 +00001824 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1825 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001826
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001827 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001828 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1829 unsigned IROffset, QualType SourceTy,
1830 unsigned SourceOffset) const;
1831 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1832 unsigned IROffset, QualType SourceTy,
1833 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001834
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001835 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +00001836 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +00001837 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001838
1839 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001840 /// such that the argument will be passed in memory.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001841 ///
1842 /// \param freeIntRegs - The number of free integer registers remaining
1843 /// available.
1844 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001845
Chris Lattner458b2aa2010-07-29 02:16:43 +00001846 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001847
Bill Wendling5cd41c42010-10-18 03:41:31 +00001848 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001849 unsigned freeIntRegs,
Bill Wendling5cd41c42010-10-18 03:41:31 +00001850 unsigned &neededInt,
Eli Friedman96fd2642013-06-12 00:13:45 +00001851 unsigned &neededSSE,
1852 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001853
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001854 bool IsIllegalVectorType(QualType Ty) const;
1855
John McCalle0fda732011-04-21 01:20:55 +00001856 /// The 0.98 ABI revision clarified a lot of ambiguities,
1857 /// unfortunately in ways that were not always consistent with
1858 /// certain previous compilers. In particular, platforms which
1859 /// required strict binary compatibility with older versions of GCC
1860 /// may need to exempt themselves.
1861 bool honorsRevision0_98() const {
John McCallc8e01702013-04-16 22:48:15 +00001862 return !getTarget().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +00001863 }
1864
David Majnemere2ae2282016-03-04 05:26:16 +00001865 /// GCC classifies <1 x long long> as SSE but compatibility with older clang
1866 // compilers require us to classify it as INTEGER.
1867 bool classifyIntegerMMXAsSSE() const {
1868 const llvm::Triple &Triple = getTarget().getTriple();
1869 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
1870 return false;
1871 if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
1872 return false;
1873 return true;
1874 }
1875
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001876 X86AVXABILevel AVXLevel;
Derek Schuffc7dd7222012-10-11 15:52:22 +00001877 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1878 // 64-bit hardware.
1879 bool Has64BitPointers;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001880
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001881public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001882 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
1883 ABIInfo(CGT), AVXLevel(AVXLevel),
Derek Schuff8a872f32012-10-11 18:21:13 +00001884 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffc7dd7222012-10-11 15:52:22 +00001885 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001886
John McCalla729c622012-02-17 03:33:10 +00001887 bool isPassedUsingAVXType(QualType type) const {
1888 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001889 // The freeIntRegs argument doesn't matter here.
Eli Friedman96fd2642013-06-12 00:13:45 +00001890 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1891 /*isNamedArg*/true);
John McCalla729c622012-02-17 03:33:10 +00001892 if (info.isDirect()) {
1893 llvm::Type *ty = info.getCoerceToType();
1894 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1895 return (vectorTy->getBitWidth() > 128);
1896 }
1897 return false;
1898 }
1899
Craig Topper4f12f102014-03-12 06:41:41 +00001900 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001901
John McCall7f416cc2015-09-08 08:05:57 +00001902 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1903 QualType Ty) const override;
Charles Davisc7d5c942015-09-17 20:55:33 +00001904 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
1905 QualType Ty) const override;
Peter Collingbourne69b004d2015-02-25 23:18:42 +00001906
1907 bool has64BitPointers() const {
1908 return Has64BitPointers;
1909 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001910};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001911
Chris Lattner04dc9572010-08-31 16:44:54 +00001912/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00001913class WinX86_64ABIInfo : public ABIInfo {
Chris Lattner04dc9572010-08-31 16:44:54 +00001914public:
Reid Kleckner11a17192015-10-28 22:29:52 +00001915 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT)
1916 : ABIInfo(CGT),
1917 IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00001918
Craig Topper4f12f102014-03-12 06:41:41 +00001919 void computeInfo(CGFunctionInfo &FI) const override;
Chris Lattner04dc9572010-08-31 16:44:54 +00001920
John McCall7f416cc2015-09-08 08:05:57 +00001921 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1922 QualType Ty) const override;
Reid Kleckner80944df2014-10-31 22:00:51 +00001923
1924 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
1925 // FIXME: Assumes vectorcall is in use.
1926 return isX86VectorTypeForVectorCall(getContext(), Ty);
1927 }
1928
1929 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
1930 uint64_t NumMembers) const override {
1931 // FIXME: Assumes vectorcall is in use.
1932 return isX86VectorCallAggregateSmallEnough(NumMembers);
1933 }
Reid Kleckner11a17192015-10-28 22:29:52 +00001934
1935private:
1936 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs,
1937 bool IsReturnType) const;
1938
1939 bool IsMingw64;
Chris Lattner04dc9572010-08-31 16:44:54 +00001940};
1941
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001942class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1943public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001944 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
Alexey Bataev00396512015-07-02 03:40:19 +00001945 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001946
John McCalla729c622012-02-17 03:33:10 +00001947 const X86_64ABIInfo &getABIInfo() const {
1948 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1949 }
1950
Craig Topper4f12f102014-03-12 06:41:41 +00001951 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +00001952 return 7;
1953 }
1954
1955 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00001956 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00001957 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001958
John McCall943fae92010-05-27 06:19:26 +00001959 // 0-15 are the 16 integer registers.
1960 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00001961 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +00001962 return false;
1963 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001964
Jay Foad7c57be32011-07-11 09:56:20 +00001965 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001966 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +00001967 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001968 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1969 }
1970
John McCalla729c622012-02-17 03:33:10 +00001971 bool isNoProtoCallVariadic(const CallArgList &args,
Craig Topper4f12f102014-03-12 06:41:41 +00001972 const FunctionNoProtoType *fnType) const override {
John McCallcbc038a2011-09-21 08:08:30 +00001973 // The default CC on x86-64 sets %al to the number of SSA
1974 // registers used, and GCC sets this when calling an unprototyped
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001975 // function, so we override the default behavior. However, don't do
Eli Friedmanb8e45b22011-12-06 03:08:26 +00001976 // that when AVX types are involved: the ABI explicitly states it is
1977 // undefined, and it doesn't work in practice because of how the ABI
1978 // defines varargs anyway.
Reid Kleckner78af0702013-08-27 23:08:25 +00001979 if (fnType->getCallConv() == CC_C) {
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001980 bool HasAVXType = false;
John McCalla729c622012-02-17 03:33:10 +00001981 for (CallArgList::const_iterator
1982 it = args.begin(), ie = args.end(); it != ie; ++it) {
1983 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1984 HasAVXType = true;
1985 break;
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001986 }
1987 }
John McCalla729c622012-02-17 03:33:10 +00001988
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001989 if (!HasAVXType)
1990 return true;
1991 }
John McCallcbc038a2011-09-21 08:08:30 +00001992
John McCalla729c622012-02-17 03:33:10 +00001993 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCallcbc038a2011-09-21 08:08:30 +00001994 }
1995
Craig Topper4f12f102014-03-12 06:41:41 +00001996 llvm::Constant *
1997 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourne69b004d2015-02-25 23:18:42 +00001998 unsigned Sig;
1999 if (getABIInfo().has64BitPointers())
2000 Sig = (0xeb << 0) | // jmp rel8
2001 (0x0a << 8) | // .+0x0c
2002 ('F' << 16) |
2003 ('T' << 24);
2004 else
2005 Sig = (0xeb << 0) | // jmp rel8
2006 (0x06 << 8) | // .+0x08
2007 ('F' << 16) |
2008 ('T' << 24);
Peter Collingbourneb453cd62013-10-20 21:29:19 +00002009 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2010 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00002011
2012 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2013 CodeGen::CodeGenModule &CGM) const override {
2014 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2015 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2016 llvm::Function *Fn = cast<llvm::Function>(GV);
2017 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2018 }
2019 }
2020 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002021};
2022
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002023class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
2024public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002025 PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2026 : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002027
2028 void getDependentLibraryOption(llvm::StringRef Lib,
Alexander Kornienko34eb2072015-04-11 02:00:23 +00002029 llvm::SmallString<24> &Opt) const override {
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002030 Opt = "\01";
Yunzhong Gaod65200c2015-07-20 17:46:56 +00002031 // If the argument contains a space, enclose it in quotes.
2032 if (Lib.find(" ") != StringRef::npos)
2033 Opt += "\"" + Lib.str() + "\"";
2034 else
2035 Opt += Lib;
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002036 }
2037};
2038
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002039static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
Michael Kupersteinf0e4ccf2015-02-16 11:57:43 +00002040 // If the argument does not end in .lib, automatically add the suffix.
2041 // If the argument contains a space, enclose it in quotes.
2042 // This matches the behavior of MSVC.
2043 bool Quote = (Lib.find(" ") != StringRef::npos);
2044 std::string ArgStr = Quote ? "\"" : "";
2045 ArgStr += Lib;
Rui Ueyama727025a2013-10-31 19:12:53 +00002046 if (!Lib.endswith_lower(".lib"))
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002047 ArgStr += ".lib";
Michael Kupersteinf0e4ccf2015-02-16 11:57:43 +00002048 ArgStr += Quote ? "\"" : "";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002049 return ArgStr;
2050}
2051
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002052class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2053public:
John McCall1fe2a8c2013-06-18 02:46:29 +00002054 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Michael Kupersteindc745202015-10-19 07:52:25 +00002055 bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2056 unsigned NumRegisterParameters)
2057 : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00002058 Win32StructABI, NumRegisterParameters, false) {}
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002059
Eric Christopher162c91c2015-06-05 22:03:00 +00002060 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Hans Wennborg77dc2362015-01-20 19:45:50 +00002061 CodeGen::CodeGenModule &CGM) const override;
2062
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002063 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00002064 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002065 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002066 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002067 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00002068
2069 void getDetectMismatchOption(llvm::StringRef Name,
2070 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00002071 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00002072 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00002073 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002074};
2075
Hans Wennborg77dc2362015-01-20 19:45:50 +00002076static void addStackProbeSizeTargetAttribute(const Decl *D,
2077 llvm::GlobalValue *GV,
2078 CodeGen::CodeGenModule &CGM) {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00002079 if (D && isa<FunctionDecl>(D)) {
Hans Wennborg77dc2362015-01-20 19:45:50 +00002080 if (CGM.getCodeGenOpts().StackProbeSize != 4096) {
2081 llvm::Function *Fn = cast<llvm::Function>(GV);
2082
Eric Christopher7565e0d2015-05-29 23:09:49 +00002083 Fn->addFnAttr("stack-probe-size",
2084 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
Hans Wennborg77dc2362015-01-20 19:45:50 +00002085 }
2086 }
2087}
2088
Eric Christopher162c91c2015-06-05 22:03:00 +00002089void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Hans Wennborg77dc2362015-01-20 19:45:50 +00002090 llvm::GlobalValue *GV,
2091 CodeGen::CodeGenModule &CGM) const {
Eric Christopher162c91c2015-06-05 22:03:00 +00002092 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Hans Wennborg77dc2362015-01-20 19:45:50 +00002093
2094 addStackProbeSizeTargetAttribute(D, GV, CGM);
2095}
2096
Chris Lattner04dc9572010-08-31 16:44:54 +00002097class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2098public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002099 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2100 X86AVXABILevel AVXLevel)
Alexey Bataev00396512015-07-02 03:40:19 +00002101 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
Chris Lattner04dc9572010-08-31 16:44:54 +00002102
Eric Christopher162c91c2015-06-05 22:03:00 +00002103 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Hans Wennborg77dc2362015-01-20 19:45:50 +00002104 CodeGen::CodeGenModule &CGM) const override;
2105
Craig Topper4f12f102014-03-12 06:41:41 +00002106 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
Chris Lattner04dc9572010-08-31 16:44:54 +00002107 return 7;
2108 }
2109
2110 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00002111 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00002112 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002113
Chris Lattner04dc9572010-08-31 16:44:54 +00002114 // 0-15 are the 16 integer registers.
2115 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00002116 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattner04dc9572010-08-31 16:44:54 +00002117 return false;
2118 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002119
2120 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00002121 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002122 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002123 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002124 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00002125
2126 void getDetectMismatchOption(llvm::StringRef Name,
2127 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00002128 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00002129 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00002130 }
Chris Lattner04dc9572010-08-31 16:44:54 +00002131};
2132
Eric Christopher162c91c2015-06-05 22:03:00 +00002133void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Hans Wennborg77dc2362015-01-20 19:45:50 +00002134 llvm::GlobalValue *GV,
2135 CodeGen::CodeGenModule &CGM) const {
Eric Christopher162c91c2015-06-05 22:03:00 +00002136 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Hans Wennborg77dc2362015-01-20 19:45:50 +00002137
Alexey Bataevd51e9932016-01-15 04:06:31 +00002138 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2139 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2140 llvm::Function *Fn = cast<llvm::Function>(GV);
2141 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2142 }
2143 }
2144
Hans Wennborg77dc2362015-01-20 19:45:50 +00002145 addStackProbeSizeTargetAttribute(D, GV, CGM);
2146}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002147}
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002148
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002149void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2150 Class &Hi) const {
2151 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2152 //
2153 // (a) If one of the classes is Memory, the whole argument is passed in
2154 // memory.
2155 //
2156 // (b) If X87UP is not preceded by X87, the whole argument is passed in
2157 // memory.
2158 //
2159 // (c) If the size of the aggregate exceeds two eightbytes and the first
2160 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2161 // argument is passed in memory. NOTE: This is necessary to keep the
2162 // ABI working for processors that don't support the __m256 type.
2163 //
2164 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2165 //
2166 // Some of these are enforced by the merging logic. Others can arise
2167 // only with unions; for example:
2168 // union { _Complex double; unsigned; }
2169 //
2170 // Note that clauses (b) and (c) were added in 0.98.
2171 //
2172 if (Hi == Memory)
2173 Lo = Memory;
2174 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2175 Lo = Memory;
2176 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2177 Lo = Memory;
2178 if (Hi == SSEUp && Lo != SSE)
2179 Hi = SSE;
2180}
2181
Chris Lattnerd776fb12010-06-28 21:43:59 +00002182X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002183 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2184 // classified recursively so that always two fields are
2185 // considered. The resulting class is calculated according to
2186 // the classes of the fields in the eightbyte:
2187 //
2188 // (a) If both classes are equal, this is the resulting class.
2189 //
2190 // (b) If one of the classes is NO_CLASS, the resulting class is
2191 // the other class.
2192 //
2193 // (c) If one of the classes is MEMORY, the result is the MEMORY
2194 // class.
2195 //
2196 // (d) If one of the classes is INTEGER, the result is the
2197 // INTEGER.
2198 //
2199 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2200 // MEMORY is used as class.
2201 //
2202 // (f) Otherwise class SSE is used.
2203
2204 // Accum should never be memory (we should have returned) or
2205 // ComplexX87 (because this cannot be passed in a structure).
2206 assert((Accum != Memory && Accum != ComplexX87) &&
2207 "Invalid accumulated classification during merge.");
2208 if (Accum == Field || Field == NoClass)
2209 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002210 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002211 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002212 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002213 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002214 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002215 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002216 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2217 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002218 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002219 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002220}
2221
Chris Lattner5c740f12010-06-30 19:14:05 +00002222void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman96fd2642013-06-12 00:13:45 +00002223 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002224 // FIXME: This code can be simplified by introducing a simple value class for
2225 // Class pairs with appropriate constructor methods for the various
2226 // situations.
2227
2228 // FIXME: Some of the split computations are wrong; unaligned vectors
2229 // shouldn't be passed in registers for example, so there is no chance they
2230 // can straddle an eightbyte. Verify & simplify.
2231
2232 Lo = Hi = NoClass;
2233
2234 Class &Current = OffsetBase < 64 ? Lo : Hi;
2235 Current = Memory;
2236
John McCall9dd450b2009-09-21 23:43:11 +00002237 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002238 BuiltinType::Kind k = BT->getKind();
2239
2240 if (k == BuiltinType::Void) {
2241 Current = NoClass;
2242 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2243 Lo = Integer;
2244 Hi = Integer;
2245 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2246 Current = Integer;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002247 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002248 Current = SSE;
2249 } else if (k == BuiltinType::LongDouble) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002250 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2251 if (LDF == &llvm::APFloat::IEEEquad) {
2252 Lo = SSE;
2253 Hi = SSEUp;
2254 } else if (LDF == &llvm::APFloat::x87DoubleExtended) {
2255 Lo = X87;
2256 Hi = X87Up;
2257 } else if (LDF == &llvm::APFloat::IEEEdouble) {
2258 Current = SSE;
2259 } else
2260 llvm_unreachable("unexpected long double representation!");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002261 }
2262 // FIXME: _Decimal32 and _Decimal64 are SSE.
2263 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00002264 return;
2265 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002266
Chris Lattnerd776fb12010-06-28 21:43:59 +00002267 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002268 // Classify the underlying integer type.
Eli Friedman96fd2642013-06-12 00:13:45 +00002269 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002270 return;
2271 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002272
Chris Lattnerd776fb12010-06-28 21:43:59 +00002273 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002274 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002275 return;
2276 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002277
Chris Lattnerd776fb12010-06-28 21:43:59 +00002278 if (Ty->isMemberPointerType()) {
Jan Wen Voung01c21e82014-10-02 16:56:57 +00002279 if (Ty->isMemberFunctionPointerType()) {
2280 if (Has64BitPointers) {
2281 // If Has64BitPointers, this is an {i64, i64}, so classify both
2282 // Lo and Hi now.
2283 Lo = Hi = Integer;
2284 } else {
2285 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2286 // straddles an eightbyte boundary, Hi should be classified as well.
2287 uint64_t EB_FuncPtr = (OffsetBase) / 64;
2288 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2289 if (EB_FuncPtr != EB_ThisAdj) {
2290 Lo = Hi = Integer;
2291 } else {
2292 Current = Integer;
2293 }
2294 }
2295 } else {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00002296 Current = Integer;
Jan Wen Voung01c21e82014-10-02 16:56:57 +00002297 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00002298 return;
2299 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002300
Chris Lattnerd776fb12010-06-28 21:43:59 +00002301 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002302 uint64_t Size = getContext().getTypeSize(VT);
David Majnemerf8d14db2015-07-17 05:49:13 +00002303 if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2304 // gcc passes the following as integer:
2305 // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2306 // 2 bytes - <2 x char>, <1 x short>
2307 // 1 byte - <1 x char>
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002308 Current = Integer;
2309
2310 // If this type crosses an eightbyte boundary, it should be
2311 // split.
David Majnemerf8d14db2015-07-17 05:49:13 +00002312 uint64_t EB_Lo = (OffsetBase) / 64;
2313 uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2314 if (EB_Lo != EB_Hi)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002315 Hi = Lo;
2316 } else if (Size == 64) {
David Majnemere2ae2282016-03-04 05:26:16 +00002317 QualType ElementType = VT->getElementType();
2318
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002319 // gcc passes <1 x double> in memory. :(
David Majnemere2ae2282016-03-04 05:26:16 +00002320 if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002321 return;
2322
David Majnemere2ae2282016-03-04 05:26:16 +00002323 // gcc passes <1 x long long> as SSE but clang used to unconditionally
2324 // pass them as integer. For platforms where clang is the de facto
2325 // platform compiler, we must continue to use integer.
2326 if (!classifyIntegerMMXAsSSE() &&
2327 (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2328 ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2329 ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2330 ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002331 Current = Integer;
2332 else
2333 Current = SSE;
2334
2335 // If this type crosses an eightbyte boundary, it should be
2336 // split.
2337 if (OffsetBase && OffsetBase != 64)
2338 Hi = Lo;
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002339 } else if (Size == 128 ||
2340 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002341 // Arguments of 256-bits are split into four eightbyte chunks. The
2342 // least significant one belongs to class SSE and all the others to class
2343 // SSEUP. The original Lo and Hi design considers that types can't be
2344 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2345 // This design isn't correct for 256-bits, but since there're no cases
2346 // where the upper parts would need to be inspected, avoid adding
2347 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman96fd2642013-06-12 00:13:45 +00002348 //
2349 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2350 // registers if they are "named", i.e. not part of the "..." of a
2351 // variadic function.
Ahmed Bougacha0b938282015-06-22 21:31:43 +00002352 //
2353 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2354 // split into eight eightbyte chunks, one SSE and seven SSEUP.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002355 Lo = SSE;
2356 Hi = SSEUp;
2357 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00002358 return;
2359 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002360
Chris Lattnerd776fb12010-06-28 21:43:59 +00002361 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002362 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002363
Chris Lattner2b037972010-07-29 02:01:43 +00002364 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00002365 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002366 if (Size <= 64)
2367 Current = Integer;
2368 else if (Size <= 128)
2369 Lo = Hi = Integer;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002370 } else if (ET == getContext().FloatTy) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002371 Current = SSE;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002372 } else if (ET == getContext().DoubleTy) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002373 Lo = Hi = SSE;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002374 } else if (ET == getContext().LongDoubleTy) {
2375 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2376 if (LDF == &llvm::APFloat::IEEEquad)
2377 Current = Memory;
2378 else if (LDF == &llvm::APFloat::x87DoubleExtended)
2379 Current = ComplexX87;
2380 else if (LDF == &llvm::APFloat::IEEEdouble)
2381 Lo = Hi = SSE;
2382 else
2383 llvm_unreachable("unexpected long double representation!");
2384 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002385
2386 // If this complex type crosses an eightbyte boundary then it
2387 // should be split.
2388 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00002389 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002390 if (Hi == NoClass && EB_Real != EB_Imag)
2391 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002392
Chris Lattnerd776fb12010-06-28 21:43:59 +00002393 return;
2394 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002395
Chris Lattner2b037972010-07-29 02:01:43 +00002396 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002397 // Arrays are treated like structures.
2398
Chris Lattner2b037972010-07-29 02:01:43 +00002399 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002400
2401 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002402 // than four eightbytes, ..., it has class MEMORY.
2403 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002404 return;
2405
2406 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2407 // fields, it has class MEMORY.
2408 //
2409 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00002410 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002411 return;
2412
2413 // Otherwise implement simplified merge. We could be smarter about
2414 // this, but it isn't worth it and would be harder to verify.
2415 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00002416 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002417 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00002418
2419 // The only case a 256-bit wide vector could be used is when the array
2420 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2421 // to work for sizes wider than 128, early check and fallback to memory.
2422 if (Size > 128 && EltSize != 256)
2423 return;
2424
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002425 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2426 Class FieldLo, FieldHi;
Eli Friedman96fd2642013-06-12 00:13:45 +00002427 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002428 Lo = merge(Lo, FieldLo);
2429 Hi = merge(Hi, FieldHi);
2430 if (Lo == Memory || Hi == Memory)
2431 break;
2432 }
2433
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002434 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002435 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002436 return;
2437 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002438
Chris Lattnerd776fb12010-06-28 21:43:59 +00002439 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002440 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002441
2442 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002443 // than four eightbytes, ..., it has class MEMORY.
2444 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002445 return;
2446
Anders Carlsson20759ad2009-09-16 15:53:40 +00002447 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2448 // copy constructor or a non-trivial destructor, it is passed by invisible
2449 // reference.
Mark Lacey3825e832013-10-06 01:33:34 +00002450 if (getRecordArgABI(RT, getCXXABI()))
Anders Carlsson20759ad2009-09-16 15:53:40 +00002451 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002452
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002453 const RecordDecl *RD = RT->getDecl();
2454
2455 // Assume variable sized types are passed in memory.
2456 if (RD->hasFlexibleArrayMember())
2457 return;
2458
Chris Lattner2b037972010-07-29 02:01:43 +00002459 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002460
2461 // Reset Lo class, this will be recomputed.
2462 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002463
2464 // If this is a C++ record, classify the bases first.
2465 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00002466 for (const auto &I : CXXRD->bases()) {
2467 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002468 "Unexpected base class!");
2469 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00002470 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002471
2472 // Classify this field.
2473 //
2474 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2475 // single eightbyte, each is classified separately. Each eightbyte gets
2476 // initialized to class NO_CLASS.
2477 Class FieldLo, FieldHi;
Benjamin Kramer2ef30312012-07-04 18:45:14 +00002478 uint64_t Offset =
2479 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Aaron Ballman574705e2014-03-13 15:41:46 +00002480 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002481 Lo = merge(Lo, FieldLo);
2482 Hi = merge(Hi, FieldHi);
David Majnemercefbc7c2015-07-08 05:14:29 +00002483 if (Lo == Memory || Hi == Memory) {
2484 postMerge(Size, Lo, Hi);
2485 return;
2486 }
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002487 }
2488 }
2489
2490 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002491 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00002492 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002493 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002494 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2495 bool BitField = i->isBitField();
2496
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002497 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2498 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002499 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002500 // The only case a 256-bit wide vector could be used is when the struct
2501 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2502 // to work for sizes wider than 128, early check and fallback to memory.
2503 //
2504 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
2505 Lo = Memory;
David Majnemer699dd042015-07-08 05:07:05 +00002506 postMerge(Size, Lo, Hi);
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002507 return;
2508 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002509 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00002510 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002511 Lo = Memory;
David Majnemer699dd042015-07-08 05:07:05 +00002512 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002513 return;
2514 }
2515
2516 // Classify this field.
2517 //
2518 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2519 // exceeds a single eightbyte, each is classified
2520 // separately. Each eightbyte gets initialized to class
2521 // NO_CLASS.
2522 Class FieldLo, FieldHi;
2523
2524 // Bit-fields require special handling, they do not force the
2525 // structure to be passed in memory even if unaligned, and
2526 // therefore they can straddle an eightbyte.
2527 if (BitField) {
2528 // Ignore padding bit-fields.
2529 if (i->isUnnamedBitfield())
2530 continue;
2531
2532 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00002533 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002534
2535 uint64_t EB_Lo = Offset / 64;
2536 uint64_t EB_Hi = (Offset + Size - 1) / 64;
Sylvestre Ledru0c4813e2013-10-06 09:54:18 +00002537
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002538 if (EB_Lo) {
2539 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2540 FieldLo = NoClass;
2541 FieldHi = Integer;
2542 } else {
2543 FieldLo = Integer;
2544 FieldHi = EB_Hi ? Integer : NoClass;
2545 }
2546 } else
Eli Friedman96fd2642013-06-12 00:13:45 +00002547 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002548 Lo = merge(Lo, FieldLo);
2549 Hi = merge(Hi, FieldHi);
2550 if (Lo == Memory || Hi == Memory)
2551 break;
2552 }
2553
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002554 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002555 }
2556}
2557
Chris Lattner22a931e2010-06-29 06:01:59 +00002558ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002559 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2560 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00002561 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002562 // Treat an enum type as its underlying type.
2563 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2564 Ty = EnumTy->getDecl()->getIntegerType();
2565
2566 return (Ty->isPromotableIntegerType() ?
2567 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2568 }
2569
John McCall7f416cc2015-09-08 08:05:57 +00002570 return getNaturalAlignIndirect(Ty);
Daniel Dunbar53fac692010-04-21 19:49:55 +00002571}
2572
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002573bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2574 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2575 uint64_t Size = getContext().getTypeSize(VecTy);
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002576 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002577 if (Size <= 64 || Size > LargestVector)
2578 return true;
2579 }
2580
2581 return false;
2582}
2583
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002584ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2585 unsigned freeIntRegs) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002586 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2587 // place naturally.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002588 //
2589 // This assumption is optimistic, as there could be free registers available
2590 // when we need to pass this argument in memory, and LLVM could try to pass
2591 // the argument in the free register. This does not seem to happen currently,
2592 // but this code would be much safer if we could mark the argument with
2593 // 'onstack'. See PR12193.
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002594 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002595 // Treat an enum type as its underlying type.
2596 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2597 Ty = EnumTy->getDecl()->getIntegerType();
2598
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002599 return (Ty->isPromotableIntegerType() ?
2600 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002601 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002602
Mark Lacey3825e832013-10-06 01:33:34 +00002603 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00002604 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson20759ad2009-09-16 15:53:40 +00002605
Chris Lattner44c2b902011-05-22 23:21:23 +00002606 // Compute the byval alignment. We specify the alignment of the byval in all
2607 // cases so that the mid-level optimizer knows the alignment of the byval.
2608 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002609
2610 // Attempt to avoid passing indirect results using byval when possible. This
2611 // is important for good codegen.
2612 //
2613 // We do this by coercing the value into a scalar type which the backend can
2614 // handle naturally (i.e., without using byval).
2615 //
2616 // For simplicity, we currently only do this when we have exhausted all of the
2617 // free integer registers. Doing this when there are free integer registers
2618 // would require more care, as we would have to ensure that the coerced value
2619 // did not claim the unused register. That would require either reording the
2620 // arguments to the function (so that any subsequent inreg values came first),
2621 // or only doing this optimization when there were no following arguments that
2622 // might be inreg.
2623 //
2624 // We currently expect it to be rare (particularly in well written code) for
2625 // arguments to be passed on the stack when there are still free integer
2626 // registers available (this would typically imply large structs being passed
2627 // by value), so this seems like a fair tradeoff for now.
2628 //
2629 // We can revisit this if the backend grows support for 'onstack' parameter
2630 // attributes. See PR12193.
2631 if (freeIntRegs == 0) {
2632 uint64_t Size = getContext().getTypeSize(Ty);
2633
2634 // If this type fits in an eightbyte, coerce it into the matching integral
2635 // type, which will end up on the stack (with alignment 8).
2636 if (Align == 8 && Size <= 64)
2637 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2638 Size));
2639 }
2640
John McCall7f416cc2015-09-08 08:05:57 +00002641 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002642}
2643
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002644/// The ABI specifies that a value should be passed in a full vector XMM/YMM
2645/// register. Pick an LLVM IR type that will be passed as a vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002646llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002647 // Wrapper structs/arrays that only contain vectors are passed just like
2648 // vectors; strip them off if present.
2649 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
2650 Ty = QualType(InnerTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002651
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002652 llvm::Type *IRType = CGT.ConvertType(Ty);
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002653 if (isa<llvm::VectorType>(IRType) ||
2654 IRType->getTypeID() == llvm::Type::FP128TyID)
Andrea Di Biagioe7347c62015-06-02 19:34:40 +00002655 return IRType;
2656
2657 // We couldn't find the preferred IR vector type for 'Ty'.
2658 uint64_t Size = getContext().getTypeSize(Ty);
2659 assert((Size == 128 || Size == 256) && "Invalid type found!");
2660
2661 // Return a LLVM IR vector type based on the size of 'Ty'.
2662 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
2663 Size / 64);
Chris Lattner4200fe42010-07-29 04:56:46 +00002664}
2665
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002666/// BitsContainNoUserData - Return true if the specified [start,end) bit range
2667/// is known to either be off the end of the specified type or being in
2668/// alignment padding. The user type specified is known to be at most 128 bits
2669/// in size, and have passed through X86_64ABIInfo::classify with a successful
2670/// classification that put one of the two halves in the INTEGER class.
2671///
2672/// It is conservatively correct to return false.
2673static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2674 unsigned EndBit, ASTContext &Context) {
2675 // If the bytes being queried are off the end of the type, there is no user
2676 // data hiding here. This handles analysis of builtins, vectors and other
2677 // types that don't contain interesting padding.
2678 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2679 if (TySize <= StartBit)
2680 return true;
2681
Chris Lattner98076a22010-07-29 07:43:55 +00002682 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2683 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2684 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2685
2686 // Check each element to see if the element overlaps with the queried range.
2687 for (unsigned i = 0; i != NumElts; ++i) {
2688 // If the element is after the span we care about, then we're done..
2689 unsigned EltOffset = i*EltSize;
2690 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002691
Chris Lattner98076a22010-07-29 07:43:55 +00002692 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2693 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
2694 EndBit-EltOffset, Context))
2695 return false;
2696 }
2697 // If it overlaps no elements, then it is safe to process as padding.
2698 return true;
2699 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002700
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002701 if (const RecordType *RT = Ty->getAs<RecordType>()) {
2702 const RecordDecl *RD = RT->getDecl();
2703 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002704
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002705 // If this is a C++ record, check the bases first.
2706 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00002707 for (const auto &I : CXXRD->bases()) {
2708 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002709 "Unexpected base class!");
2710 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00002711 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002712
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002713 // If the base is after the span we care about, ignore it.
Benjamin Kramer2ef30312012-07-04 18:45:14 +00002714 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002715 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002716
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002717 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
Aaron Ballman574705e2014-03-13 15:41:46 +00002718 if (!BitsContainNoUserData(I.getType(), BaseStart,
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002719 EndBit-BaseOffset, Context))
2720 return false;
2721 }
2722 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002723
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002724 // Verify that no field has data that overlaps the region of interest. Yes
2725 // this could be sped up a lot by being smarter about queried fields,
2726 // however we're only looking at structs up to 16 bytes, so we don't care
2727 // much.
2728 unsigned idx = 0;
2729 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2730 i != e; ++i, ++idx) {
2731 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002732
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002733 // If we found a field after the region we care about, then we're done.
2734 if (FieldOffset >= EndBit) break;
2735
2736 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
2737 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
2738 Context))
2739 return false;
2740 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002741
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002742 // If nothing in this record overlapped the area of interest, then we're
2743 // clean.
2744 return true;
2745 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002746
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002747 return false;
2748}
2749
Chris Lattnere556a712010-07-29 18:39:32 +00002750/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
2751/// float member at the specified offset. For example, {int,{float}} has a
2752/// float at offset 4. It is conservatively correct for this routine to return
2753/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00002754static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmowdd31ca12012-10-08 16:25:52 +00002755 const llvm::DataLayout &TD) {
Chris Lattnere556a712010-07-29 18:39:32 +00002756 // Base case if we find a float.
2757 if (IROffset == 0 && IRType->isFloatTy())
2758 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002759
Chris Lattnere556a712010-07-29 18:39:32 +00002760 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00002761 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00002762 const llvm::StructLayout *SL = TD.getStructLayout(STy);
2763 unsigned Elt = SL->getElementContainingOffset(IROffset);
2764 IROffset -= SL->getElementOffset(Elt);
2765 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
2766 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002767
Chris Lattnere556a712010-07-29 18:39:32 +00002768 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00002769 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2770 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00002771 unsigned EltSize = TD.getTypeAllocSize(EltTy);
2772 IROffset -= IROffset/EltSize*EltSize;
2773 return ContainsFloatAtOffset(EltTy, IROffset, TD);
2774 }
2775
2776 return false;
2777}
2778
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002779
2780/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
2781/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002782llvm::Type *X86_64ABIInfo::
2783GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002784 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00002785 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002786 // pass as float if the last 4 bytes is just padding. This happens for
2787 // structs that contain 3 floats.
2788 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
2789 SourceOffset*8+64, getContext()))
2790 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002791
Chris Lattnere556a712010-07-29 18:39:32 +00002792 // We want to pass as <2 x float> if the LLVM IR type contains a float at
2793 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
2794 // case.
Micah Villmowdd31ca12012-10-08 16:25:52 +00002795 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
2796 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner9f8b4512010-08-25 23:39:14 +00002797 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002798
Chris Lattner7f4b81a2010-07-29 18:13:09 +00002799 return llvm::Type::getDoubleTy(getVMContext());
2800}
2801
2802
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002803/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
2804/// an 8-byte GPR. This means that we either have a scalar or we are talking
2805/// about the high or low part of an up-to-16-byte struct. This routine picks
2806/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002807/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
2808/// etc).
2809///
2810/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
2811/// the source type. IROffset is an offset in bytes into the LLVM IR type that
2812/// the 8-byte value references. PrefType may be null.
2813///
Alp Toker9907f082014-07-09 14:06:35 +00002814/// SourceTy is the source-level type for the entire argument. SourceOffset is
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002815/// an offset into this that we're processing (which is always either 0 or 8).
2816///
Chris Lattnera5f58b02011-07-09 17:41:47 +00002817llvm::Type *X86_64ABIInfo::
2818GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002819 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002820 // If we're dealing with an un-offset LLVM IR type, then it means that we're
2821 // returning an 8-byte unit starting with it. See if we can safely use it.
2822 if (IROffset == 0) {
2823 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffc7dd7222012-10-11 15:52:22 +00002824 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
2825 IRType->isIntegerTy(64))
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002826 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002827
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002828 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
2829 // goodness in the source type is just tail padding. This is allowed to
2830 // kick in for struct {double,int} on the int, but not on
2831 // struct{double,int,int} because we wouldn't return the second int. We
2832 // have to do this analysis on the source type because we can't depend on
2833 // unions being lowered a specific way etc.
2834 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffc7dd7222012-10-11 15:52:22 +00002835 IRType->isIntegerTy(32) ||
2836 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2837 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2838 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002839
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002840 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2841 SourceOffset*8+64, getContext()))
2842 return IRType;
2843 }
2844 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002845
Chris Lattner2192fe52011-07-18 04:24:23 +00002846 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002847 // If this is a struct, recurse into the field at the specified offset.
Micah Villmowdd31ca12012-10-08 16:25:52 +00002848 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002849 if (IROffset < SL->getSizeInBytes()) {
2850 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2851 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002852
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002853 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2854 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002855 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002856 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002857
Chris Lattner2192fe52011-07-18 04:24:23 +00002858 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00002859 llvm::Type *EltTy = ATy->getElementType();
Micah Villmowdd31ca12012-10-08 16:25:52 +00002860 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner98076a22010-07-29 07:43:55 +00002861 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00002862 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2863 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00002864 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002865
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002866 // Okay, we don't have any better idea of what to pass, so we pass this in an
2867 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00002868 unsigned TySizeInBytes =
2869 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002870
Chris Lattner3f763422010-07-29 17:34:39 +00002871 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002872
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002873 // It is always safe to classify this as an integer type up to i64 that
2874 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00002875 return llvm::IntegerType::get(getVMContext(),
2876 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00002877}
2878
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002879
2880/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2881/// be used as elements of a two register pair to pass or return, return a
2882/// first class aggregate to represent them. For example, if the low part of
2883/// a by-value argument should be passed as i32* and the high part as float,
2884/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002885static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00002886GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmowdd31ca12012-10-08 16:25:52 +00002887 const llvm::DataLayout &TD) {
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002888 // In order to correctly satisfy the ABI, we need to the high part to start
2889 // at offset 8. If the high and low parts we inferred are both 4-byte types
2890 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2891 // the second element at offset 8. Check for this:
2892 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2893 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Rui Ueyama83aa9792016-01-14 21:00:27 +00002894 unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002895 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002896
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002897 // To handle this, we have to increase the size of the low part so that the
2898 // second element will start at an 8 byte offset. We can't increase the size
2899 // of the second element because it might make us access off the end of the
2900 // struct.
2901 if (HiStart != 8) {
Derek Schuff5ec51282015-06-24 22:36:38 +00002902 // There are usually two sorts of types the ABI generation code can produce
2903 // for the low part of a pair that aren't 8 bytes in size: float or
2904 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
2905 // NaCl).
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002906 // Promote these to a larger type.
2907 if (Lo->isFloatTy())
2908 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2909 else {
Derek Schuff3c6a48d2015-06-24 22:36:36 +00002910 assert((Lo->isIntegerTy() || Lo->isPointerTy())
2911 && "Invalid/unknown lo type");
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002912 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2913 }
2914 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002915
Reid Kleckneree7cf842014-12-01 22:02:27 +00002916 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002917
2918
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002919 // Verify that the second element is at an 8-byte offset.
2920 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2921 "Invalid x86-64 argument pair!");
2922 return Result;
2923}
2924
Chris Lattner31faff52010-07-28 23:06:14 +00002925ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00002926classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00002927 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2928 // classification algorithm.
2929 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00002930 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner31faff52010-07-28 23:06:14 +00002931
2932 // Check some invariants.
2933 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00002934 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2935
Craig Topper8a13c412014-05-21 05:09:00 +00002936 llvm::Type *ResType = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00002937 switch (Lo) {
2938 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002939 if (Hi == NoClass)
2940 return ABIArgInfo::getIgnore();
2941 // If the low part is just padding, it takes no register, leave ResType
2942 // null.
2943 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2944 "Unknown missing lo part");
2945 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002946
2947 case SSEUp:
2948 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00002949 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00002950
2951 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2952 // hidden argument.
2953 case Memory:
2954 return getIndirectReturnResult(RetTy);
2955
2956 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2957 // available register of the sequence %rax, %rdx is used.
2958 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002959 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002960
Chris Lattner1f3a0632010-07-29 21:42:50 +00002961 // If we have a sign or zero extended integer, make sure to return Extend
2962 // so that the parameter gets the right LLVM IR attributes.
2963 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2964 // Treat an enum type as its underlying type.
2965 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2966 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002967
Chris Lattner1f3a0632010-07-29 21:42:50 +00002968 if (RetTy->isIntegralOrEnumerationType() &&
2969 RetTy->isPromotableIntegerType())
2970 return ABIArgInfo::getExtend();
2971 }
Chris Lattner31faff52010-07-28 23:06:14 +00002972 break;
2973
2974 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2975 // available SSE register of the sequence %xmm0, %xmm1 is used.
2976 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002977 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00002978 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002979
2980 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2981 // returned on the X87 stack in %st0 as 80-bit x87 number.
2982 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00002983 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00002984 break;
Chris Lattner31faff52010-07-28 23:06:14 +00002985
2986 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2987 // part of the value is returned in %st0 and the imaginary part in
2988 // %st1.
2989 case ComplexX87:
2990 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00002991 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00002992 llvm::Type::getX86_FP80Ty(getVMContext()),
Reid Kleckneree7cf842014-12-01 22:02:27 +00002993 nullptr);
Chris Lattner31faff52010-07-28 23:06:14 +00002994 break;
2995 }
2996
Craig Topper8a13c412014-05-21 05:09:00 +00002997 llvm::Type *HighPart = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00002998 switch (Hi) {
2999 // Memory was handled previously and X87 should
3000 // never occur as a hi class.
3001 case Memory:
3002 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00003003 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00003004
3005 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003006 case NoClass:
3007 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003008
Chris Lattner52b3c132010-09-01 00:20:33 +00003009 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003010 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003011 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3012 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00003013 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00003014 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003015 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003016 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3017 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00003018 break;
3019
3020 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003021 // is passed in the next available eightbyte chunk if the last used
3022 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00003023 //
Chris Lattner57540c52011-04-15 05:22:18 +00003024 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00003025 case SSEUp:
3026 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003027 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00003028 break;
3029
3030 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3031 // returned together with the previous X87 value in %st0.
3032 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00003033 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00003034 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00003035 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00003036 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00003037 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003038 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003039 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3040 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00003041 }
Chris Lattner31faff52010-07-28 23:06:14 +00003042 break;
3043 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003044
Chris Lattner52b3c132010-09-01 00:20:33 +00003045 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003046 // known to pass in the high eightbyte of the result. We do this by forming a
3047 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003048 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00003049 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner31faff52010-07-28 23:06:14 +00003050
Chris Lattner1f3a0632010-07-29 21:42:50 +00003051 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00003052}
3053
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003054ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman96fd2642013-06-12 00:13:45 +00003055 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3056 bool isNamedArg)
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003057 const
3058{
Reid Klecknerb1be6832014-11-15 01:41:41 +00003059 Ty = useFirstFieldIfTransparentUnion(Ty);
3060
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003061 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00003062 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003063
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003064 // Check some invariants.
3065 // FIXME: Enforce these by construction.
3066 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003067 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3068
3069 neededInt = 0;
3070 neededSSE = 0;
Craig Topper8a13c412014-05-21 05:09:00 +00003071 llvm::Type *ResType = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003072 switch (Lo) {
3073 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003074 if (Hi == NoClass)
3075 return ABIArgInfo::getIgnore();
3076 // If the low part is just padding, it takes no register, leave ResType
3077 // null.
3078 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3079 "Unknown missing lo part");
3080 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003081
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003082 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3083 // on the stack.
3084 case Memory:
3085
3086 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3087 // COMPLEX_X87, it is passed in memory.
3088 case X87:
3089 case ComplexX87:
Mark Lacey3825e832013-10-06 01:33:34 +00003090 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
Eli Friedman4774b7e2011-06-29 07:04:55 +00003091 ++neededInt;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003092 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003093
3094 case SSEUp:
3095 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00003096 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003097
3098 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3099 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3100 // and %r9 is used.
3101 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00003102 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003103
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003104 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003105 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00003106
3107 // If we have a sign or zero extended integer, make sure to return Extend
3108 // so that the parameter gets the right LLVM IR attributes.
3109 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3110 // Treat an enum type as its underlying type.
3111 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3112 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003113
Chris Lattner1f3a0632010-07-29 21:42:50 +00003114 if (Ty->isIntegralOrEnumerationType() &&
3115 Ty->isPromotableIntegerType())
3116 return ABIArgInfo::getExtend();
3117 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003118
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003119 break;
3120
3121 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3122 // available SSE register is used, the registers are taken in the
3123 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00003124 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003125 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00003126 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00003127 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003128 break;
3129 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00003130 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003131
Craig Topper8a13c412014-05-21 05:09:00 +00003132 llvm::Type *HighPart = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003133 switch (Hi) {
3134 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00003135 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003136 // which is passed in memory.
3137 case Memory:
3138 case X87:
3139 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00003140 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003141
3142 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003143
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003144 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003145 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003146 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003147 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003148
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003149 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3150 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003151 break;
3152
3153 // X87Up generally doesn't occur here (long double is passed in
3154 // memory), except in situations involving unions.
3155 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003156 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003157 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003158
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003159 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3160 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003161
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003162 ++neededSSE;
3163 break;
3164
3165 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3166 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003167 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003168 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00003169 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003170 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003171 break;
3172 }
3173
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003174 // If a high part was specified, merge it together with the low part. It is
3175 // known to pass in the high eightbyte of the result. We do this by forming a
3176 // first class struct aggregate with the high and low part: {low, high}
3177 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00003178 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003179
Chris Lattner1f3a0632010-07-29 21:42:50 +00003180 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003181}
3182
Chris Lattner22326a12010-07-29 02:31:05 +00003183void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003184
Reid Kleckner40ca9132014-05-13 22:05:45 +00003185 if (!getCXXABI().classifyReturnType(FI))
3186 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003187
3188 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00003189 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003190
3191 // If the return value is indirect, then the hidden argument is consuming one
3192 // integer register.
3193 if (FI.getReturnInfo().isIndirect())
3194 --freeIntRegs;
3195
Peter Collingbournef7706832014-12-12 23:41:25 +00003196 // The chain argument effectively gives us another free register.
3197 if (FI.isChainCall())
3198 ++freeIntRegs;
3199
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003200 unsigned NumRequiredArgs = FI.getNumRequiredArgs();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003201 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3202 // get assigned (in left-to-right order) for passing as follows...
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003203 unsigned ArgNo = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003204 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003205 it != ie; ++it, ++ArgNo) {
3206 bool IsNamedArg = ArgNo < NumRequiredArgs;
Eli Friedman96fd2642013-06-12 00:13:45 +00003207
Bill Wendling9987c0e2010-10-18 23:51:38 +00003208 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003209 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003210 neededSSE, IsNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003211
3212 // AMD64-ABI 3.2.3p3: If there are no registers available for any
3213 // eightbyte of an argument, the whole argument is passed on the
3214 // stack. If registers have already been assigned for some
3215 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00003216 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003217 freeIntRegs -= neededInt;
3218 freeSSERegs -= neededSSE;
3219 } else {
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003220 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003221 }
3222 }
3223}
3224
John McCall7f416cc2015-09-08 08:05:57 +00003225static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3226 Address VAListAddr, QualType Ty) {
3227 Address overflow_arg_area_p = CGF.Builder.CreateStructGEP(
3228 VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003229 llvm::Value *overflow_arg_area =
3230 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3231
3232 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3233 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00003234 // It isn't stated explicitly in the standard, but in practice we use
3235 // alignment greater than 16 where necessary.
Petar Jovanovic402257b2015-12-04 00:26:47 +00003236 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3237 if (Align > CharUnits::fromQuantity(8)) {
3238 overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3239 Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003240 }
3241
3242 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00003243 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003244 llvm::Value *Res =
3245 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003246 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003247
3248 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3249 // l->overflow_arg_area + sizeof(type).
3250 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3251 // an 8 byte boundary.
3252
3253 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00003254 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00003255 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003256 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3257 "overflow_arg_area.next");
3258 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3259
3260 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
Petar Jovanovic402257b2015-12-04 00:26:47 +00003261 return Address(Res, Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003262}
3263
John McCall7f416cc2015-09-08 08:05:57 +00003264Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3265 QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003266 // Assume that va_list type is correct; should be pointer to LLVM type:
3267 // struct {
3268 // i32 gp_offset;
3269 // i32 fp_offset;
3270 // i8* overflow_arg_area;
3271 // i8* reg_save_area;
3272 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00003273 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003274
John McCall7f416cc2015-09-08 08:05:57 +00003275 Ty = getContext().getCanonicalType(Ty);
Eric Christopher7565e0d2015-05-29 23:09:49 +00003276 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
Eli Friedman96fd2642013-06-12 00:13:45 +00003277 /*isNamedArg*/false);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003278
3279 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3280 // in the registers. If not go to step 7.
3281 if (!neededInt && !neededSSE)
John McCall7f416cc2015-09-08 08:05:57 +00003282 return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003283
3284 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3285 // general purpose registers needed to pass type and num_fp to hold
3286 // the number of floating point registers needed.
3287
3288 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3289 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3290 // l->fp_offset > 304 - num_fp * 16 go to step 7.
3291 //
3292 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3293 // register save space).
3294
Craig Topper8a13c412014-05-21 05:09:00 +00003295 llvm::Value *InRegs = nullptr;
John McCall7f416cc2015-09-08 08:05:57 +00003296 Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3297 llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003298 if (neededInt) {
David Blaikie1ed728c2015-04-05 22:45:47 +00003299 gp_offset_p =
John McCall7f416cc2015-09-08 08:05:57 +00003300 CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(),
3301 "gp_offset_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003302 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00003303 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3304 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003305 }
3306
3307 if (neededSSE) {
David Blaikie1ed728c2015-04-05 22:45:47 +00003308 fp_offset_p =
John McCall7f416cc2015-09-08 08:05:57 +00003309 CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4),
3310 "fp_offset_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003311 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3312 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00003313 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3314 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003315 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3316 }
3317
3318 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3319 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3320 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3321 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3322
3323 // Emit code to load the value if it was passed in registers.
3324
3325 CGF.EmitBlock(InRegBlock);
3326
3327 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3328 // an offset of l->gp_offset and/or l->fp_offset. This may require
3329 // copying to a temporary location in case the parameter is passed
3330 // in different register classes or requires an alignment greater
3331 // than 8 for general purpose registers and 16 for XMM registers.
3332 //
3333 // FIXME: This really results in shameful code when we end up needing to
3334 // collect arguments from different places; often what should result in a
3335 // simple assembling of a structure from scattered addresses has many more
3336 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00003337 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00003338 llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
3339 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)),
3340 "reg_save_area");
3341
3342 Address RegAddr = Address::invalid();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003343 if (neededInt && neededSSE) {
3344 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003345 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003346 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
John McCall7f416cc2015-09-08 08:05:57 +00003347 Address Tmp = CGF.CreateMemTemp(Ty);
3348 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003349 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003350 llvm::Type *TyLo = ST->getElementType(0);
3351 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00003352 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003353 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003354 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
3355 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
John McCall7f416cc2015-09-08 08:05:57 +00003356 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
3357 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
Rafael Espindola0a500af2014-06-24 20:01:50 +00003358 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
3359 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003360
John McCall7f416cc2015-09-08 08:05:57 +00003361 // Copy the first element.
3362 llvm::Value *V =
3363 CGF.Builder.CreateDefaultAlignedLoad(
3364 CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
3365 CGF.Builder.CreateStore(V,
3366 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3367
3368 // Copy the second element.
3369 V = CGF.Builder.CreateDefaultAlignedLoad(
3370 CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
3371 CharUnits Offset = CharUnits::fromQuantity(
3372 getDataLayout().getStructLayout(ST)->getElementOffset(1));
3373 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset));
3374
3375 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003376 } else if (neededInt) {
John McCall7f416cc2015-09-08 08:05:57 +00003377 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
3378 CharUnits::fromQuantity(8));
3379 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003380
3381 // Copy to a temporary if necessary to ensure the appropriate alignment.
3382 std::pair<CharUnits, CharUnits> SizeAlign =
John McCall7f416cc2015-09-08 08:05:57 +00003383 getContext().getTypeInfoInChars(Ty);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003384 uint64_t TySize = SizeAlign.first.getQuantity();
John McCall7f416cc2015-09-08 08:05:57 +00003385 CharUnits TyAlign = SizeAlign.second;
3386
3387 // Copy into a temporary if the type is more aligned than the
3388 // register save area.
3389 if (TyAlign.getQuantity() > 8) {
3390 Address Tmp = CGF.CreateMemTemp(Ty);
3391 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003392 RegAddr = Tmp;
3393 }
John McCall7f416cc2015-09-08 08:05:57 +00003394
Chris Lattner0cf24192010-06-28 20:05:43 +00003395 } else if (neededSSE == 1) {
John McCall7f416cc2015-09-08 08:05:57 +00003396 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3397 CharUnits::fromQuantity(16));
3398 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003399 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00003400 assert(neededSSE == 2 && "Invalid number of needed registers!");
3401 // SSE registers are spaced 16 bytes apart in the register save
3402 // area, we need to collect the two eightbytes together.
John McCall7f416cc2015-09-08 08:05:57 +00003403 // The ABI isn't explicit about this, but it seems reasonable
3404 // to assume that the slots are 16-byte aligned, since the stack is
3405 // naturally 16-byte aligned and the prologue is expected to store
3406 // all the SSE registers to the RSA.
3407 Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3408 CharUnits::fromQuantity(16));
3409 Address RegAddrHi =
3410 CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
3411 CharUnits::fromQuantity(16));
Chris Lattnerece04092012-02-07 00:39:47 +00003412 llvm::Type *DoubleTy = CGF.DoubleTy;
Reid Kleckneree7cf842014-12-01 22:02:27 +00003413 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr);
John McCall7f416cc2015-09-08 08:05:57 +00003414 llvm::Value *V;
3415 Address Tmp = CGF.CreateMemTemp(Ty);
3416 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3417 V = CGF.Builder.CreateLoad(
3418 CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy));
3419 CGF.Builder.CreateStore(V,
3420 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3421 V = CGF.Builder.CreateLoad(
3422 CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy));
3423 CGF.Builder.CreateStore(V,
3424 CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8)));
3425
3426 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003427 }
3428
3429 // AMD64-ABI 3.5.7p5: Step 5. Set:
3430 // l->gp_offset = l->gp_offset + num_gp * 8
3431 // l->fp_offset = l->fp_offset + num_fp * 16.
3432 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003433 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003434 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3435 gp_offset_p);
3436 }
3437 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003438 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003439 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3440 fp_offset_p);
3441 }
3442 CGF.EmitBranch(ContBlock);
3443
3444 // Emit code to load the value if it was passed in memory.
3445
3446 CGF.EmitBlock(InMemBlock);
John McCall7f416cc2015-09-08 08:05:57 +00003447 Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003448
3449 // Return the appropriate result.
3450
3451 CGF.EmitBlock(ContBlock);
John McCall7f416cc2015-09-08 08:05:57 +00003452 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
3453 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003454 return ResAddr;
3455}
3456
Charles Davisc7d5c942015-09-17 20:55:33 +00003457Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
3458 QualType Ty) const {
3459 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3460 CGF.getContext().getTypeInfoInChars(Ty),
3461 CharUnits::fromQuantity(8),
3462 /*allowHigherAlign*/ false);
3463}
3464
Reid Kleckner80944df2014-10-31 22:00:51 +00003465ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
3466 bool IsReturnType) const {
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003467
3468 if (Ty->isVoidType())
3469 return ABIArgInfo::getIgnore();
3470
3471 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3472 Ty = EnumTy->getDecl()->getIntegerType();
3473
Reid Kleckner80944df2014-10-31 22:00:51 +00003474 TypeInfo Info = getContext().getTypeInfo(Ty);
3475 uint64_t Width = Info.Width;
Reid Kleckner11a17192015-10-28 22:29:52 +00003476 CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003477
Reid Kleckner9005f412014-05-02 00:51:20 +00003478 const RecordType *RT = Ty->getAs<RecordType>();
3479 if (RT) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00003480 if (!IsReturnType) {
Mark Lacey3825e832013-10-06 01:33:34 +00003481 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00003482 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00003483 }
3484
3485 if (RT->getDecl()->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00003486 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003487
Reid Kleckner9005f412014-05-02 00:51:20 +00003488 }
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00003489
Reid Kleckner80944df2014-10-31 22:00:51 +00003490 // vectorcall adds the concept of a homogenous vector aggregate, similar to
3491 // other targets.
3492 const Type *Base = nullptr;
3493 uint64_t NumElts = 0;
3494 if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) {
3495 if (FreeSSERegs >= NumElts) {
3496 FreeSSERegs -= NumElts;
3497 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3498 return ABIArgInfo::getDirect();
3499 return ABIArgInfo::getExpand();
3500 }
Reid Kleckner11a17192015-10-28 22:29:52 +00003501 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
Reid Kleckner80944df2014-10-31 22:00:51 +00003502 }
3503
3504
Reid Klecknerec87fec2014-05-02 01:17:12 +00003505 if (Ty->isMemberPointerType()) {
Reid Kleckner7f5f0f32014-05-02 01:14:59 +00003506 // If the member pointer is represented by an LLVM int or ptr, pass it
3507 // directly.
3508 llvm::Type *LLTy = CGT.ConvertType(Ty);
3509 if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3510 return ABIArgInfo::getDirect();
Reid Kleckner9005f412014-05-02 00:51:20 +00003511 }
3512
Michael Kuperstein4f818702015-02-24 09:35:58 +00003513 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00003514 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3515 // not 1, 2, 4, or 8 bytes, must be passed by reference."
Reid Kleckner80944df2014-10-31 22:00:51 +00003516 if (Width > 64 || !llvm::isPowerOf2_64(Width))
John McCall7f416cc2015-09-08 08:05:57 +00003517 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003518
Reid Kleckner9005f412014-05-02 00:51:20 +00003519 // Otherwise, coerce it to a small integer.
Reid Kleckner80944df2014-10-31 22:00:51 +00003520 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003521 }
3522
Julien Lerouge10dcff82014-08-27 00:36:55 +00003523 // Bool type is always extended to the ABI, other builtin types are not
3524 // extended.
3525 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3526 if (BT && BT->getKind() == BuiltinType::Bool)
Julien Lerougee8d34fa2014-08-26 22:11:53 +00003527 return ABIArgInfo::getExtend();
3528
Reid Kleckner11a17192015-10-28 22:29:52 +00003529 // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It
3530 // passes them indirectly through memory.
3531 if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) {
3532 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
3533 if (LDF == &llvm::APFloat::x87DoubleExtended)
3534 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3535 }
3536
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003537 return ABIArgInfo::getDirect();
3538}
3539
3540void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner80944df2014-10-31 22:00:51 +00003541 bool IsVectorCall =
3542 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
Reid Kleckner37abaca2014-05-09 22:46:15 +00003543
Reid Kleckner80944df2014-10-31 22:00:51 +00003544 // We can use up to 4 SSE return registers with vectorcall.
3545 unsigned FreeSSERegs = IsVectorCall ? 4 : 0;
3546 if (!getCXXABI().classifyReturnType(FI))
3547 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true);
3548
3549 // We can use up to 6 SSE register parameters with vectorcall.
3550 FreeSSERegs = IsVectorCall ? 6 : 0;
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003551 for (auto &I : FI.arguments())
Reid Kleckner80944df2014-10-31 22:00:51 +00003552 I.info = classify(I.type, FreeSSERegs, false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003553}
3554
John McCall7f416cc2015-09-08 08:05:57 +00003555Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3556 QualType Ty) const {
3557 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3558 CGF.getContext().getTypeInfoInChars(Ty),
3559 CharUnits::fromQuantity(8),
3560 /*allowHigherAlign*/ false);
Chris Lattner04dc9572010-08-31 16:44:54 +00003561}
Chris Lattner0cf24192010-06-28 20:05:43 +00003562
John McCallea8d8bb2010-03-11 00:10:12 +00003563// PowerPC-32
John McCallea8d8bb2010-03-11 00:10:12 +00003564namespace {
Roman Divacky8a12d842014-11-03 18:32:54 +00003565/// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
3566class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003567bool IsSoftFloatABI;
John McCallea8d8bb2010-03-11 00:10:12 +00003568public:
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003569 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
3570 : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
Roman Divacky8a12d842014-11-03 18:32:54 +00003571
John McCall7f416cc2015-09-08 08:05:57 +00003572 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3573 QualType Ty) const override;
Roman Divacky8a12d842014-11-03 18:32:54 +00003574};
3575
3576class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
3577public:
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003578 PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
3579 : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003580
Craig Topper4f12f102014-03-12 06:41:41 +00003581 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallea8d8bb2010-03-11 00:10:12 +00003582 // This is recovered from gcc output.
3583 return 1; // r1 is the dedicated stack pointer
3584 }
3585
3586 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00003587 llvm::Value *Address) const override;
John McCallea8d8bb2010-03-11 00:10:12 +00003588};
3589
Alexander Kornienkoab9db512015-06-22 23:07:51 +00003590}
John McCallea8d8bb2010-03-11 00:10:12 +00003591
James Y Knight29b5f082016-02-24 02:59:33 +00003592// TODO: this implementation is now likely redundant with
3593// DefaultABIInfo::EmitVAArg.
John McCall7f416cc2015-09-08 08:05:57 +00003594Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
3595 QualType Ty) const {
Roman Divacky039b9702016-02-20 08:31:24 +00003596 const unsigned OverflowLimit = 8;
Roman Divacky8a12d842014-11-03 18:32:54 +00003597 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
3598 // TODO: Implement this. For now ignore.
3599 (void)CTy;
James Y Knight29b5f082016-02-24 02:59:33 +00003600 return Address::invalid(); // FIXME?
Roman Divacky8a12d842014-11-03 18:32:54 +00003601 }
3602
John McCall7f416cc2015-09-08 08:05:57 +00003603 // struct __va_list_tag {
3604 // unsigned char gpr;
3605 // unsigned char fpr;
3606 // unsigned short reserved;
3607 // void *overflow_arg_area;
3608 // void *reg_save_area;
3609 // };
3610
Roman Divacky8a12d842014-11-03 18:32:54 +00003611 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
Eric Christopher7565e0d2015-05-29 23:09:49 +00003612 bool isInt =
3613 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003614 bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
John McCall7f416cc2015-09-08 08:05:57 +00003615
3616 // All aggregates are passed indirectly? That doesn't seem consistent
3617 // with the argument-lowering code.
3618 bool isIndirect = Ty->isAggregateType();
Roman Divacky8a12d842014-11-03 18:32:54 +00003619
3620 CGBuilderTy &Builder = CGF.Builder;
John McCall7f416cc2015-09-08 08:05:57 +00003621
3622 // The calling convention either uses 1-2 GPRs or 1 FPR.
3623 Address NumRegsAddr = Address::invalid();
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003624 if (isInt || IsSoftFloatABI) {
John McCall7f416cc2015-09-08 08:05:57 +00003625 NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr");
3626 } else {
3627 NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr");
Roman Divacky8a12d842014-11-03 18:32:54 +00003628 }
John McCall7f416cc2015-09-08 08:05:57 +00003629
3630 llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
3631
3632 // "Align" the register count when TY is i64.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003633 if (isI64 || (isF64 && IsSoftFloatABI)) {
John McCall7f416cc2015-09-08 08:05:57 +00003634 NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
3635 NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
3636 }
Roman Divacky8a12d842014-11-03 18:32:54 +00003637
Eric Christopher7565e0d2015-05-29 23:09:49 +00003638 llvm::Value *CC =
Roman Divacky039b9702016-02-20 08:31:24 +00003639 Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
Roman Divacky8a12d842014-11-03 18:32:54 +00003640
3641 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
3642 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
3643 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
3644
3645 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
3646
John McCall7f416cc2015-09-08 08:05:57 +00003647 llvm::Type *DirectTy = CGF.ConvertType(Ty);
3648 if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
Roman Divacky8a12d842014-11-03 18:32:54 +00003649
John McCall7f416cc2015-09-08 08:05:57 +00003650 // Case 1: consume registers.
3651 Address RegAddr = Address::invalid();
3652 {
3653 CGF.EmitBlock(UsingRegs);
3654
3655 Address RegSaveAreaPtr =
3656 Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8));
3657 RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
3658 CharUnits::fromQuantity(8));
3659 assert(RegAddr.getElementType() == CGF.Int8Ty);
3660
3661 // Floating-point registers start after the general-purpose registers.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003662 if (!(isInt || IsSoftFloatABI)) {
John McCall7f416cc2015-09-08 08:05:57 +00003663 RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
3664 CharUnits::fromQuantity(32));
3665 }
3666
3667 // Get the address of the saved value by scaling the number of
3668 // registers we've used by the number of
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003669 CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
John McCall7f416cc2015-09-08 08:05:57 +00003670 llvm::Value *RegOffset =
3671 Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
3672 RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
3673 RegAddr.getPointer(), RegOffset),
3674 RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
3675 RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
3676
3677 // Increase the used-register count.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00003678 NumRegs =
3679 Builder.CreateAdd(NumRegs,
3680 Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
John McCall7f416cc2015-09-08 08:05:57 +00003681 Builder.CreateStore(NumRegs, NumRegsAddr);
3682
3683 CGF.EmitBranch(Cont);
Roman Divacky8a12d842014-11-03 18:32:54 +00003684 }
Roman Divacky8a12d842014-11-03 18:32:54 +00003685
John McCall7f416cc2015-09-08 08:05:57 +00003686 // Case 2: consume space in the overflow area.
3687 Address MemAddr = Address::invalid();
3688 {
3689 CGF.EmitBlock(UsingOverflow);
Roman Divacky8a12d842014-11-03 18:32:54 +00003690
Roman Divacky039b9702016-02-20 08:31:24 +00003691 Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
3692
John McCall7f416cc2015-09-08 08:05:57 +00003693 // Everything in the overflow area is rounded up to a size of at least 4.
3694 CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
3695
3696 CharUnits Size;
3697 if (!isIndirect) {
3698 auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003699 Size = TypeInfo.first.alignTo(OverflowAreaAlign);
John McCall7f416cc2015-09-08 08:05:57 +00003700 } else {
3701 Size = CGF.getPointerSize();
3702 }
3703
3704 Address OverflowAreaAddr =
3705 Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4));
Petar Jovanovic402257b2015-12-04 00:26:47 +00003706 Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
John McCall7f416cc2015-09-08 08:05:57 +00003707 OverflowAreaAlign);
Petar Jovanovic402257b2015-12-04 00:26:47 +00003708 // Round up address of argument to alignment
3709 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3710 if (Align > OverflowAreaAlign) {
3711 llvm::Value *Ptr = OverflowArea.getPointer();
3712 OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
3713 Align);
3714 }
3715
John McCall7f416cc2015-09-08 08:05:57 +00003716 MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
3717
3718 // Increase the overflow area.
3719 OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
3720 Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
3721 CGF.EmitBranch(Cont);
3722 }
Roman Divacky8a12d842014-11-03 18:32:54 +00003723
3724 CGF.EmitBlock(Cont);
3725
John McCall7f416cc2015-09-08 08:05:57 +00003726 // Merge the cases with a phi.
3727 Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
3728 "vaarg.addr");
Roman Divacky8a12d842014-11-03 18:32:54 +00003729
John McCall7f416cc2015-09-08 08:05:57 +00003730 // Load the pointer if the argument was passed indirectly.
3731 if (isIndirect) {
3732 Result = Address(Builder.CreateLoad(Result, "aggr"),
3733 getContext().getTypeAlignInChars(Ty));
Roman Divacky8a12d842014-11-03 18:32:54 +00003734 }
3735
3736 return Result;
3737}
3738
John McCallea8d8bb2010-03-11 00:10:12 +00003739bool
3740PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3741 llvm::Value *Address) const {
3742 // This is calculated from the LLVM and GCC tables and verified
3743 // against gcc output. AFAIK all ABIs use the same encoding.
3744
3745 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallea8d8bb2010-03-11 00:10:12 +00003746
Chris Lattnerece04092012-02-07 00:39:47 +00003747 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallea8d8bb2010-03-11 00:10:12 +00003748 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3749 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
3750 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
3751
3752 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00003753 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00003754
3755 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00003756 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00003757
3758 // 64-76 are various 4-byte special-purpose registers:
3759 // 64: mq
3760 // 65: lr
3761 // 66: ctr
3762 // 67: ap
3763 // 68-75 cr0-7
3764 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00003765 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00003766
3767 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00003768 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00003769
3770 // 109: vrsave
3771 // 110: vscr
3772 // 111: spe_acc
3773 // 112: spefscr
3774 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00003775 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00003776
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003777 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00003778}
3779
Roman Divackyd966e722012-05-09 18:22:46 +00003780// PowerPC-64
3781
3782namespace {
Bill Schmidt25cb3492012-10-03 19:18:57 +00003783/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
James Y Knight29b5f082016-02-24 02:59:33 +00003784class PPC64_SVR4_ABIInfo : public ABIInfo {
Ulrich Weigandb7122372014-07-21 00:48:09 +00003785public:
3786 enum ABIKind {
3787 ELFv1 = 0,
3788 ELFv2
3789 };
3790
3791private:
3792 static const unsigned GPRBits = 64;
3793 ABIKind Kind;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003794 bool HasQPX;
3795
3796 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
3797 // will be passed in a QPX register.
3798 bool IsQPXVectorTy(const Type *Ty) const {
3799 if (!HasQPX)
3800 return false;
3801
3802 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3803 unsigned NumElements = VT->getNumElements();
3804 if (NumElements == 1)
3805 return false;
3806
3807 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
3808 if (getContext().getTypeSize(Ty) <= 256)
3809 return true;
3810 } else if (VT->getElementType()->
3811 isSpecificBuiltinType(BuiltinType::Float)) {
3812 if (getContext().getTypeSize(Ty) <= 128)
3813 return true;
3814 }
3815 }
3816
3817 return false;
3818 }
3819
3820 bool IsQPXVectorTy(QualType Ty) const {
3821 return IsQPXVectorTy(Ty.getTypePtr());
3822 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00003823
3824public:
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003825 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX)
James Y Knight29b5f082016-02-24 02:59:33 +00003826 : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00003827
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003828 bool isPromotableTypeForABI(QualType Ty) const;
John McCall7f416cc2015-09-08 08:05:57 +00003829 CharUnits getParamTypeAlignment(QualType Ty) const;
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003830
3831 ABIArgInfo classifyReturnType(QualType RetTy) const;
3832 ABIArgInfo classifyArgumentType(QualType Ty) const;
3833
Reid Klecknere9f6a712014-10-31 17:10:41 +00003834 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
3835 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
3836 uint64_t Members) const override;
3837
Bill Schmidt84d37792012-10-12 19:26:17 +00003838 // TODO: We can add more logic to computeInfo to improve performance.
3839 // Example: For aggregate arguments that fit in a register, we could
3840 // use getDirectInReg (as is done below for structs containing a single
3841 // floating-point value) to avoid pushing them to memory on function
3842 // entry. This would require changing the logic in PPCISelLowering
3843 // when lowering the parameters in the caller and args in the callee.
Craig Topper4f12f102014-03-12 06:41:41 +00003844 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00003845 if (!getCXXABI().classifyReturnType(FI))
3846 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003847 for (auto &I : FI.arguments()) {
Bill Schmidt84d37792012-10-12 19:26:17 +00003848 // We rely on the default argument classification for the most part.
3849 // One exception: An aggregate containing a single floating-point
Bill Schmidt179afae2013-07-23 22:15:57 +00003850 // or vector item must be passed in a register if one is available.
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003851 const Type *T = isSingleElementStruct(I.type, getContext());
Bill Schmidt84d37792012-10-12 19:26:17 +00003852 if (T) {
3853 const BuiltinType *BT = T->getAs<BuiltinType>();
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003854 if (IsQPXVectorTy(T) ||
3855 (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
Ulrich Weigandf4eba982014-07-10 16:39:01 +00003856 (BT && BT->isFloatingPoint())) {
Bill Schmidt84d37792012-10-12 19:26:17 +00003857 QualType QT(T, 0);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003858 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
Bill Schmidt84d37792012-10-12 19:26:17 +00003859 continue;
3860 }
3861 }
Aaron Ballmanec47bc22014-03-17 18:10:01 +00003862 I.info = classifyArgumentType(I.type);
Bill Schmidt84d37792012-10-12 19:26:17 +00003863 }
3864 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00003865
John McCall7f416cc2015-09-08 08:05:57 +00003866 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3867 QualType Ty) const override;
Bill Schmidt25cb3492012-10-03 19:18:57 +00003868};
3869
3870class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003871
Bill Schmidt25cb3492012-10-03 19:18:57 +00003872public:
Ulrich Weigandb7122372014-07-21 00:48:09 +00003873 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003874 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX)
Alexey Bataev00396512015-07-02 03:40:19 +00003875 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00003876
Craig Topper4f12f102014-03-12 06:41:41 +00003877 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Bill Schmidt25cb3492012-10-03 19:18:57 +00003878 // This is recovered from gcc output.
3879 return 1; // r1 is the dedicated stack pointer
3880 }
3881
3882 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00003883 llvm::Value *Address) const override;
Bill Schmidt25cb3492012-10-03 19:18:57 +00003884};
3885
Roman Divackyd966e722012-05-09 18:22:46 +00003886class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3887public:
3888 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
3889
Craig Topper4f12f102014-03-12 06:41:41 +00003890 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyd966e722012-05-09 18:22:46 +00003891 // This is recovered from gcc output.
3892 return 1; // r1 is the dedicated stack pointer
3893 }
3894
3895 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00003896 llvm::Value *Address) const override;
Roman Divackyd966e722012-05-09 18:22:46 +00003897};
3898
Alexander Kornienkoab9db512015-06-22 23:07:51 +00003899}
Roman Divackyd966e722012-05-09 18:22:46 +00003900
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00003901// Return true if the ABI requires Ty to be passed sign- or zero-
3902// extended to 64 bits.
3903bool
3904PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
3905 // Treat an enum type as its underlying type.
3906 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3907 Ty = EnumTy->getDecl()->getIntegerType();
3908
3909 // Promotable integer types are required to be promoted by the ABI.
3910 if (Ty->isPromotableIntegerType())
3911 return true;
3912
3913 // In addition to the usual promotable integer types, we also need to
3914 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
3915 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3916 switch (BT->getKind()) {
3917 case BuiltinType::Int:
3918 case BuiltinType::UInt:
3919 return true;
3920 default:
3921 break;
3922 }
3923
3924 return false;
3925}
3926
John McCall7f416cc2015-09-08 08:05:57 +00003927/// isAlignedParamType - Determine whether a type requires 16-byte or
3928/// higher alignment in the parameter area. Always returns at least 8.
3929CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
Ulrich Weigand581badc2014-07-10 17:20:07 +00003930 // Complex types are passed just like their elements.
3931 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
3932 Ty = CTy->getElementType();
3933
3934 // Only vector types of size 16 bytes need alignment (larger types are
3935 // passed via reference, smaller types are not aligned).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003936 if (IsQPXVectorTy(Ty)) {
3937 if (getContext().getTypeSize(Ty) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00003938 return CharUnits::fromQuantity(32);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003939
John McCall7f416cc2015-09-08 08:05:57 +00003940 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003941 } else if (Ty->isVectorType()) {
John McCall7f416cc2015-09-08 08:05:57 +00003942 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003943 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00003944
3945 // For single-element float/vector structs, we consider the whole type
3946 // to have the same alignment requirements as its single element.
3947 const Type *AlignAsType = nullptr;
3948 const Type *EltType = isSingleElementStruct(Ty, getContext());
3949 if (EltType) {
3950 const BuiltinType *BT = EltType->getAs<BuiltinType>();
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003951 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
Ulrich Weigand581badc2014-07-10 17:20:07 +00003952 getContext().getTypeSize(EltType) == 128) ||
3953 (BT && BT->isFloatingPoint()))
3954 AlignAsType = EltType;
3955 }
3956
Ulrich Weigandb7122372014-07-21 00:48:09 +00003957 // Likewise for ELFv2 homogeneous aggregates.
3958 const Type *Base = nullptr;
3959 uint64_t Members = 0;
3960 if (!AlignAsType && Kind == ELFv2 &&
3961 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
3962 AlignAsType = Base;
3963
Ulrich Weigand581badc2014-07-10 17:20:07 +00003964 // With special case aggregates, only vector base types need alignment.
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003965 if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
3966 if (getContext().getTypeSize(AlignAsType) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00003967 return CharUnits::fromQuantity(32);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003968
John McCall7f416cc2015-09-08 08:05:57 +00003969 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003970 } else if (AlignAsType) {
John McCall7f416cc2015-09-08 08:05:57 +00003971 return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003972 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00003973
3974 // Otherwise, we only need alignment for any aggregate type that
3975 // has an alignment requirement of >= 16 bytes.
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003976 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
3977 if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
John McCall7f416cc2015-09-08 08:05:57 +00003978 return CharUnits::fromQuantity(32);
3979 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00003980 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00003981
John McCall7f416cc2015-09-08 08:05:57 +00003982 return CharUnits::fromQuantity(8);
Ulrich Weigand581badc2014-07-10 17:20:07 +00003983}
3984
Ulrich Weigandb7122372014-07-21 00:48:09 +00003985/// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
3986/// aggregate. Base is set to the base element type, and Members is set
3987/// to the number of base elements.
Reid Klecknere9f6a712014-10-31 17:10:41 +00003988bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
3989 uint64_t &Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00003990 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
3991 uint64_t NElements = AT->getSize().getZExtValue();
3992 if (NElements == 0)
3993 return false;
3994 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
3995 return false;
3996 Members *= NElements;
3997 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3998 const RecordDecl *RD = RT->getDecl();
3999 if (RD->hasFlexibleArrayMember())
4000 return false;
4001
4002 Members = 0;
Ulrich Weiganda094f042014-10-29 13:23:20 +00004003
4004 // If this is a C++ record, check the bases first.
4005 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4006 for (const auto &I : CXXRD->bases()) {
4007 // Ignore empty records.
4008 if (isEmptyRecord(getContext(), I.getType(), true))
4009 continue;
4010
4011 uint64_t FldMembers;
4012 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
4013 return false;
4014
4015 Members += FldMembers;
4016 }
4017 }
4018
Ulrich Weigandb7122372014-07-21 00:48:09 +00004019 for (const auto *FD : RD->fields()) {
4020 // Ignore (non-zero arrays of) empty records.
4021 QualType FT = FD->getType();
4022 while (const ConstantArrayType *AT =
4023 getContext().getAsConstantArrayType(FT)) {
4024 if (AT->getSize().getZExtValue() == 0)
4025 return false;
4026 FT = AT->getElementType();
4027 }
4028 if (isEmptyRecord(getContext(), FT, true))
4029 continue;
4030
4031 // For compatibility with GCC, ignore empty bitfields in C++ mode.
4032 if (getContext().getLangOpts().CPlusPlus &&
4033 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4034 continue;
4035
4036 uint64_t FldMembers;
4037 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
4038 return false;
4039
4040 Members = (RD->isUnion() ?
4041 std::max(Members, FldMembers) : Members + FldMembers);
4042 }
4043
4044 if (!Base)
4045 return false;
4046
4047 // Ensure there is no padding.
4048 if (getContext().getTypeSize(Base) * Members !=
4049 getContext().getTypeSize(Ty))
4050 return false;
4051 } else {
4052 Members = 1;
4053 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
4054 Members = 2;
4055 Ty = CT->getElementType();
4056 }
4057
Reid Klecknere9f6a712014-10-31 17:10:41 +00004058 // Most ABIs only support float, double, and some vector type widths.
4059 if (!isHomogeneousAggregateBaseType(Ty))
Ulrich Weigandb7122372014-07-21 00:48:09 +00004060 return false;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004061
4062 // The base type must be the same for all members. Types that
4063 // agree in both total size and mode (float vs. vector) are
4064 // treated as being equivalent here.
4065 const Type *TyPtr = Ty.getTypePtr();
4066 if (!Base)
4067 Base = TyPtr;
4068
4069 if (Base->isVectorType() != TyPtr->isVectorType() ||
4070 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
4071 return false;
4072 }
Reid Klecknere9f6a712014-10-31 17:10:41 +00004073 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
4074}
Ulrich Weigandb7122372014-07-21 00:48:09 +00004075
Reid Klecknere9f6a712014-10-31 17:10:41 +00004076bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4077 // Homogeneous aggregates for ELFv2 must have base types of float,
4078 // double, long double, or 128-bit vectors.
4079 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4080 if (BT->getKind() == BuiltinType::Float ||
4081 BT->getKind() == BuiltinType::Double ||
4082 BT->getKind() == BuiltinType::LongDouble)
4083 return true;
4084 }
4085 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004086 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
Reid Klecknere9f6a712014-10-31 17:10:41 +00004087 return true;
4088 }
4089 return false;
4090}
4091
4092bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
4093 const Type *Base, uint64_t Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004094 // Vector types require one register, floating point types require one
4095 // or two registers depending on their size.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004096 uint32_t NumRegs =
4097 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004098
4099 // Homogeneous Aggregates may occupy at most 8 registers.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004100 return Members * NumRegs <= 8;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004101}
4102
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004103ABIArgInfo
4104PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00004105 Ty = useFirstFieldIfTransparentUnion(Ty);
4106
Bill Schmidt90b22c92012-11-27 02:46:43 +00004107 if (Ty->isAnyComplexType())
4108 return ABIArgInfo::getDirect();
4109
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004110 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
4111 // or via reference (larger than 16 bytes).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004112 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004113 uint64_t Size = getContext().getTypeSize(Ty);
4114 if (Size > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004115 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004116 else if (Size < 128) {
4117 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4118 return ABIArgInfo::getDirect(CoerceTy);
4119 }
4120 }
4121
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004122 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +00004123 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00004124 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004125
John McCall7f416cc2015-09-08 08:05:57 +00004126 uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
4127 uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
Ulrich Weigandb7122372014-07-21 00:48:09 +00004128
4129 // ELFv2 homogeneous aggregates are passed as array types.
4130 const Type *Base = nullptr;
4131 uint64_t Members = 0;
4132 if (Kind == ELFv2 &&
4133 isHomogeneousAggregate(Ty, Base, Members)) {
4134 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4135 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4136 return ABIArgInfo::getDirect(CoerceTy);
4137 }
4138
Ulrich Weigand601957f2014-07-21 00:56:36 +00004139 // If an aggregate may end up fully in registers, we do not
4140 // use the ByVal method, but pass the aggregate as array.
4141 // This is usually beneficial since we avoid forcing the
4142 // back-end to store the argument to memory.
4143 uint64_t Bits = getContext().getTypeSize(Ty);
4144 if (Bits > 0 && Bits <= 8 * GPRBits) {
4145 llvm::Type *CoerceTy;
4146
4147 // Types up to 8 bytes are passed as integer type (which will be
4148 // properly aligned in the argument save area doubleword).
4149 if (Bits <= GPRBits)
Rui Ueyama83aa9792016-01-14 21:00:27 +00004150 CoerceTy =
4151 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Ulrich Weigand601957f2014-07-21 00:56:36 +00004152 // Larger types are passed as arrays, with the base type selected
4153 // according to the required alignment in the save area.
4154 else {
4155 uint64_t RegBits = ABIAlign * 8;
Rui Ueyama83aa9792016-01-14 21:00:27 +00004156 uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
Ulrich Weigand601957f2014-07-21 00:56:36 +00004157 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
4158 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
4159 }
4160
4161 return ABIArgInfo::getDirect(CoerceTy);
4162 }
4163
Ulrich Weigandb7122372014-07-21 00:48:09 +00004164 // All other aggregates are passed ByVal.
John McCall7f416cc2015-09-08 08:05:57 +00004165 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
4166 /*ByVal=*/true,
Ulrich Weigand581badc2014-07-10 17:20:07 +00004167 /*Realign=*/TyAlign > ABIAlign);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004168 }
4169
4170 return (isPromotableTypeForABI(Ty) ?
4171 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4172}
4173
4174ABIArgInfo
4175PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4176 if (RetTy->isVoidType())
4177 return ABIArgInfo::getIgnore();
4178
Bill Schmidta3d121c2012-12-17 04:20:17 +00004179 if (RetTy->isAnyComplexType())
4180 return ABIArgInfo::getDirect();
4181
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004182 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
4183 // or via reference (larger than 16 bytes).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004184 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004185 uint64_t Size = getContext().getTypeSize(RetTy);
4186 if (Size > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004187 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004188 else if (Size < 128) {
4189 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4190 return ABIArgInfo::getDirect(CoerceTy);
4191 }
4192 }
4193
Ulrich Weigandb7122372014-07-21 00:48:09 +00004194 if (isAggregateTypeForABI(RetTy)) {
4195 // ELFv2 homogeneous aggregates are returned as array types.
4196 const Type *Base = nullptr;
4197 uint64_t Members = 0;
4198 if (Kind == ELFv2 &&
4199 isHomogeneousAggregate(RetTy, Base, Members)) {
4200 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4201 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4202 return ABIArgInfo::getDirect(CoerceTy);
4203 }
4204
4205 // ELFv2 small aggregates are returned in up to two registers.
4206 uint64_t Bits = getContext().getTypeSize(RetTy);
4207 if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
4208 if (Bits == 0)
4209 return ABIArgInfo::getIgnore();
4210
4211 llvm::Type *CoerceTy;
4212 if (Bits > GPRBits) {
4213 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
Reid Kleckneree7cf842014-12-01 22:02:27 +00004214 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr);
Ulrich Weigandb7122372014-07-21 00:48:09 +00004215 } else
Rui Ueyama83aa9792016-01-14 21:00:27 +00004216 CoerceTy =
4217 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Ulrich Weigandb7122372014-07-21 00:48:09 +00004218 return ABIArgInfo::getDirect(CoerceTy);
4219 }
4220
4221 // All other aggregates are returned indirectly.
John McCall7f416cc2015-09-08 08:05:57 +00004222 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandb7122372014-07-21 00:48:09 +00004223 }
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004224
4225 return (isPromotableTypeForABI(RetTy) ?
4226 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4227}
4228
Bill Schmidt25cb3492012-10-03 19:18:57 +00004229// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
John McCall7f416cc2015-09-08 08:05:57 +00004230Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4231 QualType Ty) const {
4232 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4233 TypeInfo.second = getParamTypeAlignment(Ty);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004234
John McCall7f416cc2015-09-08 08:05:57 +00004235 CharUnits SlotSize = CharUnits::fromQuantity(8);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004236
Bill Schmidt924c4782013-01-14 17:45:36 +00004237 // If we have a complex type and the base type is smaller than 8 bytes,
4238 // the ABI calls for the real and imaginary parts to be right-adjusted
4239 // in separate doublewords. However, Clang expects us to produce a
4240 // pointer to a structure with the two parts packed tightly. So generate
4241 // loads of the real and imaginary parts relative to the va_list pointer,
4242 // and store them to a temporary structure.
John McCall7f416cc2015-09-08 08:05:57 +00004243 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4244 CharUnits EltSize = TypeInfo.first / 2;
4245 if (EltSize < SlotSize) {
4246 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
4247 SlotSize * 2, SlotSize,
4248 SlotSize, /*AllowHigher*/ true);
4249
4250 Address RealAddr = Addr;
4251 Address ImagAddr = RealAddr;
4252 if (CGF.CGM.getDataLayout().isBigEndian()) {
4253 RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
4254 SlotSize - EltSize);
4255 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
4256 2 * SlotSize - EltSize);
4257 } else {
4258 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
4259 }
4260
4261 llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
4262 RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
4263 ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
4264 llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
4265 llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
4266
4267 Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
4268 CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
4269 /*init*/ true);
4270 return Temp;
Ulrich Weigandbebc55b2014-06-20 16:37:40 +00004271 }
Bill Schmidt924c4782013-01-14 17:45:36 +00004272 }
4273
John McCall7f416cc2015-09-08 08:05:57 +00004274 // Otherwise, just use the general rule.
4275 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
4276 TypeInfo, SlotSize, /*AllowHigher*/ true);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004277}
4278
4279static bool
4280PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4281 llvm::Value *Address) {
Roman Divackyd966e722012-05-09 18:22:46 +00004282 // This is calculated from the LLVM and GCC tables and verified
4283 // against gcc output. AFAIK all ABIs use the same encoding.
4284
4285 CodeGen::CGBuilderTy &Builder = CGF.Builder;
4286
4287 llvm::IntegerType *i8 = CGF.Int8Ty;
4288 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4289 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4290 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4291
4292 // 0-31: r0-31, the 8-byte general-purpose registers
4293 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
4294
4295 // 32-63: fp0-31, the 8-byte floating-point registers
4296 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4297
4298 // 64-76 are various 4-byte special-purpose registers:
4299 // 64: mq
4300 // 65: lr
4301 // 66: ctr
4302 // 67: ap
4303 // 68-75 cr0-7
4304 // 76: xer
4305 AssignToArrayRange(Builder, Address, Four8, 64, 76);
4306
4307 // 77-108: v0-31, the 16-byte vector registers
4308 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4309
4310 // 109: vrsave
4311 // 110: vscr
4312 // 111: spe_acc
4313 // 112: spefscr
4314 // 113: sfp
4315 AssignToArrayRange(Builder, Address, Four8, 109, 113);
4316
4317 return false;
4318}
John McCallea8d8bb2010-03-11 00:10:12 +00004319
Bill Schmidt25cb3492012-10-03 19:18:57 +00004320bool
4321PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
4322 CodeGen::CodeGenFunction &CGF,
4323 llvm::Value *Address) const {
4324
4325 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4326}
4327
4328bool
4329PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4330 llvm::Value *Address) const {
4331
4332 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4333}
4334
Chris Lattner0cf24192010-06-28 20:05:43 +00004335//===----------------------------------------------------------------------===//
Tim Northover573cbee2014-05-24 12:52:07 +00004336// AArch64 ABI Implementation
Tim Northovera2ee4332014-03-29 15:09:45 +00004337//===----------------------------------------------------------------------===//
4338
4339namespace {
4340
Tim Northover573cbee2014-05-24 12:52:07 +00004341class AArch64ABIInfo : public ABIInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00004342public:
4343 enum ABIKind {
4344 AAPCS = 0,
4345 DarwinPCS
4346 };
4347
4348private:
4349 ABIKind Kind;
4350
4351public:
Tim Northover573cbee2014-05-24 12:52:07 +00004352 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00004353
4354private:
4355 ABIKind getABIKind() const { return Kind; }
4356 bool isDarwinPCS() const { return Kind == DarwinPCS; }
4357
4358 ABIArgInfo classifyReturnType(QualType RetTy) const;
Tim Northoverb047bfa2014-11-27 21:02:49 +00004359 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004360 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4361 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4362 uint64_t Members) const override;
4363
Tim Northovera2ee4332014-03-29 15:09:45 +00004364 bool isIllegalVectorType(QualType Ty) const;
4365
David Blaikie1cbb9712014-11-14 19:09:44 +00004366 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00004367 if (!getCXXABI().classifyReturnType(FI))
4368 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Tim Northover5ffc0922014-04-17 10:20:38 +00004369
Tim Northoverb047bfa2014-11-27 21:02:49 +00004370 for (auto &it : FI.arguments())
4371 it.info = classifyArgumentType(it.type);
Tim Northovera2ee4332014-03-29 15:09:45 +00004372 }
4373
John McCall7f416cc2015-09-08 08:05:57 +00004374 Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4375 CodeGenFunction &CGF) const;
Tim Northovera2ee4332014-03-29 15:09:45 +00004376
John McCall7f416cc2015-09-08 08:05:57 +00004377 Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
4378 CodeGenFunction &CGF) const;
Tim Northovera2ee4332014-03-29 15:09:45 +00004379
John McCall7f416cc2015-09-08 08:05:57 +00004380 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4381 QualType Ty) const override {
Tim Northovera2ee4332014-03-29 15:09:45 +00004382 return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
4383 : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
4384 }
4385};
4386
Tim Northover573cbee2014-05-24 12:52:07 +00004387class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00004388public:
Tim Northover573cbee2014-05-24 12:52:07 +00004389 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
4390 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00004391
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004392 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
Tim Northovera2ee4332014-03-29 15:09:45 +00004393 return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue";
4394 }
4395
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004396 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4397 return 31;
4398 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004399
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004400 bool doesReturnSlotInterfereWithArgs() const override { return false; }
Tim Northovera2ee4332014-03-29 15:09:45 +00004401};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004402}
Tim Northovera2ee4332014-03-29 15:09:45 +00004403
Tim Northoverb047bfa2014-11-27 21:02:49 +00004404ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00004405 Ty = useFirstFieldIfTransparentUnion(Ty);
4406
Tim Northovera2ee4332014-03-29 15:09:45 +00004407 // Handle illegal vector types here.
4408 if (isIllegalVectorType(Ty)) {
4409 uint64_t Size = getContext().getTypeSize(Ty);
Nirav Dave9a8f97e2016-02-22 16:48:42 +00004410 // Android promotes <2 x i8> to i16, not i32
4411 if(isAndroid() && (Size <= 16)) {
4412 llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
4413 return ABIArgInfo::getDirect(ResType);
4414 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004415 if (Size <= 32) {
4416 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
Tim Northovera2ee4332014-03-29 15:09:45 +00004417 return ABIArgInfo::getDirect(ResType);
4418 }
4419 if (Size == 64) {
4420 llvm::Type *ResType =
4421 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
Tim Northovera2ee4332014-03-29 15:09:45 +00004422 return ABIArgInfo::getDirect(ResType);
4423 }
4424 if (Size == 128) {
4425 llvm::Type *ResType =
4426 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
Tim Northovera2ee4332014-03-29 15:09:45 +00004427 return ABIArgInfo::getDirect(ResType);
4428 }
John McCall7f416cc2015-09-08 08:05:57 +00004429 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Tim Northovera2ee4332014-03-29 15:09:45 +00004430 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004431
4432 if (!isAggregateTypeForABI(Ty)) {
4433 // Treat an enum type as its underlying type.
4434 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4435 Ty = EnumTy->getDecl()->getIntegerType();
4436
Tim Northovera2ee4332014-03-29 15:09:45 +00004437 return (Ty->isPromotableIntegerType() && isDarwinPCS()
4438 ? ABIArgInfo::getExtend()
4439 : ABIArgInfo::getDirect());
4440 }
4441
4442 // Structures with either a non-trivial destructor or a non-trivial
4443 // copy constructor are always indirect.
Reid Kleckner40ca9132014-05-13 22:05:45 +00004444 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
John McCall7f416cc2015-09-08 08:05:57 +00004445 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
4446 CGCXXABI::RAA_DirectInMemory);
Tim Northovera2ee4332014-03-29 15:09:45 +00004447 }
4448
4449 // Empty records are always ignored on Darwin, but actually passed in C++ mode
4450 // elsewhere for GNU compatibility.
4451 if (isEmptyRecord(getContext(), Ty, true)) {
4452 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
4453 return ABIArgInfo::getIgnore();
4454
Tim Northovera2ee4332014-03-29 15:09:45 +00004455 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4456 }
4457
4458 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
Craig Topper8a13c412014-05-21 05:09:00 +00004459 const Type *Base = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004460 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004461 if (isHomogeneousAggregate(Ty, Base, Members)) {
Tim Northoverb047bfa2014-11-27 21:02:49 +00004462 return ABIArgInfo::getDirect(
4463 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
Tim Northovera2ee4332014-03-29 15:09:45 +00004464 }
4465
4466 // Aggregates <= 16 bytes are passed directly in registers or on the stack.
4467 uint64_t Size = getContext().getTypeSize(Ty);
4468 if (Size <= 128) {
Tim Northoverc801b4a2014-04-15 14:55:11 +00004469 unsigned Alignment = getContext().getTypeAlign(Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00004470 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
Tim Northoverb047bfa2014-11-27 21:02:49 +00004471
Tim Northovera2ee4332014-03-29 15:09:45 +00004472 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4473 // For aggregates with 16-byte alignment, we use i128.
Tim Northoverc801b4a2014-04-15 14:55:11 +00004474 if (Alignment < 128 && Size == 128) {
Tim Northovera2ee4332014-03-29 15:09:45 +00004475 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4476 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4477 }
4478 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4479 }
4480
John McCall7f416cc2015-09-08 08:05:57 +00004481 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Tim Northovera2ee4332014-03-29 15:09:45 +00004482}
4483
Tim Northover573cbee2014-05-24 12:52:07 +00004484ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00004485 if (RetTy->isVoidType())
4486 return ABIArgInfo::getIgnore();
4487
4488 // Large vector types should be returned via memory.
4489 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004490 return getNaturalAlignIndirect(RetTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00004491
4492 if (!isAggregateTypeForABI(RetTy)) {
4493 // Treat an enum type as its underlying type.
4494 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4495 RetTy = EnumTy->getDecl()->getIntegerType();
4496
Tim Northover4dab6982014-04-18 13:46:08 +00004497 return (RetTy->isPromotableIntegerType() && isDarwinPCS()
4498 ? ABIArgInfo::getExtend()
4499 : ABIArgInfo::getDirect());
Tim Northovera2ee4332014-03-29 15:09:45 +00004500 }
4501
Tim Northovera2ee4332014-03-29 15:09:45 +00004502 if (isEmptyRecord(getContext(), RetTy, true))
4503 return ABIArgInfo::getIgnore();
4504
Craig Topper8a13c412014-05-21 05:09:00 +00004505 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004506 uint64_t Members = 0;
4507 if (isHomogeneousAggregate(RetTy, Base, Members))
Tim Northovera2ee4332014-03-29 15:09:45 +00004508 // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
4509 return ABIArgInfo::getDirect();
4510
4511 // Aggregates <= 16 bytes are returned directly in registers or on the stack.
4512 uint64_t Size = getContext().getTypeSize(RetTy);
4513 if (Size <= 128) {
Pete Cooper635b5092015-04-17 22:16:24 +00004514 unsigned Alignment = getContext().getTypeAlign(RetTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00004515 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
Pete Cooper635b5092015-04-17 22:16:24 +00004516
4517 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4518 // For aggregates with 16-byte alignment, we use i128.
4519 if (Alignment < 128 && Size == 128) {
4520 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4521 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4522 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004523 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4524 }
4525
John McCall7f416cc2015-09-08 08:05:57 +00004526 return getNaturalAlignIndirect(RetTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00004527}
4528
Tim Northover573cbee2014-05-24 12:52:07 +00004529/// isIllegalVectorType - check whether the vector type is legal for AArch64.
4530bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00004531 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4532 // Check whether VT is legal.
4533 unsigned NumElements = VT->getNumElements();
4534 uint64_t Size = getContext().getTypeSize(VT);
4535 // NumElements should be power of 2 between 1 and 16.
4536 if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16)
4537 return true;
4538 return Size != 64 && (Size != 128 || NumElements == 1);
4539 }
4540 return false;
4541}
4542
Reid Klecknere9f6a712014-10-31 17:10:41 +00004543bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4544 // Homogeneous aggregates for AAPCS64 must have base types of a floating
4545 // point type or a short-vector type. This is the same as the 32-bit ABI,
4546 // but with the difference that any floating-point type is allowed,
4547 // including __fp16.
4548 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4549 if (BT->isFloatingPoint())
4550 return true;
4551 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
4552 unsigned VecSize = getContext().getTypeSize(VT);
4553 if (VecSize == 64 || VecSize == 128)
4554 return true;
4555 }
4556 return false;
4557}
4558
4559bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
4560 uint64_t Members) const {
4561 return Members <= 4;
4562}
4563
John McCall7f416cc2015-09-08 08:05:57 +00004564Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
Tim Northoverb047bfa2014-11-27 21:02:49 +00004565 QualType Ty,
4566 CodeGenFunction &CGF) const {
4567 ABIArgInfo AI = classifyArgumentType(Ty);
Reid Klecknere9f6a712014-10-31 17:10:41 +00004568 bool IsIndirect = AI.isIndirect();
4569
Tim Northoverb047bfa2014-11-27 21:02:49 +00004570 llvm::Type *BaseTy = CGF.ConvertType(Ty);
4571 if (IsIndirect)
4572 BaseTy = llvm::PointerType::getUnqual(BaseTy);
4573 else if (AI.getCoerceToType())
4574 BaseTy = AI.getCoerceToType();
4575
4576 unsigned NumRegs = 1;
4577 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
4578 BaseTy = ArrTy->getElementType();
4579 NumRegs = ArrTy->getNumElements();
4580 }
4581 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
4582
Tim Northovera2ee4332014-03-29 15:09:45 +00004583 // The AArch64 va_list type and handling is specified in the Procedure Call
4584 // Standard, section B.4:
4585 //
4586 // struct {
4587 // void *__stack;
4588 // void *__gr_top;
4589 // void *__vr_top;
4590 // int __gr_offs;
4591 // int __vr_offs;
4592 // };
4593
4594 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
4595 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4596 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
4597 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
Tim Northovera2ee4332014-03-29 15:09:45 +00004598
John McCall7f416cc2015-09-08 08:05:57 +00004599 auto TyInfo = getContext().getTypeInfoInChars(Ty);
4600 CharUnits TyAlign = TyInfo.second;
4601
4602 Address reg_offs_p = Address::invalid();
4603 llvm::Value *reg_offs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004604 int reg_top_index;
John McCall7f416cc2015-09-08 08:05:57 +00004605 CharUnits reg_top_offset;
4606 int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity();
Tim Northoverb047bfa2014-11-27 21:02:49 +00004607 if (!IsFPR) {
Tim Northovera2ee4332014-03-29 15:09:45 +00004608 // 3 is the field number of __gr_offs
David Blaikie2e804282015-04-05 22:47:07 +00004609 reg_offs_p =
John McCall7f416cc2015-09-08 08:05:57 +00004610 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
4611 "gr_offs_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00004612 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
4613 reg_top_index = 1; // field number for __gr_top
John McCall7f416cc2015-09-08 08:05:57 +00004614 reg_top_offset = CharUnits::fromQuantity(8);
Rui Ueyama83aa9792016-01-14 21:00:27 +00004615 RegSize = llvm::alignTo(RegSize, 8);
Tim Northovera2ee4332014-03-29 15:09:45 +00004616 } else {
Tim Northovera2ee4332014-03-29 15:09:45 +00004617 // 4 is the field number of __vr_offs.
David Blaikie2e804282015-04-05 22:47:07 +00004618 reg_offs_p =
John McCall7f416cc2015-09-08 08:05:57 +00004619 CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28),
4620 "vr_offs_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00004621 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
4622 reg_top_index = 2; // field number for __vr_top
John McCall7f416cc2015-09-08 08:05:57 +00004623 reg_top_offset = CharUnits::fromQuantity(16);
Tim Northoverb047bfa2014-11-27 21:02:49 +00004624 RegSize = 16 * NumRegs;
Tim Northovera2ee4332014-03-29 15:09:45 +00004625 }
4626
4627 //=======================================
4628 // Find out where argument was passed
4629 //=======================================
4630
4631 // If reg_offs >= 0 we're already using the stack for this type of
4632 // argument. We don't want to keep updating reg_offs (in case it overflows,
4633 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
4634 // whatever they get).
Craig Topper8a13c412014-05-21 05:09:00 +00004635 llvm::Value *UsingStack = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004636 UsingStack = CGF.Builder.CreateICmpSGE(
4637 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
4638
4639 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
4640
4641 // Otherwise, at least some kind of argument could go in these registers, the
Bob Wilson3abf1692014-04-21 01:23:36 +00004642 // question is whether this particular type is too big.
Tim Northovera2ee4332014-03-29 15:09:45 +00004643 CGF.EmitBlock(MaybeRegBlock);
4644
4645 // Integer arguments may need to correct register alignment (for example a
4646 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
4647 // align __gr_offs to calculate the potential address.
John McCall7f416cc2015-09-08 08:05:57 +00004648 if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
4649 int Align = TyAlign.getQuantity();
Tim Northovera2ee4332014-03-29 15:09:45 +00004650
4651 reg_offs = CGF.Builder.CreateAdd(
4652 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
4653 "align_regoffs");
4654 reg_offs = CGF.Builder.CreateAnd(
4655 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
4656 "aligned_regoffs");
4657 }
4658
4659 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
John McCall7f416cc2015-09-08 08:05:57 +00004660 // The fact that this is done unconditionally reflects the fact that
4661 // allocating an argument to the stack also uses up all the remaining
4662 // registers of the appropriate kind.
Craig Topper8a13c412014-05-21 05:09:00 +00004663 llvm::Value *NewOffset = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004664 NewOffset = CGF.Builder.CreateAdd(
4665 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
4666 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
4667
4668 // Now we're in a position to decide whether this argument really was in
4669 // registers or not.
Craig Topper8a13c412014-05-21 05:09:00 +00004670 llvm::Value *InRegs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004671 InRegs = CGF.Builder.CreateICmpSLE(
4672 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
4673
4674 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
4675
4676 //=======================================
4677 // Argument was in registers
4678 //=======================================
4679
4680 // Now we emit the code for if the argument was originally passed in
4681 // registers. First start the appropriate block:
4682 CGF.EmitBlock(InRegBlock);
4683
John McCall7f416cc2015-09-08 08:05:57 +00004684 llvm::Value *reg_top = nullptr;
4685 Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index,
4686 reg_top_offset, "reg_top_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00004687 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
John McCall7f416cc2015-09-08 08:05:57 +00004688 Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
4689 CharUnits::fromQuantity(IsFPR ? 16 : 8));
4690 Address RegAddr = Address::invalid();
4691 llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00004692
4693 if (IsIndirect) {
4694 // If it's been passed indirectly (actually a struct), whatever we find from
4695 // stored registers or on the stack will actually be a struct **.
4696 MemTy = llvm::PointerType::getUnqual(MemTy);
4697 }
4698
Craig Topper8a13c412014-05-21 05:09:00 +00004699 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004700 uint64_t NumMembers = 0;
4701 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
James Molloy467be602014-05-07 14:45:55 +00004702 if (IsHFA && NumMembers > 1) {
Tim Northovera2ee4332014-03-29 15:09:45 +00004703 // Homogeneous aggregates passed in registers will have their elements split
4704 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4705 // qN+1, ...). We reload and store into a temporary local variable
4706 // contiguously.
4707 assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
John McCall7f416cc2015-09-08 08:05:57 +00004708 auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
Tim Northovera2ee4332014-03-29 15:09:45 +00004709 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4710 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
John McCall7f416cc2015-09-08 08:05:57 +00004711 Address Tmp = CGF.CreateTempAlloca(HFATy,
4712 std::max(TyAlign, BaseTyInfo.second));
Tim Northovera2ee4332014-03-29 15:09:45 +00004713
John McCall7f416cc2015-09-08 08:05:57 +00004714 // On big-endian platforms, the value will be right-aligned in its slot.
4715 int Offset = 0;
4716 if (CGF.CGM.getDataLayout().isBigEndian() &&
4717 BaseTyInfo.first.getQuantity() < 16)
4718 Offset = 16 - BaseTyInfo.first.getQuantity();
4719
Tim Northovera2ee4332014-03-29 15:09:45 +00004720 for (unsigned i = 0; i < NumMembers; ++i) {
John McCall7f416cc2015-09-08 08:05:57 +00004721 CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
4722 Address LoadAddr =
4723 CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
4724 LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
4725
4726 Address StoreAddr =
4727 CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first);
Tim Northovera2ee4332014-03-29 15:09:45 +00004728
4729 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4730 CGF.Builder.CreateStore(Elem, StoreAddr);
4731 }
4732
John McCall7f416cc2015-09-08 08:05:57 +00004733 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00004734 } else {
John McCall7f416cc2015-09-08 08:05:57 +00004735 // Otherwise the object is contiguous in memory.
4736
4737 // It might be right-aligned in its slot.
4738 CharUnits SlotSize = BaseAddr.getAlignment();
4739 if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
James Molloy467be602014-05-07 14:45:55 +00004740 (IsHFA || !isAggregateTypeForABI(Ty)) &&
John McCall7f416cc2015-09-08 08:05:57 +00004741 TyInfo.first < SlotSize) {
4742 CharUnits Offset = SlotSize - TyInfo.first;
4743 BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
Tim Northovera2ee4332014-03-29 15:09:45 +00004744 }
4745
John McCall7f416cc2015-09-08 08:05:57 +00004746 RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00004747 }
4748
4749 CGF.EmitBranch(ContBlock);
4750
4751 //=======================================
4752 // Argument was on the stack
4753 //=======================================
4754 CGF.EmitBlock(OnStackBlock);
4755
John McCall7f416cc2015-09-08 08:05:57 +00004756 Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0,
4757 CharUnits::Zero(), "stack_p");
4758 llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
Tim Northovera2ee4332014-03-29 15:09:45 +00004759
John McCall7f416cc2015-09-08 08:05:57 +00004760 // Again, stack arguments may need realignment. In this case both integer and
Tim Northovera2ee4332014-03-29 15:09:45 +00004761 // floating-point ones might be affected.
John McCall7f416cc2015-09-08 08:05:57 +00004762 if (!IsIndirect && TyAlign.getQuantity() > 8) {
4763 int Align = TyAlign.getQuantity();
Tim Northovera2ee4332014-03-29 15:09:45 +00004764
John McCall7f416cc2015-09-08 08:05:57 +00004765 OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00004766
John McCall7f416cc2015-09-08 08:05:57 +00004767 OnStackPtr = CGF.Builder.CreateAdd(
4768 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
Tim Northovera2ee4332014-03-29 15:09:45 +00004769 "align_stack");
John McCall7f416cc2015-09-08 08:05:57 +00004770 OnStackPtr = CGF.Builder.CreateAnd(
4771 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
Tim Northovera2ee4332014-03-29 15:09:45 +00004772 "align_stack");
4773
John McCall7f416cc2015-09-08 08:05:57 +00004774 OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00004775 }
John McCall7f416cc2015-09-08 08:05:57 +00004776 Address OnStackAddr(OnStackPtr,
4777 std::max(CharUnits::fromQuantity(8), TyAlign));
Tim Northovera2ee4332014-03-29 15:09:45 +00004778
John McCall7f416cc2015-09-08 08:05:57 +00004779 // All stack slots are multiples of 8 bytes.
4780 CharUnits StackSlotSize = CharUnits::fromQuantity(8);
4781 CharUnits StackSize;
Tim Northovera2ee4332014-03-29 15:09:45 +00004782 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00004783 StackSize = StackSlotSize;
Tim Northovera2ee4332014-03-29 15:09:45 +00004784 else
Rui Ueyama83aa9792016-01-14 21:00:27 +00004785 StackSize = TyInfo.first.alignTo(StackSlotSize);
Tim Northovera2ee4332014-03-29 15:09:45 +00004786
John McCall7f416cc2015-09-08 08:05:57 +00004787 llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
Tim Northovera2ee4332014-03-29 15:09:45 +00004788 llvm::Value *NewStack =
John McCall7f416cc2015-09-08 08:05:57 +00004789 CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
Tim Northovera2ee4332014-03-29 15:09:45 +00004790
4791 // Write the new value of __stack for the next call to va_arg
4792 CGF.Builder.CreateStore(NewStack, stack_p);
4793
4794 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
John McCall7f416cc2015-09-08 08:05:57 +00004795 TyInfo.first < StackSlotSize) {
4796 CharUnits Offset = StackSlotSize - TyInfo.first;
4797 OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
Tim Northovera2ee4332014-03-29 15:09:45 +00004798 }
4799
John McCall7f416cc2015-09-08 08:05:57 +00004800 OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00004801
4802 CGF.EmitBranch(ContBlock);
4803
4804 //=======================================
4805 // Tidy up
4806 //=======================================
4807 CGF.EmitBlock(ContBlock);
4808
John McCall7f416cc2015-09-08 08:05:57 +00004809 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
4810 OnStackAddr, OnStackBlock, "vaargs.addr");
Tim Northovera2ee4332014-03-29 15:09:45 +00004811
4812 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00004813 return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
4814 TyInfo.second);
Tim Northovera2ee4332014-03-29 15:09:45 +00004815
4816 return ResAddr;
4817}
4818
John McCall7f416cc2015-09-08 08:05:57 +00004819Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4820 CodeGenFunction &CGF) const {
4821 // The backend's lowering doesn't support va_arg for aggregates or
4822 // illegal vector types. Lower VAArg here for these cases and use
4823 // the LLVM va_arg instruction for everything else.
Tim Northovera2ee4332014-03-29 15:09:45 +00004824 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
James Y Knight29b5f082016-02-24 02:59:33 +00004825 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Tim Northovera2ee4332014-03-29 15:09:45 +00004826
John McCall7f416cc2015-09-08 08:05:57 +00004827 CharUnits SlotSize = CharUnits::fromQuantity(8);
Tim Northovera2ee4332014-03-29 15:09:45 +00004828
John McCall7f416cc2015-09-08 08:05:57 +00004829 // Empty records are ignored for parameter passing purposes.
Tim Northovera2ee4332014-03-29 15:09:45 +00004830 if (isEmptyRecord(getContext(), Ty, true)) {
John McCall7f416cc2015-09-08 08:05:57 +00004831 Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
4832 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
4833 return Addr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004834 }
4835
John McCall7f416cc2015-09-08 08:05:57 +00004836 // The size of the actual thing passed, which might end up just
4837 // being a pointer for indirect types.
4838 auto TyInfo = getContext().getTypeInfoInChars(Ty);
4839
4840 // Arguments bigger than 16 bytes which aren't homogeneous
4841 // aggregates should be passed indirectly.
4842 bool IsIndirect = false;
4843 if (TyInfo.first.getQuantity() > 16) {
4844 const Type *Base = nullptr;
4845 uint64_t Members = 0;
4846 IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
Tim Northovera2ee4332014-03-29 15:09:45 +00004847 }
4848
John McCall7f416cc2015-09-08 08:05:57 +00004849 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4850 TyInfo, SlotSize, /*AllowHigherAlign*/ true);
Tim Northovera2ee4332014-03-29 15:09:45 +00004851}
4852
4853//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00004854// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00004855//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00004856
4857namespace {
4858
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004859class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00004860public:
4861 enum ABIKind {
4862 APCS = 0,
4863 AAPCS = 1,
Tim Northover5627d392015-10-30 16:30:45 +00004864 AAPCS_VFP = 2,
4865 AAPCS16_VFP = 3,
Daniel Dunbar020daa92009-09-12 01:00:39 +00004866 };
4867
4868private:
4869 ABIKind Kind;
4870
4871public:
Tim Northoverbc784d12015-02-24 17:22:40 +00004872 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00004873 setCCs();
John McCall882987f2013-02-28 19:01:20 +00004874 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00004875
John McCall3480ef22011-08-30 01:42:09 +00004876 bool isEABI() const {
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00004877 switch (getTarget().getTriple().getEnvironment()) {
4878 case llvm::Triple::Android:
4879 case llvm::Triple::EABI:
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00004880 case llvm::Triple::EABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00004881 case llvm::Triple::GNUEABI:
Joerg Sonnenberger0c1652d2013-12-16 18:30:28 +00004882 case llvm::Triple::GNUEABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00004883 return true;
4884 default:
4885 return false;
4886 }
John McCall3480ef22011-08-30 01:42:09 +00004887 }
4888
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00004889 bool isEABIHF() const {
4890 switch (getTarget().getTriple().getEnvironment()) {
4891 case llvm::Triple::EABIHF:
4892 case llvm::Triple::GNUEABIHF:
4893 return true;
4894 default:
4895 return false;
4896 }
4897 }
4898
Daniel Dunbar020daa92009-09-12 01:00:39 +00004899 ABIKind getABIKind() const { return Kind; }
4900
Tim Northovera484bc02013-10-01 14:34:25 +00004901private:
Amara Emerson9dc78782014-01-28 10:56:36 +00004902 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
Tim Northoverbc784d12015-02-24 17:22:40 +00004903 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
Manman Renfef9e312012-10-16 19:18:39 +00004904 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004905
Reid Klecknere9f6a712014-10-31 17:10:41 +00004906 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4907 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4908 uint64_t Members) const override;
4909
Craig Topper4f12f102014-03-12 06:41:41 +00004910 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004911
John McCall7f416cc2015-09-08 08:05:57 +00004912 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4913 QualType Ty) const override;
John McCall882987f2013-02-28 19:01:20 +00004914
4915 llvm::CallingConv::ID getLLVMDefaultCC() const;
4916 llvm::CallingConv::ID getABIDefaultCC() const;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00004917 void setCCs();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00004918};
4919
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004920class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
4921public:
Chris Lattner2b037972010-07-29 02:01:43 +00004922 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
4923 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00004924
John McCall3480ef22011-08-30 01:42:09 +00004925 const ARMABIInfo &getABIInfo() const {
4926 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
4927 }
4928
Craig Topper4f12f102014-03-12 06:41:41 +00004929 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallbeec5a02010-03-06 00:35:14 +00004930 return 13;
4931 }
Roman Divackyc1617352011-05-18 19:36:54 +00004932
Craig Topper4f12f102014-03-12 06:41:41 +00004933 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
John McCall31168b02011-06-15 23:02:42 +00004934 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
4935 }
4936
Roman Divackyc1617352011-05-18 19:36:54 +00004937 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004938 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00004939 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divackyc1617352011-05-18 19:36:54 +00004940
4941 // 0-15 are the 16 integer registers.
Chris Lattnerece04092012-02-07 00:39:47 +00004942 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divackyc1617352011-05-18 19:36:54 +00004943 return false;
4944 }
John McCall3480ef22011-08-30 01:42:09 +00004945
Craig Topper4f12f102014-03-12 06:41:41 +00004946 unsigned getSizeOfUnwindException() const override {
John McCall3480ef22011-08-30 01:42:09 +00004947 if (getABIInfo().isEABI()) return 88;
4948 return TargetCodeGenInfo::getSizeOfUnwindException();
4949 }
Tim Northovera484bc02013-10-01 14:34:25 +00004950
Eric Christopher162c91c2015-06-05 22:03:00 +00004951 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00004952 CodeGen::CodeGenModule &CGM) const override {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00004953 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Tim Northovera484bc02013-10-01 14:34:25 +00004954 if (!FD)
4955 return;
4956
4957 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
4958 if (!Attr)
4959 return;
4960
4961 const char *Kind;
4962 switch (Attr->getInterrupt()) {
4963 case ARMInterruptAttr::Generic: Kind = ""; break;
4964 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
4965 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
4966 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
4967 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
4968 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
4969 }
4970
4971 llvm::Function *Fn = cast<llvm::Function>(GV);
4972
4973 Fn->addFnAttr("interrupt", Kind);
4974
Tim Northover5627d392015-10-30 16:30:45 +00004975 ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
4976 if (ABI == ARMABIInfo::APCS)
Tim Northovera484bc02013-10-01 14:34:25 +00004977 return;
4978
4979 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
4980 // however this is not necessarily true on taking any interrupt. Instruct
4981 // the backend to perform a realignment as part of the function prologue.
4982 llvm::AttrBuilder B;
4983 B.addStackAlignmentAttr(8);
4984 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
4985 llvm::AttributeSet::get(CGM.getLLVMContext(),
4986 llvm::AttributeSet::FunctionIndex,
4987 B));
4988 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00004989};
4990
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00004991class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00004992public:
4993 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
4994 : ARMTargetCodeGenInfo(CGT, K) {}
4995
Eric Christopher162c91c2015-06-05 22:03:00 +00004996 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00004997 CodeGen::CodeGenModule &CGM) const override;
4998};
4999
Eric Christopher162c91c2015-06-05 22:03:00 +00005000void WindowsARMTargetCodeGenInfo::setTargetAttributes(
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005001 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
Eric Christopher162c91c2015-06-05 22:03:00 +00005002 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005003 addStackProbeSizeTargetAttribute(D, GV, CGM);
5004}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005005}
Daniel Dunbard59655c2009-09-12 00:59:49 +00005006
Chris Lattner22326a12010-07-29 02:31:05 +00005007void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Tim Northoverbc784d12015-02-24 17:22:40 +00005008 if (!getCXXABI().classifyReturnType(FI))
Eric Christopher7565e0d2015-05-29 23:09:49 +00005009 FI.getReturnInfo() =
5010 classifyReturnType(FI.getReturnType(), FI.isVariadic());
Oliver Stannard405bded2014-02-11 09:25:50 +00005011
Tim Northoverbc784d12015-02-24 17:22:40 +00005012 for (auto &I : FI.arguments())
5013 I.info = classifyArgumentType(I.type, FI.isVariadic());
Daniel Dunbar020daa92009-09-12 01:00:39 +00005014
Anton Korobeynikov231e8752011-04-14 20:06:49 +00005015 // Always honor user-specified calling convention.
5016 if (FI.getCallingConvention() != llvm::CallingConv::C)
5017 return;
5018
John McCall882987f2013-02-28 19:01:20 +00005019 llvm::CallingConv::ID cc = getRuntimeCC();
5020 if (cc != llvm::CallingConv::C)
Tim Northoverbc784d12015-02-24 17:22:40 +00005021 FI.setEffectiveCallingConvention(cc);
John McCall882987f2013-02-28 19:01:20 +00005022}
Rafael Espindolaa92c4422010-06-16 16:13:39 +00005023
John McCall882987f2013-02-28 19:01:20 +00005024/// Return the default calling convention that LLVM will use.
5025llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
5026 // The default calling convention that LLVM will infer.
Tim Northoverd88ecb32016-01-27 19:32:40 +00005027 if (isEABIHF() || getTarget().getTriple().isWatchABI())
John McCall882987f2013-02-28 19:01:20 +00005028 return llvm::CallingConv::ARM_AAPCS_VFP;
5029 else if (isEABI())
5030 return llvm::CallingConv::ARM_AAPCS;
5031 else
5032 return llvm::CallingConv::ARM_APCS;
5033}
5034
5035/// Return the calling convention that our ABI would like us to use
5036/// as the C calling convention.
5037llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar020daa92009-09-12 01:00:39 +00005038 switch (getABIKind()) {
John McCall882987f2013-02-28 19:01:20 +00005039 case APCS: return llvm::CallingConv::ARM_APCS;
5040 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
5041 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Tim Northover5627d392015-10-30 16:30:45 +00005042 case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar020daa92009-09-12 01:00:39 +00005043 }
John McCall882987f2013-02-28 19:01:20 +00005044 llvm_unreachable("bad ABI kind");
5045}
5046
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005047void ARMABIInfo::setCCs() {
John McCall882987f2013-02-28 19:01:20 +00005048 assert(getRuntimeCC() == llvm::CallingConv::C);
5049
5050 // Don't muddy up the IR with a ton of explicit annotations if
5051 // they'd just match what LLVM will infer from the triple.
5052 llvm::CallingConv::ID abiCC = getABIDefaultCC();
5053 if (abiCC != getLLVMDefaultCC())
5054 RuntimeCC = abiCC;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005055
Tim Northover5627d392015-10-30 16:30:45 +00005056 // AAPCS apparently requires runtime support functions to be soft-float, but
5057 // that's almost certainly for historic reasons (Thumb1 not supporting VFP
5058 // most likely). It's more convenient for AAPCS16_VFP to be hard-float.
5059 switch (getABIKind()) {
5060 case APCS:
5061 case AAPCS16_VFP:
5062 if (abiCC != getLLVMDefaultCC())
5063 BuiltinCC = abiCC;
5064 break;
5065 case AAPCS:
5066 case AAPCS_VFP:
5067 BuiltinCC = llvm::CallingConv::ARM_AAPCS;
5068 break;
5069 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005070}
5071
Tim Northoverbc784d12015-02-24 17:22:40 +00005072ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
5073 bool isVariadic) const {
Manman Ren2a523d82012-10-30 23:21:41 +00005074 // 6.1.2.1 The following argument types are VFP CPRCs:
5075 // A single-precision floating-point type (including promoted
5076 // half-precision types); A double-precision floating-point type;
5077 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
5078 // with a Base Type of a single- or double-precision floating-point type,
5079 // 64-bit containerized vectors or 128-bit containerized vectors with one
5080 // to four Elements.
Tim Northover5a1558e2014-11-07 22:30:50 +00005081 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005082
Reid Klecknerb1be6832014-11-15 01:41:41 +00005083 Ty = useFirstFieldIfTransparentUnion(Ty);
5084
Manman Renfef9e312012-10-16 19:18:39 +00005085 // Handle illegal vector types here.
5086 if (isIllegalVectorType(Ty)) {
5087 uint64_t Size = getContext().getTypeSize(Ty);
5088 if (Size <= 32) {
5089 llvm::Type *ResType =
5090 llvm::Type::getInt32Ty(getVMContext());
Tim Northover5a1558e2014-11-07 22:30:50 +00005091 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005092 }
5093 if (Size == 64) {
5094 llvm::Type *ResType = llvm::VectorType::get(
5095 llvm::Type::getInt32Ty(getVMContext()), 2);
Tim Northover5a1558e2014-11-07 22:30:50 +00005096 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005097 }
5098 if (Size == 128) {
5099 llvm::Type *ResType = llvm::VectorType::get(
5100 llvm::Type::getInt32Ty(getVMContext()), 4);
Tim Northover5a1558e2014-11-07 22:30:50 +00005101 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005102 }
John McCall7f416cc2015-09-08 08:05:57 +00005103 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Manman Renfef9e312012-10-16 19:18:39 +00005104 }
5105
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005106 // __fp16 gets passed as if it were an int or float, but with the top 16 bits
5107 // unspecified. This is not done for OpenCL as it handles the half type
5108 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar8e2e9d62016-03-18 16:58:36 +00005109 if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005110 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5111 llvm::Type::getFloatTy(getVMContext()) :
5112 llvm::Type::getInt32Ty(getVMContext());
5113 return ABIArgInfo::getDirect(ResType);
5114 }
5115
John McCalla1dee5302010-08-22 10:59:02 +00005116 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005117 // Treat an enum type as its underlying type.
Oliver Stannard405bded2014-02-11 09:25:50 +00005118 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005119 Ty = EnumTy->getDecl()->getIntegerType();
Oliver Stannard405bded2014-02-11 09:25:50 +00005120 }
Douglas Gregora71cc152010-02-02 20:10:50 +00005121
Tim Northover5a1558e2014-11-07 22:30:50 +00005122 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5123 : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00005124 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005125
Oliver Stannard405bded2014-02-11 09:25:50 +00005126 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
John McCall7f416cc2015-09-08 08:05:57 +00005127 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Oliver Stannard405bded2014-02-11 09:25:50 +00005128 }
Tim Northover1060eae2013-06-21 22:49:34 +00005129
Daniel Dunbar09d33622009-09-14 21:54:03 +00005130 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005131 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00005132 return ABIArgInfo::getIgnore();
5133
Tim Northover5a1558e2014-11-07 22:30:50 +00005134 if (IsEffectivelyAAPCS_VFP) {
Manman Ren2a523d82012-10-30 23:21:41 +00005135 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
5136 // into VFP registers.
Craig Topper8a13c412014-05-21 05:09:00 +00005137 const Type *Base = nullptr;
Manman Ren2a523d82012-10-30 23:21:41 +00005138 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005139 if (isHomogeneousAggregate(Ty, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005140 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Ren2a523d82012-10-30 23:21:41 +00005141 // Base can be a floating-point or a vector.
Tim Northover5a1558e2014-11-07 22:30:50 +00005142 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005143 }
Tim Northover5627d392015-10-30 16:30:45 +00005144 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5145 // WatchOS does have homogeneous aggregates. Note that we intentionally use
5146 // this convention even for a variadic function: the backend will use GPRs
5147 // if needed.
5148 const Type *Base = nullptr;
5149 uint64_t Members = 0;
5150 if (isHomogeneousAggregate(Ty, Base, Members)) {
5151 assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
5152 llvm::Type *Ty =
5153 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
5154 return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5155 }
5156 }
5157
5158 if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5159 getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
5160 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
5161 // bigger than 128-bits, they get placed in space allocated by the caller,
5162 // and a pointer is passed.
5163 return ABIArgInfo::getIndirect(
5164 CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
Bob Wilsone826a2a2011-08-03 05:58:22 +00005165 }
5166
Manman Ren6c30e132012-08-13 21:23:55 +00005167 // Support byval for ARM.
Manman Ren77b02382012-11-06 19:05:29 +00005168 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
5169 // most 8-byte. We realign the indirect argument if type alignment is bigger
5170 // than ABI alignment.
Manman Ren505d68f2012-11-05 22:42:46 +00005171 uint64_t ABIAlign = 4;
5172 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
5173 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
Tim Northoverd157e192015-03-09 21:40:42 +00005174 getABIKind() == ARMABIInfo::AAPCS)
Manman Ren505d68f2012-11-05 22:42:46 +00005175 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Tim Northoverd157e192015-03-09 21:40:42 +00005176
Manman Ren8cd99812012-11-06 04:58:01 +00005177 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
Tim Northover5627d392015-10-30 16:30:45 +00005178 assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
John McCall7f416cc2015-09-08 08:05:57 +00005179 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5180 /*ByVal=*/true,
5181 /*Realign=*/TyAlign > ABIAlign);
Eli Friedmane66abda2012-08-09 00:31:40 +00005182 }
5183
Daniel Dunbarb34b0802010-09-23 01:54:28 +00005184 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2192fe52011-07-18 04:24:23 +00005185 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005186 unsigned SizeRegs;
Eli Friedmane66abda2012-08-09 00:31:40 +00005187 // FIXME: Try to match the types of the arguments more accurately where
5188 // we can.
5189 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson8e2b75d2011-08-01 23:39:04 +00005190 ElemTy = llvm::Type::getInt32Ty(getVMContext());
5191 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren6fdb1582012-06-25 22:04:00 +00005192 } else {
Manman Ren6fdb1582012-06-25 22:04:00 +00005193 ElemTy = llvm::Type::getInt64Ty(getVMContext());
5194 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00005195 }
Stuart Hastings4b214952011-04-28 18:16:06 +00005196
Tim Northover5a1558e2014-11-07 22:30:50 +00005197 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005198}
5199
Chris Lattner458b2aa2010-07-29 02:16:43 +00005200static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005201 llvm::LLVMContext &VMContext) {
5202 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
5203 // is called integer-like if its size is less than or equal to one word, and
5204 // the offset of each of its addressable sub-fields is zero.
5205
5206 uint64_t Size = Context.getTypeSize(Ty);
5207
5208 // Check that the type fits in a word.
5209 if (Size > 32)
5210 return false;
5211
5212 // FIXME: Handle vector types!
5213 if (Ty->isVectorType())
5214 return false;
5215
Daniel Dunbard53bac72009-09-14 02:20:34 +00005216 // Float types are never treated as "integer like".
5217 if (Ty->isRealFloatingType())
5218 return false;
5219
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005220 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00005221 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005222 return true;
5223
Daniel Dunbar96ebba52010-02-01 23:31:26 +00005224 // Small complex integer types are "integer like".
5225 if (const ComplexType *CT = Ty->getAs<ComplexType>())
5226 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005227
5228 // Single element and zero sized arrays should be allowed, by the definition
5229 // above, but they are not.
5230
5231 // Otherwise, it must be a record type.
5232 const RecordType *RT = Ty->getAs<RecordType>();
5233 if (!RT) return false;
5234
5235 // Ignore records with flexible arrays.
5236 const RecordDecl *RD = RT->getDecl();
5237 if (RD->hasFlexibleArrayMember())
5238 return false;
5239
5240 // Check that all sub-fields are at offset 0, and are themselves "integer
5241 // like".
5242 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
5243
5244 bool HadField = false;
5245 unsigned idx = 0;
5246 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5247 i != e; ++i, ++idx) {
David Blaikie40ed2972012-06-06 20:45:41 +00005248 const FieldDecl *FD = *i;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005249
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005250 // Bit-fields are not addressable, we only need to verify they are "integer
5251 // like". We still have to disallow a subsequent non-bitfield, for example:
5252 // struct { int : 0; int x }
5253 // is non-integer like according to gcc.
5254 if (FD->isBitField()) {
5255 if (!RD->isUnion())
5256 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005257
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005258 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5259 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005260
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005261 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005262 }
5263
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005264 // Check if this field is at offset 0.
5265 if (Layout.getFieldOffset(idx) != 0)
5266 return false;
5267
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005268 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5269 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00005270
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005271 // Only allow at most one field in a structure. This doesn't match the
5272 // wording above, but follows gcc in situations with a field following an
5273 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005274 if (!RD->isUnion()) {
5275 if (HadField)
5276 return false;
5277
5278 HadField = true;
5279 }
5280 }
5281
5282 return true;
5283}
5284
Oliver Stannard405bded2014-02-11 09:25:50 +00005285ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
5286 bool isVariadic) const {
Tim Northover5627d392015-10-30 16:30:45 +00005287 bool IsEffectivelyAAPCS_VFP =
5288 (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005289
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005290 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005291 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005292
Daniel Dunbar19964db2010-09-23 01:54:32 +00005293 // Large vector types should be returned via memory.
Oliver Stannard405bded2014-02-11 09:25:50 +00005294 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
John McCall7f416cc2015-09-08 08:05:57 +00005295 return getNaturalAlignIndirect(RetTy);
Oliver Stannard405bded2014-02-11 09:25:50 +00005296 }
Daniel Dunbar19964db2010-09-23 01:54:32 +00005297
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005298 // __fp16 gets returned as if it were an int or float, but with the top 16
5299 // bits unspecified. This is not done for OpenCL as it handles the half type
5300 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar8e2e9d62016-03-18 16:58:36 +00005301 if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005302 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5303 llvm::Type::getFloatTy(getVMContext()) :
5304 llvm::Type::getInt32Ty(getVMContext());
5305 return ABIArgInfo::getDirect(ResType);
5306 }
5307
John McCalla1dee5302010-08-22 10:59:02 +00005308 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005309 // Treat an enum type as its underlying type.
5310 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5311 RetTy = EnumTy->getDecl()->getIntegerType();
5312
Tim Northover5a1558e2014-11-07 22:30:50 +00005313 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5314 : ABIArgInfo::getDirect();
Douglas Gregora71cc152010-02-02 20:10:50 +00005315 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005316
5317 // Are we following APCS?
5318 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00005319 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005320 return ABIArgInfo::getIgnore();
5321
Daniel Dunbareedf1512010-02-01 23:31:19 +00005322 // Complex types are all returned as packed integers.
5323 //
5324 // FIXME: Consider using 2 x vector types if the back end handles them
5325 // correctly.
5326 if (RetTy->isAnyComplexType())
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005327 return ABIArgInfo::getDirect(llvm::IntegerType::get(
5328 getVMContext(), getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00005329
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005330 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005331 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005332 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005333 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005334 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00005335 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005336 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00005337 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5338 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005339 }
5340
5341 // Otherwise return in memory.
John McCall7f416cc2015-09-08 08:05:57 +00005342 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005343 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005344
5345 // Otherwise this is an AAPCS variant.
5346
Chris Lattner458b2aa2010-07-29 02:16:43 +00005347 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005348 return ABIArgInfo::getIgnore();
5349
Bob Wilson1d9269a2011-11-02 04:51:36 +00005350 // Check for homogeneous aggregates with AAPCS-VFP.
Tim Northover5a1558e2014-11-07 22:30:50 +00005351 if (IsEffectivelyAAPCS_VFP) {
Craig Topper8a13c412014-05-21 05:09:00 +00005352 const Type *Base = nullptr;
Tim Northover5627d392015-10-30 16:30:45 +00005353 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005354 if (isHomogeneousAggregate(RetTy, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005355 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson1d9269a2011-11-02 04:51:36 +00005356 // Homogeneous Aggregates are returned directly.
Tim Northover5a1558e2014-11-07 22:30:50 +00005357 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005358 }
Bob Wilson1d9269a2011-11-02 04:51:36 +00005359 }
5360
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005361 // Aggregates <= 4 bytes are returned in r0; other aggregates
5362 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005363 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005364 if (Size <= 32) {
Christian Pirkerc3d32172014-07-03 09:28:12 +00005365 if (getDataLayout().isBigEndian())
5366 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
Tim Northover5a1558e2014-11-07 22:30:50 +00005367 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Christian Pirkerc3d32172014-07-03 09:28:12 +00005368
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005369 // Return in the smallest viable integer type.
5370 if (Size <= 8)
Tim Northover5a1558e2014-11-07 22:30:50 +00005371 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005372 if (Size <= 16)
Tim Northover5a1558e2014-11-07 22:30:50 +00005373 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5374 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Tim Northover5627d392015-10-30 16:30:45 +00005375 } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
5376 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
5377 llvm::Type *CoerceTy =
Rui Ueyama83aa9792016-01-14 21:00:27 +00005378 llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
Tim Northover5627d392015-10-30 16:30:45 +00005379 return ABIArgInfo::getDirect(CoerceTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005380 }
5381
John McCall7f416cc2015-09-08 08:05:57 +00005382 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005383}
5384
Manman Renfef9e312012-10-16 19:18:39 +00005385/// isIllegalVector - check whether Ty is an illegal vector type.
5386bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
Stephen Hines8267e7d2015-12-04 01:39:30 +00005387 if (const VectorType *VT = Ty->getAs<VectorType> ()) {
5388 if (isAndroid()) {
5389 // Android shipped using Clang 3.1, which supported a slightly different
5390 // vector ABI. The primary differences were that 3-element vector types
5391 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
5392 // accepts that legacy behavior for Android only.
5393 // Check whether VT is legal.
5394 unsigned NumElements = VT->getNumElements();
5395 // NumElements should be power of 2 or equal to 3.
5396 if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
5397 return true;
5398 } else {
5399 // Check whether VT is legal.
5400 unsigned NumElements = VT->getNumElements();
5401 uint64_t Size = getContext().getTypeSize(VT);
5402 // NumElements should be power of 2.
5403 if (!llvm::isPowerOf2_32(NumElements))
5404 return true;
5405 // Size should be greater than 32 bits.
5406 return Size <= 32;
5407 }
Manman Renfef9e312012-10-16 19:18:39 +00005408 }
5409 return false;
5410}
5411
Reid Klecknere9f6a712014-10-31 17:10:41 +00005412bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5413 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
5414 // double, or 64-bit or 128-bit vectors.
5415 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5416 if (BT->getKind() == BuiltinType::Float ||
5417 BT->getKind() == BuiltinType::Double ||
5418 BT->getKind() == BuiltinType::LongDouble)
5419 return true;
5420 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5421 unsigned VecSize = getContext().getTypeSize(VT);
5422 if (VecSize == 64 || VecSize == 128)
5423 return true;
5424 }
5425 return false;
5426}
5427
5428bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5429 uint64_t Members) const {
5430 return Members <= 4;
5431}
5432
John McCall7f416cc2015-09-08 08:05:57 +00005433Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5434 QualType Ty) const {
5435 CharUnits SlotSize = CharUnits::fromQuantity(4);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005436
John McCall7f416cc2015-09-08 08:05:57 +00005437 // Empty records are ignored for parameter passing purposes.
Tim Northover1711cc92013-06-21 23:05:33 +00005438 if (isEmptyRecord(getContext(), Ty, true)) {
John McCall7f416cc2015-09-08 08:05:57 +00005439 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
5440 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5441 return Addr;
Tim Northover1711cc92013-06-21 23:05:33 +00005442 }
5443
John McCall7f416cc2015-09-08 08:05:57 +00005444 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5445 CharUnits TyAlignForABI = TyInfo.second;
Manman Rencca54d02012-10-16 19:01:37 +00005446
John McCall7f416cc2015-09-08 08:05:57 +00005447 // Use indirect if size of the illegal vector is bigger than 16 bytes.
5448 bool IsIndirect = false;
Tim Northover5627d392015-10-30 16:30:45 +00005449 const Type *Base = nullptr;
5450 uint64_t Members = 0;
John McCall7f416cc2015-09-08 08:05:57 +00005451 if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
5452 IsIndirect = true;
5453
Tim Northover5627d392015-10-30 16:30:45 +00005454 // ARMv7k passes structs bigger than 16 bytes indirectly, in space
5455 // allocated by the caller.
5456 } else if (TyInfo.first > CharUnits::fromQuantity(16) &&
5457 getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5458 !isHomogeneousAggregate(Ty, Base, Members)) {
5459 IsIndirect = true;
5460
John McCall7f416cc2015-09-08 08:05:57 +00005461 // Otherwise, bound the type's ABI alignment.
Manman Rencca54d02012-10-16 19:01:37 +00005462 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
5463 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
John McCall7f416cc2015-09-08 08:05:57 +00005464 // Our callers should be prepared to handle an under-aligned address.
5465 } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
5466 getABIKind() == ARMABIInfo::AAPCS) {
5467 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
5468 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
Tim Northover4c5cb9c2015-11-02 19:32:23 +00005469 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5470 // ARMv7k allows type alignment up to 16 bytes.
5471 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
5472 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
John McCall7f416cc2015-09-08 08:05:57 +00005473 } else {
5474 TyAlignForABI = CharUnits::fromQuantity(4);
Manman Renfef9e312012-10-16 19:18:39 +00005475 }
John McCall7f416cc2015-09-08 08:05:57 +00005476 TyInfo.second = TyAlignForABI;
Manman Rencca54d02012-10-16 19:01:37 +00005477
John McCall7f416cc2015-09-08 08:05:57 +00005478 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
5479 SlotSize, /*AllowHigherAlign*/ true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005480}
5481
Chris Lattner0cf24192010-06-28 20:05:43 +00005482//===----------------------------------------------------------------------===//
Justin Holewinski83e96682012-05-24 17:43:12 +00005483// NVPTX ABI Implementation
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005484//===----------------------------------------------------------------------===//
5485
5486namespace {
5487
Justin Holewinski83e96682012-05-24 17:43:12 +00005488class NVPTXABIInfo : public ABIInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005489public:
Justin Holewinski36837432013-03-30 14:38:24 +00005490 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005491
5492 ABIArgInfo classifyReturnType(QualType RetTy) const;
5493 ABIArgInfo classifyArgumentType(QualType Ty) const;
5494
Craig Topper4f12f102014-03-12 06:41:41 +00005495 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00005496 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5497 QualType Ty) const override;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005498};
5499
Justin Holewinski83e96682012-05-24 17:43:12 +00005500class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005501public:
Justin Holewinski83e96682012-05-24 17:43:12 +00005502 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
5503 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Craig Topper4f12f102014-03-12 06:41:41 +00005504
Eric Christopher162c91c2015-06-05 22:03:00 +00005505 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00005506 CodeGen::CodeGenModule &M) const override;
Justin Holewinski36837432013-03-30 14:38:24 +00005507private:
Eli Benderskye06a2c42014-04-15 16:57:05 +00005508 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
5509 // resulting MDNode to the nvvm.annotations MDNode.
5510 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005511};
5512
Justin Holewinski83e96682012-05-24 17:43:12 +00005513ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005514 if (RetTy->isVoidType())
5515 return ABIArgInfo::getIgnore();
Justin Holewinskif9329ff2013-11-20 20:35:34 +00005516
5517 // note: this is different from default ABI
5518 if (!RetTy->isScalarType())
5519 return ABIArgInfo::getDirect();
5520
5521 // Treat an enum type as its underlying type.
5522 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5523 RetTy = EnumTy->getDecl()->getIntegerType();
5524
5525 return (RetTy->isPromotableIntegerType() ?
5526 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005527}
5528
Justin Holewinski83e96682012-05-24 17:43:12 +00005529ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinskif9329ff2013-11-20 20:35:34 +00005530 // Treat an enum type as its underlying type.
5531 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5532 Ty = EnumTy->getDecl()->getIntegerType();
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005533
Eli Bendersky95338a02014-10-29 13:43:21 +00005534 // Return aggregates type as indirect by value
5535 if (isAggregateTypeForABI(Ty))
John McCall7f416cc2015-09-08 08:05:57 +00005536 return getNaturalAlignIndirect(Ty, /* byval */ true);
Eli Bendersky95338a02014-10-29 13:43:21 +00005537
Justin Holewinskif9329ff2013-11-20 20:35:34 +00005538 return (Ty->isPromotableIntegerType() ?
5539 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005540}
5541
Justin Holewinski83e96682012-05-24 17:43:12 +00005542void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00005543 if (!getCXXABI().classifyReturnType(FI))
5544 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00005545 for (auto &I : FI.arguments())
5546 I.info = classifyArgumentType(I.type);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005547
5548 // Always honor user-specified calling convention.
5549 if (FI.getCallingConvention() != llvm::CallingConv::C)
5550 return;
5551
John McCall882987f2013-02-28 19:01:20 +00005552 FI.setEffectiveCallingConvention(getRuntimeCC());
5553}
5554
John McCall7f416cc2015-09-08 08:05:57 +00005555Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5556 QualType Ty) const {
Justin Holewinski83e96682012-05-24 17:43:12 +00005557 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005558}
5559
Justin Holewinski83e96682012-05-24 17:43:12 +00005560void NVPTXTargetCodeGenInfo::
Eric Christopher162c91c2015-06-05 22:03:00 +00005561setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Justin Holewinski83e96682012-05-24 17:43:12 +00005562 CodeGen::CodeGenModule &M) const{
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00005563 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Justin Holewinski38031972011-10-05 17:58:44 +00005564 if (!FD) return;
5565
5566 llvm::Function *F = cast<llvm::Function>(GV);
5567
5568 // Perform special handling in OpenCL mode
David Blaikiebbafb8a2012-03-11 07:00:24 +00005569 if (M.getLangOpts().OpenCL) {
Justin Holewinski36837432013-03-30 14:38:24 +00005570 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski38031972011-10-05 17:58:44 +00005571 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00005572 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinski36837432013-03-30 14:38:24 +00005573 // OpenCL __kernel functions get kernel metadata
Eli Benderskye06a2c42014-04-15 16:57:05 +00005574 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5575 addNVVMMetadata(F, "kernel", 1);
Justin Holewinski38031972011-10-05 17:58:44 +00005576 // And kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00005577 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski38031972011-10-05 17:58:44 +00005578 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00005579 }
Justin Holewinski38031972011-10-05 17:58:44 +00005580
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00005581 // Perform special handling in CUDA mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005582 if (M.getLangOpts().CUDA) {
Justin Holewinski36837432013-03-30 14:38:24 +00005583 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00005584 // __global__ functions cannot be called from the device, we do not
5585 // need to set the noinline attribute.
Eli Benderskye06a2c42014-04-15 16:57:05 +00005586 if (FD->hasAttr<CUDAGlobalAttr>()) {
5587 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5588 addNVVMMetadata(F, "kernel", 1);
5589 }
Artem Belevich7093e402015-04-21 22:55:54 +00005590 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
Eli Benderskye06a2c42014-04-15 16:57:05 +00005591 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
Artem Belevich7093e402015-04-21 22:55:54 +00005592 llvm::APSInt MaxThreads(32);
5593 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
5594 if (MaxThreads > 0)
5595 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
5596
5597 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
5598 // not specified in __launch_bounds__ or if the user specified a 0 value,
5599 // we don't have to add a PTX directive.
5600 if (Attr->getMinBlocks()) {
5601 llvm::APSInt MinBlocks(32);
5602 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
5603 if (MinBlocks > 0)
5604 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
5605 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
Eli Benderskye06a2c42014-04-15 16:57:05 +00005606 }
5607 }
Justin Holewinski38031972011-10-05 17:58:44 +00005608 }
5609}
5610
Eli Benderskye06a2c42014-04-15 16:57:05 +00005611void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
5612 int Operand) {
Justin Holewinski36837432013-03-30 14:38:24 +00005613 llvm::Module *M = F->getParent();
5614 llvm::LLVMContext &Ctx = M->getContext();
5615
5616 // Get "nvvm.annotations" metadata node
5617 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
5618
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00005619 llvm::Metadata *MDVals[] = {
5620 llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
5621 llvm::ConstantAsMetadata::get(
5622 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
Justin Holewinski36837432013-03-30 14:38:24 +00005623 // Append metadata to nvvm.annotations
5624 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
5625}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005626}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00005627
5628//===----------------------------------------------------------------------===//
Ulrich Weigand47445072013-05-06 16:26:41 +00005629// SystemZ ABI Implementation
5630//===----------------------------------------------------------------------===//
5631
5632namespace {
5633
5634class SystemZABIInfo : public ABIInfo {
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005635 bool HasVector;
5636
Ulrich Weigand47445072013-05-06 16:26:41 +00005637public:
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005638 SystemZABIInfo(CodeGenTypes &CGT, bool HV)
5639 : ABIInfo(CGT), HasVector(HV) {}
Ulrich Weigand47445072013-05-06 16:26:41 +00005640
5641 bool isPromotableIntegerType(QualType Ty) const;
5642 bool isCompoundType(QualType Ty) const;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005643 bool isVectorArgumentType(QualType Ty) const;
Ulrich Weigand47445072013-05-06 16:26:41 +00005644 bool isFPArgumentType(QualType Ty) const;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005645 QualType GetSingleElementType(QualType Ty) const;
Ulrich Weigand47445072013-05-06 16:26:41 +00005646
5647 ABIArgInfo classifyReturnType(QualType RetTy) const;
5648 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
5649
Craig Topper4f12f102014-03-12 06:41:41 +00005650 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00005651 if (!getCXXABI().classifyReturnType(FI))
5652 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00005653 for (auto &I : FI.arguments())
5654 I.info = classifyArgumentType(I.type);
Ulrich Weigand47445072013-05-06 16:26:41 +00005655 }
5656
John McCall7f416cc2015-09-08 08:05:57 +00005657 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5658 QualType Ty) const override;
Ulrich Weigand47445072013-05-06 16:26:41 +00005659};
5660
5661class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
5662public:
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005663 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
5664 : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
Ulrich Weigand47445072013-05-06 16:26:41 +00005665};
5666
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005667}
Ulrich Weigand47445072013-05-06 16:26:41 +00005668
5669bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
5670 // Treat an enum type as its underlying type.
5671 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5672 Ty = EnumTy->getDecl()->getIntegerType();
5673
5674 // Promotable integer types are required to be promoted by the ABI.
5675 if (Ty->isPromotableIntegerType())
5676 return true;
5677
5678 // 32-bit values must also be promoted.
5679 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5680 switch (BT->getKind()) {
5681 case BuiltinType::Int:
5682 case BuiltinType::UInt:
5683 return true;
5684 default:
5685 return false;
5686 }
5687 return false;
5688}
5689
5690bool SystemZABIInfo::isCompoundType(QualType Ty) const {
Ulrich Weigand759449c2015-03-30 13:49:01 +00005691 return (Ty->isAnyComplexType() ||
5692 Ty->isVectorType() ||
5693 isAggregateTypeForABI(Ty));
Ulrich Weigand47445072013-05-06 16:26:41 +00005694}
5695
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005696bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
5697 return (HasVector &&
5698 Ty->isVectorType() &&
5699 getContext().getTypeSize(Ty) <= 128);
5700}
5701
Ulrich Weigand47445072013-05-06 16:26:41 +00005702bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
5703 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5704 switch (BT->getKind()) {
5705 case BuiltinType::Float:
5706 case BuiltinType::Double:
5707 return true;
5708 default:
5709 return false;
5710 }
5711
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005712 return false;
5713}
5714
5715QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
Ulrich Weigand47445072013-05-06 16:26:41 +00005716 if (const RecordType *RT = Ty->getAsStructureType()) {
5717 const RecordDecl *RD = RT->getDecl();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005718 QualType Found;
Ulrich Weigand47445072013-05-06 16:26:41 +00005719
5720 // If this is a C++ record, check the bases first.
5721 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +00005722 for (const auto &I : CXXRD->bases()) {
5723 QualType Base = I.getType();
Ulrich Weigand47445072013-05-06 16:26:41 +00005724
5725 // Empty bases don't affect things either way.
5726 if (isEmptyRecord(getContext(), Base, true))
5727 continue;
5728
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005729 if (!Found.isNull())
5730 return Ty;
5731 Found = GetSingleElementType(Base);
Ulrich Weigand47445072013-05-06 16:26:41 +00005732 }
5733
5734 // Check the fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00005735 for (const auto *FD : RD->fields()) {
Ulrich Weigand759449c2015-03-30 13:49:01 +00005736 // For compatibility with GCC, ignore empty bitfields in C++ mode.
Ulrich Weigand47445072013-05-06 16:26:41 +00005737 // Unlike isSingleElementStruct(), empty structure and array fields
5738 // do count. So do anonymous bitfields that aren't zero-sized.
Ulrich Weigand759449c2015-03-30 13:49:01 +00005739 if (getContext().getLangOpts().CPlusPlus &&
5740 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
5741 continue;
Ulrich Weigand47445072013-05-06 16:26:41 +00005742
5743 // Unlike isSingleElementStruct(), arrays do not count.
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005744 // Nested structures still do though.
5745 if (!Found.isNull())
5746 return Ty;
5747 Found = GetSingleElementType(FD->getType());
Ulrich Weigand47445072013-05-06 16:26:41 +00005748 }
5749
5750 // Unlike isSingleElementStruct(), trailing padding is allowed.
5751 // An 8-byte aligned struct s { float f; } is passed as a double.
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005752 if (!Found.isNull())
5753 return Found;
Ulrich Weigand47445072013-05-06 16:26:41 +00005754 }
5755
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005756 return Ty;
Ulrich Weigand47445072013-05-06 16:26:41 +00005757}
5758
John McCall7f416cc2015-09-08 08:05:57 +00005759Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5760 QualType Ty) const {
Ulrich Weigand47445072013-05-06 16:26:41 +00005761 // Assume that va_list type is correct; should be pointer to LLVM type:
5762 // struct {
5763 // i64 __gpr;
5764 // i64 __fpr;
5765 // i8 *__overflow_arg_area;
5766 // i8 *__reg_save_area;
5767 // };
5768
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005769 // Every non-vector argument occupies 8 bytes and is passed by preference
5770 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
5771 // always passed on the stack.
John McCall7f416cc2015-09-08 08:05:57 +00005772 Ty = getContext().getCanonicalType(Ty);
5773 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Ulrich Weigand759449c2015-03-30 13:49:01 +00005774 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00005775 llvm::Type *DirectTy = ArgTy;
Ulrich Weigand47445072013-05-06 16:26:41 +00005776 ABIArgInfo AI = classifyArgumentType(Ty);
Ulrich Weigand47445072013-05-06 16:26:41 +00005777 bool IsIndirect = AI.isIndirect();
Ulrich Weigand759449c2015-03-30 13:49:01 +00005778 bool InFPRs = false;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005779 bool IsVector = false;
John McCall7f416cc2015-09-08 08:05:57 +00005780 CharUnits UnpaddedSize;
5781 CharUnits DirectAlign;
Ulrich Weigand47445072013-05-06 16:26:41 +00005782 if (IsIndirect) {
John McCall7f416cc2015-09-08 08:05:57 +00005783 DirectTy = llvm::PointerType::getUnqual(DirectTy);
5784 UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
Ulrich Weigand759449c2015-03-30 13:49:01 +00005785 } else {
5786 if (AI.getCoerceToType())
5787 ArgTy = AI.getCoerceToType();
5788 InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005789 IsVector = ArgTy->isVectorTy();
John McCall7f416cc2015-09-08 08:05:57 +00005790 UnpaddedSize = TyInfo.first;
5791 DirectAlign = TyInfo.second;
Ulrich Weigand759449c2015-03-30 13:49:01 +00005792 }
John McCall7f416cc2015-09-08 08:05:57 +00005793 CharUnits PaddedSize = CharUnits::fromQuantity(8);
5794 if (IsVector && UnpaddedSize > PaddedSize)
5795 PaddedSize = CharUnits::fromQuantity(16);
5796 assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
Ulrich Weigand47445072013-05-06 16:26:41 +00005797
John McCall7f416cc2015-09-08 08:05:57 +00005798 CharUnits Padding = (PaddedSize - UnpaddedSize);
Ulrich Weigand47445072013-05-06 16:26:41 +00005799
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005800 llvm::Type *IndexTy = CGF.Int64Ty;
John McCall7f416cc2015-09-08 08:05:57 +00005801 llvm::Value *PaddedSizeV =
5802 llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005803
5804 if (IsVector) {
5805 // Work out the address of a vector argument on the stack.
5806 // Vector arguments are always passed in the high bits of a
5807 // single (8 byte) or double (16 byte) stack slot.
John McCall7f416cc2015-09-08 08:05:57 +00005808 Address OverflowArgAreaPtr =
5809 CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16),
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005810 "overflow_arg_area_ptr");
John McCall7f416cc2015-09-08 08:05:57 +00005811 Address OverflowArgArea =
5812 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
5813 TyInfo.second);
5814 Address MemAddr =
5815 CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005816
5817 // Update overflow_arg_area_ptr pointer
5818 llvm::Value *NewOverflowArgArea =
John McCall7f416cc2015-09-08 08:05:57 +00005819 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
5820 "overflow_arg_area");
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005821 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
5822
5823 return MemAddr;
5824 }
5825
John McCall7f416cc2015-09-08 08:05:57 +00005826 assert(PaddedSize.getQuantity() == 8);
5827
5828 unsigned MaxRegs, RegCountField, RegSaveIndex;
5829 CharUnits RegPadding;
Ulrich Weigand47445072013-05-06 16:26:41 +00005830 if (InFPRs) {
5831 MaxRegs = 4; // Maximum of 4 FPR arguments
5832 RegCountField = 1; // __fpr
5833 RegSaveIndex = 16; // save offset for f0
John McCall7f416cc2015-09-08 08:05:57 +00005834 RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
Ulrich Weigand47445072013-05-06 16:26:41 +00005835 } else {
5836 MaxRegs = 5; // Maximum of 5 GPR arguments
5837 RegCountField = 0; // __gpr
5838 RegSaveIndex = 2; // save offset for r2
5839 RegPadding = Padding; // values are passed in the low bits of a GPR
5840 }
5841
John McCall7f416cc2015-09-08 08:05:57 +00005842 Address RegCountPtr = CGF.Builder.CreateStructGEP(
5843 VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8),
5844 "reg_count_ptr");
Ulrich Weigand47445072013-05-06 16:26:41 +00005845 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
Ulrich Weigand47445072013-05-06 16:26:41 +00005846 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
5847 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
Oliver Stannard405bded2014-02-11 09:25:50 +00005848 "fits_in_regs");
Ulrich Weigand47445072013-05-06 16:26:41 +00005849
5850 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5851 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
5852 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
5853 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
5854
5855 // Emit code to load the value if it was passed in registers.
5856 CGF.EmitBlock(InRegBlock);
5857
5858 // Work out the address of an argument register.
Ulrich Weigand47445072013-05-06 16:26:41 +00005859 llvm::Value *ScaledRegCount =
5860 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
5861 llvm::Value *RegBase =
John McCall7f416cc2015-09-08 08:05:57 +00005862 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
5863 + RegPadding.getQuantity());
Ulrich Weigand47445072013-05-06 16:26:41 +00005864 llvm::Value *RegOffset =
5865 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
John McCall7f416cc2015-09-08 08:05:57 +00005866 Address RegSaveAreaPtr =
5867 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
5868 "reg_save_area_ptr");
Ulrich Weigand47445072013-05-06 16:26:41 +00005869 llvm::Value *RegSaveArea =
5870 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
John McCall7f416cc2015-09-08 08:05:57 +00005871 Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
5872 "raw_reg_addr"),
5873 PaddedSize);
5874 Address RegAddr =
5875 CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00005876
5877 // Update the register count
5878 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
5879 llvm::Value *NewRegCount =
5880 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
5881 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
5882 CGF.EmitBranch(ContBlock);
5883
5884 // Emit code to load the value if it was passed in memory.
5885 CGF.EmitBlock(InMemBlock);
5886
5887 // Work out the address of a stack argument.
John McCall7f416cc2015-09-08 08:05:57 +00005888 Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
5889 VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr");
5890 Address OverflowArgArea =
5891 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
5892 PaddedSize);
5893 Address RawMemAddr =
5894 CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
5895 Address MemAddr =
5896 CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00005897
5898 // Update overflow_arg_area_ptr pointer
5899 llvm::Value *NewOverflowArgArea =
John McCall7f416cc2015-09-08 08:05:57 +00005900 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
5901 "overflow_arg_area");
Ulrich Weigand47445072013-05-06 16:26:41 +00005902 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
5903 CGF.EmitBranch(ContBlock);
5904
5905 // Return the appropriate result.
5906 CGF.EmitBlock(ContBlock);
John McCall7f416cc2015-09-08 08:05:57 +00005907 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
5908 MemAddr, InMemBlock, "va_arg.addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00005909
5910 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00005911 ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
5912 TyInfo.second);
Ulrich Weigand47445072013-05-06 16:26:41 +00005913
5914 return ResAddr;
5915}
5916
Ulrich Weigand47445072013-05-06 16:26:41 +00005917ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
5918 if (RetTy->isVoidType())
5919 return ABIArgInfo::getIgnore();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005920 if (isVectorArgumentType(RetTy))
5921 return ABIArgInfo::getDirect();
Ulrich Weigand47445072013-05-06 16:26:41 +00005922 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
John McCall7f416cc2015-09-08 08:05:57 +00005923 return getNaturalAlignIndirect(RetTy);
Ulrich Weigand47445072013-05-06 16:26:41 +00005924 return (isPromotableIntegerType(RetTy) ?
5925 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5926}
5927
5928ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
5929 // Handle the generic C++ ABI.
Mark Lacey3825e832013-10-06 01:33:34 +00005930 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00005931 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand47445072013-05-06 16:26:41 +00005932
5933 // Integers and enums are extended to full register width.
5934 if (isPromotableIntegerType(Ty))
5935 return ABIArgInfo::getExtend();
5936
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005937 // Handle vector types and vector-like structure types. Note that
5938 // as opposed to float-like structure types, we do not allow any
5939 // padding for vector-like structures, so verify the sizes match.
Ulrich Weigand47445072013-05-06 16:26:41 +00005940 uint64_t Size = getContext().getTypeSize(Ty);
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005941 QualType SingleElementTy = GetSingleElementType(Ty);
5942 if (isVectorArgumentType(SingleElementTy) &&
5943 getContext().getTypeSize(SingleElementTy) == Size)
5944 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
5945
5946 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
Ulrich Weigand47445072013-05-06 16:26:41 +00005947 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
John McCall7f416cc2015-09-08 08:05:57 +00005948 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00005949
5950 // Handle small structures.
5951 if (const RecordType *RT = Ty->getAs<RecordType>()) {
5952 // Structures with flexible arrays have variable length, so really
5953 // fail the size test above.
5954 const RecordDecl *RD = RT->getDecl();
5955 if (RD->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00005956 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00005957
5958 // The structure is passed as an unextended integer, a float, or a double.
5959 llvm::Type *PassTy;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00005960 if (isFPArgumentType(SingleElementTy)) {
Ulrich Weigand47445072013-05-06 16:26:41 +00005961 assert(Size == 32 || Size == 64);
5962 if (Size == 32)
5963 PassTy = llvm::Type::getFloatTy(getVMContext());
5964 else
5965 PassTy = llvm::Type::getDoubleTy(getVMContext());
5966 } else
5967 PassTy = llvm::IntegerType::get(getVMContext(), Size);
5968 return ABIArgInfo::getDirect(PassTy);
5969 }
5970
5971 // Non-structure compounds are passed indirectly.
5972 if (isCompoundType(Ty))
John McCall7f416cc2015-09-08 08:05:57 +00005973 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00005974
Craig Topper8a13c412014-05-21 05:09:00 +00005975 return ABIArgInfo::getDirect(nullptr);
Ulrich Weigand47445072013-05-06 16:26:41 +00005976}
5977
5978//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005979// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00005980//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005981
5982namespace {
5983
5984class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
5985public:
Chris Lattner2b037972010-07-29 02:01:43 +00005986 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
5987 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Eric Christopher162c91c2015-06-05 22:03:00 +00005988 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00005989 CodeGen::CodeGenModule &M) const override;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005990};
5991
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005992}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005993
Eric Christopher162c91c2015-06-05 22:03:00 +00005994void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005995 llvm::GlobalValue *GV,
5996 CodeGen::CodeGenModule &M) const {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00005997 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005998 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
5999 // Handle 'interrupt' attribute:
6000 llvm::Function *F = cast<llvm::Function>(GV);
6001
6002 // Step 1: Set ISR calling convention.
6003 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
6004
6005 // Step 2: Add attributes goodness.
Bill Wendling207f0532012-12-20 19:27:06 +00006006 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006007
6008 // Step 3: Emit ISR vector alias.
Anton Korobeynikovc5a7f922012-11-26 18:59:10 +00006009 unsigned Num = attr->getNumber() / 2;
Rafael Espindola234405b2014-05-17 21:30:14 +00006010 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
6011 "__isr_" + Twine(Num), F);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006012 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006013 }
6014}
6015
Chris Lattner0cf24192010-06-28 20:05:43 +00006016//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00006017// MIPS ABI Implementation. This works for both little-endian and
6018// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00006019//===----------------------------------------------------------------------===//
6020
John McCall943fae92010-05-27 06:19:26 +00006021namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00006022class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00006023 bool IsO32;
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006024 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
6025 void CoerceToIntArgs(uint64_t TySize,
Craig Topper5603df42013-07-05 19:34:19 +00006026 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006027 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006028 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006029 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006030public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006031 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006032 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006033 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00006034
6035 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006036 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Craig Topper4f12f102014-03-12 06:41:41 +00006037 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00006038 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6039 QualType Ty) const override;
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00006040 bool shouldSignExtUnsignedType(QualType Ty) const override;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006041};
6042
John McCall943fae92010-05-27 06:19:26 +00006043class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00006044 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00006045public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006046 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
6047 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
Akira Hatanaka14378522011-11-02 23:14:57 +00006048 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00006049
Craig Topper4f12f102014-03-12 06:41:41 +00006050 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCall943fae92010-05-27 06:19:26 +00006051 return 29;
6052 }
6053
Eric Christopher162c91c2015-06-05 22:03:00 +00006054 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00006055 CodeGen::CodeGenModule &CGM) const override {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006056 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Reed Kotler3d5966f2013-03-13 20:40:30 +00006057 if (!FD) return;
Rafael Espindolaa0851a22013-03-19 14:32:23 +00006058 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotler3d5966f2013-03-13 20:40:30 +00006059 if (FD->hasAttr<Mips16Attr>()) {
6060 Fn->addFnAttr("mips16");
6061 }
6062 else if (FD->hasAttr<NoMips16Attr>()) {
6063 Fn->addFnAttr("nomips16");
6064 }
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006065
6066 const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
6067 if (!Attr)
6068 return;
6069
6070 const char *Kind;
6071 switch (Attr->getInterrupt()) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006072 case MipsInterruptAttr::eic: Kind = "eic"; break;
6073 case MipsInterruptAttr::sw0: Kind = "sw0"; break;
6074 case MipsInterruptAttr::sw1: Kind = "sw1"; break;
6075 case MipsInterruptAttr::hw0: Kind = "hw0"; break;
6076 case MipsInterruptAttr::hw1: Kind = "hw1"; break;
6077 case MipsInterruptAttr::hw2: Kind = "hw2"; break;
6078 case MipsInterruptAttr::hw3: Kind = "hw3"; break;
6079 case MipsInterruptAttr::hw4: Kind = "hw4"; break;
6080 case MipsInterruptAttr::hw5: Kind = "hw5"; break;
6081 }
6082
6083 Fn->addFnAttr("interrupt", Kind);
6084
Reed Kotler373feca2013-01-16 17:10:28 +00006085 }
Reed Kotler3d5966f2013-03-13 20:40:30 +00006086
John McCall943fae92010-05-27 06:19:26 +00006087 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00006088 llvm::Value *Address) const override;
John McCall3480ef22011-08-30 01:42:09 +00006089
Craig Topper4f12f102014-03-12 06:41:41 +00006090 unsigned getSizeOfUnwindException() const override {
Akira Hatanaka0486db02011-09-20 18:23:28 +00006091 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00006092 }
John McCall943fae92010-05-27 06:19:26 +00006093};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006094}
John McCall943fae92010-05-27 06:19:26 +00006095
Eric Christopher7565e0d2015-05-29 23:09:49 +00006096void MipsABIInfo::CoerceToIntArgs(
6097 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006098 llvm::IntegerType *IntTy =
6099 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006100
6101 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
6102 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
6103 ArgList.push_back(IntTy);
6104
6105 // If necessary, add one more integer type to ArgList.
6106 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
6107
6108 if (R)
6109 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006110}
6111
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006112// In N32/64, an aligned double precision floating point field is passed in
6113// a register.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006114llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006115 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
6116
6117 if (IsO32) {
6118 CoerceToIntArgs(TySize, ArgList);
6119 return llvm::StructType::get(getVMContext(), ArgList);
6120 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006121
Akira Hatanaka02e13e52012-01-12 00:52:17 +00006122 if (Ty->isComplexType())
6123 return CGT.ConvertType(Ty);
Akira Hatanaka79f04612012-01-10 23:12:19 +00006124
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00006125 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006126
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006127 // Unions/vectors are passed in integer registers.
6128 if (!RT || !RT->isStructureOrClassType()) {
6129 CoerceToIntArgs(TySize, ArgList);
6130 return llvm::StructType::get(getVMContext(), ArgList);
6131 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006132
6133 const RecordDecl *RD = RT->getDecl();
6134 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006135 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Eric Christopher7565e0d2015-05-29 23:09:49 +00006136
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006137 uint64_t LastOffset = 0;
6138 unsigned idx = 0;
6139 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
6140
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00006141 // Iterate over fields in the struct/class and check if there are any aligned
6142 // double fields.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006143 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6144 i != e; ++i, ++idx) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006145 const QualType Ty = i->getType();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006146 const BuiltinType *BT = Ty->getAs<BuiltinType>();
6147
6148 if (!BT || BT->getKind() != BuiltinType::Double)
6149 continue;
6150
6151 uint64_t Offset = Layout.getFieldOffset(idx);
6152 if (Offset % 64) // Ignore doubles that are not aligned.
6153 continue;
6154
6155 // Add ((Offset - LastOffset) / 64) args of type i64.
6156 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
6157 ArgList.push_back(I64);
6158
6159 // Add double type.
6160 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
6161 LastOffset = Offset + 64;
6162 }
6163
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006164 CoerceToIntArgs(TySize - LastOffset, IntArgList);
6165 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006166
6167 return llvm::StructType::get(getVMContext(), ArgList);
6168}
6169
Akira Hatanakaddd66342013-10-29 18:41:15 +00006170llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
6171 uint64_t Offset) const {
6172 if (OrigOffset + MinABIStackAlignInBytes > Offset)
Craig Topper8a13c412014-05-21 05:09:00 +00006173 return nullptr;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006174
Akira Hatanakaddd66342013-10-29 18:41:15 +00006175 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
Akira Hatanaka1632af62012-01-09 19:31:25 +00006176}
Akira Hatanaka21ee88c2012-01-10 22:44:52 +00006177
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006178ABIArgInfo
6179MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Daniel Sanders998c9102015-01-14 12:00:12 +00006180 Ty = useFirstFieldIfTransparentUnion(Ty);
6181
Akira Hatanaka1632af62012-01-09 19:31:25 +00006182 uint64_t OrigOffset = Offset;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006183 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanaka1632af62012-01-09 19:31:25 +00006184 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006185
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006186 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
6187 (uint64_t)StackAlignInBytes);
Rui Ueyama83aa9792016-01-14 21:00:27 +00006188 unsigned CurrOffset = llvm::alignTo(Offset, Align);
6189 Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006190
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006191 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanakab579fe52011-06-02 00:09:17 +00006192 // Ignore empty aggregates.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006193 if (TySize == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00006194 return ABIArgInfo::getIgnore();
6195
Mark Lacey3825e832013-10-06 01:33:34 +00006196 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006197 Offset = OrigOffset + MinABIStackAlignInBytes;
John McCall7f416cc2015-09-08 08:05:57 +00006198 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006199 }
Akira Hatanakadf425db2011-08-01 18:09:58 +00006200
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006201 // If we have reached here, aggregates are passed directly by coercing to
6202 // another structure type. Padding is inserted if the offset of the
6203 // aggregate is unaligned.
Daniel Sandersaa1b3552014-10-24 15:30:16 +00006204 ABIArgInfo ArgInfo =
6205 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
6206 getPaddingType(OrigOffset, CurrOffset));
6207 ArgInfo.setInReg(true);
6208 return ArgInfo;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006209 }
6210
6211 // Treat an enum type as its underlying type.
6212 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6213 Ty = EnumTy->getDecl()->getIntegerType();
6214
Daniel Sanders5b445b32014-10-24 14:42:42 +00006215 // All integral types are promoted to the GPR width.
6216 if (Ty->isIntegralOrEnumerationType())
Akira Hatanaka1632af62012-01-09 19:31:25 +00006217 return ABIArgInfo::getExtend();
6218
Akira Hatanakaddd66342013-10-29 18:41:15 +00006219 return ABIArgInfo::getDirect(
Craig Topper8a13c412014-05-21 05:09:00 +00006220 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00006221}
6222
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006223llvm::Type*
6224MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakab6f74432012-02-09 18:49:26 +00006225 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006226 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006227
Akira Hatanakab6f74432012-02-09 18:49:26 +00006228 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006229 const RecordDecl *RD = RT->getDecl();
Akira Hatanakab6f74432012-02-09 18:49:26 +00006230 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6231 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006232
Akira Hatanakab6f74432012-02-09 18:49:26 +00006233 // N32/64 returns struct/classes in floating point registers if the
6234 // following conditions are met:
6235 // 1. The size of the struct/class is no larger than 128-bit.
6236 // 2. The struct/class has one or two fields all of which are floating
6237 // point types.
Eric Christopher7565e0d2015-05-29 23:09:49 +00006238 // 3. The offset of the first field is zero (this follows what gcc does).
Akira Hatanakab6f74432012-02-09 18:49:26 +00006239 //
6240 // Any other composite results are returned in integer registers.
6241 //
6242 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
6243 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
6244 for (; b != e; ++b) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006245 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006246
Akira Hatanakab6f74432012-02-09 18:49:26 +00006247 if (!BT || !BT->isFloatingPoint())
6248 break;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006249
David Blaikie2d7c57e2012-04-30 02:36:29 +00006250 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakab6f74432012-02-09 18:49:26 +00006251 }
6252
6253 if (b == e)
6254 return llvm::StructType::get(getVMContext(), RTList,
6255 RD->hasAttr<PackedAttr>());
6256
6257 RTList.clear();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006258 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006259 }
6260
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006261 CoerceToIntArgs(Size, RTList);
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006262 return llvm::StructType::get(getVMContext(), RTList);
6263}
6264
Akira Hatanakab579fe52011-06-02 00:09:17 +00006265ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanaka60f5fe62012-01-23 23:18:57 +00006266 uint64_t Size = getContext().getTypeSize(RetTy);
6267
Daniel Sandersed39f582014-09-04 13:28:14 +00006268 if (RetTy->isVoidType())
6269 return ABIArgInfo::getIgnore();
6270
6271 // O32 doesn't treat zero-sized structs differently from other structs.
6272 // However, N32/N64 ignores zero sized return values.
6273 if (!IsO32 && Size == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00006274 return ABIArgInfo::getIgnore();
6275
Akira Hatanakac37eddf2012-05-11 21:01:17 +00006276 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006277 if (Size <= 128) {
6278 if (RetTy->isAnyComplexType())
6279 return ABIArgInfo::getDirect();
6280
Daniel Sanderse5018b62014-09-04 15:05:39 +00006281 // O32 returns integer vectors in registers and N32/N64 returns all small
Daniel Sanders00a56ff2014-09-04 15:07:43 +00006282 // aggregates in registers.
Daniel Sanderse5018b62014-09-04 15:05:39 +00006283 if (!IsO32 ||
6284 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
6285 ABIArgInfo ArgInfo =
6286 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
6287 ArgInfo.setInReg(true);
6288 return ArgInfo;
6289 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006290 }
Akira Hatanakab579fe52011-06-02 00:09:17 +00006291
John McCall7f416cc2015-09-08 08:05:57 +00006292 return getNaturalAlignIndirect(RetTy);
Akira Hatanakab579fe52011-06-02 00:09:17 +00006293 }
6294
6295 // Treat an enum type as its underlying type.
6296 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6297 RetTy = EnumTy->getDecl()->getIntegerType();
6298
6299 return (RetTy->isPromotableIntegerType() ?
6300 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6301}
6302
6303void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanaka32604a92012-01-12 01:10:09 +00006304 ABIArgInfo &RetInfo = FI.getReturnInfo();
Reid Kleckner40ca9132014-05-13 22:05:45 +00006305 if (!getCXXABI().classifyReturnType(FI))
6306 RetInfo = classifyReturnType(FI.getReturnType());
Akira Hatanaka32604a92012-01-12 01:10:09 +00006307
Eric Christopher7565e0d2015-05-29 23:09:49 +00006308 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006309 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanaka32604a92012-01-12 01:10:09 +00006310
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006311 for (auto &I : FI.arguments())
6312 I.info = classifyArgumentType(I.type, Offset);
Akira Hatanakab579fe52011-06-02 00:09:17 +00006313}
6314
John McCall7f416cc2015-09-08 08:05:57 +00006315Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6316 QualType OrigTy) const {
6317 QualType Ty = OrigTy;
Daniel Sanders59229dc2014-11-19 10:01:35 +00006318
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006319 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
6320 // Pointers are also promoted in the same way but this only matters for N32.
Daniel Sanders59229dc2014-11-19 10:01:35 +00006321 unsigned SlotSizeInBits = IsO32 ? 32 : 64;
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006322 unsigned PtrWidth = getTarget().getPointerWidth(0);
John McCall7f416cc2015-09-08 08:05:57 +00006323 bool DidPromote = false;
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006324 if ((Ty->isIntegerType() &&
John McCall7f416cc2015-09-08 08:05:57 +00006325 getContext().getIntWidth(Ty) < SlotSizeInBits) ||
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006326 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
John McCall7f416cc2015-09-08 08:05:57 +00006327 DidPromote = true;
6328 Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
6329 Ty->isSignedIntegerType());
Daniel Sanders59229dc2014-11-19 10:01:35 +00006330 }
Eric Christopher7565e0d2015-05-29 23:09:49 +00006331
John McCall7f416cc2015-09-08 08:05:57 +00006332 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00006333
John McCall7f416cc2015-09-08 08:05:57 +00006334 // The alignment of things in the argument area is never larger than
6335 // StackAlignInBytes.
6336 TyInfo.second =
6337 std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
6338
6339 // MinABIStackAlignInBytes is the size of argument slots on the stack.
6340 CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
6341
6342 Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6343 TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
6344
6345
6346 // If there was a promotion, "unpromote" into a temporary.
6347 // TODO: can we just use a pointer into a subset of the original slot?
6348 if (DidPromote) {
6349 Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
6350 llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
6351
6352 // Truncate down to the right width.
6353 llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
6354 : CGF.IntPtrTy);
6355 llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
6356 if (OrigTy->isPointerType())
6357 V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
6358
6359 CGF.Builder.CreateStore(V, Temp);
6360 Addr = Temp;
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00006361 }
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00006362
John McCall7f416cc2015-09-08 08:05:57 +00006363 return Addr;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006364}
6365
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00006366bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
6367 int TySize = getContext().getTypeSize(Ty);
Eric Christopher7565e0d2015-05-29 23:09:49 +00006368
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00006369 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
6370 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
6371 return true;
Eric Christopher7565e0d2015-05-29 23:09:49 +00006372
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00006373 return false;
6374}
6375
John McCall943fae92010-05-27 06:19:26 +00006376bool
6377MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6378 llvm::Value *Address) const {
6379 // This information comes from gcc's implementation, which seems to
6380 // as canonical as it gets.
6381
John McCall943fae92010-05-27 06:19:26 +00006382 // Everything on MIPS is 4 bytes. Double-precision FP registers
6383 // are aliased to pairs of single-precision FP registers.
Chris Lattnerece04092012-02-07 00:39:47 +00006384 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCall943fae92010-05-27 06:19:26 +00006385
6386 // 0-31 are the general purpose registers, $0 - $31.
6387 // 32-63 are the floating-point registers, $f0 - $f31.
6388 // 64 and 65 are the multiply/divide registers, $hi and $lo.
6389 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattnerece04092012-02-07 00:39:47 +00006390 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCall943fae92010-05-27 06:19:26 +00006391
6392 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
6393 // They are one bit wide and ignored here.
6394
6395 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
6396 // (coprocessor 1 is the FP unit)
6397 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
6398 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
6399 // 176-181 are the DSP accumulator registers.
Chris Lattnerece04092012-02-07 00:39:47 +00006400 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCall943fae92010-05-27 06:19:26 +00006401 return false;
6402}
6403
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006404//===----------------------------------------------------------------------===//
6405// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
Eric Christopher7565e0d2015-05-29 23:09:49 +00006406// Currently subclassed only to implement custom OpenCL C function attribute
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006407// handling.
6408//===----------------------------------------------------------------------===//
6409
6410namespace {
6411
6412class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
6413public:
6414 TCETargetCodeGenInfo(CodeGenTypes &CGT)
6415 : DefaultTargetCodeGenInfo(CGT) {}
6416
Eric Christopher162c91c2015-06-05 22:03:00 +00006417 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Craig Topper4f12f102014-03-12 06:41:41 +00006418 CodeGen::CodeGenModule &M) const override;
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006419};
6420
Eric Christopher162c91c2015-06-05 22:03:00 +00006421void TCETargetCodeGenInfo::setTargetAttributes(
Eric Christopher7565e0d2015-05-29 23:09:49 +00006422 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006423 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006424 if (!FD) return;
6425
6426 llvm::Function *F = cast<llvm::Function>(GV);
Eric Christopher7565e0d2015-05-29 23:09:49 +00006427
David Blaikiebbafb8a2012-03-11 07:00:24 +00006428 if (M.getLangOpts().OpenCL) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006429 if (FD->hasAttr<OpenCLKernelAttr>()) {
6430 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00006431 F->addFnAttr(llvm::Attribute::NoInline);
Aaron Ballman36a18ff2013-12-19 13:16:35 +00006432 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
6433 if (Attr) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006434 // Convert the reqd_work_group_size() attributes to metadata.
6435 llvm::LLVMContext &Context = F->getContext();
Eric Christopher7565e0d2015-05-29 23:09:49 +00006436 llvm::NamedMDNode *OpenCLMetadata =
6437 M.getModule().getOrInsertNamedMetadata(
6438 "opencl.kernel_wg_size_info");
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006439
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00006440 SmallVector<llvm::Metadata *, 5> Operands;
6441 Operands.push_back(llvm::ConstantAsMetadata::get(F));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006442
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00006443 Operands.push_back(
6444 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6445 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
6446 Operands.push_back(
6447 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6448 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
6449 Operands.push_back(
6450 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6451 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006452
Eric Christopher7565e0d2015-05-29 23:09:49 +00006453 // Add a boolean constant operand for "required" (true) or "hint"
6454 // (false) for implementing the work_group_size_hint attr later.
6455 // Currently always true as the hint is not yet implemented.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00006456 Operands.push_back(
6457 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00006458 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
6459 }
6460 }
6461 }
6462}
6463
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006464}
John McCall943fae92010-05-27 06:19:26 +00006465
Tony Linthicum76329bf2011-12-12 21:14:55 +00006466//===----------------------------------------------------------------------===//
6467// Hexagon ABI Implementation
6468//===----------------------------------------------------------------------===//
6469
6470namespace {
6471
6472class HexagonABIInfo : public ABIInfo {
6473
6474
6475public:
6476 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6477
6478private:
6479
6480 ABIArgInfo classifyReturnType(QualType RetTy) const;
6481 ABIArgInfo classifyArgumentType(QualType RetTy) const;
6482
Craig Topper4f12f102014-03-12 06:41:41 +00006483 void computeInfo(CGFunctionInfo &FI) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00006484
John McCall7f416cc2015-09-08 08:05:57 +00006485 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6486 QualType Ty) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00006487};
6488
6489class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
6490public:
6491 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
6492 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
6493
Craig Topper4f12f102014-03-12 06:41:41 +00006494 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Tony Linthicum76329bf2011-12-12 21:14:55 +00006495 return 29;
6496 }
6497};
6498
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006499}
Tony Linthicum76329bf2011-12-12 21:14:55 +00006500
6501void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00006502 if (!getCXXABI().classifyReturnType(FI))
6503 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006504 for (auto &I : FI.arguments())
6505 I.info = classifyArgumentType(I.type);
Tony Linthicum76329bf2011-12-12 21:14:55 +00006506}
6507
6508ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
6509 if (!isAggregateTypeForABI(Ty)) {
6510 // Treat an enum type as its underlying type.
6511 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6512 Ty = EnumTy->getDecl()->getIntegerType();
6513
6514 return (Ty->isPromotableIntegerType() ?
6515 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6516 }
6517
6518 // Ignore empty records.
6519 if (isEmptyRecord(getContext(), Ty, true))
6520 return ABIArgInfo::getIgnore();
6521
Mark Lacey3825e832013-10-06 01:33:34 +00006522 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00006523 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum76329bf2011-12-12 21:14:55 +00006524
6525 uint64_t Size = getContext().getTypeSize(Ty);
6526 if (Size > 64)
John McCall7f416cc2015-09-08 08:05:57 +00006527 return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00006528 // Pass in the smallest viable integer type.
6529 else if (Size > 32)
6530 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6531 else if (Size > 16)
6532 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6533 else if (Size > 8)
6534 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6535 else
6536 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6537}
6538
6539ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
6540 if (RetTy->isVoidType())
6541 return ABIArgInfo::getIgnore();
6542
6543 // Large vector types should be returned via memory.
6544 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
John McCall7f416cc2015-09-08 08:05:57 +00006545 return getNaturalAlignIndirect(RetTy);
Tony Linthicum76329bf2011-12-12 21:14:55 +00006546
6547 if (!isAggregateTypeForABI(RetTy)) {
6548 // Treat an enum type as its underlying type.
6549 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6550 RetTy = EnumTy->getDecl()->getIntegerType();
6551
6552 return (RetTy->isPromotableIntegerType() ?
6553 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6554 }
6555
Tony Linthicum76329bf2011-12-12 21:14:55 +00006556 if (isEmptyRecord(getContext(), RetTy, true))
6557 return ABIArgInfo::getIgnore();
6558
6559 // Aggregates <= 8 bytes are returned in r0; other aggregates
6560 // are returned indirectly.
6561 uint64_t Size = getContext().getTypeSize(RetTy);
6562 if (Size <= 64) {
6563 // Return in the smallest viable integer type.
6564 if (Size <= 8)
6565 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6566 if (Size <= 16)
6567 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6568 if (Size <= 32)
6569 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6570 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6571 }
6572
John McCall7f416cc2015-09-08 08:05:57 +00006573 return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00006574}
6575
John McCall7f416cc2015-09-08 08:05:57 +00006576Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6577 QualType Ty) const {
6578 // FIXME: Someone needs to audit that this handle alignment correctly.
6579 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6580 getContext().getTypeInfoInChars(Ty),
6581 CharUnits::fromQuantity(4),
6582 /*AllowHigherAlign*/ true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00006583}
6584
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006585//===----------------------------------------------------------------------===//
6586// AMDGPU ABI Implementation
6587//===----------------------------------------------------------------------===//
6588
6589namespace {
6590
6591class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
6592public:
6593 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
6594 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Eric Christopher162c91c2015-06-05 22:03:00 +00006595 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006596 CodeGen::CodeGenModule &M) const override;
6597};
6598
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006599}
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006600
Eric Christopher162c91c2015-06-05 22:03:00 +00006601void AMDGPUTargetCodeGenInfo::setTargetAttributes(
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006602 const Decl *D,
6603 llvm::GlobalValue *GV,
6604 CodeGen::CodeGenModule &M) const {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006605 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00006606 if (!FD)
6607 return;
6608
6609 if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
6610 llvm::Function *F = cast<llvm::Function>(GV);
6611 uint32_t NumVGPR = Attr->getNumVGPR();
6612 if (NumVGPR != 0)
6613 F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR));
6614 }
6615
6616 if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
6617 llvm::Function *F = cast<llvm::Function>(GV);
6618 unsigned NumSGPR = Attr->getNumSGPR();
6619 if (NumSGPR != 0)
6620 F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR));
6621 }
6622}
6623
Tony Linthicum76329bf2011-12-12 21:14:55 +00006624
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006625//===----------------------------------------------------------------------===//
6626// SPARC v9 ABI Implementation.
6627// Based on the SPARC Compliance Definition version 2.4.1.
6628//
6629// Function arguments a mapped to a nominal "parameter array" and promoted to
6630// registers depending on their type. Each argument occupies 8 or 16 bytes in
6631// the array, structs larger than 16 bytes are passed indirectly.
6632//
6633// One case requires special care:
6634//
6635// struct mixed {
6636// int i;
6637// float f;
6638// };
6639//
6640// When a struct mixed is passed by value, it only occupies 8 bytes in the
6641// parameter array, but the int is passed in an integer register, and the float
6642// is passed in a floating point register. This is represented as two arguments
6643// with the LLVM IR inreg attribute:
6644//
6645// declare void f(i32 inreg %i, float inreg %f)
6646//
6647// The code generator will only allocate 4 bytes from the parameter array for
6648// the inreg arguments. All other arguments are allocated a multiple of 8
6649// bytes.
6650//
6651namespace {
6652class SparcV9ABIInfo : public ABIInfo {
6653public:
6654 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6655
6656private:
6657 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
Craig Topper4f12f102014-03-12 06:41:41 +00006658 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00006659 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6660 QualType Ty) const override;
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00006661
6662 // Coercion type builder for structs passed in registers. The coercion type
6663 // serves two purposes:
6664 //
6665 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
6666 // in registers.
6667 // 2. Expose aligned floating point elements as first-level elements, so the
6668 // code generator knows to pass them in floating point registers.
6669 //
6670 // We also compute the InReg flag which indicates that the struct contains
6671 // aligned 32-bit floats.
6672 //
6673 struct CoerceBuilder {
6674 llvm::LLVMContext &Context;
6675 const llvm::DataLayout &DL;
6676 SmallVector<llvm::Type*, 8> Elems;
6677 uint64_t Size;
6678 bool InReg;
6679
6680 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
6681 : Context(c), DL(dl), Size(0), InReg(false) {}
6682
6683 // Pad Elems with integers until Size is ToSize.
6684 void pad(uint64_t ToSize) {
6685 assert(ToSize >= Size && "Cannot remove elements");
6686 if (ToSize == Size)
6687 return;
6688
6689 // Finish the current 64-bit word.
Rui Ueyama83aa9792016-01-14 21:00:27 +00006690 uint64_t Aligned = llvm::alignTo(Size, 64);
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00006691 if (Aligned > Size && Aligned <= ToSize) {
6692 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
6693 Size = Aligned;
6694 }
6695
6696 // Add whole 64-bit words.
6697 while (Size + 64 <= ToSize) {
6698 Elems.push_back(llvm::Type::getInt64Ty(Context));
6699 Size += 64;
6700 }
6701
6702 // Final in-word padding.
6703 if (Size < ToSize) {
6704 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
6705 Size = ToSize;
6706 }
6707 }
6708
6709 // Add a floating point element at Offset.
6710 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
6711 // Unaligned floats are treated as integers.
6712 if (Offset % Bits)
6713 return;
6714 // The InReg flag is only required if there are any floats < 64 bits.
6715 if (Bits < 64)
6716 InReg = true;
6717 pad(Offset);
6718 Elems.push_back(Ty);
6719 Size = Offset + Bits;
6720 }
6721
6722 // Add a struct type to the coercion type, starting at Offset (in bits).
6723 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
6724 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
6725 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
6726 llvm::Type *ElemTy = StrTy->getElementType(i);
6727 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
6728 switch (ElemTy->getTypeID()) {
6729 case llvm::Type::StructTyID:
6730 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
6731 break;
6732 case llvm::Type::FloatTyID:
6733 addFloat(ElemOffset, ElemTy, 32);
6734 break;
6735 case llvm::Type::DoubleTyID:
6736 addFloat(ElemOffset, ElemTy, 64);
6737 break;
6738 case llvm::Type::FP128TyID:
6739 addFloat(ElemOffset, ElemTy, 128);
6740 break;
6741 case llvm::Type::PointerTyID:
6742 if (ElemOffset % 64 == 0) {
6743 pad(ElemOffset);
6744 Elems.push_back(ElemTy);
6745 Size += 64;
6746 }
6747 break;
6748 default:
6749 break;
6750 }
6751 }
6752 }
6753
6754 // Check if Ty is a usable substitute for the coercion type.
6755 bool isUsableType(llvm::StructType *Ty) const {
Benjamin Kramer39ccabe2015-03-02 11:57:06 +00006756 return llvm::makeArrayRef(Elems) == Ty->elements();
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00006757 }
6758
6759 // Get the coercion type as a literal struct type.
6760 llvm::Type *getType() const {
6761 if (Elems.size() == 1)
6762 return Elems.front();
6763 else
6764 return llvm::StructType::get(Context, Elems);
6765 }
6766 };
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006767};
6768} // end anonymous namespace
6769
6770ABIArgInfo
6771SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
6772 if (Ty->isVoidType())
6773 return ABIArgInfo::getIgnore();
6774
6775 uint64_t Size = getContext().getTypeSize(Ty);
6776
6777 // Anything too big to fit in registers is passed with an explicit indirect
6778 // pointer / sret pointer.
6779 if (Size > SizeLimit)
John McCall7f416cc2015-09-08 08:05:57 +00006780 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006781
6782 // Treat an enum type as its underlying type.
6783 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6784 Ty = EnumTy->getDecl()->getIntegerType();
6785
6786 // Integer types smaller than a register are extended.
6787 if (Size < 64 && Ty->isIntegerType())
6788 return ABIArgInfo::getExtend();
6789
6790 // Other non-aggregates go in registers.
6791 if (!isAggregateTypeForABI(Ty))
6792 return ABIArgInfo::getDirect();
6793
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00006794 // If a C++ object has either a non-trivial copy constructor or a non-trivial
6795 // destructor, it is passed with an explicit indirect pointer / sret pointer.
6796 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00006797 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00006798
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006799 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00006800 // Build a coercion type from the LLVM struct type.
6801 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
6802 if (!StrTy)
6803 return ABIArgInfo::getDirect();
6804
6805 CoerceBuilder CB(getVMContext(), getDataLayout());
6806 CB.addStruct(0, StrTy);
Rui Ueyama83aa9792016-01-14 21:00:27 +00006807 CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00006808
6809 // Try to use the original type for coercion.
6810 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
6811
6812 if (CB.InReg)
6813 return ABIArgInfo::getDirectInReg(CoerceTy);
6814 else
6815 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006816}
6817
John McCall7f416cc2015-09-08 08:05:57 +00006818Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6819 QualType Ty) const {
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006820 ABIArgInfo AI = classifyType(Ty, 16 * 8);
6821 llvm::Type *ArgTy = CGT.ConvertType(Ty);
6822 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
6823 AI.setCoerceToType(ArgTy);
6824
John McCall7f416cc2015-09-08 08:05:57 +00006825 CharUnits SlotSize = CharUnits::fromQuantity(8);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006826
John McCall7f416cc2015-09-08 08:05:57 +00006827 CGBuilderTy &Builder = CGF.Builder;
6828 Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
6829 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
6830
6831 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
6832
6833 Address ArgAddr = Address::invalid();
6834 CharUnits Stride;
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006835 switch (AI.getKind()) {
6836 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00006837 case ABIArgInfo::CoerceAndExpand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00006838 case ABIArgInfo::InAlloca:
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006839 llvm_unreachable("Unsupported ABI kind for va_arg");
6840
John McCall7f416cc2015-09-08 08:05:57 +00006841 case ABIArgInfo::Extend: {
6842 Stride = SlotSize;
6843 CharUnits Offset = SlotSize - TypeInfo.first;
6844 ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006845 break;
John McCall7f416cc2015-09-08 08:05:57 +00006846 }
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006847
John McCall7f416cc2015-09-08 08:05:57 +00006848 case ABIArgInfo::Direct: {
6849 auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
Rui Ueyama83aa9792016-01-14 21:00:27 +00006850 Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006851 ArgAddr = Addr;
6852 break;
John McCall7f416cc2015-09-08 08:05:57 +00006853 }
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006854
6855 case ABIArgInfo::Indirect:
John McCall7f416cc2015-09-08 08:05:57 +00006856 Stride = SlotSize;
6857 ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
6858 ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
6859 TypeInfo.second);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006860 break;
6861
6862 case ABIArgInfo::Ignore:
John McCall7f416cc2015-09-08 08:05:57 +00006863 return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006864 }
6865
6866 // Update VAList.
John McCall7f416cc2015-09-08 08:05:57 +00006867 llvm::Value *NextPtr =
6868 Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next");
6869 Builder.CreateStore(NextPtr, VAListAddr);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00006870
John McCall7f416cc2015-09-08 08:05:57 +00006871 return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006872}
6873
6874void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
6875 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006876 for (auto &I : FI.arguments())
6877 I.info = classifyType(I.type, 16 * 8);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006878}
6879
6880namespace {
6881class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
6882public:
6883 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
6884 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
Roman Divackyf02c9942014-02-24 18:46:27 +00006885
Craig Topper4f12f102014-03-12 06:41:41 +00006886 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyf02c9942014-02-24 18:46:27 +00006887 return 14;
6888 }
6889
6890 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00006891 llvm::Value *Address) const override;
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006892};
6893} // end anonymous namespace
6894
Roman Divackyf02c9942014-02-24 18:46:27 +00006895bool
6896SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6897 llvm::Value *Address) const {
6898 // This is calculated from the LLVM and GCC tables and verified
6899 // against gcc output. AFAIK all ABIs use the same encoding.
6900
6901 CodeGen::CGBuilderTy &Builder = CGF.Builder;
6902
6903 llvm::IntegerType *i8 = CGF.Int8Ty;
6904 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
6905 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
6906
6907 // 0-31: the 8-byte general-purpose registers
6908 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
6909
6910 // 32-63: f0-31, the 4-byte floating-point registers
6911 AssignToArrayRange(Builder, Address, Four8, 32, 63);
6912
6913 // Y = 64
6914 // PSR = 65
6915 // WIM = 66
6916 // TBR = 67
6917 // PC = 68
6918 // NPC = 69
6919 // FSR = 70
6920 // CSR = 71
6921 AssignToArrayRange(Builder, Address, Eight8, 64, 71);
Eric Christopher7565e0d2015-05-29 23:09:49 +00006922
Roman Divackyf02c9942014-02-24 18:46:27 +00006923 // 72-87: d0-15, the 8-byte floating-point registers
6924 AssignToArrayRange(Builder, Address, Eight8, 72, 87);
6925
6926 return false;
6927}
6928
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00006929
Robert Lytton0e076492013-08-13 09:43:10 +00006930//===----------------------------------------------------------------------===//
Robert Lyttond21e2d72014-03-03 13:45:29 +00006931// XCore ABI Implementation
Robert Lytton0e076492013-08-13 09:43:10 +00006932//===----------------------------------------------------------------------===//
Robert Lytton844aeeb2014-05-02 09:33:20 +00006933
Robert Lytton0e076492013-08-13 09:43:10 +00006934namespace {
Robert Lytton844aeeb2014-05-02 09:33:20 +00006935
6936/// A SmallStringEnc instance is used to build up the TypeString by passing
6937/// it by reference between functions that append to it.
6938typedef llvm::SmallString<128> SmallStringEnc;
6939
6940/// TypeStringCache caches the meta encodings of Types.
6941///
6942/// The reason for caching TypeStrings is two fold:
6943/// 1. To cache a type's encoding for later uses;
6944/// 2. As a means to break recursive member type inclusion.
6945///
6946/// A cache Entry can have a Status of:
6947/// NonRecursive: The type encoding is not recursive;
6948/// Recursive: The type encoding is recursive;
6949/// Incomplete: An incomplete TypeString;
6950/// IncompleteUsed: An incomplete TypeString that has been used in a
6951/// Recursive type encoding.
6952///
6953/// A NonRecursive entry will have all of its sub-members expanded as fully
6954/// as possible. Whilst it may contain types which are recursive, the type
6955/// itself is not recursive and thus its encoding may be safely used whenever
6956/// the type is encountered.
6957///
6958/// A Recursive entry will have all of its sub-members expanded as fully as
6959/// possible. The type itself is recursive and it may contain other types which
6960/// are recursive. The Recursive encoding must not be used during the expansion
6961/// of a recursive type's recursive branch. For simplicity the code uses
6962/// IncompleteCount to reject all usage of Recursive encodings for member types.
6963///
6964/// An Incomplete entry is always a RecordType and only encodes its
6965/// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
6966/// are placed into the cache during type expansion as a means to identify and
6967/// handle recursive inclusion of types as sub-members. If there is recursion
6968/// the entry becomes IncompleteUsed.
6969///
6970/// During the expansion of a RecordType's members:
6971///
6972/// If the cache contains a NonRecursive encoding for the member type, the
6973/// cached encoding is used;
6974///
6975/// If the cache contains a Recursive encoding for the member type, the
6976/// cached encoding is 'Swapped' out, as it may be incorrect, and...
6977///
6978/// If the member is a RecordType, an Incomplete encoding is placed into the
6979/// cache to break potential recursive inclusion of itself as a sub-member;
6980///
6981/// Once a member RecordType has been expanded, its temporary incomplete
6982/// entry is removed from the cache. If a Recursive encoding was swapped out
6983/// it is swapped back in;
6984///
6985/// If an incomplete entry is used to expand a sub-member, the incomplete
6986/// entry is marked as IncompleteUsed. The cache keeps count of how many
6987/// IncompleteUsed entries it currently contains in IncompleteUsedCount;
6988///
6989/// If a member's encoding is found to be a NonRecursive or Recursive viz:
6990/// IncompleteUsedCount==0, the member's encoding is added to the cache.
6991/// Else the member is part of a recursive type and thus the recursion has
6992/// been exited too soon for the encoding to be correct for the member.
6993///
6994class TypeStringCache {
6995 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
6996 struct Entry {
6997 std::string Str; // The encoded TypeString for the type.
6998 enum Status State; // Information about the encoding in 'Str'.
6999 std::string Swapped; // A temporary place holder for a Recursive encoding
7000 // during the expansion of RecordType's members.
7001 };
7002 std::map<const IdentifierInfo *, struct Entry> Map;
7003 unsigned IncompleteCount; // Number of Incomplete entries in the Map.
7004 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
7005public:
Hans Wennborg4afe5042015-07-22 20:46:26 +00007006 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
Robert Lytton844aeeb2014-05-02 09:33:20 +00007007 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
7008 bool removeIncomplete(const IdentifierInfo *ID);
7009 void addIfComplete(const IdentifierInfo *ID, StringRef Str,
7010 bool IsRecursive);
7011 StringRef lookupStr(const IdentifierInfo *ID);
7012};
7013
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00007014/// TypeString encodings for enum & union fields must be order.
Robert Lytton844aeeb2014-05-02 09:33:20 +00007015/// FieldEncoding is a helper for this ordering process.
7016class FieldEncoding {
7017 bool HasName;
7018 std::string Enc;
7019public:
Hans Wennborg4afe5042015-07-22 20:46:26 +00007020 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
7021 StringRef str() {return Enc.c_str();}
Robert Lytton844aeeb2014-05-02 09:33:20 +00007022 bool operator<(const FieldEncoding &rhs) const {
7023 if (HasName != rhs.HasName) return HasName;
7024 return Enc < rhs.Enc;
7025 }
7026};
7027
Robert Lytton7d1db152013-08-19 09:46:39 +00007028class XCoreABIInfo : public DefaultABIInfo {
7029public:
7030 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
John McCall7f416cc2015-09-08 08:05:57 +00007031 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7032 QualType Ty) const override;
Robert Lytton7d1db152013-08-19 09:46:39 +00007033};
7034
Robert Lyttond21e2d72014-03-03 13:45:29 +00007035class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
Robert Lytton844aeeb2014-05-02 09:33:20 +00007036 mutable TypeStringCache TSC;
Robert Lytton0e076492013-08-13 09:43:10 +00007037public:
Robert Lyttond21e2d72014-03-03 13:45:29 +00007038 XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
Robert Lytton7d1db152013-08-19 09:46:39 +00007039 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
Rafael Espindola8dcd6e72014-05-08 15:01:48 +00007040 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7041 CodeGen::CodeGenModule &M) const override;
Robert Lytton0e076492013-08-13 09:43:10 +00007042};
Robert Lytton844aeeb2014-05-02 09:33:20 +00007043
Robert Lytton2d196952013-10-11 10:29:34 +00007044} // End anonymous namespace.
Robert Lytton0e076492013-08-13 09:43:10 +00007045
James Y Knight29b5f082016-02-24 02:59:33 +00007046// TODO: this implementation is likely now redundant with the default
7047// EmitVAArg.
John McCall7f416cc2015-09-08 08:05:57 +00007048Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7049 QualType Ty) const {
Robert Lytton7d1db152013-08-19 09:46:39 +00007050 CGBuilderTy &Builder = CGF.Builder;
Robert Lytton7d1db152013-08-19 09:46:39 +00007051
Robert Lytton2d196952013-10-11 10:29:34 +00007052 // Get the VAList.
John McCall7f416cc2015-09-08 08:05:57 +00007053 CharUnits SlotSize = CharUnits::fromQuantity(4);
7054 Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
Robert Lytton7d1db152013-08-19 09:46:39 +00007055
Robert Lytton2d196952013-10-11 10:29:34 +00007056 // Handle the argument.
7057 ABIArgInfo AI = classifyArgumentType(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00007058 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
Robert Lytton2d196952013-10-11 10:29:34 +00007059 llvm::Type *ArgTy = CGT.ConvertType(Ty);
7060 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
7061 AI.setCoerceToType(ArgTy);
Robert Lytton7d1db152013-08-19 09:46:39 +00007062 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
John McCall7f416cc2015-09-08 08:05:57 +00007063
7064 Address Val = Address::invalid();
7065 CharUnits ArgSize = CharUnits::Zero();
Robert Lytton7d1db152013-08-19 09:46:39 +00007066 switch (AI.getKind()) {
Robert Lytton7d1db152013-08-19 09:46:39 +00007067 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00007068 case ABIArgInfo::CoerceAndExpand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00007069 case ABIArgInfo::InAlloca:
Robert Lytton7d1db152013-08-19 09:46:39 +00007070 llvm_unreachable("Unsupported ABI kind for va_arg");
7071 case ABIArgInfo::Ignore:
John McCall7f416cc2015-09-08 08:05:57 +00007072 Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
7073 ArgSize = CharUnits::Zero();
Robert Lytton2d196952013-10-11 10:29:34 +00007074 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00007075 case ABIArgInfo::Extend:
7076 case ABIArgInfo::Direct:
John McCall7f416cc2015-09-08 08:05:57 +00007077 Val = Builder.CreateBitCast(AP, ArgPtrTy);
7078 ArgSize = CharUnits::fromQuantity(
7079 getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
Rui Ueyama83aa9792016-01-14 21:00:27 +00007080 ArgSize = ArgSize.alignTo(SlotSize);
Robert Lytton2d196952013-10-11 10:29:34 +00007081 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00007082 case ABIArgInfo::Indirect:
John McCall7f416cc2015-09-08 08:05:57 +00007083 Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
7084 Val = Address(Builder.CreateLoad(Val), TypeAlign);
7085 ArgSize = SlotSize;
Robert Lytton2d196952013-10-11 10:29:34 +00007086 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00007087 }
Robert Lytton2d196952013-10-11 10:29:34 +00007088
7089 // Increment the VAList.
John McCall7f416cc2015-09-08 08:05:57 +00007090 if (!ArgSize.isZero()) {
7091 llvm::Value *APN =
7092 Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize);
7093 Builder.CreateStore(APN, VAListAddr);
Robert Lytton2d196952013-10-11 10:29:34 +00007094 }
John McCall7f416cc2015-09-08 08:05:57 +00007095
Robert Lytton2d196952013-10-11 10:29:34 +00007096 return Val;
Robert Lytton7d1db152013-08-19 09:46:39 +00007097}
Robert Lytton0e076492013-08-13 09:43:10 +00007098
Robert Lytton844aeeb2014-05-02 09:33:20 +00007099/// During the expansion of a RecordType, an incomplete TypeString is placed
7100/// into the cache as a means to identify and break recursion.
7101/// If there is a Recursive encoding in the cache, it is swapped out and will
7102/// be reinserted by removeIncomplete().
7103/// All other types of encoding should have been used rather than arriving here.
7104void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
7105 std::string StubEnc) {
7106 if (!ID)
7107 return;
7108 Entry &E = Map[ID];
7109 assert( (E.Str.empty() || E.State == Recursive) &&
7110 "Incorrectly use of addIncomplete");
7111 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
7112 E.Swapped.swap(E.Str); // swap out the Recursive
7113 E.Str.swap(StubEnc);
7114 E.State = Incomplete;
7115 ++IncompleteCount;
7116}
7117
7118/// Once the RecordType has been expanded, the temporary incomplete TypeString
7119/// must be removed from the cache.
7120/// If a Recursive was swapped out by addIncomplete(), it will be replaced.
7121/// Returns true if the RecordType was defined recursively.
7122bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
7123 if (!ID)
7124 return false;
7125 auto I = Map.find(ID);
7126 assert(I != Map.end() && "Entry not present");
7127 Entry &E = I->second;
7128 assert( (E.State == Incomplete ||
7129 E.State == IncompleteUsed) &&
7130 "Entry must be an incomplete type");
7131 bool IsRecursive = false;
7132 if (E.State == IncompleteUsed) {
7133 // We made use of our Incomplete encoding, thus we are recursive.
7134 IsRecursive = true;
7135 --IncompleteUsedCount;
7136 }
7137 if (E.Swapped.empty())
7138 Map.erase(I);
7139 else {
7140 // Swap the Recursive back.
7141 E.Swapped.swap(E.Str);
7142 E.Swapped.clear();
7143 E.State = Recursive;
7144 }
7145 --IncompleteCount;
7146 return IsRecursive;
7147}
7148
7149/// Add the encoded TypeString to the cache only if it is NonRecursive or
7150/// Recursive (viz: all sub-members were expanded as fully as possible).
7151void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
7152 bool IsRecursive) {
7153 if (!ID || IncompleteUsedCount)
7154 return; // No key or it is is an incomplete sub-type so don't add.
7155 Entry &E = Map[ID];
7156 if (IsRecursive && !E.Str.empty()) {
7157 assert(E.State==Recursive && E.Str.size() == Str.size() &&
7158 "This is not the same Recursive entry");
7159 // The parent container was not recursive after all, so we could have used
7160 // this Recursive sub-member entry after all, but we assumed the worse when
7161 // we started viz: IncompleteCount!=0.
7162 return;
7163 }
7164 assert(E.Str.empty() && "Entry already present");
7165 E.Str = Str.str();
7166 E.State = IsRecursive? Recursive : NonRecursive;
7167}
7168
7169/// Return a cached TypeString encoding for the ID. If there isn't one, or we
7170/// are recursively expanding a type (IncompleteCount != 0) and the cached
7171/// encoding is Recursive, return an empty StringRef.
7172StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
7173 if (!ID)
7174 return StringRef(); // We have no key.
7175 auto I = Map.find(ID);
7176 if (I == Map.end())
7177 return StringRef(); // We have no encoding.
7178 Entry &E = I->second;
7179 if (E.State == Recursive && IncompleteCount)
7180 return StringRef(); // We don't use Recursive encodings for member types.
7181
7182 if (E.State == Incomplete) {
7183 // The incomplete type is being used to break out of recursion.
7184 E.State = IncompleteUsed;
7185 ++IncompleteUsedCount;
7186 }
7187 return E.Str.c_str();
7188}
7189
7190/// The XCore ABI includes a type information section that communicates symbol
7191/// type information to the linker. The linker uses this information to verify
7192/// safety/correctness of things such as array bound and pointers et al.
7193/// The ABI only requires C (and XC) language modules to emit TypeStrings.
7194/// This type information (TypeString) is emitted into meta data for all global
7195/// symbols: definitions, declarations, functions & variables.
7196///
7197/// The TypeString carries type, qualifier, name, size & value details.
7198/// Please see 'Tools Development Guide' section 2.16.2 for format details:
Eric Christopher7565e0d2015-05-29 23:09:49 +00007199/// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
Robert Lytton844aeeb2014-05-02 09:33:20 +00007200/// The output is tested by test/CodeGen/xcore-stringtype.c.
7201///
7202static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7203 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
7204
7205/// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
7206void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7207 CodeGen::CodeGenModule &CGM) const {
7208 SmallStringEnc Enc;
7209 if (getTypeString(Enc, D, CGM, TSC)) {
7210 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007211 llvm::SmallVector<llvm::Metadata *, 2> MDVals;
7212 MDVals.push_back(llvm::ConstantAsMetadata::get(GV));
Robert Lytton844aeeb2014-05-02 09:33:20 +00007213 MDVals.push_back(llvm::MDString::get(Ctx, Enc.str()));
7214 llvm::NamedMDNode *MD =
7215 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
7216 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
7217 }
7218}
7219
7220static bool appendType(SmallStringEnc &Enc, QualType QType,
7221 const CodeGen::CodeGenModule &CGM,
7222 TypeStringCache &TSC);
7223
7224/// Helper function for appendRecordType().
Eric Christopher7565e0d2015-05-29 23:09:49 +00007225/// Builds a SmallVector containing the encoded field types in declaration
7226/// order.
Robert Lytton844aeeb2014-05-02 09:33:20 +00007227static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
7228 const RecordDecl *RD,
7229 const CodeGen::CodeGenModule &CGM,
7230 TypeStringCache &TSC) {
Hans Wennborga302cd92014-08-21 16:06:57 +00007231 for (const auto *Field : RD->fields()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00007232 SmallStringEnc Enc;
7233 Enc += "m(";
Hans Wennborga302cd92014-08-21 16:06:57 +00007234 Enc += Field->getName();
Robert Lytton844aeeb2014-05-02 09:33:20 +00007235 Enc += "){";
Hans Wennborga302cd92014-08-21 16:06:57 +00007236 if (Field->isBitField()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00007237 Enc += "b(";
7238 llvm::raw_svector_ostream OS(Enc);
Hans Wennborga302cd92014-08-21 16:06:57 +00007239 OS << Field->getBitWidthValue(CGM.getContext());
Robert Lytton844aeeb2014-05-02 09:33:20 +00007240 Enc += ':';
7241 }
Hans Wennborga302cd92014-08-21 16:06:57 +00007242 if (!appendType(Enc, Field->getType(), CGM, TSC))
Robert Lytton844aeeb2014-05-02 09:33:20 +00007243 return false;
Hans Wennborga302cd92014-08-21 16:06:57 +00007244 if (Field->isBitField())
Robert Lytton844aeeb2014-05-02 09:33:20 +00007245 Enc += ')';
7246 Enc += '}';
Benjamin Kramer3204b152015-05-29 19:42:19 +00007247 FE.emplace_back(!Field->getName().empty(), Enc);
Robert Lytton844aeeb2014-05-02 09:33:20 +00007248 }
7249 return true;
7250}
7251
7252/// Appends structure and union types to Enc and adds encoding to cache.
7253/// Recursively calls appendType (via extractFieldType) for each field.
7254/// Union types have their fields ordered according to the ABI.
7255static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
7256 const CodeGen::CodeGenModule &CGM,
7257 TypeStringCache &TSC, const IdentifierInfo *ID) {
7258 // Append the cached TypeString if we have one.
7259 StringRef TypeString = TSC.lookupStr(ID);
7260 if (!TypeString.empty()) {
7261 Enc += TypeString;
7262 return true;
7263 }
7264
7265 // Start to emit an incomplete TypeString.
7266 size_t Start = Enc.size();
7267 Enc += (RT->isUnionType()? 'u' : 's');
7268 Enc += '(';
7269 if (ID)
7270 Enc += ID->getName();
7271 Enc += "){";
7272
7273 // We collect all encoded fields and order as necessary.
7274 bool IsRecursive = false;
Robert Lytton844aeeb2014-05-02 09:33:20 +00007275 const RecordDecl *RD = RT->getDecl()->getDefinition();
7276 if (RD && !RD->field_empty()) {
7277 // An incomplete TypeString stub is placed in the cache for this RecordType
7278 // so that recursive calls to this RecordType will use it whilst building a
7279 // complete TypeString for this RecordType.
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00007280 SmallVector<FieldEncoding, 16> FE;
Robert Lytton844aeeb2014-05-02 09:33:20 +00007281 std::string StubEnc(Enc.substr(Start).str());
7282 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
7283 TSC.addIncomplete(ID, std::move(StubEnc));
7284 if (!extractFieldType(FE, RD, CGM, TSC)) {
7285 (void) TSC.removeIncomplete(ID);
7286 return false;
7287 }
7288 IsRecursive = TSC.removeIncomplete(ID);
7289 // The ABI requires unions to be sorted but not structures.
7290 // See FieldEncoding::operator< for sort algorithm.
7291 if (RT->isUnionType())
7292 std::sort(FE.begin(), FE.end());
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00007293 // We can now complete the TypeString.
7294 unsigned E = FE.size();
Robert Lytton844aeeb2014-05-02 09:33:20 +00007295 for (unsigned I = 0; I != E; ++I) {
7296 if (I)
7297 Enc += ',';
7298 Enc += FE[I].str();
7299 }
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00007300 }
Robert Lytton844aeeb2014-05-02 09:33:20 +00007301 Enc += '}';
7302 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
7303 return true;
7304}
7305
7306/// Appends enum types to Enc and adds the encoding to the cache.
7307static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
7308 TypeStringCache &TSC,
7309 const IdentifierInfo *ID) {
7310 // Append the cached TypeString if we have one.
7311 StringRef TypeString = TSC.lookupStr(ID);
7312 if (!TypeString.empty()) {
7313 Enc += TypeString;
7314 return true;
7315 }
7316
7317 size_t Start = Enc.size();
7318 Enc += "e(";
7319 if (ID)
7320 Enc += ID->getName();
7321 Enc += "){";
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00007322
7323 // We collect all encoded enumerations and order them alphanumerically.
Robert Lytton844aeeb2014-05-02 09:33:20 +00007324 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00007325 SmallVector<FieldEncoding, 16> FE;
7326 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
7327 ++I) {
7328 SmallStringEnc EnumEnc;
7329 EnumEnc += "m(";
7330 EnumEnc += I->getName();
7331 EnumEnc += "){";
7332 I->getInitVal().toString(EnumEnc);
7333 EnumEnc += '}';
7334 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
7335 }
7336 std::sort(FE.begin(), FE.end());
7337 unsigned E = FE.size();
7338 for (unsigned I = 0; I != E; ++I) {
7339 if (I)
Robert Lytton844aeeb2014-05-02 09:33:20 +00007340 Enc += ',';
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00007341 Enc += FE[I].str();
Robert Lytton844aeeb2014-05-02 09:33:20 +00007342 }
7343 }
7344 Enc += '}';
7345 TSC.addIfComplete(ID, Enc.substr(Start), false);
7346 return true;
7347}
7348
7349/// Appends type's qualifier to Enc.
7350/// This is done prior to appending the type's encoding.
7351static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
7352 // Qualifiers are emitted in alphabetical order.
Craig Topper273dbc62015-10-18 05:29:26 +00007353 static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
Robert Lytton844aeeb2014-05-02 09:33:20 +00007354 int Lookup = 0;
7355 if (QT.isConstQualified())
7356 Lookup += 1<<0;
7357 if (QT.isRestrictQualified())
7358 Lookup += 1<<1;
7359 if (QT.isVolatileQualified())
7360 Lookup += 1<<2;
7361 Enc += Table[Lookup];
7362}
7363
7364/// Appends built-in types to Enc.
7365static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
7366 const char *EncType;
7367 switch (BT->getKind()) {
7368 case BuiltinType::Void:
7369 EncType = "0";
7370 break;
7371 case BuiltinType::Bool:
7372 EncType = "b";
7373 break;
7374 case BuiltinType::Char_U:
7375 EncType = "uc";
7376 break;
7377 case BuiltinType::UChar:
7378 EncType = "uc";
7379 break;
7380 case BuiltinType::SChar:
7381 EncType = "sc";
7382 break;
7383 case BuiltinType::UShort:
7384 EncType = "us";
7385 break;
7386 case BuiltinType::Short:
7387 EncType = "ss";
7388 break;
7389 case BuiltinType::UInt:
7390 EncType = "ui";
7391 break;
7392 case BuiltinType::Int:
7393 EncType = "si";
7394 break;
7395 case BuiltinType::ULong:
7396 EncType = "ul";
7397 break;
7398 case BuiltinType::Long:
7399 EncType = "sl";
7400 break;
7401 case BuiltinType::ULongLong:
7402 EncType = "ull";
7403 break;
7404 case BuiltinType::LongLong:
7405 EncType = "sll";
7406 break;
7407 case BuiltinType::Float:
7408 EncType = "ft";
7409 break;
7410 case BuiltinType::Double:
7411 EncType = "d";
7412 break;
7413 case BuiltinType::LongDouble:
7414 EncType = "ld";
7415 break;
7416 default:
7417 return false;
7418 }
7419 Enc += EncType;
7420 return true;
7421}
7422
7423/// Appends a pointer encoding to Enc before calling appendType for the pointee.
7424static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
7425 const CodeGen::CodeGenModule &CGM,
7426 TypeStringCache &TSC) {
7427 Enc += "p(";
7428 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
7429 return false;
7430 Enc += ')';
7431 return true;
7432}
7433
7434/// Appends array encoding to Enc before calling appendType for the element.
Robert Lytton6adb20f2014-06-05 09:06:21 +00007435static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
7436 const ArrayType *AT,
Robert Lytton844aeeb2014-05-02 09:33:20 +00007437 const CodeGen::CodeGenModule &CGM,
7438 TypeStringCache &TSC, StringRef NoSizeEnc) {
7439 if (AT->getSizeModifier() != ArrayType::Normal)
7440 return false;
7441 Enc += "a(";
7442 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
7443 CAT->getSize().toStringUnsigned(Enc);
7444 else
7445 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
7446 Enc += ':';
Robert Lytton6adb20f2014-06-05 09:06:21 +00007447 // The Qualifiers should be attached to the type rather than the array.
7448 appendQualifier(Enc, QT);
Robert Lytton844aeeb2014-05-02 09:33:20 +00007449 if (!appendType(Enc, AT->getElementType(), CGM, TSC))
7450 return false;
7451 Enc += ')';
7452 return true;
7453}
7454
7455/// Appends a function encoding to Enc, calling appendType for the return type
7456/// and the arguments.
7457static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
7458 const CodeGen::CodeGenModule &CGM,
7459 TypeStringCache &TSC) {
7460 Enc += "f{";
7461 if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
7462 return false;
7463 Enc += "}(";
7464 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
7465 // N.B. we are only interested in the adjusted param types.
7466 auto I = FPT->param_type_begin();
7467 auto E = FPT->param_type_end();
7468 if (I != E) {
7469 do {
7470 if (!appendType(Enc, *I, CGM, TSC))
7471 return false;
7472 ++I;
7473 if (I != E)
7474 Enc += ',';
7475 } while (I != E);
7476 if (FPT->isVariadic())
7477 Enc += ",va";
7478 } else {
7479 if (FPT->isVariadic())
7480 Enc += "va";
7481 else
7482 Enc += '0';
7483 }
7484 }
7485 Enc += ')';
7486 return true;
7487}
7488
7489/// Handles the type's qualifier before dispatching a call to handle specific
7490/// type encodings.
7491static bool appendType(SmallStringEnc &Enc, QualType QType,
7492 const CodeGen::CodeGenModule &CGM,
7493 TypeStringCache &TSC) {
7494
7495 QualType QT = QType.getCanonicalType();
7496
Robert Lytton6adb20f2014-06-05 09:06:21 +00007497 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
7498 // The Qualifiers should be attached to the type rather than the array.
7499 // Thus we don't call appendQualifier() here.
7500 return appendArrayType(Enc, QT, AT, CGM, TSC, "");
7501
Robert Lytton844aeeb2014-05-02 09:33:20 +00007502 appendQualifier(Enc, QT);
7503
7504 if (const BuiltinType *BT = QT->getAs<BuiltinType>())
7505 return appendBuiltinType(Enc, BT);
7506
Robert Lytton844aeeb2014-05-02 09:33:20 +00007507 if (const PointerType *PT = QT->getAs<PointerType>())
7508 return appendPointerType(Enc, PT, CGM, TSC);
7509
7510 if (const EnumType *ET = QT->getAs<EnumType>())
7511 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
7512
7513 if (const RecordType *RT = QT->getAsStructureType())
7514 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7515
7516 if (const RecordType *RT = QT->getAsUnionType())
7517 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7518
7519 if (const FunctionType *FT = QT->getAs<FunctionType>())
7520 return appendFunctionType(Enc, FT, CGM, TSC);
7521
7522 return false;
7523}
7524
7525static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7526 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
7527 if (!D)
7528 return false;
7529
7530 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7531 if (FD->getLanguageLinkage() != CLanguageLinkage)
7532 return false;
7533 return appendType(Enc, FD->getType(), CGM, TSC);
7534 }
7535
7536 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
7537 if (VD->getLanguageLinkage() != CLanguageLinkage)
7538 return false;
7539 QualType QT = VD->getType().getCanonicalType();
7540 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
7541 // Global ArrayTypes are given a size of '*' if the size is unknown.
Robert Lytton6adb20f2014-06-05 09:06:21 +00007542 // The Qualifiers should be attached to the type rather than the array.
7543 // Thus we don't call appendQualifier() here.
7544 return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
Robert Lytton844aeeb2014-05-02 09:33:20 +00007545 }
7546 return appendType(Enc, QT, CGM, TSC);
7547 }
7548 return false;
7549}
7550
7551
Robert Lytton0e076492013-08-13 09:43:10 +00007552//===----------------------------------------------------------------------===//
7553// Driver code
7554//===----------------------------------------------------------------------===//
7555
Rafael Espindola9f834732014-09-19 01:54:22 +00007556const llvm::Triple &CodeGenModule::getTriple() const {
7557 return getTarget().getTriple();
7558}
7559
7560bool CodeGenModule::supportsCOMDAT() const {
7561 return !getTriple().isOSBinFormatMachO();
7562}
7563
Chris Lattner2b037972010-07-29 02:01:43 +00007564const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00007565 if (TheTargetCodeGenInfo)
7566 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00007567
John McCallc8e01702013-04-16 22:48:15 +00007568 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00007569 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00007570 default:
Chris Lattner2b037972010-07-29 02:01:43 +00007571 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00007572
Derek Schuff09338a22012-09-06 17:37:28 +00007573 case llvm::Triple::le32:
7574 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00007575 case llvm::Triple::mips:
7576 case llvm::Triple::mipsel:
Petar Jovanovic26a4a402015-07-08 13:07:31 +00007577 if (Triple.getOS() == llvm::Triple::NaCl)
7578 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
Akira Hatanakac4baedd2013-11-11 22:10:46 +00007579 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
7580
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00007581 case llvm::Triple::mips64:
7582 case llvm::Triple::mips64el:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00007583 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
7584
Tim Northover25e8a672014-05-24 12:51:25 +00007585 case llvm::Triple::aarch64:
Tim Northover40956e62014-07-23 12:32:58 +00007586 case llvm::Triple::aarch64_be: {
Tim Northover573cbee2014-05-24 12:52:07 +00007587 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
Alp Toker4925ba72014-06-07 23:30:42 +00007588 if (getTarget().getABI() == "darwinpcs")
Tim Northover573cbee2014-05-24 12:52:07 +00007589 Kind = AArch64ABIInfo::DarwinPCS;
Tim Northovera2ee4332014-03-29 15:09:45 +00007590
Tim Northover573cbee2014-05-24 12:52:07 +00007591 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind));
Tim Northovera2ee4332014-03-29 15:09:45 +00007592 }
7593
Dan Gohmanc2853072015-09-03 22:51:53 +00007594 case llvm::Triple::wasm32:
7595 case llvm::Triple::wasm64:
7596 return *(TheTargetCodeGenInfo = new WebAssemblyTargetCodeGenInfo(Types));
7597
Daniel Dunbard59655c2009-09-12 00:59:49 +00007598 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +00007599 case llvm::Triple::armeb:
Daniel Dunbard59655c2009-09-12 00:59:49 +00007600 case llvm::Triple::thumb:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +00007601 case llvm::Triple::thumbeb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00007602 {
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00007603 if (Triple.getOS() == llvm::Triple::Win32) {
7604 TheTargetCodeGenInfo =
7605 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP);
7606 return *TheTargetCodeGenInfo;
7607 }
7608
Sandeep Patel45df3dd2011-04-05 00:23:47 +00007609 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Tim Northover5627d392015-10-30 16:30:45 +00007610 StringRef ABIStr = getTarget().getABI();
7611 if (ABIStr == "apcs-gnu")
Sandeep Patel45df3dd2011-04-05 00:23:47 +00007612 Kind = ARMABIInfo::APCS;
Tim Northover5627d392015-10-30 16:30:45 +00007613 else if (ABIStr == "aapcs16")
7614 Kind = ARMABIInfo::AAPCS16_VFP;
David Tweed8f676532012-10-25 13:33:01 +00007615 else if (CodeGenOpts.FloatABI == "hard" ||
John McCallc8e01702013-04-16 22:48:15 +00007616 (CodeGenOpts.FloatABI != "soft" &&
7617 Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
Sandeep Patel45df3dd2011-04-05 00:23:47 +00007618 Kind = ARMABIInfo::AAPCS_VFP;
7619
Derek Schuff71658bd2015-01-29 00:47:04 +00007620 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
Sandeep Patel45df3dd2011-04-05 00:23:47 +00007621 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00007622
John McCallea8d8bb2010-03-11 00:10:12 +00007623 case llvm::Triple::ppc:
Petar Jovanovic88a328f2015-12-14 17:51:50 +00007624 return *(TheTargetCodeGenInfo =
7625 new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft"));
Roman Divackyd966e722012-05-09 18:22:46 +00007626 case llvm::Triple::ppc64:
Ulrich Weigandb7122372014-07-21 00:48:09 +00007627 if (Triple.isOSBinFormatELF()) {
Ulrich Weigandb7122372014-07-21 00:48:09 +00007628 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
Ulrich Weigand8afad612014-07-28 13:17:52 +00007629 if (getTarget().getABI() == "elfv2")
7630 Kind = PPC64_SVR4_ABIInfo::ELFv2;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00007631 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Ulrich Weigand8afad612014-07-28 13:17:52 +00007632
Ulrich Weigandb7122372014-07-21 00:48:09 +00007633 return *(TheTargetCodeGenInfo =
Hal Finkel0d0a1a52015-03-11 19:14:15 +00007634 new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
Ulrich Weigandb7122372014-07-21 00:48:09 +00007635 } else
Bill Schmidt25cb3492012-10-03 19:18:57 +00007636 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
Ulrich Weigandb7122372014-07-21 00:48:09 +00007637 case llvm::Triple::ppc64le: {
Bill Schmidt778d3872013-07-26 01:36:11 +00007638 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
Ulrich Weigandb7122372014-07-21 00:48:09 +00007639 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00007640 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
Ulrich Weigand8afad612014-07-28 13:17:52 +00007641 Kind = PPC64_SVR4_ABIInfo::ELFv1;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00007642 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Ulrich Weigand8afad612014-07-28 13:17:52 +00007643
Ulrich Weigandb7122372014-07-21 00:48:09 +00007644 return *(TheTargetCodeGenInfo =
Hal Finkel0d0a1a52015-03-11 19:14:15 +00007645 new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
Ulrich Weigandb7122372014-07-21 00:48:09 +00007646 }
John McCallea8d8bb2010-03-11 00:10:12 +00007647
Peter Collingbournec947aae2012-05-20 23:28:41 +00007648 case llvm::Triple::nvptx:
7649 case llvm::Triple::nvptx64:
Justin Holewinski83e96682012-05-24 17:43:12 +00007650 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00007651
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00007652 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00007653 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00007654
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00007655 case llvm::Triple::systemz: {
7656 bool HasVector = getTarget().getABI() == "vector";
7657 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types,
7658 HasVector));
7659 }
Ulrich Weigand47445072013-05-06 16:26:41 +00007660
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007661 case llvm::Triple::tce:
7662 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
7663
Eli Friedman33465822011-07-08 23:31:17 +00007664 case llvm::Triple::x86: {
John McCall1fe2a8c2013-06-18 02:46:29 +00007665 bool IsDarwinVectorABI = Triple.isOSDarwin();
Michael Kupersteindc745202015-10-19 07:52:25 +00007666 bool RetSmallStructInRegABI =
John McCall1fe2a8c2013-06-18 02:46:29 +00007667 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
Saleem Abdulrasoolec5c6242014-11-23 02:16:24 +00007668 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00007669
John McCall1fe2a8c2013-06-18 02:46:29 +00007670 if (Triple.getOS() == llvm::Triple::Win32) {
Eric Christopher7565e0d2015-05-29 23:09:49 +00007671 return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo(
Michael Kupersteindc745202015-10-19 07:52:25 +00007672 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
Eric Christopher7565e0d2015-05-29 23:09:49 +00007673 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
John McCall1fe2a8c2013-06-18 02:46:29 +00007674 } else {
Eric Christopher7565e0d2015-05-29 23:09:49 +00007675 return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo(
Michael Kupersteindc745202015-10-19 07:52:25 +00007676 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00007677 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
7678 CodeGenOpts.FloatABI == "soft"));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00007679 }
Eli Friedman33465822011-07-08 23:31:17 +00007680 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00007681
Eli Friedmanbfd5add2011-12-02 00:11:43 +00007682 case llvm::Triple::x86_64: {
Ahmed Bougachad39a4152015-06-22 21:30:39 +00007683 StringRef ABI = getTarget().getABI();
Ahmed Bougacha0b938282015-06-22 21:31:43 +00007684 X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 :
7685 ABI == "avx" ? X86AVXABILevel::AVX :
Ahmed Bougachad39a4152015-06-22 21:30:39 +00007686 X86AVXABILevel::None);
7687
Chris Lattner04dc9572010-08-31 16:44:54 +00007688 switch (Triple.getOS()) {
7689 case llvm::Triple::Win32:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00007690 return *(TheTargetCodeGenInfo =
7691 new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
Alex Rosenberg12207fa2015-01-27 14:47:44 +00007692 case llvm::Triple::PS4:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00007693 return *(TheTargetCodeGenInfo =
7694 new PS4TargetCodeGenInfo(Types, AVXLevel));
Chris Lattner04dc9572010-08-31 16:44:54 +00007695 default:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00007696 return *(TheTargetCodeGenInfo =
7697 new X86_64TargetCodeGenInfo(Types, AVXLevel));
Chris Lattner04dc9572010-08-31 16:44:54 +00007698 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00007699 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00007700 case llvm::Triple::hexagon:
7701 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007702 case llvm::Triple::r600:
7703 return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types));
Tom Stellardd8e38a32015-01-06 20:34:47 +00007704 case llvm::Triple::amdgcn:
7705 return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types));
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007706 case llvm::Triple::sparcv9:
7707 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
Robert Lytton0e076492013-08-13 09:43:10 +00007708 case llvm::Triple::xcore:
Robert Lyttond21e2d72014-03-03 13:45:29 +00007709 return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types));
Eli Friedmanbfd5add2011-12-02 00:11:43 +00007710 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00007711}