blob: 36b6b92ab8213a9983957d1370e2c65e097c886e [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();
169 // Arrays of empty records count as empty.
170 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
171 if (isEmptyRecord(Context, AT->getElementType()))
172 return true;
173
174 return isEmptyRecord(Context, FT);
175}
176
177/// isEmptyRecord - Return true iff a structure contains only empty
178/// fields. Note that a structure with a flexible array member is not
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000179/// considered empty.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000180static bool isEmptyRecord(ASTContext &Context, QualType T) {
Daniel Dunbara7446422009-03-31 19:01:39 +0000181 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000182 if (!RT)
183 return 0;
184 const RecordDecl *RD = RT->getDecl();
185 if (RD->hasFlexibleArrayMember())
186 return false;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000187 for (RecordDecl::field_iterator i = RD->field_begin(Context),
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000188 e = RD->field_end(Context); i != e; ++i)
189 if (!isEmptyField(Context, *i))
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000190 return false;
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000191 return true;
192}
193
194/// isSingleElementStruct - Determine if a structure is a "single
195/// element struct", i.e. it has exactly one non-empty field or
196/// exactly one field which is itself a single element
197/// struct. Structures with flexible array members are never
198/// considered single element structs.
199///
200/// \return The field declaration for the single non-empty field, if
201/// it exists.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000202static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000203 const RecordType *RT = T->getAsStructureType();
204 if (!RT)
205 return 0;
206
207 const RecordDecl *RD = RT->getDecl();
208 if (RD->hasFlexibleArrayMember())
209 return 0;
210
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000211 const Type *Found = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000212 for (RecordDecl::field_iterator i = RD->field_begin(Context),
213 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000214 const FieldDecl *FD = *i;
215 QualType FT = FD->getType();
216
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000217 // Ignore empty fields.
218 if (isEmptyField(Context, FD))
219 continue;
220
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000221 // Treat single element arrays as the element
222 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
223 if (AT->getSize().getZExtValue() == 1)
224 FT = AT->getElementType();
225
Daniel Dunbar6f3d7602009-05-08 21:04:47 +0000226 if (Found)
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000227 return 0;
Daniel Dunbar6f3d7602009-05-08 21:04:47 +0000228
229 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000230 Found = FT.getTypePtr();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000231 } else {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000232 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000233 if (!Found)
234 return 0;
235 }
236 }
237
238 return Found;
239}
240
241static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
242 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
243 return false;
244
245 uint64_t Size = Context.getTypeSize(Ty);
246 return Size == 32 || Size == 64;
247}
248
249static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
250 ASTContext &Context) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000251 for (RecordDecl::field_iterator i = RD->field_begin(Context),
252 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000253 const FieldDecl *FD = *i;
254
255 if (!is32Or64BitBasicType(FD->getType(), Context))
256 return false;
257
Daniel Dunbaref495d42009-04-27 18:31:32 +0000258 // FIXME: Reject bit-fields wholesale; there are two problems, we
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000259 // don't know how to expand them yet, and the predicate for
260 // telling if a bitfield still counts as "basic" is more
261 // complicated than what we were doing previously.
262 if (FD->isBitField())
263 return false;
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000264 }
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000265
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000266 return true;
267}
268
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000269namespace {
270/// DefaultABIInfo - The default implementation for ABI specific
271/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000272/// self-consistent and sensible LLVM IR generation, but does not
273/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000274class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000275 ABIArgInfo classifyReturnType(QualType RetTy,
276 ASTContext &Context) const;
277
278 ABIArgInfo classifyArgumentType(QualType RetTy,
279 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000280
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000281 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
282 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
283 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
284 it != ie; ++it)
285 it->info = classifyArgumentType(it->type, Context);
286 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000287
288 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
289 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000290};
291
292/// X86_32ABIInfo - The X86-32 ABI information.
293class X86_32ABIInfo : public ABIInfo {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000294 ASTContext &Context;
Eli Friedman5e175802009-03-23 23:26:24 +0000295 bool IsDarwin;
296
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000297 static bool isRegisterSize(unsigned Size) {
298 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
299 }
300
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000301 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
302
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000303public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000304 ABIArgInfo classifyReturnType(QualType RetTy,
305 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000306
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000307 ABIArgInfo classifyArgumentType(QualType RetTy,
308 ASTContext &Context) const;
309
310 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
311 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
312 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
313 it != ie; ++it)
314 it->info = classifyArgumentType(it->type, Context);
315 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000316
317 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
318 CodeGenFunction &CGF) const;
Eli Friedman5e175802009-03-23 23:26:24 +0000319
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000320 X86_32ABIInfo(ASTContext &Context, bool d)
321 : ABIInfo(), Context(Context), IsDarwin(d) {}
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000322};
323}
324
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000325
326/// shouldReturnTypeInRegister - Determine if the given type should be
327/// passed in a register (for the Darwin ABI).
328bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
329 ASTContext &Context) {
330 uint64_t Size = Context.getTypeSize(Ty);
331
332 // Type must be register sized.
333 if (!isRegisterSize(Size))
334 return false;
335
336 if (Ty->isVectorType()) {
337 // 64- and 128- bit vectors inside structures are not returned in
338 // registers.
339 if (Size == 64 || Size == 128)
340 return false;
341
342 return true;
343 }
344
345 // If this is a builtin, pointer, or complex type, it is ok.
346 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
347 return true;
348
349 // Arrays are treated like records.
350 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
351 return shouldReturnTypeInRegister(AT->getElementType(), Context);
352
353 // Otherwise, it must be a record type.
354 const RecordType *RT = Ty->getAsRecordType();
355 if (!RT) return false;
356
357 // Structure types are passed in register if all fields would be
358 // passed in a register.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000359 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context),
360 e = RT->getDecl()->field_end(Context); i != e; ++i) {
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000361 const FieldDecl *FD = *i;
362
Daniel Dunbar8ac59ae2009-05-11 18:58:49 +0000363 // Empty fields are ignored.
364 if (isEmptyField(Context, FD))
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000365 continue;
366
367 // Check fields recursively.
368 if (!shouldReturnTypeInRegister(FD->getType(), Context))
369 return false;
370 }
371
372 return true;
373}
374
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000375ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
376 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000377 if (RetTy->isVoidType()) {
378 return ABIArgInfo::getIgnore();
Daniel Dunbar2a7bb3f2009-04-01 06:13:08 +0000379 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
380 // On Darwin, some vectors are returned in registers.
381 if (IsDarwin) {
382 uint64_t Size = Context.getTypeSize(RetTy);
383
384 // 128-bit vectors are a special case; they are returned in
385 // registers and we need to make sure to pick a type the LLVM
386 // backend will like.
387 if (Size == 128)
388 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
389 2));
390
391 // Always return in register if it fits in a general purpose
392 // register, or if it is 64 bits and has a single element.
393 if ((Size == 8 || Size == 16 || Size == 32) ||
394 (Size == 64 && VT->getNumElements() == 1))
395 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
396
397 return ABIArgInfo::getIndirect(0);
398 }
399
400 return ABIArgInfo::getDirect();
Daniel Dunbareec02622009-02-03 06:30:17 +0000401 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaref495d42009-04-27 18:31:32 +0000402 // Structures with flexible arrays are always indirect.
403 if (const RecordType *RT = RetTy->getAsStructureType())
404 if (RT->getDecl()->hasFlexibleArrayMember())
405 return ABIArgInfo::getIndirect(0);
406
Eli Friedman5e175802009-03-23 23:26:24 +0000407 // Outside of Darwin, structs and unions are always indirect.
408 if (!IsDarwin && !RetTy->isAnyComplexType())
409 return ABIArgInfo::getIndirect(0);
Daniel Dunbaref495d42009-04-27 18:31:32 +0000410
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000411 // Classify "single element" structs as their element type.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000412 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000413 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000414 if (BT->isIntegerType()) {
Daniel Dunbar9f4a5a42009-05-08 21:30:11 +0000415 // We need to use the size of the structure, padding
416 // bit-fields can adjust that to be larger than the single
417 // element type.
418 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000419 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
420 } else if (BT->getKind() == BuiltinType::Float) {
Daniel Dunbar9f4a5a42009-05-08 21:30:11 +0000421 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
422 "Unexpect single element structure size!");
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000423 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
424 } else if (BT->getKind() == BuiltinType::Double) {
Daniel Dunbar9f4a5a42009-05-08 21:30:11 +0000425 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
426 "Unexpect single element structure size!");
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000427 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
428 }
429 } else if (SeltTy->isPointerType()) {
430 // FIXME: It would be really nice if this could come out as
431 // the proper pointer type.
432 llvm::Type *PtrTy =
433 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
434 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000435 } else if (SeltTy->isVectorType()) {
436 // 64- and 128-bit vectors are never returned in a
437 // register when inside a structure.
438 uint64_t Size = Context.getTypeSize(RetTy);
439 if (Size == 64 || Size == 128)
440 return ABIArgInfo::getIndirect(0);
441
442 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000443 }
444 }
445
Daniel Dunbar73d66602008-09-10 07:04:09 +0000446 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000447 if (isRegisterSize(Size)) {
448 // Always return in register for unions for now.
449 // FIXME: This is wrong, but better than treating as a
450 // structure.
451 if (RetTy->isUnionType())
452 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
453
454 // Small structures which are register sized are generally returned
455 // in a register.
456 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context))
457 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
458 }
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000459
460 return ABIArgInfo::getIndirect(0);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000461 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000462 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000463 }
464}
465
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000466ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000467 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000468 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000469 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000470 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000471 if (const RecordType *RT = Ty->getAsStructureType())
472 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000473 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000474
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000475 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000476 uint64_t Size = Context.getTypeSize(Ty);
477 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000478 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000479
480 // Expand structs with size <= 128-bits which consist only of
481 // basic types (int, long long, float, double, xxx*). This is
482 // non-recursive and does not ignore empty fields.
483 if (const RecordType *RT = Ty->getAsStructureType()) {
484 if (Context.getTypeSize(Ty) <= 4*32 &&
485 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
486 return ABIArgInfo::getExpand();
487 }
488
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000489 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000490 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000491 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000492 }
493}
494
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000495llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
496 CodeGenFunction &CGF) const {
497 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
498 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
499
500 CGBuilderTy &Builder = CGF.Builder;
501 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
502 "ap");
503 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
504 llvm::Type *PTy =
505 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
506 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
507
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000508 uint64_t Offset =
509 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000510 llvm::Value *NextAddr =
511 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000512 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000513 "ap.next");
514 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
515
516 return AddrTyped;
517}
518
Daniel Dunbare09a9692009-01-24 08:32:22 +0000519namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000520/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000521class X86_64ABIInfo : public ABIInfo {
522 enum Class {
523 Integer = 0,
524 SSE,
525 SSEUp,
526 X87,
527 X87Up,
528 ComplexX87,
529 NoClass,
530 Memory
531 };
532
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000533 /// merge - Implement the X86_64 ABI merging algorithm.
534 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000535 /// Merge an accumulating classification \arg Accum with a field
536 /// classification \arg Field.
537 ///
538 /// \param Accum - The accumulating classification. This should
539 /// always be either NoClass or the result of a previous merge
540 /// call. In addition, this should never be Memory (the caller
541 /// should just return Memory for the aggregate).
542 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000543
Daniel Dunbare09a9692009-01-24 08:32:22 +0000544 /// classify - Determine the x86_64 register classes in which the
545 /// given type T should be passed.
546 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000547 /// \param Lo - The classification for the parts of the type
548 /// residing in the low word of the containing object.
549 ///
550 /// \param Hi - The classification for the parts of the type
551 /// residing in the high word of the containing object.
552 ///
553 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000554 /// containing object. Some parameters are classified different
555 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000556 ///
557 /// If a word is unused its result will be NoClass; if a type should
558 /// be passed in Memory then at least the classification of \arg Lo
559 /// will be Memory.
560 ///
561 /// The \arg Lo class will be NoClass iff the argument is ignored.
562 ///
563 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar92e88642009-02-17 07:55:55 +0000564 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000565 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000566 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000567
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000568 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
569 /// to coerce to, chose the best way to pass Ty in the same place
570 /// that \arg CoerceTo would be passed, but while keeping the
571 /// emitted code as simple as possible.
572 ///
573 /// FIXME: Note, this should be cleaned up to just take an
574 /// enumeration of all the ways we might want to pass things,
575 /// instead of constructing an LLVM type. This makes this code more
576 /// explicit, and it makes it clearer that we are also doing this
577 /// for correctness in the case of passing scalar types.
578 ABIArgInfo getCoerceResult(QualType Ty,
579 const llvm::Type *CoerceTo,
580 ASTContext &Context) const;
581
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000582 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000583 ASTContext &Context) const;
584
585 ABIArgInfo classifyArgumentType(QualType Ty,
586 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000587 unsigned &neededInt,
588 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000589
590public:
591 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000592
593 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
594 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000595};
596}
597
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000598X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
599 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000600 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
601 // classified recursively so that always two fields are
602 // considered. The resulting class is calculated according to
603 // the classes of the fields in the eightbyte:
604 //
605 // (a) If both classes are equal, this is the resulting class.
606 //
607 // (b) If one of the classes is NO_CLASS, the resulting class is
608 // the other class.
609 //
610 // (c) If one of the classes is MEMORY, the result is the MEMORY
611 // class.
612 //
613 // (d) If one of the classes is INTEGER, the result is the
614 // INTEGER.
615 //
616 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
617 // MEMORY is used as class.
618 //
619 // (f) Otherwise class SSE is used.
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000620
621 // Accum should never be memory (we should have returned) or
622 // ComplexX87 (because this cannot be passed in a structure).
623 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000624 "Invalid accumulated classification during merge.");
625 if (Accum == Field || Field == NoClass)
626 return Accum;
627 else if (Field == Memory)
628 return Memory;
629 else if (Accum == NoClass)
630 return Field;
631 else if (Accum == Integer || Field == Integer)
632 return Integer;
633 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
634 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000635 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000636 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000637}
638
Daniel Dunbare09a9692009-01-24 08:32:22 +0000639void X86_64ABIInfo::classify(QualType Ty,
640 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000641 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000642 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000643 // FIXME: This code can be simplified by introducing a simple value
644 // class for Class pairs with appropriate constructor methods for
645 // the various situations.
646
Daniel Dunbard97f5952009-02-22 04:48:22 +0000647 // FIXME: Some of the split computations are wrong; unaligned
648 // vectors shouldn't be passed in registers for example, so there is
649 // no chance they can straddle an eightbyte. Verify & simplify.
650
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000651 Lo = Hi = NoClass;
652
653 Class &Current = OffsetBase < 64 ? Lo : Hi;
654 Current = Memory;
655
Daniel Dunbare09a9692009-01-24 08:32:22 +0000656 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
657 BuiltinType::Kind k = BT->getKind();
658
Daniel Dunbar1358b202009-01-26 21:26:08 +0000659 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000660 Current = NoClass;
Chris Lattner6cc7e412009-04-30 02:43:43 +0000661 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattner3d8e0682009-04-30 06:22:07 +0000662 Lo = Integer;
663 Hi = Integer;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000664 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000665 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000666 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000667 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000668 } else if (k == BuiltinType::LongDouble) {
669 Lo = X87;
670 Hi = X87Up;
671 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000672 // FIXME: _Decimal32 and _Decimal64 are SSE.
673 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson1d234462009-02-26 17:31:15 +0000674 } else if (const EnumType *ET = Ty->getAsEnumType()) {
675 // Classify the underlying integer type.
676 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000677 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000678 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000679 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000680 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000681 if (Size == 32) {
682 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
683 // float> as integer.
684 Current = Integer;
685
686 // If this type crosses an eightbyte boundary, it should be
687 // split.
688 uint64_t EB_Real = (OffsetBase) / 64;
689 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
690 if (EB_Real != EB_Imag)
691 Hi = Lo;
692 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000693 // gcc passes <1 x double> in memory. :(
694 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000695 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000696
697 // gcc passes <1 x long long> as INTEGER.
698 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
699 Current = Integer;
700 else
701 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000702
703 // If this type crosses an eightbyte boundary, it should be
704 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000705 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000706 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000707 } else if (Size == 128) {
708 Lo = SSE;
709 Hi = SSEUp;
710 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000711 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000712 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000713
Daniel Dunbare413f532009-01-30 18:40:10 +0000714 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000715 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000716 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000717 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000718 else if (Size <= 128)
719 Lo = Hi = Integer;
720 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000721 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000722 else if (ET == Context.DoubleTy)
723 Lo = Hi = SSE;
724 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000725 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000726
727 // If this complex type crosses an eightbyte boundary then it
728 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000729 uint64_t EB_Real = (OffsetBase) / 64;
730 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000731 if (Hi == NoClass && EB_Real != EB_Imag)
732 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000733 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
734 // Arrays are treated like structures.
735
736 uint64_t Size = Context.getTypeSize(Ty);
737
738 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
739 // than two eightbytes, ..., it has class MEMORY.
740 if (Size > 128)
741 return;
742
743 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
744 // fields, it has class MEMORY.
745 //
746 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000747 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000748 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000749
750 // Otherwise implement simplified merge. We could be smarter about
751 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000752 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000753 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
754 uint64_t ArraySize = AT->getSize().getZExtValue();
755 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
756 Class FieldLo, FieldHi;
757 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000758 Lo = merge(Lo, FieldLo);
759 Hi = merge(Hi, FieldHi);
760 if (Lo == Memory || Hi == Memory)
761 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000762 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000763
764 // Do post merger cleanup (see below). Only case we worry about is Memory.
765 if (Hi == Memory)
766 Lo = Memory;
767 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000768 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000769 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000770
771 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
772 // than two eightbytes, ..., it has class MEMORY.
773 if (Size > 128)
774 return;
775
776 const RecordDecl *RD = RT->getDecl();
777
778 // Assume variable sized types are passed in memory.
779 if (RD->hasFlexibleArrayMember())
780 return;
781
782 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
783
784 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000785 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000786 unsigned idx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000787 for (RecordDecl::field_iterator i = RD->field_begin(Context),
788 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000789 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000790 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000791
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000792 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
793 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000794 //
Daniel Dunbaref495d42009-04-27 18:31:32 +0000795 // Note, skip this test for bit-fields, see below.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000796 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000797 Lo = Memory;
798 return;
799 }
800
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000801 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000802 //
803 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
804 // exceeds a single eightbyte, each is classified
805 // separately. Each eightbyte gets initialized to class
806 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000807 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000808
Daniel Dunbaref495d42009-04-27 18:31:32 +0000809 // Bit-fields require special handling, they do not force the
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000810 // structure to be passed in memory even if unaligned, and
811 // therefore they can straddle an eightbyte.
812 if (BitField) {
Daniel Dunbar9bb29952009-05-08 22:26:44 +0000813 // Ignore padding bit-fields.
814 if (i->isUnnamedBitfield())
815 continue;
816
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000817 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman5255e7a2009-04-26 19:19:15 +0000818 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000819
820 uint64_t EB_Lo = Offset / 64;
821 uint64_t EB_Hi = (Offset + Size - 1) / 64;
822 FieldLo = FieldHi = NoClass;
823 if (EB_Lo) {
824 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
825 FieldLo = NoClass;
826 FieldHi = Integer;
827 } else {
828 FieldLo = Integer;
829 FieldHi = EB_Hi ? Integer : NoClass;
830 }
831 } else
832 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000833 Lo = merge(Lo, FieldLo);
834 Hi = merge(Hi, FieldHi);
835 if (Lo == Memory || Hi == Memory)
836 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000837 }
838
839 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
840 //
841 // (a) If one of the classes is MEMORY, the whole argument is
842 // passed in memory.
843 //
844 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
845
846 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000847 // the merge (just bail).
848 //
849 // The second condition occurs in the case of unions; for example
850 // union { _Complex double; unsigned; }.
851 if (Hi == Memory)
852 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000853 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000854 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000855 }
856}
857
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000858ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
859 const llvm::Type *CoerceTo,
860 ASTContext &Context) const {
861 if (CoerceTo == llvm::Type::Int64Ty) {
862 // Integer and pointer types will end up in a general purpose
863 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000864 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000865 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000866
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000867 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000868 // FIXME: It would probably be better to make CGFunctionInfo only
869 // map using canonical types than to canonize here.
870 QualType CTy = Context.getCanonicalType(Ty);
871
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000872 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000873 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000874 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000875
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000876 }
877
878 return ABIArgInfo::getCoerce(CoerceTo);
879}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000880
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000881ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
882 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000883 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
884 // classification algorithm.
885 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000886 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000887
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000888 // Check some invariants.
889 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
890 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
891 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
892
Daniel Dunbare09a9692009-01-24 08:32:22 +0000893 const llvm::Type *ResType = 0;
894 switch (Lo) {
895 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000896 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000897
898 case SSEUp:
899 case X87Up:
900 assert(0 && "Invalid classification for lo word.");
901
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000902 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000903 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000904 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000905 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000906
907 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
908 // available register of the sequence %rax, %rdx is used.
909 case Integer:
910 ResType = llvm::Type::Int64Ty; break;
911
912 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
913 // available SSE register of the sequence %xmm0, %xmm1 is used.
914 case SSE:
915 ResType = llvm::Type::DoubleTy; break;
916
917 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
918 // returned on the X87 stack in %st0 as 80-bit x87 number.
919 case X87:
920 ResType = llvm::Type::X86_FP80Ty; break;
921
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000922 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
923 // part of the value is returned in %st0 and the imaginary part in
924 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000925 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000926 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000927 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
928 llvm::Type::X86_FP80Ty,
929 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000930 break;
931 }
932
933 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000934 // Memory was handled previously and X87 should
935 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000936 case Memory:
937 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000938 assert(0 && "Invalid classification for hi word.");
939
Daniel Dunbar92e88642009-02-17 07:55:55 +0000940 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000941 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000942
Daniel Dunbare09a9692009-01-24 08:32:22 +0000943 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000944 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
945 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000946 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000947 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
948 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000949
950 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
951 // is passed in the upper half of the last used SSE register.
952 //
953 // SSEUP should always be preceeded by SSE, just widen.
954 case SSEUp:
955 assert(Lo == SSE && "Unexpected SSEUp classification.");
956 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
957 break;
958
959 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000960 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000961 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000962 // If X87Up is preceeded by X87, we don't need to do
963 // anything. However, in some cases with unions it may not be
964 // preceeded by X87. In such situations we follow gcc and pass the
965 // extra bits in an SSE reg.
966 if (Lo != X87)
967 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000968 break;
969 }
970
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000971 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000972}
973
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000974ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000975 unsigned &neededInt,
976 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000977 X86_64ABIInfo::Class Lo, Hi;
978 classify(Ty, Context, 0, Lo, Hi);
979
980 // Check some invariants.
981 // FIXME: Enforce these by construction.
982 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
983 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
984 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
985
Daniel Dunbare978cb92009-02-10 17:06:09 +0000986 neededInt = 0;
987 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000988 const llvm::Type *ResType = 0;
989 switch (Lo) {
990 case NoClass:
991 return ABIArgInfo::getIgnore();
992
993 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
994 // on the stack.
995 case Memory:
996
997 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
998 // COMPLEX_X87, it is passed in memory.
999 case X87:
1000 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001001 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001002
1003 case SSEUp:
1004 case X87Up:
1005 assert(0 && "Invalid classification for lo word.");
1006
1007 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1008 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1009 // and %r9 is used.
1010 case Integer:
1011 ++neededInt;
1012 ResType = llvm::Type::Int64Ty;
1013 break;
1014
1015 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1016 // available SSE register is used, the registers are taken in the
1017 // order from %xmm0 to %xmm7.
1018 case SSE:
1019 ++neededSSE;
1020 ResType = llvm::Type::DoubleTy;
1021 break;
Daniel Dunbareec02622009-02-03 06:30:17 +00001022 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001023
1024 switch (Hi) {
1025 // Memory was handled previously, ComplexX87 and X87 should
1026 // never occur as hi classes, and X87Up must be preceed by X87,
1027 // which is passed in memory.
1028 case Memory:
1029 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001030 case ComplexX87:
1031 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001032 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001033
1034 case NoClass: break;
1035 case Integer:
1036 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1037 ++neededInt;
1038 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +00001039
1040 // X87Up generally doesn't occur here (long double is passed in
1041 // memory), except in situations involving unions.
1042 case X87Up:
1043 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001044 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1045 ++neededSSE;
1046 break;
1047
1048 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1049 // eightbyte is passed in the upper half of the last used SSE
1050 // register.
1051 case SSEUp:
1052 assert(Lo == SSE && "Unexpected SSEUp classification.");
1053 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1054 break;
1055 }
1056
Daniel Dunbar87c4dc92009-02-14 02:09:24 +00001057 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001058}
1059
1060void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1061 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1062
1063 // Keep track of the number of assigned registers.
1064 unsigned freeIntRegs = 6, freeSSERegs = 8;
1065
1066 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1067 // get assigned (in left-to-right order) for passing as follows...
1068 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +00001069 it != ie; ++it) {
1070 unsigned neededInt, neededSSE;
1071 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1072
1073 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1074 // eightbyte of an argument, the whole argument is passed on the
1075 // stack. If registers have already been assigned for some
1076 // eightbytes of such an argument, the assignments get reverted.
1077 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1078 freeIntRegs -= neededInt;
1079 freeSSERegs -= neededSSE;
1080 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001081 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +00001082 }
1083 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001084}
1085
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001086static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1087 QualType Ty,
1088 CodeGenFunction &CGF) {
1089 llvm::Value *overflow_arg_area_p =
1090 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1091 llvm::Value *overflow_arg_area =
1092 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1093
1094 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1095 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001096 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001097 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001098 // Note that we follow the ABI & gcc here, even though the type
1099 // could in theory have an alignment greater than 16. This case
1100 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001101
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001102 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1103 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1104 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1105 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1106 llvm::Type::Int64Ty);
1107 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1108 overflow_arg_area =
1109 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1110 overflow_arg_area->getType(),
1111 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001112 }
1113
1114 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1115 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1116 llvm::Value *Res =
1117 CGF.Builder.CreateBitCast(overflow_arg_area,
1118 llvm::PointerType::getUnqual(LTy));
1119
1120 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1121 // l->overflow_arg_area + sizeof(type).
1122 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1123 // an 8 byte boundary.
1124
1125 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1126 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1127 (SizeInBytes + 7) & ~7);
1128 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1129 "overflow_arg_area.next");
1130 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1131
1132 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1133 return Res;
1134}
1135
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001136llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1137 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001138 // Assume that va_list type is correct; should be pointer to LLVM type:
1139 // struct {
1140 // i32 gp_offset;
1141 // i32 fp_offset;
1142 // i8* overflow_arg_area;
1143 // i8* reg_save_area;
1144 // };
1145 unsigned neededInt, neededSSE;
1146 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1147 neededInt, neededSSE);
1148
1149 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1150 // in the registers. If not go to step 7.
1151 if (!neededInt && !neededSSE)
1152 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1153
1154 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1155 // general purpose registers needed to pass type and num_fp to hold
1156 // the number of floating point registers needed.
1157
1158 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1159 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1160 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1161 //
1162 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1163 // register save space).
1164
1165 llvm::Value *InRegs = 0;
1166 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1167 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1168 if (neededInt) {
1169 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1170 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1171 InRegs =
1172 CGF.Builder.CreateICmpULE(gp_offset,
1173 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1174 48 - neededInt * 8),
1175 "fits_in_gp");
1176 }
1177
1178 if (neededSSE) {
1179 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1180 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1181 llvm::Value *FitsInFP =
1182 CGF.Builder.CreateICmpULE(fp_offset,
1183 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001184 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001185 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001186 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001187 }
1188
1189 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1190 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1191 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1192 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1193
1194 // Emit code to load the value if it was passed in registers.
1195
1196 CGF.EmitBlock(InRegBlock);
1197
1198 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1199 // an offset of l->gp_offset and/or l->fp_offset. This may require
1200 // copying to a temporary location in case the parameter is passed
1201 // in different register classes or requires an alignment greater
1202 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001203 //
1204 // FIXME: This really results in shameful code when we end up
1205 // needing to collect arguments from different places; often what
1206 // should result in a simple assembling of a structure from
1207 // scattered addresses has many more loads than necessary. Can we
1208 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001209 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1210 llvm::Value *RegAddr =
1211 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1212 "reg_save_area");
1213 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001214 // FIXME: Cleanup.
1215 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1216 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1217 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1218 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1219 const llvm::Type *TyLo = ST->getElementType(0);
1220 const llvm::Type *TyHi = ST->getElementType(1);
1221 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1222 "Unexpected ABI info for mixed regs");
1223 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1224 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1225 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1226 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1227 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1228 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1229 llvm::Value *V =
1230 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1231 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1232 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1233 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1234
1235 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001236 } else if (neededInt) {
1237 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1238 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1239 llvm::PointerType::getUnqual(LTy));
1240 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001241 if (neededSSE == 1) {
1242 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1243 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1244 llvm::PointerType::getUnqual(LTy));
1245 } else {
1246 assert(neededSSE == 2 && "Invalid number of needed registers!");
1247 // SSE registers are spaced 16 bytes apart in the register save
1248 // area, we need to collect the two eightbytes together.
1249 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1250 llvm::Value *RegAddrHi =
1251 CGF.Builder.CreateGEP(RegAddrLo,
1252 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1253 const llvm::Type *DblPtrTy =
1254 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1255 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1256 llvm::Type::DoubleTy,
1257 NULL);
1258 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1259 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1260 DblPtrTy));
1261 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1262 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1263 DblPtrTy));
1264 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1265 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1266 llvm::PointerType::getUnqual(LTy));
1267 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001268 }
1269
1270 // AMD64-ABI 3.5.7p5: Step 5. Set:
1271 // l->gp_offset = l->gp_offset + num_gp * 8
1272 // l->fp_offset = l->fp_offset + num_fp * 16.
1273 if (neededInt) {
1274 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1275 neededInt * 8);
1276 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1277 gp_offset_p);
1278 }
1279 if (neededSSE) {
1280 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1281 neededSSE * 16);
1282 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1283 fp_offset_p);
1284 }
1285 CGF.EmitBranch(ContBlock);
1286
1287 // Emit code to load the value if it was passed in memory.
1288
1289 CGF.EmitBlock(InMemBlock);
1290 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1291
1292 // Return the appropriate result.
1293
1294 CGF.EmitBlock(ContBlock);
1295 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1296 "vaarg.addr");
1297 ResAddr->reserveOperandSpace(2);
1298 ResAddr->addIncoming(RegAddr, InRegBlock);
1299 ResAddr->addIncoming(MemAddr, InMemBlock);
1300
1301 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001302}
1303
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001304// ABI Info for PIC16
1305class PIC16ABIInfo : public ABIInfo {
1306 ABIArgInfo classifyReturnType(QualType RetTy,
1307 ASTContext &Context) const;
1308
1309 ABIArgInfo classifyArgumentType(QualType RetTy,
1310 ASTContext &Context) const;
1311
1312 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1313 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1314 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1315 it != ie; ++it)
1316 it->info = classifyArgumentType(it->type, Context);
1317 }
1318
1319 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1320 CodeGenFunction &CGF) const;
1321
1322};
1323
1324ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1325 ASTContext &Context) const {
1326 if (RetTy->isVoidType()) {
1327 return ABIArgInfo::getIgnore();
1328 } else {
1329 return ABIArgInfo::getDirect();
1330 }
1331}
1332
1333ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1334 ASTContext &Context) const {
1335 return ABIArgInfo::getDirect();
1336}
1337
1338llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1339 CodeGenFunction &CGF) const {
1340 return 0;
1341}
1342
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001343class ARMABIInfo : public ABIInfo {
1344 ABIArgInfo classifyReturnType(QualType RetTy,
1345 ASTContext &Context) const;
1346
1347 ABIArgInfo classifyArgumentType(QualType RetTy,
1348 ASTContext &Context) const;
1349
1350 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1351
1352 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1353 CodeGenFunction &CGF) const;
1354};
1355
1356void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1357 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1358 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1359 it != ie; ++it) {
1360 it->info = classifyArgumentType(it->type, Context);
1361 }
1362}
1363
1364ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1365 ASTContext &Context) const {
1366 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1367 return ABIArgInfo::getDirect();
1368 }
1369 // FIXME: This is kind of nasty... but there isn't much choice
1370 // because the ARM backend doesn't support byval.
1371 // FIXME: This doesn't handle alignment > 64 bits.
1372 const llvm::Type* ElemTy;
1373 unsigned SizeRegs;
1374 if (Context.getTypeAlign(Ty) > 32) {
1375 ElemTy = llvm::Type::Int64Ty;
1376 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1377 } else {
1378 ElemTy = llvm::Type::Int32Ty;
1379 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1380 }
1381 std::vector<const llvm::Type*> LLVMFields;
1382 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1383 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1384 return ABIArgInfo::getCoerce(STy);
1385}
1386
1387ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1388 ASTContext &Context) const {
1389 if (RetTy->isVoidType()) {
1390 return ABIArgInfo::getIgnore();
1391 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1392 // Aggregates <= 4 bytes are returned in r0; other aggregates
1393 // are returned indirectly.
1394 uint64_t Size = Context.getTypeSize(RetTy);
1395 if (Size <= 32)
1396 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1397 return ABIArgInfo::getIndirect(0);
1398 } else {
1399 return ABIArgInfo::getDirect();
1400 }
1401}
1402
1403llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1404 CodeGenFunction &CGF) const {
1405 // FIXME: Need to handle alignment
1406 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1407 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1408
1409 CGBuilderTy &Builder = CGF.Builder;
1410 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1411 "ap");
1412 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1413 llvm::Type *PTy =
1414 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1415 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1416
1417 uint64_t Offset =
1418 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1419 llvm::Value *NextAddr =
1420 Builder.CreateGEP(Addr,
1421 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1422 "ap.next");
1423 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1424
1425 return AddrTyped;
1426}
1427
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001428ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001429 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001430 if (RetTy->isVoidType()) {
1431 return ABIArgInfo::getIgnore();
1432 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001433 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001434 } else {
1435 return ABIArgInfo::getDirect();
1436 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001437}
1438
1439ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001440 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001441 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001442 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001443 } else {
1444 return ABIArgInfo::getDirect();
1445 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001446}
1447
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001448llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1449 CodeGenFunction &CGF) const {
1450 return 0;
1451}
1452
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001453const ABIInfo &CodeGenTypes::getABIInfo() const {
1454 if (TheABIInfo)
1455 return *TheABIInfo;
1456
1457 // For now we just cache this in the CodeGenTypes and don't bother
1458 // to free it.
1459 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1460 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman5e175802009-03-23 23:26:24 +00001461 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001462 switch (getContext().Target.getPointerWidth(0)) {
1463 case 32:
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001464 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001465 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001466 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001467 }
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001468 } else if (strcmp(TargetPrefix, "arm") == 0) {
1469 // FIXME: Support for OABI?
1470 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta88b4e512009-04-21 06:01:16 +00001471 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1472 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001473 }
1474
1475 return *(TheABIInfo = new DefaultABIInfo);
1476}
1477
Daniel Dunbare126ab12008-09-10 02:41:04 +00001478/***/
1479
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001480CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1481 const llvm::SmallVector<QualType, 16> &ArgTys) {
1482 NumArgs = ArgTys.size();
1483 Args = new ArgInfo[1 + NumArgs];
1484 Args[0].type = ResTy;
1485 for (unsigned i = 0; i < NumArgs; ++i)
1486 Args[1 + i].type = ArgTys[i];
1487}
1488
1489/***/
1490
Daniel Dunbar04d35782008-09-17 00:51:38 +00001491void CodeGenTypes::GetExpandedTypes(QualType Ty,
1492 std::vector<const llvm::Type*> &ArgTys) {
1493 const RecordType *RT = Ty->getAsStructureType();
1494 assert(RT && "Can only expand structure types.");
1495 const RecordDecl *RD = RT->getDecl();
1496 assert(!RD->hasFlexibleArrayMember() &&
1497 "Cannot expand structure with flexible array.");
1498
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001499 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1500 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001501 const FieldDecl *FD = *i;
1502 assert(!FD->isBitField() &&
1503 "Cannot expand structure with bit-field members.");
1504
1505 QualType FT = FD->getType();
1506 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1507 GetExpandedTypes(FT, ArgTys);
1508 } else {
1509 ArgTys.push_back(ConvertType(FT));
1510 }
1511 }
1512}
1513
1514llvm::Function::arg_iterator
1515CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1516 llvm::Function::arg_iterator AI) {
1517 const RecordType *RT = Ty->getAsStructureType();
1518 assert(RT && "Can only expand structure types.");
1519
1520 RecordDecl *RD = RT->getDecl();
1521 assert(LV.isSimple() &&
1522 "Unexpected non-simple lvalue during struct expansion.");
1523 llvm::Value *Addr = LV.getAddress();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001524 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1525 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001526 FieldDecl *FD = *i;
1527 QualType FT = FD->getType();
1528
1529 // FIXME: What are the right qualifiers here?
1530 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1531 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1532 AI = ExpandTypeFromArgs(FT, LV, AI);
1533 } else {
1534 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1535 ++AI;
1536 }
1537 }
1538
1539 return AI;
1540}
1541
1542void
1543CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1544 llvm::SmallVector<llvm::Value*, 16> &Args) {
1545 const RecordType *RT = Ty->getAsStructureType();
1546 assert(RT && "Can only expand structure types.");
1547
1548 RecordDecl *RD = RT->getDecl();
1549 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1550 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001551 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1552 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-09-17 00:51:38 +00001553 FieldDecl *FD = *i;
1554 QualType FT = FD->getType();
1555
1556 // FIXME: What are the right qualifiers here?
1557 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1558 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1559 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1560 } else {
1561 RValue RV = EmitLoadOfLValue(LV, FT);
1562 assert(RV.isScalar() &&
1563 "Unexpected non-scalar rvalue during struct expansion.");
1564 Args.push_back(RV.getScalarVal());
1565 }
1566 }
1567}
1568
Daniel Dunbar84379912009-02-02 19:06:38 +00001569/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1570/// a pointer to an object of type \arg Ty.
1571///
1572/// This safely handles the case when the src type is smaller than the
1573/// destination type; in this situation the values of bits which not
1574/// present in the src are undefined.
1575static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1576 const llvm::Type *Ty,
1577 CodeGenFunction &CGF) {
1578 const llvm::Type *SrcTy =
1579 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001580 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1581 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar84379912009-02-02 19:06:38 +00001582
Daniel Dunbar77071992009-02-03 05:59:18 +00001583 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001584 if (SrcSize == DstSize) {
1585 llvm::Value *Casted =
1586 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001587 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1588 // FIXME: Use better alignment / avoid requiring aligned load.
1589 Load->setAlignment(1);
1590 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001591 } else {
1592 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1593
1594 // Otherwise do coercion through memory. This is stupid, but
1595 // simple.
1596 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1597 llvm::Value *Casted =
1598 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001599 llvm::StoreInst *Store =
1600 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1601 // FIXME: Use better alignment / avoid requiring aligned store.
1602 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001603 return CGF.Builder.CreateLoad(Tmp);
1604 }
1605}
1606
1607/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1608/// where the source and destination may have different types.
1609///
1610/// This safely handles the case when the src type is larger than the
1611/// destination type; the upper bits of the src will be lost.
1612static void CreateCoercedStore(llvm::Value *Src,
1613 llvm::Value *DstPtr,
1614 CodeGenFunction &CGF) {
1615 const llvm::Type *SrcTy = Src->getType();
1616 const llvm::Type *DstTy =
1617 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1618
Duncan Sandsee6f6f82009-05-09 07:08:47 +00001619 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1620 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar84379912009-02-02 19:06:38 +00001621
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001622 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001623 if (SrcSize == DstSize) {
1624 llvm::Value *Casted =
1625 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001626 // FIXME: Use better alignment / avoid requiring aligned store.
1627 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001628 } else {
1629 assert(SrcSize > DstSize && "Coercion is missing bits!");
1630
1631 // Otherwise do coercion through memory. This is stupid, but
1632 // simple.
1633 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1634 CGF.Builder.CreateStore(Src, Tmp);
1635 llvm::Value *Casted =
1636 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001637 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1638 // FIXME: Use better alignment / avoid requiring aligned load.
1639 Load->setAlignment(1);
1640 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001641 }
1642}
1643
Daniel Dunbar04d35782008-09-17 00:51:38 +00001644/***/
1645
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001646bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001647 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001648}
1649
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001650const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001651CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001652 std::vector<const llvm::Type*> ArgTys;
1653
1654 const llvm::Type *ResultType = 0;
1655
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001656 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001657 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001658 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001659 case ABIArgInfo::Expand:
1660 assert(0 && "Invalid ABI kind for return argument");
1661
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001662 case ABIArgInfo::Direct:
1663 ResultType = ConvertType(RetTy);
1664 break;
1665
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001666 case ABIArgInfo::Indirect: {
1667 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001668 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001669 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001670 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1671 break;
1672 }
1673
Daniel Dunbar1358b202009-01-26 21:26:08 +00001674 case ABIArgInfo::Ignore:
1675 ResultType = llvm::Type::VoidTy;
1676 break;
1677
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001678 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001679 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001680 break;
1681 }
1682
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001683 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1684 ie = FI.arg_end(); it != ie; ++it) {
1685 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001686
1687 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001688 case ABIArgInfo::Ignore:
1689 break;
1690
Daniel Dunbar04d35782008-09-17 00:51:38 +00001691 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001692 ArgTys.push_back(AI.getCoerceToType());
1693 break;
1694
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001695 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001696 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001697 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1698 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001699 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001700 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001701
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001702 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001703 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001704 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001705
1706 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001707 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001708 break;
1709 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001710 }
1711
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001712 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001713}
1714
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001715void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001716 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001717 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001718 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001719 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001720
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001721 // FIXME: handle sseregparm someday...
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001722 if (TargetDecl) {
Daniel Dunbar78582862009-04-13 21:08:27 +00001723 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001724 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar78582862009-04-13 21:08:27 +00001725 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001726 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbar78582862009-04-13 21:08:27 +00001727 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssondd6791c2008-10-05 23:32:53 +00001728 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar78582862009-04-13 21:08:27 +00001729 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar521c3a32009-04-10 22:14:52 +00001730 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001731 }
1732
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001733 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001734 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001735 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001736 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001737 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001738 if (RetTy->isPromotableIntegerType()) {
1739 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001740 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001741 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001742 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001743 }
1744 }
1745 break;
1746
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001747 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001748 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001749 llvm::Attribute::StructRet |
1750 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001751 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001752 // sret disables readnone and readonly
1753 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1754 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001755 break;
1756
Daniel Dunbar1358b202009-01-26 21:26:08 +00001757 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001758 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001759 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001760
Daniel Dunbar22e30052008-09-11 01:48:57 +00001761 case ABIArgInfo::Expand:
1762 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001763 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001764
Devang Patel2bb6eb82008-09-26 22:53:57 +00001765 if (RetAttrs)
1766 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001767
1768 // FIXME: we need to honour command line settings also...
1769 // FIXME: RegParm should be reduced in case of nested functions and/or global
1770 // register variable.
1771 signed RegParm = 0;
1772 if (TargetDecl)
1773 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1774 RegParm = RegParmAttr->getNumParams();
1775
1776 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001777 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1778 ie = FI.arg_end(); it != ie; ++it) {
1779 QualType ParamType = it->type;
1780 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001781 unsigned Attributes = 0;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001782
Daniel Dunbar22e30052008-09-11 01:48:57 +00001783 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001784 case ABIArgInfo::Coerce:
1785 break;
1786
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001787 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001788 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001789 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001790 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001791 // byval disables readnone and readonly.
1792 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1793 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001794 break;
1795
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001796 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001797 if (ParamType->isPromotableIntegerType()) {
1798 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001799 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001800 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001801 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001802 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001803 }
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001804 if (RegParm > 0 &&
1805 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1806 RegParm -=
1807 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1808 if (RegParm >= 0)
1809 Attributes |= llvm::Attribute::InReg;
1810 }
1811 // FIXME: handle sseregparm someday...
Daniel Dunbar22e30052008-09-11 01:48:57 +00001812 break;
Anton Korobeynikov2431e602009-04-04 00:49:24 +00001813
Daniel Dunbar1358b202009-01-26 21:26:08 +00001814 case ABIArgInfo::Ignore:
1815 // Skip increment, no matching LLVM parameter.
1816 continue;
1817
Daniel Dunbar04d35782008-09-17 00:51:38 +00001818 case ABIArgInfo::Expand: {
1819 std::vector<const llvm::Type*> Tys;
1820 // FIXME: This is rather inefficient. Do we ever actually need
1821 // to do anything here? The result should be just reconstructed
1822 // on the other side, so extension should be a non-issue.
1823 getTypes().GetExpandedTypes(ParamType, Tys);
1824 Index += Tys.size();
1825 continue;
1826 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001827 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001828
Devang Patela85a9ef2008-09-25 21:02:23 +00001829 if (Attributes)
1830 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001831 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001832 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001833 if (FuncAttrs)
1834 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001835}
1836
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001837void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1838 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001839 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001840 // FIXME: We no longer need the types from FunctionArgList; lift up
1841 // and simplify.
1842
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001843 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1844 llvm::Function::arg_iterator AI = Fn->arg_begin();
1845
1846 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001847 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001848 AI->setName("agg.result");
1849 ++AI;
1850 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001851
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001852 assert(FI.arg_size() == Args.size() &&
1853 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001854 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001855 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001856 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001857 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001858 QualType Ty = info_it->type;
1859 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001860
1861 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001862 case ABIArgInfo::Indirect: {
1863 llvm::Value* V = AI;
1864 if (hasAggregateLLVMType(Ty)) {
1865 // Do nothing, aggregates and complex variables are accessed by
1866 // reference.
1867 } else {
1868 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001869 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001870 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1871 // This must be a promotion, for something like
1872 // "void a(x) short x; {..."
1873 V = EmitScalarConversion(V, Ty, Arg->getType());
1874 }
1875 }
1876 EmitParmDecl(*Arg, V);
1877 break;
1878 }
1879
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001880 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001881 assert(AI != Fn->arg_end() && "Argument mismatch!");
1882 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001883 if (hasAggregateLLVMType(Ty)) {
1884 // Create a temporary alloca to hold the argument; the rest of
1885 // codegen expects to access aggregates & complex values by
1886 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001887 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001888 Builder.CreateStore(AI, V);
1889 } else {
1890 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1891 // This must be a promotion, for something like
1892 // "void a(x) short x; {..."
1893 V = EmitScalarConversion(V, Ty, Arg->getType());
1894 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001895 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001896 EmitParmDecl(*Arg, V);
1897 break;
1898 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001899
1900 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001901 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001902 // we need to create a temporary and reconstruct it from the
1903 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001904 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001905 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001906 (Name + ".addr").c_str());
1907 // FIXME: What are the right qualifiers here?
1908 llvm::Function::arg_iterator End =
1909 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1910 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001911
Daniel Dunbar04d35782008-09-17 00:51:38 +00001912 // Name the arguments used in expansion and increment AI.
1913 unsigned Index = 0;
1914 for (; AI != End; ++AI, ++Index)
1915 AI->setName(Name + "." + llvm::utostr(Index));
1916 continue;
1917 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001918
1919 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001920 // Initialize the local variable appropriately.
1921 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001922 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001923 } else {
1924 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1925 }
1926
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001927 // Skip increment, no matching LLVM parameter.
1928 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001929
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001930 case ABIArgInfo::Coerce: {
1931 assert(AI != Fn->arg_end() && "Argument mismatch!");
1932 // FIXME: This is very wasteful; EmitParmDecl is just going to
1933 // drop the result in a new alloca anyway, so we could just
1934 // store into that directly if we broke the abstraction down
1935 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001936 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001937 CreateCoercedStore(AI, V, *this);
1938 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001939 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001940 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001941 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1942 // This must be a promotion, for something like
1943 // "void a(x) short x; {..."
1944 V = EmitScalarConversion(V, Ty, Arg->getType());
1945 }
1946 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001947 EmitParmDecl(*Arg, V);
1948 break;
1949 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001950 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001951
1952 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001953 }
1954 assert(AI == Fn->arg_end() && "Argument mismatch!");
1955}
1956
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001957void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001958 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001959 llvm::Value *RV = 0;
1960
1961 // Functions with no result always return void.
1962 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001963 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001964 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001965
1966 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001967 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001968 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001969 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1970 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1971 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1972 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1973 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001974 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1975 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001976 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001977 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001978
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001979 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001980 // The internal return value temp always will have
1981 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001982 RV = Builder.CreateLoad(ReturnValue);
1983 break;
1984
Daniel Dunbar1358b202009-01-26 21:26:08 +00001985 case ABIArgInfo::Ignore:
1986 break;
1987
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001988 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001989 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001990 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001991
Daniel Dunbar22e30052008-09-11 01:48:57 +00001992 case ABIArgInfo::Expand:
1993 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001994 }
1995 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001996
1997 if (RV) {
1998 Builder.CreateRet(RV);
1999 } else {
2000 Builder.CreateRetVoid();
2001 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002002}
2003
Anders Carlssond927fa72009-04-08 20:47:54 +00002004RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
2005 return EmitAnyExprToTemp(E);
2006}
2007
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00002008RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
2009 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002010 const CallArgList &CallArgs,
2011 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00002012 // FIXME: We no longer need the types from CallArgs; lift up and
2013 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002014 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002015
2016 // Handle struct-return functions by passing a pointer to the
2017 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00002018 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00002019 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00002020 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002021 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002022 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002023 }
2024
Daniel Dunbar14c884a2009-02-04 21:17:21 +00002025 assert(CallInfo.arg_size() == CallArgs.size() &&
2026 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00002027 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002028 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00002029 I != E; ++I, ++info_it) {
2030 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002031 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00002032
2033 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002034 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002035 if (RV.isScalar() || RV.isComplex()) {
2036 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002037 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002038 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002039 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00002040 else
2041 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2042 } else {
2043 Args.push_back(RV.getAggregateAddr());
2044 }
2045 break;
2046
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002047 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00002048 if (RV.isScalar()) {
2049 Args.push_back(RV.getScalarVal());
2050 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002051 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2052 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2053 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2054 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00002055 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00002056 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00002057 }
2058 break;
2059
Daniel Dunbar1358b202009-01-26 21:26:08 +00002060 case ABIArgInfo::Ignore:
2061 break;
2062
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002063 case ABIArgInfo::Coerce: {
2064 // FIXME: Avoid the conversion through memory if possible.
2065 llvm::Value *SrcPtr;
2066 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00002067 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002068 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002069 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002070 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002071 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2072 } else
2073 SrcPtr = RV.getAggregateAddr();
2074 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2075 *this));
2076 break;
2077 }
2078
Daniel Dunbar04d35782008-09-17 00:51:38 +00002079 case ABIArgInfo::Expand:
2080 ExpandTypeToArgs(I->second, RV, Args);
2081 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002082 }
2083 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002084
Daniel Dunbar0a067402009-02-23 17:26:39 +00002085 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00002086 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00002087 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00002088 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2089 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00002090
Daniel Dunbar90e43452009-03-02 04:32:35 +00002091 llvm::CallSite CS;
2092 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2093 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002094 } else {
2095 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00002096 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2097 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00002098 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00002099 }
2100
Daniel Dunbar90e43452009-03-02 04:32:35 +00002101 CS.setAttributes(Attrs);
2102 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2103 CS.setCallingConv(F->getCallingConv());
2104
2105 // If the call doesn't return, finish the basic block and clear the
2106 // insertion point; this allows the rest of IRgen to discard
2107 // unreachable code.
2108 if (CS.doesNotReturn()) {
2109 Builder.CreateUnreachable();
2110 Builder.ClearInsertionPoint();
2111
2112 // FIXME: For now, emit a dummy basic block because expr
2113 // emitters in generally are not ready to handle emitting
2114 // expressions at unreachable points.
2115 EnsureInsertPoint();
2116
2117 // Return a reasonable RValue.
2118 return GetUndefRValue(RetTy);
2119 }
2120
2121 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +00002122 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002123 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00002124
2125 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002126 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00002127 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00002128 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +00002129 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00002130 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +00002131 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00002132
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002133 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00002134 if (RetTy->isAnyComplexType()) {
2135 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2136 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2137 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +00002138 }
2139 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002140 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00002141 Builder.CreateStore(CI, V);
2142 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002143 }
2144 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00002145
Daniel Dunbar1358b202009-01-26 21:26:08 +00002146 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00002147 // If we are ignoring an argument that had a result, make sure to
2148 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00002149 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00002150
Daniel Dunbar73d66602008-09-10 07:04:09 +00002151 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002152 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002153 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00002154 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00002155 if (RetTy->isAnyComplexType())
2156 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +00002157 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00002158 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002159 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00002160 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00002161
Daniel Dunbar22e30052008-09-11 01:48:57 +00002162 case ABIArgInfo::Expand:
2163 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002164 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00002165
2166 assert(0 && "Unhandled ABIArgInfo::Kind");
2167 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002168}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00002169
2170/* VarArg handling */
2171
2172llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2173 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2174}