blob: 2223f65acf468ab02b3733f7bd0079990d4b909d [file] [log] [blame]
Daniel Dunbar0dbe2272008-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 Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000021#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000022#include "clang/AST/DeclObjC.h"
Daniel Dunbar99037e52009-01-29 08:13:58 +000023#include "clang/AST/RecordLayout.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000024#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000025#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000026#include "llvm/Support/CallSite.h"
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +000027#include "llvm/Support/CommandLine.h"
Daniel Dunbarbe9eb092009-02-12 09:04:14 +000028#include "llvm/Support/MathExtras.h"
Daniel Dunbar6f7279b2009-02-04 23:24:38 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000030#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000031
32#include "ABIInfo.h"
33
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000034using namespace clang;
35using namespace CodeGen;
36
37/***/
38
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000039// FIXME: Use iterator and sidestep silly type array creation.
40
Daniel Dunbar541b63b2009-02-02 23:23:47 +000041const
Douglas Gregor72564e72009-02-26 23:50:07 +000042CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000043 return getFunctionInfo(FTNP->getResultType(),
44 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000045}
46
Daniel Dunbar541b63b2009-02-02 23:23:47 +000047const
Douglas Gregor72564e72009-02-26 23:50:07 +000048CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000049 llvm::SmallVector<QualType, 16> ArgTys;
50 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000051 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000052 ArgTys.push_back(FTP->getArgType(i));
53 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000054}
55
Anders Carlssonf6f8ae52009-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 Dunbar541b63b2009-02-02 23:23:47 +000067const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Anders Carlssonf6f8ae52009-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 Dunbar0dbe2272008-09-08 21:33:45 +000073 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +000074 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar541b63b2009-02-02 23:23:47 +000075 return getFunctionInfo(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +000076 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000077}
78
Daniel Dunbar541b63b2009-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 Lattner20732162009-02-20 06:23:21 +000084 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000085 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000086 ArgTys.push_back((*i)->getType());
87 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000088}
89
Daniel Dunbar541b63b2009-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 Dunbar725ad312009-01-31 02:19:00 +000094 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
95 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000096 ArgTys.push_back(i->second);
97 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000098}
99
Daniel Dunbar541b63b2009-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 Dunbarbb36d332009-02-02 21:43:58 +0000104 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
105 i != e; ++i)
Daniel Dunbar541b63b2009-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 Dunbar40a6be62009-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 Dunbar88c2fa92009-02-03 05:31:23 +0000121 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000122 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000123 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000124
125 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000126 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000127
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000128 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000129}
130
131/***/
132
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000133ABIInfo::~ABIInfo() {}
134
Daniel Dunbar6f7279b2009-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 Dunbar6f7279b2009-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 Dunbar6f7279b2009-02-04 23:24:38 +0000147 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000148 case Indirect:
149 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-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 Dunbar5bde6f42009-03-31 19:01:39 +0000160/// isEmptyRecord - Return true iff a structure has no non-empty
Daniel Dunbar834af452008-09-17 21:22:33 +0000161/// members. Note that a structure with a flexible array member is not
162/// considered empty.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000163static bool isEmptyRecord(ASTContext &Context, QualType T) {
Daniel Dunbar5bde6f42009-03-31 19:01:39 +0000164 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar834af452008-09-17 21:22:33 +0000165 if (!RT)
166 return 0;
167 const RecordDecl *RD = RT->getDecl();
168 if (RD->hasFlexibleArrayMember())
169 return false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000170 for (RecordDecl::field_iterator i = RD->field_begin(Context),
171 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000172 const FieldDecl *FD = *i;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000173 if (!isEmptyRecord(Context, FD->getType()))
Daniel Dunbar834af452008-09-17 21:22:33 +0000174 return false;
175 }
176 return true;
177}
178
179/// isSingleElementStruct - Determine if a structure is a "single
180/// element struct", i.e. it has exactly one non-empty field or
181/// exactly one field which is itself a single element
182/// struct. Structures with flexible array members are never
183/// considered single element structs.
184///
185/// \return The field declaration for the single non-empty field, if
186/// it exists.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000187static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000188 const RecordType *RT = T->getAsStructureType();
189 if (!RT)
190 return 0;
191
192 const RecordDecl *RD = RT->getDecl();
193 if (RD->hasFlexibleArrayMember())
194 return 0;
195
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000196 const Type *Found = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000197 for (RecordDecl::field_iterator i = RD->field_begin(Context),
198 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000199 const FieldDecl *FD = *i;
200 QualType FT = FD->getType();
201
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000202 // Treat single element arrays as the element
203 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
204 if (AT->getSize().getZExtValue() == 1)
205 FT = AT->getElementType();
206
Daniel Dunbarfcab2ca2009-05-08 21:04:47 +0000207 // Ignore empty records and padding bit-fields.
Daniel Dunbar8236bf12009-05-08 22:26:44 +0000208 if (isEmptyRecord(Context, FT) || FD->isUnnamedBitfield())
Daniel Dunbarfcab2ca2009-05-08 21:04:47 +0000209 continue;
210
211 if (Found)
Daniel Dunbar834af452008-09-17 21:22:33 +0000212 return 0;
Daniel Dunbarfcab2ca2009-05-08 21:04:47 +0000213
214 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000215 Found = FT.getTypePtr();
Daniel Dunbar834af452008-09-17 21:22:33 +0000216 } else {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000217 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000218 if (!Found)
219 return 0;
220 }
221 }
222
223 return Found;
224}
225
226static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
227 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
228 return false;
229
230 uint64_t Size = Context.getTypeSize(Ty);
231 return Size == 32 || Size == 64;
232}
233
234static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
235 ASTContext &Context) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000236 for (RecordDecl::field_iterator i = RD->field_begin(Context),
237 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000238 const FieldDecl *FD = *i;
239
240 if (!is32Or64BitBasicType(FD->getType(), Context))
241 return false;
242
Daniel Dunbar8e034442009-04-27 18:31:32 +0000243 // FIXME: Reject bit-fields wholesale; there are two problems, we
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000244 // don't know how to expand them yet, and the predicate for
245 // telling if a bitfield still counts as "basic" is more
246 // complicated than what we were doing previously.
247 if (FD->isBitField())
248 return false;
Daniel Dunbar834af452008-09-17 21:22:33 +0000249 }
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000250
Daniel Dunbar834af452008-09-17 21:22:33 +0000251 return true;
252}
253
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000254namespace {
255/// DefaultABIInfo - The default implementation for ABI specific
256/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000257/// self-consistent and sensible LLVM IR generation, but does not
258/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000259class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000260 ABIArgInfo classifyReturnType(QualType RetTy,
261 ASTContext &Context) const;
262
263 ABIArgInfo classifyArgumentType(QualType RetTy,
264 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000265
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000266 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
267 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
268 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
269 it != ie; ++it)
270 it->info = classifyArgumentType(it->type, Context);
271 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000272
273 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
274 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000275};
276
277/// X86_32ABIInfo - The X86-32 ABI information.
278class X86_32ABIInfo : public ABIInfo {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000279 ASTContext &Context;
Eli Friedman9fd58e82009-03-23 23:26:24 +0000280 bool IsDarwin;
281
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000282 static bool isRegisterSize(unsigned Size) {
283 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
284 }
285
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000286 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
287
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000288public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000289 ABIArgInfo classifyReturnType(QualType RetTy,
290 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000291
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000292 ABIArgInfo classifyArgumentType(QualType RetTy,
293 ASTContext &Context) const;
294
295 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
296 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
297 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
298 it != ie; ++it)
299 it->info = classifyArgumentType(it->type, Context);
300 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000301
302 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
303 CodeGenFunction &CGF) const;
Eli Friedman9fd58e82009-03-23 23:26:24 +0000304
Douglas Gregor6ab35242009-04-09 21:40:53 +0000305 X86_32ABIInfo(ASTContext &Context, bool d)
306 : ABIInfo(), Context(Context), IsDarwin(d) {}
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000307};
308}
309
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000310
311/// shouldReturnTypeInRegister - Determine if the given type should be
312/// passed in a register (for the Darwin ABI).
313bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
314 ASTContext &Context) {
315 uint64_t Size = Context.getTypeSize(Ty);
316
317 // Type must be register sized.
318 if (!isRegisterSize(Size))
319 return false;
320
321 if (Ty->isVectorType()) {
322 // 64- and 128- bit vectors inside structures are not returned in
323 // registers.
324 if (Size == 64 || Size == 128)
325 return false;
326
327 return true;
328 }
329
330 // If this is a builtin, pointer, or complex type, it is ok.
331 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
332 return true;
333
334 // Arrays are treated like records.
335 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
336 return shouldReturnTypeInRegister(AT->getElementType(), Context);
337
338 // Otherwise, it must be a record type.
339 const RecordType *RT = Ty->getAsRecordType();
340 if (!RT) return false;
341
342 // Structure types are passed in register if all fields would be
343 // passed in a register.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000344 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context),
345 e = RT->getDecl()->field_end(Context); i != e; ++i) {
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000346 const FieldDecl *FD = *i;
347
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000348 // Empty structures are ignored.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000349 if (isEmptyRecord(Context, FD->getType()))
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000350 continue;
351
Daniel Dunbarf7fff322009-05-08 20:21:04 +0000352 // As are arrays of empty structures, but not generally, so we
353 // can't add this test higher in this routine.
354 if (const ConstantArrayType *AT =
355 Context.getAsConstantArrayType(FD->getType()))
356 if (isEmptyRecord(Context, AT->getElementType()))
357 continue;
358
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000359 // Check fields recursively.
360 if (!shouldReturnTypeInRegister(FD->getType(), Context))
361 return false;
362 }
363
364 return true;
365}
366
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000367ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
368 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000369 if (RetTy->isVoidType()) {
370 return ABIArgInfo::getIgnore();
Daniel Dunbar36043162009-04-01 06:13:08 +0000371 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
372 // On Darwin, some vectors are returned in registers.
373 if (IsDarwin) {
374 uint64_t Size = Context.getTypeSize(RetTy);
375
376 // 128-bit vectors are a special case; they are returned in
377 // registers and we need to make sure to pick a type the LLVM
378 // backend will like.
379 if (Size == 128)
380 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
381 2));
382
383 // Always return in register if it fits in a general purpose
384 // register, or if it is 64 bits and has a single element.
385 if ((Size == 8 || Size == 16 || Size == 32) ||
386 (Size == 64 && VT->getNumElements() == 1))
387 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
388
389 return ABIArgInfo::getIndirect(0);
390 }
391
392 return ABIArgInfo::getDirect();
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000393 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8e034442009-04-27 18:31:32 +0000394 // Structures with flexible arrays are always indirect.
395 if (const RecordType *RT = RetTy->getAsStructureType())
396 if (RT->getDecl()->hasFlexibleArrayMember())
397 return ABIArgInfo::getIndirect(0);
398
Eli Friedman9fd58e82009-03-23 23:26:24 +0000399 // Outside of Darwin, structs and unions are always indirect.
400 if (!IsDarwin && !RetTy->isAnyComplexType())
401 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8e034442009-04-27 18:31:32 +0000402
Daniel Dunbar834af452008-09-17 21:22:33 +0000403 // Classify "single element" structs as their element type.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000404 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000405 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000406 if (BT->isIntegerType()) {
Daniel Dunbar2e001162009-05-08 21:30:11 +0000407 // We need to use the size of the structure, padding
408 // bit-fields can adjust that to be larger than the single
409 // element type.
410 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar834af452008-09-17 21:22:33 +0000411 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
412 } else if (BT->getKind() == BuiltinType::Float) {
Daniel Dunbar2e001162009-05-08 21:30:11 +0000413 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
414 "Unexpect single element structure size!");
Daniel Dunbar834af452008-09-17 21:22:33 +0000415 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
416 } else if (BT->getKind() == BuiltinType::Double) {
Daniel Dunbar2e001162009-05-08 21:30:11 +0000417 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
418 "Unexpect single element structure size!");
Daniel Dunbar834af452008-09-17 21:22:33 +0000419 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
420 }
421 } else if (SeltTy->isPointerType()) {
422 // FIXME: It would be really nice if this could come out as
423 // the proper pointer type.
424 llvm::Type *PtrTy =
425 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
426 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000427 } else if (SeltTy->isVectorType()) {
428 // 64- and 128-bit vectors are never returned in a
429 // register when inside a structure.
430 uint64_t Size = Context.getTypeSize(RetTy);
431 if (Size == 64 || Size == 128)
432 return ABIArgInfo::getIndirect(0);
433
434 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000435 }
436 }
437
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000438 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000439 if (isRegisterSize(Size)) {
440 // Always return in register for unions for now.
441 // FIXME: This is wrong, but better than treating as a
442 // structure.
443 if (RetTy->isUnionType())
444 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
445
446 // Small structures which are register sized are generally returned
447 // in a register.
448 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context))
449 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
450 }
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000451
452 return ABIArgInfo::getIndirect(0);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000453 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000454 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000455 }
456}
457
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000458ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000459 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000460 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000461 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000462 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000463 if (const RecordType *RT = Ty->getAsStructureType())
464 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000465 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000466
Daniel Dunbar3170c932009-02-05 01:50:07 +0000467 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000468 uint64_t Size = Context.getTypeSize(Ty);
469 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000470 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000471
472 // Expand structs with size <= 128-bits which consist only of
473 // basic types (int, long long, float, double, xxx*). This is
474 // non-recursive and does not ignore empty fields.
475 if (const RecordType *RT = Ty->getAsStructureType()) {
476 if (Context.getTypeSize(Ty) <= 4*32 &&
477 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
478 return ABIArgInfo::getExpand();
479 }
480
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000481 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000482 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000483 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000484 }
485}
486
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000487llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
488 CodeGenFunction &CGF) const {
489 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
490 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
491
492 CGBuilderTy &Builder = CGF.Builder;
493 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
494 "ap");
495 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
496 llvm::Type *PTy =
497 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
498 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
499
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000500 uint64_t Offset =
501 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000502 llvm::Value *NextAddr =
503 Builder.CreateGEP(Addr,
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000504 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000505 "ap.next");
506 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
507
508 return AddrTyped;
509}
510
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000511namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000512/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000513class X86_64ABIInfo : public ABIInfo {
514 enum Class {
515 Integer = 0,
516 SSE,
517 SSEUp,
518 X87,
519 X87Up,
520 ComplexX87,
521 NoClass,
522 Memory
523 };
524
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000525 /// merge - Implement the X86_64 ABI merging algorithm.
526 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000527 /// Merge an accumulating classification \arg Accum with a field
528 /// classification \arg Field.
529 ///
530 /// \param Accum - The accumulating classification. This should
531 /// always be either NoClass or the result of a previous merge
532 /// call. In addition, this should never be Memory (the caller
533 /// should just return Memory for the aggregate).
534 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000535
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000536 /// classify - Determine the x86_64 register classes in which the
537 /// given type T should be passed.
538 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000539 /// \param Lo - The classification for the parts of the type
540 /// residing in the low word of the containing object.
541 ///
542 /// \param Hi - The classification for the parts of the type
543 /// residing in the high word of the containing object.
544 ///
545 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000546 /// containing object. Some parameters are classified different
547 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000548 ///
549 /// If a word is unused its result will be NoClass; if a type should
550 /// be passed in Memory then at least the classification of \arg Lo
551 /// will be Memory.
552 ///
553 /// The \arg Lo class will be NoClass iff the argument is ignored.
554 ///
555 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000556 /// also be ComplexX87.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000557 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000558 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000559
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000560 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
561 /// to coerce to, chose the best way to pass Ty in the same place
562 /// that \arg CoerceTo would be passed, but while keeping the
563 /// emitted code as simple as possible.
564 ///
565 /// FIXME: Note, this should be cleaned up to just take an
566 /// enumeration of all the ways we might want to pass things,
567 /// instead of constructing an LLVM type. This makes this code more
568 /// explicit, and it makes it clearer that we are also doing this
569 /// for correctness in the case of passing scalar types.
570 ABIArgInfo getCoerceResult(QualType Ty,
571 const llvm::Type *CoerceTo,
572 ASTContext &Context) const;
573
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000574 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000575 ASTContext &Context) const;
576
577 ABIArgInfo classifyArgumentType(QualType Ty,
578 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000579 unsigned &neededInt,
580 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000581
582public:
583 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000584
585 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
586 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000587};
588}
589
Daniel Dunbarc4503572009-01-31 00:06:58 +0000590X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
591 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000592 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
593 // classified recursively so that always two fields are
594 // considered. The resulting class is calculated according to
595 // the classes of the fields in the eightbyte:
596 //
597 // (a) If both classes are equal, this is the resulting class.
598 //
599 // (b) If one of the classes is NO_CLASS, the resulting class is
600 // the other class.
601 //
602 // (c) If one of the classes is MEMORY, the result is the MEMORY
603 // class.
604 //
605 // (d) If one of the classes is INTEGER, the result is the
606 // INTEGER.
607 //
608 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
609 // MEMORY is used as class.
610 //
611 // (f) Otherwise class SSE is used.
Daniel Dunbar100f4022009-03-06 17:50:25 +0000612
613 // Accum should never be memory (we should have returned) or
614 // ComplexX87 (because this cannot be passed in a structure).
615 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbarc4503572009-01-31 00:06:58 +0000616 "Invalid accumulated classification during merge.");
617 if (Accum == Field || Field == NoClass)
618 return Accum;
619 else if (Field == Memory)
620 return Memory;
621 else if (Accum == NoClass)
622 return Field;
623 else if (Accum == Integer || Field == Integer)
624 return Integer;
625 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
626 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000627 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000628 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000629}
630
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000631void X86_64ABIInfo::classify(QualType Ty,
632 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000633 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000634 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000635 // FIXME: This code can be simplified by introducing a simple value
636 // class for Class pairs with appropriate constructor methods for
637 // the various situations.
638
Daniel Dunbare28099b2009-02-22 04:48:22 +0000639 // FIXME: Some of the split computations are wrong; unaligned
640 // vectors shouldn't be passed in registers for example, so there is
641 // no chance they can straddle an eightbyte. Verify & simplify.
642
Daniel Dunbarc4503572009-01-31 00:06:58 +0000643 Lo = Hi = NoClass;
644
645 Class &Current = OffsetBase < 64 ? Lo : Hi;
646 Current = Memory;
647
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000648 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
649 BuiltinType::Kind k = BT->getKind();
650
Daniel Dunbar11434922009-01-26 21:26:08 +0000651 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000652 Current = NoClass;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000653 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattnerae69e002009-04-30 06:22:07 +0000654 Lo = Integer;
655 Hi = Integer;
Daniel Dunbar11434922009-01-26 21:26:08 +0000656 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000657 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000658 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000659 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000660 } else if (k == BuiltinType::LongDouble) {
661 Lo = X87;
662 Hi = X87Up;
663 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000664 // FIXME: _Decimal32 and _Decimal64 are SSE.
665 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson708762b2009-02-26 17:31:15 +0000666 } else if (const EnumType *ET = Ty->getAsEnumType()) {
667 // Classify the underlying integer type.
668 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbar89588912009-02-26 20:52:22 +0000669 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000670 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000671 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000672 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbare28099b2009-02-22 04:48:22 +0000673 if (Size == 32) {
674 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
675 // float> as integer.
676 Current = Integer;
677
678 // If this type crosses an eightbyte boundary, it should be
679 // split.
680 uint64_t EB_Real = (OffsetBase) / 64;
681 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
682 if (EB_Real != EB_Imag)
683 Hi = Lo;
684 } else if (Size == 64) {
Daniel Dunbar0af99292009-02-22 04:16:10 +0000685 // gcc passes <1 x double> in memory. :(
686 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000687 return;
Daniel Dunbar0af99292009-02-22 04:16:10 +0000688
689 // gcc passes <1 x long long> as INTEGER.
690 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
691 Current = Integer;
692 else
693 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000694
695 // If this type crosses an eightbyte boundary, it should be
696 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000697 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000698 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000699 } else if (Size == 128) {
700 Lo = SSE;
701 Hi = SSEUp;
702 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000703 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000704 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000705
Daniel Dunbare33edf12009-01-30 18:40:10 +0000706 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar0af99292009-02-22 04:16:10 +0000707 if (ET->isIntegralType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000708 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000709 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000710 else if (Size <= 128)
711 Lo = Hi = Integer;
712 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000713 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000714 else if (ET == Context.DoubleTy)
715 Lo = Hi = SSE;
716 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000717 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000718
719 // If this complex type crosses an eightbyte boundary then it
720 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000721 uint64_t EB_Real = (OffsetBase) / 64;
722 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000723 if (Hi == NoClass && EB_Real != EB_Imag)
724 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000725 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
726 // Arrays are treated like structures.
727
728 uint64_t Size = Context.getTypeSize(Ty);
729
730 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
731 // than two eightbytes, ..., it has class MEMORY.
732 if (Size > 128)
733 return;
734
735 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
736 // fields, it has class MEMORY.
737 //
738 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000739 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000740 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000741
742 // Otherwise implement simplified merge. We could be smarter about
743 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000744 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000745 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
746 uint64_t ArraySize = AT->getSize().getZExtValue();
747 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
748 Class FieldLo, FieldHi;
749 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000750 Lo = merge(Lo, FieldLo);
751 Hi = merge(Hi, FieldHi);
752 if (Lo == Memory || Hi == Memory)
753 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000754 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000755
756 // Do post merger cleanup (see below). Only case we worry about is Memory.
757 if (Hi == Memory)
758 Lo = Memory;
759 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000760 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000761 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000762
763 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
764 // than two eightbytes, ..., it has class MEMORY.
765 if (Size > 128)
766 return;
767
768 const RecordDecl *RD = RT->getDecl();
769
770 // Assume variable sized types are passed in memory.
771 if (RD->hasFlexibleArrayMember())
772 return;
773
774 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
775
776 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000777 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000778 unsigned idx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000779 for (RecordDecl::field_iterator i = RD->field_begin(Context),
780 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000781 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbardd81d442009-02-17 02:45:44 +0000782 bool BitField = i->isBitField();
Daniel Dunbar99037e52009-01-29 08:13:58 +0000783
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000784 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
785 // fields, it has class MEMORY.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000786 //
Daniel Dunbar8e034442009-04-27 18:31:32 +0000787 // Note, skip this test for bit-fields, see below.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000788 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar99037e52009-01-29 08:13:58 +0000789 Lo = Memory;
790 return;
791 }
792
Daniel Dunbar99037e52009-01-29 08:13:58 +0000793 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000794 //
795 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
796 // exceeds a single eightbyte, each is classified
797 // separately. Each eightbyte gets initialized to class
798 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000799 Class FieldLo, FieldHi;
Daniel Dunbardd81d442009-02-17 02:45:44 +0000800
Daniel Dunbar8e034442009-04-27 18:31:32 +0000801 // Bit-fields require special handling, they do not force the
Daniel Dunbardd81d442009-02-17 02:45:44 +0000802 // structure to be passed in memory even if unaligned, and
803 // therefore they can straddle an eightbyte.
804 if (BitField) {
Daniel Dunbar8236bf12009-05-08 22:26:44 +0000805 // Ignore padding bit-fields.
806 if (i->isUnnamedBitfield())
807 continue;
808
Daniel Dunbardd81d442009-02-17 02:45:44 +0000809 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman9a901bb2009-04-26 19:19:15 +0000810 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbardd81d442009-02-17 02:45:44 +0000811
812 uint64_t EB_Lo = Offset / 64;
813 uint64_t EB_Hi = (Offset + Size - 1) / 64;
814 FieldLo = FieldHi = NoClass;
815 if (EB_Lo) {
816 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
817 FieldLo = NoClass;
818 FieldHi = Integer;
819 } else {
820 FieldLo = Integer;
821 FieldHi = EB_Hi ? Integer : NoClass;
822 }
823 } else
824 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000825 Lo = merge(Lo, FieldLo);
826 Hi = merge(Hi, FieldHi);
827 if (Lo == Memory || Hi == Memory)
828 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000829 }
830
831 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
832 //
833 // (a) If one of the classes is MEMORY, the whole argument is
834 // passed in memory.
835 //
836 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
837
838 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000839 // the merge (just bail).
840 //
841 // The second condition occurs in the case of unions; for example
842 // union { _Complex double; unsigned; }.
843 if (Hi == Memory)
844 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000845 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000846 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000847 }
848}
849
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000850ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
851 const llvm::Type *CoerceTo,
852 ASTContext &Context) const {
853 if (CoerceTo == llvm::Type::Int64Ty) {
854 // Integer and pointer types will end up in a general purpose
855 // register.
Daniel Dunbar0af99292009-02-22 04:16:10 +0000856 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000857 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000858
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000859 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000860 // FIXME: It would probably be better to make CGFunctionInfo only
861 // map using canonical types than to canonize here.
862 QualType CTy = Context.getCanonicalType(Ty);
863
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000864 // Float and double end up in a single SSE reg.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000865 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000866 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000867
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000868 }
869
870 return ABIArgInfo::getCoerce(CoerceTo);
871}
Daniel Dunbarc4503572009-01-31 00:06:58 +0000872
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000873ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
874 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000875 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
876 // classification algorithm.
877 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000878 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000879
Daniel Dunbarc4503572009-01-31 00:06:58 +0000880 // Check some invariants.
881 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
882 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
883 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
884
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000885 const llvm::Type *ResType = 0;
886 switch (Lo) {
887 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000888 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000889
890 case SSEUp:
891 case X87Up:
892 assert(0 && "Invalid classification for lo word.");
893
Daniel Dunbarc4503572009-01-31 00:06:58 +0000894 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000895 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000896 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000897 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000898
899 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
900 // available register of the sequence %rax, %rdx is used.
901 case Integer:
902 ResType = llvm::Type::Int64Ty; break;
903
904 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
905 // available SSE register of the sequence %xmm0, %xmm1 is used.
906 case SSE:
907 ResType = llvm::Type::DoubleTy; break;
908
909 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
910 // returned on the X87 stack in %st0 as 80-bit x87 number.
911 case X87:
912 ResType = llvm::Type::X86_FP80Ty; break;
913
Daniel Dunbarc4503572009-01-31 00:06:58 +0000914 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
915 // part of the value is returned in %st0 and the imaginary part in
916 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000917 case ComplexX87:
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000918 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar3e030b42009-02-18 03:44:19 +0000919 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
920 llvm::Type::X86_FP80Ty,
921 NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000922 break;
923 }
924
925 switch (Hi) {
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000926 // Memory was handled previously and X87 should
927 // never occur as a hi class.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000928 case Memory:
929 case X87:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000930 assert(0 && "Invalid classification for hi word.");
931
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000932 case ComplexX87: // Previously handled.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000933 case NoClass: break;
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000934
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000935 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000936 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
937 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000938 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000939 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
940 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000941
942 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
943 // is passed in the upper half of the last used SSE register.
944 //
945 // SSEUP should always be preceeded by SSE, just widen.
946 case SSEUp:
947 assert(Lo == SSE && "Unexpected SSEUp classification.");
948 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
949 break;
950
951 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000952 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000953 case X87Up:
Daniel Dunbar100f4022009-03-06 17:50:25 +0000954 // If X87Up is preceeded by X87, we don't need to do
955 // anything. However, in some cases with unions it may not be
956 // preceeded by X87. In such situations we follow gcc and pass the
957 // extra bits in an SSE reg.
958 if (Lo != X87)
959 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000960 break;
961 }
962
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000963 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000964}
965
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000966ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000967 unsigned &neededInt,
968 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000969 X86_64ABIInfo::Class Lo, Hi;
970 classify(Ty, Context, 0, Lo, Hi);
971
972 // Check some invariants.
973 // FIXME: Enforce these by construction.
974 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
975 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
976 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
977
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000978 neededInt = 0;
979 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000980 const llvm::Type *ResType = 0;
981 switch (Lo) {
982 case NoClass:
983 return ABIArgInfo::getIgnore();
984
985 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
986 // on the stack.
987 case Memory:
988
989 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
990 // COMPLEX_X87, it is passed in memory.
991 case X87:
992 case ComplexX87:
Daniel Dunbar245f5532009-02-22 08:17:51 +0000993 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000994
995 case SSEUp:
996 case X87Up:
997 assert(0 && "Invalid classification for lo word.");
998
999 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1000 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1001 // and %r9 is used.
1002 case Integer:
1003 ++neededInt;
1004 ResType = llvm::Type::Int64Ty;
1005 break;
1006
1007 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1008 // available SSE register is used, the registers are taken in the
1009 // order from %xmm0 to %xmm7.
1010 case SSE:
1011 ++neededSSE;
1012 ResType = llvm::Type::DoubleTy;
1013 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001014 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001015
1016 switch (Hi) {
1017 // Memory was handled previously, ComplexX87 and X87 should
1018 // never occur as hi classes, and X87Up must be preceed by X87,
1019 // which is passed in memory.
1020 case Memory:
1021 case X87:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001022 case ComplexX87:
1023 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar100f4022009-03-06 17:50:25 +00001024 break;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001025
1026 case NoClass: break;
1027 case Integer:
1028 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1029 ++neededInt;
1030 break;
Daniel Dunbar100f4022009-03-06 17:50:25 +00001031
1032 // X87Up generally doesn't occur here (long double is passed in
1033 // memory), except in situations involving unions.
1034 case X87Up:
1035 case SSE:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001036 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1037 ++neededSSE;
1038 break;
1039
1040 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1041 // eightbyte is passed in the upper half of the last used SSE
1042 // register.
1043 case SSEUp:
1044 assert(Lo == SSE && "Unexpected SSEUp classification.");
1045 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1046 break;
1047 }
1048
Daniel Dunbar644f4c32009-02-14 02:09:24 +00001049 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001050}
1051
1052void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1053 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1054
1055 // Keep track of the number of assigned registers.
1056 unsigned freeIntRegs = 6, freeSSERegs = 8;
1057
1058 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1059 // get assigned (in left-to-right order) for passing as follows...
1060 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001061 it != ie; ++it) {
1062 unsigned neededInt, neededSSE;
1063 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1064
1065 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1066 // eightbyte of an argument, the whole argument is passed on the
1067 // stack. If registers have already been assigned for some
1068 // eightbytes of such an argument, the assignments get reverted.
1069 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1070 freeIntRegs -= neededInt;
1071 freeSSERegs -= neededSSE;
1072 } else {
Daniel Dunbar245f5532009-02-22 08:17:51 +00001073 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001074 }
1075 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001076}
1077
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001078static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1079 QualType Ty,
1080 CodeGenFunction &CGF) {
1081 llvm::Value *overflow_arg_area_p =
1082 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1083 llvm::Value *overflow_arg_area =
1084 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1085
1086 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1087 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001088 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001089 if (Align > 8) {
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001090 // Note that we follow the ABI & gcc here, even though the type
1091 // could in theory have an alignment greater than 16. This case
1092 // shouldn't ever matter in practice.
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001093
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001094 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1095 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1096 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1097 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1098 llvm::Type::Int64Ty);
1099 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1100 overflow_arg_area =
1101 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1102 overflow_arg_area->getType(),
1103 "overflow_arg_area.align");
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001104 }
1105
1106 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1107 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1108 llvm::Value *Res =
1109 CGF.Builder.CreateBitCast(overflow_arg_area,
1110 llvm::PointerType::getUnqual(LTy));
1111
1112 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1113 // l->overflow_arg_area + sizeof(type).
1114 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1115 // an 8 byte boundary.
1116
1117 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1118 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1119 (SizeInBytes + 7) & ~7);
1120 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1121 "overflow_arg_area.next");
1122 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1123
1124 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1125 return Res;
1126}
1127
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001128llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1129 CodeGenFunction &CGF) const {
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001130 // Assume that va_list type is correct; should be pointer to LLVM type:
1131 // struct {
1132 // i32 gp_offset;
1133 // i32 fp_offset;
1134 // i8* overflow_arg_area;
1135 // i8* reg_save_area;
1136 // };
1137 unsigned neededInt, neededSSE;
1138 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1139 neededInt, neededSSE);
1140
1141 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1142 // in the registers. If not go to step 7.
1143 if (!neededInt && !neededSSE)
1144 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1145
1146 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1147 // general purpose registers needed to pass type and num_fp to hold
1148 // the number of floating point registers needed.
1149
1150 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1151 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1152 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1153 //
1154 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1155 // register save space).
1156
1157 llvm::Value *InRegs = 0;
1158 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1159 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1160 if (neededInt) {
1161 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1162 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1163 InRegs =
1164 CGF.Builder.CreateICmpULE(gp_offset,
1165 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1166 48 - neededInt * 8),
1167 "fits_in_gp");
1168 }
1169
1170 if (neededSSE) {
1171 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1172 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1173 llvm::Value *FitsInFP =
1174 CGF.Builder.CreateICmpULE(fp_offset,
1175 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar90dafa12009-02-18 22:19:44 +00001176 176 - neededSSE * 16),
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001177 "fits_in_fp");
Daniel Dunbarf2313462009-02-18 22:05:01 +00001178 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001179 }
1180
1181 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1182 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1183 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1184 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1185
1186 // Emit code to load the value if it was passed in registers.
1187
1188 CGF.EmitBlock(InRegBlock);
1189
1190 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1191 // an offset of l->gp_offset and/or l->fp_offset. This may require
1192 // copying to a temporary location in case the parameter is passed
1193 // in different register classes or requires an alignment greater
1194 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001195 //
1196 // FIXME: This really results in shameful code when we end up
1197 // needing to collect arguments from different places; often what
1198 // should result in a simple assembling of a structure from
1199 // scattered addresses has many more loads than necessary. Can we
1200 // clean this up?
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001201 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1202 llvm::Value *RegAddr =
1203 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1204 "reg_save_area");
1205 if (neededInt && neededSSE) {
Daniel Dunbar55e5d892009-02-13 17:46:31 +00001206 // FIXME: Cleanup.
1207 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1208 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1209 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1210 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1211 const llvm::Type *TyLo = ST->getElementType(0);
1212 const llvm::Type *TyHi = ST->getElementType(1);
1213 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1214 "Unexpected ABI info for mixed regs");
1215 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1216 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1217 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1218 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1219 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1220 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1221 llvm::Value *V =
1222 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1223 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1224 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1225 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1226
1227 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001228 } else if (neededInt) {
1229 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1230 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1231 llvm::PointerType::getUnqual(LTy));
1232 } else {
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001233 if (neededSSE == 1) {
1234 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1235 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1236 llvm::PointerType::getUnqual(LTy));
1237 } else {
1238 assert(neededSSE == 2 && "Invalid number of needed registers!");
1239 // SSE registers are spaced 16 bytes apart in the register save
1240 // area, we need to collect the two eightbytes together.
1241 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1242 llvm::Value *RegAddrHi =
1243 CGF.Builder.CreateGEP(RegAddrLo,
1244 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1245 const llvm::Type *DblPtrTy =
1246 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1247 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1248 llvm::Type::DoubleTy,
1249 NULL);
1250 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1251 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1252 DblPtrTy));
1253 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1254 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1255 DblPtrTy));
1256 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1257 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1258 llvm::PointerType::getUnqual(LTy));
1259 }
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001260 }
1261
1262 // AMD64-ABI 3.5.7p5: Step 5. Set:
1263 // l->gp_offset = l->gp_offset + num_gp * 8
1264 // l->fp_offset = l->fp_offset + num_fp * 16.
1265 if (neededInt) {
1266 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1267 neededInt * 8);
1268 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1269 gp_offset_p);
1270 }
1271 if (neededSSE) {
1272 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1273 neededSSE * 16);
1274 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1275 fp_offset_p);
1276 }
1277 CGF.EmitBranch(ContBlock);
1278
1279 // Emit code to load the value if it was passed in memory.
1280
1281 CGF.EmitBlock(InMemBlock);
1282 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1283
1284 // Return the appropriate result.
1285
1286 CGF.EmitBlock(ContBlock);
1287 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1288 "vaarg.addr");
1289 ResAddr->reserveOperandSpace(2);
1290 ResAddr->addIncoming(RegAddr, InRegBlock);
1291 ResAddr->addIncoming(MemAddr, InMemBlock);
1292
1293 return ResAddr;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001294}
1295
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001296// ABI Info for PIC16
1297class PIC16ABIInfo : public ABIInfo {
1298 ABIArgInfo classifyReturnType(QualType RetTy,
1299 ASTContext &Context) const;
1300
1301 ABIArgInfo classifyArgumentType(QualType RetTy,
1302 ASTContext &Context) const;
1303
1304 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1305 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1306 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1307 it != ie; ++it)
1308 it->info = classifyArgumentType(it->type, Context);
1309 }
1310
1311 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1312 CodeGenFunction &CGF) const;
1313
1314};
1315
1316ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1317 ASTContext &Context) const {
1318 if (RetTy->isVoidType()) {
1319 return ABIArgInfo::getIgnore();
1320 } else {
1321 return ABIArgInfo::getDirect();
1322 }
1323}
1324
1325ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1326 ASTContext &Context) const {
1327 return ABIArgInfo::getDirect();
1328}
1329
1330llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1331 CodeGenFunction &CGF) const {
1332 return 0;
1333}
1334
Eli Friedmana027ea92009-03-29 00:15:25 +00001335class ARMABIInfo : public ABIInfo {
1336 ABIArgInfo classifyReturnType(QualType RetTy,
1337 ASTContext &Context) const;
1338
1339 ABIArgInfo classifyArgumentType(QualType RetTy,
1340 ASTContext &Context) const;
1341
1342 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1343
1344 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1345 CodeGenFunction &CGF) const;
1346};
1347
1348void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1349 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1350 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1351 it != ie; ++it) {
1352 it->info = classifyArgumentType(it->type, Context);
1353 }
1354}
1355
1356ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1357 ASTContext &Context) const {
1358 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1359 return ABIArgInfo::getDirect();
1360 }
1361 // FIXME: This is kind of nasty... but there isn't much choice
1362 // because the ARM backend doesn't support byval.
1363 // FIXME: This doesn't handle alignment > 64 bits.
1364 const llvm::Type* ElemTy;
1365 unsigned SizeRegs;
1366 if (Context.getTypeAlign(Ty) > 32) {
1367 ElemTy = llvm::Type::Int64Ty;
1368 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1369 } else {
1370 ElemTy = llvm::Type::Int32Ty;
1371 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1372 }
1373 std::vector<const llvm::Type*> LLVMFields;
1374 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1375 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1376 return ABIArgInfo::getCoerce(STy);
1377}
1378
1379ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1380 ASTContext &Context) const {
1381 if (RetTy->isVoidType()) {
1382 return ABIArgInfo::getIgnore();
1383 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1384 // Aggregates <= 4 bytes are returned in r0; other aggregates
1385 // are returned indirectly.
1386 uint64_t Size = Context.getTypeSize(RetTy);
1387 if (Size <= 32)
1388 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1389 return ABIArgInfo::getIndirect(0);
1390 } else {
1391 return ABIArgInfo::getDirect();
1392 }
1393}
1394
1395llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1396 CodeGenFunction &CGF) const {
1397 // FIXME: Need to handle alignment
1398 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1399 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1400
1401 CGBuilderTy &Builder = CGF.Builder;
1402 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1403 "ap");
1404 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1405 llvm::Type *PTy =
1406 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1407 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1408
1409 uint64_t Offset =
1410 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1411 llvm::Value *NextAddr =
1412 Builder.CreateGEP(Addr,
1413 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1414 "ap.next");
1415 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1416
1417 return AddrTyped;
1418}
1419
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001420ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001421 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001422 if (RetTy->isVoidType()) {
1423 return ABIArgInfo::getIgnore();
1424 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001425 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001426 } else {
1427 return ABIArgInfo::getDirect();
1428 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001429}
1430
1431ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001432 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001433 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001434 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001435 } else {
1436 return ABIArgInfo::getDirect();
1437 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001438}
1439
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001440llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1441 CodeGenFunction &CGF) const {
1442 return 0;
1443}
1444
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001445const ABIInfo &CodeGenTypes::getABIInfo() const {
1446 if (TheABIInfo)
1447 return *TheABIInfo;
1448
1449 // For now we just cache this in the CodeGenTypes and don't bother
1450 // to free it.
1451 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1452 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman9fd58e82009-03-23 23:26:24 +00001453 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001454 switch (getContext().Target.getPointerWidth(0)) {
1455 case 32:
Douglas Gregor6ab35242009-04-09 21:40:53 +00001456 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001457 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +00001458 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001459 }
Eli Friedmana027ea92009-03-29 00:15:25 +00001460 } else if (strcmp(TargetPrefix, "arm") == 0) {
1461 // FIXME: Support for OABI?
1462 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001463 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1464 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001465 }
1466
1467 return *(TheABIInfo = new DefaultABIInfo);
1468}
1469
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001470/***/
1471
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001472CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1473 const llvm::SmallVector<QualType, 16> &ArgTys) {
1474 NumArgs = ArgTys.size();
1475 Args = new ArgInfo[1 + NumArgs];
1476 Args[0].type = ResTy;
1477 for (unsigned i = 0; i < NumArgs; ++i)
1478 Args[1 + i].type = ArgTys[i];
1479}
1480
1481/***/
1482
Daniel Dunbar56273772008-09-17 00:51:38 +00001483void CodeGenTypes::GetExpandedTypes(QualType Ty,
1484 std::vector<const llvm::Type*> &ArgTys) {
1485 const RecordType *RT = Ty->getAsStructureType();
1486 assert(RT && "Can only expand structure types.");
1487 const RecordDecl *RD = RT->getDecl();
1488 assert(!RD->hasFlexibleArrayMember() &&
1489 "Cannot expand structure with flexible array.");
1490
Douglas Gregor6ab35242009-04-09 21:40:53 +00001491 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1492 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001493 const FieldDecl *FD = *i;
1494 assert(!FD->isBitField() &&
1495 "Cannot expand structure with bit-field members.");
1496
1497 QualType FT = FD->getType();
1498 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1499 GetExpandedTypes(FT, ArgTys);
1500 } else {
1501 ArgTys.push_back(ConvertType(FT));
1502 }
1503 }
1504}
1505
1506llvm::Function::arg_iterator
1507CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1508 llvm::Function::arg_iterator AI) {
1509 const RecordType *RT = Ty->getAsStructureType();
1510 assert(RT && "Can only expand structure types.");
1511
1512 RecordDecl *RD = RT->getDecl();
1513 assert(LV.isSimple() &&
1514 "Unexpected non-simple lvalue during struct expansion.");
1515 llvm::Value *Addr = LV.getAddress();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001516 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1517 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001518 FieldDecl *FD = *i;
1519 QualType FT = FD->getType();
1520
1521 // FIXME: What are the right qualifiers here?
1522 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1523 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1524 AI = ExpandTypeFromArgs(FT, LV, AI);
1525 } else {
1526 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1527 ++AI;
1528 }
1529 }
1530
1531 return AI;
1532}
1533
1534void
1535CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1536 llvm::SmallVector<llvm::Value*, 16> &Args) {
1537 const RecordType *RT = Ty->getAsStructureType();
1538 assert(RT && "Can only expand structure types.");
1539
1540 RecordDecl *RD = RT->getDecl();
1541 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1542 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001543 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1544 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001545 FieldDecl *FD = *i;
1546 QualType FT = FD->getType();
1547
1548 // FIXME: What are the right qualifiers here?
1549 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1550 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1551 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1552 } else {
1553 RValue RV = EmitLoadOfLValue(LV, FT);
1554 assert(RV.isScalar() &&
1555 "Unexpected non-scalar rvalue during struct expansion.");
1556 Args.push_back(RV.getScalarVal());
1557 }
1558 }
1559}
1560
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001561/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1562/// a pointer to an object of type \arg Ty.
1563///
1564/// This safely handles the case when the src type is smaller than the
1565/// destination type; in this situation the values of bits which not
1566/// present in the src are undefined.
1567static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1568 const llvm::Type *Ty,
1569 CodeGenFunction &CGF) {
1570 const llvm::Type *SrcTy =
1571 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +00001572 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1573 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001574
Daniel Dunbarb225be42009-02-03 05:59:18 +00001575 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001576 if (SrcSize == DstSize) {
1577 llvm::Value *Casted =
1578 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001579 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1580 // FIXME: Use better alignment / avoid requiring aligned load.
1581 Load->setAlignment(1);
1582 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001583 } else {
1584 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1585
1586 // Otherwise do coercion through memory. This is stupid, but
1587 // simple.
1588 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1589 llvm::Value *Casted =
1590 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001591 llvm::StoreInst *Store =
1592 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1593 // FIXME: Use better alignment / avoid requiring aligned store.
1594 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001595 return CGF.Builder.CreateLoad(Tmp);
1596 }
1597}
1598
1599/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1600/// where the source and destination may have different types.
1601///
1602/// This safely handles the case when the src type is larger than the
1603/// destination type; the upper bits of the src will be lost.
1604static void CreateCoercedStore(llvm::Value *Src,
1605 llvm::Value *DstPtr,
1606 CodeGenFunction &CGF) {
1607 const llvm::Type *SrcTy = Src->getType();
1608 const llvm::Type *DstTy =
1609 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1610
Duncan Sands9408c452009-05-09 07:08:47 +00001611 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1612 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001613
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001614 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001615 if (SrcSize == DstSize) {
1616 llvm::Value *Casted =
1617 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001618 // FIXME: Use better alignment / avoid requiring aligned store.
1619 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001620 } else {
1621 assert(SrcSize > DstSize && "Coercion is missing bits!");
1622
1623 // Otherwise do coercion through memory. This is stupid, but
1624 // simple.
1625 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1626 CGF.Builder.CreateStore(Src, Tmp);
1627 llvm::Value *Casted =
1628 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001629 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1630 // FIXME: Use better alignment / avoid requiring aligned load.
1631 Load->setAlignment(1);
1632 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001633 }
1634}
1635
Daniel Dunbar56273772008-09-17 00:51:38 +00001636/***/
1637
Daniel Dunbar88b53962009-02-02 22:03:45 +00001638bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001639 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001640}
1641
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001642const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001643CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001644 std::vector<const llvm::Type*> ArgTys;
1645
1646 const llvm::Type *ResultType = 0;
1647
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001648 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001649 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001650 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001651 case ABIArgInfo::Expand:
1652 assert(0 && "Invalid ABI kind for return argument");
1653
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001654 case ABIArgInfo::Direct:
1655 ResultType = ConvertType(RetTy);
1656 break;
1657
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001658 case ABIArgInfo::Indirect: {
1659 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001660 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001661 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001662 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1663 break;
1664 }
1665
Daniel Dunbar11434922009-01-26 21:26:08 +00001666 case ABIArgInfo::Ignore:
1667 ResultType = llvm::Type::VoidTy;
1668 break;
1669
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001670 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001671 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001672 break;
1673 }
1674
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001675 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1676 ie = FI.arg_end(); it != ie; ++it) {
1677 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001678
1679 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001680 case ABIArgInfo::Ignore:
1681 break;
1682
Daniel Dunbar56273772008-09-17 00:51:38 +00001683 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001684 ArgTys.push_back(AI.getCoerceToType());
1685 break;
1686
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001687 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001688 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001689 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1690 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001691 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001692 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001693
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001694 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001695 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001696 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001697
1698 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001699 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001700 break;
1701 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001702 }
1703
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001704 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001705}
1706
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001707void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001708 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001709 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001710 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001711 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001712
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001713 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001714 if (TargetDecl) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001715 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001716 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001717 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001718 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001719 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001720 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001721 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +00001722 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001723 }
1724
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001725 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001726 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001727 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001728 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001729 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001730 if (RetTy->isPromotableIntegerType()) {
1731 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001732 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001733 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001734 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001735 }
1736 }
1737 break;
1738
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001739 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001740 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001741 llvm::Attribute::StructRet |
1742 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001743 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001744 // sret disables readnone and readonly
1745 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1746 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001747 break;
1748
Daniel Dunbar11434922009-01-26 21:26:08 +00001749 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001750 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001751 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001752
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001753 case ABIArgInfo::Expand:
1754 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001755 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001756
Devang Patela2c69122008-09-26 22:53:57 +00001757 if (RetAttrs)
1758 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001759
1760 // FIXME: we need to honour command line settings also...
1761 // FIXME: RegParm should be reduced in case of nested functions and/or global
1762 // register variable.
1763 signed RegParm = 0;
1764 if (TargetDecl)
1765 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1766 RegParm = RegParmAttr->getNumParams();
1767
1768 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001769 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1770 ie = FI.arg_end(); it != ie; ++it) {
1771 QualType ParamType = it->type;
1772 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001773 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001774
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001775 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001776 case ABIArgInfo::Coerce:
1777 break;
1778
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001779 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001780 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001781 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001782 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001783 // byval disables readnone and readonly.
1784 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1785 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001786 break;
1787
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001788 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001789 if (ParamType->isPromotableIntegerType()) {
1790 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001791 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001792 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001793 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001794 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001795 }
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001796 if (RegParm > 0 &&
1797 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1798 RegParm -=
1799 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1800 if (RegParm >= 0)
1801 Attributes |= llvm::Attribute::InReg;
1802 }
1803 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001804 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001805
Daniel Dunbar11434922009-01-26 21:26:08 +00001806 case ABIArgInfo::Ignore:
1807 // Skip increment, no matching LLVM parameter.
1808 continue;
1809
Daniel Dunbar56273772008-09-17 00:51:38 +00001810 case ABIArgInfo::Expand: {
1811 std::vector<const llvm::Type*> Tys;
1812 // FIXME: This is rather inefficient. Do we ever actually need
1813 // to do anything here? The result should be just reconstructed
1814 // on the other side, so extension should be a non-issue.
1815 getTypes().GetExpandedTypes(ParamType, Tys);
1816 Index += Tys.size();
1817 continue;
1818 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001819 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001820
Devang Patel761d7f72008-09-25 21:02:23 +00001821 if (Attributes)
1822 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001823 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001824 }
Devang Patela2c69122008-09-26 22:53:57 +00001825 if (FuncAttrs)
1826 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001827}
1828
Daniel Dunbar88b53962009-02-02 22:03:45 +00001829void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1830 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001831 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001832 // FIXME: We no longer need the types from FunctionArgList; lift up
1833 // and simplify.
1834
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001835 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1836 llvm::Function::arg_iterator AI = Fn->arg_begin();
1837
1838 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001839 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001840 AI->setName("agg.result");
1841 ++AI;
1842 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001843
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001844 assert(FI.arg_size() == Args.size() &&
1845 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001846 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001847 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001848 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001849 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001850 QualType Ty = info_it->type;
1851 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001852
1853 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001854 case ABIArgInfo::Indirect: {
1855 llvm::Value* V = AI;
1856 if (hasAggregateLLVMType(Ty)) {
1857 // Do nothing, aggregates and complex variables are accessed by
1858 // reference.
1859 } else {
1860 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001861 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001862 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1863 // This must be a promotion, for something like
1864 // "void a(x) short x; {..."
1865 V = EmitScalarConversion(V, Ty, Arg->getType());
1866 }
1867 }
1868 EmitParmDecl(*Arg, V);
1869 break;
1870 }
1871
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001872 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001873 assert(AI != Fn->arg_end() && "Argument mismatch!");
1874 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001875 if (hasAggregateLLVMType(Ty)) {
1876 // Create a temporary alloca to hold the argument; the rest of
1877 // codegen expects to access aggregates & complex values by
1878 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001879 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001880 Builder.CreateStore(AI, V);
1881 } else {
1882 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1883 // This must be a promotion, for something like
1884 // "void a(x) short x; {..."
1885 V = EmitScalarConversion(V, Ty, Arg->getType());
1886 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001887 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001888 EmitParmDecl(*Arg, V);
1889 break;
1890 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001891
1892 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001893 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001894 // we need to create a temporary and reconstruct it from the
1895 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001896 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001897 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001898 (Name + ".addr").c_str());
1899 // FIXME: What are the right qualifiers here?
1900 llvm::Function::arg_iterator End =
1901 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1902 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001903
Daniel Dunbar56273772008-09-17 00:51:38 +00001904 // Name the arguments used in expansion and increment AI.
1905 unsigned Index = 0;
1906 for (; AI != End; ++AI, ++Index)
1907 AI->setName(Name + "." + llvm::utostr(Index));
1908 continue;
1909 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001910
1911 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001912 // Initialize the local variable appropriately.
1913 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001914 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001915 } else {
1916 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1917 }
1918
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001919 // Skip increment, no matching LLVM parameter.
1920 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001921
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001922 case ABIArgInfo::Coerce: {
1923 assert(AI != Fn->arg_end() && "Argument mismatch!");
1924 // FIXME: This is very wasteful; EmitParmDecl is just going to
1925 // drop the result in a new alloca anyway, so we could just
1926 // store into that directly if we broke the abstraction down
1927 // more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001928 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001929 CreateCoercedStore(AI, V, *this);
1930 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001931 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001932 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001933 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1934 // This must be a promotion, for something like
1935 // "void a(x) short x; {..."
1936 V = EmitScalarConversion(V, Ty, Arg->getType());
1937 }
1938 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001939 EmitParmDecl(*Arg, V);
1940 break;
1941 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001942 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001943
1944 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001945 }
1946 assert(AI == Fn->arg_end() && "Argument mismatch!");
1947}
1948
Daniel Dunbar88b53962009-02-02 22:03:45 +00001949void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001950 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001951 llvm::Value *RV = 0;
1952
1953 // Functions with no result always return void.
1954 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001955 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001956 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001957
1958 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001959 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001960 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001961 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1962 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1963 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1964 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1965 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001966 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1967 false);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001968 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001969 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001970
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001971 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001972 // The internal return value temp always will have
1973 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001974 RV = Builder.CreateLoad(ReturnValue);
1975 break;
1976
Daniel Dunbar11434922009-01-26 21:26:08 +00001977 case ABIArgInfo::Ignore:
1978 break;
1979
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001980 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001981 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001982 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001983
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001984 case ABIArgInfo::Expand:
1985 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001986 }
1987 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001988
1989 if (RV) {
1990 Builder.CreateRet(RV);
1991 } else {
1992 Builder.CreateRetVoid();
1993 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001994}
1995
Anders Carlsson0139bb92009-04-08 20:47:54 +00001996RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
1997 return EmitAnyExprToTemp(E);
1998}
1999
Daniel Dunbar88b53962009-02-02 22:03:45 +00002000RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
2001 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002002 const CallArgList &CallArgs,
2003 const Decl *TargetDecl) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00002004 // FIXME: We no longer need the types from CallArgs; lift up and
2005 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002006 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002007
2008 // Handle struct-return functions by passing a pointer to the
2009 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002010 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00002011 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00002012 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002013 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002014 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002015 }
2016
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00002017 assert(CallInfo.arg_size() == CallArgs.size() &&
2018 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00002019 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002020 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00002021 I != E; ++I, ++info_it) {
2022 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002023 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00002024
2025 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002026 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00002027 if (RV.isScalar() || RV.isComplex()) {
2028 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002029 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00002030 if (RV.isScalar())
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002031 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00002032 else
2033 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2034 } else {
2035 Args.push_back(RV.getAggregateAddr());
2036 }
2037 break;
2038
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002039 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00002040 if (RV.isScalar()) {
2041 Args.push_back(RV.getScalarVal());
2042 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002043 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2044 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2045 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2046 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00002047 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002048 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00002049 }
2050 break;
2051
Daniel Dunbar11434922009-01-26 21:26:08 +00002052 case ABIArgInfo::Ignore:
2053 break;
2054
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002055 case ABIArgInfo::Coerce: {
2056 // FIXME: Avoid the conversion through memory if possible.
2057 llvm::Value *SrcPtr;
2058 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00002059 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002060 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002061 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002062 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002063 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2064 } else
2065 SrcPtr = RV.getAggregateAddr();
2066 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2067 *this));
2068 break;
2069 }
2070
Daniel Dunbar56273772008-09-17 00:51:38 +00002071 case ABIArgInfo::Expand:
2072 ExpandTypeToArgs(I->second, RV, Args);
2073 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002074 }
2075 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002076
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002077 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel761d7f72008-09-25 21:02:23 +00002078 CodeGen::AttributeListType AttributeList;
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002079 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002080 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2081 AttributeList.end());
Daniel Dunbar725ad312009-01-31 02:19:00 +00002082
Daniel Dunbard14151d2009-03-02 04:32:35 +00002083 llvm::CallSite CS;
2084 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2085 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002086 } else {
2087 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbard14151d2009-03-02 04:32:35 +00002088 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2089 &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002090 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00002091 }
2092
Daniel Dunbard14151d2009-03-02 04:32:35 +00002093 CS.setAttributes(Attrs);
2094 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2095 CS.setCallingConv(F->getCallingConv());
2096
2097 // If the call doesn't return, finish the basic block and clear the
2098 // insertion point; this allows the rest of IRgen to discard
2099 // unreachable code.
2100 if (CS.doesNotReturn()) {
2101 Builder.CreateUnreachable();
2102 Builder.ClearInsertionPoint();
2103
2104 // FIXME: For now, emit a dummy basic block because expr
2105 // emitters in generally are not ready to handle emitting
2106 // expressions at unreachable points.
2107 EnsureInsertPoint();
2108
2109 // Return a reasonable RValue.
2110 return GetUndefRValue(RetTy);
2111 }
2112
2113 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner34030842009-03-22 00:32:22 +00002114 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002115 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002116
2117 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002118 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002119 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00002120 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00002121 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00002122 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00002123 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002124
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002125 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002126 if (RetTy->isAnyComplexType()) {
2127 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2128 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2129 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00002130 }
2131 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002132 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002133 Builder.CreateStore(CI, V);
2134 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002135 }
2136 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002137
Daniel Dunbar11434922009-01-26 21:26:08 +00002138 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00002139 // If we are ignoring an argument that had a result, make sure to
2140 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00002141 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00002142
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002143 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002144 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002145 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00002146 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00002147 if (RetTy->isAnyComplexType())
2148 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner34030842009-03-22 00:32:22 +00002149 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00002150 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002151 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002152 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002153
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002154 case ABIArgInfo::Expand:
2155 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002156 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002157
2158 assert(0 && "Unhandled ABIArgInfo::Kind");
2159 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002160}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00002161
2162/* VarArg handling */
2163
2164llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2165 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2166}