blob: d253bb2ae16f01e94367e6bb2e694206c6b0b3a6 [file] [log] [blame]
Daniel Dunbara8f02052008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
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
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
Anders Carlsson7a785352009-04-03 22:48:58 +000021#include "clang/AST/DeclCXX.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000022#include "clang/AST/DeclObjC.h"
Daniel Dunbar51a2d192009-01-29 08:13:58 +000023#include "clang/AST/RecordLayout.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000024#include "llvm/ADT/StringExtras.h"
Devang Patel98bfe502008-09-24 01:01:36 +000025#include "llvm/Attributes.h"
Daniel Dunbar90e43452009-03-02 04:32:35 +000026#include "llvm/Support/CallSite.h"
Daniel Dunbare09a9692009-01-24 08:32:22 +000027#include "llvm/Support/CommandLine.h"
Daniel Dunbar3cfcec72009-02-12 09:04:14 +000028#include "llvm/Support/MathExtras.h"
Daniel Dunbar9f4874e2009-02-04 23:24:38 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000030#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000031
32#include "ABIInfo.h"
33
Daniel Dunbara8f02052008-09-08 21:33:45 +000034using namespace clang;
35using namespace CodeGen;
36
37/***/
38
Daniel Dunbara8f02052008-09-08 21:33:45 +000039// FIXME: Use iterator and sidestep silly type array creation.
40
Daniel Dunbar34bda882009-02-02 23:23:47 +000041const
Douglas Gregor4fa58902009-02-26 23:50:07 +000042CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000043 return getFunctionInfo(FTNP->getResultType(),
44 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000045}
46
Daniel Dunbar34bda882009-02-02 23:23:47 +000047const
Douglas Gregor4fa58902009-02-26 23:50:07 +000048CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000049 llvm::SmallVector<QualType, 16> ArgTys;
50 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000051 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000052 ArgTys.push_back(FTP->getArgType(i));
53 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000054}
55
Anders Carlsson7a785352009-04-03 22:48:58 +000056const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
57 llvm::SmallVector<QualType, 16> ArgTys;
Chris Lattnerd36c9d52009-05-12 20:27:19 +000058 // Add the 'this' pointer unless this is a static method.
59 if (MD->isInstance())
60 ArgTys.push_back(MD->getThisType(Context));
Anders Carlsson7a785352009-04-03 22:48:58 +000061
62 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType();
63 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
64 ArgTys.push_back(FTP->getArgType(i));
65 return getFunctionInfo(FTP->getResultType(), ArgTys);
66}
67
Daniel Dunbar34bda882009-02-02 23:23:47 +000068const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerd36c9d52009-05-12 20:27:19 +000069 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlsson7a785352009-04-03 22:48:58 +000070 if (MD->isInstance())
71 return getFunctionInfo(MD);
Anders Carlsson7a785352009-04-03 22:48:58 +000072
Daniel Dunbara8f02052008-09-08 21:33:45 +000073 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +000074 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar34bda882009-02-02 23:23:47 +000075 return getFunctionInfo(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +000076 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000077}
78
Daniel Dunbar34bda882009-02-02 23:23:47 +000079const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
80 llvm::SmallVector<QualType, 16> ArgTys;
81 ArgTys.push_back(MD->getSelfDecl()->getType());
82 ArgTys.push_back(Context.getObjCSelType());
83 // FIXME: Kill copy?
Chris Lattner9408eb12009-02-20 06:23:21 +000084 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbara8f02052008-09-08 21:33:45 +000085 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000086 ArgTys.push_back((*i)->getType());
87 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000088}
89
Daniel Dunbar34bda882009-02-02 23:23:47 +000090const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
91 const CallArgList &Args) {
92 // FIXME: Kill copy.
93 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000094 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
95 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000096 ArgTys.push_back(i->second);
97 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000098}
99
Daniel Dunbar34bda882009-02-02 23:23:47 +0000100const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
101 const FunctionArgList &Args) {
102 // FIXME: Kill copy.
103 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000104 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
105 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +0000106 ArgTys.push_back(i->second);
107 return getFunctionInfo(ResTy, ArgTys);
108}
109
110const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
111 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000112 // Lookup or create unique function info.
113 llvm::FoldingSetNodeID ID;
114 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
115
116 void *InsertPos = 0;
117 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
118 if (FI)
119 return *FI;
120
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000121 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000122 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000123 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000124
125 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000126 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000127
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000128 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000129}
130
131/***/
132
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000133ABIInfo::~ABIInfo() {}
134
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000135void ABIArgInfo::dump() const {
136 fprintf(stderr, "(ABIArgInfo Kind=");
137 switch (TheKind) {
138 case Direct:
139 fprintf(stderr, "Direct");
140 break;
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000141 case Ignore:
142 fprintf(stderr, "Ignore");
143 break;
144 case Coerce:
145 fprintf(stderr, "Coerce Type=");
146 getCoerceToType()->print(llvm::errs());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000147 break;
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000148 case Indirect:
149 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000150 break;
151 case Expand:
152 fprintf(stderr, "Expand");
153 break;
154 }
155 fprintf(stderr, ")\n");
156}
157
158/***/
159
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000160static bool isEmptyRecord(ASTContext &Context, QualType T);
161
162/// isEmptyField - Return true iff a the field is "empty", that is it
163/// is an unnamed bit-field or an (array of) empty record(s).
164static bool isEmptyField(ASTContext &Context, const FieldDecl *FD) {
165 if (FD->isUnnamedBitfield())
166 return true;
167
168 QualType FT = FD->getType();
Daniel Dunbar46bf0102009-05-11 23:01:34 +0000169 // Constant arrays of empty records count as empty, strip them off.
170 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
171 FT = AT->getElementType();
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000172
173 return isEmptyRecord(Context, FT);
174}
175
176/// isEmptyRecord - Return true iff a structure contains only empty
177/// fields. Note that a structure with a flexible array member is not
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000178/// considered empty.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000179static bool isEmptyRecord(ASTContext &Context, QualType T) {
Daniel Dunbara7446422009-03-31 19:01:39 +0000180 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000181 if (!RT)
182 return 0;
183 const RecordDecl *RD = RT->getDecl();
184 if (RD->hasFlexibleArrayMember())
185 return false;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000186 for (RecordDecl::field_iterator i = RD->field_begin(Context),
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000187 e = RD->field_end(Context); i != e; ++i)
188 if (!isEmptyField(Context, *i))
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000189 return false;
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000190 return true;
191}
192
193/// isSingleElementStruct - Determine if a structure is a "single
194/// element struct", i.e. it has exactly one non-empty field or
195/// exactly one field which is itself a single element
196/// struct. Structures with flexible array members are never
197/// considered single element structs.
198///
199/// \return The field declaration for the single non-empty field, if
200/// it exists.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000201static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000202 const RecordType *RT = T->getAsStructureType();
203 if (!RT)
204 return 0;
205
206 const RecordDecl *RD = RT->getDecl();
207 if (RD->hasFlexibleArrayMember())
208 return 0;
209
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000210 const Type *Found = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(Context),
212 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000213 const FieldDecl *FD = *i;
214 QualType FT = FD->getType();
215
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000216 // Ignore empty fields.
217 if (isEmptyField(Context, FD))
218 continue;
219
Daniel Dunbar46bf0102009-05-11 23:01:34 +0000220 // If we already found an element then this isn't a single-element
221 // struct.
Daniel Dunbar6f3d7602009-05-08 21:04:47 +0000222 if (Found)
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000223 return 0;
Daniel Dunbar6f3d7602009-05-08 21:04:47 +0000224
Daniel Dunbar46bf0102009-05-11 23:01:34 +0000225 // Treat single element arrays as the element.
226 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
227 if (AT->getSize().getZExtValue() != 1)
228 break;
229 FT = AT->getElementType();
230 }
231
Daniel Dunbar6f3d7602009-05-08 21:04:47 +0000232 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000233 Found = FT.getTypePtr();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000234 } else {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000235 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000236 if (!Found)
237 return 0;
238 }
239 }
240
241 return Found;
242}
243
244static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
245 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
246 return false;
247
248 uint64_t Size = Context.getTypeSize(Ty);
249 return Size == 32 || Size == 64;
250}
251
252static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
253 ASTContext &Context) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000254 for (RecordDecl::field_iterator i = RD->field_begin(Context),
255 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000256 const FieldDecl *FD = *i;
257
258 if (!is32Or64BitBasicType(FD->getType(), Context))
259 return false;
260
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000261 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
262 // how to expand them yet, and the predicate for telling if a bitfield still
263 // counts as "basic" is more complicated than what we were doing previously.
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000264 if (FD->isBitField())
265 return false;
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000266 }
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000267
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000268 return true;
269}
270
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000271namespace {
272/// DefaultABIInfo - The default implementation for ABI specific
273/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000274/// self-consistent and sensible LLVM IR generation, but does not
275/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000276class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000277 ABIArgInfo classifyReturnType(QualType RetTy,
278 ASTContext &Context) const;
279
280 ABIArgInfo classifyArgumentType(QualType RetTy,
281 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000282
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000283 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
284 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
285 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
286 it != ie; ++it)
287 it->info = classifyArgumentType(it->type, Context);
288 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000289
290 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
291 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000292};
293
294/// X86_32ABIInfo - The X86-32 ABI information.
295class X86_32ABIInfo : public ABIInfo {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000296 ASTContext &Context;
Eli Friedman5e175802009-03-23 23:26:24 +0000297 bool IsDarwin;
298
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000299 static bool isRegisterSize(unsigned Size) {
300 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
301 }
302
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000303 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
304
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000305public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000306 ABIArgInfo classifyReturnType(QualType RetTy,
307 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000308
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000309 ABIArgInfo classifyArgumentType(QualType RetTy,
310 ASTContext &Context) const;
311
312 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
313 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
314 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
315 it != ie; ++it)
316 it->info = classifyArgumentType(it->type, Context);
317 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000318
319 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
320 CodeGenFunction &CGF) const;
Eli Friedman5e175802009-03-23 23:26:24 +0000321
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000322 X86_32ABIInfo(ASTContext &Context, bool d)
323 : ABIInfo(), Context(Context), IsDarwin(d) {}
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000324};
325}
326
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000327
328/// shouldReturnTypeInRegister - Determine if the given type should be
329/// passed in a register (for the Darwin ABI).
330bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
331 ASTContext &Context) {
332 uint64_t Size = Context.getTypeSize(Ty);
333
334 // Type must be register sized.
335 if (!isRegisterSize(Size))
336 return false;
337
338 if (Ty->isVectorType()) {
339 // 64- and 128- bit vectors inside structures are not returned in
340 // registers.
341 if (Size == 64 || Size == 128)
342 return false;
343
344 return true;
345 }
346
347 // If this is a builtin, pointer, or complex type, it is ok.
348 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
349 return true;
350
351 // Arrays are treated like records.
352 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
353 return shouldReturnTypeInRegister(AT->getElementType(), Context);
354
355 // Otherwise, it must be a record type.
356 const RecordType *RT = Ty->getAsRecordType();
357 if (!RT) return false;
358
359 // Structure types are passed in register if all fields would be
360 // passed in a register.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000361 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context),
362 e = RT->getDecl()->field_end(Context); i != e; ++i) {
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000363 const FieldDecl *FD = *i;
364
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000365 // Empty fields are ignored.
366 if (isEmptyField(Context, FD))
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000367 continue;
368
369 // Check fields recursively.
370 if (!shouldReturnTypeInRegister(FD->getType(), Context))
371 return false;
372 }
373
374 return true;
375}
376
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000377ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
378 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000379 if (RetTy->isVoidType()) {
380 return ABIArgInfo::getIgnore();
Daniel Dunbar2a7bb3f2009-04-01 06:13:08 +0000381 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
382 // On Darwin, some vectors are returned in registers.
383 if (IsDarwin) {
384 uint64_t Size = Context.getTypeSize(RetTy);
385
386 // 128-bit vectors are a special case; they are returned in
387 // registers and we need to make sure to pick a type the LLVM
388 // backend will like.
389 if (Size == 128)
390 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
391 2));
392
393 // Always return in register if it fits in a general purpose
394 // register, or if it is 64 bits and has a single element.
395 if ((Size == 8 || Size == 16 || Size == 32) ||
396 (Size == 64 && VT->getNumElements() == 1))
397 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
398
399 return ABIArgInfo::getIndirect(0);
400 }
401
402 return ABIArgInfo::getDirect();
Daniel Dunbareec02622009-02-03 06:30:17 +0000403 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaref495d42009-04-27 18:31:32 +0000404 // Structures with flexible arrays are always indirect.
405 if (const RecordType *RT = RetTy->getAsStructureType())
406 if (RT->getDecl()->hasFlexibleArrayMember())
407 return ABIArgInfo::getIndirect(0);
408
Eli Friedman5e175802009-03-23 23:26:24 +0000409 // Outside of Darwin, structs and unions are always indirect.
410 if (!IsDarwin && !RetTy->isAnyComplexType())
411 return ABIArgInfo::getIndirect(0);
Daniel Dunbaref495d42009-04-27 18:31:32 +0000412
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000413 // Classify "single element" structs as their element type.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000414 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000415 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000416 if (BT->isIntegerType()) {
Daniel Dunbar9f4a5a42009-05-08 21:30:11 +0000417 // We need to use the size of the structure, padding
418 // bit-fields can adjust that to be larger than the single
419 // element type.
420 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000421 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
422 } else if (BT->getKind() == BuiltinType::Float) {
Daniel Dunbar9f4a5a42009-05-08 21:30:11 +0000423 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
424 "Unexpect single element structure size!");
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000425 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
426 } else if (BT->getKind() == BuiltinType::Double) {
Daniel Dunbar9f4a5a42009-05-08 21:30:11 +0000427 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
428 "Unexpect single element structure size!");
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000429 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
430 }
431 } else if (SeltTy->isPointerType()) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000432 // FIXME: It would be really nice if this could come out as the proper
433 // pointer type.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000434 llvm::Type *PtrTy =
435 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
436 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000437 } else if (SeltTy->isVectorType()) {
438 // 64- and 128-bit vectors are never returned in a
439 // register when inside a structure.
440 uint64_t Size = Context.getTypeSize(RetTy);
441 if (Size == 64 || Size == 128)
442 return ABIArgInfo::getIndirect(0);
443
444 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000445 }
446 }
447
Daniel Dunbar0e727132009-05-12 17:00:20 +0000448 // Small structures which are register sized are generally returned
449 // in a register.
450 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
451 uint64_t Size = Context.getTypeSize(RetTy);
452 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000453 }
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000454
455 return ABIArgInfo::getIndirect(0);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000456 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000457 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000458 }
459}
460
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000461ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000462 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000463 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000464 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000465 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000466 if (const RecordType *RT = Ty->getAsStructureType())
467 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000468 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000469
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000470 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000471 uint64_t Size = Context.getTypeSize(Ty);
472 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000473 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000474
475 // Expand structs with size <= 128-bits which consist only of
476 // basic types (int, long long, float, double, xxx*). This is
477 // non-recursive and does not ignore empty fields.
478 if (const RecordType *RT = Ty->getAsStructureType()) {
479 if (Context.getTypeSize(Ty) <= 4*32 &&
480 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
481 return ABIArgInfo::getExpand();
482 }
483
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000484 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000485 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000486 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000487 }
488}
489
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000490llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
491 CodeGenFunction &CGF) const {
492 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
493 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
494
495 CGBuilderTy &Builder = CGF.Builder;
496 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
497 "ap");
498 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
499 llvm::Type *PTy =
500 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
501 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
502
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000503 uint64_t Offset =
504 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000505 llvm::Value *NextAddr =
506 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000507 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000508 "ap.next");
509 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
510
511 return AddrTyped;
512}
513
Daniel Dunbare09a9692009-01-24 08:32:22 +0000514namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000515/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000516class X86_64ABIInfo : public ABIInfo {
517 enum Class {
518 Integer = 0,
519 SSE,
520 SSEUp,
521 X87,
522 X87Up,
523 ComplexX87,
524 NoClass,
525 Memory
526 };
527
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000528 /// merge - Implement the X86_64 ABI merging algorithm.
529 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000530 /// Merge an accumulating classification \arg Accum with a field
531 /// classification \arg Field.
532 ///
533 /// \param Accum - The accumulating classification. This should
534 /// always be either NoClass or the result of a previous merge
535 /// call. In addition, this should never be Memory (the caller
536 /// should just return Memory for the aggregate).
537 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000538
Daniel Dunbare09a9692009-01-24 08:32:22 +0000539 /// classify - Determine the x86_64 register classes in which the
540 /// given type T should be passed.
541 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000542 /// \param Lo - The classification for the parts of the type
543 /// residing in the low word of the containing object.
544 ///
545 /// \param Hi - The classification for the parts of the type
546 /// residing in the high word of the containing object.
547 ///
548 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000549 /// containing object. Some parameters are classified different
550 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000551 ///
552 /// If a word is unused its result will be NoClass; if a type should
553 /// be passed in Memory then at least the classification of \arg Lo
554 /// will be Memory.
555 ///
556 /// The \arg Lo class will be NoClass iff the argument is ignored.
557 ///
558 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar92e88642009-02-17 07:55:55 +0000559 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000560 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000561 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000562
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000563 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
564 /// to coerce to, chose the best way to pass Ty in the same place
565 /// that \arg CoerceTo would be passed, but while keeping the
566 /// emitted code as simple as possible.
567 ///
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000568 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
569 /// the ways we might want to pass things, instead of constructing an LLVM
570 /// type. This makes this code more explicit, and it makes it clearer that we
571 /// are also doing this for correctness in the case of passing scalar types.
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000572 ABIArgInfo getCoerceResult(QualType Ty,
573 const llvm::Type *CoerceTo,
574 ASTContext &Context) const;
575
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000576 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000577 ASTContext &Context) const;
578
579 ABIArgInfo classifyArgumentType(QualType Ty,
580 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000581 unsigned &neededInt,
582 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000583
584public:
585 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000586
587 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
588 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000589};
590}
591
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000592X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
593 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000594 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
595 // classified recursively so that always two fields are
596 // considered. The resulting class is calculated according to
597 // the classes of the fields in the eightbyte:
598 //
599 // (a) If both classes are equal, this is the resulting class.
600 //
601 // (b) If one of the classes is NO_CLASS, the resulting class is
602 // the other class.
603 //
604 // (c) If one of the classes is MEMORY, the result is the MEMORY
605 // class.
606 //
607 // (d) If one of the classes is INTEGER, the result is the
608 // INTEGER.
609 //
610 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
611 // MEMORY is used as class.
612 //
613 // (f) Otherwise class SSE is used.
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000614
615 // Accum should never be memory (we should have returned) or
616 // ComplexX87 (because this cannot be passed in a structure).
617 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000618 "Invalid accumulated classification during merge.");
619 if (Accum == Field || Field == NoClass)
620 return Accum;
621 else if (Field == Memory)
622 return Memory;
623 else if (Accum == NoClass)
624 return Field;
625 else if (Accum == Integer || Field == Integer)
626 return Integer;
Daniel Dunbar647e30f2009-05-12 15:22:40 +0000627 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
628 Accum == X87 || Accum == X87Up)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000629 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000630 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000631 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000632}
633
Daniel Dunbare09a9692009-01-24 08:32:22 +0000634void X86_64ABIInfo::classify(QualType Ty,
635 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000636 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000637 Class &Lo, Class &Hi) const {
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000638 // FIXME: This code can be simplified by introducing a simple value class for
639 // Class pairs with appropriate constructor methods for the various
640 // situations.
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000641
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000642 // FIXME: Some of the split computations are wrong; unaligned vectors
643 // shouldn't be passed in registers for example, so there is no chance they
644 // can straddle an eightbyte. Verify & simplify.
Daniel Dunbard97f5952009-02-22 04:48:22 +0000645
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000646 Lo = Hi = NoClass;
647
648 Class &Current = OffsetBase < 64 ? Lo : Hi;
649 Current = Memory;
650
Daniel Dunbare09a9692009-01-24 08:32:22 +0000651 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
652 BuiltinType::Kind k = BT->getKind();
653
Daniel Dunbar1358b202009-01-26 21:26:08 +0000654 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000655 Current = NoClass;
Chris Lattner6cc7e412009-04-30 02:43:43 +0000656 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattner3d8e0682009-04-30 06:22:07 +0000657 Lo = Integer;
658 Hi = Integer;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000659 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000660 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000661 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000662 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000663 } else if (k == BuiltinType::LongDouble) {
664 Lo = X87;
665 Hi = X87Up;
666 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000667 // FIXME: _Decimal32 and _Decimal64 are SSE.
668 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson1d234462009-02-26 17:31:15 +0000669 } else if (const EnumType *ET = Ty->getAsEnumType()) {
670 // Classify the underlying integer type.
671 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000672 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000673 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000674 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000675 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000676 if (Size == 32) {
677 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
678 // float> as integer.
679 Current = Integer;
680
681 // If this type crosses an eightbyte boundary, it should be
682 // split.
683 uint64_t EB_Real = (OffsetBase) / 64;
684 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
685 if (EB_Real != EB_Imag)
686 Hi = Lo;
687 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000688 // gcc passes <1 x double> in memory. :(
689 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000690 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000691
692 // gcc passes <1 x long long> as INTEGER.
693 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
694 Current = Integer;
695 else
696 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000697
698 // If this type crosses an eightbyte boundary, it should be
699 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000700 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000701 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000702 } else if (Size == 128) {
703 Lo = SSE;
704 Hi = SSEUp;
705 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000706 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000707 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000708
Daniel Dunbare413f532009-01-30 18:40:10 +0000709 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000710 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000711 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000712 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000713 else if (Size <= 128)
714 Lo = Hi = Integer;
715 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000716 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000717 else if (ET == Context.DoubleTy)
718 Lo = Hi = SSE;
719 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000720 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000721
722 // If this complex type crosses an eightbyte boundary then it
723 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000724 uint64_t EB_Real = (OffsetBase) / 64;
725 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000726 if (Hi == NoClass && EB_Real != EB_Imag)
727 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000728 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
729 // Arrays are treated like structures.
730
731 uint64_t Size = Context.getTypeSize(Ty);
732
733 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
734 // than two eightbytes, ..., it has class MEMORY.
735 if (Size > 128)
736 return;
737
738 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
739 // fields, it has class MEMORY.
740 //
741 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000742 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000743 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000744
745 // Otherwise implement simplified merge. We could be smarter about
746 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000747 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000748 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
749 uint64_t ArraySize = AT->getSize().getZExtValue();
750 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
751 Class FieldLo, FieldHi;
752 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000753 Lo = merge(Lo, FieldLo);
754 Hi = merge(Hi, FieldHi);
755 if (Lo == Memory || Hi == Memory)
756 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000757 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000758
759 // Do post merger cleanup (see below). Only case we worry about is Memory.
760 if (Hi == Memory)
761 Lo = Memory;
762 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000763 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000764 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000765
766 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
767 // than two eightbytes, ..., it has class MEMORY.
768 if (Size > 128)
769 return;
770
771 const RecordDecl *RD = RT->getDecl();
772
773 // Assume variable sized types are passed in memory.
774 if (RD->hasFlexibleArrayMember())
775 return;
776
777 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
778
779 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000780 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000781 unsigned idx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000782 for (RecordDecl::field_iterator i = RD->field_begin(Context),
783 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000784 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000785 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000786
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000787 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
788 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000789 //
Daniel Dunbaref495d42009-04-27 18:31:32 +0000790 // Note, skip this test for bit-fields, see below.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000791 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000792 Lo = Memory;
793 return;
794 }
795
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000796 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000797 //
798 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
799 // exceeds a single eightbyte, each is classified
800 // separately. Each eightbyte gets initialized to class
801 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000802 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000803
Daniel Dunbaref495d42009-04-27 18:31:32 +0000804 // Bit-fields require special handling, they do not force the
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000805 // structure to be passed in memory even if unaligned, and
806 // therefore they can straddle an eightbyte.
807 if (BitField) {
Daniel Dunbar9bb29952009-05-08 22:26:44 +0000808 // Ignore padding bit-fields.
809 if (i->isUnnamedBitfield())
810 continue;
811
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000812 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman5255e7a2009-04-26 19:19:15 +0000813 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000814
815 uint64_t EB_Lo = Offset / 64;
816 uint64_t EB_Hi = (Offset + Size - 1) / 64;
817 FieldLo = FieldHi = NoClass;
818 if (EB_Lo) {
819 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
820 FieldLo = NoClass;
821 FieldHi = Integer;
822 } else {
823 FieldLo = Integer;
824 FieldHi = EB_Hi ? Integer : NoClass;
825 }
826 } else
827 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000828 Lo = merge(Lo, FieldLo);
829 Hi = merge(Hi, FieldHi);
830 if (Lo == Memory || Hi == Memory)
831 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000832 }
833
834 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
835 //
836 // (a) If one of the classes is MEMORY, the whole argument is
837 // passed in memory.
838 //
839 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
840
841 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000842 // the merge (just bail).
843 //
844 // The second condition occurs in the case of unions; for example
845 // union { _Complex double; unsigned; }.
846 if (Hi == Memory)
847 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000848 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000849 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000850 }
851}
852
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000853ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
854 const llvm::Type *CoerceTo,
855 ASTContext &Context) const {
856 if (CoerceTo == llvm::Type::Int64Ty) {
857 // Integer and pointer types will end up in a general purpose
858 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000859 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000860 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000861
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000862 } else if (CoerceTo == llvm::Type::DoubleTy) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000863 // FIXME: It would probably be better to make CGFunctionInfo only map using
864 // canonical types than to canonize here.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000865 QualType CTy = Context.getCanonicalType(Ty);
866
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000867 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000868 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000869 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000870
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000871 }
872
873 return ABIArgInfo::getCoerce(CoerceTo);
874}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000875
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000876ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
877 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000878 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
879 // classification algorithm.
880 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000881 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000882
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000883 // Check some invariants.
884 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
885 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
886 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
887
Daniel Dunbare09a9692009-01-24 08:32:22 +0000888 const llvm::Type *ResType = 0;
889 switch (Lo) {
890 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000891 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000892
893 case SSEUp:
894 case X87Up:
895 assert(0 && "Invalid classification for lo word.");
896
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000897 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000898 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000899 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000900 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000901
902 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
903 // available register of the sequence %rax, %rdx is used.
904 case Integer:
905 ResType = llvm::Type::Int64Ty; break;
906
907 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
908 // available SSE register of the sequence %xmm0, %xmm1 is used.
909 case SSE:
910 ResType = llvm::Type::DoubleTy; break;
911
912 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
913 // returned on the X87 stack in %st0 as 80-bit x87 number.
914 case X87:
915 ResType = llvm::Type::X86_FP80Ty; break;
916
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000917 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
918 // part of the value is returned in %st0 and the imaginary part in
919 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000920 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000921 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000922 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
923 llvm::Type::X86_FP80Ty,
924 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000925 break;
926 }
927
928 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000929 // Memory was handled previously and X87 should
930 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000931 case Memory:
932 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000933 assert(0 && "Invalid classification for hi word.");
934
Daniel Dunbar92e88642009-02-17 07:55:55 +0000935 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000936 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000937
Daniel Dunbare09a9692009-01-24 08:32:22 +0000938 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000939 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
940 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000941 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000942 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
943 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000944
945 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
946 // is passed in the upper half of the last used SSE register.
947 //
948 // SSEUP should always be preceeded by SSE, just widen.
949 case SSEUp:
950 assert(Lo == SSE && "Unexpected SSEUp classification.");
951 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
952 break;
953
954 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000955 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000956 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000957 // If X87Up is preceeded by X87, we don't need to do
958 // anything. However, in some cases with unions it may not be
959 // preceeded by X87. In such situations we follow gcc and pass the
960 // extra bits in an SSE reg.
961 if (Lo != X87)
962 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000963 break;
964 }
965
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000966 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000967}
968
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000969ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000970 unsigned &neededInt,
971 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000972 X86_64ABIInfo::Class Lo, Hi;
973 classify(Ty, Context, 0, Lo, Hi);
974
975 // Check some invariants.
976 // FIXME: Enforce these by construction.
977 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
978 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
979 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
980
Daniel Dunbare978cb92009-02-10 17:06:09 +0000981 neededInt = 0;
982 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000983 const llvm::Type *ResType = 0;
984 switch (Lo) {
985 case NoClass:
986 return ABIArgInfo::getIgnore();
987
988 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
989 // on the stack.
990 case Memory:
991
992 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
993 // COMPLEX_X87, it is passed in memory.
994 case X87:
995 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000996 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000997
998 case SSEUp:
999 case X87Up:
1000 assert(0 && "Invalid classification for lo word.");
1001
1002 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1003 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1004 // and %r9 is used.
1005 case Integer:
1006 ++neededInt;
1007 ResType = llvm::Type::Int64Ty;
1008 break;
1009
1010 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1011 // available SSE register is used, the registers are taken in the
1012 // order from %xmm0 to %xmm7.
1013 case SSE:
1014 ++neededSSE;
1015 ResType = llvm::Type::DoubleTy;
1016 break;
Daniel Dunbareec02622009-02-03 06:30:17 +00001017 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001018
1019 switch (Hi) {
1020 // Memory was handled previously, ComplexX87 and X87 should
1021 // never occur as hi classes, and X87Up must be preceed by X87,
1022 // which is passed in memory.
1023 case Memory:
1024 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001025 case ComplexX87:
1026 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001027 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001028
1029 case NoClass: break;
1030 case Integer:
1031 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1032 ++neededInt;
1033 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001034
1035 // X87Up generally doesn't occur here (long double is passed in
1036 // memory), except in situations involving unions.
1037 case X87Up:
1038 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001039 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1040 ++neededSSE;
1041 break;
1042
1043 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1044 // eightbyte is passed in the upper half of the last used SSE
1045 // register.
1046 case SSEUp:
1047 assert(Lo == SSE && "Unexpected SSEUp classification.");
1048 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1049 break;
1050 }
1051
Daniel Dunbar87c4dc92009-02-14 02:09:24 +00001052 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001053}
1054
1055void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1056 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1057
1058 // Keep track of the number of assigned registers.
1059 unsigned freeIntRegs = 6, freeSSERegs = 8;
1060
1061 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1062 // get assigned (in left-to-right order) for passing as follows...
1063 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +00001064 it != ie; ++it) {
1065 unsigned neededInt, neededSSE;
1066 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1067
1068 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1069 // eightbyte of an argument, the whole argument is passed on the
1070 // stack. If registers have already been assigned for some
1071 // eightbytes of such an argument, the assignments get reverted.
1072 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1073 freeIntRegs -= neededInt;
1074 freeSSERegs -= neededSSE;
1075 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001076 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +00001077 }
1078 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001079}
1080
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001081static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1082 QualType Ty,
1083 CodeGenFunction &CGF) {
1084 llvm::Value *overflow_arg_area_p =
1085 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1086 llvm::Value *overflow_arg_area =
1087 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1088
1089 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1090 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001091 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001092 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001093 // Note that we follow the ABI & gcc here, even though the type
1094 // could in theory have an alignment greater than 16. This case
1095 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001096
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001097 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1098 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1099 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1100 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1101 llvm::Type::Int64Ty);
1102 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1103 overflow_arg_area =
1104 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1105 overflow_arg_area->getType(),
1106 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001107 }
1108
1109 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1110 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1111 llvm::Value *Res =
1112 CGF.Builder.CreateBitCast(overflow_arg_area,
1113 llvm::PointerType::getUnqual(LTy));
1114
1115 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1116 // l->overflow_arg_area + sizeof(type).
1117 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1118 // an 8 byte boundary.
1119
1120 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1121 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1122 (SizeInBytes + 7) & ~7);
1123 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1124 "overflow_arg_area.next");
1125 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1126
1127 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1128 return Res;
1129}
1130
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001131llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1132 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001133 // Assume that va_list type is correct; should be pointer to LLVM type:
1134 // struct {
1135 // i32 gp_offset;
1136 // i32 fp_offset;
1137 // i8* overflow_arg_area;
1138 // i8* reg_save_area;
1139 // };
1140 unsigned neededInt, neededSSE;
1141 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1142 neededInt, neededSSE);
1143
1144 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1145 // in the registers. If not go to step 7.
1146 if (!neededInt && !neededSSE)
1147 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1148
1149 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1150 // general purpose registers needed to pass type and num_fp to hold
1151 // the number of floating point registers needed.
1152
1153 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1154 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1155 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1156 //
1157 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1158 // register save space).
1159
1160 llvm::Value *InRegs = 0;
1161 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1162 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1163 if (neededInt) {
1164 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1165 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1166 InRegs =
1167 CGF.Builder.CreateICmpULE(gp_offset,
1168 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1169 48 - neededInt * 8),
1170 "fits_in_gp");
1171 }
1172
1173 if (neededSSE) {
1174 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1175 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1176 llvm::Value *FitsInFP =
1177 CGF.Builder.CreateICmpULE(fp_offset,
1178 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001179 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001180 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001181 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001182 }
1183
1184 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1185 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1186 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1187 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1188
1189 // Emit code to load the value if it was passed in registers.
1190
1191 CGF.EmitBlock(InRegBlock);
1192
1193 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1194 // an offset of l->gp_offset and/or l->fp_offset. This may require
1195 // copying to a temporary location in case the parameter is passed
1196 // in different register classes or requires an alignment greater
1197 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001198 //
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001199 // FIXME: This really results in shameful code when we end up needing to
1200 // collect arguments from different places; often what should result in a
1201 // simple assembling of a structure from scattered addresses has many more
1202 // loads than necessary. Can we clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001203 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1204 llvm::Value *RegAddr =
1205 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1206 "reg_save_area");
1207 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001208 // FIXME: Cleanup.
1209 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1210 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1211 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1212 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1213 const llvm::Type *TyLo = ST->getElementType(0);
1214 const llvm::Type *TyHi = ST->getElementType(1);
1215 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1216 "Unexpected ABI info for mixed regs");
1217 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1218 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1219 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1220 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1221 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1222 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1223 llvm::Value *V =
1224 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1225 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1226 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1227 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1228
1229 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001230 } else if (neededInt) {
1231 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1232 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1233 llvm::PointerType::getUnqual(LTy));
1234 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001235 if (neededSSE == 1) {
1236 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1237 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1238 llvm::PointerType::getUnqual(LTy));
1239 } else {
1240 assert(neededSSE == 2 && "Invalid number of needed registers!");
1241 // SSE registers are spaced 16 bytes apart in the register save
1242 // area, we need to collect the two eightbytes together.
1243 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1244 llvm::Value *RegAddrHi =
1245 CGF.Builder.CreateGEP(RegAddrLo,
1246 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1247 const llvm::Type *DblPtrTy =
1248 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1249 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1250 llvm::Type::DoubleTy,
1251 NULL);
1252 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1253 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1254 DblPtrTy));
1255 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1256 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1257 DblPtrTy));
1258 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1259 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1260 llvm::PointerType::getUnqual(LTy));
1261 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001262 }
1263
1264 // AMD64-ABI 3.5.7p5: Step 5. Set:
1265 // l->gp_offset = l->gp_offset + num_gp * 8
1266 // l->fp_offset = l->fp_offset + num_fp * 16.
1267 if (neededInt) {
1268 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1269 neededInt * 8);
1270 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1271 gp_offset_p);
1272 }
1273 if (neededSSE) {
1274 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1275 neededSSE * 16);
1276 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1277 fp_offset_p);
1278 }
1279 CGF.EmitBranch(ContBlock);
1280
1281 // Emit code to load the value if it was passed in memory.
1282
1283 CGF.EmitBlock(InMemBlock);
1284 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1285
1286 // Return the appropriate result.
1287
1288 CGF.EmitBlock(ContBlock);
1289 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1290 "vaarg.addr");
1291 ResAddr->reserveOperandSpace(2);
1292 ResAddr->addIncoming(RegAddr, InRegBlock);
1293 ResAddr->addIncoming(MemAddr, InMemBlock);
1294
1295 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001296}
1297
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001298// ABI Info for PIC16
1299class PIC16ABIInfo : public ABIInfo {
1300 ABIArgInfo classifyReturnType(QualType RetTy,
1301 ASTContext &Context) const;
1302
1303 ABIArgInfo classifyArgumentType(QualType RetTy,
1304 ASTContext &Context) const;
1305
1306 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1307 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1308 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1309 it != ie; ++it)
1310 it->info = classifyArgumentType(it->type, Context);
1311 }
1312
1313 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1314 CodeGenFunction &CGF) const;
1315
1316};
1317
1318ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1319 ASTContext &Context) const {
1320 if (RetTy->isVoidType()) {
1321 return ABIArgInfo::getIgnore();
1322 } else {
1323 return ABIArgInfo::getDirect();
1324 }
1325}
1326
1327ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1328 ASTContext &Context) const {
1329 return ABIArgInfo::getDirect();
1330}
1331
1332llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1333 CodeGenFunction &CGF) const {
1334 return 0;
1335}
1336
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001337class ARMABIInfo : public ABIInfo {
1338 ABIArgInfo classifyReturnType(QualType RetTy,
1339 ASTContext &Context) const;
1340
1341 ABIArgInfo classifyArgumentType(QualType RetTy,
1342 ASTContext &Context) const;
1343
1344 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1345
1346 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1347 CodeGenFunction &CGF) const;
1348};
1349
1350void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1351 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1352 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1353 it != ie; ++it) {
1354 it->info = classifyArgumentType(it->type, Context);
1355 }
1356}
1357
1358ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1359 ASTContext &Context) const {
1360 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1361 return ABIArgInfo::getDirect();
1362 }
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001363 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1364 // backend doesn't support byval.
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001365 // FIXME: This doesn't handle alignment > 64 bits.
1366 const llvm::Type* ElemTy;
1367 unsigned SizeRegs;
1368 if (Context.getTypeAlign(Ty) > 32) {
1369 ElemTy = llvm::Type::Int64Ty;
1370 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1371 } else {
1372 ElemTy = llvm::Type::Int32Ty;
1373 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1374 }
1375 std::vector<const llvm::Type*> LLVMFields;
1376 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1377 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1378 return ABIArgInfo::getCoerce(STy);
1379}
1380
1381ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1382 ASTContext &Context) const {
1383 if (RetTy->isVoidType()) {
1384 return ABIArgInfo::getIgnore();
1385 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1386 // Aggregates <= 4 bytes are returned in r0; other aggregates
1387 // are returned indirectly.
1388 uint64_t Size = Context.getTypeSize(RetTy);
1389 if (Size <= 32)
1390 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1391 return ABIArgInfo::getIndirect(0);
1392 } else {
1393 return ABIArgInfo::getDirect();
1394 }
1395}
1396
1397llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1398 CodeGenFunction &CGF) const {
1399 // FIXME: Need to handle alignment
1400 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1401 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1402
1403 CGBuilderTy &Builder = CGF.Builder;
1404 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1405 "ap");
1406 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1407 llvm::Type *PTy =
1408 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1409 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1410
1411 uint64_t Offset =
1412 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1413 llvm::Value *NextAddr =
1414 Builder.CreateGEP(Addr,
1415 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1416 "ap.next");
1417 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1418
1419 return AddrTyped;
1420}
1421
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001422ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001423 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001424 if (RetTy->isVoidType()) {
1425 return ABIArgInfo::getIgnore();
1426 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001427 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001428 } else {
1429 return ABIArgInfo::getDirect();
1430 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001431}
1432
1433ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001434 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001435 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001436 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001437 } else {
1438 return ABIArgInfo::getDirect();
1439 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001440}
1441
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001442llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1443 CodeGenFunction &CGF) const {
1444 return 0;
1445}
1446
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001447const ABIInfo &CodeGenTypes::getABIInfo() const {
1448 if (TheABIInfo)
1449 return *TheABIInfo;
1450
1451 // For now we just cache this in the CodeGenTypes and don't bother
1452 // to free it.
1453 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1454 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman5e175802009-03-23 23:26:24 +00001455 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001456 switch (getContext().Target.getPointerWidth(0)) {
1457 case 32:
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001458 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001459 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001460 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001461 }
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001462 } else if (strcmp(TargetPrefix, "arm") == 0) {
1463 // FIXME: Support for OABI?
1464 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001465 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1466 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001467 }
1468
1469 return *(TheABIInfo = new DefaultABIInfo);
1470}
1471
Daniel Dunbare126ab12008-09-10 02:41:04 +00001472/***/
1473
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001474CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1475 const llvm::SmallVector<QualType, 16> &ArgTys) {
1476 NumArgs = ArgTys.size();
1477 Args = new ArgInfo[1 + NumArgs];
1478 Args[0].type = ResTy;
1479 for (unsigned i = 0; i < NumArgs; ++i)
1480 Args[1 + i].type = ArgTys[i];
1481}
1482
1483/***/
1484
Daniel Dunbar04d35782008-09-17 00:51:38 +00001485void CodeGenTypes::GetExpandedTypes(QualType Ty,
1486 std::vector<const llvm::Type*> &ArgTys) {
1487 const RecordType *RT = Ty->getAsStructureType();
1488 assert(RT && "Can only expand structure types.");
1489 const RecordDecl *RD = RT->getDecl();
1490 assert(!RD->hasFlexibleArrayMember() &&
1491 "Cannot expand structure with flexible array.");
1492
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001493 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1494 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001495 const FieldDecl *FD = *i;
1496 assert(!FD->isBitField() &&
1497 "Cannot expand structure with bit-field members.");
1498
1499 QualType FT = FD->getType();
1500 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1501 GetExpandedTypes(FT, ArgTys);
1502 } else {
1503 ArgTys.push_back(ConvertType(FT));
1504 }
1505 }
1506}
1507
1508llvm::Function::arg_iterator
1509CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1510 llvm::Function::arg_iterator AI) {
1511 const RecordType *RT = Ty->getAsStructureType();
1512 assert(RT && "Can only expand structure types.");
1513
1514 RecordDecl *RD = RT->getDecl();
1515 assert(LV.isSimple() &&
1516 "Unexpected non-simple lvalue during struct expansion.");
1517 llvm::Value *Addr = LV.getAddress();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001518 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1519 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001520 FieldDecl *FD = *i;
1521 QualType FT = FD->getType();
1522
1523 // FIXME: What are the right qualifiers here?
1524 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1525 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1526 AI = ExpandTypeFromArgs(FT, LV, AI);
1527 } else {
1528 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1529 ++AI;
1530 }
1531 }
1532
1533 return AI;
1534}
1535
1536void
1537CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1538 llvm::SmallVector<llvm::Value*, 16> &Args) {
1539 const RecordType *RT = Ty->getAsStructureType();
1540 assert(RT && "Can only expand structure types.");
1541
1542 RecordDecl *RD = RT->getDecl();
1543 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1544 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001545 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1546 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001547 FieldDecl *FD = *i;
1548 QualType FT = FD->getType();
1549
1550 // FIXME: What are the right qualifiers here?
1551 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1552 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1553 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1554 } else {
1555 RValue RV = EmitLoadOfLValue(LV, FT);
1556 assert(RV.isScalar() &&
1557 "Unexpected non-scalar rvalue during struct expansion.");
1558 Args.push_back(RV.getScalarVal());
1559 }
1560 }
1561}
1562
Daniel Dunbar84379912009-02-02 19:06:38 +00001563/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1564/// a pointer to an object of type \arg Ty.
1565///
1566/// This safely handles the case when the src type is smaller than the
1567/// destination type; in this situation the values of bits which not
1568/// present in the src are undefined.
1569static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1570 const llvm::Type *Ty,
1571 CodeGenFunction &CGF) {
1572 const llvm::Type *SrcTy =
1573 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001574 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1575 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar84379912009-02-02 19:06:38 +00001576
Daniel Dunbar77071992009-02-03 05:59:18 +00001577 // If load is legal, just bitcast the src pointer.
Daniel Dunbar93317922009-05-13 18:54:26 +00001578 if (SrcSize >= DstSize) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001579 // Generally SrcSize is never greater than DstSize, since this means we are
1580 // losing bits. However, this can happen in cases where the structure has
1581 // additional padding, for example due to a user specified alignment.
Daniel Dunbar93317922009-05-13 18:54:26 +00001582 //
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001583 // FIXME: Assert that we aren't truncating non-padding bits when have access
1584 // to that information.
Daniel Dunbar84379912009-02-02 19:06:38 +00001585 llvm::Value *Casted =
1586 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001587 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1588 // FIXME: Use better alignment / avoid requiring aligned load.
1589 Load->setAlignment(1);
1590 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001591 } else {
Daniel Dunbar84379912009-02-02 19:06:38 +00001592 // Otherwise do coercion through memory. This is stupid, but
1593 // simple.
1594 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1595 llvm::Value *Casted =
1596 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001597 llvm::StoreInst *Store =
1598 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1599 // FIXME: Use better alignment / avoid requiring aligned store.
1600 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001601 return CGF.Builder.CreateLoad(Tmp);
1602 }
1603}
1604
1605/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1606/// where the source and destination may have different types.
1607///
1608/// This safely handles the case when the src type is larger than the
1609/// destination type; the upper bits of the src will be lost.
1610static void CreateCoercedStore(llvm::Value *Src,
1611 llvm::Value *DstPtr,
1612 CodeGenFunction &CGF) {
1613 const llvm::Type *SrcTy = Src->getType();
1614 const llvm::Type *DstTy =
1615 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1616
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001617 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1618 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar84379912009-02-02 19:06:38 +00001619
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001620 // If store is legal, just bitcast the src pointer.
Daniel Dunbar93317922009-05-13 18:54:26 +00001621 if (SrcSize >= DstSize) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001622 // Generally SrcSize is never greater than DstSize, since this means we are
1623 // losing bits. However, this can happen in cases where the structure has
1624 // additional padding, for example due to a user specified alignment.
Daniel Dunbar93317922009-05-13 18:54:26 +00001625 //
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001626 // FIXME: Assert that we aren't truncating non-padding bits when have access
1627 // to that information.
Daniel Dunbar84379912009-02-02 19:06:38 +00001628 llvm::Value *Casted =
1629 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001630 // FIXME: Use better alignment / avoid requiring aligned store.
1631 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001632 } else {
Daniel Dunbar84379912009-02-02 19:06:38 +00001633 // Otherwise do coercion through memory. This is stupid, but
1634 // simple.
1635 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1636 CGF.Builder.CreateStore(Src, Tmp);
1637 llvm::Value *Casted =
1638 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001639 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1640 // FIXME: Use better alignment / avoid requiring aligned load.
1641 Load->setAlignment(1);
1642 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001643 }
1644}
1645
Daniel Dunbar04d35782008-09-17 00:51:38 +00001646/***/
1647
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001648bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001649 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001650}
1651
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001652const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001653CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001654 std::vector<const llvm::Type*> ArgTys;
1655
1656 const llvm::Type *ResultType = 0;
1657
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001658 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001659 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001660 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001661 case ABIArgInfo::Expand:
1662 assert(0 && "Invalid ABI kind for return argument");
1663
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001664 case ABIArgInfo::Direct:
1665 ResultType = ConvertType(RetTy);
1666 break;
1667
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001668 case ABIArgInfo::Indirect: {
1669 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001670 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001671 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001672 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1673 break;
1674 }
1675
Daniel Dunbar1358b202009-01-26 21:26:08 +00001676 case ABIArgInfo::Ignore:
1677 ResultType = llvm::Type::VoidTy;
1678 break;
1679
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001680 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001681 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001682 break;
1683 }
1684
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001685 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1686 ie = FI.arg_end(); it != ie; ++it) {
1687 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001688
1689 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001690 case ABIArgInfo::Ignore:
1691 break;
1692
Daniel Dunbar04d35782008-09-17 00:51:38 +00001693 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001694 ArgTys.push_back(AI.getCoerceToType());
1695 break;
1696
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001697 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001698 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001699 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1700 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001701 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001702 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001703
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001704 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001705 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001706 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001707
1708 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001709 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001710 break;
1711 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001712 }
1713
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001714 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001715}
1716
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001717void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001718 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001719 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001720 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001721 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001722
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001723 // FIXME: handle sseregparm someday...
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001724 if (TargetDecl) {
Daniel Dunbar78582862009-04-13 21:08:27 +00001725 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001726 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar78582862009-04-13 21:08:27 +00001727 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001728 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbar78582862009-04-13 21:08:27 +00001729 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssondd6791c2008-10-05 23:32:53 +00001730 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar78582862009-04-13 21:08:27 +00001731 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar521c3a32009-04-10 22:14:52 +00001732 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001733 }
1734
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001735 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001736 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001737 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001738 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001739 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001740 if (RetTy->isPromotableIntegerType()) {
1741 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001742 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001743 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001744 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001745 }
1746 }
1747 break;
1748
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001749 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001750 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001751 llvm::Attribute::StructRet |
1752 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001753 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001754 // sret disables readnone and readonly
1755 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1756 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001757 break;
1758
Daniel Dunbar1358b202009-01-26 21:26:08 +00001759 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001760 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001761 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001762
Daniel Dunbar22e30052008-09-11 01:48:57 +00001763 case ABIArgInfo::Expand:
1764 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001765 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001766
Devang Patel2bb6eb82008-09-26 22:53:57 +00001767 if (RetAttrs)
1768 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001769
1770 // FIXME: we need to honour command line settings also...
1771 // FIXME: RegParm should be reduced in case of nested functions and/or global
1772 // register variable.
1773 signed RegParm = 0;
1774 if (TargetDecl)
1775 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1776 RegParm = RegParmAttr->getNumParams();
1777
1778 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001779 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1780 ie = FI.arg_end(); it != ie; ++it) {
1781 QualType ParamType = it->type;
1782 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001783 unsigned Attributes = 0;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001784
Daniel Dunbar22e30052008-09-11 01:48:57 +00001785 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001786 case ABIArgInfo::Coerce:
1787 break;
1788
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001789 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001790 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001791 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001792 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001793 // byval disables readnone and readonly.
1794 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1795 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001796 break;
1797
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001798 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001799 if (ParamType->isPromotableIntegerType()) {
1800 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001801 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001802 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001803 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001804 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001805 }
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001806 if (RegParm > 0 &&
1807 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1808 RegParm -=
1809 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1810 if (RegParm >= 0)
1811 Attributes |= llvm::Attribute::InReg;
1812 }
1813 // FIXME: handle sseregparm someday...
Daniel Dunbar22e30052008-09-11 01:48:57 +00001814 break;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001815
Daniel Dunbar1358b202009-01-26 21:26:08 +00001816 case ABIArgInfo::Ignore:
1817 // Skip increment, no matching LLVM parameter.
1818 continue;
1819
Daniel Dunbar04d35782008-09-17 00:51:38 +00001820 case ABIArgInfo::Expand: {
1821 std::vector<const llvm::Type*> Tys;
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001822 // FIXME: This is rather inefficient. Do we ever actually need to do
1823 // anything here? The result should be just reconstructed on the other
1824 // side, so extension should be a non-issue.
Daniel Dunbar04d35782008-09-17 00:51:38 +00001825 getTypes().GetExpandedTypes(ParamType, Tys);
1826 Index += Tys.size();
1827 continue;
1828 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001829 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001830
Devang Patela85a9ef2008-09-25 21:02:23 +00001831 if (Attributes)
1832 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001833 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001834 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001835 if (FuncAttrs)
1836 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001837}
1838
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001839void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1840 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001841 const FunctionArgList &Args) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001842 // FIXME: We no longer need the types from FunctionArgList; lift up and
1843 // simplify.
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001844
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001845 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1846 llvm::Function::arg_iterator AI = Fn->arg_begin();
1847
1848 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001849 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001850 AI->setName("agg.result");
1851 ++AI;
1852 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001853
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001854 assert(FI.arg_size() == Args.size() &&
1855 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001856 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001857 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001858 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001859 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001860 QualType Ty = info_it->type;
1861 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001862
1863 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001864 case ABIArgInfo::Indirect: {
1865 llvm::Value* V = AI;
1866 if (hasAggregateLLVMType(Ty)) {
1867 // Do nothing, aggregates and complex variables are accessed by
1868 // reference.
1869 } else {
1870 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001871 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001872 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1873 // This must be a promotion, for something like
1874 // "void a(x) short x; {..."
1875 V = EmitScalarConversion(V, Ty, Arg->getType());
1876 }
1877 }
1878 EmitParmDecl(*Arg, V);
1879 break;
1880 }
1881
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001882 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001883 assert(AI != Fn->arg_end() && "Argument mismatch!");
1884 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001885 if (hasAggregateLLVMType(Ty)) {
1886 // Create a temporary alloca to hold the argument; the rest of
1887 // codegen expects to access aggregates & complex values by
1888 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001889 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001890 Builder.CreateStore(AI, V);
1891 } else {
1892 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1893 // This must be a promotion, for something like
1894 // "void a(x) short x; {..."
1895 V = EmitScalarConversion(V, Ty, Arg->getType());
1896 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001897 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001898 EmitParmDecl(*Arg, V);
1899 break;
1900 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001901
1902 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001903 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001904 // we need to create a temporary and reconstruct it from the
1905 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001906 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001907 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001908 (Name + ".addr").c_str());
1909 // FIXME: What are the right qualifiers here?
1910 llvm::Function::arg_iterator End =
1911 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1912 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001913
Daniel Dunbar04d35782008-09-17 00:51:38 +00001914 // Name the arguments used in expansion and increment AI.
1915 unsigned Index = 0;
1916 for (; AI != End; ++AI, ++Index)
1917 AI->setName(Name + "." + llvm::utostr(Index));
1918 continue;
1919 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001920
1921 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001922 // Initialize the local variable appropriately.
1923 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001924 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001925 } else {
1926 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1927 }
1928
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001929 // Skip increment, no matching LLVM parameter.
1930 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001931
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001932 case ABIArgInfo::Coerce: {
1933 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpba2cb0e2009-05-16 07:57:57 +00001934 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
1935 // result in a new alloca anyway, so we could just store into that
1936 // directly if we broke the abstraction down more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001937 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001938 CreateCoercedStore(AI, V, *this);
1939 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001940 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001941 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001942 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1943 // This must be a promotion, for something like
1944 // "void a(x) short x; {..."
1945 V = EmitScalarConversion(V, Ty, Arg->getType());
1946 }
1947 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001948 EmitParmDecl(*Arg, V);
1949 break;
1950 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001951 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001952
1953 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001954 }
1955 assert(AI == Fn->arg_end() && "Argument mismatch!");
1956}
1957
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001958void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001959 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001960 llvm::Value *RV = 0;
1961
1962 // Functions with no result always return void.
1963 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001964 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001965 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001966
1967 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001968 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001969 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001970 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1971 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1972 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1973 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1974 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001975 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1976 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001977 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001978 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001979
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001980 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001981 // The internal return value temp always will have
1982 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001983 RV = Builder.CreateLoad(ReturnValue);
1984 break;
1985
Daniel Dunbar1358b202009-01-26 21:26:08 +00001986 case ABIArgInfo::Ignore:
1987 break;
1988
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001989 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001990 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001991 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001992
Daniel Dunbar22e30052008-09-11 01:48:57 +00001993 case ABIArgInfo::Expand:
1994 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001995 }
1996 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001997
1998 if (RV) {
1999 Builder.CreateRet(RV);
2000 } else {
2001 Builder.CreateRetVoid();
2002 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002003}
2004
Anders Carlssond927fa72009-04-08 20:47:54 +00002005RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
2006 return EmitAnyExprToTemp(E);
2007}
2008
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00002009RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
2010 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002011 const CallArgList &CallArgs,
2012 const Decl *TargetDecl) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +00002013 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002014 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002015
2016 // Handle struct-return functions by passing a pointer to the
2017 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00002018 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00002019 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00002020 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002021 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002022 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002023 }
2024
Daniel Dunbar14c884a2009-02-04 21:17:21 +00002025 assert(CallInfo.arg_size() == CallArgs.size() &&
2026 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00002027 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002028 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00002029 I != E; ++I, ++info_it) {
2030 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002031 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00002032
2033 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002034 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002035 if (RV.isScalar() || RV.isComplex()) {
2036 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002037 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002038 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002039 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002040 else
2041 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2042 } else {
2043 Args.push_back(RV.getAggregateAddr());
2044 }
2045 break;
2046
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002047 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00002048 if (RV.isScalar()) {
2049 Args.push_back(RV.getScalarVal());
2050 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002051 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2052 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2053 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2054 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00002055 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002056 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00002057 }
2058 break;
2059
Daniel Dunbar1358b202009-01-26 21:26:08 +00002060 case ABIArgInfo::Ignore:
2061 break;
2062
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002063 case ABIArgInfo::Coerce: {
2064 // FIXME: Avoid the conversion through memory if possible.
2065 llvm::Value *SrcPtr;
2066 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00002067 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002068 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002069 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002070 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002071 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2072 } else
2073 SrcPtr = RV.getAggregateAddr();
2074 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2075 *this));
2076 break;
2077 }
2078
Daniel Dunbar04d35782008-09-17 00:51:38 +00002079 case ABIArgInfo::Expand:
2080 ExpandTypeToArgs(I->second, RV, Args);
2081 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002082 }
2083 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002084
Daniel Dunbar0a067402009-02-23 17:26:39 +00002085 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00002086 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002087 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00002088 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2089 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00002090
Daniel Dunbar90e43452009-03-02 04:32:35 +00002091 llvm::CallSite CS;
2092 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2093 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002094 } else {
2095 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00002096 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2097 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002098 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00002099 }
2100
Daniel Dunbar90e43452009-03-02 04:32:35 +00002101 CS.setAttributes(Attrs);
2102 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2103 CS.setCallingConv(F->getCallingConv());
2104
2105 // If the call doesn't return, finish the basic block and clear the
2106 // insertion point; this allows the rest of IRgen to discard
2107 // unreachable code.
2108 if (CS.doesNotReturn()) {
2109 Builder.CreateUnreachable();
2110 Builder.ClearInsertionPoint();
2111
Mike Stumpba2cb0e2009-05-16 07:57:57 +00002112 // FIXME: For now, emit a dummy basic block because expr emitters in
2113 // generally are not ready to handle emitting expressions at unreachable
2114 // points.
Daniel Dunbar90e43452009-03-02 04:32:35 +00002115 EnsureInsertPoint();
2116
2117 // Return a reasonable RValue.
2118 return GetUndefRValue(RetTy);
2119 }
2120
2121 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +00002122 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002123 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00002124
2125 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002126 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00002127 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00002128 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +00002129 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00002130 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +00002131 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00002132
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002133 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00002134 if (RetTy->isAnyComplexType()) {
2135 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2136 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2137 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +00002138 }
2139 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002140 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00002141 Builder.CreateStore(CI, V);
2142 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002143 }
2144 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00002145
Daniel Dunbar1358b202009-01-26 21:26:08 +00002146 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00002147 // If we are ignoring an argument that had a result, make sure to
2148 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00002149 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00002150
Daniel Dunbar73d66602008-09-10 07:04:09 +00002151 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002152 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002153 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00002154 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00002155 if (RetTy->isAnyComplexType())
2156 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +00002157 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00002158 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002159 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00002160 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00002161
Daniel Dunbar22e30052008-09-11 01:48:57 +00002162 case ABIArgInfo::Expand:
2163 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002164 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00002165
2166 assert(0 && "Unhandled ABIArgInfo::Kind");
2167 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002168}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00002169
2170/* VarArg handling */
2171
2172llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2173 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2174}