blob: 43db767158eccd803787850fc875f34176f968d7 [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 Carlsson5529b242009-04-06 18:05:26 +000056const
57CGFunctionInfo &CodeGenTypes::getFunctionInfo(const BlockPointerType *BPT) {
58 llvm::SmallVector<QualType, 16> ArgTys;
59 const FunctionProtoType *FTP =
60 BPT->getPointeeType()->getAsFunctionProtoType();
61
62 // Add the block pointer.
63 ArgTys.push_back(Context.getPointerType(Context.VoidTy));
64 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
65 ArgTys.push_back(FTP->getArgType(i));
66 return getFunctionInfo(FTP->getResultType(), ArgTys);
67}
68
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000069const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
70 llvm::SmallVector<QualType, 16> ArgTys;
71 // Add the 'this' pointer.
72 ArgTys.push_back(MD->getThisType(Context));
73
74 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType();
75 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
76 ArgTys.push_back(FTP->getArgType(i));
77 return getFunctionInfo(FTP->getResultType(), ArgTys);
78}
79
Daniel Dunbar541b63b2009-02-02 23:23:47 +000080const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000081 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
82 if (MD->isInstance())
83 return getFunctionInfo(MD);
84 }
85
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000086 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +000087 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar541b63b2009-02-02 23:23:47 +000088 return getFunctionInfo(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +000089 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000090}
91
Daniel Dunbar541b63b2009-02-02 23:23:47 +000092const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
93 llvm::SmallVector<QualType, 16> ArgTys;
94 ArgTys.push_back(MD->getSelfDecl()->getType());
95 ArgTys.push_back(Context.getObjCSelType());
96 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +000097 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000098 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000099 ArgTys.push_back((*i)->getType());
100 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000101}
102
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000103const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
104 const CallArgList &Args) {
105 // FIXME: Kill copy.
106 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +0000107 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
108 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000109 ArgTys.push_back(i->second);
110 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000111}
112
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000113const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
114 const FunctionArgList &Args) {
115 // FIXME: Kill copy.
116 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000117 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
118 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000119 ArgTys.push_back(i->second);
120 return getFunctionInfo(ResTy, ArgTys);
121}
122
123const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
124 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000125 // Lookup or create unique function info.
126 llvm::FoldingSetNodeID ID;
127 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
128
129 void *InsertPos = 0;
130 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
131 if (FI)
132 return *FI;
133
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000134 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000135 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000136 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000137
138 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000139 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000140
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000141 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000142}
143
144/***/
145
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000146ABIInfo::~ABIInfo() {}
147
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000148void ABIArgInfo::dump() const {
149 fprintf(stderr, "(ABIArgInfo Kind=");
150 switch (TheKind) {
151 case Direct:
152 fprintf(stderr, "Direct");
153 break;
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000154 case Ignore:
155 fprintf(stderr, "Ignore");
156 break;
157 case Coerce:
158 fprintf(stderr, "Coerce Type=");
159 getCoerceToType()->print(llvm::errs());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000160 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000161 case Indirect:
162 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000163 break;
164 case Expand:
165 fprintf(stderr, "Expand");
166 break;
167 }
168 fprintf(stderr, ")\n");
169}
170
171/***/
172
Daniel Dunbar5bde6f42009-03-31 19:01:39 +0000173/// isEmptyRecord - Return true iff a structure has no non-empty
Daniel Dunbar834af452008-09-17 21:22:33 +0000174/// members. Note that a structure with a flexible array member is not
175/// considered empty.
Daniel Dunbar5bde6f42009-03-31 19:01:39 +0000176static bool isEmptyRecord(QualType T) {
177 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar834af452008-09-17 21:22:33 +0000178 if (!RT)
179 return 0;
180 const RecordDecl *RD = RT->getDecl();
181 if (RD->hasFlexibleArrayMember())
182 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000183 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000184 e = RD->field_end(); i != e; ++i) {
185 const FieldDecl *FD = *i;
Daniel Dunbar5bde6f42009-03-31 19:01:39 +0000186 if (!isEmptyRecord(FD->getType()))
Daniel Dunbar834af452008-09-17 21:22:33 +0000187 return false;
188 }
189 return true;
190}
191
192/// isSingleElementStruct - Determine if a structure is a "single
193/// element struct", i.e. it has exactly one non-empty field or
194/// exactly one field which is itself a single element
195/// struct. Structures with flexible array members are never
196/// considered single element structs.
197///
198/// \return The field declaration for the single non-empty field, if
199/// it exists.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000200static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000201 const RecordType *RT = T->getAsStructureType();
202 if (!RT)
203 return 0;
204
205 const RecordDecl *RD = RT->getDecl();
206 if (RD->hasFlexibleArrayMember())
207 return 0;
208
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000209 const Type *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000210 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000211 e = RD->field_end(); i != e; ++i) {
212 const FieldDecl *FD = *i;
213 QualType FT = FD->getType();
214
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000215 // Treat single element arrays as the element
216 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
217 if (AT->getSize().getZExtValue() == 1)
218 FT = AT->getElementType();
219
Daniel Dunbar5bde6f42009-03-31 19:01:39 +0000220 if (isEmptyRecord(FT)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000221 // Ignore
222 } else if (Found) {
223 return 0;
224 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000225 Found = FT.getTypePtr();
Daniel Dunbar834af452008-09-17 21:22:33 +0000226 } else {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000227 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000228 if (!Found)
229 return 0;
230 }
231 }
232
233 return Found;
234}
235
236static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
237 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
238 return false;
239
240 uint64_t Size = Context.getTypeSize(Ty);
241 return Size == 32 || Size == 64;
242}
243
244static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
245 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000246 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000247 e = RD->field_end(); i != e; ++i) {
248 const FieldDecl *FD = *i;
249
250 if (!is32Or64BitBasicType(FD->getType(), Context))
251 return false;
252
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000253 // FIXME: Reject bitfields wholesale; there are two problems, we
254 // don't know how to expand them yet, and the predicate for
255 // telling if a bitfield still counts as "basic" is more
256 // complicated than what we were doing previously.
257 if (FD->isBitField())
258 return false;
Daniel Dunbar834af452008-09-17 21:22:33 +0000259 }
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000260
Daniel Dunbar834af452008-09-17 21:22:33 +0000261 return true;
262}
263
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000264namespace {
265/// DefaultABIInfo - The default implementation for ABI specific
266/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000267/// self-consistent and sensible LLVM IR generation, but does not
268/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000269class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000270 ABIArgInfo classifyReturnType(QualType RetTy,
271 ASTContext &Context) const;
272
273 ABIArgInfo classifyArgumentType(QualType RetTy,
274 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000275
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000276 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
277 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
278 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
279 it != ie; ++it)
280 it->info = classifyArgumentType(it->type, Context);
281 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000282
283 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
284 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000285};
286
287/// X86_32ABIInfo - The X86-32 ABI information.
288class X86_32ABIInfo : public ABIInfo {
Eli Friedman9fd58e82009-03-23 23:26:24 +0000289 bool IsDarwin;
290
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000291 static bool isRegisterSize(unsigned Size) {
292 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
293 }
294
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000295 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
296
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000297public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000298 ABIArgInfo classifyReturnType(QualType RetTy,
299 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000300
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000301 ABIArgInfo classifyArgumentType(QualType RetTy,
302 ASTContext &Context) const;
303
304 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
305 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
306 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
307 it != ie; ++it)
308 it->info = classifyArgumentType(it->type, Context);
309 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000310
311 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
312 CodeGenFunction &CGF) const;
Eli Friedman9fd58e82009-03-23 23:26:24 +0000313
314 X86_32ABIInfo(bool d) : ABIInfo(), IsDarwin(d) {}
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000315};
316}
317
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000318
319/// shouldReturnTypeInRegister - Determine if the given type should be
320/// passed in a register (for the Darwin ABI).
321bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
322 ASTContext &Context) {
323 uint64_t Size = Context.getTypeSize(Ty);
324
325 // Type must be register sized.
326 if (!isRegisterSize(Size))
327 return false;
328
329 if (Ty->isVectorType()) {
330 // 64- and 128- bit vectors inside structures are not returned in
331 // registers.
332 if (Size == 64 || Size == 128)
333 return false;
334
335 return true;
336 }
337
338 // If this is a builtin, pointer, or complex type, it is ok.
339 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
340 return true;
341
342 // Arrays are treated like records.
343 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
344 return shouldReturnTypeInRegister(AT->getElementType(), Context);
345
346 // Otherwise, it must be a record type.
347 const RecordType *RT = Ty->getAsRecordType();
348 if (!RT) return false;
349
350 // Structure types are passed in register if all fields would be
351 // passed in a register.
352 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
353 e = RT->getDecl()->field_end(); i != e; ++i) {
354 const FieldDecl *FD = *i;
355
356 // FIXME: Reject bitfields wholesale for now; this is incorrect.
357 if (FD->isBitField())
358 return false;
359
360 // Empty structures are ignored.
361 if (isEmptyRecord(FD->getType()))
362 continue;
363
364 // Check fields recursively.
365 if (!shouldReturnTypeInRegister(FD->getType(), Context))
366 return false;
367 }
368
369 return true;
370}
371
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000372ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
373 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000374 if (RetTy->isVoidType()) {
375 return ABIArgInfo::getIgnore();
Daniel Dunbar36043162009-04-01 06:13:08 +0000376 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
377 // On Darwin, some vectors are returned in registers.
378 if (IsDarwin) {
379 uint64_t Size = Context.getTypeSize(RetTy);
380
381 // 128-bit vectors are a special case; they are returned in
382 // registers and we need to make sure to pick a type the LLVM
383 // backend will like.
384 if (Size == 128)
385 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
386 2));
387
388 // Always return in register if it fits in a general purpose
389 // register, or if it is 64 bits and has a single element.
390 if ((Size == 8 || Size == 16 || Size == 32) ||
391 (Size == 64 && VT->getNumElements() == 1))
392 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
393
394 return ABIArgInfo::getIndirect(0);
395 }
396
397 return ABIArgInfo::getDirect();
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000398 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
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 Dunbar834af452008-09-17 21:22:33 +0000402 // Classify "single element" structs as their element type.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000403 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000404 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
405 // FIXME: This is gross, it would be nice if we could just
406 // pass back SeltTy and have clients deal with it. Is it worth
407 // supporting coerce to both LLVM and clang Types?
408 if (BT->isIntegerType()) {
409 uint64_t Size = Context.getTypeSize(SeltTy);
410 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
411 } else if (BT->getKind() == BuiltinType::Float) {
412 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
413 } else if (BT->getKind() == BuiltinType::Double) {
414 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
415 }
416 } else if (SeltTy->isPointerType()) {
417 // FIXME: It would be really nice if this could come out as
418 // the proper pointer type.
419 llvm::Type *PtrTy =
420 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
421 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000422 } else if (SeltTy->isVectorType()) {
423 // 64- and 128-bit vectors are never returned in a
424 // register when inside a structure.
425 uint64_t Size = Context.getTypeSize(RetTy);
426 if (Size == 64 || Size == 128)
427 return ABIArgInfo::getIndirect(0);
428
429 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000430 }
431 }
432
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000433 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000434 if (isRegisterSize(Size)) {
435 // Always return in register for unions for now.
436 // FIXME: This is wrong, but better than treating as a
437 // structure.
438 if (RetTy->isUnionType())
439 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
440
441 // Small structures which are register sized are generally returned
442 // in a register.
443 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context))
444 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
445 }
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000446
447 return ABIArgInfo::getIndirect(0);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000448 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000449 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000450 }
451}
452
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000453ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000454 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000455 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000456 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000457 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000458 if (const RecordType *RT = Ty->getAsStructureType())
459 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000460 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000461
Daniel Dunbar3170c932009-02-05 01:50:07 +0000462 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000463 uint64_t Size = Context.getTypeSize(Ty);
464 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000465 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000466
467 // Expand structs with size <= 128-bits which consist only of
468 // basic types (int, long long, float, double, xxx*). This is
469 // non-recursive and does not ignore empty fields.
470 if (const RecordType *RT = Ty->getAsStructureType()) {
471 if (Context.getTypeSize(Ty) <= 4*32 &&
472 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
473 return ABIArgInfo::getExpand();
474 }
475
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000476 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000477 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000478 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000479 }
480}
481
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000482llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
483 CodeGenFunction &CGF) const {
484 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
485 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
486
487 CGBuilderTy &Builder = CGF.Builder;
488 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
489 "ap");
490 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
491 llvm::Type *PTy =
492 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
493 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
494
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000495 uint64_t Offset =
496 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000497 llvm::Value *NextAddr =
498 Builder.CreateGEP(Addr,
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000499 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000500 "ap.next");
501 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
502
503 return AddrTyped;
504}
505
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000506namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000507/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000508class X86_64ABIInfo : public ABIInfo {
509 enum Class {
510 Integer = 0,
511 SSE,
512 SSEUp,
513 X87,
514 X87Up,
515 ComplexX87,
516 NoClass,
517 Memory
518 };
519
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000520 /// merge - Implement the X86_64 ABI merging algorithm.
521 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000522 /// Merge an accumulating classification \arg Accum with a field
523 /// classification \arg Field.
524 ///
525 /// \param Accum - The accumulating classification. This should
526 /// always be either NoClass or the result of a previous merge
527 /// call. In addition, this should never be Memory (the caller
528 /// should just return Memory for the aggregate).
529 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000530
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000531 /// classify - Determine the x86_64 register classes in which the
532 /// given type T should be passed.
533 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000534 /// \param Lo - The classification for the parts of the type
535 /// residing in the low word of the containing object.
536 ///
537 /// \param Hi - The classification for the parts of the type
538 /// residing in the high word of the containing object.
539 ///
540 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000541 /// containing object. Some parameters are classified different
542 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000543 ///
544 /// If a word is unused its result will be NoClass; if a type should
545 /// be passed in Memory then at least the classification of \arg Lo
546 /// will be Memory.
547 ///
548 /// The \arg Lo class will be NoClass iff the argument is ignored.
549 ///
550 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000551 /// also be ComplexX87.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000552 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000553 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000554
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000555 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
556 /// to coerce to, chose the best way to pass Ty in the same place
557 /// that \arg CoerceTo would be passed, but while keeping the
558 /// emitted code as simple as possible.
559 ///
560 /// FIXME: Note, this should be cleaned up to just take an
561 /// enumeration of all the ways we might want to pass things,
562 /// instead of constructing an LLVM type. This makes this code more
563 /// explicit, and it makes it clearer that we are also doing this
564 /// for correctness in the case of passing scalar types.
565 ABIArgInfo getCoerceResult(QualType Ty,
566 const llvm::Type *CoerceTo,
567 ASTContext &Context) const;
568
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000569 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000570 ASTContext &Context) const;
571
572 ABIArgInfo classifyArgumentType(QualType Ty,
573 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000574 unsigned &neededInt,
575 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000576
577public:
578 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000579
580 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
581 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000582};
583}
584
Daniel Dunbarc4503572009-01-31 00:06:58 +0000585X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
586 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000587 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
588 // classified recursively so that always two fields are
589 // considered. The resulting class is calculated according to
590 // the classes of the fields in the eightbyte:
591 //
592 // (a) If both classes are equal, this is the resulting class.
593 //
594 // (b) If one of the classes is NO_CLASS, the resulting class is
595 // the other class.
596 //
597 // (c) If one of the classes is MEMORY, the result is the MEMORY
598 // class.
599 //
600 // (d) If one of the classes is INTEGER, the result is the
601 // INTEGER.
602 //
603 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
604 // MEMORY is used as class.
605 //
606 // (f) Otherwise class SSE is used.
Daniel Dunbar100f4022009-03-06 17:50:25 +0000607
608 // Accum should never be memory (we should have returned) or
609 // ComplexX87 (because this cannot be passed in a structure).
610 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbarc4503572009-01-31 00:06:58 +0000611 "Invalid accumulated classification during merge.");
612 if (Accum == Field || Field == NoClass)
613 return Accum;
614 else if (Field == Memory)
615 return Memory;
616 else if (Accum == NoClass)
617 return Field;
618 else if (Accum == Integer || Field == Integer)
619 return Integer;
620 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
621 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000622 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000623 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000624}
625
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000626void X86_64ABIInfo::classify(QualType Ty,
627 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000628 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000629 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000630 // FIXME: This code can be simplified by introducing a simple value
631 // class for Class pairs with appropriate constructor methods for
632 // the various situations.
633
Daniel Dunbare28099b2009-02-22 04:48:22 +0000634 // FIXME: Some of the split computations are wrong; unaligned
635 // vectors shouldn't be passed in registers for example, so there is
636 // no chance they can straddle an eightbyte. Verify & simplify.
637
Daniel Dunbarc4503572009-01-31 00:06:58 +0000638 Lo = Hi = NoClass;
639
640 Class &Current = OffsetBase < 64 ? Lo : Hi;
641 Current = Memory;
642
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000643 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
644 BuiltinType::Kind k = BT->getKind();
645
Daniel Dunbar11434922009-01-26 21:26:08 +0000646 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000647 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000648 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000649 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000650 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000651 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000652 } else if (k == BuiltinType::LongDouble) {
653 Lo = X87;
654 Hi = X87Up;
655 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000656 // FIXME: _Decimal32 and _Decimal64 are SSE.
657 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000658 // FIXME: __int128 is (Integer, Integer).
Anders Carlsson708762b2009-02-26 17:31:15 +0000659 } else if (const EnumType *ET = Ty->getAsEnumType()) {
660 // Classify the underlying integer type.
661 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbar89588912009-02-26 20:52:22 +0000662 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000663 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000664 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000665 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbare28099b2009-02-22 04:48:22 +0000666 if (Size == 32) {
667 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
668 // float> as integer.
669 Current = Integer;
670
671 // If this type crosses an eightbyte boundary, it should be
672 // split.
673 uint64_t EB_Real = (OffsetBase) / 64;
674 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
675 if (EB_Real != EB_Imag)
676 Hi = Lo;
677 } else if (Size == 64) {
Daniel Dunbar0af99292009-02-22 04:16:10 +0000678 // gcc passes <1 x double> in memory. :(
679 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000680 return;
Daniel Dunbar0af99292009-02-22 04:16:10 +0000681
682 // gcc passes <1 x long long> as INTEGER.
683 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
684 Current = Integer;
685 else
686 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000687
688 // If this type crosses an eightbyte boundary, it should be
689 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000690 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000691 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000692 } else if (Size == 128) {
693 Lo = SSE;
694 Hi = SSEUp;
695 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000696 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000697 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000698
Daniel Dunbare33edf12009-01-30 18:40:10 +0000699 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar0af99292009-02-22 04:16:10 +0000700 if (ET->isIntegralType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000701 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000702 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000703 else if (Size <= 128)
704 Lo = Hi = Integer;
705 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000706 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000707 else if (ET == Context.DoubleTy)
708 Lo = Hi = SSE;
709 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000710 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000711
712 // If this complex type crosses an eightbyte boundary then it
713 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000714 uint64_t EB_Real = (OffsetBase) / 64;
715 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000716 if (Hi == NoClass && EB_Real != EB_Imag)
717 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000718 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
719 // Arrays are treated like structures.
720
721 uint64_t Size = Context.getTypeSize(Ty);
722
723 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
724 // than two eightbytes, ..., it has class MEMORY.
725 if (Size > 128)
726 return;
727
728 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
729 // fields, it has class MEMORY.
730 //
731 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000732 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000733 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000734
735 // Otherwise implement simplified merge. We could be smarter about
736 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000737 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000738 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
739 uint64_t ArraySize = AT->getSize().getZExtValue();
740 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
741 Class FieldLo, FieldHi;
742 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000743 Lo = merge(Lo, FieldLo);
744 Hi = merge(Hi, FieldHi);
745 if (Lo == Memory || Hi == Memory)
746 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000747 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000748
749 // Do post merger cleanup (see below). Only case we worry about is Memory.
750 if (Hi == Memory)
751 Lo = Memory;
752 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000753 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000754 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000755
756 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
757 // than two eightbytes, ..., it has class MEMORY.
758 if (Size > 128)
759 return;
760
761 const RecordDecl *RD = RT->getDecl();
762
763 // Assume variable sized types are passed in memory.
764 if (RD->hasFlexibleArrayMember())
765 return;
766
767 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
768
769 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000770 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000771 unsigned idx = 0;
772 for (RecordDecl::field_iterator i = RD->field_begin(),
773 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000774 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbardd81d442009-02-17 02:45:44 +0000775 bool BitField = i->isBitField();
Daniel Dunbar99037e52009-01-29 08:13:58 +0000776
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000777 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
778 // fields, it has class MEMORY.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000779 //
780 // Note, skip this test for bitfields, see below.
781 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar99037e52009-01-29 08:13:58 +0000782 Lo = Memory;
783 return;
784 }
785
Daniel Dunbar99037e52009-01-29 08:13:58 +0000786 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000787 //
788 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
789 // exceeds a single eightbyte, each is classified
790 // separately. Each eightbyte gets initialized to class
791 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000792 Class FieldLo, FieldHi;
Daniel Dunbardd81d442009-02-17 02:45:44 +0000793
794 // Bitfields require special handling, they do not force the
795 // structure to be passed in memory even if unaligned, and
796 // therefore they can straddle an eightbyte.
797 if (BitField) {
798 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
799 uint64_t Size =
800 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
801
802 uint64_t EB_Lo = Offset / 64;
803 uint64_t EB_Hi = (Offset + Size - 1) / 64;
804 FieldLo = FieldHi = NoClass;
805 if (EB_Lo) {
806 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
807 FieldLo = NoClass;
808 FieldHi = Integer;
809 } else {
810 FieldLo = Integer;
811 FieldHi = EB_Hi ? Integer : NoClass;
812 }
813 } else
814 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000815 Lo = merge(Lo, FieldLo);
816 Hi = merge(Hi, FieldHi);
817 if (Lo == Memory || Hi == Memory)
818 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000819 }
820
821 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
822 //
823 // (a) If one of the classes is MEMORY, the whole argument is
824 // passed in memory.
825 //
826 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
827
828 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000829 // the merge (just bail).
830 //
831 // The second condition occurs in the case of unions; for example
832 // union { _Complex double; unsigned; }.
833 if (Hi == Memory)
834 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000835 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000836 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000837 }
838}
839
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000840ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
841 const llvm::Type *CoerceTo,
842 ASTContext &Context) const {
843 if (CoerceTo == llvm::Type::Int64Ty) {
844 // Integer and pointer types will end up in a general purpose
845 // register.
Daniel Dunbar0af99292009-02-22 04:16:10 +0000846 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000847 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000848
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000849 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000850 // FIXME: It would probably be better to make CGFunctionInfo only
851 // map using canonical types than to canonize here.
852 QualType CTy = Context.getCanonicalType(Ty);
853
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000854 // Float and double end up in a single SSE reg.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000855 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000856 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000857
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000858 }
859
860 return ABIArgInfo::getCoerce(CoerceTo);
861}
Daniel Dunbarc4503572009-01-31 00:06:58 +0000862
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000863ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
864 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000865 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
866 // classification algorithm.
867 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000868 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000869
Daniel Dunbarc4503572009-01-31 00:06:58 +0000870 // Check some invariants.
871 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
872 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
873 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
874
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000875 const llvm::Type *ResType = 0;
876 switch (Lo) {
877 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000878 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000879
880 case SSEUp:
881 case X87Up:
882 assert(0 && "Invalid classification for lo word.");
883
Daniel Dunbarc4503572009-01-31 00:06:58 +0000884 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000885 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000886 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000887 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000888
889 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
890 // available register of the sequence %rax, %rdx is used.
891 case Integer:
892 ResType = llvm::Type::Int64Ty; break;
893
894 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
895 // available SSE register of the sequence %xmm0, %xmm1 is used.
896 case SSE:
897 ResType = llvm::Type::DoubleTy; break;
898
899 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
900 // returned on the X87 stack in %st0 as 80-bit x87 number.
901 case X87:
902 ResType = llvm::Type::X86_FP80Ty; break;
903
Daniel Dunbarc4503572009-01-31 00:06:58 +0000904 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
905 // part of the value is returned in %st0 and the imaginary part in
906 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000907 case ComplexX87:
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000908 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar3e030b42009-02-18 03:44:19 +0000909 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
910 llvm::Type::X86_FP80Ty,
911 NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000912 break;
913 }
914
915 switch (Hi) {
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000916 // Memory was handled previously and X87 should
917 // never occur as a hi class.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000918 case Memory:
919 case X87:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000920 assert(0 && "Invalid classification for hi word.");
921
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000922 case ComplexX87: // Previously handled.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000923 case NoClass: break;
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000924
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000925 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000926 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
927 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000928 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000929 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
930 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000931
932 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
933 // is passed in the upper half of the last used SSE register.
934 //
935 // SSEUP should always be preceeded by SSE, just widen.
936 case SSEUp:
937 assert(Lo == SSE && "Unexpected SSEUp classification.");
938 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
939 break;
940
941 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000942 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000943 case X87Up:
Daniel Dunbar100f4022009-03-06 17:50:25 +0000944 // If X87Up is preceeded by X87, we don't need to do
945 // anything. However, in some cases with unions it may not be
946 // preceeded by X87. In such situations we follow gcc and pass the
947 // extra bits in an SSE reg.
948 if (Lo != X87)
949 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000950 break;
951 }
952
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000953 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000954}
955
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000956ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000957 unsigned &neededInt,
958 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000959 X86_64ABIInfo::Class Lo, Hi;
960 classify(Ty, Context, 0, Lo, Hi);
961
962 // Check some invariants.
963 // FIXME: Enforce these by construction.
964 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
965 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
966 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
967
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000968 neededInt = 0;
969 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000970 const llvm::Type *ResType = 0;
971 switch (Lo) {
972 case NoClass:
973 return ABIArgInfo::getIgnore();
974
975 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
976 // on the stack.
977 case Memory:
978
979 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
980 // COMPLEX_X87, it is passed in memory.
981 case X87:
982 case ComplexX87:
Daniel Dunbar245f5532009-02-22 08:17:51 +0000983 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000984
985 case SSEUp:
986 case X87Up:
987 assert(0 && "Invalid classification for lo word.");
988
989 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
990 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
991 // and %r9 is used.
992 case Integer:
993 ++neededInt;
994 ResType = llvm::Type::Int64Ty;
995 break;
996
997 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
998 // available SSE register is used, the registers are taken in the
999 // order from %xmm0 to %xmm7.
1000 case SSE:
1001 ++neededSSE;
1002 ResType = llvm::Type::DoubleTy;
1003 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001004 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001005
1006 switch (Hi) {
1007 // Memory was handled previously, ComplexX87 and X87 should
1008 // never occur as hi classes, and X87Up must be preceed by X87,
1009 // which is passed in memory.
1010 case Memory:
1011 case X87:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001012 case ComplexX87:
1013 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar100f4022009-03-06 17:50:25 +00001014 break;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001015
1016 case NoClass: break;
1017 case Integer:
1018 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1019 ++neededInt;
1020 break;
Daniel Dunbar100f4022009-03-06 17:50:25 +00001021
1022 // X87Up generally doesn't occur here (long double is passed in
1023 // memory), except in situations involving unions.
1024 case X87Up:
1025 case SSE:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001026 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1027 ++neededSSE;
1028 break;
1029
1030 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1031 // eightbyte is passed in the upper half of the last used SSE
1032 // register.
1033 case SSEUp:
1034 assert(Lo == SSE && "Unexpected SSEUp classification.");
1035 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1036 break;
1037 }
1038
Daniel Dunbar644f4c32009-02-14 02:09:24 +00001039 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001040}
1041
1042void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1043 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1044
1045 // Keep track of the number of assigned registers.
1046 unsigned freeIntRegs = 6, freeSSERegs = 8;
1047
1048 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1049 // get assigned (in left-to-right order) for passing as follows...
1050 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001051 it != ie; ++it) {
1052 unsigned neededInt, neededSSE;
1053 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1054
1055 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1056 // eightbyte of an argument, the whole argument is passed on the
1057 // stack. If registers have already been assigned for some
1058 // eightbytes of such an argument, the assignments get reverted.
1059 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1060 freeIntRegs -= neededInt;
1061 freeSSERegs -= neededSSE;
1062 } else {
Daniel Dunbar245f5532009-02-22 08:17:51 +00001063 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001064 }
1065 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001066}
1067
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001068static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1069 QualType Ty,
1070 CodeGenFunction &CGF) {
1071 llvm::Value *overflow_arg_area_p =
1072 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1073 llvm::Value *overflow_arg_area =
1074 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1075
1076 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1077 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001078 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001079 if (Align > 8) {
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001080 // Note that we follow the ABI & gcc here, even though the type
1081 // could in theory have an alignment greater than 16. This case
1082 // shouldn't ever matter in practice.
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001083
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001084 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1085 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1086 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1087 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1088 llvm::Type::Int64Ty);
1089 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1090 overflow_arg_area =
1091 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1092 overflow_arg_area->getType(),
1093 "overflow_arg_area.align");
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001094 }
1095
1096 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1097 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1098 llvm::Value *Res =
1099 CGF.Builder.CreateBitCast(overflow_arg_area,
1100 llvm::PointerType::getUnqual(LTy));
1101
1102 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1103 // l->overflow_arg_area + sizeof(type).
1104 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1105 // an 8 byte boundary.
1106
1107 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1108 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1109 (SizeInBytes + 7) & ~7);
1110 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1111 "overflow_arg_area.next");
1112 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1113
1114 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1115 return Res;
1116}
1117
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001118llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1119 CodeGenFunction &CGF) const {
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001120 // Assume that va_list type is correct; should be pointer to LLVM type:
1121 // struct {
1122 // i32 gp_offset;
1123 // i32 fp_offset;
1124 // i8* overflow_arg_area;
1125 // i8* reg_save_area;
1126 // };
1127 unsigned neededInt, neededSSE;
1128 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1129 neededInt, neededSSE);
1130
1131 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1132 // in the registers. If not go to step 7.
1133 if (!neededInt && !neededSSE)
1134 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1135
1136 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1137 // general purpose registers needed to pass type and num_fp to hold
1138 // the number of floating point registers needed.
1139
1140 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1141 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1142 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1143 //
1144 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1145 // register save space).
1146
1147 llvm::Value *InRegs = 0;
1148 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1149 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1150 if (neededInt) {
1151 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1152 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1153 InRegs =
1154 CGF.Builder.CreateICmpULE(gp_offset,
1155 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1156 48 - neededInt * 8),
1157 "fits_in_gp");
1158 }
1159
1160 if (neededSSE) {
1161 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1162 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1163 llvm::Value *FitsInFP =
1164 CGF.Builder.CreateICmpULE(fp_offset,
1165 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar90dafa12009-02-18 22:19:44 +00001166 176 - neededSSE * 16),
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001167 "fits_in_fp");
Daniel Dunbarf2313462009-02-18 22:05:01 +00001168 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001169 }
1170
1171 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1172 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1173 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1174 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1175
1176 // Emit code to load the value if it was passed in registers.
1177
1178 CGF.EmitBlock(InRegBlock);
1179
1180 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1181 // an offset of l->gp_offset and/or l->fp_offset. This may require
1182 // copying to a temporary location in case the parameter is passed
1183 // in different register classes or requires an alignment greater
1184 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001185 //
1186 // FIXME: This really results in shameful code when we end up
1187 // needing to collect arguments from different places; often what
1188 // should result in a simple assembling of a structure from
1189 // scattered addresses has many more loads than necessary. Can we
1190 // clean this up?
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001191 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1192 llvm::Value *RegAddr =
1193 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1194 "reg_save_area");
1195 if (neededInt && neededSSE) {
Daniel Dunbar55e5d892009-02-13 17:46:31 +00001196 // FIXME: Cleanup.
1197 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1198 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1199 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1200 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1201 const llvm::Type *TyLo = ST->getElementType(0);
1202 const llvm::Type *TyHi = ST->getElementType(1);
1203 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1204 "Unexpected ABI info for mixed regs");
1205 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1206 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1207 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1208 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1209 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1210 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1211 llvm::Value *V =
1212 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1213 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1214 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1215 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1216
1217 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001218 } else if (neededInt) {
1219 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1220 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1221 llvm::PointerType::getUnqual(LTy));
1222 } else {
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001223 if (neededSSE == 1) {
1224 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1225 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1226 llvm::PointerType::getUnqual(LTy));
1227 } else {
1228 assert(neededSSE == 2 && "Invalid number of needed registers!");
1229 // SSE registers are spaced 16 bytes apart in the register save
1230 // area, we need to collect the two eightbytes together.
1231 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1232 llvm::Value *RegAddrHi =
1233 CGF.Builder.CreateGEP(RegAddrLo,
1234 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1235 const llvm::Type *DblPtrTy =
1236 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1237 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1238 llvm::Type::DoubleTy,
1239 NULL);
1240 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1241 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1242 DblPtrTy));
1243 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1244 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1245 DblPtrTy));
1246 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1247 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1248 llvm::PointerType::getUnqual(LTy));
1249 }
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001250 }
1251
1252 // AMD64-ABI 3.5.7p5: Step 5. Set:
1253 // l->gp_offset = l->gp_offset + num_gp * 8
1254 // l->fp_offset = l->fp_offset + num_fp * 16.
1255 if (neededInt) {
1256 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1257 neededInt * 8);
1258 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1259 gp_offset_p);
1260 }
1261 if (neededSSE) {
1262 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1263 neededSSE * 16);
1264 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1265 fp_offset_p);
1266 }
1267 CGF.EmitBranch(ContBlock);
1268
1269 // Emit code to load the value if it was passed in memory.
1270
1271 CGF.EmitBlock(InMemBlock);
1272 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1273
1274 // Return the appropriate result.
1275
1276 CGF.EmitBlock(ContBlock);
1277 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1278 "vaarg.addr");
1279 ResAddr->reserveOperandSpace(2);
1280 ResAddr->addIncoming(RegAddr, InRegBlock);
1281 ResAddr->addIncoming(MemAddr, InMemBlock);
1282
1283 return ResAddr;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001284}
1285
Eli Friedmana027ea92009-03-29 00:15:25 +00001286class ARMABIInfo : public ABIInfo {
1287 ABIArgInfo classifyReturnType(QualType RetTy,
1288 ASTContext &Context) const;
1289
1290 ABIArgInfo classifyArgumentType(QualType RetTy,
1291 ASTContext &Context) const;
1292
1293 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1294
1295 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1296 CodeGenFunction &CGF) const;
1297};
1298
1299void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1300 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1301 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1302 it != ie; ++it) {
1303 it->info = classifyArgumentType(it->type, Context);
1304 }
1305}
1306
1307ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1308 ASTContext &Context) const {
1309 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1310 return ABIArgInfo::getDirect();
1311 }
1312 // FIXME: This is kind of nasty... but there isn't much choice
1313 // because the ARM backend doesn't support byval.
1314 // FIXME: This doesn't handle alignment > 64 bits.
1315 const llvm::Type* ElemTy;
1316 unsigned SizeRegs;
1317 if (Context.getTypeAlign(Ty) > 32) {
1318 ElemTy = llvm::Type::Int64Ty;
1319 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1320 } else {
1321 ElemTy = llvm::Type::Int32Ty;
1322 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1323 }
1324 std::vector<const llvm::Type*> LLVMFields;
1325 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1326 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1327 return ABIArgInfo::getCoerce(STy);
1328}
1329
1330ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1331 ASTContext &Context) const {
1332 if (RetTy->isVoidType()) {
1333 return ABIArgInfo::getIgnore();
1334 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1335 // Aggregates <= 4 bytes are returned in r0; other aggregates
1336 // are returned indirectly.
1337 uint64_t Size = Context.getTypeSize(RetTy);
1338 if (Size <= 32)
1339 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1340 return ABIArgInfo::getIndirect(0);
1341 } else {
1342 return ABIArgInfo::getDirect();
1343 }
1344}
1345
1346llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1347 CodeGenFunction &CGF) const {
1348 // FIXME: Need to handle alignment
1349 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1350 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1351
1352 CGBuilderTy &Builder = CGF.Builder;
1353 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1354 "ap");
1355 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1356 llvm::Type *PTy =
1357 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1358 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1359
1360 uint64_t Offset =
1361 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1362 llvm::Value *NextAddr =
1363 Builder.CreateGEP(Addr,
1364 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1365 "ap.next");
1366 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1367
1368 return AddrTyped;
1369}
1370
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001371ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001372 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001373 if (RetTy->isVoidType()) {
1374 return ABIArgInfo::getIgnore();
1375 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001376 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001377 } else {
1378 return ABIArgInfo::getDirect();
1379 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001380}
1381
1382ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001383 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001384 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001385 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001386 } else {
1387 return ABIArgInfo::getDirect();
1388 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001389}
1390
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001391llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1392 CodeGenFunction &CGF) const {
1393 return 0;
1394}
1395
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001396const ABIInfo &CodeGenTypes::getABIInfo() const {
1397 if (TheABIInfo)
1398 return *TheABIInfo;
1399
1400 // For now we just cache this in the CodeGenTypes and don't bother
1401 // to free it.
1402 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1403 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman9fd58e82009-03-23 23:26:24 +00001404 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001405 switch (getContext().Target.getPointerWidth(0)) {
1406 case 32:
Eli Friedman9fd58e82009-03-23 23:26:24 +00001407 return *(TheABIInfo = new X86_32ABIInfo(IsDarwin));
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001408 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +00001409 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001410 }
Eli Friedmana027ea92009-03-29 00:15:25 +00001411 } else if (strcmp(TargetPrefix, "arm") == 0) {
1412 // FIXME: Support for OABI?
1413 return *(TheABIInfo = new ARMABIInfo());
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001414 }
1415
1416 return *(TheABIInfo = new DefaultABIInfo);
1417}
1418
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001419/***/
1420
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001421CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1422 const llvm::SmallVector<QualType, 16> &ArgTys) {
1423 NumArgs = ArgTys.size();
1424 Args = new ArgInfo[1 + NumArgs];
1425 Args[0].type = ResTy;
1426 for (unsigned i = 0; i < NumArgs; ++i)
1427 Args[1 + i].type = ArgTys[i];
1428}
1429
1430/***/
1431
Daniel Dunbar56273772008-09-17 00:51:38 +00001432void CodeGenTypes::GetExpandedTypes(QualType Ty,
1433 std::vector<const llvm::Type*> &ArgTys) {
1434 const RecordType *RT = Ty->getAsStructureType();
1435 assert(RT && "Can only expand structure types.");
1436 const RecordDecl *RD = RT->getDecl();
1437 assert(!RD->hasFlexibleArrayMember() &&
1438 "Cannot expand structure with flexible array.");
1439
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001440 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +00001441 e = RD->field_end(); i != e; ++i) {
1442 const FieldDecl *FD = *i;
1443 assert(!FD->isBitField() &&
1444 "Cannot expand structure with bit-field members.");
1445
1446 QualType FT = FD->getType();
1447 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1448 GetExpandedTypes(FT, ArgTys);
1449 } else {
1450 ArgTys.push_back(ConvertType(FT));
1451 }
1452 }
1453}
1454
1455llvm::Function::arg_iterator
1456CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1457 llvm::Function::arg_iterator AI) {
1458 const RecordType *RT = Ty->getAsStructureType();
1459 assert(RT && "Can only expand structure types.");
1460
1461 RecordDecl *RD = RT->getDecl();
1462 assert(LV.isSimple() &&
1463 "Unexpected non-simple lvalue during struct expansion.");
1464 llvm::Value *Addr = LV.getAddress();
1465 for (RecordDecl::field_iterator i = RD->field_begin(),
1466 e = RD->field_end(); i != e; ++i) {
1467 FieldDecl *FD = *i;
1468 QualType FT = FD->getType();
1469
1470 // FIXME: What are the right qualifiers here?
1471 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1472 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1473 AI = ExpandTypeFromArgs(FT, LV, AI);
1474 } else {
1475 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1476 ++AI;
1477 }
1478 }
1479
1480 return AI;
1481}
1482
1483void
1484CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1485 llvm::SmallVector<llvm::Value*, 16> &Args) {
1486 const RecordType *RT = Ty->getAsStructureType();
1487 assert(RT && "Can only expand structure types.");
1488
1489 RecordDecl *RD = RT->getDecl();
1490 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1491 llvm::Value *Addr = RV.getAggregateAddr();
1492 for (RecordDecl::field_iterator i = RD->field_begin(),
1493 e = RD->field_end(); i != e; ++i) {
1494 FieldDecl *FD = *i;
1495 QualType FT = FD->getType();
1496
1497 // FIXME: What are the right qualifiers here?
1498 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1499 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1500 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1501 } else {
1502 RValue RV = EmitLoadOfLValue(LV, FT);
1503 assert(RV.isScalar() &&
1504 "Unexpected non-scalar rvalue during struct expansion.");
1505 Args.push_back(RV.getScalarVal());
1506 }
1507 }
1508}
1509
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001510/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1511/// a pointer to an object of type \arg Ty.
1512///
1513/// This safely handles the case when the src type is smaller than the
1514/// destination type; in this situation the values of bits which not
1515/// present in the src are undefined.
1516static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1517 const llvm::Type *Ty,
1518 CodeGenFunction &CGF) {
1519 const llvm::Type *SrcTy =
1520 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1521 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1522 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1523
Daniel Dunbarb225be42009-02-03 05:59:18 +00001524 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001525 if (SrcSize == DstSize) {
1526 llvm::Value *Casted =
1527 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001528 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1529 // FIXME: Use better alignment / avoid requiring aligned load.
1530 Load->setAlignment(1);
1531 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001532 } else {
1533 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1534
1535 // Otherwise do coercion through memory. This is stupid, but
1536 // simple.
1537 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1538 llvm::Value *Casted =
1539 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001540 llvm::StoreInst *Store =
1541 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1542 // FIXME: Use better alignment / avoid requiring aligned store.
1543 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001544 return CGF.Builder.CreateLoad(Tmp);
1545 }
1546}
1547
1548/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1549/// where the source and destination may have different types.
1550///
1551/// This safely handles the case when the src type is larger than the
1552/// destination type; the upper bits of the src will be lost.
1553static void CreateCoercedStore(llvm::Value *Src,
1554 llvm::Value *DstPtr,
1555 CodeGenFunction &CGF) {
1556 const llvm::Type *SrcTy = Src->getType();
1557 const llvm::Type *DstTy =
1558 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1559
1560 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1561 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1562
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001563 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001564 if (SrcSize == DstSize) {
1565 llvm::Value *Casted =
1566 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001567 // FIXME: Use better alignment / avoid requiring aligned store.
1568 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001569 } else {
1570 assert(SrcSize > DstSize && "Coercion is missing bits!");
1571
1572 // Otherwise do coercion through memory. This is stupid, but
1573 // simple.
1574 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1575 CGF.Builder.CreateStore(Src, Tmp);
1576 llvm::Value *Casted =
1577 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001578 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1579 // FIXME: Use better alignment / avoid requiring aligned load.
1580 Load->setAlignment(1);
1581 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001582 }
1583}
1584
Daniel Dunbar56273772008-09-17 00:51:38 +00001585/***/
1586
Daniel Dunbar88b53962009-02-02 22:03:45 +00001587bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001588 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001589}
1590
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001591const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001592CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001593 std::vector<const llvm::Type*> ArgTys;
1594
1595 const llvm::Type *ResultType = 0;
1596
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001597 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001598 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001599 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001600 case ABIArgInfo::Expand:
1601 assert(0 && "Invalid ABI kind for return argument");
1602
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001603 case ABIArgInfo::Direct:
1604 ResultType = ConvertType(RetTy);
1605 break;
1606
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001607 case ABIArgInfo::Indirect: {
1608 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001609 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001610 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001611 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1612 break;
1613 }
1614
Daniel Dunbar11434922009-01-26 21:26:08 +00001615 case ABIArgInfo::Ignore:
1616 ResultType = llvm::Type::VoidTy;
1617 break;
1618
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001619 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001620 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001621 break;
1622 }
1623
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001624 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1625 ie = FI.arg_end(); it != ie; ++it) {
1626 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001627
1628 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001629 case ABIArgInfo::Ignore:
1630 break;
1631
Daniel Dunbar56273772008-09-17 00:51:38 +00001632 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001633 ArgTys.push_back(AI.getCoerceToType());
1634 break;
1635
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001636 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001637 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001638 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1639 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001640 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001641 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001642
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001643 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001644 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001645 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001646
1647 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001648 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001649 break;
1650 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001651 }
1652
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001653 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001654}
1655
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001656void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001657 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001658 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001659 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001660 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001661
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001662 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001663 if (TargetDecl) {
1664 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001665 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001666 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001667 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001668 if (TargetDecl->getAttr<PureAttr>())
1669 FuncAttrs |= llvm::Attribute::ReadOnly;
1670 if (TargetDecl->getAttr<ConstAttr>())
1671 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001672 }
1673
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001674 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001675 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001676 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001677 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001678 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001679 if (RetTy->isPromotableIntegerType()) {
1680 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001681 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001682 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001683 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001684 }
1685 }
1686 break;
1687
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001688 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001689 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001690 llvm::Attribute::StructRet |
1691 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001692 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001693 // sret disables readnone and readonly
1694 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1695 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001696 break;
1697
Daniel Dunbar11434922009-01-26 21:26:08 +00001698 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001699 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001700 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001701
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001702 case ABIArgInfo::Expand:
1703 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001704 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001705
Devang Patela2c69122008-09-26 22:53:57 +00001706 if (RetAttrs)
1707 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001708
1709 // FIXME: we need to honour command line settings also...
1710 // FIXME: RegParm should be reduced in case of nested functions and/or global
1711 // register variable.
1712 signed RegParm = 0;
1713 if (TargetDecl)
1714 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1715 RegParm = RegParmAttr->getNumParams();
1716
1717 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001718 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1719 ie = FI.arg_end(); it != ie; ++it) {
1720 QualType ParamType = it->type;
1721 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001722 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001723
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001724 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001725 case ABIArgInfo::Coerce:
1726 break;
1727
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001728 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001729 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001730 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001731 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001732 // byval disables readnone and readonly.
1733 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1734 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001735 break;
1736
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001737 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001738 if (ParamType->isPromotableIntegerType()) {
1739 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001740 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001741 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001742 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001743 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001744 }
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001745 if (RegParm > 0 &&
1746 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1747 RegParm -=
1748 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1749 if (RegParm >= 0)
1750 Attributes |= llvm::Attribute::InReg;
1751 }
1752 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001753 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001754
Daniel Dunbar11434922009-01-26 21:26:08 +00001755 case ABIArgInfo::Ignore:
1756 // Skip increment, no matching LLVM parameter.
1757 continue;
1758
Daniel Dunbar56273772008-09-17 00:51:38 +00001759 case ABIArgInfo::Expand: {
1760 std::vector<const llvm::Type*> Tys;
1761 // FIXME: This is rather inefficient. Do we ever actually need
1762 // to do anything here? The result should be just reconstructed
1763 // on the other side, so extension should be a non-issue.
1764 getTypes().GetExpandedTypes(ParamType, Tys);
1765 Index += Tys.size();
1766 continue;
1767 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001768 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001769
Devang Patel761d7f72008-09-25 21:02:23 +00001770 if (Attributes)
1771 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001772 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001773 }
Devang Patela2c69122008-09-26 22:53:57 +00001774 if (FuncAttrs)
1775 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001776}
1777
Daniel Dunbar88b53962009-02-02 22:03:45 +00001778void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1779 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001780 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001781 // FIXME: We no longer need the types from FunctionArgList; lift up
1782 // and simplify.
1783
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001784 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1785 llvm::Function::arg_iterator AI = Fn->arg_begin();
1786
1787 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001788 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001789 AI->setName("agg.result");
1790 ++AI;
1791 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001792
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001793 assert(FI.arg_size() == Args.size() &&
1794 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001795 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001796 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001797 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001798 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001799 QualType Ty = info_it->type;
1800 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001801
1802 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001803 case ABIArgInfo::Indirect: {
1804 llvm::Value* V = AI;
1805 if (hasAggregateLLVMType(Ty)) {
1806 // Do nothing, aggregates and complex variables are accessed by
1807 // reference.
1808 } else {
1809 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001810 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001811 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1812 // This must be a promotion, for something like
1813 // "void a(x) short x; {..."
1814 V = EmitScalarConversion(V, Ty, Arg->getType());
1815 }
1816 }
1817 EmitParmDecl(*Arg, V);
1818 break;
1819 }
1820
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001821 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001822 assert(AI != Fn->arg_end() && "Argument mismatch!");
1823 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001824 if (hasAggregateLLVMType(Ty)) {
1825 // Create a temporary alloca to hold the argument; the rest of
1826 // codegen expects to access aggregates & complex values by
1827 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001828 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001829 Builder.CreateStore(AI, V);
1830 } else {
1831 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1832 // This must be a promotion, for something like
1833 // "void a(x) short x; {..."
1834 V = EmitScalarConversion(V, Ty, Arg->getType());
1835 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001836 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001837 EmitParmDecl(*Arg, V);
1838 break;
1839 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001840
1841 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001842 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001843 // we need to create a temporary and reconstruct it from the
1844 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001845 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001846 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001847 (Name + ".addr").c_str());
1848 // FIXME: What are the right qualifiers here?
1849 llvm::Function::arg_iterator End =
1850 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1851 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001852
Daniel Dunbar56273772008-09-17 00:51:38 +00001853 // Name the arguments used in expansion and increment AI.
1854 unsigned Index = 0;
1855 for (; AI != End; ++AI, ++Index)
1856 AI->setName(Name + "." + llvm::utostr(Index));
1857 continue;
1858 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001859
1860 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001861 // Initialize the local variable appropriately.
1862 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001863 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001864 } else {
1865 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1866 }
1867
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001868 // Skip increment, no matching LLVM parameter.
1869 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001870
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001871 case ABIArgInfo::Coerce: {
1872 assert(AI != Fn->arg_end() && "Argument mismatch!");
1873 // FIXME: This is very wasteful; EmitParmDecl is just going to
1874 // drop the result in a new alloca anyway, so we could just
1875 // store into that directly if we broke the abstraction down
1876 // more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001877 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001878 CreateCoercedStore(AI, V, *this);
1879 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001880 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001881 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001882 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 }
1887 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001888 EmitParmDecl(*Arg, V);
1889 break;
1890 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001891 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001892
1893 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001894 }
1895 assert(AI == Fn->arg_end() && "Argument mismatch!");
1896}
1897
Daniel Dunbar88b53962009-02-02 22:03:45 +00001898void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001899 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001900 llvm::Value *RV = 0;
1901
1902 // Functions with no result always return void.
1903 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001904 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001905 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001906
1907 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001908 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001909 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001910 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1911 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1912 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1913 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1914 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001915 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1916 false);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001917 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001918 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001919
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001920 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001921 // The internal return value temp always will have
1922 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001923 RV = Builder.CreateLoad(ReturnValue);
1924 break;
1925
Daniel Dunbar11434922009-01-26 21:26:08 +00001926 case ABIArgInfo::Ignore:
1927 break;
1928
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001929 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001930 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001931 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001932
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001933 case ABIArgInfo::Expand:
1934 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001935 }
1936 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001937
1938 if (RV) {
1939 Builder.CreateRet(RV);
1940 } else {
1941 Builder.CreateRetVoid();
1942 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001943}
1944
Daniel Dunbar88b53962009-02-02 22:03:45 +00001945RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1946 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001947 const CallArgList &CallArgs,
1948 const Decl *TargetDecl) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001949 // FIXME: We no longer need the types from CallArgs; lift up and
1950 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001951 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001952
1953 // Handle struct-return functions by passing a pointer to the
1954 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001955 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001956 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00001957 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001958 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001959 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001960 }
1961
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001962 assert(CallInfo.arg_size() == CallArgs.size() &&
1963 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001964 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001965 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001966 I != E; ++I, ++info_it) {
1967 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001968 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001969
1970 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001971 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001972 if (RV.isScalar() || RV.isComplex()) {
1973 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001974 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001975 if (RV.isScalar())
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001976 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001977 else
1978 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1979 } else {
1980 Args.push_back(RV.getAggregateAddr());
1981 }
1982 break;
1983
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001984 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001985 if (RV.isScalar()) {
1986 Args.push_back(RV.getScalarVal());
1987 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001988 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1989 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1990 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1991 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00001992 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001993 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00001994 }
1995 break;
1996
Daniel Dunbar11434922009-01-26 21:26:08 +00001997 case ABIArgInfo::Ignore:
1998 break;
1999
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002000 case ABIArgInfo::Coerce: {
2001 // FIXME: Avoid the conversion through memory if possible.
2002 llvm::Value *SrcPtr;
2003 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00002004 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002005 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002006 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002007 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002008 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2009 } else
2010 SrcPtr = RV.getAggregateAddr();
2011 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2012 *this));
2013 break;
2014 }
2015
Daniel Dunbar56273772008-09-17 00:51:38 +00002016 case ABIArgInfo::Expand:
2017 ExpandTypeToArgs(I->second, RV, Args);
2018 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002019 }
2020 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002021
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002022 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel761d7f72008-09-25 21:02:23 +00002023 CodeGen::AttributeListType AttributeList;
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002024 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002025 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2026 AttributeList.end());
Daniel Dunbar725ad312009-01-31 02:19:00 +00002027
Daniel Dunbard14151d2009-03-02 04:32:35 +00002028 llvm::CallSite CS;
2029 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2030 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002031 } else {
2032 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbard14151d2009-03-02 04:32:35 +00002033 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2034 &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002035 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00002036 }
2037
Daniel Dunbard14151d2009-03-02 04:32:35 +00002038 CS.setAttributes(Attrs);
2039 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2040 CS.setCallingConv(F->getCallingConv());
2041
2042 // If the call doesn't return, finish the basic block and clear the
2043 // insertion point; this allows the rest of IRgen to discard
2044 // unreachable code.
2045 if (CS.doesNotReturn()) {
2046 Builder.CreateUnreachable();
2047 Builder.ClearInsertionPoint();
2048
2049 // FIXME: For now, emit a dummy basic block because expr
2050 // emitters in generally are not ready to handle emitting
2051 // expressions at unreachable points.
2052 EnsureInsertPoint();
2053
2054 // Return a reasonable RValue.
2055 return GetUndefRValue(RetTy);
2056 }
2057
2058 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner34030842009-03-22 00:32:22 +00002059 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002060 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002061
2062 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002063 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002064 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00002065 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00002066 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00002067 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00002068 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002069
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002070 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002071 if (RetTy->isAnyComplexType()) {
2072 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2073 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2074 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00002075 }
2076 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002077 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002078 Builder.CreateStore(CI, V);
2079 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002080 }
2081 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002082
Daniel Dunbar11434922009-01-26 21:26:08 +00002083 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00002084 // If we are ignoring an argument that had a result, make sure to
2085 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00002086 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00002087
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002088 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002089 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002090 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00002091 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00002092 if (RetTy->isAnyComplexType())
2093 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner34030842009-03-22 00:32:22 +00002094 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00002095 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002096 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002097 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002098
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002099 case ABIArgInfo::Expand:
2100 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002101 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002102
2103 assert(0 && "Unhandled ABIArgInfo::Kind");
2104 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002105}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00002106
2107/* VarArg handling */
2108
2109llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2110 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2111}