blob: a142729343977d69e9d1db5a2723e2a7caeca76c [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 Dunbar8ac59ae2009-05-11 18:58:49 +0000160static bool isEmptyRecord(ASTContext &Context, QualType T);
161
162/// isEmptyField - Return true iff a the field is "empty", that is it
163/// is an unnamed bit-field or an (array of) empty record(s).
164static bool isEmptyField(ASTContext &Context, const FieldDecl *FD) {
165 if (FD->isUnnamedBitfield())
166 return true;
167
168 QualType FT = FD->getType();
Daniel Dunbar46bf0102009-05-11 23:01:34 +0000169 // Constant arrays of empty records count as empty, strip them off.
170 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
171 FT = AT->getElementType();
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000172
173 return isEmptyRecord(Context, FT);
174}
175
176/// isEmptyRecord - Return true iff a structure contains only empty
177/// fields. Note that a structure with a flexible array member is not
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000178/// considered empty.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000179static bool isEmptyRecord(ASTContext &Context, QualType T) {
Daniel Dunbara7446422009-03-31 19:01:39 +0000180 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000181 if (!RT)
182 return 0;
183 const RecordDecl *RD = RT->getDecl();
184 if (RD->hasFlexibleArrayMember())
185 return false;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000186 for (RecordDecl::field_iterator i = RD->field_begin(Context),
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000187 e = RD->field_end(Context); i != e; ++i)
188 if (!isEmptyField(Context, *i))
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000189 return false;
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000190 return true;
191}
192
193/// isSingleElementStruct - Determine if a structure is a "single
194/// element struct", i.e. it has exactly one non-empty field or
195/// exactly one field which is itself a single element
196/// struct. Structures with flexible array members are never
197/// considered single element structs.
198///
199/// \return The field declaration for the single non-empty field, if
200/// it exists.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000201static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000202 const RecordType *RT = T->getAsStructureType();
203 if (!RT)
204 return 0;
205
206 const RecordDecl *RD = RT->getDecl();
207 if (RD->hasFlexibleArrayMember())
208 return 0;
209
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000210 const Type *Found = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(Context),
212 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000213 const FieldDecl *FD = *i;
214 QualType FT = FD->getType();
215
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000216 // Ignore empty fields.
217 if (isEmptyField(Context, FD))
218 continue;
219
Daniel Dunbar46bf0102009-05-11 23:01:34 +0000220 // If we already found an element then this isn't a single-element
221 // struct.
Daniel Dunbar6f3d7602009-05-08 21:04:47 +0000222 if (Found)
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000223 return 0;
Daniel Dunbar6f3d7602009-05-08 21:04:47 +0000224
Daniel Dunbar46bf0102009-05-11 23:01:34 +0000225 // Treat single element arrays as the element.
226 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
227 if (AT->getSize().getZExtValue() != 1)
228 break;
229 FT = AT->getElementType();
230 }
231
Daniel Dunbar6f3d7602009-05-08 21:04:47 +0000232 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000233 Found = FT.getTypePtr();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000234 } else {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000235 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000236 if (!Found)
237 return 0;
238 }
239 }
240
241 return Found;
242}
243
244static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
245 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
246 return false;
247
248 uint64_t Size = Context.getTypeSize(Ty);
249 return Size == 32 || Size == 64;
250}
251
252static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
253 ASTContext &Context) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000254 for (RecordDecl::field_iterator i = RD->field_begin(Context),
255 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000256 const FieldDecl *FD = *i;
257
258 if (!is32Or64BitBasicType(FD->getType(), Context))
259 return false;
260
Daniel Dunbaref495d42009-04-27 18:31:32 +0000261 // FIXME: Reject bit-fields wholesale; there are two problems, we
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000262 // don't know how to expand them yet, and the predicate for
263 // telling if a bitfield still counts as "basic" is more
264 // complicated than what we were doing previously.
265 if (FD->isBitField())
266 return false;
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000267 }
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000268
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000269 return true;
270}
271
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000272namespace {
273/// DefaultABIInfo - The default implementation for ABI specific
274/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000275/// self-consistent and sensible LLVM IR generation, but does not
276/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000277class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000278 ABIArgInfo classifyReturnType(QualType RetTy,
279 ASTContext &Context) const;
280
281 ABIArgInfo classifyArgumentType(QualType RetTy,
282 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000283
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000284 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
285 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
286 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
287 it != ie; ++it)
288 it->info = classifyArgumentType(it->type, Context);
289 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000290
291 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
292 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000293};
294
295/// X86_32ABIInfo - The X86-32 ABI information.
296class X86_32ABIInfo : public ABIInfo {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000297 ASTContext &Context;
Eli Friedman5e175802009-03-23 23:26:24 +0000298 bool IsDarwin;
299
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000300 static bool isRegisterSize(unsigned Size) {
301 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
302 }
303
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000304 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
305
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000306public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000307 ABIArgInfo classifyReturnType(QualType RetTy,
308 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000309
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000310 ABIArgInfo classifyArgumentType(QualType RetTy,
311 ASTContext &Context) const;
312
313 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
314 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
315 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
316 it != ie; ++it)
317 it->info = classifyArgumentType(it->type, Context);
318 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000319
320 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
321 CodeGenFunction &CGF) const;
Eli Friedman5e175802009-03-23 23:26:24 +0000322
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000323 X86_32ABIInfo(ASTContext &Context, bool d)
324 : ABIInfo(), Context(Context), IsDarwin(d) {}
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000325};
326}
327
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000328
329/// shouldReturnTypeInRegister - Determine if the given type should be
330/// passed in a register (for the Darwin ABI).
331bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
332 ASTContext &Context) {
333 uint64_t Size = Context.getTypeSize(Ty);
334
335 // Type must be register sized.
336 if (!isRegisterSize(Size))
337 return false;
338
339 if (Ty->isVectorType()) {
340 // 64- and 128- bit vectors inside structures are not returned in
341 // registers.
342 if (Size == 64 || Size == 128)
343 return false;
344
345 return true;
346 }
347
348 // If this is a builtin, pointer, or complex type, it is ok.
349 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
350 return true;
351
352 // Arrays are treated like records.
353 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
354 return shouldReturnTypeInRegister(AT->getElementType(), Context);
355
356 // Otherwise, it must be a record type.
357 const RecordType *RT = Ty->getAsRecordType();
358 if (!RT) return false;
359
360 // Structure types are passed in register if all fields would be
361 // passed in a register.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000362 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context),
363 e = RT->getDecl()->field_end(Context); i != e; ++i) {
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000364 const FieldDecl *FD = *i;
365
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000366 // Empty fields are ignored.
367 if (isEmptyField(Context, FD))
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000368 continue;
369
370 // Check fields recursively.
371 if (!shouldReturnTypeInRegister(FD->getType(), Context))
372 return false;
373 }
374
375 return true;
376}
377
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000378ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
379 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000380 if (RetTy->isVoidType()) {
381 return ABIArgInfo::getIgnore();
Daniel Dunbar2a7bb3f2009-04-01 06:13:08 +0000382 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
383 // On Darwin, some vectors are returned in registers.
384 if (IsDarwin) {
385 uint64_t Size = Context.getTypeSize(RetTy);
386
387 // 128-bit vectors are a special case; they are returned in
388 // registers and we need to make sure to pick a type the LLVM
389 // backend will like.
390 if (Size == 128)
391 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
392 2));
393
394 // Always return in register if it fits in a general purpose
395 // register, or if it is 64 bits and has a single element.
396 if ((Size == 8 || Size == 16 || Size == 32) ||
397 (Size == 64 && VT->getNumElements() == 1))
398 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
399
400 return ABIArgInfo::getIndirect(0);
401 }
402
403 return ABIArgInfo::getDirect();
Daniel Dunbareec02622009-02-03 06:30:17 +0000404 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaref495d42009-04-27 18:31:32 +0000405 // Structures with flexible arrays are always indirect.
406 if (const RecordType *RT = RetTy->getAsStructureType())
407 if (RT->getDecl()->hasFlexibleArrayMember())
408 return ABIArgInfo::getIndirect(0);
409
Eli Friedman5e175802009-03-23 23:26:24 +0000410 // Outside of Darwin, structs and unions are always indirect.
411 if (!IsDarwin && !RetTy->isAnyComplexType())
412 return ABIArgInfo::getIndirect(0);
Daniel Dunbaref495d42009-04-27 18:31:32 +0000413
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000414 // Classify "single element" structs as their element type.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000415 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000416 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000417 if (BT->isIntegerType()) {
Daniel Dunbar9f4a5a42009-05-08 21:30:11 +0000418 // We need to use the size of the structure, padding
419 // bit-fields can adjust that to be larger than the single
420 // element type.
421 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000422 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
423 } else if (BT->getKind() == BuiltinType::Float) {
Daniel Dunbar9f4a5a42009-05-08 21:30:11 +0000424 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
425 "Unexpect single element structure size!");
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000426 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
427 } else if (BT->getKind() == BuiltinType::Double) {
Daniel Dunbar9f4a5a42009-05-08 21:30:11 +0000428 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
429 "Unexpect single element structure size!");
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000430 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
431 }
432 } else if (SeltTy->isPointerType()) {
433 // FIXME: It would be really nice if this could come out as
434 // the proper pointer type.
435 llvm::Type *PtrTy =
436 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
437 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000438 } else if (SeltTy->isVectorType()) {
439 // 64- and 128-bit vectors are never returned in a
440 // register when inside a structure.
441 uint64_t Size = Context.getTypeSize(RetTy);
442 if (Size == 64 || Size == 128)
443 return ABIArgInfo::getIndirect(0);
444
445 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000446 }
447 }
448
Daniel Dunbar73d66602008-09-10 07:04:09 +0000449 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000450 if (isRegisterSize(Size)) {
451 // Always return in register for unions for now.
452 // FIXME: This is wrong, but better than treating as a
453 // structure.
454 if (RetTy->isUnionType())
455 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
456
457 // Small structures which are register sized are generally returned
458 // in a register.
459 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context))
460 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
461 }
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000462
463 return ABIArgInfo::getIndirect(0);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000464 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000465 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000466 }
467}
468
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000469ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000470 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000471 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000472 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000473 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000474 if (const RecordType *RT = Ty->getAsStructureType())
475 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000476 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000477
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000478 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000479 uint64_t Size = Context.getTypeSize(Ty);
480 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000481 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000482
483 // Expand structs with size <= 128-bits which consist only of
484 // basic types (int, long long, float, double, xxx*). This is
485 // non-recursive and does not ignore empty fields.
486 if (const RecordType *RT = Ty->getAsStructureType()) {
487 if (Context.getTypeSize(Ty) <= 4*32 &&
488 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
489 return ABIArgInfo::getExpand();
490 }
491
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000492 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000493 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000494 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000495 }
496}
497
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000498llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
499 CodeGenFunction &CGF) const {
500 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
501 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
502
503 CGBuilderTy &Builder = CGF.Builder;
504 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
505 "ap");
506 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
507 llvm::Type *PTy =
508 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
509 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
510
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000511 uint64_t Offset =
512 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000513 llvm::Value *NextAddr =
514 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000515 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000516 "ap.next");
517 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
518
519 return AddrTyped;
520}
521
Daniel Dunbare09a9692009-01-24 08:32:22 +0000522namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000523/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000524class X86_64ABIInfo : public ABIInfo {
525 enum Class {
526 Integer = 0,
527 SSE,
528 SSEUp,
529 X87,
530 X87Up,
531 ComplexX87,
532 NoClass,
533 Memory
534 };
535
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000536 /// merge - Implement the X86_64 ABI merging algorithm.
537 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000538 /// Merge an accumulating classification \arg Accum with a field
539 /// classification \arg Field.
540 ///
541 /// \param Accum - The accumulating classification. This should
542 /// always be either NoClass or the result of a previous merge
543 /// call. In addition, this should never be Memory (the caller
544 /// should just return Memory for the aggregate).
545 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000546
Daniel Dunbare09a9692009-01-24 08:32:22 +0000547 /// classify - Determine the x86_64 register classes in which the
548 /// given type T should be passed.
549 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000550 /// \param Lo - The classification for the parts of the type
551 /// residing in the low word of the containing object.
552 ///
553 /// \param Hi - The classification for the parts of the type
554 /// residing in the high word of the containing object.
555 ///
556 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000557 /// containing object. Some parameters are classified different
558 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000559 ///
560 /// If a word is unused its result will be NoClass; if a type should
561 /// be passed in Memory then at least the classification of \arg Lo
562 /// will be Memory.
563 ///
564 /// The \arg Lo class will be NoClass iff the argument is ignored.
565 ///
566 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar92e88642009-02-17 07:55:55 +0000567 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000568 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000569 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000570
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000571 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
572 /// to coerce to, chose the best way to pass Ty in the same place
573 /// that \arg CoerceTo would be passed, but while keeping the
574 /// emitted code as simple as possible.
575 ///
576 /// FIXME: Note, this should be cleaned up to just take an
577 /// enumeration of all the ways we might want to pass things,
578 /// instead of constructing an LLVM type. This makes this code more
579 /// explicit, and it makes it clearer that we are also doing this
580 /// for correctness in the case of passing scalar types.
581 ABIArgInfo getCoerceResult(QualType Ty,
582 const llvm::Type *CoerceTo,
583 ASTContext &Context) const;
584
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000585 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000586 ASTContext &Context) const;
587
588 ABIArgInfo classifyArgumentType(QualType Ty,
589 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000590 unsigned &neededInt,
591 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000592
593public:
594 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000595
596 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
597 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000598};
599}
600
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000601X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
602 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000603 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
604 // classified recursively so that always two fields are
605 // considered. The resulting class is calculated according to
606 // the classes of the fields in the eightbyte:
607 //
608 // (a) If both classes are equal, this is the resulting class.
609 //
610 // (b) If one of the classes is NO_CLASS, the resulting class is
611 // the other class.
612 //
613 // (c) If one of the classes is MEMORY, the result is the MEMORY
614 // class.
615 //
616 // (d) If one of the classes is INTEGER, the result is the
617 // INTEGER.
618 //
619 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
620 // MEMORY is used as class.
621 //
622 // (f) Otherwise class SSE is used.
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000623
624 // Accum should never be memory (we should have returned) or
625 // ComplexX87 (because this cannot be passed in a structure).
626 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000627 "Invalid accumulated classification during merge.");
628 if (Accum == Field || Field == NoClass)
629 return Accum;
630 else if (Field == Memory)
631 return Memory;
632 else if (Accum == NoClass)
633 return Field;
634 else if (Accum == Integer || Field == Integer)
635 return Integer;
Daniel Dunbar647e30f2009-05-12 15:22:40 +0000636 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
637 Accum == X87 || Accum == X87Up)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000638 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000639 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000640 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000641}
642
Daniel Dunbare09a9692009-01-24 08:32:22 +0000643void X86_64ABIInfo::classify(QualType Ty,
644 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000645 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000646 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000647 // FIXME: This code can be simplified by introducing a simple value
648 // class for Class pairs with appropriate constructor methods for
649 // the various situations.
650
Daniel Dunbard97f5952009-02-22 04:48:22 +0000651 // FIXME: Some of the split computations are wrong; unaligned
652 // vectors shouldn't be passed in registers for example, so there is
653 // no chance they can straddle an eightbyte. Verify & simplify.
654
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000655 Lo = Hi = NoClass;
656
657 Class &Current = OffsetBase < 64 ? Lo : Hi;
658 Current = Memory;
659
Daniel Dunbare09a9692009-01-24 08:32:22 +0000660 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
661 BuiltinType::Kind k = BT->getKind();
662
Daniel Dunbar1358b202009-01-26 21:26:08 +0000663 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000664 Current = NoClass;
Chris Lattner6cc7e412009-04-30 02:43:43 +0000665 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattner3d8e0682009-04-30 06:22:07 +0000666 Lo = Integer;
667 Hi = Integer;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000668 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000669 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000670 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000671 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000672 } else if (k == BuiltinType::LongDouble) {
673 Lo = X87;
674 Hi = X87Up;
675 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000676 // FIXME: _Decimal32 and _Decimal64 are SSE.
677 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson1d234462009-02-26 17:31:15 +0000678 } else if (const EnumType *ET = Ty->getAsEnumType()) {
679 // Classify the underlying integer type.
680 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000681 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000682 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000683 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000684 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000685 if (Size == 32) {
686 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
687 // float> as integer.
688 Current = Integer;
689
690 // If this type crosses an eightbyte boundary, it should be
691 // split.
692 uint64_t EB_Real = (OffsetBase) / 64;
693 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
694 if (EB_Real != EB_Imag)
695 Hi = Lo;
696 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000697 // gcc passes <1 x double> in memory. :(
698 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000699 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000700
701 // gcc passes <1 x long long> as INTEGER.
702 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
703 Current = Integer;
704 else
705 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000706
707 // If this type crosses an eightbyte boundary, it should be
708 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000709 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000710 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000711 } else if (Size == 128) {
712 Lo = SSE;
713 Hi = SSEUp;
714 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000715 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000716 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000717
Daniel Dunbare413f532009-01-30 18:40:10 +0000718 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000719 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000720 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000721 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000722 else if (Size <= 128)
723 Lo = Hi = Integer;
724 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000725 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000726 else if (ET == Context.DoubleTy)
727 Lo = Hi = SSE;
728 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000729 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000730
731 // If this complex type crosses an eightbyte boundary then it
732 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000733 uint64_t EB_Real = (OffsetBase) / 64;
734 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000735 if (Hi == NoClass && EB_Real != EB_Imag)
736 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000737 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
738 // Arrays are treated like structures.
739
740 uint64_t Size = Context.getTypeSize(Ty);
741
742 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
743 // than two eightbytes, ..., it has class MEMORY.
744 if (Size > 128)
745 return;
746
747 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
748 // fields, it has class MEMORY.
749 //
750 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000751 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000752 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000753
754 // Otherwise implement simplified merge. We could be smarter about
755 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000756 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000757 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
758 uint64_t ArraySize = AT->getSize().getZExtValue();
759 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
760 Class FieldLo, FieldHi;
761 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000762 Lo = merge(Lo, FieldLo);
763 Hi = merge(Hi, FieldHi);
764 if (Lo == Memory || Hi == Memory)
765 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000766 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000767
768 // Do post merger cleanup (see below). Only case we worry about is Memory.
769 if (Hi == Memory)
770 Lo = Memory;
771 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000772 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000773 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000774
775 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
776 // than two eightbytes, ..., it has class MEMORY.
777 if (Size > 128)
778 return;
779
780 const RecordDecl *RD = RT->getDecl();
781
782 // Assume variable sized types are passed in memory.
783 if (RD->hasFlexibleArrayMember())
784 return;
785
786 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
787
788 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000789 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000790 unsigned idx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000791 for (RecordDecl::field_iterator i = RD->field_begin(Context),
792 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000793 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000794 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000795
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000796 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
797 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000798 //
Daniel Dunbaref495d42009-04-27 18:31:32 +0000799 // Note, skip this test for bit-fields, see below.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000800 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000801 Lo = Memory;
802 return;
803 }
804
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000805 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000806 //
807 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
808 // exceeds a single eightbyte, each is classified
809 // separately. Each eightbyte gets initialized to class
810 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000811 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000812
Daniel Dunbaref495d42009-04-27 18:31:32 +0000813 // Bit-fields require special handling, they do not force the
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000814 // structure to be passed in memory even if unaligned, and
815 // therefore they can straddle an eightbyte.
816 if (BitField) {
Daniel Dunbar9bb29952009-05-08 22:26:44 +0000817 // Ignore padding bit-fields.
818 if (i->isUnnamedBitfield())
819 continue;
820
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000821 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman5255e7a2009-04-26 19:19:15 +0000822 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000823
824 uint64_t EB_Lo = Offset / 64;
825 uint64_t EB_Hi = (Offset + Size - 1) / 64;
826 FieldLo = FieldHi = NoClass;
827 if (EB_Lo) {
828 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
829 FieldLo = NoClass;
830 FieldHi = Integer;
831 } else {
832 FieldLo = Integer;
833 FieldHi = EB_Hi ? Integer : NoClass;
834 }
835 } else
836 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000837 Lo = merge(Lo, FieldLo);
838 Hi = merge(Hi, FieldHi);
839 if (Lo == Memory || Hi == Memory)
840 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000841 }
842
843 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
844 //
845 // (a) If one of the classes is MEMORY, the whole argument is
846 // passed in memory.
847 //
848 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
849
850 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000851 // the merge (just bail).
852 //
853 // The second condition occurs in the case of unions; for example
854 // union { _Complex double; unsigned; }.
855 if (Hi == Memory)
856 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000857 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000858 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000859 }
860}
861
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000862ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
863 const llvm::Type *CoerceTo,
864 ASTContext &Context) const {
865 if (CoerceTo == llvm::Type::Int64Ty) {
866 // Integer and pointer types will end up in a general purpose
867 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000868 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000869 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000870
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000871 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000872 // FIXME: It would probably be better to make CGFunctionInfo only
873 // map using canonical types than to canonize here.
874 QualType CTy = Context.getCanonicalType(Ty);
875
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000876 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000877 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000878 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000879
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000880 }
881
882 return ABIArgInfo::getCoerce(CoerceTo);
883}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000884
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000885ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
886 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000887 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
888 // classification algorithm.
889 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000890 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000891
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000892 // Check some invariants.
893 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
894 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
895 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
896
Daniel Dunbare09a9692009-01-24 08:32:22 +0000897 const llvm::Type *ResType = 0;
898 switch (Lo) {
899 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000900 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000901
902 case SSEUp:
903 case X87Up:
904 assert(0 && "Invalid classification for lo word.");
905
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000906 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000907 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000908 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000909 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000910
911 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
912 // available register of the sequence %rax, %rdx is used.
913 case Integer:
914 ResType = llvm::Type::Int64Ty; break;
915
916 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
917 // available SSE register of the sequence %xmm0, %xmm1 is used.
918 case SSE:
919 ResType = llvm::Type::DoubleTy; break;
920
921 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
922 // returned on the X87 stack in %st0 as 80-bit x87 number.
923 case X87:
924 ResType = llvm::Type::X86_FP80Ty; break;
925
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000926 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
927 // part of the value is returned in %st0 and the imaginary part in
928 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000929 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000930 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000931 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
932 llvm::Type::X86_FP80Ty,
933 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000934 break;
935 }
936
937 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000938 // Memory was handled previously and X87 should
939 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000940 case Memory:
941 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000942 assert(0 && "Invalid classification for hi word.");
943
Daniel Dunbar92e88642009-02-17 07:55:55 +0000944 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000945 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000946
Daniel Dunbare09a9692009-01-24 08:32:22 +0000947 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000948 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
949 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000950 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000951 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
952 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000953
954 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
955 // is passed in the upper half of the last used SSE register.
956 //
957 // SSEUP should always be preceeded by SSE, just widen.
958 case SSEUp:
959 assert(Lo == SSE && "Unexpected SSEUp classification.");
960 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
961 break;
962
963 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000964 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000965 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000966 // If X87Up is preceeded by X87, we don't need to do
967 // anything. However, in some cases with unions it may not be
968 // preceeded by X87. In such situations we follow gcc and pass the
969 // extra bits in an SSE reg.
970 if (Lo != X87)
971 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000972 break;
973 }
974
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000975 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000976}
977
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000978ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000979 unsigned &neededInt,
980 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000981 X86_64ABIInfo::Class Lo, Hi;
982 classify(Ty, Context, 0, Lo, Hi);
983
984 // Check some invariants.
985 // FIXME: Enforce these by construction.
986 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
987 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
988 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
989
Daniel Dunbare978cb92009-02-10 17:06:09 +0000990 neededInt = 0;
991 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000992 const llvm::Type *ResType = 0;
993 switch (Lo) {
994 case NoClass:
995 return ABIArgInfo::getIgnore();
996
997 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
998 // on the stack.
999 case Memory:
1000
1001 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1002 // COMPLEX_X87, it is passed in memory.
1003 case X87:
1004 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001005 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001006
1007 case SSEUp:
1008 case X87Up:
1009 assert(0 && "Invalid classification for lo word.");
1010
1011 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1012 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1013 // and %r9 is used.
1014 case Integer:
1015 ++neededInt;
1016 ResType = llvm::Type::Int64Ty;
1017 break;
1018
1019 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1020 // available SSE register is used, the registers are taken in the
1021 // order from %xmm0 to %xmm7.
1022 case SSE:
1023 ++neededSSE;
1024 ResType = llvm::Type::DoubleTy;
1025 break;
Daniel Dunbareec02622009-02-03 06:30:17 +00001026 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001027
1028 switch (Hi) {
1029 // Memory was handled previously, ComplexX87 and X87 should
1030 // never occur as hi classes, and X87Up must be preceed by X87,
1031 // which is passed in memory.
1032 case Memory:
1033 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001034 case ComplexX87:
1035 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001036 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001037
1038 case NoClass: break;
1039 case Integer:
1040 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1041 ++neededInt;
1042 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001043
1044 // X87Up generally doesn't occur here (long double is passed in
1045 // memory), except in situations involving unions.
1046 case X87Up:
1047 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001048 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1049 ++neededSSE;
1050 break;
1051
1052 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1053 // eightbyte is passed in the upper half of the last used SSE
1054 // register.
1055 case SSEUp:
1056 assert(Lo == SSE && "Unexpected SSEUp classification.");
1057 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1058 break;
1059 }
1060
Daniel Dunbar87c4dc92009-02-14 02:09:24 +00001061 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001062}
1063
1064void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1065 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1066
1067 // Keep track of the number of assigned registers.
1068 unsigned freeIntRegs = 6, freeSSERegs = 8;
1069
1070 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1071 // get assigned (in left-to-right order) for passing as follows...
1072 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +00001073 it != ie; ++it) {
1074 unsigned neededInt, neededSSE;
1075 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1076
1077 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1078 // eightbyte of an argument, the whole argument is passed on the
1079 // stack. If registers have already been assigned for some
1080 // eightbytes of such an argument, the assignments get reverted.
1081 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1082 freeIntRegs -= neededInt;
1083 freeSSERegs -= neededSSE;
1084 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001085 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +00001086 }
1087 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001088}
1089
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001090static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1091 QualType Ty,
1092 CodeGenFunction &CGF) {
1093 llvm::Value *overflow_arg_area_p =
1094 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1095 llvm::Value *overflow_arg_area =
1096 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1097
1098 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1099 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001100 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001101 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001102 // Note that we follow the ABI & gcc here, even though the type
1103 // could in theory have an alignment greater than 16. This case
1104 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001105
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001106 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1107 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1108 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1109 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1110 llvm::Type::Int64Ty);
1111 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1112 overflow_arg_area =
1113 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1114 overflow_arg_area->getType(),
1115 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001116 }
1117
1118 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1119 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1120 llvm::Value *Res =
1121 CGF.Builder.CreateBitCast(overflow_arg_area,
1122 llvm::PointerType::getUnqual(LTy));
1123
1124 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1125 // l->overflow_arg_area + sizeof(type).
1126 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1127 // an 8 byte boundary.
1128
1129 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1130 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1131 (SizeInBytes + 7) & ~7);
1132 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1133 "overflow_arg_area.next");
1134 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1135
1136 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1137 return Res;
1138}
1139
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001140llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1141 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001142 // Assume that va_list type is correct; should be pointer to LLVM type:
1143 // struct {
1144 // i32 gp_offset;
1145 // i32 fp_offset;
1146 // i8* overflow_arg_area;
1147 // i8* reg_save_area;
1148 // };
1149 unsigned neededInt, neededSSE;
1150 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1151 neededInt, neededSSE);
1152
1153 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1154 // in the registers. If not go to step 7.
1155 if (!neededInt && !neededSSE)
1156 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1157
1158 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1159 // general purpose registers needed to pass type and num_fp to hold
1160 // the number of floating point registers needed.
1161
1162 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1163 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1164 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1165 //
1166 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1167 // register save space).
1168
1169 llvm::Value *InRegs = 0;
1170 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1171 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1172 if (neededInt) {
1173 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1174 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1175 InRegs =
1176 CGF.Builder.CreateICmpULE(gp_offset,
1177 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1178 48 - neededInt * 8),
1179 "fits_in_gp");
1180 }
1181
1182 if (neededSSE) {
1183 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1184 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1185 llvm::Value *FitsInFP =
1186 CGF.Builder.CreateICmpULE(fp_offset,
1187 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001188 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001189 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001190 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001191 }
1192
1193 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1194 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1195 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1196 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1197
1198 // Emit code to load the value if it was passed in registers.
1199
1200 CGF.EmitBlock(InRegBlock);
1201
1202 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1203 // an offset of l->gp_offset and/or l->fp_offset. This may require
1204 // copying to a temporary location in case the parameter is passed
1205 // in different register classes or requires an alignment greater
1206 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001207 //
1208 // FIXME: This really results in shameful code when we end up
1209 // needing to collect arguments from different places; often what
1210 // should result in a simple assembling of a structure from
1211 // scattered addresses has many more loads than necessary. Can we
1212 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001213 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1214 llvm::Value *RegAddr =
1215 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1216 "reg_save_area");
1217 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001218 // FIXME: Cleanup.
1219 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1220 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1221 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1222 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1223 const llvm::Type *TyLo = ST->getElementType(0);
1224 const llvm::Type *TyHi = ST->getElementType(1);
1225 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1226 "Unexpected ABI info for mixed regs");
1227 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1228 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1229 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1230 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1231 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1232 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1233 llvm::Value *V =
1234 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1235 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1236 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1237 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1238
1239 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001240 } else if (neededInt) {
1241 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1242 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1243 llvm::PointerType::getUnqual(LTy));
1244 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001245 if (neededSSE == 1) {
1246 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1247 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1248 llvm::PointerType::getUnqual(LTy));
1249 } else {
1250 assert(neededSSE == 2 && "Invalid number of needed registers!");
1251 // SSE registers are spaced 16 bytes apart in the register save
1252 // area, we need to collect the two eightbytes together.
1253 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1254 llvm::Value *RegAddrHi =
1255 CGF.Builder.CreateGEP(RegAddrLo,
1256 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1257 const llvm::Type *DblPtrTy =
1258 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1259 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1260 llvm::Type::DoubleTy,
1261 NULL);
1262 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1263 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1264 DblPtrTy));
1265 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1266 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1267 DblPtrTy));
1268 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1269 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1270 llvm::PointerType::getUnqual(LTy));
1271 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001272 }
1273
1274 // AMD64-ABI 3.5.7p5: Step 5. Set:
1275 // l->gp_offset = l->gp_offset + num_gp * 8
1276 // l->fp_offset = l->fp_offset + num_fp * 16.
1277 if (neededInt) {
1278 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1279 neededInt * 8);
1280 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1281 gp_offset_p);
1282 }
1283 if (neededSSE) {
1284 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1285 neededSSE * 16);
1286 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1287 fp_offset_p);
1288 }
1289 CGF.EmitBranch(ContBlock);
1290
1291 // Emit code to load the value if it was passed in memory.
1292
1293 CGF.EmitBlock(InMemBlock);
1294 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1295
1296 // Return the appropriate result.
1297
1298 CGF.EmitBlock(ContBlock);
1299 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1300 "vaarg.addr");
1301 ResAddr->reserveOperandSpace(2);
1302 ResAddr->addIncoming(RegAddr, InRegBlock);
1303 ResAddr->addIncoming(MemAddr, InMemBlock);
1304
1305 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001306}
1307
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001308// ABI Info for PIC16
1309class PIC16ABIInfo : public ABIInfo {
1310 ABIArgInfo classifyReturnType(QualType RetTy,
1311 ASTContext &Context) const;
1312
1313 ABIArgInfo classifyArgumentType(QualType RetTy,
1314 ASTContext &Context) const;
1315
1316 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1317 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1318 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1319 it != ie; ++it)
1320 it->info = classifyArgumentType(it->type, Context);
1321 }
1322
1323 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1324 CodeGenFunction &CGF) const;
1325
1326};
1327
1328ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1329 ASTContext &Context) const {
1330 if (RetTy->isVoidType()) {
1331 return ABIArgInfo::getIgnore();
1332 } else {
1333 return ABIArgInfo::getDirect();
1334 }
1335}
1336
1337ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1338 ASTContext &Context) const {
1339 return ABIArgInfo::getDirect();
1340}
1341
1342llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1343 CodeGenFunction &CGF) const {
1344 return 0;
1345}
1346
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001347class ARMABIInfo : public ABIInfo {
1348 ABIArgInfo classifyReturnType(QualType RetTy,
1349 ASTContext &Context) const;
1350
1351 ABIArgInfo classifyArgumentType(QualType RetTy,
1352 ASTContext &Context) const;
1353
1354 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1355
1356 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1357 CodeGenFunction &CGF) const;
1358};
1359
1360void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1361 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1362 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1363 it != ie; ++it) {
1364 it->info = classifyArgumentType(it->type, Context);
1365 }
1366}
1367
1368ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1369 ASTContext &Context) const {
1370 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1371 return ABIArgInfo::getDirect();
1372 }
1373 // FIXME: This is kind of nasty... but there isn't much choice
1374 // because the ARM backend doesn't support byval.
1375 // FIXME: This doesn't handle alignment > 64 bits.
1376 const llvm::Type* ElemTy;
1377 unsigned SizeRegs;
1378 if (Context.getTypeAlign(Ty) > 32) {
1379 ElemTy = llvm::Type::Int64Ty;
1380 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1381 } else {
1382 ElemTy = llvm::Type::Int32Ty;
1383 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1384 }
1385 std::vector<const llvm::Type*> LLVMFields;
1386 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1387 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1388 return ABIArgInfo::getCoerce(STy);
1389}
1390
1391ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1392 ASTContext &Context) const {
1393 if (RetTy->isVoidType()) {
1394 return ABIArgInfo::getIgnore();
1395 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1396 // Aggregates <= 4 bytes are returned in r0; other aggregates
1397 // are returned indirectly.
1398 uint64_t Size = Context.getTypeSize(RetTy);
1399 if (Size <= 32)
1400 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1401 return ABIArgInfo::getIndirect(0);
1402 } else {
1403 return ABIArgInfo::getDirect();
1404 }
1405}
1406
1407llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1408 CodeGenFunction &CGF) const {
1409 // FIXME: Need to handle alignment
1410 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1411 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1412
1413 CGBuilderTy &Builder = CGF.Builder;
1414 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1415 "ap");
1416 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1417 llvm::Type *PTy =
1418 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1419 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1420
1421 uint64_t Offset =
1422 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1423 llvm::Value *NextAddr =
1424 Builder.CreateGEP(Addr,
1425 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1426 "ap.next");
1427 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1428
1429 return AddrTyped;
1430}
1431
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001432ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001433 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001434 if (RetTy->isVoidType()) {
1435 return ABIArgInfo::getIgnore();
1436 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001437 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001438 } else {
1439 return ABIArgInfo::getDirect();
1440 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001441}
1442
1443ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001444 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001445 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001446 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001447 } else {
1448 return ABIArgInfo::getDirect();
1449 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001450}
1451
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001452llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1453 CodeGenFunction &CGF) const {
1454 return 0;
1455}
1456
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001457const ABIInfo &CodeGenTypes::getABIInfo() const {
1458 if (TheABIInfo)
1459 return *TheABIInfo;
1460
1461 // For now we just cache this in the CodeGenTypes and don't bother
1462 // to free it.
1463 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1464 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman5e175802009-03-23 23:26:24 +00001465 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001466 switch (getContext().Target.getPointerWidth(0)) {
1467 case 32:
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001468 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001469 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001470 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001471 }
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001472 } else if (strcmp(TargetPrefix, "arm") == 0) {
1473 // FIXME: Support for OABI?
1474 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001475 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1476 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001477 }
1478
1479 return *(TheABIInfo = new DefaultABIInfo);
1480}
1481
Daniel Dunbare126ab12008-09-10 02:41:04 +00001482/***/
1483
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001484CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1485 const llvm::SmallVector<QualType, 16> &ArgTys) {
1486 NumArgs = ArgTys.size();
1487 Args = new ArgInfo[1 + NumArgs];
1488 Args[0].type = ResTy;
1489 for (unsigned i = 0; i < NumArgs; ++i)
1490 Args[1 + i].type = ArgTys[i];
1491}
1492
1493/***/
1494
Daniel Dunbar04d35782008-09-17 00:51:38 +00001495void CodeGenTypes::GetExpandedTypes(QualType Ty,
1496 std::vector<const llvm::Type*> &ArgTys) {
1497 const RecordType *RT = Ty->getAsStructureType();
1498 assert(RT && "Can only expand structure types.");
1499 const RecordDecl *RD = RT->getDecl();
1500 assert(!RD->hasFlexibleArrayMember() &&
1501 "Cannot expand structure with flexible array.");
1502
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001503 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1504 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001505 const FieldDecl *FD = *i;
1506 assert(!FD->isBitField() &&
1507 "Cannot expand structure with bit-field members.");
1508
1509 QualType FT = FD->getType();
1510 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1511 GetExpandedTypes(FT, ArgTys);
1512 } else {
1513 ArgTys.push_back(ConvertType(FT));
1514 }
1515 }
1516}
1517
1518llvm::Function::arg_iterator
1519CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1520 llvm::Function::arg_iterator AI) {
1521 const RecordType *RT = Ty->getAsStructureType();
1522 assert(RT && "Can only expand structure types.");
1523
1524 RecordDecl *RD = RT->getDecl();
1525 assert(LV.isSimple() &&
1526 "Unexpected non-simple lvalue during struct expansion.");
1527 llvm::Value *Addr = LV.getAddress();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001528 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1529 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001530 FieldDecl *FD = *i;
1531 QualType FT = FD->getType();
1532
1533 // FIXME: What are the right qualifiers here?
1534 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1535 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1536 AI = ExpandTypeFromArgs(FT, LV, AI);
1537 } else {
1538 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1539 ++AI;
1540 }
1541 }
1542
1543 return AI;
1544}
1545
1546void
1547CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1548 llvm::SmallVector<llvm::Value*, 16> &Args) {
1549 const RecordType *RT = Ty->getAsStructureType();
1550 assert(RT && "Can only expand structure types.");
1551
1552 RecordDecl *RD = RT->getDecl();
1553 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1554 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001555 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1556 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001557 FieldDecl *FD = *i;
1558 QualType FT = FD->getType();
1559
1560 // FIXME: What are the right qualifiers here?
1561 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1562 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1563 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1564 } else {
1565 RValue RV = EmitLoadOfLValue(LV, FT);
1566 assert(RV.isScalar() &&
1567 "Unexpected non-scalar rvalue during struct expansion.");
1568 Args.push_back(RV.getScalarVal());
1569 }
1570 }
1571}
1572
Daniel Dunbar84379912009-02-02 19:06:38 +00001573/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1574/// a pointer to an object of type \arg Ty.
1575///
1576/// This safely handles the case when the src type is smaller than the
1577/// destination type; in this situation the values of bits which not
1578/// present in the src are undefined.
1579static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1580 const llvm::Type *Ty,
1581 CodeGenFunction &CGF) {
1582 const llvm::Type *SrcTy =
1583 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001584 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1585 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar84379912009-02-02 19:06:38 +00001586
Daniel Dunbar77071992009-02-03 05:59:18 +00001587 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001588 if (SrcSize == DstSize) {
1589 llvm::Value *Casted =
1590 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001591 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1592 // FIXME: Use better alignment / avoid requiring aligned load.
1593 Load->setAlignment(1);
1594 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001595 } else {
1596 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1597
1598 // Otherwise do coercion through memory. This is stupid, but
1599 // simple.
1600 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1601 llvm::Value *Casted =
1602 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001603 llvm::StoreInst *Store =
1604 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1605 // FIXME: Use better alignment / avoid requiring aligned store.
1606 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001607 return CGF.Builder.CreateLoad(Tmp);
1608 }
1609}
1610
1611/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1612/// where the source and destination may have different types.
1613///
1614/// This safely handles the case when the src type is larger than the
1615/// destination type; the upper bits of the src will be lost.
1616static void CreateCoercedStore(llvm::Value *Src,
1617 llvm::Value *DstPtr,
1618 CodeGenFunction &CGF) {
1619 const llvm::Type *SrcTy = Src->getType();
1620 const llvm::Type *DstTy =
1621 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1622
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001623 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1624 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar84379912009-02-02 19:06:38 +00001625
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001626 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001627 if (SrcSize == DstSize) {
1628 llvm::Value *Casted =
1629 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001630 // FIXME: Use better alignment / avoid requiring aligned store.
1631 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001632 } else {
1633 assert(SrcSize > DstSize && "Coercion is missing bits!");
1634
1635 // Otherwise do coercion through memory. This is stupid, but
1636 // simple.
1637 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1638 CGF.Builder.CreateStore(Src, Tmp);
1639 llvm::Value *Casted =
1640 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001641 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1642 // FIXME: Use better alignment / avoid requiring aligned load.
1643 Load->setAlignment(1);
1644 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001645 }
1646}
1647
Daniel Dunbar04d35782008-09-17 00:51:38 +00001648/***/
1649
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001650bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001651 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001652}
1653
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001654const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001655CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001656 std::vector<const llvm::Type*> ArgTys;
1657
1658 const llvm::Type *ResultType = 0;
1659
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001660 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001661 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001662 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001663 case ABIArgInfo::Expand:
1664 assert(0 && "Invalid ABI kind for return argument");
1665
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001666 case ABIArgInfo::Direct:
1667 ResultType = ConvertType(RetTy);
1668 break;
1669
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001670 case ABIArgInfo::Indirect: {
1671 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001672 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001673 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001674 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1675 break;
1676 }
1677
Daniel Dunbar1358b202009-01-26 21:26:08 +00001678 case ABIArgInfo::Ignore:
1679 ResultType = llvm::Type::VoidTy;
1680 break;
1681
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001682 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001683 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001684 break;
1685 }
1686
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001687 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1688 ie = FI.arg_end(); it != ie; ++it) {
1689 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001690
1691 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001692 case ABIArgInfo::Ignore:
1693 break;
1694
Daniel Dunbar04d35782008-09-17 00:51:38 +00001695 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001696 ArgTys.push_back(AI.getCoerceToType());
1697 break;
1698
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001699 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001700 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001701 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1702 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001703 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001704 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001705
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001706 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001707 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001708 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001709
1710 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001711 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001712 break;
1713 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001714 }
1715
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001716 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001717}
1718
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001719void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001720 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001721 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001722 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001723 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001724
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001725 // FIXME: handle sseregparm someday...
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001726 if (TargetDecl) {
Daniel Dunbar78582862009-04-13 21:08:27 +00001727 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001728 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar78582862009-04-13 21:08:27 +00001729 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001730 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbar78582862009-04-13 21:08:27 +00001731 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssondd6791c2008-10-05 23:32:53 +00001732 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar78582862009-04-13 21:08:27 +00001733 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar521c3a32009-04-10 22:14:52 +00001734 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001735 }
1736
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001737 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001738 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001739 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001740 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001741 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001742 if (RetTy->isPromotableIntegerType()) {
1743 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001744 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001745 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001746 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001747 }
1748 }
1749 break;
1750
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001751 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001752 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001753 llvm::Attribute::StructRet |
1754 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001755 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001756 // sret disables readnone and readonly
1757 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1758 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001759 break;
1760
Daniel Dunbar1358b202009-01-26 21:26:08 +00001761 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001762 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001763 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001764
Daniel Dunbar22e30052008-09-11 01:48:57 +00001765 case ABIArgInfo::Expand:
1766 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001767 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001768
Devang Patel2bb6eb82008-09-26 22:53:57 +00001769 if (RetAttrs)
1770 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001771
1772 // FIXME: we need to honour command line settings also...
1773 // FIXME: RegParm should be reduced in case of nested functions and/or global
1774 // register variable.
1775 signed RegParm = 0;
1776 if (TargetDecl)
1777 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1778 RegParm = RegParmAttr->getNumParams();
1779
1780 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001781 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1782 ie = FI.arg_end(); it != ie; ++it) {
1783 QualType ParamType = it->type;
1784 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001785 unsigned Attributes = 0;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001786
Daniel Dunbar22e30052008-09-11 01:48:57 +00001787 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001788 case ABIArgInfo::Coerce:
1789 break;
1790
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001791 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001792 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001793 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001794 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001795 // byval disables readnone and readonly.
1796 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1797 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001798 break;
1799
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001800 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001801 if (ParamType->isPromotableIntegerType()) {
1802 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001803 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001804 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001805 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001806 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001807 }
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001808 if (RegParm > 0 &&
1809 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1810 RegParm -=
1811 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1812 if (RegParm >= 0)
1813 Attributes |= llvm::Attribute::InReg;
1814 }
1815 // FIXME: handle sseregparm someday...
Daniel Dunbar22e30052008-09-11 01:48:57 +00001816 break;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001817
Daniel Dunbar1358b202009-01-26 21:26:08 +00001818 case ABIArgInfo::Ignore:
1819 // Skip increment, no matching LLVM parameter.
1820 continue;
1821
Daniel Dunbar04d35782008-09-17 00:51:38 +00001822 case ABIArgInfo::Expand: {
1823 std::vector<const llvm::Type*> Tys;
1824 // FIXME: This is rather inefficient. Do we ever actually need
1825 // to do anything here? The result should be just reconstructed
1826 // on the other side, so extension should be a non-issue.
1827 getTypes().GetExpandedTypes(ParamType, Tys);
1828 Index += Tys.size();
1829 continue;
1830 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001831 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001832
Devang Patela85a9ef2008-09-25 21:02:23 +00001833 if (Attributes)
1834 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001835 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001836 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001837 if (FuncAttrs)
1838 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001839}
1840
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001841void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1842 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001843 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001844 // FIXME: We no longer need the types from FunctionArgList; lift up
1845 // and simplify.
1846
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001847 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1848 llvm::Function::arg_iterator AI = Fn->arg_begin();
1849
1850 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001851 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001852 AI->setName("agg.result");
1853 ++AI;
1854 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001855
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001856 assert(FI.arg_size() == Args.size() &&
1857 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001858 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001859 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001860 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001861 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001862 QualType Ty = info_it->type;
1863 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001864
1865 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001866 case ABIArgInfo::Indirect: {
1867 llvm::Value* V = AI;
1868 if (hasAggregateLLVMType(Ty)) {
1869 // Do nothing, aggregates and complex variables are accessed by
1870 // reference.
1871 } else {
1872 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001873 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001874 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1875 // This must be a promotion, for something like
1876 // "void a(x) short x; {..."
1877 V = EmitScalarConversion(V, Ty, Arg->getType());
1878 }
1879 }
1880 EmitParmDecl(*Arg, V);
1881 break;
1882 }
1883
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001884 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001885 assert(AI != Fn->arg_end() && "Argument mismatch!");
1886 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001887 if (hasAggregateLLVMType(Ty)) {
1888 // Create a temporary alloca to hold the argument; the rest of
1889 // codegen expects to access aggregates & complex values by
1890 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001891 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001892 Builder.CreateStore(AI, V);
1893 } else {
1894 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1895 // This must be a promotion, for something like
1896 // "void a(x) short x; {..."
1897 V = EmitScalarConversion(V, Ty, Arg->getType());
1898 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001899 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001900 EmitParmDecl(*Arg, V);
1901 break;
1902 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001903
1904 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001905 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001906 // we need to create a temporary and reconstruct it from the
1907 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001908 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001909 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001910 (Name + ".addr").c_str());
1911 // FIXME: What are the right qualifiers here?
1912 llvm::Function::arg_iterator End =
1913 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1914 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001915
Daniel Dunbar04d35782008-09-17 00:51:38 +00001916 // Name the arguments used in expansion and increment AI.
1917 unsigned Index = 0;
1918 for (; AI != End; ++AI, ++Index)
1919 AI->setName(Name + "." + llvm::utostr(Index));
1920 continue;
1921 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001922
1923 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001924 // Initialize the local variable appropriately.
1925 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001926 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001927 } else {
1928 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1929 }
1930
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001931 // Skip increment, no matching LLVM parameter.
1932 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001933
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001934 case ABIArgInfo::Coerce: {
1935 assert(AI != Fn->arg_end() && "Argument mismatch!");
1936 // FIXME: This is very wasteful; EmitParmDecl is just going to
1937 // drop the result in a new alloca anyway, so we could just
1938 // store into that directly if we broke the abstraction down
1939 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001940 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001941 CreateCoercedStore(AI, V, *this);
1942 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001943 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001944 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001945 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1946 // This must be a promotion, for something like
1947 // "void a(x) short x; {..."
1948 V = EmitScalarConversion(V, Ty, Arg->getType());
1949 }
1950 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001951 EmitParmDecl(*Arg, V);
1952 break;
1953 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001954 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001955
1956 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001957 }
1958 assert(AI == Fn->arg_end() && "Argument mismatch!");
1959}
1960
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001961void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001962 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001963 llvm::Value *RV = 0;
1964
1965 // Functions with no result always return void.
1966 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001967 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001968 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001969
1970 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001971 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001972 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001973 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1974 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1975 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1976 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1977 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001978 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1979 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001980 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001981 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001982
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001983 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001984 // The internal return value temp always will have
1985 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001986 RV = Builder.CreateLoad(ReturnValue);
1987 break;
1988
Daniel Dunbar1358b202009-01-26 21:26:08 +00001989 case ABIArgInfo::Ignore:
1990 break;
1991
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001992 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001993 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001994 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001995
Daniel Dunbar22e30052008-09-11 01:48:57 +00001996 case ABIArgInfo::Expand:
1997 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001998 }
1999 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00002000
2001 if (RV) {
2002 Builder.CreateRet(RV);
2003 } else {
2004 Builder.CreateRetVoid();
2005 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002006}
2007
Anders Carlssond927fa72009-04-08 20:47:54 +00002008RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
2009 return EmitAnyExprToTemp(E);
2010}
2011
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00002012RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
2013 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002014 const CallArgList &CallArgs,
2015 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00002016 // FIXME: We no longer need the types from CallArgs; lift up and
2017 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002018 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002019
2020 // Handle struct-return functions by passing a pointer to the
2021 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00002022 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00002023 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00002024 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002025 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002026 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002027 }
2028
Daniel Dunbar14c884a2009-02-04 21:17:21 +00002029 assert(CallInfo.arg_size() == CallArgs.size() &&
2030 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00002031 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002032 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00002033 I != E; ++I, ++info_it) {
2034 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002035 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00002036
2037 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002038 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002039 if (RV.isScalar() || RV.isComplex()) {
2040 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002041 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002042 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002043 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002044 else
2045 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2046 } else {
2047 Args.push_back(RV.getAggregateAddr());
2048 }
2049 break;
2050
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002051 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00002052 if (RV.isScalar()) {
2053 Args.push_back(RV.getScalarVal());
2054 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002055 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2056 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2057 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2058 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00002059 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002060 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00002061 }
2062 break;
2063
Daniel Dunbar1358b202009-01-26 21:26:08 +00002064 case ABIArgInfo::Ignore:
2065 break;
2066
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002067 case ABIArgInfo::Coerce: {
2068 // FIXME: Avoid the conversion through memory if possible.
2069 llvm::Value *SrcPtr;
2070 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00002071 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002072 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002073 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002074 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002075 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2076 } else
2077 SrcPtr = RV.getAggregateAddr();
2078 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2079 *this));
2080 break;
2081 }
2082
Daniel Dunbar04d35782008-09-17 00:51:38 +00002083 case ABIArgInfo::Expand:
2084 ExpandTypeToArgs(I->second, RV, Args);
2085 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002086 }
2087 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002088
Daniel Dunbar0a067402009-02-23 17:26:39 +00002089 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00002090 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002091 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00002092 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2093 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00002094
Daniel Dunbar90e43452009-03-02 04:32:35 +00002095 llvm::CallSite CS;
2096 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2097 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002098 } else {
2099 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00002100 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2101 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002102 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00002103 }
2104
Daniel Dunbar90e43452009-03-02 04:32:35 +00002105 CS.setAttributes(Attrs);
2106 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2107 CS.setCallingConv(F->getCallingConv());
2108
2109 // If the call doesn't return, finish the basic block and clear the
2110 // insertion point; this allows the rest of IRgen to discard
2111 // unreachable code.
2112 if (CS.doesNotReturn()) {
2113 Builder.CreateUnreachable();
2114 Builder.ClearInsertionPoint();
2115
2116 // FIXME: For now, emit a dummy basic block because expr
2117 // emitters in generally are not ready to handle emitting
2118 // expressions at unreachable points.
2119 EnsureInsertPoint();
2120
2121 // Return a reasonable RValue.
2122 return GetUndefRValue(RetTy);
2123 }
2124
2125 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +00002126 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002127 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00002128
2129 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002130 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00002131 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00002132 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +00002133 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00002134 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +00002135 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00002136
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002137 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00002138 if (RetTy->isAnyComplexType()) {
2139 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2140 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2141 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +00002142 }
2143 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002144 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00002145 Builder.CreateStore(CI, V);
2146 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002147 }
2148 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00002149
Daniel Dunbar1358b202009-01-26 21:26:08 +00002150 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00002151 // If we are ignoring an argument that had a result, make sure to
2152 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00002153 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00002154
Daniel Dunbar73d66602008-09-10 07:04:09 +00002155 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002156 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002157 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00002158 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00002159 if (RetTy->isAnyComplexType())
2160 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +00002161 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00002162 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002163 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00002164 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00002165
Daniel Dunbar22e30052008-09-11 01:48:57 +00002166 case ABIArgInfo::Expand:
2167 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002168 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00002169
2170 assert(0 && "Unhandled ABIArgInfo::Kind");
2171 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002172}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00002173
2174/* VarArg handling */
2175
2176llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2177 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2178}