blob: eba5ff5b5e755736b98a3c5b37309243ff14156d [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;
58 // Add the 'this' pointer.
59 ArgTys.push_back(MD->getThisType(Context));
60
61 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType();
62 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
63 ArgTys.push_back(FTP->getArgType(i));
64 return getFunctionInfo(FTP->getResultType(), ArgTys);
65}
66
Daniel Dunbar34bda882009-02-02 23:23:47 +000067const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Anders Carlsson7a785352009-04-03 22:48:58 +000068 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
69 if (MD->isInstance())
70 return getFunctionInfo(MD);
71 }
72
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 Dunbara7446422009-03-31 19:01:39 +0000160/// isEmptyRecord - Return true iff a structure has no non-empty
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000161/// members. Note that a structure with a flexible array member is not
162/// considered empty.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000163static bool isEmptyRecord(ASTContext &Context, QualType T) {
Daniel Dunbara7446422009-03-31 19:01:39 +0000164 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000165 if (!RT)
166 return 0;
167 const RecordDecl *RD = RT->getDecl();
168 if (RD->hasFlexibleArrayMember())
169 return false;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000170 for (RecordDecl::field_iterator i = RD->field_begin(Context),
171 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000172 const FieldDecl *FD = *i;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000173 if (!isEmptyRecord(Context, FD->getType()))
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000174 return false;
175 }
176 return true;
177}
178
179/// isSingleElementStruct - Determine if a structure is a "single
180/// element struct", i.e. it has exactly one non-empty field or
181/// exactly one field which is itself a single element
182/// struct. Structures with flexible array members are never
183/// considered single element structs.
184///
185/// \return The field declaration for the single non-empty field, if
186/// it exists.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000187static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000188 const RecordType *RT = T->getAsStructureType();
189 if (!RT)
190 return 0;
191
192 const RecordDecl *RD = RT->getDecl();
193 if (RD->hasFlexibleArrayMember())
194 return 0;
195
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000196 const Type *Found = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000197 for (RecordDecl::field_iterator i = RD->field_begin(Context),
198 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000199 const FieldDecl *FD = *i;
200 QualType FT = FD->getType();
201
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000202 // Treat single element arrays as the element
203 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
204 if (AT->getSize().getZExtValue() == 1)
205 FT = AT->getElementType();
206
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000207 if (isEmptyRecord(Context, FT)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000208 // Ignore
209 } else if (Found) {
210 return 0;
211 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000212 Found = FT.getTypePtr();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000213 } else {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000214 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000215 if (!Found)
216 return 0;
217 }
218 }
219
220 return Found;
221}
222
223static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
224 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
225 return false;
226
227 uint64_t Size = Context.getTypeSize(Ty);
228 return Size == 32 || Size == 64;
229}
230
231static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
232 ASTContext &Context) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000233 for (RecordDecl::field_iterator i = RD->field_begin(Context),
234 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000235 const FieldDecl *FD = *i;
236
237 if (!is32Or64BitBasicType(FD->getType(), Context))
238 return false;
239
Daniel Dunbaref495d42009-04-27 18:31:32 +0000240 // FIXME: Reject bit-fields wholesale; there are two problems, we
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000241 // don't know how to expand them yet, and the predicate for
242 // telling if a bitfield still counts as "basic" is more
243 // complicated than what we were doing previously.
244 if (FD->isBitField())
245 return false;
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000246 }
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000247
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000248 return true;
249}
250
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000251namespace {
252/// DefaultABIInfo - The default implementation for ABI specific
253/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000254/// self-consistent and sensible LLVM IR generation, but does not
255/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000256class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000257 ABIArgInfo classifyReturnType(QualType RetTy,
258 ASTContext &Context) const;
259
260 ABIArgInfo classifyArgumentType(QualType RetTy,
261 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000262
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000263 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
264 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
265 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
266 it != ie; ++it)
267 it->info = classifyArgumentType(it->type, Context);
268 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000269
270 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
271 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000272};
273
274/// X86_32ABIInfo - The X86-32 ABI information.
275class X86_32ABIInfo : public ABIInfo {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000276 ASTContext &Context;
Eli Friedman5e175802009-03-23 23:26:24 +0000277 bool IsDarwin;
278
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000279 static bool isRegisterSize(unsigned Size) {
280 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
281 }
282
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000283 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
284
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000285public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000286 ABIArgInfo classifyReturnType(QualType RetTy,
287 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000288
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000289 ABIArgInfo classifyArgumentType(QualType RetTy,
290 ASTContext &Context) const;
291
292 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
293 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
294 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
295 it != ie; ++it)
296 it->info = classifyArgumentType(it->type, Context);
297 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000298
299 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
300 CodeGenFunction &CGF) const;
Eli Friedman5e175802009-03-23 23:26:24 +0000301
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000302 X86_32ABIInfo(ASTContext &Context, bool d)
303 : ABIInfo(), Context(Context), IsDarwin(d) {}
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000304};
305}
306
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000307
308/// shouldReturnTypeInRegister - Determine if the given type should be
309/// passed in a register (for the Darwin ABI).
310bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
311 ASTContext &Context) {
312 uint64_t Size = Context.getTypeSize(Ty);
313
314 // Type must be register sized.
315 if (!isRegisterSize(Size))
316 return false;
317
318 if (Ty->isVectorType()) {
319 // 64- and 128- bit vectors inside structures are not returned in
320 // registers.
321 if (Size == 64 || Size == 128)
322 return false;
323
324 return true;
325 }
326
327 // If this is a builtin, pointer, or complex type, it is ok.
328 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
329 return true;
330
331 // Arrays are treated like records.
332 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
333 return shouldReturnTypeInRegister(AT->getElementType(), Context);
334
335 // Otherwise, it must be a record type.
336 const RecordType *RT = Ty->getAsRecordType();
337 if (!RT) return false;
338
339 // Structure types are passed in register if all fields would be
340 // passed in a register.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000341 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context),
342 e = RT->getDecl()->field_end(Context); i != e; ++i) {
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000343 const FieldDecl *FD = *i;
344
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000345 // Empty structures are ignored.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000346 if (isEmptyRecord(Context, FD->getType()))
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000347 continue;
348
Daniel Dunbarbfe39d42009-05-08 20:21:04 +0000349 // As are arrays of empty structures, but not generally, so we
350 // can't add this test higher in this routine.
351 if (const ConstantArrayType *AT =
352 Context.getAsConstantArrayType(FD->getType()))
353 if (isEmptyRecord(Context, AT->getElementType()))
354 continue;
355
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000356 // Check fields recursively.
357 if (!shouldReturnTypeInRegister(FD->getType(), Context))
358 return false;
359 }
360
361 return true;
362}
363
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000364ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
365 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000366 if (RetTy->isVoidType()) {
367 return ABIArgInfo::getIgnore();
Daniel Dunbar2a7bb3f2009-04-01 06:13:08 +0000368 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
369 // On Darwin, some vectors are returned in registers.
370 if (IsDarwin) {
371 uint64_t Size = Context.getTypeSize(RetTy);
372
373 // 128-bit vectors are a special case; they are returned in
374 // registers and we need to make sure to pick a type the LLVM
375 // backend will like.
376 if (Size == 128)
377 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
378 2));
379
380 // Always return in register if it fits in a general purpose
381 // register, or if it is 64 bits and has a single element.
382 if ((Size == 8 || Size == 16 || Size == 32) ||
383 (Size == 64 && VT->getNumElements() == 1))
384 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
385
386 return ABIArgInfo::getIndirect(0);
387 }
388
389 return ABIArgInfo::getDirect();
Daniel Dunbareec02622009-02-03 06:30:17 +0000390 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaref495d42009-04-27 18:31:32 +0000391 // Structures with flexible arrays are always indirect.
392 if (const RecordType *RT = RetTy->getAsStructureType())
393 if (RT->getDecl()->hasFlexibleArrayMember())
394 return ABIArgInfo::getIndirect(0);
395
Eli Friedman5e175802009-03-23 23:26:24 +0000396 // Outside of Darwin, structs and unions are always indirect.
397 if (!IsDarwin && !RetTy->isAnyComplexType())
398 return ABIArgInfo::getIndirect(0);
Daniel Dunbaref495d42009-04-27 18:31:32 +0000399
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000400 // Classify "single element" structs as their element type.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000401 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000402 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
403 // FIXME: This is gross, it would be nice if we could just
404 // pass back SeltTy and have clients deal with it. Is it worth
405 // supporting coerce to both LLVM and clang Types?
406 if (BT->isIntegerType()) {
407 uint64_t Size = Context.getTypeSize(SeltTy);
408 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
409 } else if (BT->getKind() == BuiltinType::Float) {
410 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
411 } else if (BT->getKind() == BuiltinType::Double) {
412 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
413 }
414 } else if (SeltTy->isPointerType()) {
415 // FIXME: It would be really nice if this could come out as
416 // the proper pointer type.
417 llvm::Type *PtrTy =
418 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
419 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000420 } else if (SeltTy->isVectorType()) {
421 // 64- and 128-bit vectors are never returned in a
422 // register when inside a structure.
423 uint64_t Size = Context.getTypeSize(RetTy);
424 if (Size == 64 || Size == 128)
425 return ABIArgInfo::getIndirect(0);
426
427 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000428 }
429 }
430
Daniel Dunbar73d66602008-09-10 07:04:09 +0000431 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000432 if (isRegisterSize(Size)) {
433 // Always return in register for unions for now.
434 // FIXME: This is wrong, but better than treating as a
435 // structure.
436 if (RetTy->isUnionType())
437 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
438
439 // Small structures which are register sized are generally returned
440 // in a register.
441 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context))
442 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
443 }
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000444
445 return ABIArgInfo::getIndirect(0);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000446 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000447 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000448 }
449}
450
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000451ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000452 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000453 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000454 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000455 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000456 if (const RecordType *RT = Ty->getAsStructureType())
457 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000458 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000459
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000460 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000461 uint64_t Size = Context.getTypeSize(Ty);
462 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000463 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000464
465 // Expand structs with size <= 128-bits which consist only of
466 // basic types (int, long long, float, double, xxx*). This is
467 // non-recursive and does not ignore empty fields.
468 if (const RecordType *RT = Ty->getAsStructureType()) {
469 if (Context.getTypeSize(Ty) <= 4*32 &&
470 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
471 return ABIArgInfo::getExpand();
472 }
473
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000474 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000475 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000476 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000477 }
478}
479
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000480llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
481 CodeGenFunction &CGF) const {
482 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
483 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
484
485 CGBuilderTy &Builder = CGF.Builder;
486 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
487 "ap");
488 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
489 llvm::Type *PTy =
490 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
491 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
492
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000493 uint64_t Offset =
494 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000495 llvm::Value *NextAddr =
496 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000497 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000498 "ap.next");
499 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
500
501 return AddrTyped;
502}
503
Daniel Dunbare09a9692009-01-24 08:32:22 +0000504namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000505/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000506class X86_64ABIInfo : public ABIInfo {
507 enum Class {
508 Integer = 0,
509 SSE,
510 SSEUp,
511 X87,
512 X87Up,
513 ComplexX87,
514 NoClass,
515 Memory
516 };
517
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000518 /// merge - Implement the X86_64 ABI merging algorithm.
519 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000520 /// Merge an accumulating classification \arg Accum with a field
521 /// classification \arg Field.
522 ///
523 /// \param Accum - The accumulating classification. This should
524 /// always be either NoClass or the result of a previous merge
525 /// call. In addition, this should never be Memory (the caller
526 /// should just return Memory for the aggregate).
527 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000528
Daniel Dunbare09a9692009-01-24 08:32:22 +0000529 /// classify - Determine the x86_64 register classes in which the
530 /// given type T should be passed.
531 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000532 /// \param Lo - The classification for the parts of the type
533 /// residing in the low word of the containing object.
534 ///
535 /// \param Hi - The classification for the parts of the type
536 /// residing in the high word of the containing object.
537 ///
538 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000539 /// containing object. Some parameters are classified different
540 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000541 ///
542 /// If a word is unused its result will be NoClass; if a type should
543 /// be passed in Memory then at least the classification of \arg Lo
544 /// will be Memory.
545 ///
546 /// The \arg Lo class will be NoClass iff the argument is ignored.
547 ///
548 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar92e88642009-02-17 07:55:55 +0000549 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000550 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000551 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000552
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000553 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
554 /// to coerce to, chose the best way to pass Ty in the same place
555 /// that \arg CoerceTo would be passed, but while keeping the
556 /// emitted code as simple as possible.
557 ///
558 /// FIXME: Note, this should be cleaned up to just take an
559 /// enumeration of all the ways we might want to pass things,
560 /// instead of constructing an LLVM type. This makes this code more
561 /// explicit, and it makes it clearer that we are also doing this
562 /// for correctness in the case of passing scalar types.
563 ABIArgInfo getCoerceResult(QualType Ty,
564 const llvm::Type *CoerceTo,
565 ASTContext &Context) const;
566
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000567 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000568 ASTContext &Context) const;
569
570 ABIArgInfo classifyArgumentType(QualType Ty,
571 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000572 unsigned &neededInt,
573 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000574
575public:
576 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000577
578 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
579 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000580};
581}
582
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000583X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
584 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000585 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
586 // classified recursively so that always two fields are
587 // considered. The resulting class is calculated according to
588 // the classes of the fields in the eightbyte:
589 //
590 // (a) If both classes are equal, this is the resulting class.
591 //
592 // (b) If one of the classes is NO_CLASS, the resulting class is
593 // the other class.
594 //
595 // (c) If one of the classes is MEMORY, the result is the MEMORY
596 // class.
597 //
598 // (d) If one of the classes is INTEGER, the result is the
599 // INTEGER.
600 //
601 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
602 // MEMORY is used as class.
603 //
604 // (f) Otherwise class SSE is used.
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000605
606 // Accum should never be memory (we should have returned) or
607 // ComplexX87 (because this cannot be passed in a structure).
608 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000609 "Invalid accumulated classification during merge.");
610 if (Accum == Field || Field == NoClass)
611 return Accum;
612 else if (Field == Memory)
613 return Memory;
614 else if (Accum == NoClass)
615 return Field;
616 else if (Accum == Integer || Field == Integer)
617 return Integer;
618 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
619 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000620 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000621 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000622}
623
Daniel Dunbare09a9692009-01-24 08:32:22 +0000624void X86_64ABIInfo::classify(QualType Ty,
625 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000626 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000627 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000628 // FIXME: This code can be simplified by introducing a simple value
629 // class for Class pairs with appropriate constructor methods for
630 // the various situations.
631
Daniel Dunbard97f5952009-02-22 04:48:22 +0000632 // FIXME: Some of the split computations are wrong; unaligned
633 // vectors shouldn't be passed in registers for example, so there is
634 // no chance they can straddle an eightbyte. Verify & simplify.
635
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000636 Lo = Hi = NoClass;
637
638 Class &Current = OffsetBase < 64 ? Lo : Hi;
639 Current = Memory;
640
Daniel Dunbare09a9692009-01-24 08:32:22 +0000641 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
642 BuiltinType::Kind k = BT->getKind();
643
Daniel Dunbar1358b202009-01-26 21:26:08 +0000644 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000645 Current = NoClass;
Chris Lattner6cc7e412009-04-30 02:43:43 +0000646 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattner3d8e0682009-04-30 06:22:07 +0000647 Lo = Integer;
648 Hi = Integer;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000649 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000650 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000651 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000652 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000653 } else if (k == BuiltinType::LongDouble) {
654 Lo = X87;
655 Hi = X87Up;
656 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000657 // FIXME: _Decimal32 and _Decimal64 are SSE.
658 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson1d234462009-02-26 17:31:15 +0000659 } else if (const EnumType *ET = Ty->getAsEnumType()) {
660 // Classify the underlying integer type.
661 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000662 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000663 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000664 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000665 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000666 if (Size == 32) {
667 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
668 // float> as integer.
669 Current = Integer;
670
671 // If this type crosses an eightbyte boundary, it should be
672 // split.
673 uint64_t EB_Real = (OffsetBase) / 64;
674 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
675 if (EB_Real != EB_Imag)
676 Hi = Lo;
677 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000678 // gcc passes <1 x double> in memory. :(
679 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000680 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000681
682 // gcc passes <1 x long long> as INTEGER.
683 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
684 Current = Integer;
685 else
686 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000687
688 // If this type crosses an eightbyte boundary, it should be
689 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000690 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000691 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000692 } else if (Size == 128) {
693 Lo = SSE;
694 Hi = SSEUp;
695 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000696 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000697 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000698
Daniel Dunbare413f532009-01-30 18:40:10 +0000699 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000700 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000701 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000702 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000703 else if (Size <= 128)
704 Lo = Hi = Integer;
705 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000706 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000707 else if (ET == Context.DoubleTy)
708 Lo = Hi = SSE;
709 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000710 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000711
712 // If this complex type crosses an eightbyte boundary then it
713 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000714 uint64_t EB_Real = (OffsetBase) / 64;
715 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000716 if (Hi == NoClass && EB_Real != EB_Imag)
717 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000718 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
719 // Arrays are treated like structures.
720
721 uint64_t Size = Context.getTypeSize(Ty);
722
723 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
724 // than two eightbytes, ..., it has class MEMORY.
725 if (Size > 128)
726 return;
727
728 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
729 // fields, it has class MEMORY.
730 //
731 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000732 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000733 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000734
735 // Otherwise implement simplified merge. We could be smarter about
736 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000737 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000738 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
739 uint64_t ArraySize = AT->getSize().getZExtValue();
740 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
741 Class FieldLo, FieldHi;
742 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000743 Lo = merge(Lo, FieldLo);
744 Hi = merge(Hi, FieldHi);
745 if (Lo == Memory || Hi == Memory)
746 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000747 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000748
749 // Do post merger cleanup (see below). Only case we worry about is Memory.
750 if (Hi == Memory)
751 Lo = Memory;
752 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000753 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000754 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000755
756 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
757 // than two eightbytes, ..., it has class MEMORY.
758 if (Size > 128)
759 return;
760
761 const RecordDecl *RD = RT->getDecl();
762
763 // Assume variable sized types are passed in memory.
764 if (RD->hasFlexibleArrayMember())
765 return;
766
767 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
768
769 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000770 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000771 unsigned idx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000772 for (RecordDecl::field_iterator i = RD->field_begin(Context),
773 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000774 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000775 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000776
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000777 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
778 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000779 //
Daniel Dunbaref495d42009-04-27 18:31:32 +0000780 // Note, skip this test for bit-fields, see below.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000781 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000782 Lo = Memory;
783 return;
784 }
785
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000786 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000787 //
788 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
789 // exceeds a single eightbyte, each is classified
790 // separately. Each eightbyte gets initialized to class
791 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000792 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000793
Daniel Dunbaref495d42009-04-27 18:31:32 +0000794 // Bit-fields require special handling, they do not force the
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000795 // structure to be passed in memory even if unaligned, and
796 // therefore they can straddle an eightbyte.
797 if (BitField) {
798 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman5255e7a2009-04-26 19:19:15 +0000799 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000800
801 uint64_t EB_Lo = Offset / 64;
802 uint64_t EB_Hi = (Offset + Size - 1) / 64;
803 FieldLo = FieldHi = NoClass;
804 if (EB_Lo) {
805 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
806 FieldLo = NoClass;
807 FieldHi = Integer;
808 } else {
809 FieldLo = Integer;
810 FieldHi = EB_Hi ? Integer : NoClass;
811 }
812 } else
813 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000814 Lo = merge(Lo, FieldLo);
815 Hi = merge(Hi, FieldHi);
816 if (Lo == Memory || Hi == Memory)
817 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000818 }
819
820 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
821 //
822 // (a) If one of the classes is MEMORY, the whole argument is
823 // passed in memory.
824 //
825 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
826
827 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000828 // the merge (just bail).
829 //
830 // The second condition occurs in the case of unions; for example
831 // union { _Complex double; unsigned; }.
832 if (Hi == Memory)
833 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000834 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000835 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000836 }
837}
838
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000839ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
840 const llvm::Type *CoerceTo,
841 ASTContext &Context) const {
842 if (CoerceTo == llvm::Type::Int64Ty) {
843 // Integer and pointer types will end up in a general purpose
844 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000845 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000846 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000847
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000848 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000849 // FIXME: It would probably be better to make CGFunctionInfo only
850 // map using canonical types than to canonize here.
851 QualType CTy = Context.getCanonicalType(Ty);
852
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000853 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000854 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000855 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000856
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000857 }
858
859 return ABIArgInfo::getCoerce(CoerceTo);
860}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000861
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000862ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
863 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000864 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
865 // classification algorithm.
866 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000867 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000868
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000869 // Check some invariants.
870 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
871 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
872 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
873
Daniel Dunbare09a9692009-01-24 08:32:22 +0000874 const llvm::Type *ResType = 0;
875 switch (Lo) {
876 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000877 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000878
879 case SSEUp:
880 case X87Up:
881 assert(0 && "Invalid classification for lo word.");
882
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000883 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000884 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000885 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000886 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000887
888 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
889 // available register of the sequence %rax, %rdx is used.
890 case Integer:
891 ResType = llvm::Type::Int64Ty; break;
892
893 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
894 // available SSE register of the sequence %xmm0, %xmm1 is used.
895 case SSE:
896 ResType = llvm::Type::DoubleTy; break;
897
898 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
899 // returned on the X87 stack in %st0 as 80-bit x87 number.
900 case X87:
901 ResType = llvm::Type::X86_FP80Ty; break;
902
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000903 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
904 // part of the value is returned in %st0 and the imaginary part in
905 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000906 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000907 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000908 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
909 llvm::Type::X86_FP80Ty,
910 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000911 break;
912 }
913
914 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000915 // Memory was handled previously and X87 should
916 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000917 case Memory:
918 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000919 assert(0 && "Invalid classification for hi word.");
920
Daniel Dunbar92e88642009-02-17 07:55:55 +0000921 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000922 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000923
Daniel Dunbare09a9692009-01-24 08:32:22 +0000924 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000925 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
926 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000927 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000928 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
929 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000930
931 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
932 // is passed in the upper half of the last used SSE register.
933 //
934 // SSEUP should always be preceeded by SSE, just widen.
935 case SSEUp:
936 assert(Lo == SSE && "Unexpected SSEUp classification.");
937 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
938 break;
939
940 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000941 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000942 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000943 // If X87Up is preceeded by X87, we don't need to do
944 // anything. However, in some cases with unions it may not be
945 // preceeded by X87. In such situations we follow gcc and pass the
946 // extra bits in an SSE reg.
947 if (Lo != X87)
948 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000949 break;
950 }
951
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000952 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000953}
954
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000955ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000956 unsigned &neededInt,
957 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000958 X86_64ABIInfo::Class Lo, Hi;
959 classify(Ty, Context, 0, Lo, Hi);
960
961 // Check some invariants.
962 // FIXME: Enforce these by construction.
963 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
964 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
965 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
966
Daniel Dunbare978cb92009-02-10 17:06:09 +0000967 neededInt = 0;
968 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000969 const llvm::Type *ResType = 0;
970 switch (Lo) {
971 case NoClass:
972 return ABIArgInfo::getIgnore();
973
974 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
975 // on the stack.
976 case Memory:
977
978 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
979 // COMPLEX_X87, it is passed in memory.
980 case X87:
981 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000982 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000983
984 case SSEUp:
985 case X87Up:
986 assert(0 && "Invalid classification for lo word.");
987
988 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
989 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
990 // and %r9 is used.
991 case Integer:
992 ++neededInt;
993 ResType = llvm::Type::Int64Ty;
994 break;
995
996 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
997 // available SSE register is used, the registers are taken in the
998 // order from %xmm0 to %xmm7.
999 case SSE:
1000 ++neededSSE;
1001 ResType = llvm::Type::DoubleTy;
1002 break;
Daniel Dunbareec02622009-02-03 06:30:17 +00001003 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001004
1005 switch (Hi) {
1006 // Memory was handled previously, ComplexX87 and X87 should
1007 // never occur as hi classes, and X87Up must be preceed by X87,
1008 // which is passed in memory.
1009 case Memory:
1010 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001011 case ComplexX87:
1012 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001013 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001014
1015 case NoClass: break;
1016 case Integer:
1017 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1018 ++neededInt;
1019 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001020
1021 // X87Up generally doesn't occur here (long double is passed in
1022 // memory), except in situations involving unions.
1023 case X87Up:
1024 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001025 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1026 ++neededSSE;
1027 break;
1028
1029 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1030 // eightbyte is passed in the upper half of the last used SSE
1031 // register.
1032 case SSEUp:
1033 assert(Lo == SSE && "Unexpected SSEUp classification.");
1034 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1035 break;
1036 }
1037
Daniel Dunbar87c4dc92009-02-14 02:09:24 +00001038 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001039}
1040
1041void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1042 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1043
1044 // Keep track of the number of assigned registers.
1045 unsigned freeIntRegs = 6, freeSSERegs = 8;
1046
1047 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1048 // get assigned (in left-to-right order) for passing as follows...
1049 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +00001050 it != ie; ++it) {
1051 unsigned neededInt, neededSSE;
1052 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1053
1054 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1055 // eightbyte of an argument, the whole argument is passed on the
1056 // stack. If registers have already been assigned for some
1057 // eightbytes of such an argument, the assignments get reverted.
1058 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1059 freeIntRegs -= neededInt;
1060 freeSSERegs -= neededSSE;
1061 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001062 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +00001063 }
1064 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001065}
1066
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001067static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1068 QualType Ty,
1069 CodeGenFunction &CGF) {
1070 llvm::Value *overflow_arg_area_p =
1071 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1072 llvm::Value *overflow_arg_area =
1073 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1074
1075 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1076 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001077 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001078 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001079 // Note that we follow the ABI & gcc here, even though the type
1080 // could in theory have an alignment greater than 16. This case
1081 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001082
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001083 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1084 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1085 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1086 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1087 llvm::Type::Int64Ty);
1088 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1089 overflow_arg_area =
1090 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1091 overflow_arg_area->getType(),
1092 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001093 }
1094
1095 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1096 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1097 llvm::Value *Res =
1098 CGF.Builder.CreateBitCast(overflow_arg_area,
1099 llvm::PointerType::getUnqual(LTy));
1100
1101 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1102 // l->overflow_arg_area + sizeof(type).
1103 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1104 // an 8 byte boundary.
1105
1106 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1107 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1108 (SizeInBytes + 7) & ~7);
1109 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1110 "overflow_arg_area.next");
1111 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1112
1113 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1114 return Res;
1115}
1116
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001117llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1118 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001119 // Assume that va_list type is correct; should be pointer to LLVM type:
1120 // struct {
1121 // i32 gp_offset;
1122 // i32 fp_offset;
1123 // i8* overflow_arg_area;
1124 // i8* reg_save_area;
1125 // };
1126 unsigned neededInt, neededSSE;
1127 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1128 neededInt, neededSSE);
1129
1130 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1131 // in the registers. If not go to step 7.
1132 if (!neededInt && !neededSSE)
1133 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1134
1135 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1136 // general purpose registers needed to pass type and num_fp to hold
1137 // the number of floating point registers needed.
1138
1139 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1140 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1141 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1142 //
1143 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1144 // register save space).
1145
1146 llvm::Value *InRegs = 0;
1147 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1148 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1149 if (neededInt) {
1150 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1151 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1152 InRegs =
1153 CGF.Builder.CreateICmpULE(gp_offset,
1154 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1155 48 - neededInt * 8),
1156 "fits_in_gp");
1157 }
1158
1159 if (neededSSE) {
1160 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1161 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1162 llvm::Value *FitsInFP =
1163 CGF.Builder.CreateICmpULE(fp_offset,
1164 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001165 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001166 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001167 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001168 }
1169
1170 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1171 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1172 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1173 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1174
1175 // Emit code to load the value if it was passed in registers.
1176
1177 CGF.EmitBlock(InRegBlock);
1178
1179 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1180 // an offset of l->gp_offset and/or l->fp_offset. This may require
1181 // copying to a temporary location in case the parameter is passed
1182 // in different register classes or requires an alignment greater
1183 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001184 //
1185 // FIXME: This really results in shameful code when we end up
1186 // needing to collect arguments from different places; often what
1187 // should result in a simple assembling of a structure from
1188 // scattered addresses has many more loads than necessary. Can we
1189 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001190 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1191 llvm::Value *RegAddr =
1192 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1193 "reg_save_area");
1194 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001195 // FIXME: Cleanup.
1196 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1197 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1198 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1199 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1200 const llvm::Type *TyLo = ST->getElementType(0);
1201 const llvm::Type *TyHi = ST->getElementType(1);
1202 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1203 "Unexpected ABI info for mixed regs");
1204 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1205 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1206 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1207 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1208 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1209 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1210 llvm::Value *V =
1211 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1212 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1213 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1214 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1215
1216 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001217 } else if (neededInt) {
1218 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1219 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1220 llvm::PointerType::getUnqual(LTy));
1221 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001222 if (neededSSE == 1) {
1223 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1224 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1225 llvm::PointerType::getUnqual(LTy));
1226 } else {
1227 assert(neededSSE == 2 && "Invalid number of needed registers!");
1228 // SSE registers are spaced 16 bytes apart in the register save
1229 // area, we need to collect the two eightbytes together.
1230 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1231 llvm::Value *RegAddrHi =
1232 CGF.Builder.CreateGEP(RegAddrLo,
1233 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1234 const llvm::Type *DblPtrTy =
1235 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1236 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1237 llvm::Type::DoubleTy,
1238 NULL);
1239 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1240 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1241 DblPtrTy));
1242 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1243 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1244 DblPtrTy));
1245 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1246 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1247 llvm::PointerType::getUnqual(LTy));
1248 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001249 }
1250
1251 // AMD64-ABI 3.5.7p5: Step 5. Set:
1252 // l->gp_offset = l->gp_offset + num_gp * 8
1253 // l->fp_offset = l->fp_offset + num_fp * 16.
1254 if (neededInt) {
1255 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1256 neededInt * 8);
1257 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1258 gp_offset_p);
1259 }
1260 if (neededSSE) {
1261 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1262 neededSSE * 16);
1263 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1264 fp_offset_p);
1265 }
1266 CGF.EmitBranch(ContBlock);
1267
1268 // Emit code to load the value if it was passed in memory.
1269
1270 CGF.EmitBlock(InMemBlock);
1271 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1272
1273 // Return the appropriate result.
1274
1275 CGF.EmitBlock(ContBlock);
1276 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1277 "vaarg.addr");
1278 ResAddr->reserveOperandSpace(2);
1279 ResAddr->addIncoming(RegAddr, InRegBlock);
1280 ResAddr->addIncoming(MemAddr, InMemBlock);
1281
1282 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001283}
1284
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001285// ABI Info for PIC16
1286class PIC16ABIInfo : public ABIInfo {
1287 ABIArgInfo classifyReturnType(QualType RetTy,
1288 ASTContext &Context) const;
1289
1290 ABIArgInfo classifyArgumentType(QualType RetTy,
1291 ASTContext &Context) const;
1292
1293 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1294 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1295 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1296 it != ie; ++it)
1297 it->info = classifyArgumentType(it->type, Context);
1298 }
1299
1300 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1301 CodeGenFunction &CGF) const;
1302
1303};
1304
1305ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1306 ASTContext &Context) const {
1307 if (RetTy->isVoidType()) {
1308 return ABIArgInfo::getIgnore();
1309 } else {
1310 return ABIArgInfo::getDirect();
1311 }
1312}
1313
1314ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1315 ASTContext &Context) const {
1316 return ABIArgInfo::getDirect();
1317}
1318
1319llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1320 CodeGenFunction &CGF) const {
1321 return 0;
1322}
1323
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001324class ARMABIInfo : public ABIInfo {
1325 ABIArgInfo classifyReturnType(QualType RetTy,
1326 ASTContext &Context) const;
1327
1328 ABIArgInfo classifyArgumentType(QualType RetTy,
1329 ASTContext &Context) const;
1330
1331 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1332
1333 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1334 CodeGenFunction &CGF) const;
1335};
1336
1337void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1338 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1339 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1340 it != ie; ++it) {
1341 it->info = classifyArgumentType(it->type, Context);
1342 }
1343}
1344
1345ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1346 ASTContext &Context) const {
1347 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1348 return ABIArgInfo::getDirect();
1349 }
1350 // FIXME: This is kind of nasty... but there isn't much choice
1351 // because the ARM backend doesn't support byval.
1352 // FIXME: This doesn't handle alignment > 64 bits.
1353 const llvm::Type* ElemTy;
1354 unsigned SizeRegs;
1355 if (Context.getTypeAlign(Ty) > 32) {
1356 ElemTy = llvm::Type::Int64Ty;
1357 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1358 } else {
1359 ElemTy = llvm::Type::Int32Ty;
1360 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1361 }
1362 std::vector<const llvm::Type*> LLVMFields;
1363 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1364 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1365 return ABIArgInfo::getCoerce(STy);
1366}
1367
1368ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1369 ASTContext &Context) const {
1370 if (RetTy->isVoidType()) {
1371 return ABIArgInfo::getIgnore();
1372 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1373 // Aggregates <= 4 bytes are returned in r0; other aggregates
1374 // are returned indirectly.
1375 uint64_t Size = Context.getTypeSize(RetTy);
1376 if (Size <= 32)
1377 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1378 return ABIArgInfo::getIndirect(0);
1379 } else {
1380 return ABIArgInfo::getDirect();
1381 }
1382}
1383
1384llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1385 CodeGenFunction &CGF) const {
1386 // FIXME: Need to handle alignment
1387 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1388 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1389
1390 CGBuilderTy &Builder = CGF.Builder;
1391 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1392 "ap");
1393 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1394 llvm::Type *PTy =
1395 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1396 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1397
1398 uint64_t Offset =
1399 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1400 llvm::Value *NextAddr =
1401 Builder.CreateGEP(Addr,
1402 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1403 "ap.next");
1404 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1405
1406 return AddrTyped;
1407}
1408
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001409ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001410 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001411 if (RetTy->isVoidType()) {
1412 return ABIArgInfo::getIgnore();
1413 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001414 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001415 } else {
1416 return ABIArgInfo::getDirect();
1417 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001418}
1419
1420ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001421 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001422 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001423 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001424 } else {
1425 return ABIArgInfo::getDirect();
1426 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001427}
1428
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001429llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1430 CodeGenFunction &CGF) const {
1431 return 0;
1432}
1433
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001434const ABIInfo &CodeGenTypes::getABIInfo() const {
1435 if (TheABIInfo)
1436 return *TheABIInfo;
1437
1438 // For now we just cache this in the CodeGenTypes and don't bother
1439 // to free it.
1440 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1441 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman5e175802009-03-23 23:26:24 +00001442 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001443 switch (getContext().Target.getPointerWidth(0)) {
1444 case 32:
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001445 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001446 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001447 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001448 }
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001449 } else if (strcmp(TargetPrefix, "arm") == 0) {
1450 // FIXME: Support for OABI?
1451 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001452 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1453 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001454 }
1455
1456 return *(TheABIInfo = new DefaultABIInfo);
1457}
1458
Daniel Dunbare126ab12008-09-10 02:41:04 +00001459/***/
1460
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001461CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1462 const llvm::SmallVector<QualType, 16> &ArgTys) {
1463 NumArgs = ArgTys.size();
1464 Args = new ArgInfo[1 + NumArgs];
1465 Args[0].type = ResTy;
1466 for (unsigned i = 0; i < NumArgs; ++i)
1467 Args[1 + i].type = ArgTys[i];
1468}
1469
1470/***/
1471
Daniel Dunbar04d35782008-09-17 00:51:38 +00001472void CodeGenTypes::GetExpandedTypes(QualType Ty,
1473 std::vector<const llvm::Type*> &ArgTys) {
1474 const RecordType *RT = Ty->getAsStructureType();
1475 assert(RT && "Can only expand structure types.");
1476 const RecordDecl *RD = RT->getDecl();
1477 assert(!RD->hasFlexibleArrayMember() &&
1478 "Cannot expand structure with flexible array.");
1479
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001480 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1481 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001482 const FieldDecl *FD = *i;
1483 assert(!FD->isBitField() &&
1484 "Cannot expand structure with bit-field members.");
1485
1486 QualType FT = FD->getType();
1487 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1488 GetExpandedTypes(FT, ArgTys);
1489 } else {
1490 ArgTys.push_back(ConvertType(FT));
1491 }
1492 }
1493}
1494
1495llvm::Function::arg_iterator
1496CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1497 llvm::Function::arg_iterator AI) {
1498 const RecordType *RT = Ty->getAsStructureType();
1499 assert(RT && "Can only expand structure types.");
1500
1501 RecordDecl *RD = RT->getDecl();
1502 assert(LV.isSimple() &&
1503 "Unexpected non-simple lvalue during struct expansion.");
1504 llvm::Value *Addr = LV.getAddress();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001505 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1506 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001507 FieldDecl *FD = *i;
1508 QualType FT = FD->getType();
1509
1510 // FIXME: What are the right qualifiers here?
1511 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1512 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1513 AI = ExpandTypeFromArgs(FT, LV, AI);
1514 } else {
1515 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1516 ++AI;
1517 }
1518 }
1519
1520 return AI;
1521}
1522
1523void
1524CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1525 llvm::SmallVector<llvm::Value*, 16> &Args) {
1526 const RecordType *RT = Ty->getAsStructureType();
1527 assert(RT && "Can only expand structure types.");
1528
1529 RecordDecl *RD = RT->getDecl();
1530 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1531 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001532 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1533 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001534 FieldDecl *FD = *i;
1535 QualType FT = FD->getType();
1536
1537 // FIXME: What are the right qualifiers here?
1538 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1539 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1540 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1541 } else {
1542 RValue RV = EmitLoadOfLValue(LV, FT);
1543 assert(RV.isScalar() &&
1544 "Unexpected non-scalar rvalue during struct expansion.");
1545 Args.push_back(RV.getScalarVal());
1546 }
1547 }
1548}
1549
Daniel Dunbar84379912009-02-02 19:06:38 +00001550/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1551/// a pointer to an object of type \arg Ty.
1552///
1553/// This safely handles the case when the src type is smaller than the
1554/// destination type; in this situation the values of bits which not
1555/// present in the src are undefined.
1556static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1557 const llvm::Type *Ty,
1558 CodeGenFunction &CGF) {
1559 const llvm::Type *SrcTy =
1560 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1561 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1562 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1563
Daniel Dunbar77071992009-02-03 05:59:18 +00001564 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001565 if (SrcSize == DstSize) {
1566 llvm::Value *Casted =
1567 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001568 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1569 // FIXME: Use better alignment / avoid requiring aligned load.
1570 Load->setAlignment(1);
1571 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001572 } else {
1573 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1574
1575 // Otherwise do coercion through memory. This is stupid, but
1576 // simple.
1577 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1578 llvm::Value *Casted =
1579 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001580 llvm::StoreInst *Store =
1581 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1582 // FIXME: Use better alignment / avoid requiring aligned store.
1583 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001584 return CGF.Builder.CreateLoad(Tmp);
1585 }
1586}
1587
1588/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1589/// where the source and destination may have different types.
1590///
1591/// This safely handles the case when the src type is larger than the
1592/// destination type; the upper bits of the src will be lost.
1593static void CreateCoercedStore(llvm::Value *Src,
1594 llvm::Value *DstPtr,
1595 CodeGenFunction &CGF) {
1596 const llvm::Type *SrcTy = Src->getType();
1597 const llvm::Type *DstTy =
1598 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1599
1600 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1601 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1602
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001603 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001604 if (SrcSize == DstSize) {
1605 llvm::Value *Casted =
1606 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001607 // FIXME: Use better alignment / avoid requiring aligned store.
1608 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001609 } else {
1610 assert(SrcSize > DstSize && "Coercion is missing bits!");
1611
1612 // Otherwise do coercion through memory. This is stupid, but
1613 // simple.
1614 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1615 CGF.Builder.CreateStore(Src, Tmp);
1616 llvm::Value *Casted =
1617 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001618 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1619 // FIXME: Use better alignment / avoid requiring aligned load.
1620 Load->setAlignment(1);
1621 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001622 }
1623}
1624
Daniel Dunbar04d35782008-09-17 00:51:38 +00001625/***/
1626
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001627bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001628 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001629}
1630
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001631const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001632CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001633 std::vector<const llvm::Type*> ArgTys;
1634
1635 const llvm::Type *ResultType = 0;
1636
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001637 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001638 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001639 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001640 case ABIArgInfo::Expand:
1641 assert(0 && "Invalid ABI kind for return argument");
1642
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001643 case ABIArgInfo::Direct:
1644 ResultType = ConvertType(RetTy);
1645 break;
1646
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001647 case ABIArgInfo::Indirect: {
1648 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001649 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001650 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001651 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1652 break;
1653 }
1654
Daniel Dunbar1358b202009-01-26 21:26:08 +00001655 case ABIArgInfo::Ignore:
1656 ResultType = llvm::Type::VoidTy;
1657 break;
1658
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001659 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001660 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001661 break;
1662 }
1663
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001664 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1665 ie = FI.arg_end(); it != ie; ++it) {
1666 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001667
1668 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001669 case ABIArgInfo::Ignore:
1670 break;
1671
Daniel Dunbar04d35782008-09-17 00:51:38 +00001672 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001673 ArgTys.push_back(AI.getCoerceToType());
1674 break;
1675
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001676 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001677 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001678 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1679 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001680 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001681 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001682
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001683 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001684 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001685 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001686
1687 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001688 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001689 break;
1690 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001691 }
1692
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001693 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001694}
1695
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001696void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001697 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001698 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001699 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001700 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001701
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001702 // FIXME: handle sseregparm someday...
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001703 if (TargetDecl) {
Daniel Dunbar78582862009-04-13 21:08:27 +00001704 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001705 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar78582862009-04-13 21:08:27 +00001706 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001707 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbar78582862009-04-13 21:08:27 +00001708 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssondd6791c2008-10-05 23:32:53 +00001709 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar78582862009-04-13 21:08:27 +00001710 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar521c3a32009-04-10 22:14:52 +00001711 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001712 }
1713
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001714 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001715 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001716 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001717 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001718 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001719 if (RetTy->isPromotableIntegerType()) {
1720 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001721 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001722 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001723 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001724 }
1725 }
1726 break;
1727
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001728 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001729 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001730 llvm::Attribute::StructRet |
1731 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001732 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001733 // sret disables readnone and readonly
1734 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1735 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001736 break;
1737
Daniel Dunbar1358b202009-01-26 21:26:08 +00001738 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001739 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001740 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001741
Daniel Dunbar22e30052008-09-11 01:48:57 +00001742 case ABIArgInfo::Expand:
1743 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001744 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001745
Devang Patel2bb6eb82008-09-26 22:53:57 +00001746 if (RetAttrs)
1747 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001748
1749 // FIXME: we need to honour command line settings also...
1750 // FIXME: RegParm should be reduced in case of nested functions and/or global
1751 // register variable.
1752 signed RegParm = 0;
1753 if (TargetDecl)
1754 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1755 RegParm = RegParmAttr->getNumParams();
1756
1757 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001758 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1759 ie = FI.arg_end(); it != ie; ++it) {
1760 QualType ParamType = it->type;
1761 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001762 unsigned Attributes = 0;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001763
Daniel Dunbar22e30052008-09-11 01:48:57 +00001764 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001765 case ABIArgInfo::Coerce:
1766 break;
1767
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001768 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001769 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001770 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001771 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001772 // byval disables readnone and readonly.
1773 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1774 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001775 break;
1776
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001777 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001778 if (ParamType->isPromotableIntegerType()) {
1779 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001780 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001781 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001782 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001783 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001784 }
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001785 if (RegParm > 0 &&
1786 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1787 RegParm -=
1788 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1789 if (RegParm >= 0)
1790 Attributes |= llvm::Attribute::InReg;
1791 }
1792 // FIXME: handle sseregparm someday...
Daniel Dunbar22e30052008-09-11 01:48:57 +00001793 break;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001794
Daniel Dunbar1358b202009-01-26 21:26:08 +00001795 case ABIArgInfo::Ignore:
1796 // Skip increment, no matching LLVM parameter.
1797 continue;
1798
Daniel Dunbar04d35782008-09-17 00:51:38 +00001799 case ABIArgInfo::Expand: {
1800 std::vector<const llvm::Type*> Tys;
1801 // FIXME: This is rather inefficient. Do we ever actually need
1802 // to do anything here? The result should be just reconstructed
1803 // on the other side, so extension should be a non-issue.
1804 getTypes().GetExpandedTypes(ParamType, Tys);
1805 Index += Tys.size();
1806 continue;
1807 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001808 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001809
Devang Patela85a9ef2008-09-25 21:02:23 +00001810 if (Attributes)
1811 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001812 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001813 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001814 if (FuncAttrs)
1815 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001816}
1817
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001818void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1819 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001820 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001821 // FIXME: We no longer need the types from FunctionArgList; lift up
1822 // and simplify.
1823
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001824 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1825 llvm::Function::arg_iterator AI = Fn->arg_begin();
1826
1827 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001828 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001829 AI->setName("agg.result");
1830 ++AI;
1831 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001832
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001833 assert(FI.arg_size() == Args.size() &&
1834 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001835 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001836 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001837 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001838 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001839 QualType Ty = info_it->type;
1840 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001841
1842 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001843 case ABIArgInfo::Indirect: {
1844 llvm::Value* V = AI;
1845 if (hasAggregateLLVMType(Ty)) {
1846 // Do nothing, aggregates and complex variables are accessed by
1847 // reference.
1848 } else {
1849 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001850 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001851 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1852 // This must be a promotion, for something like
1853 // "void a(x) short x; {..."
1854 V = EmitScalarConversion(V, Ty, Arg->getType());
1855 }
1856 }
1857 EmitParmDecl(*Arg, V);
1858 break;
1859 }
1860
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001861 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001862 assert(AI != Fn->arg_end() && "Argument mismatch!");
1863 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001864 if (hasAggregateLLVMType(Ty)) {
1865 // Create a temporary alloca to hold the argument; the rest of
1866 // codegen expects to access aggregates & complex values by
1867 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001868 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001869 Builder.CreateStore(AI, V);
1870 } else {
1871 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1872 // This must be a promotion, for something like
1873 // "void a(x) short x; {..."
1874 V = EmitScalarConversion(V, Ty, Arg->getType());
1875 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001876 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001877 EmitParmDecl(*Arg, V);
1878 break;
1879 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001880
1881 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001882 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001883 // we need to create a temporary and reconstruct it from the
1884 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001885 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001886 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001887 (Name + ".addr").c_str());
1888 // FIXME: What are the right qualifiers here?
1889 llvm::Function::arg_iterator End =
1890 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1891 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001892
Daniel Dunbar04d35782008-09-17 00:51:38 +00001893 // Name the arguments used in expansion and increment AI.
1894 unsigned Index = 0;
1895 for (; AI != End; ++AI, ++Index)
1896 AI->setName(Name + "." + llvm::utostr(Index));
1897 continue;
1898 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001899
1900 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001901 // Initialize the local variable appropriately.
1902 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001903 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001904 } else {
1905 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1906 }
1907
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001908 // Skip increment, no matching LLVM parameter.
1909 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001910
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001911 case ABIArgInfo::Coerce: {
1912 assert(AI != Fn->arg_end() && "Argument mismatch!");
1913 // FIXME: This is very wasteful; EmitParmDecl is just going to
1914 // drop the result in a new alloca anyway, so we could just
1915 // store into that directly if we broke the abstraction down
1916 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001917 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001918 CreateCoercedStore(AI, V, *this);
1919 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001920 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001921 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001922 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1923 // This must be a promotion, for something like
1924 // "void a(x) short x; {..."
1925 V = EmitScalarConversion(V, Ty, Arg->getType());
1926 }
1927 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001928 EmitParmDecl(*Arg, V);
1929 break;
1930 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001931 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001932
1933 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001934 }
1935 assert(AI == Fn->arg_end() && "Argument mismatch!");
1936}
1937
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001938void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001939 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001940 llvm::Value *RV = 0;
1941
1942 // Functions with no result always return void.
1943 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001944 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001945 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001946
1947 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001948 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001949 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001950 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1951 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1952 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1953 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1954 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001955 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1956 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001957 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001958 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001959
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001960 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001961 // The internal return value temp always will have
1962 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001963 RV = Builder.CreateLoad(ReturnValue);
1964 break;
1965
Daniel Dunbar1358b202009-01-26 21:26:08 +00001966 case ABIArgInfo::Ignore:
1967 break;
1968
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001969 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001970 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001971 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001972
Daniel Dunbar22e30052008-09-11 01:48:57 +00001973 case ABIArgInfo::Expand:
1974 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001975 }
1976 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001977
1978 if (RV) {
1979 Builder.CreateRet(RV);
1980 } else {
1981 Builder.CreateRetVoid();
1982 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001983}
1984
Anders Carlssond927fa72009-04-08 20:47:54 +00001985RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
1986 return EmitAnyExprToTemp(E);
1987}
1988
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001989RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1990 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001991 const CallArgList &CallArgs,
1992 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001993 // FIXME: We no longer need the types from CallArgs; lift up and
1994 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001995 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001996
1997 // Handle struct-return functions by passing a pointer to the
1998 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001999 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00002000 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00002001 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002002 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002003 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002004 }
2005
Daniel Dunbar14c884a2009-02-04 21:17:21 +00002006 assert(CallInfo.arg_size() == CallArgs.size() &&
2007 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00002008 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002009 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00002010 I != E; ++I, ++info_it) {
2011 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002012 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00002013
2014 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002015 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002016 if (RV.isScalar() || RV.isComplex()) {
2017 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002018 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002019 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002020 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002021 else
2022 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2023 } else {
2024 Args.push_back(RV.getAggregateAddr());
2025 }
2026 break;
2027
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002028 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00002029 if (RV.isScalar()) {
2030 Args.push_back(RV.getScalarVal());
2031 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002032 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2033 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2034 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2035 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00002036 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002037 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00002038 }
2039 break;
2040
Daniel Dunbar1358b202009-01-26 21:26:08 +00002041 case ABIArgInfo::Ignore:
2042 break;
2043
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002044 case ABIArgInfo::Coerce: {
2045 // FIXME: Avoid the conversion through memory if possible.
2046 llvm::Value *SrcPtr;
2047 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00002048 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002049 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002050 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002051 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002052 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2053 } else
2054 SrcPtr = RV.getAggregateAddr();
2055 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2056 *this));
2057 break;
2058 }
2059
Daniel Dunbar04d35782008-09-17 00:51:38 +00002060 case ABIArgInfo::Expand:
2061 ExpandTypeToArgs(I->second, RV, Args);
2062 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002063 }
2064 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002065
Daniel Dunbar0a067402009-02-23 17:26:39 +00002066 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00002067 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002068 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00002069 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2070 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00002071
Daniel Dunbar90e43452009-03-02 04:32:35 +00002072 llvm::CallSite CS;
2073 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2074 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002075 } else {
2076 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00002077 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2078 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002079 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00002080 }
2081
Daniel Dunbar90e43452009-03-02 04:32:35 +00002082 CS.setAttributes(Attrs);
2083 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2084 CS.setCallingConv(F->getCallingConv());
2085
2086 // If the call doesn't return, finish the basic block and clear the
2087 // insertion point; this allows the rest of IRgen to discard
2088 // unreachable code.
2089 if (CS.doesNotReturn()) {
2090 Builder.CreateUnreachable();
2091 Builder.ClearInsertionPoint();
2092
2093 // FIXME: For now, emit a dummy basic block because expr
2094 // emitters in generally are not ready to handle emitting
2095 // expressions at unreachable points.
2096 EnsureInsertPoint();
2097
2098 // Return a reasonable RValue.
2099 return GetUndefRValue(RetTy);
2100 }
2101
2102 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +00002103 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002104 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00002105
2106 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002107 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00002108 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00002109 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +00002110 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00002111 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +00002112 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00002113
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002114 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00002115 if (RetTy->isAnyComplexType()) {
2116 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2117 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2118 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +00002119 }
2120 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002121 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00002122 Builder.CreateStore(CI, V);
2123 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002124 }
2125 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00002126
Daniel Dunbar1358b202009-01-26 21:26:08 +00002127 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00002128 // If we are ignoring an argument that had a result, make sure to
2129 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00002130 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00002131
Daniel Dunbar73d66602008-09-10 07:04:09 +00002132 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002133 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002134 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00002135 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00002136 if (RetTy->isAnyComplexType())
2137 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +00002138 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00002139 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002140 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00002141 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00002142
Daniel Dunbar22e30052008-09-11 01:48:57 +00002143 case ABIArgInfo::Expand:
2144 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002145 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00002146
2147 assert(0 && "Unhandled ABIArgInfo::Kind");
2148 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002149}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00002150
2151/* VarArg handling */
2152
2153llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2154 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2155}