blob: 27322525bb2ac36291dd325dc98df6acb0fdd0fe [file] [log] [blame]
Daniel Dunbar0dbe2272008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000021#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000022#include "clang/AST/DeclObjC.h"
Daniel Dunbar99037e52009-01-29 08:13:58 +000023#include "clang/AST/RecordLayout.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000024#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000025#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000026#include "llvm/Support/CallSite.h"
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +000027#include "llvm/Support/CommandLine.h"
Daniel Dunbarbe9eb092009-02-12 09:04:14 +000028#include "llvm/Support/MathExtras.h"
Daniel Dunbar6f7279b2009-02-04 23:24:38 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000030#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000031
32#include "ABIInfo.h"
33
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000034using namespace clang;
35using namespace CodeGen;
36
37/***/
38
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000039// FIXME: Use iterator and sidestep silly type array creation.
40
Daniel Dunbar541b63b2009-02-02 23:23:47 +000041const
Douglas Gregor72564e72009-02-26 23:50:07 +000042CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000043 return getFunctionInfo(FTNP->getResultType(),
44 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000045}
46
Daniel Dunbar541b63b2009-02-02 23:23:47 +000047const
Douglas Gregor72564e72009-02-26 23:50:07 +000048CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000049 llvm::SmallVector<QualType, 16> ArgTys;
50 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000051 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000052 ArgTys.push_back(FTP->getArgType(i));
53 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000054}
55
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000056const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
57 llvm::SmallVector<QualType, 16> ArgTys;
58 // Add the 'this' pointer.
59 ArgTys.push_back(MD->getThisType(Context));
60
61 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType();
62 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
63 ArgTys.push_back(FTP->getArgType(i));
64 return getFunctionInfo(FTP->getResultType(), ArgTys);
65}
66
Daniel Dunbar541b63b2009-02-02 23:23:47 +000067const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000068 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
69 if (MD->isInstance())
70 return getFunctionInfo(MD);
71 }
72
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000073 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +000074 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar541b63b2009-02-02 23:23:47 +000075 return getFunctionInfo(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +000076 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000077}
78
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
80 llvm::SmallVector<QualType, 16> ArgTys;
81 ArgTys.push_back(MD->getSelfDecl()->getType());
82 ArgTys.push_back(Context.getObjCSelType());
83 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +000084 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000085 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000086 ArgTys.push_back((*i)->getType());
87 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000088}
89
Daniel Dunbar541b63b2009-02-02 23:23:47 +000090const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
91 const CallArgList &Args) {
92 // FIXME: Kill copy.
93 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000094 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
95 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000096 ArgTys.push_back(i->second);
97 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000098}
99
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000100const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
101 const FunctionArgList &Args) {
102 // FIXME: Kill copy.
103 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000104 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
105 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000106 ArgTys.push_back(i->second);
107 return getFunctionInfo(ResTy, ArgTys);
108}
109
110const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
111 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000112 // Lookup or create unique function info.
113 llvm::FoldingSetNodeID ID;
114 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
115
116 void *InsertPos = 0;
117 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
118 if (FI)
119 return *FI;
120
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000121 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000122 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000123 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000124
125 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000126 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000127
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000128 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000129}
130
131/***/
132
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000133ABIInfo::~ABIInfo() {}
134
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000135void ABIArgInfo::dump() const {
136 fprintf(stderr, "(ABIArgInfo Kind=");
137 switch (TheKind) {
138 case Direct:
139 fprintf(stderr, "Direct");
140 break;
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000141 case Ignore:
142 fprintf(stderr, "Ignore");
143 break;
144 case Coerce:
145 fprintf(stderr, "Coerce Type=");
146 getCoerceToType()->print(llvm::errs());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000147 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000148 case Indirect:
149 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000150 break;
151 case Expand:
152 fprintf(stderr, "Expand");
153 break;
154 }
155 fprintf(stderr, ")\n");
156}
157
158/***/
159
Daniel Dunbar5bde6f42009-03-31 19:01:39 +0000160/// isEmptyRecord - Return true iff a structure has no non-empty
Daniel Dunbar834af452008-09-17 21:22:33 +0000161/// members. Note that a structure with a flexible array member is not
162/// considered empty.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000163static bool isEmptyRecord(ASTContext &Context, QualType T) {
Daniel Dunbar5bde6f42009-03-31 19:01:39 +0000164 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar834af452008-09-17 21:22:33 +0000165 if (!RT)
166 return 0;
167 const RecordDecl *RD = RT->getDecl();
168 if (RD->hasFlexibleArrayMember())
169 return false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000170 for (RecordDecl::field_iterator i = RD->field_begin(Context),
171 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000172 const FieldDecl *FD = *i;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000173 if (!isEmptyRecord(Context, FD->getType()))
Daniel Dunbar834af452008-09-17 21:22:33 +0000174 return false;
175 }
176 return true;
177}
178
179/// isSingleElementStruct - Determine if a structure is a "single
180/// element struct", i.e. it has exactly one non-empty field or
181/// exactly one field which is itself a single element
182/// struct. Structures with flexible array members are never
183/// considered single element structs.
184///
185/// \return The field declaration for the single non-empty field, if
186/// it exists.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000187static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000188 const RecordType *RT = T->getAsStructureType();
189 if (!RT)
190 return 0;
191
192 const RecordDecl *RD = RT->getDecl();
193 if (RD->hasFlexibleArrayMember())
194 return 0;
195
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000196 const Type *Found = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000197 for (RecordDecl::field_iterator i = RD->field_begin(Context),
198 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000199 const FieldDecl *FD = *i;
200 QualType FT = FD->getType();
201
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000202 // Treat single element arrays as the element
203 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
204 if (AT->getSize().getZExtValue() == 1)
205 FT = AT->getElementType();
206
Douglas Gregor6ab35242009-04-09 21:40:53 +0000207 if (isEmptyRecord(Context, FT)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000208 // Ignore
209 } else if (Found) {
210 return 0;
211 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000212 Found = FT.getTypePtr();
Daniel Dunbar834af452008-09-17 21:22:33 +0000213 } else {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000214 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000215 if (!Found)
216 return 0;
217 }
218 }
219
220 return Found;
221}
222
223static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
224 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
225 return false;
226
227 uint64_t Size = Context.getTypeSize(Ty);
228 return Size == 32 || Size == 64;
229}
230
231static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
232 ASTContext &Context) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000233 for (RecordDecl::field_iterator i = RD->field_begin(Context),
234 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000235 const FieldDecl *FD = *i;
236
237 if (!is32Or64BitBasicType(FD->getType(), Context))
238 return false;
239
Daniel Dunbar8e034442009-04-27 18:31:32 +0000240 // FIXME: Reject bit-fields wholesale; there are two problems, we
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000241 // don't know how to expand them yet, and the predicate for
242 // telling if a bitfield still counts as "basic" is more
243 // complicated than what we were doing previously.
244 if (FD->isBitField())
245 return false;
Daniel Dunbar834af452008-09-17 21:22:33 +0000246 }
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000247
Daniel Dunbar834af452008-09-17 21:22:33 +0000248 return true;
249}
250
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000251namespace {
252/// DefaultABIInfo - The default implementation for ABI specific
253/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000254/// self-consistent and sensible LLVM IR generation, but does not
255/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000256class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000257 ABIArgInfo classifyReturnType(QualType RetTy,
258 ASTContext &Context) const;
259
260 ABIArgInfo classifyArgumentType(QualType RetTy,
261 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000262
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000263 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
264 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
265 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
266 it != ie; ++it)
267 it->info = classifyArgumentType(it->type, Context);
268 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000269
270 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
271 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000272};
273
274/// X86_32ABIInfo - The X86-32 ABI information.
275class X86_32ABIInfo : public ABIInfo {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000276 ASTContext &Context;
Eli Friedman9fd58e82009-03-23 23:26:24 +0000277 bool IsDarwin;
278
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000279 static bool isRegisterSize(unsigned Size) {
280 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
281 }
282
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000283 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
284
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000285public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000286 ABIArgInfo classifyReturnType(QualType RetTy,
287 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000288
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000289 ABIArgInfo classifyArgumentType(QualType RetTy,
290 ASTContext &Context) const;
291
292 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
293 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
294 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
295 it != ie; ++it)
296 it->info = classifyArgumentType(it->type, Context);
297 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000298
299 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
300 CodeGenFunction &CGF) const;
Eli Friedman9fd58e82009-03-23 23:26:24 +0000301
Douglas Gregor6ab35242009-04-09 21:40:53 +0000302 X86_32ABIInfo(ASTContext &Context, bool d)
303 : ABIInfo(), Context(Context), IsDarwin(d) {}
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000304};
305}
306
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000307
308/// shouldReturnTypeInRegister - Determine if the given type should be
309/// passed in a register (for the Darwin ABI).
310bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
311 ASTContext &Context) {
312 uint64_t Size = Context.getTypeSize(Ty);
313
314 // Type must be register sized.
315 if (!isRegisterSize(Size))
316 return false;
317
318 if (Ty->isVectorType()) {
319 // 64- and 128- bit vectors inside structures are not returned in
320 // registers.
321 if (Size == 64 || Size == 128)
322 return false;
323
324 return true;
325 }
326
327 // If this is a builtin, pointer, or complex type, it is ok.
328 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
329 return true;
330
331 // Arrays are treated like records.
332 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
333 return shouldReturnTypeInRegister(AT->getElementType(), Context);
334
335 // Otherwise, it must be a record type.
336 const RecordType *RT = Ty->getAsRecordType();
337 if (!RT) return false;
338
339 // Structure types are passed in register if all fields would be
340 // passed in a register.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000341 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context),
342 e = RT->getDecl()->field_end(Context); i != e; ++i) {
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000343 const FieldDecl *FD = *i;
344
Daniel Dunbar8e034442009-04-27 18:31:32 +0000345 // FIXME: Reject bit-fields wholesale for now; this is incorrect.
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000346 if (FD->isBitField())
347 return false;
348
349 // Empty structures are ignored.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000350 if (isEmptyRecord(Context, FD->getType()))
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000351 continue;
352
353 // Check fields recursively.
354 if (!shouldReturnTypeInRegister(FD->getType(), Context))
355 return false;
356 }
357
358 return true;
359}
360
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000361ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
362 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000363 if (RetTy->isVoidType()) {
364 return ABIArgInfo::getIgnore();
Daniel Dunbar36043162009-04-01 06:13:08 +0000365 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
366 // On Darwin, some vectors are returned in registers.
367 if (IsDarwin) {
368 uint64_t Size = Context.getTypeSize(RetTy);
369
370 // 128-bit vectors are a special case; they are returned in
371 // registers and we need to make sure to pick a type the LLVM
372 // backend will like.
373 if (Size == 128)
374 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
375 2));
376
377 // Always return in register if it fits in a general purpose
378 // register, or if it is 64 bits and has a single element.
379 if ((Size == 8 || Size == 16 || Size == 32) ||
380 (Size == 64 && VT->getNumElements() == 1))
381 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
382
383 return ABIArgInfo::getIndirect(0);
384 }
385
386 return ABIArgInfo::getDirect();
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000387 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8e034442009-04-27 18:31:32 +0000388 // Structures with flexible arrays are always indirect.
389 if (const RecordType *RT = RetTy->getAsStructureType())
390 if (RT->getDecl()->hasFlexibleArrayMember())
391 return ABIArgInfo::getIndirect(0);
392
Eli Friedman9fd58e82009-03-23 23:26:24 +0000393 // Outside of Darwin, structs and unions are always indirect.
394 if (!IsDarwin && !RetTy->isAnyComplexType())
395 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8e034442009-04-27 18:31:32 +0000396
Daniel Dunbar834af452008-09-17 21:22:33 +0000397 // Classify "single element" structs as their element type.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000398 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000399 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
400 // FIXME: This is gross, it would be nice if we could just
401 // pass back SeltTy and have clients deal with it. Is it worth
402 // supporting coerce to both LLVM and clang Types?
403 if (BT->isIntegerType()) {
404 uint64_t Size = Context.getTypeSize(SeltTy);
405 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
406 } else if (BT->getKind() == BuiltinType::Float) {
407 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
408 } else if (BT->getKind() == BuiltinType::Double) {
409 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
410 }
411 } else if (SeltTy->isPointerType()) {
412 // FIXME: It would be really nice if this could come out as
413 // the proper pointer type.
414 llvm::Type *PtrTy =
415 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
416 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000417 } else if (SeltTy->isVectorType()) {
418 // 64- and 128-bit vectors are never returned in a
419 // register when inside a structure.
420 uint64_t Size = Context.getTypeSize(RetTy);
421 if (Size == 64 || Size == 128)
422 return ABIArgInfo::getIndirect(0);
423
424 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000425 }
426 }
427
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000428 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000429 if (isRegisterSize(Size)) {
430 // Always return in register for unions for now.
431 // FIXME: This is wrong, but better than treating as a
432 // structure.
433 if (RetTy->isUnionType())
434 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
435
436 // Small structures which are register sized are generally returned
437 // in a register.
438 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context))
439 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
440 }
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000441
442 return ABIArgInfo::getIndirect(0);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000443 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000444 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000445 }
446}
447
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000448ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000449 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000450 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000451 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000452 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000453 if (const RecordType *RT = Ty->getAsStructureType())
454 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000455 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000456
Daniel Dunbar3170c932009-02-05 01:50:07 +0000457 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000458 uint64_t Size = Context.getTypeSize(Ty);
459 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000460 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000461
462 // Expand structs with size <= 128-bits which consist only of
463 // basic types (int, long long, float, double, xxx*). This is
464 // non-recursive and does not ignore empty fields.
465 if (const RecordType *RT = Ty->getAsStructureType()) {
466 if (Context.getTypeSize(Ty) <= 4*32 &&
467 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
468 return ABIArgInfo::getExpand();
469 }
470
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000471 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000472 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000473 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000474 }
475}
476
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000477llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
478 CodeGenFunction &CGF) const {
479 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
480 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
481
482 CGBuilderTy &Builder = CGF.Builder;
483 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
484 "ap");
485 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
486 llvm::Type *PTy =
487 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
488 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
489
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000490 uint64_t Offset =
491 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000492 llvm::Value *NextAddr =
493 Builder.CreateGEP(Addr,
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000494 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000495 "ap.next");
496 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
497
498 return AddrTyped;
499}
500
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000501namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000502/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000503class X86_64ABIInfo : public ABIInfo {
504 enum Class {
505 Integer = 0,
506 SSE,
507 SSEUp,
508 X87,
509 X87Up,
510 ComplexX87,
511 NoClass,
512 Memory
513 };
514
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000515 /// merge - Implement the X86_64 ABI merging algorithm.
516 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000517 /// Merge an accumulating classification \arg Accum with a field
518 /// classification \arg Field.
519 ///
520 /// \param Accum - The accumulating classification. This should
521 /// always be either NoClass or the result of a previous merge
522 /// call. In addition, this should never be Memory (the caller
523 /// should just return Memory for the aggregate).
524 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000525
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000526 /// classify - Determine the x86_64 register classes in which the
527 /// given type T should be passed.
528 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000529 /// \param Lo - The classification for the parts of the type
530 /// residing in the low word of the containing object.
531 ///
532 /// \param Hi - The classification for the parts of the type
533 /// residing in the high word of the containing object.
534 ///
535 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000536 /// containing object. Some parameters are classified different
537 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000538 ///
539 /// If a word is unused its result will be NoClass; if a type should
540 /// be passed in Memory then at least the classification of \arg Lo
541 /// will be Memory.
542 ///
543 /// The \arg Lo class will be NoClass iff the argument is ignored.
544 ///
545 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000546 /// also be ComplexX87.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000547 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000548 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000549
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000550 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
551 /// to coerce to, chose the best way to pass Ty in the same place
552 /// that \arg CoerceTo would be passed, but while keeping the
553 /// emitted code as simple as possible.
554 ///
555 /// FIXME: Note, this should be cleaned up to just take an
556 /// enumeration of all the ways we might want to pass things,
557 /// instead of constructing an LLVM type. This makes this code more
558 /// explicit, and it makes it clearer that we are also doing this
559 /// for correctness in the case of passing scalar types.
560 ABIArgInfo getCoerceResult(QualType Ty,
561 const llvm::Type *CoerceTo,
562 ASTContext &Context) const;
563
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000564 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000565 ASTContext &Context) const;
566
567 ABIArgInfo classifyArgumentType(QualType Ty,
568 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000569 unsigned &neededInt,
570 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000571
572public:
573 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000574
575 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
576 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000577};
578}
579
Daniel Dunbarc4503572009-01-31 00:06:58 +0000580X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
581 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000582 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
583 // classified recursively so that always two fields are
584 // considered. The resulting class is calculated according to
585 // the classes of the fields in the eightbyte:
586 //
587 // (a) If both classes are equal, this is the resulting class.
588 //
589 // (b) If one of the classes is NO_CLASS, the resulting class is
590 // the other class.
591 //
592 // (c) If one of the classes is MEMORY, the result is the MEMORY
593 // class.
594 //
595 // (d) If one of the classes is INTEGER, the result is the
596 // INTEGER.
597 //
598 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
599 // MEMORY is used as class.
600 //
601 // (f) Otherwise class SSE is used.
Daniel Dunbar100f4022009-03-06 17:50:25 +0000602
603 // Accum should never be memory (we should have returned) or
604 // ComplexX87 (because this cannot be passed in a structure).
605 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbarc4503572009-01-31 00:06:58 +0000606 "Invalid accumulated classification during merge.");
607 if (Accum == Field || Field == NoClass)
608 return Accum;
609 else if (Field == Memory)
610 return Memory;
611 else if (Accum == NoClass)
612 return Field;
613 else if (Accum == Integer || Field == Integer)
614 return Integer;
615 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
616 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000617 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000618 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000619}
620
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000621void X86_64ABIInfo::classify(QualType Ty,
622 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000623 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000624 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000625 // FIXME: This code can be simplified by introducing a simple value
626 // class for Class pairs with appropriate constructor methods for
627 // the various situations.
628
Daniel Dunbare28099b2009-02-22 04:48:22 +0000629 // FIXME: Some of the split computations are wrong; unaligned
630 // vectors shouldn't be passed in registers for example, so there is
631 // no chance they can straddle an eightbyte. Verify & simplify.
632
Daniel Dunbarc4503572009-01-31 00:06:58 +0000633 Lo = Hi = NoClass;
634
635 Class &Current = OffsetBase < 64 ? Lo : Hi;
636 Current = Memory;
637
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000638 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
639 BuiltinType::Kind k = BT->getKind();
640
Daniel Dunbar11434922009-01-26 21:26:08 +0000641 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000642 Current = NoClass;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000643 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattnerae69e002009-04-30 06:22:07 +0000644 Lo = Integer;
645 Hi = Integer;
Daniel Dunbar11434922009-01-26 21:26:08 +0000646 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000647 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000648 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000649 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000650 } else if (k == BuiltinType::LongDouble) {
651 Lo = X87;
652 Hi = X87Up;
653 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000654 // FIXME: _Decimal32 and _Decimal64 are SSE.
655 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson708762b2009-02-26 17:31:15 +0000656 } else if (const EnumType *ET = Ty->getAsEnumType()) {
657 // Classify the underlying integer type.
658 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbar89588912009-02-26 20:52:22 +0000659 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000660 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000661 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000662 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbare28099b2009-02-22 04:48:22 +0000663 if (Size == 32) {
664 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
665 // float> as integer.
666 Current = Integer;
667
668 // If this type crosses an eightbyte boundary, it should be
669 // split.
670 uint64_t EB_Real = (OffsetBase) / 64;
671 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
672 if (EB_Real != EB_Imag)
673 Hi = Lo;
674 } else if (Size == 64) {
Daniel Dunbar0af99292009-02-22 04:16:10 +0000675 // gcc passes <1 x double> in memory. :(
676 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000677 return;
Daniel Dunbar0af99292009-02-22 04:16:10 +0000678
679 // gcc passes <1 x long long> as INTEGER.
680 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
681 Current = Integer;
682 else
683 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000684
685 // If this type crosses an eightbyte boundary, it should be
686 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000687 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000688 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000689 } else if (Size == 128) {
690 Lo = SSE;
691 Hi = SSEUp;
692 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000693 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000694 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000695
Daniel Dunbare33edf12009-01-30 18:40:10 +0000696 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar0af99292009-02-22 04:16:10 +0000697 if (ET->isIntegralType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000698 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000699 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000700 else if (Size <= 128)
701 Lo = Hi = Integer;
702 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000703 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000704 else if (ET == Context.DoubleTy)
705 Lo = Hi = SSE;
706 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000707 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000708
709 // If this complex type crosses an eightbyte boundary then it
710 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000711 uint64_t EB_Real = (OffsetBase) / 64;
712 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000713 if (Hi == NoClass && EB_Real != EB_Imag)
714 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000715 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
716 // Arrays are treated like structures.
717
718 uint64_t Size = Context.getTypeSize(Ty);
719
720 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
721 // than two eightbytes, ..., it has class MEMORY.
722 if (Size > 128)
723 return;
724
725 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
726 // fields, it has class MEMORY.
727 //
728 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000729 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000730 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000731
732 // Otherwise implement simplified merge. We could be smarter about
733 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000734 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000735 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
736 uint64_t ArraySize = AT->getSize().getZExtValue();
737 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
738 Class FieldLo, FieldHi;
739 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000740 Lo = merge(Lo, FieldLo);
741 Hi = merge(Hi, FieldHi);
742 if (Lo == Memory || Hi == Memory)
743 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000744 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000745
746 // Do post merger cleanup (see below). Only case we worry about is Memory.
747 if (Hi == Memory)
748 Lo = Memory;
749 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000750 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000751 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000752
753 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
754 // than two eightbytes, ..., it has class MEMORY.
755 if (Size > 128)
756 return;
757
758 const RecordDecl *RD = RT->getDecl();
759
760 // Assume variable sized types are passed in memory.
761 if (RD->hasFlexibleArrayMember())
762 return;
763
764 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
765
766 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000767 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000768 unsigned idx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000769 for (RecordDecl::field_iterator i = RD->field_begin(Context),
770 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000771 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbardd81d442009-02-17 02:45:44 +0000772 bool BitField = i->isBitField();
Daniel Dunbar99037e52009-01-29 08:13:58 +0000773
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000774 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
775 // fields, it has class MEMORY.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000776 //
Daniel Dunbar8e034442009-04-27 18:31:32 +0000777 // Note, skip this test for bit-fields, see below.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000778 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar99037e52009-01-29 08:13:58 +0000779 Lo = Memory;
780 return;
781 }
782
Daniel Dunbar99037e52009-01-29 08:13:58 +0000783 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000784 //
785 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
786 // exceeds a single eightbyte, each is classified
787 // separately. Each eightbyte gets initialized to class
788 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000789 Class FieldLo, FieldHi;
Daniel Dunbardd81d442009-02-17 02:45:44 +0000790
Daniel Dunbar8e034442009-04-27 18:31:32 +0000791 // Bit-fields require special handling, they do not force the
Daniel Dunbardd81d442009-02-17 02:45:44 +0000792 // structure to be passed in memory even if unaligned, and
793 // therefore they can straddle an eightbyte.
794 if (BitField) {
795 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman9a901bb2009-04-26 19:19:15 +0000796 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbardd81d442009-02-17 02:45:44 +0000797
798 uint64_t EB_Lo = Offset / 64;
799 uint64_t EB_Hi = (Offset + Size - 1) / 64;
800 FieldLo = FieldHi = NoClass;
801 if (EB_Lo) {
802 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
803 FieldLo = NoClass;
804 FieldHi = Integer;
805 } else {
806 FieldLo = Integer;
807 FieldHi = EB_Hi ? Integer : NoClass;
808 }
809 } else
810 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000811 Lo = merge(Lo, FieldLo);
812 Hi = merge(Hi, FieldHi);
813 if (Lo == Memory || Hi == Memory)
814 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000815 }
816
817 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
818 //
819 // (a) If one of the classes is MEMORY, the whole argument is
820 // passed in memory.
821 //
822 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
823
824 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000825 // the merge (just bail).
826 //
827 // The second condition occurs in the case of unions; for example
828 // union { _Complex double; unsigned; }.
829 if (Hi == Memory)
830 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000831 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000832 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000833 }
834}
835
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000836ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
837 const llvm::Type *CoerceTo,
838 ASTContext &Context) const {
839 if (CoerceTo == llvm::Type::Int64Ty) {
840 // Integer and pointer types will end up in a general purpose
841 // register.
Daniel Dunbar0af99292009-02-22 04:16:10 +0000842 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000843 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000844
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000845 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000846 // FIXME: It would probably be better to make CGFunctionInfo only
847 // map using canonical types than to canonize here.
848 QualType CTy = Context.getCanonicalType(Ty);
849
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000850 // Float and double end up in a single SSE reg.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000851 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000852 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000853
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000854 }
855
856 return ABIArgInfo::getCoerce(CoerceTo);
857}
Daniel Dunbarc4503572009-01-31 00:06:58 +0000858
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000859ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
860 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000861 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
862 // classification algorithm.
863 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000864 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000865
Daniel Dunbarc4503572009-01-31 00:06:58 +0000866 // Check some invariants.
867 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
868 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
869 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
870
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000871 const llvm::Type *ResType = 0;
872 switch (Lo) {
873 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000874 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000875
876 case SSEUp:
877 case X87Up:
878 assert(0 && "Invalid classification for lo word.");
879
Daniel Dunbarc4503572009-01-31 00:06:58 +0000880 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000881 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000882 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000883 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000884
885 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
886 // available register of the sequence %rax, %rdx is used.
887 case Integer:
888 ResType = llvm::Type::Int64Ty; break;
889
890 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
891 // available SSE register of the sequence %xmm0, %xmm1 is used.
892 case SSE:
893 ResType = llvm::Type::DoubleTy; break;
894
895 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
896 // returned on the X87 stack in %st0 as 80-bit x87 number.
897 case X87:
898 ResType = llvm::Type::X86_FP80Ty; break;
899
Daniel Dunbarc4503572009-01-31 00:06:58 +0000900 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
901 // part of the value is returned in %st0 and the imaginary part in
902 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000903 case ComplexX87:
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000904 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar3e030b42009-02-18 03:44:19 +0000905 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
906 llvm::Type::X86_FP80Ty,
907 NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000908 break;
909 }
910
911 switch (Hi) {
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000912 // Memory was handled previously and X87 should
913 // never occur as a hi class.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000914 case Memory:
915 case X87:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000916 assert(0 && "Invalid classification for hi word.");
917
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000918 case ComplexX87: // Previously handled.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000919 case NoClass: break;
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000920
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000921 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000922 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
923 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000924 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000925 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
926 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000927
928 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
929 // is passed in the upper half of the last used SSE register.
930 //
931 // SSEUP should always be preceeded by SSE, just widen.
932 case SSEUp:
933 assert(Lo == SSE && "Unexpected SSEUp classification.");
934 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
935 break;
936
937 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000938 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000939 case X87Up:
Daniel Dunbar100f4022009-03-06 17:50:25 +0000940 // If X87Up is preceeded by X87, we don't need to do
941 // anything. However, in some cases with unions it may not be
942 // preceeded by X87. In such situations we follow gcc and pass the
943 // extra bits in an SSE reg.
944 if (Lo != X87)
945 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000946 break;
947 }
948
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000949 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000950}
951
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000952ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000953 unsigned &neededInt,
954 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000955 X86_64ABIInfo::Class Lo, Hi;
956 classify(Ty, Context, 0, Lo, Hi);
957
958 // Check some invariants.
959 // FIXME: Enforce these by construction.
960 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
961 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
962 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
963
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000964 neededInt = 0;
965 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000966 const llvm::Type *ResType = 0;
967 switch (Lo) {
968 case NoClass:
969 return ABIArgInfo::getIgnore();
970
971 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
972 // on the stack.
973 case Memory:
974
975 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
976 // COMPLEX_X87, it is passed in memory.
977 case X87:
978 case ComplexX87:
Daniel Dunbar245f5532009-02-22 08:17:51 +0000979 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000980
981 case SSEUp:
982 case X87Up:
983 assert(0 && "Invalid classification for lo word.");
984
985 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
986 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
987 // and %r9 is used.
988 case Integer:
989 ++neededInt;
990 ResType = llvm::Type::Int64Ty;
991 break;
992
993 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
994 // available SSE register is used, the registers are taken in the
995 // order from %xmm0 to %xmm7.
996 case SSE:
997 ++neededSSE;
998 ResType = llvm::Type::DoubleTy;
999 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001000 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001001
1002 switch (Hi) {
1003 // Memory was handled previously, ComplexX87 and X87 should
1004 // never occur as hi classes, and X87Up must be preceed by X87,
1005 // which is passed in memory.
1006 case Memory:
1007 case X87:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001008 case ComplexX87:
1009 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar100f4022009-03-06 17:50:25 +00001010 break;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001011
1012 case NoClass: break;
1013 case Integer:
1014 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1015 ++neededInt;
1016 break;
Daniel Dunbar100f4022009-03-06 17:50:25 +00001017
1018 // X87Up generally doesn't occur here (long double is passed in
1019 // memory), except in situations involving unions.
1020 case X87Up:
1021 case SSE:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001022 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1023 ++neededSSE;
1024 break;
1025
1026 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1027 // eightbyte is passed in the upper half of the last used SSE
1028 // register.
1029 case SSEUp:
1030 assert(Lo == SSE && "Unexpected SSEUp classification.");
1031 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1032 break;
1033 }
1034
Daniel Dunbar644f4c32009-02-14 02:09:24 +00001035 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001036}
1037
1038void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1039 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1040
1041 // Keep track of the number of assigned registers.
1042 unsigned freeIntRegs = 6, freeSSERegs = 8;
1043
1044 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1045 // get assigned (in left-to-right order) for passing as follows...
1046 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001047 it != ie; ++it) {
1048 unsigned neededInt, neededSSE;
1049 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1050
1051 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1052 // eightbyte of an argument, the whole argument is passed on the
1053 // stack. If registers have already been assigned for some
1054 // eightbytes of such an argument, the assignments get reverted.
1055 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1056 freeIntRegs -= neededInt;
1057 freeSSERegs -= neededSSE;
1058 } else {
Daniel Dunbar245f5532009-02-22 08:17:51 +00001059 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001060 }
1061 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001062}
1063
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001064static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1065 QualType Ty,
1066 CodeGenFunction &CGF) {
1067 llvm::Value *overflow_arg_area_p =
1068 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1069 llvm::Value *overflow_arg_area =
1070 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1071
1072 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1073 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001074 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001075 if (Align > 8) {
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001076 // Note that we follow the ABI & gcc here, even though the type
1077 // could in theory have an alignment greater than 16. This case
1078 // shouldn't ever matter in practice.
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001079
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001080 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1081 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1082 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1083 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1084 llvm::Type::Int64Ty);
1085 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1086 overflow_arg_area =
1087 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1088 overflow_arg_area->getType(),
1089 "overflow_arg_area.align");
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001090 }
1091
1092 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1093 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1094 llvm::Value *Res =
1095 CGF.Builder.CreateBitCast(overflow_arg_area,
1096 llvm::PointerType::getUnqual(LTy));
1097
1098 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1099 // l->overflow_arg_area + sizeof(type).
1100 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1101 // an 8 byte boundary.
1102
1103 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1104 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1105 (SizeInBytes + 7) & ~7);
1106 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1107 "overflow_arg_area.next");
1108 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1109
1110 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1111 return Res;
1112}
1113
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001114llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1115 CodeGenFunction &CGF) const {
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001116 // Assume that va_list type is correct; should be pointer to LLVM type:
1117 // struct {
1118 // i32 gp_offset;
1119 // i32 fp_offset;
1120 // i8* overflow_arg_area;
1121 // i8* reg_save_area;
1122 // };
1123 unsigned neededInt, neededSSE;
1124 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1125 neededInt, neededSSE);
1126
1127 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1128 // in the registers. If not go to step 7.
1129 if (!neededInt && !neededSSE)
1130 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1131
1132 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1133 // general purpose registers needed to pass type and num_fp to hold
1134 // the number of floating point registers needed.
1135
1136 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1137 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1138 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1139 //
1140 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1141 // register save space).
1142
1143 llvm::Value *InRegs = 0;
1144 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1145 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1146 if (neededInt) {
1147 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1148 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1149 InRegs =
1150 CGF.Builder.CreateICmpULE(gp_offset,
1151 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1152 48 - neededInt * 8),
1153 "fits_in_gp");
1154 }
1155
1156 if (neededSSE) {
1157 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1158 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1159 llvm::Value *FitsInFP =
1160 CGF.Builder.CreateICmpULE(fp_offset,
1161 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar90dafa12009-02-18 22:19:44 +00001162 176 - neededSSE * 16),
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001163 "fits_in_fp");
Daniel Dunbarf2313462009-02-18 22:05:01 +00001164 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001165 }
1166
1167 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1168 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1169 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1170 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1171
1172 // Emit code to load the value if it was passed in registers.
1173
1174 CGF.EmitBlock(InRegBlock);
1175
1176 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1177 // an offset of l->gp_offset and/or l->fp_offset. This may require
1178 // copying to a temporary location in case the parameter is passed
1179 // in different register classes or requires an alignment greater
1180 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001181 //
1182 // FIXME: This really results in shameful code when we end up
1183 // needing to collect arguments from different places; often what
1184 // should result in a simple assembling of a structure from
1185 // scattered addresses has many more loads than necessary. Can we
1186 // clean this up?
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001187 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1188 llvm::Value *RegAddr =
1189 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1190 "reg_save_area");
1191 if (neededInt && neededSSE) {
Daniel Dunbar55e5d892009-02-13 17:46:31 +00001192 // FIXME: Cleanup.
1193 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1194 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1195 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1196 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1197 const llvm::Type *TyLo = ST->getElementType(0);
1198 const llvm::Type *TyHi = ST->getElementType(1);
1199 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1200 "Unexpected ABI info for mixed regs");
1201 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1202 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1203 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1204 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1205 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1206 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1207 llvm::Value *V =
1208 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1209 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1210 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1211 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1212
1213 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001214 } else if (neededInt) {
1215 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1216 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1217 llvm::PointerType::getUnqual(LTy));
1218 } else {
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001219 if (neededSSE == 1) {
1220 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1221 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1222 llvm::PointerType::getUnqual(LTy));
1223 } else {
1224 assert(neededSSE == 2 && "Invalid number of needed registers!");
1225 // SSE registers are spaced 16 bytes apart in the register save
1226 // area, we need to collect the two eightbytes together.
1227 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1228 llvm::Value *RegAddrHi =
1229 CGF.Builder.CreateGEP(RegAddrLo,
1230 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1231 const llvm::Type *DblPtrTy =
1232 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1233 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1234 llvm::Type::DoubleTy,
1235 NULL);
1236 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1237 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1238 DblPtrTy));
1239 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1240 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1241 DblPtrTy));
1242 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1243 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1244 llvm::PointerType::getUnqual(LTy));
1245 }
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001246 }
1247
1248 // AMD64-ABI 3.5.7p5: Step 5. Set:
1249 // l->gp_offset = l->gp_offset + num_gp * 8
1250 // l->fp_offset = l->fp_offset + num_fp * 16.
1251 if (neededInt) {
1252 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1253 neededInt * 8);
1254 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1255 gp_offset_p);
1256 }
1257 if (neededSSE) {
1258 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1259 neededSSE * 16);
1260 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1261 fp_offset_p);
1262 }
1263 CGF.EmitBranch(ContBlock);
1264
1265 // Emit code to load the value if it was passed in memory.
1266
1267 CGF.EmitBlock(InMemBlock);
1268 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1269
1270 // Return the appropriate result.
1271
1272 CGF.EmitBlock(ContBlock);
1273 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1274 "vaarg.addr");
1275 ResAddr->reserveOperandSpace(2);
1276 ResAddr->addIncoming(RegAddr, InRegBlock);
1277 ResAddr->addIncoming(MemAddr, InMemBlock);
1278
1279 return ResAddr;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001280}
1281
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001282// ABI Info for PIC16
1283class PIC16ABIInfo : public ABIInfo {
1284 ABIArgInfo classifyReturnType(QualType RetTy,
1285 ASTContext &Context) const;
1286
1287 ABIArgInfo classifyArgumentType(QualType RetTy,
1288 ASTContext &Context) const;
1289
1290 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1291 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1292 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1293 it != ie; ++it)
1294 it->info = classifyArgumentType(it->type, Context);
1295 }
1296
1297 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1298 CodeGenFunction &CGF) const;
1299
1300};
1301
1302ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1303 ASTContext &Context) const {
1304 if (RetTy->isVoidType()) {
1305 return ABIArgInfo::getIgnore();
1306 } else {
1307 return ABIArgInfo::getDirect();
1308 }
1309}
1310
1311ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1312 ASTContext &Context) const {
1313 return ABIArgInfo::getDirect();
1314}
1315
1316llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1317 CodeGenFunction &CGF) const {
1318 return 0;
1319}
1320
Eli Friedmana027ea92009-03-29 00:15:25 +00001321class ARMABIInfo : public ABIInfo {
1322 ABIArgInfo classifyReturnType(QualType RetTy,
1323 ASTContext &Context) const;
1324
1325 ABIArgInfo classifyArgumentType(QualType RetTy,
1326 ASTContext &Context) const;
1327
1328 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1329
1330 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1331 CodeGenFunction &CGF) const;
1332};
1333
1334void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1335 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1336 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1337 it != ie; ++it) {
1338 it->info = classifyArgumentType(it->type, Context);
1339 }
1340}
1341
1342ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1343 ASTContext &Context) const {
1344 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1345 return ABIArgInfo::getDirect();
1346 }
1347 // FIXME: This is kind of nasty... but there isn't much choice
1348 // because the ARM backend doesn't support byval.
1349 // FIXME: This doesn't handle alignment > 64 bits.
1350 const llvm::Type* ElemTy;
1351 unsigned SizeRegs;
1352 if (Context.getTypeAlign(Ty) > 32) {
1353 ElemTy = llvm::Type::Int64Ty;
1354 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1355 } else {
1356 ElemTy = llvm::Type::Int32Ty;
1357 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1358 }
1359 std::vector<const llvm::Type*> LLVMFields;
1360 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1361 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1362 return ABIArgInfo::getCoerce(STy);
1363}
1364
1365ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1366 ASTContext &Context) const {
1367 if (RetTy->isVoidType()) {
1368 return ABIArgInfo::getIgnore();
1369 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1370 // Aggregates <= 4 bytes are returned in r0; other aggregates
1371 // are returned indirectly.
1372 uint64_t Size = Context.getTypeSize(RetTy);
1373 if (Size <= 32)
1374 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1375 return ABIArgInfo::getIndirect(0);
1376 } else {
1377 return ABIArgInfo::getDirect();
1378 }
1379}
1380
1381llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1382 CodeGenFunction &CGF) const {
1383 // FIXME: Need to handle alignment
1384 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1385 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1386
1387 CGBuilderTy &Builder = CGF.Builder;
1388 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1389 "ap");
1390 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1391 llvm::Type *PTy =
1392 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1393 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1394
1395 uint64_t Offset =
1396 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1397 llvm::Value *NextAddr =
1398 Builder.CreateGEP(Addr,
1399 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1400 "ap.next");
1401 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1402
1403 return AddrTyped;
1404}
1405
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001406ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001407 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001408 if (RetTy->isVoidType()) {
1409 return ABIArgInfo::getIgnore();
1410 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001411 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001412 } else {
1413 return ABIArgInfo::getDirect();
1414 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001415}
1416
1417ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001418 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001419 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001420 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001421 } else {
1422 return ABIArgInfo::getDirect();
1423 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001424}
1425
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001426llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1427 CodeGenFunction &CGF) const {
1428 return 0;
1429}
1430
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001431const ABIInfo &CodeGenTypes::getABIInfo() const {
1432 if (TheABIInfo)
1433 return *TheABIInfo;
1434
1435 // For now we just cache this in the CodeGenTypes and don't bother
1436 // to free it.
1437 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1438 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman9fd58e82009-03-23 23:26:24 +00001439 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001440 switch (getContext().Target.getPointerWidth(0)) {
1441 case 32:
Douglas Gregor6ab35242009-04-09 21:40:53 +00001442 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001443 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +00001444 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001445 }
Eli Friedmana027ea92009-03-29 00:15:25 +00001446 } else if (strcmp(TargetPrefix, "arm") == 0) {
1447 // FIXME: Support for OABI?
1448 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001449 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1450 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001451 }
1452
1453 return *(TheABIInfo = new DefaultABIInfo);
1454}
1455
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001456/***/
1457
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001458CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1459 const llvm::SmallVector<QualType, 16> &ArgTys) {
1460 NumArgs = ArgTys.size();
1461 Args = new ArgInfo[1 + NumArgs];
1462 Args[0].type = ResTy;
1463 for (unsigned i = 0; i < NumArgs; ++i)
1464 Args[1 + i].type = ArgTys[i];
1465}
1466
1467/***/
1468
Daniel Dunbar56273772008-09-17 00:51:38 +00001469void CodeGenTypes::GetExpandedTypes(QualType Ty,
1470 std::vector<const llvm::Type*> &ArgTys) {
1471 const RecordType *RT = Ty->getAsStructureType();
1472 assert(RT && "Can only expand structure types.");
1473 const RecordDecl *RD = RT->getDecl();
1474 assert(!RD->hasFlexibleArrayMember() &&
1475 "Cannot expand structure with flexible array.");
1476
Douglas Gregor6ab35242009-04-09 21:40:53 +00001477 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1478 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001479 const FieldDecl *FD = *i;
1480 assert(!FD->isBitField() &&
1481 "Cannot expand structure with bit-field members.");
1482
1483 QualType FT = FD->getType();
1484 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1485 GetExpandedTypes(FT, ArgTys);
1486 } else {
1487 ArgTys.push_back(ConvertType(FT));
1488 }
1489 }
1490}
1491
1492llvm::Function::arg_iterator
1493CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1494 llvm::Function::arg_iterator AI) {
1495 const RecordType *RT = Ty->getAsStructureType();
1496 assert(RT && "Can only expand structure types.");
1497
1498 RecordDecl *RD = RT->getDecl();
1499 assert(LV.isSimple() &&
1500 "Unexpected non-simple lvalue during struct expansion.");
1501 llvm::Value *Addr = LV.getAddress();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001502 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1503 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001504 FieldDecl *FD = *i;
1505 QualType FT = FD->getType();
1506
1507 // FIXME: What are the right qualifiers here?
1508 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1509 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1510 AI = ExpandTypeFromArgs(FT, LV, AI);
1511 } else {
1512 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1513 ++AI;
1514 }
1515 }
1516
1517 return AI;
1518}
1519
1520void
1521CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1522 llvm::SmallVector<llvm::Value*, 16> &Args) {
1523 const RecordType *RT = Ty->getAsStructureType();
1524 assert(RT && "Can only expand structure types.");
1525
1526 RecordDecl *RD = RT->getDecl();
1527 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1528 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001529 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1530 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001531 FieldDecl *FD = *i;
1532 QualType FT = FD->getType();
1533
1534 // FIXME: What are the right qualifiers here?
1535 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1536 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1537 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1538 } else {
1539 RValue RV = EmitLoadOfLValue(LV, FT);
1540 assert(RV.isScalar() &&
1541 "Unexpected non-scalar rvalue during struct expansion.");
1542 Args.push_back(RV.getScalarVal());
1543 }
1544 }
1545}
1546
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001547/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1548/// a pointer to an object of type \arg Ty.
1549///
1550/// This safely handles the case when the src type is smaller than the
1551/// destination type; in this situation the values of bits which not
1552/// present in the src are undefined.
1553static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1554 const llvm::Type *Ty,
1555 CodeGenFunction &CGF) {
1556 const llvm::Type *SrcTy =
1557 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1558 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1559 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1560
Daniel Dunbarb225be42009-02-03 05:59:18 +00001561 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001562 if (SrcSize == DstSize) {
1563 llvm::Value *Casted =
1564 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001565 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1566 // FIXME: Use better alignment / avoid requiring aligned load.
1567 Load->setAlignment(1);
1568 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001569 } else {
1570 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1571
1572 // Otherwise do coercion through memory. This is stupid, but
1573 // simple.
1574 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1575 llvm::Value *Casted =
1576 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001577 llvm::StoreInst *Store =
1578 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1579 // FIXME: Use better alignment / avoid requiring aligned store.
1580 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001581 return CGF.Builder.CreateLoad(Tmp);
1582 }
1583}
1584
1585/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1586/// where the source and destination may have different types.
1587///
1588/// This safely handles the case when the src type is larger than the
1589/// destination type; the upper bits of the src will be lost.
1590static void CreateCoercedStore(llvm::Value *Src,
1591 llvm::Value *DstPtr,
1592 CodeGenFunction &CGF) {
1593 const llvm::Type *SrcTy = Src->getType();
1594 const llvm::Type *DstTy =
1595 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1596
1597 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1598 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1599
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001600 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001601 if (SrcSize == DstSize) {
1602 llvm::Value *Casted =
1603 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001604 // FIXME: Use better alignment / avoid requiring aligned store.
1605 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001606 } else {
1607 assert(SrcSize > DstSize && "Coercion is missing bits!");
1608
1609 // Otherwise do coercion through memory. This is stupid, but
1610 // simple.
1611 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1612 CGF.Builder.CreateStore(Src, Tmp);
1613 llvm::Value *Casted =
1614 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001615 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1616 // FIXME: Use better alignment / avoid requiring aligned load.
1617 Load->setAlignment(1);
1618 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001619 }
1620}
1621
Daniel Dunbar56273772008-09-17 00:51:38 +00001622/***/
1623
Daniel Dunbar88b53962009-02-02 22:03:45 +00001624bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001625 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001626}
1627
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001628const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001629CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001630 std::vector<const llvm::Type*> ArgTys;
1631
1632 const llvm::Type *ResultType = 0;
1633
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001634 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001635 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001636 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001637 case ABIArgInfo::Expand:
1638 assert(0 && "Invalid ABI kind for return argument");
1639
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001640 case ABIArgInfo::Direct:
1641 ResultType = ConvertType(RetTy);
1642 break;
1643
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001644 case ABIArgInfo::Indirect: {
1645 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001646 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001647 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001648 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1649 break;
1650 }
1651
Daniel Dunbar11434922009-01-26 21:26:08 +00001652 case ABIArgInfo::Ignore:
1653 ResultType = llvm::Type::VoidTy;
1654 break;
1655
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001656 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001657 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001658 break;
1659 }
1660
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001661 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1662 ie = FI.arg_end(); it != ie; ++it) {
1663 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001664
1665 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001666 case ABIArgInfo::Ignore:
1667 break;
1668
Daniel Dunbar56273772008-09-17 00:51:38 +00001669 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001670 ArgTys.push_back(AI.getCoerceToType());
1671 break;
1672
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001673 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001674 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001675 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1676 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001677 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001678 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001679
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001680 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001681 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001682 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001683
1684 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001685 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001686 break;
1687 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001688 }
1689
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001690 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001691}
1692
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001693void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001694 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001695 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001696 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001697 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001698
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001699 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001700 if (TargetDecl) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001701 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001702 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001703 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001704 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001705 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001706 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001707 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +00001708 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001709 }
1710
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001711 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001712 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001713 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001714 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001715 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001716 if (RetTy->isPromotableIntegerType()) {
1717 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001718 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001719 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001720 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001721 }
1722 }
1723 break;
1724
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001725 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001726 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001727 llvm::Attribute::StructRet |
1728 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001729 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001730 // sret disables readnone and readonly
1731 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1732 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001733 break;
1734
Daniel Dunbar11434922009-01-26 21:26:08 +00001735 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001736 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001737 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001738
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001739 case ABIArgInfo::Expand:
1740 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001741 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001742
Devang Patela2c69122008-09-26 22:53:57 +00001743 if (RetAttrs)
1744 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001745
1746 // FIXME: we need to honour command line settings also...
1747 // FIXME: RegParm should be reduced in case of nested functions and/or global
1748 // register variable.
1749 signed RegParm = 0;
1750 if (TargetDecl)
1751 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1752 RegParm = RegParmAttr->getNumParams();
1753
1754 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001755 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1756 ie = FI.arg_end(); it != ie; ++it) {
1757 QualType ParamType = it->type;
1758 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001759 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001760
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001761 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001762 case ABIArgInfo::Coerce:
1763 break;
1764
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001765 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001766 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001767 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001768 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001769 // byval disables readnone and readonly.
1770 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1771 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001772 break;
1773
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001774 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001775 if (ParamType->isPromotableIntegerType()) {
1776 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001777 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001778 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001779 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001780 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001781 }
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001782 if (RegParm > 0 &&
1783 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1784 RegParm -=
1785 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1786 if (RegParm >= 0)
1787 Attributes |= llvm::Attribute::InReg;
1788 }
1789 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001790 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001791
Daniel Dunbar11434922009-01-26 21:26:08 +00001792 case ABIArgInfo::Ignore:
1793 // Skip increment, no matching LLVM parameter.
1794 continue;
1795
Daniel Dunbar56273772008-09-17 00:51:38 +00001796 case ABIArgInfo::Expand: {
1797 std::vector<const llvm::Type*> Tys;
1798 // FIXME: This is rather inefficient. Do we ever actually need
1799 // to do anything here? The result should be just reconstructed
1800 // on the other side, so extension should be a non-issue.
1801 getTypes().GetExpandedTypes(ParamType, Tys);
1802 Index += Tys.size();
1803 continue;
1804 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001805 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001806
Devang Patel761d7f72008-09-25 21:02:23 +00001807 if (Attributes)
1808 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001809 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001810 }
Devang Patela2c69122008-09-26 22:53:57 +00001811 if (FuncAttrs)
1812 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001813}
1814
Daniel Dunbar88b53962009-02-02 22:03:45 +00001815void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1816 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001817 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001818 // FIXME: We no longer need the types from FunctionArgList; lift up
1819 // and simplify.
1820
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001821 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1822 llvm::Function::arg_iterator AI = Fn->arg_begin();
1823
1824 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001825 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001826 AI->setName("agg.result");
1827 ++AI;
1828 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001829
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001830 assert(FI.arg_size() == Args.size() &&
1831 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001832 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001833 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001834 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001835 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001836 QualType Ty = info_it->type;
1837 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001838
1839 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001840 case ABIArgInfo::Indirect: {
1841 llvm::Value* V = AI;
1842 if (hasAggregateLLVMType(Ty)) {
1843 // Do nothing, aggregates and complex variables are accessed by
1844 // reference.
1845 } else {
1846 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001847 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001848 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1849 // This must be a promotion, for something like
1850 // "void a(x) short x; {..."
1851 V = EmitScalarConversion(V, Ty, Arg->getType());
1852 }
1853 }
1854 EmitParmDecl(*Arg, V);
1855 break;
1856 }
1857
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001858 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001859 assert(AI != Fn->arg_end() && "Argument mismatch!");
1860 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001861 if (hasAggregateLLVMType(Ty)) {
1862 // Create a temporary alloca to hold the argument; the rest of
1863 // codegen expects to access aggregates & complex values by
1864 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001865 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001866 Builder.CreateStore(AI, V);
1867 } else {
1868 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1869 // This must be a promotion, for something like
1870 // "void a(x) short x; {..."
1871 V = EmitScalarConversion(V, Ty, Arg->getType());
1872 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001873 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001874 EmitParmDecl(*Arg, V);
1875 break;
1876 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001877
1878 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001879 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001880 // we need to create a temporary and reconstruct it from the
1881 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001882 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001883 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001884 (Name + ".addr").c_str());
1885 // FIXME: What are the right qualifiers here?
1886 llvm::Function::arg_iterator End =
1887 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1888 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001889
Daniel Dunbar56273772008-09-17 00:51:38 +00001890 // Name the arguments used in expansion and increment AI.
1891 unsigned Index = 0;
1892 for (; AI != End; ++AI, ++Index)
1893 AI->setName(Name + "." + llvm::utostr(Index));
1894 continue;
1895 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001896
1897 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001898 // Initialize the local variable appropriately.
1899 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001900 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001901 } else {
1902 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1903 }
1904
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001905 // Skip increment, no matching LLVM parameter.
1906 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001907
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001908 case ABIArgInfo::Coerce: {
1909 assert(AI != Fn->arg_end() && "Argument mismatch!");
1910 // FIXME: This is very wasteful; EmitParmDecl is just going to
1911 // drop the result in a new alloca anyway, so we could just
1912 // store into that directly if we broke the abstraction down
1913 // more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001914 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001915 CreateCoercedStore(AI, V, *this);
1916 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001917 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001918 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001919 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1920 // This must be a promotion, for something like
1921 // "void a(x) short x; {..."
1922 V = EmitScalarConversion(V, Ty, Arg->getType());
1923 }
1924 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001925 EmitParmDecl(*Arg, V);
1926 break;
1927 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001928 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001929
1930 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001931 }
1932 assert(AI == Fn->arg_end() && "Argument mismatch!");
1933}
1934
Daniel Dunbar88b53962009-02-02 22:03:45 +00001935void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001936 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001937 llvm::Value *RV = 0;
1938
1939 // Functions with no result always return void.
1940 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001941 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001942 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001943
1944 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001945 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001946 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001947 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1948 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1949 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1950 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1951 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001952 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1953 false);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001954 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001955 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001956
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001957 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001958 // The internal return value temp always will have
1959 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001960 RV = Builder.CreateLoad(ReturnValue);
1961 break;
1962
Daniel Dunbar11434922009-01-26 21:26:08 +00001963 case ABIArgInfo::Ignore:
1964 break;
1965
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001966 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001967 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001968 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001969
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001970 case ABIArgInfo::Expand:
1971 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001972 }
1973 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001974
1975 if (RV) {
1976 Builder.CreateRet(RV);
1977 } else {
1978 Builder.CreateRetVoid();
1979 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001980}
1981
Anders Carlsson0139bb92009-04-08 20:47:54 +00001982RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
1983 return EmitAnyExprToTemp(E);
1984}
1985
Daniel Dunbar88b53962009-02-02 22:03:45 +00001986RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1987 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001988 const CallArgList &CallArgs,
1989 const Decl *TargetDecl) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001990 // FIXME: We no longer need the types from CallArgs; lift up and
1991 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001992 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001993
1994 // Handle struct-return functions by passing a pointer to the
1995 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001996 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001997 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00001998 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001999 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002000 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002001 }
2002
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00002003 assert(CallInfo.arg_size() == CallArgs.size() &&
2004 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00002005 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002006 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00002007 I != E; ++I, ++info_it) {
2008 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002009 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00002010
2011 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002012 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00002013 if (RV.isScalar() || RV.isComplex()) {
2014 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002015 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00002016 if (RV.isScalar())
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002017 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00002018 else
2019 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2020 } else {
2021 Args.push_back(RV.getAggregateAddr());
2022 }
2023 break;
2024
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002025 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00002026 if (RV.isScalar()) {
2027 Args.push_back(RV.getScalarVal());
2028 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002029 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2030 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2031 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2032 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00002033 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002034 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00002035 }
2036 break;
2037
Daniel Dunbar11434922009-01-26 21:26:08 +00002038 case ABIArgInfo::Ignore:
2039 break;
2040
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002041 case ABIArgInfo::Coerce: {
2042 // FIXME: Avoid the conversion through memory if possible.
2043 llvm::Value *SrcPtr;
2044 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00002045 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002046 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002047 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002048 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002049 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2050 } else
2051 SrcPtr = RV.getAggregateAddr();
2052 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2053 *this));
2054 break;
2055 }
2056
Daniel Dunbar56273772008-09-17 00:51:38 +00002057 case ABIArgInfo::Expand:
2058 ExpandTypeToArgs(I->second, RV, Args);
2059 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002060 }
2061 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002062
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002063 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel761d7f72008-09-25 21:02:23 +00002064 CodeGen::AttributeListType AttributeList;
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002065 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002066 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2067 AttributeList.end());
Daniel Dunbar725ad312009-01-31 02:19:00 +00002068
Daniel Dunbard14151d2009-03-02 04:32:35 +00002069 llvm::CallSite CS;
2070 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2071 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002072 } else {
2073 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbard14151d2009-03-02 04:32:35 +00002074 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2075 &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002076 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00002077 }
2078
Daniel Dunbard14151d2009-03-02 04:32:35 +00002079 CS.setAttributes(Attrs);
2080 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2081 CS.setCallingConv(F->getCallingConv());
2082
2083 // If the call doesn't return, finish the basic block and clear the
2084 // insertion point; this allows the rest of IRgen to discard
2085 // unreachable code.
2086 if (CS.doesNotReturn()) {
2087 Builder.CreateUnreachable();
2088 Builder.ClearInsertionPoint();
2089
2090 // FIXME: For now, emit a dummy basic block because expr
2091 // emitters in generally are not ready to handle emitting
2092 // expressions at unreachable points.
2093 EnsureInsertPoint();
2094
2095 // Return a reasonable RValue.
2096 return GetUndefRValue(RetTy);
2097 }
2098
2099 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner34030842009-03-22 00:32:22 +00002100 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002101 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002102
2103 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002104 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002105 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00002106 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00002107 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00002108 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00002109 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002110
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002111 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002112 if (RetTy->isAnyComplexType()) {
2113 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2114 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2115 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00002116 }
2117 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002118 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002119 Builder.CreateStore(CI, V);
2120 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002121 }
2122 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002123
Daniel Dunbar11434922009-01-26 21:26:08 +00002124 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00002125 // If we are ignoring an argument that had a result, make sure to
2126 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00002127 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00002128
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002129 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002130 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002131 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00002132 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00002133 if (RetTy->isAnyComplexType())
2134 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner34030842009-03-22 00:32:22 +00002135 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00002136 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002137 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002138 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002139
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002140 case ABIArgInfo::Expand:
2141 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002142 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002143
2144 assert(0 && "Unhandled ABIArgInfo::Kind");
2145 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002146}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00002147
2148/* VarArg handling */
2149
2150llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2151 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2152}