blob: 41d4d7c90b90fb9c5fb728dddb95462234da0914 [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 Dunbar9f052cb2009-03-11 22:05:26 +0000240 // FIXME: Reject bitfields wholesale; there are two problems, we
241 // 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
345 // FIXME: Reject bitfields wholesale for now; this is incorrect.
346 if (FD->isBitField())
347 return false;
348
349 // Empty structures are ignored.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000350 if (isEmptyRecord(Context, FD->getType()))
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000351 continue;
352
353 // Check fields recursively.
354 if (!shouldReturnTypeInRegister(FD->getType(), Context))
355 return false;
356 }
357
358 return true;
359}
360
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000361ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
362 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000363 if (RetTy->isVoidType()) {
364 return ABIArgInfo::getIgnore();
Daniel Dunbar2a7bb3f2009-04-01 06:13:08 +0000365 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
366 // On Darwin, some vectors are returned in registers.
367 if (IsDarwin) {
368 uint64_t Size = Context.getTypeSize(RetTy);
369
370 // 128-bit vectors are a special case; they are returned in
371 // registers and we need to make sure to pick a type the LLVM
372 // backend will like.
373 if (Size == 128)
374 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
375 2));
376
377 // Always return in register if it fits in a general purpose
378 // register, or if it is 64 bits and has a single element.
379 if ((Size == 8 || Size == 16 || Size == 32) ||
380 (Size == 64 && VT->getNumElements() == 1))
381 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
382
383 return ABIArgInfo::getIndirect(0);
384 }
385
386 return ABIArgInfo::getDirect();
Daniel Dunbareec02622009-02-03 06:30:17 +0000387 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedman5e175802009-03-23 23:26:24 +0000388 // Outside of Darwin, structs and unions are always indirect.
389 if (!IsDarwin && !RetTy->isAnyComplexType())
390 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000391 // Classify "single element" structs as their element type.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000392 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000393 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
394 // FIXME: This is gross, it would be nice if we could just
395 // pass back SeltTy and have clients deal with it. Is it worth
396 // supporting coerce to both LLVM and clang Types?
397 if (BT->isIntegerType()) {
398 uint64_t Size = Context.getTypeSize(SeltTy);
399 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
400 } else if (BT->getKind() == BuiltinType::Float) {
401 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
402 } else if (BT->getKind() == BuiltinType::Double) {
403 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
404 }
405 } else if (SeltTy->isPointerType()) {
406 // FIXME: It would be really nice if this could come out as
407 // the proper pointer type.
408 llvm::Type *PtrTy =
409 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
410 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000411 } else if (SeltTy->isVectorType()) {
412 // 64- and 128-bit vectors are never returned in a
413 // register when inside a structure.
414 uint64_t Size = Context.getTypeSize(RetTy);
415 if (Size == 64 || Size == 128)
416 return ABIArgInfo::getIndirect(0);
417
418 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000419 }
420 }
421
Daniel Dunbar73d66602008-09-10 07:04:09 +0000422 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000423 if (isRegisterSize(Size)) {
424 // Always return in register for unions for now.
425 // FIXME: This is wrong, but better than treating as a
426 // structure.
427 if (RetTy->isUnionType())
428 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
429
430 // Small structures which are register sized are generally returned
431 // in a register.
432 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context))
433 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
434 }
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000435
436 return ABIArgInfo::getIndirect(0);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000437 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000438 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000439 }
440}
441
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000442ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000443 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000444 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000445 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000446 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000447 if (const RecordType *RT = Ty->getAsStructureType())
448 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000449 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000450
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000451 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000452 uint64_t Size = Context.getTypeSize(Ty);
453 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000454 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000455
456 // Expand structs with size <= 128-bits which consist only of
457 // basic types (int, long long, float, double, xxx*). This is
458 // non-recursive and does not ignore empty fields.
459 if (const RecordType *RT = Ty->getAsStructureType()) {
460 if (Context.getTypeSize(Ty) <= 4*32 &&
461 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
462 return ABIArgInfo::getExpand();
463 }
464
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000465 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000466 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000467 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000468 }
469}
470
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000471llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
472 CodeGenFunction &CGF) const {
473 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
474 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
475
476 CGBuilderTy &Builder = CGF.Builder;
477 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
478 "ap");
479 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
480 llvm::Type *PTy =
481 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
482 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
483
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000484 uint64_t Offset =
485 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000486 llvm::Value *NextAddr =
487 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000488 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000489 "ap.next");
490 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
491
492 return AddrTyped;
493}
494
Daniel Dunbare09a9692009-01-24 08:32:22 +0000495namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000496/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000497class X86_64ABIInfo : public ABIInfo {
498 enum Class {
499 Integer = 0,
500 SSE,
501 SSEUp,
502 X87,
503 X87Up,
504 ComplexX87,
505 NoClass,
506 Memory
507 };
508
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000509 /// merge - Implement the X86_64 ABI merging algorithm.
510 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000511 /// Merge an accumulating classification \arg Accum with a field
512 /// classification \arg Field.
513 ///
514 /// \param Accum - The accumulating classification. This should
515 /// always be either NoClass or the result of a previous merge
516 /// call. In addition, this should never be Memory (the caller
517 /// should just return Memory for the aggregate).
518 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000519
Daniel Dunbare09a9692009-01-24 08:32:22 +0000520 /// classify - Determine the x86_64 register classes in which the
521 /// given type T should be passed.
522 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000523 /// \param Lo - The classification for the parts of the type
524 /// residing in the low word of the containing object.
525 ///
526 /// \param Hi - The classification for the parts of the type
527 /// residing in the high word of the containing object.
528 ///
529 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000530 /// containing object. Some parameters are classified different
531 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000532 ///
533 /// If a word is unused its result will be NoClass; if a type should
534 /// be passed in Memory then at least the classification of \arg Lo
535 /// will be Memory.
536 ///
537 /// The \arg Lo class will be NoClass iff the argument is ignored.
538 ///
539 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar92e88642009-02-17 07:55:55 +0000540 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000541 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000542 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000543
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000544 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
545 /// to coerce to, chose the best way to pass Ty in the same place
546 /// that \arg CoerceTo would be passed, but while keeping the
547 /// emitted code as simple as possible.
548 ///
549 /// FIXME: Note, this should be cleaned up to just take an
550 /// enumeration of all the ways we might want to pass things,
551 /// instead of constructing an LLVM type. This makes this code more
552 /// explicit, and it makes it clearer that we are also doing this
553 /// for correctness in the case of passing scalar types.
554 ABIArgInfo getCoerceResult(QualType Ty,
555 const llvm::Type *CoerceTo,
556 ASTContext &Context) const;
557
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000558 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000559 ASTContext &Context) const;
560
561 ABIArgInfo classifyArgumentType(QualType Ty,
562 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000563 unsigned &neededInt,
564 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000565
566public:
567 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000568
569 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
570 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000571};
572}
573
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000574X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
575 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000576 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
577 // classified recursively so that always two fields are
578 // considered. The resulting class is calculated according to
579 // the classes of the fields in the eightbyte:
580 //
581 // (a) If both classes are equal, this is the resulting class.
582 //
583 // (b) If one of the classes is NO_CLASS, the resulting class is
584 // the other class.
585 //
586 // (c) If one of the classes is MEMORY, the result is the MEMORY
587 // class.
588 //
589 // (d) If one of the classes is INTEGER, the result is the
590 // INTEGER.
591 //
592 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
593 // MEMORY is used as class.
594 //
595 // (f) Otherwise class SSE is used.
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000596
597 // Accum should never be memory (we should have returned) or
598 // ComplexX87 (because this cannot be passed in a structure).
599 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000600 "Invalid accumulated classification during merge.");
601 if (Accum == Field || Field == NoClass)
602 return Accum;
603 else if (Field == Memory)
604 return Memory;
605 else if (Accum == NoClass)
606 return Field;
607 else if (Accum == Integer || Field == Integer)
608 return Integer;
609 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
610 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000611 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000612 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000613}
614
Daniel Dunbare09a9692009-01-24 08:32:22 +0000615void X86_64ABIInfo::classify(QualType Ty,
616 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000617 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000618 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000619 // FIXME: This code can be simplified by introducing a simple value
620 // class for Class pairs with appropriate constructor methods for
621 // the various situations.
622
Daniel Dunbard97f5952009-02-22 04:48:22 +0000623 // FIXME: Some of the split computations are wrong; unaligned
624 // vectors shouldn't be passed in registers for example, so there is
625 // no chance they can straddle an eightbyte. Verify & simplify.
626
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000627 Lo = Hi = NoClass;
628
629 Class &Current = OffsetBase < 64 ? Lo : Hi;
630 Current = Memory;
631
Daniel Dunbare09a9692009-01-24 08:32:22 +0000632 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
633 BuiltinType::Kind k = BT->getKind();
634
Daniel Dunbar1358b202009-01-26 21:26:08 +0000635 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000636 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000637 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000638 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000639 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000640 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000641 } else if (k == BuiltinType::LongDouble) {
642 Lo = X87;
643 Hi = X87Up;
644 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000645 // FIXME: _Decimal32 and _Decimal64 are SSE.
646 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000647 // FIXME: __int128 is (Integer, Integer).
Anders Carlsson1d234462009-02-26 17:31:15 +0000648 } else if (const EnumType *ET = Ty->getAsEnumType()) {
649 // Classify the underlying integer type.
650 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000651 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000652 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000653 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000654 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000655 if (Size == 32) {
656 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
657 // float> as integer.
658 Current = Integer;
659
660 // If this type crosses an eightbyte boundary, it should be
661 // split.
662 uint64_t EB_Real = (OffsetBase) / 64;
663 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
664 if (EB_Real != EB_Imag)
665 Hi = Lo;
666 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000667 // gcc passes <1 x double> in memory. :(
668 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000669 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000670
671 // gcc passes <1 x long long> as INTEGER.
672 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
673 Current = Integer;
674 else
675 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000676
677 // If this type crosses an eightbyte boundary, it should be
678 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000679 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000680 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000681 } else if (Size == 128) {
682 Lo = SSE;
683 Hi = SSEUp;
684 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000685 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000686 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000687
Daniel Dunbare413f532009-01-30 18:40:10 +0000688 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000689 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000690 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000691 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000692 else if (Size <= 128)
693 Lo = Hi = Integer;
694 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000695 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000696 else if (ET == Context.DoubleTy)
697 Lo = Hi = SSE;
698 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000699 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000700
701 // If this complex type crosses an eightbyte boundary then it
702 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000703 uint64_t EB_Real = (OffsetBase) / 64;
704 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000705 if (Hi == NoClass && EB_Real != EB_Imag)
706 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000707 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
708 // Arrays are treated like structures.
709
710 uint64_t Size = Context.getTypeSize(Ty);
711
712 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
713 // than two eightbytes, ..., it has class MEMORY.
714 if (Size > 128)
715 return;
716
717 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
718 // fields, it has class MEMORY.
719 //
720 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000721 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000722 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000723
724 // Otherwise implement simplified merge. We could be smarter about
725 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000726 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000727 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
728 uint64_t ArraySize = AT->getSize().getZExtValue();
729 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
730 Class FieldLo, FieldHi;
731 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000732 Lo = merge(Lo, FieldLo);
733 Hi = merge(Hi, FieldHi);
734 if (Lo == Memory || Hi == Memory)
735 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000736 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000737
738 // Do post merger cleanup (see below). Only case we worry about is Memory.
739 if (Hi == Memory)
740 Lo = Memory;
741 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000742 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000743 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000744
745 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
746 // than two eightbytes, ..., it has class MEMORY.
747 if (Size > 128)
748 return;
749
750 const RecordDecl *RD = RT->getDecl();
751
752 // Assume variable sized types are passed in memory.
753 if (RD->hasFlexibleArrayMember())
754 return;
755
756 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
757
758 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000759 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000760 unsigned idx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000761 for (RecordDecl::field_iterator i = RD->field_begin(Context),
762 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000763 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000764 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000765
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000766 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
767 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000768 //
769 // Note, skip this test for bitfields, see below.
770 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000771 Lo = Memory;
772 return;
773 }
774
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000775 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000776 //
777 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
778 // exceeds a single eightbyte, each is classified
779 // separately. Each eightbyte gets initialized to class
780 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000781 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000782
783 // Bitfields require special handling, they do not force the
784 // structure to be passed in memory even if unaligned, and
785 // therefore they can straddle an eightbyte.
786 if (BitField) {
787 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman5255e7a2009-04-26 19:19:15 +0000788 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000789
790 uint64_t EB_Lo = Offset / 64;
791 uint64_t EB_Hi = (Offset + Size - 1) / 64;
792 FieldLo = FieldHi = NoClass;
793 if (EB_Lo) {
794 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
795 FieldLo = NoClass;
796 FieldHi = Integer;
797 } else {
798 FieldLo = Integer;
799 FieldHi = EB_Hi ? Integer : NoClass;
800 }
801 } else
802 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000803 Lo = merge(Lo, FieldLo);
804 Hi = merge(Hi, FieldHi);
805 if (Lo == Memory || Hi == Memory)
806 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000807 }
808
809 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
810 //
811 // (a) If one of the classes is MEMORY, the whole argument is
812 // passed in memory.
813 //
814 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
815
816 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000817 // the merge (just bail).
818 //
819 // The second condition occurs in the case of unions; for example
820 // union { _Complex double; unsigned; }.
821 if (Hi == Memory)
822 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000823 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000824 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000825 }
826}
827
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000828ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
829 const llvm::Type *CoerceTo,
830 ASTContext &Context) const {
831 if (CoerceTo == llvm::Type::Int64Ty) {
832 // Integer and pointer types will end up in a general purpose
833 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000834 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000835 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000836
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000837 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000838 // FIXME: It would probably be better to make CGFunctionInfo only
839 // map using canonical types than to canonize here.
840 QualType CTy = Context.getCanonicalType(Ty);
841
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000842 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000843 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000844 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000845
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000846 }
847
848 return ABIArgInfo::getCoerce(CoerceTo);
849}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000850
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000851ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
852 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000853 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
854 // classification algorithm.
855 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000856 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000857
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000858 // Check some invariants.
859 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
860 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
861 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
862
Daniel Dunbare09a9692009-01-24 08:32:22 +0000863 const llvm::Type *ResType = 0;
864 switch (Lo) {
865 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000866 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000867
868 case SSEUp:
869 case X87Up:
870 assert(0 && "Invalid classification for lo word.");
871
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000872 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000873 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000874 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000875 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000876
877 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
878 // available register of the sequence %rax, %rdx is used.
879 case Integer:
880 ResType = llvm::Type::Int64Ty; break;
881
882 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
883 // available SSE register of the sequence %xmm0, %xmm1 is used.
884 case SSE:
885 ResType = llvm::Type::DoubleTy; break;
886
887 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
888 // returned on the X87 stack in %st0 as 80-bit x87 number.
889 case X87:
890 ResType = llvm::Type::X86_FP80Ty; break;
891
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000892 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
893 // part of the value is returned in %st0 and the imaginary part in
894 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000895 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000896 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000897 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
898 llvm::Type::X86_FP80Ty,
899 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000900 break;
901 }
902
903 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000904 // Memory was handled previously and X87 should
905 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000906 case Memory:
907 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000908 assert(0 && "Invalid classification for hi word.");
909
Daniel Dunbar92e88642009-02-17 07:55:55 +0000910 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000911 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000912
Daniel Dunbare09a9692009-01-24 08:32:22 +0000913 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000914 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
915 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000916 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000917 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
918 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000919
920 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
921 // is passed in the upper half of the last used SSE register.
922 //
923 // SSEUP should always be preceeded by SSE, just widen.
924 case SSEUp:
925 assert(Lo == SSE && "Unexpected SSEUp classification.");
926 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
927 break;
928
929 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000930 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000931 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000932 // If X87Up is preceeded by X87, we don't need to do
933 // anything. However, in some cases with unions it may not be
934 // preceeded by X87. In such situations we follow gcc and pass the
935 // extra bits in an SSE reg.
936 if (Lo != X87)
937 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000938 break;
939 }
940
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000941 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000942}
943
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000944ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000945 unsigned &neededInt,
946 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000947 X86_64ABIInfo::Class Lo, Hi;
948 classify(Ty, Context, 0, Lo, Hi);
949
950 // Check some invariants.
951 // FIXME: Enforce these by construction.
952 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
953 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
954 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
955
Daniel Dunbare978cb92009-02-10 17:06:09 +0000956 neededInt = 0;
957 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000958 const llvm::Type *ResType = 0;
959 switch (Lo) {
960 case NoClass:
961 return ABIArgInfo::getIgnore();
962
963 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
964 // on the stack.
965 case Memory:
966
967 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
968 // COMPLEX_X87, it is passed in memory.
969 case X87:
970 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000971 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000972
973 case SSEUp:
974 case X87Up:
975 assert(0 && "Invalid classification for lo word.");
976
977 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
978 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
979 // and %r9 is used.
980 case Integer:
981 ++neededInt;
982 ResType = llvm::Type::Int64Ty;
983 break;
984
985 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
986 // available SSE register is used, the registers are taken in the
987 // order from %xmm0 to %xmm7.
988 case SSE:
989 ++neededSSE;
990 ResType = llvm::Type::DoubleTy;
991 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000992 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000993
994 switch (Hi) {
995 // Memory was handled previously, ComplexX87 and X87 should
996 // never occur as hi classes, and X87Up must be preceed by X87,
997 // which is passed in memory.
998 case Memory:
999 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001000 case ComplexX87:
1001 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001002 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001003
1004 case NoClass: break;
1005 case Integer:
1006 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1007 ++neededInt;
1008 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001009
1010 // X87Up generally doesn't occur here (long double is passed in
1011 // memory), except in situations involving unions.
1012 case X87Up:
1013 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001014 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1015 ++neededSSE;
1016 break;
1017
1018 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1019 // eightbyte is passed in the upper half of the last used SSE
1020 // register.
1021 case SSEUp:
1022 assert(Lo == SSE && "Unexpected SSEUp classification.");
1023 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1024 break;
1025 }
1026
Daniel Dunbar87c4dc92009-02-14 02:09:24 +00001027 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001028}
1029
1030void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1031 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1032
1033 // Keep track of the number of assigned registers.
1034 unsigned freeIntRegs = 6, freeSSERegs = 8;
1035
1036 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1037 // get assigned (in left-to-right order) for passing as follows...
1038 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +00001039 it != ie; ++it) {
1040 unsigned neededInt, neededSSE;
1041 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1042
1043 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1044 // eightbyte of an argument, the whole argument is passed on the
1045 // stack. If registers have already been assigned for some
1046 // eightbytes of such an argument, the assignments get reverted.
1047 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1048 freeIntRegs -= neededInt;
1049 freeSSERegs -= neededSSE;
1050 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001051 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +00001052 }
1053 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001054}
1055
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001056static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1057 QualType Ty,
1058 CodeGenFunction &CGF) {
1059 llvm::Value *overflow_arg_area_p =
1060 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1061 llvm::Value *overflow_arg_area =
1062 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1063
1064 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1065 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001066 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001067 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001068 // Note that we follow the ABI & gcc here, even though the type
1069 // could in theory have an alignment greater than 16. This case
1070 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001071
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001072 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1073 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1074 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1075 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1076 llvm::Type::Int64Ty);
1077 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1078 overflow_arg_area =
1079 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1080 overflow_arg_area->getType(),
1081 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001082 }
1083
1084 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1085 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1086 llvm::Value *Res =
1087 CGF.Builder.CreateBitCast(overflow_arg_area,
1088 llvm::PointerType::getUnqual(LTy));
1089
1090 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1091 // l->overflow_arg_area + sizeof(type).
1092 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1093 // an 8 byte boundary.
1094
1095 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1096 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1097 (SizeInBytes + 7) & ~7);
1098 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1099 "overflow_arg_area.next");
1100 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1101
1102 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1103 return Res;
1104}
1105
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001106llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1107 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001108 // Assume that va_list type is correct; should be pointer to LLVM type:
1109 // struct {
1110 // i32 gp_offset;
1111 // i32 fp_offset;
1112 // i8* overflow_arg_area;
1113 // i8* reg_save_area;
1114 // };
1115 unsigned neededInt, neededSSE;
1116 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1117 neededInt, neededSSE);
1118
1119 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1120 // in the registers. If not go to step 7.
1121 if (!neededInt && !neededSSE)
1122 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1123
1124 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1125 // general purpose registers needed to pass type and num_fp to hold
1126 // the number of floating point registers needed.
1127
1128 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1129 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1130 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1131 //
1132 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1133 // register save space).
1134
1135 llvm::Value *InRegs = 0;
1136 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1137 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1138 if (neededInt) {
1139 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1140 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1141 InRegs =
1142 CGF.Builder.CreateICmpULE(gp_offset,
1143 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1144 48 - neededInt * 8),
1145 "fits_in_gp");
1146 }
1147
1148 if (neededSSE) {
1149 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1150 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1151 llvm::Value *FitsInFP =
1152 CGF.Builder.CreateICmpULE(fp_offset,
1153 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001154 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001155 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001156 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001157 }
1158
1159 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1160 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1161 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1162 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1163
1164 // Emit code to load the value if it was passed in registers.
1165
1166 CGF.EmitBlock(InRegBlock);
1167
1168 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1169 // an offset of l->gp_offset and/or l->fp_offset. This may require
1170 // copying to a temporary location in case the parameter is passed
1171 // in different register classes or requires an alignment greater
1172 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001173 //
1174 // FIXME: This really results in shameful code when we end up
1175 // needing to collect arguments from different places; often what
1176 // should result in a simple assembling of a structure from
1177 // scattered addresses has many more loads than necessary. Can we
1178 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001179 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1180 llvm::Value *RegAddr =
1181 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1182 "reg_save_area");
1183 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001184 // FIXME: Cleanup.
1185 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1186 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1187 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1188 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1189 const llvm::Type *TyLo = ST->getElementType(0);
1190 const llvm::Type *TyHi = ST->getElementType(1);
1191 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1192 "Unexpected ABI info for mixed regs");
1193 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1194 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1195 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1196 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1197 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1198 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1199 llvm::Value *V =
1200 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1201 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1202 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1203 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1204
1205 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001206 } else if (neededInt) {
1207 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1208 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1209 llvm::PointerType::getUnqual(LTy));
1210 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001211 if (neededSSE == 1) {
1212 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1213 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1214 llvm::PointerType::getUnqual(LTy));
1215 } else {
1216 assert(neededSSE == 2 && "Invalid number of needed registers!");
1217 // SSE registers are spaced 16 bytes apart in the register save
1218 // area, we need to collect the two eightbytes together.
1219 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1220 llvm::Value *RegAddrHi =
1221 CGF.Builder.CreateGEP(RegAddrLo,
1222 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1223 const llvm::Type *DblPtrTy =
1224 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1225 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1226 llvm::Type::DoubleTy,
1227 NULL);
1228 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1229 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1230 DblPtrTy));
1231 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1232 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1233 DblPtrTy));
1234 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1235 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1236 llvm::PointerType::getUnqual(LTy));
1237 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001238 }
1239
1240 // AMD64-ABI 3.5.7p5: Step 5. Set:
1241 // l->gp_offset = l->gp_offset + num_gp * 8
1242 // l->fp_offset = l->fp_offset + num_fp * 16.
1243 if (neededInt) {
1244 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1245 neededInt * 8);
1246 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1247 gp_offset_p);
1248 }
1249 if (neededSSE) {
1250 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1251 neededSSE * 16);
1252 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1253 fp_offset_p);
1254 }
1255 CGF.EmitBranch(ContBlock);
1256
1257 // Emit code to load the value if it was passed in memory.
1258
1259 CGF.EmitBlock(InMemBlock);
1260 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1261
1262 // Return the appropriate result.
1263
1264 CGF.EmitBlock(ContBlock);
1265 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1266 "vaarg.addr");
1267 ResAddr->reserveOperandSpace(2);
1268 ResAddr->addIncoming(RegAddr, InRegBlock);
1269 ResAddr->addIncoming(MemAddr, InMemBlock);
1270
1271 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001272}
1273
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001274// ABI Info for PIC16
1275class PIC16ABIInfo : public ABIInfo {
1276 ABIArgInfo classifyReturnType(QualType RetTy,
1277 ASTContext &Context) const;
1278
1279 ABIArgInfo classifyArgumentType(QualType RetTy,
1280 ASTContext &Context) const;
1281
1282 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1283 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1284 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1285 it != ie; ++it)
1286 it->info = classifyArgumentType(it->type, Context);
1287 }
1288
1289 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1290 CodeGenFunction &CGF) const;
1291
1292};
1293
1294ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1295 ASTContext &Context) const {
1296 if (RetTy->isVoidType()) {
1297 return ABIArgInfo::getIgnore();
1298 } else {
1299 return ABIArgInfo::getDirect();
1300 }
1301}
1302
1303ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1304 ASTContext &Context) const {
1305 return ABIArgInfo::getDirect();
1306}
1307
1308llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1309 CodeGenFunction &CGF) const {
1310 return 0;
1311}
1312
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001313class ARMABIInfo : public ABIInfo {
1314 ABIArgInfo classifyReturnType(QualType RetTy,
1315 ASTContext &Context) const;
1316
1317 ABIArgInfo classifyArgumentType(QualType RetTy,
1318 ASTContext &Context) const;
1319
1320 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1321
1322 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1323 CodeGenFunction &CGF) const;
1324};
1325
1326void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1327 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1328 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1329 it != ie; ++it) {
1330 it->info = classifyArgumentType(it->type, Context);
1331 }
1332}
1333
1334ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1335 ASTContext &Context) const {
1336 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1337 return ABIArgInfo::getDirect();
1338 }
1339 // FIXME: This is kind of nasty... but there isn't much choice
1340 // because the ARM backend doesn't support byval.
1341 // FIXME: This doesn't handle alignment > 64 bits.
1342 const llvm::Type* ElemTy;
1343 unsigned SizeRegs;
1344 if (Context.getTypeAlign(Ty) > 32) {
1345 ElemTy = llvm::Type::Int64Ty;
1346 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1347 } else {
1348 ElemTy = llvm::Type::Int32Ty;
1349 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1350 }
1351 std::vector<const llvm::Type*> LLVMFields;
1352 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1353 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1354 return ABIArgInfo::getCoerce(STy);
1355}
1356
1357ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1358 ASTContext &Context) const {
1359 if (RetTy->isVoidType()) {
1360 return ABIArgInfo::getIgnore();
1361 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1362 // Aggregates <= 4 bytes are returned in r0; other aggregates
1363 // are returned indirectly.
1364 uint64_t Size = Context.getTypeSize(RetTy);
1365 if (Size <= 32)
1366 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1367 return ABIArgInfo::getIndirect(0);
1368 } else {
1369 return ABIArgInfo::getDirect();
1370 }
1371}
1372
1373llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1374 CodeGenFunction &CGF) const {
1375 // FIXME: Need to handle alignment
1376 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1377 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1378
1379 CGBuilderTy &Builder = CGF.Builder;
1380 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1381 "ap");
1382 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1383 llvm::Type *PTy =
1384 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1385 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1386
1387 uint64_t Offset =
1388 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1389 llvm::Value *NextAddr =
1390 Builder.CreateGEP(Addr,
1391 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1392 "ap.next");
1393 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1394
1395 return AddrTyped;
1396}
1397
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001398ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001399 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001400 if (RetTy->isVoidType()) {
1401 return ABIArgInfo::getIgnore();
1402 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001403 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001404 } else {
1405 return ABIArgInfo::getDirect();
1406 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001407}
1408
1409ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001410 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001411 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001412 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001413 } else {
1414 return ABIArgInfo::getDirect();
1415 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001416}
1417
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001418llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1419 CodeGenFunction &CGF) const {
1420 return 0;
1421}
1422
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001423const ABIInfo &CodeGenTypes::getABIInfo() const {
1424 if (TheABIInfo)
1425 return *TheABIInfo;
1426
1427 // For now we just cache this in the CodeGenTypes and don't bother
1428 // to free it.
1429 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1430 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman5e175802009-03-23 23:26:24 +00001431 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001432 switch (getContext().Target.getPointerWidth(0)) {
1433 case 32:
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001434 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001435 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001436 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001437 }
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001438 } else if (strcmp(TargetPrefix, "arm") == 0) {
1439 // FIXME: Support for OABI?
1440 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001441 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1442 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001443 }
1444
1445 return *(TheABIInfo = new DefaultABIInfo);
1446}
1447
Daniel Dunbare126ab12008-09-10 02:41:04 +00001448/***/
1449
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001450CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1451 const llvm::SmallVector<QualType, 16> &ArgTys) {
1452 NumArgs = ArgTys.size();
1453 Args = new ArgInfo[1 + NumArgs];
1454 Args[0].type = ResTy;
1455 for (unsigned i = 0; i < NumArgs; ++i)
1456 Args[1 + i].type = ArgTys[i];
1457}
1458
1459/***/
1460
Daniel Dunbar04d35782008-09-17 00:51:38 +00001461void CodeGenTypes::GetExpandedTypes(QualType Ty,
1462 std::vector<const llvm::Type*> &ArgTys) {
1463 const RecordType *RT = Ty->getAsStructureType();
1464 assert(RT && "Can only expand structure types.");
1465 const RecordDecl *RD = RT->getDecl();
1466 assert(!RD->hasFlexibleArrayMember() &&
1467 "Cannot expand structure with flexible array.");
1468
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001469 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1470 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001471 const FieldDecl *FD = *i;
1472 assert(!FD->isBitField() &&
1473 "Cannot expand structure with bit-field members.");
1474
1475 QualType FT = FD->getType();
1476 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1477 GetExpandedTypes(FT, ArgTys);
1478 } else {
1479 ArgTys.push_back(ConvertType(FT));
1480 }
1481 }
1482}
1483
1484llvm::Function::arg_iterator
1485CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1486 llvm::Function::arg_iterator AI) {
1487 const RecordType *RT = Ty->getAsStructureType();
1488 assert(RT && "Can only expand structure types.");
1489
1490 RecordDecl *RD = RT->getDecl();
1491 assert(LV.isSimple() &&
1492 "Unexpected non-simple lvalue during struct expansion.");
1493 llvm::Value *Addr = LV.getAddress();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001494 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1495 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001496 FieldDecl *FD = *i;
1497 QualType FT = FD->getType();
1498
1499 // FIXME: What are the right qualifiers here?
1500 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1501 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1502 AI = ExpandTypeFromArgs(FT, LV, AI);
1503 } else {
1504 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1505 ++AI;
1506 }
1507 }
1508
1509 return AI;
1510}
1511
1512void
1513CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1514 llvm::SmallVector<llvm::Value*, 16> &Args) {
1515 const RecordType *RT = Ty->getAsStructureType();
1516 assert(RT && "Can only expand structure types.");
1517
1518 RecordDecl *RD = RT->getDecl();
1519 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1520 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001521 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1522 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001523 FieldDecl *FD = *i;
1524 QualType FT = FD->getType();
1525
1526 // FIXME: What are the right qualifiers here?
1527 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1528 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1529 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1530 } else {
1531 RValue RV = EmitLoadOfLValue(LV, FT);
1532 assert(RV.isScalar() &&
1533 "Unexpected non-scalar rvalue during struct expansion.");
1534 Args.push_back(RV.getScalarVal());
1535 }
1536 }
1537}
1538
Daniel Dunbar84379912009-02-02 19:06:38 +00001539/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1540/// a pointer to an object of type \arg Ty.
1541///
1542/// This safely handles the case when the src type is smaller than the
1543/// destination type; in this situation the values of bits which not
1544/// present in the src are undefined.
1545static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1546 const llvm::Type *Ty,
1547 CodeGenFunction &CGF) {
1548 const llvm::Type *SrcTy =
1549 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1550 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1551 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1552
Daniel Dunbar77071992009-02-03 05:59:18 +00001553 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001554 if (SrcSize == DstSize) {
1555 llvm::Value *Casted =
1556 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001557 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1558 // FIXME: Use better alignment / avoid requiring aligned load.
1559 Load->setAlignment(1);
1560 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001561 } else {
1562 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1563
1564 // Otherwise do coercion through memory. This is stupid, but
1565 // simple.
1566 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1567 llvm::Value *Casted =
1568 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001569 llvm::StoreInst *Store =
1570 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1571 // FIXME: Use better alignment / avoid requiring aligned store.
1572 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001573 return CGF.Builder.CreateLoad(Tmp);
1574 }
1575}
1576
1577/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1578/// where the source and destination may have different types.
1579///
1580/// This safely handles the case when the src type is larger than the
1581/// destination type; the upper bits of the src will be lost.
1582static void CreateCoercedStore(llvm::Value *Src,
1583 llvm::Value *DstPtr,
1584 CodeGenFunction &CGF) {
1585 const llvm::Type *SrcTy = Src->getType();
1586 const llvm::Type *DstTy =
1587 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1588
1589 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1590 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1591
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001592 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001593 if (SrcSize == DstSize) {
1594 llvm::Value *Casted =
1595 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001596 // FIXME: Use better alignment / avoid requiring aligned store.
1597 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001598 } else {
1599 assert(SrcSize > DstSize && "Coercion is missing bits!");
1600
1601 // Otherwise do coercion through memory. This is stupid, but
1602 // simple.
1603 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1604 CGF.Builder.CreateStore(Src, Tmp);
1605 llvm::Value *Casted =
1606 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001607 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1608 // FIXME: Use better alignment / avoid requiring aligned load.
1609 Load->setAlignment(1);
1610 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001611 }
1612}
1613
Daniel Dunbar04d35782008-09-17 00:51:38 +00001614/***/
1615
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001616bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001617 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001618}
1619
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001620const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001621CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001622 std::vector<const llvm::Type*> ArgTys;
1623
1624 const llvm::Type *ResultType = 0;
1625
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001626 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001627 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001628 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001629 case ABIArgInfo::Expand:
1630 assert(0 && "Invalid ABI kind for return argument");
1631
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001632 case ABIArgInfo::Direct:
1633 ResultType = ConvertType(RetTy);
1634 break;
1635
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001636 case ABIArgInfo::Indirect: {
1637 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001638 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001639 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001640 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1641 break;
1642 }
1643
Daniel Dunbar1358b202009-01-26 21:26:08 +00001644 case ABIArgInfo::Ignore:
1645 ResultType = llvm::Type::VoidTy;
1646 break;
1647
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001648 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001649 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001650 break;
1651 }
1652
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001653 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1654 ie = FI.arg_end(); it != ie; ++it) {
1655 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001656
1657 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001658 case ABIArgInfo::Ignore:
1659 break;
1660
Daniel Dunbar04d35782008-09-17 00:51:38 +00001661 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001662 ArgTys.push_back(AI.getCoerceToType());
1663 break;
1664
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001665 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001666 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001667 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1668 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001669 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001670 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001671
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001672 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001673 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001674 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001675
1676 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001677 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001678 break;
1679 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001680 }
1681
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001682 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001683}
1684
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001685void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001686 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001687 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001688 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001689 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001690
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001691 // FIXME: handle sseregparm someday...
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001692 if (TargetDecl) {
Daniel Dunbar78582862009-04-13 21:08:27 +00001693 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001694 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar78582862009-04-13 21:08:27 +00001695 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001696 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbar78582862009-04-13 21:08:27 +00001697 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssondd6791c2008-10-05 23:32:53 +00001698 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar78582862009-04-13 21:08:27 +00001699 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar521c3a32009-04-10 22:14:52 +00001700 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001701 }
1702
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001703 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001704 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001705 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001706 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001707 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001708 if (RetTy->isPromotableIntegerType()) {
1709 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001710 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001711 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001712 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001713 }
1714 }
1715 break;
1716
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001717 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001718 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001719 llvm::Attribute::StructRet |
1720 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001721 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001722 // sret disables readnone and readonly
1723 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1724 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001725 break;
1726
Daniel Dunbar1358b202009-01-26 21:26:08 +00001727 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001728 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001729 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001730
Daniel Dunbar22e30052008-09-11 01:48:57 +00001731 case ABIArgInfo::Expand:
1732 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001733 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001734
Devang Patel2bb6eb82008-09-26 22:53:57 +00001735 if (RetAttrs)
1736 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001737
1738 // FIXME: we need to honour command line settings also...
1739 // FIXME: RegParm should be reduced in case of nested functions and/or global
1740 // register variable.
1741 signed RegParm = 0;
1742 if (TargetDecl)
1743 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1744 RegParm = RegParmAttr->getNumParams();
1745
1746 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001747 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1748 ie = FI.arg_end(); it != ie; ++it) {
1749 QualType ParamType = it->type;
1750 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001751 unsigned Attributes = 0;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001752
Daniel Dunbar22e30052008-09-11 01:48:57 +00001753 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001754 case ABIArgInfo::Coerce:
1755 break;
1756
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001757 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001758 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001759 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001760 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001761 // byval disables readnone and readonly.
1762 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1763 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001764 break;
1765
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001766 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001767 if (ParamType->isPromotableIntegerType()) {
1768 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001769 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001770 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001771 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001772 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001773 }
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001774 if (RegParm > 0 &&
1775 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1776 RegParm -=
1777 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1778 if (RegParm >= 0)
1779 Attributes |= llvm::Attribute::InReg;
1780 }
1781 // FIXME: handle sseregparm someday...
Daniel Dunbar22e30052008-09-11 01:48:57 +00001782 break;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001783
Daniel Dunbar1358b202009-01-26 21:26:08 +00001784 case ABIArgInfo::Ignore:
1785 // Skip increment, no matching LLVM parameter.
1786 continue;
1787
Daniel Dunbar04d35782008-09-17 00:51:38 +00001788 case ABIArgInfo::Expand: {
1789 std::vector<const llvm::Type*> Tys;
1790 // FIXME: This is rather inefficient. Do we ever actually need
1791 // to do anything here? The result should be just reconstructed
1792 // on the other side, so extension should be a non-issue.
1793 getTypes().GetExpandedTypes(ParamType, Tys);
1794 Index += Tys.size();
1795 continue;
1796 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001797 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001798
Devang Patela85a9ef2008-09-25 21:02:23 +00001799 if (Attributes)
1800 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001801 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001802 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001803 if (FuncAttrs)
1804 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001805}
1806
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001807void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1808 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001809 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001810 // FIXME: We no longer need the types from FunctionArgList; lift up
1811 // and simplify.
1812
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001813 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1814 llvm::Function::arg_iterator AI = Fn->arg_begin();
1815
1816 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001817 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001818 AI->setName("agg.result");
1819 ++AI;
1820 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001821
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001822 assert(FI.arg_size() == Args.size() &&
1823 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001824 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001825 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001826 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001827 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001828 QualType Ty = info_it->type;
1829 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001830
1831 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001832 case ABIArgInfo::Indirect: {
1833 llvm::Value* V = AI;
1834 if (hasAggregateLLVMType(Ty)) {
1835 // Do nothing, aggregates and complex variables are accessed by
1836 // reference.
1837 } else {
1838 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001839 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001840 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1841 // This must be a promotion, for something like
1842 // "void a(x) short x; {..."
1843 V = EmitScalarConversion(V, Ty, Arg->getType());
1844 }
1845 }
1846 EmitParmDecl(*Arg, V);
1847 break;
1848 }
1849
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001850 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001851 assert(AI != Fn->arg_end() && "Argument mismatch!");
1852 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001853 if (hasAggregateLLVMType(Ty)) {
1854 // Create a temporary alloca to hold the argument; the rest of
1855 // codegen expects to access aggregates & complex values by
1856 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001857 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001858 Builder.CreateStore(AI, V);
1859 } else {
1860 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1861 // This must be a promotion, for something like
1862 // "void a(x) short x; {..."
1863 V = EmitScalarConversion(V, Ty, Arg->getType());
1864 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001865 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001866 EmitParmDecl(*Arg, V);
1867 break;
1868 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001869
1870 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001871 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001872 // we need to create a temporary and reconstruct it from the
1873 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001874 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001875 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001876 (Name + ".addr").c_str());
1877 // FIXME: What are the right qualifiers here?
1878 llvm::Function::arg_iterator End =
1879 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1880 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001881
Daniel Dunbar04d35782008-09-17 00:51:38 +00001882 // Name the arguments used in expansion and increment AI.
1883 unsigned Index = 0;
1884 for (; AI != End; ++AI, ++Index)
1885 AI->setName(Name + "." + llvm::utostr(Index));
1886 continue;
1887 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001888
1889 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001890 // Initialize the local variable appropriately.
1891 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001892 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001893 } else {
1894 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1895 }
1896
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001897 // Skip increment, no matching LLVM parameter.
1898 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001899
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001900 case ABIArgInfo::Coerce: {
1901 assert(AI != Fn->arg_end() && "Argument mismatch!");
1902 // FIXME: This is very wasteful; EmitParmDecl is just going to
1903 // drop the result in a new alloca anyway, so we could just
1904 // store into that directly if we broke the abstraction down
1905 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001906 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001907 CreateCoercedStore(AI, V, *this);
1908 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001909 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001910 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001911 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1912 // This must be a promotion, for something like
1913 // "void a(x) short x; {..."
1914 V = EmitScalarConversion(V, Ty, Arg->getType());
1915 }
1916 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001917 EmitParmDecl(*Arg, V);
1918 break;
1919 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001920 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001921
1922 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001923 }
1924 assert(AI == Fn->arg_end() && "Argument mismatch!");
1925}
1926
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001927void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001928 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001929 llvm::Value *RV = 0;
1930
1931 // Functions with no result always return void.
1932 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001933 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001934 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001935
1936 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001937 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001938 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001939 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1940 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1941 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1942 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1943 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001944 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1945 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001946 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001947 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001948
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001949 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001950 // The internal return value temp always will have
1951 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001952 RV = Builder.CreateLoad(ReturnValue);
1953 break;
1954
Daniel Dunbar1358b202009-01-26 21:26:08 +00001955 case ABIArgInfo::Ignore:
1956 break;
1957
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001958 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001959 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001960 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001961
Daniel Dunbar22e30052008-09-11 01:48:57 +00001962 case ABIArgInfo::Expand:
1963 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001964 }
1965 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001966
1967 if (RV) {
1968 Builder.CreateRet(RV);
1969 } else {
1970 Builder.CreateRetVoid();
1971 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001972}
1973
Anders Carlssond927fa72009-04-08 20:47:54 +00001974RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
1975 return EmitAnyExprToTemp(E);
1976}
1977
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001978RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1979 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001980 const CallArgList &CallArgs,
1981 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001982 // FIXME: We no longer need the types from CallArgs; lift up and
1983 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001984 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001985
1986 // Handle struct-return functions by passing a pointer to the
1987 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001988 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001989 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001990 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001991 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001992 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001993 }
1994
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001995 assert(CallInfo.arg_size() == CallArgs.size() &&
1996 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001997 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001998 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001999 I != E; ++I, ++info_it) {
2000 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002001 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00002002
2003 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002004 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002005 if (RV.isScalar() || RV.isComplex()) {
2006 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002007 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002008 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002009 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002010 else
2011 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2012 } else {
2013 Args.push_back(RV.getAggregateAddr());
2014 }
2015 break;
2016
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002017 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00002018 if (RV.isScalar()) {
2019 Args.push_back(RV.getScalarVal());
2020 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002021 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2022 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2023 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2024 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00002025 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002026 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00002027 }
2028 break;
2029
Daniel Dunbar1358b202009-01-26 21:26:08 +00002030 case ABIArgInfo::Ignore:
2031 break;
2032
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002033 case ABIArgInfo::Coerce: {
2034 // FIXME: Avoid the conversion through memory if possible.
2035 llvm::Value *SrcPtr;
2036 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00002037 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002038 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002039 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002040 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002041 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2042 } else
2043 SrcPtr = RV.getAggregateAddr();
2044 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2045 *this));
2046 break;
2047 }
2048
Daniel Dunbar04d35782008-09-17 00:51:38 +00002049 case ABIArgInfo::Expand:
2050 ExpandTypeToArgs(I->second, RV, Args);
2051 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002052 }
2053 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002054
Daniel Dunbar0a067402009-02-23 17:26:39 +00002055 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00002056 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002057 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00002058 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2059 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00002060
Daniel Dunbar90e43452009-03-02 04:32:35 +00002061 llvm::CallSite CS;
2062 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2063 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002064 } else {
2065 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00002066 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2067 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002068 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00002069 }
2070
Daniel Dunbar90e43452009-03-02 04:32:35 +00002071 CS.setAttributes(Attrs);
2072 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2073 CS.setCallingConv(F->getCallingConv());
2074
2075 // If the call doesn't return, finish the basic block and clear the
2076 // insertion point; this allows the rest of IRgen to discard
2077 // unreachable code.
2078 if (CS.doesNotReturn()) {
2079 Builder.CreateUnreachable();
2080 Builder.ClearInsertionPoint();
2081
2082 // FIXME: For now, emit a dummy basic block because expr
2083 // emitters in generally are not ready to handle emitting
2084 // expressions at unreachable points.
2085 EnsureInsertPoint();
2086
2087 // Return a reasonable RValue.
2088 return GetUndefRValue(RetTy);
2089 }
2090
2091 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +00002092 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002093 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00002094
2095 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002096 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00002097 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00002098 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +00002099 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00002100 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +00002101 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00002102
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002103 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00002104 if (RetTy->isAnyComplexType()) {
2105 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2106 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2107 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +00002108 }
2109 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002110 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00002111 Builder.CreateStore(CI, V);
2112 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002113 }
2114 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00002115
Daniel Dunbar1358b202009-01-26 21:26:08 +00002116 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00002117 // If we are ignoring an argument that had a result, make sure to
2118 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00002119 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00002120
Daniel Dunbar73d66602008-09-10 07:04:09 +00002121 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002122 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002123 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00002124 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00002125 if (RetTy->isAnyComplexType())
2126 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +00002127 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00002128 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002129 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00002130 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00002131
Daniel Dunbar22e30052008-09-11 01:48:57 +00002132 case ABIArgInfo::Expand:
2133 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002134 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00002135
2136 assert(0 && "Unhandled ABIArgInfo::Kind");
2137 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002138}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00002139
2140/* VarArg handling */
2141
2142llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2143 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2144}