blob: c46e7901d9f518428cfab520c3465841a22621b2 [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;
636 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
637 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000638 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000639 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000640}
641
Daniel Dunbare09a9692009-01-24 08:32:22 +0000642void X86_64ABIInfo::classify(QualType Ty,
643 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000644 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000645 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000646 // FIXME: This code can be simplified by introducing a simple value
647 // class for Class pairs with appropriate constructor methods for
648 // the various situations.
649
Daniel Dunbard97f5952009-02-22 04:48:22 +0000650 // FIXME: Some of the split computations are wrong; unaligned
651 // vectors shouldn't be passed in registers for example, so there is
652 // no chance they can straddle an eightbyte. Verify & simplify.
653
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000654 Lo = Hi = NoClass;
655
656 Class &Current = OffsetBase < 64 ? Lo : Hi;
657 Current = Memory;
658
Daniel Dunbare09a9692009-01-24 08:32:22 +0000659 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
660 BuiltinType::Kind k = BT->getKind();
661
Daniel Dunbar1358b202009-01-26 21:26:08 +0000662 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000663 Current = NoClass;
Chris Lattner6cc7e412009-04-30 02:43:43 +0000664 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattner3d8e0682009-04-30 06:22:07 +0000665 Lo = Integer;
666 Hi = Integer;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000667 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000668 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000669 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000670 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000671 } else if (k == BuiltinType::LongDouble) {
672 Lo = X87;
673 Hi = X87Up;
674 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000675 // FIXME: _Decimal32 and _Decimal64 are SSE.
676 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson1d234462009-02-26 17:31:15 +0000677 } else if (const EnumType *ET = Ty->getAsEnumType()) {
678 // Classify the underlying integer type.
679 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000680 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000681 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000682 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000683 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000684 if (Size == 32) {
685 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
686 // float> as integer.
687 Current = Integer;
688
689 // If this type crosses an eightbyte boundary, it should be
690 // split.
691 uint64_t EB_Real = (OffsetBase) / 64;
692 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
693 if (EB_Real != EB_Imag)
694 Hi = Lo;
695 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000696 // gcc passes <1 x double> in memory. :(
697 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000698 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000699
700 // gcc passes <1 x long long> as INTEGER.
701 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
702 Current = Integer;
703 else
704 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000705
706 // If this type crosses an eightbyte boundary, it should be
707 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000708 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000709 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000710 } else if (Size == 128) {
711 Lo = SSE;
712 Hi = SSEUp;
713 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000714 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000715 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000716
Daniel Dunbare413f532009-01-30 18:40:10 +0000717 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000718 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000719 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000720 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000721 else if (Size <= 128)
722 Lo = Hi = Integer;
723 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000724 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000725 else if (ET == Context.DoubleTy)
726 Lo = Hi = SSE;
727 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000728 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000729
730 // If this complex type crosses an eightbyte boundary then it
731 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000732 uint64_t EB_Real = (OffsetBase) / 64;
733 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000734 if (Hi == NoClass && EB_Real != EB_Imag)
735 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000736 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
737 // Arrays are treated like structures.
738
739 uint64_t Size = Context.getTypeSize(Ty);
740
741 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
742 // than two eightbytes, ..., it has class MEMORY.
743 if (Size > 128)
744 return;
745
746 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
747 // fields, it has class MEMORY.
748 //
749 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000750 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000751 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000752
753 // Otherwise implement simplified merge. We could be smarter about
754 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000755 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000756 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
757 uint64_t ArraySize = AT->getSize().getZExtValue();
758 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
759 Class FieldLo, FieldHi;
760 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000761 Lo = merge(Lo, FieldLo);
762 Hi = merge(Hi, FieldHi);
763 if (Lo == Memory || Hi == Memory)
764 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000765 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000766
767 // Do post merger cleanup (see below). Only case we worry about is Memory.
768 if (Hi == Memory)
769 Lo = Memory;
770 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000771 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000772 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000773
774 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
775 // than two eightbytes, ..., it has class MEMORY.
776 if (Size > 128)
777 return;
778
779 const RecordDecl *RD = RT->getDecl();
780
781 // Assume variable sized types are passed in memory.
782 if (RD->hasFlexibleArrayMember())
783 return;
784
785 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
786
787 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000788 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000789 unsigned idx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000790 for (RecordDecl::field_iterator i = RD->field_begin(Context),
791 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000792 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000793 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000794
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000795 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
796 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000797 //
Daniel Dunbaref495d42009-04-27 18:31:32 +0000798 // Note, skip this test for bit-fields, see below.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000799 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000800 Lo = Memory;
801 return;
802 }
803
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000804 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000805 //
806 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
807 // exceeds a single eightbyte, each is classified
808 // separately. Each eightbyte gets initialized to class
809 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000810 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000811
Daniel Dunbaref495d42009-04-27 18:31:32 +0000812 // Bit-fields require special handling, they do not force the
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000813 // structure to be passed in memory even if unaligned, and
814 // therefore they can straddle an eightbyte.
815 if (BitField) {
Daniel Dunbar9bb29952009-05-08 22:26:44 +0000816 // Ignore padding bit-fields.
817 if (i->isUnnamedBitfield())
818 continue;
819
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000820 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman5255e7a2009-04-26 19:19:15 +0000821 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000822
823 uint64_t EB_Lo = Offset / 64;
824 uint64_t EB_Hi = (Offset + Size - 1) / 64;
825 FieldLo = FieldHi = NoClass;
826 if (EB_Lo) {
827 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
828 FieldLo = NoClass;
829 FieldHi = Integer;
830 } else {
831 FieldLo = Integer;
832 FieldHi = EB_Hi ? Integer : NoClass;
833 }
834 } else
835 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000836 Lo = merge(Lo, FieldLo);
837 Hi = merge(Hi, FieldHi);
838 if (Lo == Memory || Hi == Memory)
839 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000840 }
841
842 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
843 //
844 // (a) If one of the classes is MEMORY, the whole argument is
845 // passed in memory.
846 //
847 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
848
849 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000850 // the merge (just bail).
851 //
852 // The second condition occurs in the case of unions; for example
853 // union { _Complex double; unsigned; }.
854 if (Hi == Memory)
855 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000856 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000857 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000858 }
859}
860
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000861ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
862 const llvm::Type *CoerceTo,
863 ASTContext &Context) const {
864 if (CoerceTo == llvm::Type::Int64Ty) {
865 // Integer and pointer types will end up in a general purpose
866 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000867 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000868 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000869
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000870 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000871 // FIXME: It would probably be better to make CGFunctionInfo only
872 // map using canonical types than to canonize here.
873 QualType CTy = Context.getCanonicalType(Ty);
874
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000875 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000876 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000877 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000878
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000879 }
880
881 return ABIArgInfo::getCoerce(CoerceTo);
882}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000883
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000884ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
885 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000886 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
887 // classification algorithm.
888 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000889 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000890
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000891 // Check some invariants.
892 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
893 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
894 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
895
Daniel Dunbare09a9692009-01-24 08:32:22 +0000896 const llvm::Type *ResType = 0;
897 switch (Lo) {
898 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000899 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000900
901 case SSEUp:
902 case X87Up:
903 assert(0 && "Invalid classification for lo word.");
904
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000905 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000906 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000907 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000908 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000909
910 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
911 // available register of the sequence %rax, %rdx is used.
912 case Integer:
913 ResType = llvm::Type::Int64Ty; break;
914
915 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
916 // available SSE register of the sequence %xmm0, %xmm1 is used.
917 case SSE:
918 ResType = llvm::Type::DoubleTy; break;
919
920 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
921 // returned on the X87 stack in %st0 as 80-bit x87 number.
922 case X87:
923 ResType = llvm::Type::X86_FP80Ty; break;
924
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000925 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
926 // part of the value is returned in %st0 and the imaginary part in
927 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000928 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000929 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000930 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
931 llvm::Type::X86_FP80Ty,
932 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000933 break;
934 }
935
936 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000937 // Memory was handled previously and X87 should
938 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000939 case Memory:
940 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000941 assert(0 && "Invalid classification for hi word.");
942
Daniel Dunbar92e88642009-02-17 07:55:55 +0000943 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000944 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000945
Daniel Dunbare09a9692009-01-24 08:32:22 +0000946 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000947 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
948 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000949 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000950 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
951 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000952
953 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
954 // is passed in the upper half of the last used SSE register.
955 //
956 // SSEUP should always be preceeded by SSE, just widen.
957 case SSEUp:
958 assert(Lo == SSE && "Unexpected SSEUp classification.");
959 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
960 break;
961
962 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000963 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000964 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000965 // If X87Up is preceeded by X87, we don't need to do
966 // anything. However, in some cases with unions it may not be
967 // preceeded by X87. In such situations we follow gcc and pass the
968 // extra bits in an SSE reg.
969 if (Lo != X87)
970 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000971 break;
972 }
973
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000974 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000975}
976
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000977ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000978 unsigned &neededInt,
979 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000980 X86_64ABIInfo::Class Lo, Hi;
981 classify(Ty, Context, 0, Lo, Hi);
982
983 // Check some invariants.
984 // FIXME: Enforce these by construction.
985 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
986 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
987 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
988
Daniel Dunbare978cb92009-02-10 17:06:09 +0000989 neededInt = 0;
990 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000991 const llvm::Type *ResType = 0;
992 switch (Lo) {
993 case NoClass:
994 return ABIArgInfo::getIgnore();
995
996 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
997 // on the stack.
998 case Memory:
999
1000 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1001 // COMPLEX_X87, it is passed in memory.
1002 case X87:
1003 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001004 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001005
1006 case SSEUp:
1007 case X87Up:
1008 assert(0 && "Invalid classification for lo word.");
1009
1010 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1011 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1012 // and %r9 is used.
1013 case Integer:
1014 ++neededInt;
1015 ResType = llvm::Type::Int64Ty;
1016 break;
1017
1018 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1019 // available SSE register is used, the registers are taken in the
1020 // order from %xmm0 to %xmm7.
1021 case SSE:
1022 ++neededSSE;
1023 ResType = llvm::Type::DoubleTy;
1024 break;
Daniel Dunbareec02622009-02-03 06:30:17 +00001025 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001026
1027 switch (Hi) {
1028 // Memory was handled previously, ComplexX87 and X87 should
1029 // never occur as hi classes, and X87Up must be preceed by X87,
1030 // which is passed in memory.
1031 case Memory:
1032 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001033 case ComplexX87:
1034 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001035 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001036
1037 case NoClass: break;
1038 case Integer:
1039 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1040 ++neededInt;
1041 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001042
1043 // X87Up generally doesn't occur here (long double is passed in
1044 // memory), except in situations involving unions.
1045 case X87Up:
1046 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001047 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1048 ++neededSSE;
1049 break;
1050
1051 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1052 // eightbyte is passed in the upper half of the last used SSE
1053 // register.
1054 case SSEUp:
1055 assert(Lo == SSE && "Unexpected SSEUp classification.");
1056 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1057 break;
1058 }
1059
Daniel Dunbar87c4dc92009-02-14 02:09:24 +00001060 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001061}
1062
1063void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1064 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1065
1066 // Keep track of the number of assigned registers.
1067 unsigned freeIntRegs = 6, freeSSERegs = 8;
1068
1069 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1070 // get assigned (in left-to-right order) for passing as follows...
1071 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +00001072 it != ie; ++it) {
1073 unsigned neededInt, neededSSE;
1074 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1075
1076 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1077 // eightbyte of an argument, the whole argument is passed on the
1078 // stack. If registers have already been assigned for some
1079 // eightbytes of such an argument, the assignments get reverted.
1080 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1081 freeIntRegs -= neededInt;
1082 freeSSERegs -= neededSSE;
1083 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001084 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +00001085 }
1086 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001087}
1088
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001089static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1090 QualType Ty,
1091 CodeGenFunction &CGF) {
1092 llvm::Value *overflow_arg_area_p =
1093 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1094 llvm::Value *overflow_arg_area =
1095 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1096
1097 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1098 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001099 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001100 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001101 // Note that we follow the ABI & gcc here, even though the type
1102 // could in theory have an alignment greater than 16. This case
1103 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001104
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001105 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1106 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1107 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1108 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1109 llvm::Type::Int64Ty);
1110 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1111 overflow_arg_area =
1112 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1113 overflow_arg_area->getType(),
1114 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001115 }
1116
1117 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1118 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1119 llvm::Value *Res =
1120 CGF.Builder.CreateBitCast(overflow_arg_area,
1121 llvm::PointerType::getUnqual(LTy));
1122
1123 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1124 // l->overflow_arg_area + sizeof(type).
1125 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1126 // an 8 byte boundary.
1127
1128 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1129 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1130 (SizeInBytes + 7) & ~7);
1131 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1132 "overflow_arg_area.next");
1133 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1134
1135 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1136 return Res;
1137}
1138
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001139llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1140 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001141 // Assume that va_list type is correct; should be pointer to LLVM type:
1142 // struct {
1143 // i32 gp_offset;
1144 // i32 fp_offset;
1145 // i8* overflow_arg_area;
1146 // i8* reg_save_area;
1147 // };
1148 unsigned neededInt, neededSSE;
1149 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1150 neededInt, neededSSE);
1151
1152 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1153 // in the registers. If not go to step 7.
1154 if (!neededInt && !neededSSE)
1155 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1156
1157 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1158 // general purpose registers needed to pass type and num_fp to hold
1159 // the number of floating point registers needed.
1160
1161 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1162 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1163 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1164 //
1165 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1166 // register save space).
1167
1168 llvm::Value *InRegs = 0;
1169 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1170 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1171 if (neededInt) {
1172 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1173 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1174 InRegs =
1175 CGF.Builder.CreateICmpULE(gp_offset,
1176 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1177 48 - neededInt * 8),
1178 "fits_in_gp");
1179 }
1180
1181 if (neededSSE) {
1182 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1183 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1184 llvm::Value *FitsInFP =
1185 CGF.Builder.CreateICmpULE(fp_offset,
1186 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001187 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001188 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001189 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001190 }
1191
1192 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1193 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1194 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1195 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1196
1197 // Emit code to load the value if it was passed in registers.
1198
1199 CGF.EmitBlock(InRegBlock);
1200
1201 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1202 // an offset of l->gp_offset and/or l->fp_offset. This may require
1203 // copying to a temporary location in case the parameter is passed
1204 // in different register classes or requires an alignment greater
1205 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001206 //
1207 // FIXME: This really results in shameful code when we end up
1208 // needing to collect arguments from different places; often what
1209 // should result in a simple assembling of a structure from
1210 // scattered addresses has many more loads than necessary. Can we
1211 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001212 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1213 llvm::Value *RegAddr =
1214 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1215 "reg_save_area");
1216 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001217 // FIXME: Cleanup.
1218 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1219 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1220 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1221 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1222 const llvm::Type *TyLo = ST->getElementType(0);
1223 const llvm::Type *TyHi = ST->getElementType(1);
1224 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1225 "Unexpected ABI info for mixed regs");
1226 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1227 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1228 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1229 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1230 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1231 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1232 llvm::Value *V =
1233 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1234 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1235 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1236 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1237
1238 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001239 } else if (neededInt) {
1240 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1241 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1242 llvm::PointerType::getUnqual(LTy));
1243 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001244 if (neededSSE == 1) {
1245 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1246 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1247 llvm::PointerType::getUnqual(LTy));
1248 } else {
1249 assert(neededSSE == 2 && "Invalid number of needed registers!");
1250 // SSE registers are spaced 16 bytes apart in the register save
1251 // area, we need to collect the two eightbytes together.
1252 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1253 llvm::Value *RegAddrHi =
1254 CGF.Builder.CreateGEP(RegAddrLo,
1255 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1256 const llvm::Type *DblPtrTy =
1257 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1258 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1259 llvm::Type::DoubleTy,
1260 NULL);
1261 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1262 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1263 DblPtrTy));
1264 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1265 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1266 DblPtrTy));
1267 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1268 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1269 llvm::PointerType::getUnqual(LTy));
1270 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001271 }
1272
1273 // AMD64-ABI 3.5.7p5: Step 5. Set:
1274 // l->gp_offset = l->gp_offset + num_gp * 8
1275 // l->fp_offset = l->fp_offset + num_fp * 16.
1276 if (neededInt) {
1277 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1278 neededInt * 8);
1279 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1280 gp_offset_p);
1281 }
1282 if (neededSSE) {
1283 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1284 neededSSE * 16);
1285 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1286 fp_offset_p);
1287 }
1288 CGF.EmitBranch(ContBlock);
1289
1290 // Emit code to load the value if it was passed in memory.
1291
1292 CGF.EmitBlock(InMemBlock);
1293 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1294
1295 // Return the appropriate result.
1296
1297 CGF.EmitBlock(ContBlock);
1298 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1299 "vaarg.addr");
1300 ResAddr->reserveOperandSpace(2);
1301 ResAddr->addIncoming(RegAddr, InRegBlock);
1302 ResAddr->addIncoming(MemAddr, InMemBlock);
1303
1304 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001305}
1306
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001307// ABI Info for PIC16
1308class PIC16ABIInfo : public ABIInfo {
1309 ABIArgInfo classifyReturnType(QualType RetTy,
1310 ASTContext &Context) const;
1311
1312 ABIArgInfo classifyArgumentType(QualType RetTy,
1313 ASTContext &Context) const;
1314
1315 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1316 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1317 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1318 it != ie; ++it)
1319 it->info = classifyArgumentType(it->type, Context);
1320 }
1321
1322 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1323 CodeGenFunction &CGF) const;
1324
1325};
1326
1327ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1328 ASTContext &Context) const {
1329 if (RetTy->isVoidType()) {
1330 return ABIArgInfo::getIgnore();
1331 } else {
1332 return ABIArgInfo::getDirect();
1333 }
1334}
1335
1336ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1337 ASTContext &Context) const {
1338 return ABIArgInfo::getDirect();
1339}
1340
1341llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1342 CodeGenFunction &CGF) const {
1343 return 0;
1344}
1345
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001346class ARMABIInfo : public ABIInfo {
1347 ABIArgInfo classifyReturnType(QualType RetTy,
1348 ASTContext &Context) const;
1349
1350 ABIArgInfo classifyArgumentType(QualType RetTy,
1351 ASTContext &Context) const;
1352
1353 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1354
1355 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1356 CodeGenFunction &CGF) const;
1357};
1358
1359void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1360 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1361 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1362 it != ie; ++it) {
1363 it->info = classifyArgumentType(it->type, Context);
1364 }
1365}
1366
1367ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1368 ASTContext &Context) const {
1369 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1370 return ABIArgInfo::getDirect();
1371 }
1372 // FIXME: This is kind of nasty... but there isn't much choice
1373 // because the ARM backend doesn't support byval.
1374 // FIXME: This doesn't handle alignment > 64 bits.
1375 const llvm::Type* ElemTy;
1376 unsigned SizeRegs;
1377 if (Context.getTypeAlign(Ty) > 32) {
1378 ElemTy = llvm::Type::Int64Ty;
1379 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1380 } else {
1381 ElemTy = llvm::Type::Int32Ty;
1382 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1383 }
1384 std::vector<const llvm::Type*> LLVMFields;
1385 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1386 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1387 return ABIArgInfo::getCoerce(STy);
1388}
1389
1390ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1391 ASTContext &Context) const {
1392 if (RetTy->isVoidType()) {
1393 return ABIArgInfo::getIgnore();
1394 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1395 // Aggregates <= 4 bytes are returned in r0; other aggregates
1396 // are returned indirectly.
1397 uint64_t Size = Context.getTypeSize(RetTy);
1398 if (Size <= 32)
1399 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1400 return ABIArgInfo::getIndirect(0);
1401 } else {
1402 return ABIArgInfo::getDirect();
1403 }
1404}
1405
1406llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1407 CodeGenFunction &CGF) const {
1408 // FIXME: Need to handle alignment
1409 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1410 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1411
1412 CGBuilderTy &Builder = CGF.Builder;
1413 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1414 "ap");
1415 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1416 llvm::Type *PTy =
1417 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1418 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1419
1420 uint64_t Offset =
1421 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1422 llvm::Value *NextAddr =
1423 Builder.CreateGEP(Addr,
1424 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1425 "ap.next");
1426 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1427
1428 return AddrTyped;
1429}
1430
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001431ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001432 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001433 if (RetTy->isVoidType()) {
1434 return ABIArgInfo::getIgnore();
1435 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001436 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001437 } else {
1438 return ABIArgInfo::getDirect();
1439 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001440}
1441
1442ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001443 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001444 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001445 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001446 } else {
1447 return ABIArgInfo::getDirect();
1448 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001449}
1450
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001451llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1452 CodeGenFunction &CGF) const {
1453 return 0;
1454}
1455
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001456const ABIInfo &CodeGenTypes::getABIInfo() const {
1457 if (TheABIInfo)
1458 return *TheABIInfo;
1459
1460 // For now we just cache this in the CodeGenTypes and don't bother
1461 // to free it.
1462 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1463 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman5e175802009-03-23 23:26:24 +00001464 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001465 switch (getContext().Target.getPointerWidth(0)) {
1466 case 32:
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001467 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001468 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001469 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001470 }
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001471 } else if (strcmp(TargetPrefix, "arm") == 0) {
1472 // FIXME: Support for OABI?
1473 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001474 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1475 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001476 }
1477
1478 return *(TheABIInfo = new DefaultABIInfo);
1479}
1480
Daniel Dunbare126ab12008-09-10 02:41:04 +00001481/***/
1482
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001483CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1484 const llvm::SmallVector<QualType, 16> &ArgTys) {
1485 NumArgs = ArgTys.size();
1486 Args = new ArgInfo[1 + NumArgs];
1487 Args[0].type = ResTy;
1488 for (unsigned i = 0; i < NumArgs; ++i)
1489 Args[1 + i].type = ArgTys[i];
1490}
1491
1492/***/
1493
Daniel Dunbar04d35782008-09-17 00:51:38 +00001494void CodeGenTypes::GetExpandedTypes(QualType Ty,
1495 std::vector<const llvm::Type*> &ArgTys) {
1496 const RecordType *RT = Ty->getAsStructureType();
1497 assert(RT && "Can only expand structure types.");
1498 const RecordDecl *RD = RT->getDecl();
1499 assert(!RD->hasFlexibleArrayMember() &&
1500 "Cannot expand structure with flexible array.");
1501
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001502 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1503 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001504 const FieldDecl *FD = *i;
1505 assert(!FD->isBitField() &&
1506 "Cannot expand structure with bit-field members.");
1507
1508 QualType FT = FD->getType();
1509 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1510 GetExpandedTypes(FT, ArgTys);
1511 } else {
1512 ArgTys.push_back(ConvertType(FT));
1513 }
1514 }
1515}
1516
1517llvm::Function::arg_iterator
1518CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1519 llvm::Function::arg_iterator AI) {
1520 const RecordType *RT = Ty->getAsStructureType();
1521 assert(RT && "Can only expand structure types.");
1522
1523 RecordDecl *RD = RT->getDecl();
1524 assert(LV.isSimple() &&
1525 "Unexpected non-simple lvalue during struct expansion.");
1526 llvm::Value *Addr = LV.getAddress();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001527 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1528 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001529 FieldDecl *FD = *i;
1530 QualType FT = FD->getType();
1531
1532 // FIXME: What are the right qualifiers here?
1533 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1534 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1535 AI = ExpandTypeFromArgs(FT, LV, AI);
1536 } else {
1537 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1538 ++AI;
1539 }
1540 }
1541
1542 return AI;
1543}
1544
1545void
1546CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1547 llvm::SmallVector<llvm::Value*, 16> &Args) {
1548 const RecordType *RT = Ty->getAsStructureType();
1549 assert(RT && "Can only expand structure types.");
1550
1551 RecordDecl *RD = RT->getDecl();
1552 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1553 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001554 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1555 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001556 FieldDecl *FD = *i;
1557 QualType FT = FD->getType();
1558
1559 // FIXME: What are the right qualifiers here?
1560 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1561 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1562 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1563 } else {
1564 RValue RV = EmitLoadOfLValue(LV, FT);
1565 assert(RV.isScalar() &&
1566 "Unexpected non-scalar rvalue during struct expansion.");
1567 Args.push_back(RV.getScalarVal());
1568 }
1569 }
1570}
1571
Daniel Dunbar84379912009-02-02 19:06:38 +00001572/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1573/// a pointer to an object of type \arg Ty.
1574///
1575/// This safely handles the case when the src type is smaller than the
1576/// destination type; in this situation the values of bits which not
1577/// present in the src are undefined.
1578static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1579 const llvm::Type *Ty,
1580 CodeGenFunction &CGF) {
1581 const llvm::Type *SrcTy =
1582 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001583 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1584 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar84379912009-02-02 19:06:38 +00001585
Daniel Dunbar77071992009-02-03 05:59:18 +00001586 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001587 if (SrcSize == DstSize) {
1588 llvm::Value *Casted =
1589 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001590 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1591 // FIXME: Use better alignment / avoid requiring aligned load.
1592 Load->setAlignment(1);
1593 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001594 } else {
1595 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1596
1597 // Otherwise do coercion through memory. This is stupid, but
1598 // simple.
1599 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1600 llvm::Value *Casted =
1601 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001602 llvm::StoreInst *Store =
1603 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1604 // FIXME: Use better alignment / avoid requiring aligned store.
1605 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001606 return CGF.Builder.CreateLoad(Tmp);
1607 }
1608}
1609
1610/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1611/// where the source and destination may have different types.
1612///
1613/// This safely handles the case when the src type is larger than the
1614/// destination type; the upper bits of the src will be lost.
1615static void CreateCoercedStore(llvm::Value *Src,
1616 llvm::Value *DstPtr,
1617 CodeGenFunction &CGF) {
1618 const llvm::Type *SrcTy = Src->getType();
1619 const llvm::Type *DstTy =
1620 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1621
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001622 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1623 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar84379912009-02-02 19:06:38 +00001624
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001625 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001626 if (SrcSize == DstSize) {
1627 llvm::Value *Casted =
1628 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001629 // FIXME: Use better alignment / avoid requiring aligned store.
1630 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001631 } else {
1632 assert(SrcSize > DstSize && "Coercion is missing bits!");
1633
1634 // Otherwise do coercion through memory. This is stupid, but
1635 // simple.
1636 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1637 CGF.Builder.CreateStore(Src, Tmp);
1638 llvm::Value *Casted =
1639 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001640 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1641 // FIXME: Use better alignment / avoid requiring aligned load.
1642 Load->setAlignment(1);
1643 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001644 }
1645}
1646
Daniel Dunbar04d35782008-09-17 00:51:38 +00001647/***/
1648
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001649bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001650 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001651}
1652
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001653const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001654CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001655 std::vector<const llvm::Type*> ArgTys;
1656
1657 const llvm::Type *ResultType = 0;
1658
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001659 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001660 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001661 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001662 case ABIArgInfo::Expand:
1663 assert(0 && "Invalid ABI kind for return argument");
1664
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001665 case ABIArgInfo::Direct:
1666 ResultType = ConvertType(RetTy);
1667 break;
1668
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001669 case ABIArgInfo::Indirect: {
1670 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001671 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001672 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001673 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1674 break;
1675 }
1676
Daniel Dunbar1358b202009-01-26 21:26:08 +00001677 case ABIArgInfo::Ignore:
1678 ResultType = llvm::Type::VoidTy;
1679 break;
1680
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001681 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001682 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001683 break;
1684 }
1685
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001686 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1687 ie = FI.arg_end(); it != ie; ++it) {
1688 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001689
1690 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001691 case ABIArgInfo::Ignore:
1692 break;
1693
Daniel Dunbar04d35782008-09-17 00:51:38 +00001694 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001695 ArgTys.push_back(AI.getCoerceToType());
1696 break;
1697
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001698 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001699 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001700 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1701 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001702 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001703 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001704
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001705 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001706 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001707 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001708
1709 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001710 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001711 break;
1712 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001713 }
1714
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001715 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001716}
1717
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001718void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001719 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001720 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001721 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001722 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001723
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001724 // FIXME: handle sseregparm someday...
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001725 if (TargetDecl) {
Daniel Dunbar78582862009-04-13 21:08:27 +00001726 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001727 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar78582862009-04-13 21:08:27 +00001728 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001729 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbar78582862009-04-13 21:08:27 +00001730 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssondd6791c2008-10-05 23:32:53 +00001731 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar78582862009-04-13 21:08:27 +00001732 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar521c3a32009-04-10 22:14:52 +00001733 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001734 }
1735
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001736 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001737 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001738 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001739 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001740 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001741 if (RetTy->isPromotableIntegerType()) {
1742 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001743 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001744 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001745 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001746 }
1747 }
1748 break;
1749
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001750 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001751 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001752 llvm::Attribute::StructRet |
1753 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001754 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001755 // sret disables readnone and readonly
1756 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1757 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001758 break;
1759
Daniel Dunbar1358b202009-01-26 21:26:08 +00001760 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001761 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001762 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001763
Daniel Dunbar22e30052008-09-11 01:48:57 +00001764 case ABIArgInfo::Expand:
1765 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001766 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001767
Devang Patel2bb6eb82008-09-26 22:53:57 +00001768 if (RetAttrs)
1769 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001770
1771 // FIXME: we need to honour command line settings also...
1772 // FIXME: RegParm should be reduced in case of nested functions and/or global
1773 // register variable.
1774 signed RegParm = 0;
1775 if (TargetDecl)
1776 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1777 RegParm = RegParmAttr->getNumParams();
1778
1779 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001780 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1781 ie = FI.arg_end(); it != ie; ++it) {
1782 QualType ParamType = it->type;
1783 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001784 unsigned Attributes = 0;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001785
Daniel Dunbar22e30052008-09-11 01:48:57 +00001786 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001787 case ABIArgInfo::Coerce:
1788 break;
1789
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001790 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001791 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001792 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001793 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001794 // byval disables readnone and readonly.
1795 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1796 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001797 break;
1798
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001799 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001800 if (ParamType->isPromotableIntegerType()) {
1801 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001802 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001803 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001804 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001805 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001806 }
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001807 if (RegParm > 0 &&
1808 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1809 RegParm -=
1810 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1811 if (RegParm >= 0)
1812 Attributes |= llvm::Attribute::InReg;
1813 }
1814 // FIXME: handle sseregparm someday...
Daniel Dunbar22e30052008-09-11 01:48:57 +00001815 break;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001816
Daniel Dunbar1358b202009-01-26 21:26:08 +00001817 case ABIArgInfo::Ignore:
1818 // Skip increment, no matching LLVM parameter.
1819 continue;
1820
Daniel Dunbar04d35782008-09-17 00:51:38 +00001821 case ABIArgInfo::Expand: {
1822 std::vector<const llvm::Type*> Tys;
1823 // FIXME: This is rather inefficient. Do we ever actually need
1824 // to do anything here? The result should be just reconstructed
1825 // on the other side, so extension should be a non-issue.
1826 getTypes().GetExpandedTypes(ParamType, Tys);
1827 Index += Tys.size();
1828 continue;
1829 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001830 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001831
Devang Patela85a9ef2008-09-25 21:02:23 +00001832 if (Attributes)
1833 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001834 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001835 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001836 if (FuncAttrs)
1837 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001838}
1839
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001840void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1841 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001842 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001843 // FIXME: We no longer need the types from FunctionArgList; lift up
1844 // and simplify.
1845
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001846 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1847 llvm::Function::arg_iterator AI = Fn->arg_begin();
1848
1849 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001850 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001851 AI->setName("agg.result");
1852 ++AI;
1853 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001854
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001855 assert(FI.arg_size() == Args.size() &&
1856 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001857 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001858 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001859 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001860 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001861 QualType Ty = info_it->type;
1862 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001863
1864 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001865 case ABIArgInfo::Indirect: {
1866 llvm::Value* V = AI;
1867 if (hasAggregateLLVMType(Ty)) {
1868 // Do nothing, aggregates and complex variables are accessed by
1869 // reference.
1870 } else {
1871 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001872 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001873 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1874 // This must be a promotion, for something like
1875 // "void a(x) short x; {..."
1876 V = EmitScalarConversion(V, Ty, Arg->getType());
1877 }
1878 }
1879 EmitParmDecl(*Arg, V);
1880 break;
1881 }
1882
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001883 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001884 assert(AI != Fn->arg_end() && "Argument mismatch!");
1885 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001886 if (hasAggregateLLVMType(Ty)) {
1887 // Create a temporary alloca to hold the argument; the rest of
1888 // codegen expects to access aggregates & complex values by
1889 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001890 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001891 Builder.CreateStore(AI, V);
1892 } else {
1893 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1894 // This must be a promotion, for something like
1895 // "void a(x) short x; {..."
1896 V = EmitScalarConversion(V, Ty, Arg->getType());
1897 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001898 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001899 EmitParmDecl(*Arg, V);
1900 break;
1901 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001902
1903 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001904 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001905 // we need to create a temporary and reconstruct it from the
1906 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001907 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001908 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001909 (Name + ".addr").c_str());
1910 // FIXME: What are the right qualifiers here?
1911 llvm::Function::arg_iterator End =
1912 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1913 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001914
Daniel Dunbar04d35782008-09-17 00:51:38 +00001915 // Name the arguments used in expansion and increment AI.
1916 unsigned Index = 0;
1917 for (; AI != End; ++AI, ++Index)
1918 AI->setName(Name + "." + llvm::utostr(Index));
1919 continue;
1920 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001921
1922 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001923 // Initialize the local variable appropriately.
1924 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001925 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001926 } else {
1927 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1928 }
1929
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001930 // Skip increment, no matching LLVM parameter.
1931 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001932
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001933 case ABIArgInfo::Coerce: {
1934 assert(AI != Fn->arg_end() && "Argument mismatch!");
1935 // FIXME: This is very wasteful; EmitParmDecl is just going to
1936 // drop the result in a new alloca anyway, so we could just
1937 // store into that directly if we broke the abstraction down
1938 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001939 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001940 CreateCoercedStore(AI, V, *this);
1941 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001942 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001943 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001944 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1945 // This must be a promotion, for something like
1946 // "void a(x) short x; {..."
1947 V = EmitScalarConversion(V, Ty, Arg->getType());
1948 }
1949 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001950 EmitParmDecl(*Arg, V);
1951 break;
1952 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001953 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001954
1955 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001956 }
1957 assert(AI == Fn->arg_end() && "Argument mismatch!");
1958}
1959
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001960void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001961 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001962 llvm::Value *RV = 0;
1963
1964 // Functions with no result always return void.
1965 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001966 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001967 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001968
1969 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001970 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001971 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001972 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1973 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1974 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1975 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1976 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001977 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1978 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001979 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001980 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001981
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001982 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001983 // The internal return value temp always will have
1984 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001985 RV = Builder.CreateLoad(ReturnValue);
1986 break;
1987
Daniel Dunbar1358b202009-01-26 21:26:08 +00001988 case ABIArgInfo::Ignore:
1989 break;
1990
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001991 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001992 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001993 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001994
Daniel Dunbar22e30052008-09-11 01:48:57 +00001995 case ABIArgInfo::Expand:
1996 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001997 }
1998 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001999
2000 if (RV) {
2001 Builder.CreateRet(RV);
2002 } else {
2003 Builder.CreateRetVoid();
2004 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002005}
2006
Anders Carlssond927fa72009-04-08 20:47:54 +00002007RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
2008 return EmitAnyExprToTemp(E);
2009}
2010
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00002011RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
2012 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002013 const CallArgList &CallArgs,
2014 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00002015 // FIXME: We no longer need the types from CallArgs; lift up and
2016 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002017 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002018
2019 // Handle struct-return functions by passing a pointer to the
2020 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00002021 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00002022 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00002023 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002024 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002025 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002026 }
2027
Daniel Dunbar14c884a2009-02-04 21:17:21 +00002028 assert(CallInfo.arg_size() == CallArgs.size() &&
2029 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00002030 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002031 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00002032 I != E; ++I, ++info_it) {
2033 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002034 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00002035
2036 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002037 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002038 if (RV.isScalar() || RV.isComplex()) {
2039 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002040 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002041 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002042 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002043 else
2044 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2045 } else {
2046 Args.push_back(RV.getAggregateAddr());
2047 }
2048 break;
2049
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002050 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00002051 if (RV.isScalar()) {
2052 Args.push_back(RV.getScalarVal());
2053 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002054 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2055 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2056 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2057 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00002058 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002059 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00002060 }
2061 break;
2062
Daniel Dunbar1358b202009-01-26 21:26:08 +00002063 case ABIArgInfo::Ignore:
2064 break;
2065
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002066 case ABIArgInfo::Coerce: {
2067 // FIXME: Avoid the conversion through memory if possible.
2068 llvm::Value *SrcPtr;
2069 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00002070 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002071 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002072 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002073 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002074 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2075 } else
2076 SrcPtr = RV.getAggregateAddr();
2077 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2078 *this));
2079 break;
2080 }
2081
Daniel Dunbar04d35782008-09-17 00:51:38 +00002082 case ABIArgInfo::Expand:
2083 ExpandTypeToArgs(I->second, RV, Args);
2084 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002085 }
2086 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002087
Daniel Dunbar0a067402009-02-23 17:26:39 +00002088 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00002089 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002090 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00002091 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2092 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00002093
Daniel Dunbar90e43452009-03-02 04:32:35 +00002094 llvm::CallSite CS;
2095 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2096 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002097 } else {
2098 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00002099 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2100 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002101 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00002102 }
2103
Daniel Dunbar90e43452009-03-02 04:32:35 +00002104 CS.setAttributes(Attrs);
2105 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2106 CS.setCallingConv(F->getCallingConv());
2107
2108 // If the call doesn't return, finish the basic block and clear the
2109 // insertion point; this allows the rest of IRgen to discard
2110 // unreachable code.
2111 if (CS.doesNotReturn()) {
2112 Builder.CreateUnreachable();
2113 Builder.ClearInsertionPoint();
2114
2115 // FIXME: For now, emit a dummy basic block because expr
2116 // emitters in generally are not ready to handle emitting
2117 // expressions at unreachable points.
2118 EnsureInsertPoint();
2119
2120 // Return a reasonable RValue.
2121 return GetUndefRValue(RetTy);
2122 }
2123
2124 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +00002125 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002126 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00002127
2128 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002129 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00002130 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00002131 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +00002132 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00002133 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +00002134 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00002135
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002136 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00002137 if (RetTy->isAnyComplexType()) {
2138 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2139 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2140 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +00002141 }
2142 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002143 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00002144 Builder.CreateStore(CI, V);
2145 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002146 }
2147 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00002148
Daniel Dunbar1358b202009-01-26 21:26:08 +00002149 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00002150 // If we are ignoring an argument that had a result, make sure to
2151 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00002152 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00002153
Daniel Dunbar73d66602008-09-10 07:04:09 +00002154 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002155 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002156 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00002157 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00002158 if (RetTy->isAnyComplexType())
2159 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +00002160 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00002161 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002162 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00002163 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00002164
Daniel Dunbar22e30052008-09-11 01:48:57 +00002165 case ABIArgInfo::Expand:
2166 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002167 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00002168
2169 assert(0 && "Unhandled ABIArgInfo::Kind");
2170 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002171}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00002172
2173/* VarArg handling */
2174
2175llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2176 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2177}