blob: e9f2bba303b7266b1678a7aff57b0460e4b0a786 [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
Daniel Dunbarf7fff322009-05-08 20:21:04 +0000353 // As are arrays of empty structures, but not generally, so we
354 // can't add this test higher in this routine.
355 if (const ConstantArrayType *AT =
356 Context.getAsConstantArrayType(FD->getType()))
357 if (isEmptyRecord(Context, AT->getElementType()))
358 continue;
359
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000360 // Check fields recursively.
361 if (!shouldReturnTypeInRegister(FD->getType(), Context))
362 return false;
363 }
364
365 return true;
366}
367
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000368ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
369 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000370 if (RetTy->isVoidType()) {
371 return ABIArgInfo::getIgnore();
Daniel Dunbar36043162009-04-01 06:13:08 +0000372 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
373 // On Darwin, some vectors are returned in registers.
374 if (IsDarwin) {
375 uint64_t Size = Context.getTypeSize(RetTy);
376
377 // 128-bit vectors are a special case; they are returned in
378 // registers and we need to make sure to pick a type the LLVM
379 // backend will like.
380 if (Size == 128)
381 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
382 2));
383
384 // Always return in register if it fits in a general purpose
385 // register, or if it is 64 bits and has a single element.
386 if ((Size == 8 || Size == 16 || Size == 32) ||
387 (Size == 64 && VT->getNumElements() == 1))
388 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
389
390 return ABIArgInfo::getIndirect(0);
391 }
392
393 return ABIArgInfo::getDirect();
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000394 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8e034442009-04-27 18:31:32 +0000395 // Structures with flexible arrays are always indirect.
396 if (const RecordType *RT = RetTy->getAsStructureType())
397 if (RT->getDecl()->hasFlexibleArrayMember())
398 return ABIArgInfo::getIndirect(0);
399
Eli Friedman9fd58e82009-03-23 23:26:24 +0000400 // Outside of Darwin, structs and unions are always indirect.
401 if (!IsDarwin && !RetTy->isAnyComplexType())
402 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8e034442009-04-27 18:31:32 +0000403
Daniel Dunbar834af452008-09-17 21:22:33 +0000404 // Classify "single element" structs as their element type.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000405 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000406 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
407 // FIXME: This is gross, it would be nice if we could just
408 // pass back SeltTy and have clients deal with it. Is it worth
409 // supporting coerce to both LLVM and clang Types?
410 if (BT->isIntegerType()) {
411 uint64_t Size = Context.getTypeSize(SeltTy);
412 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
413 } else if (BT->getKind() == BuiltinType::Float) {
414 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
415 } else if (BT->getKind() == BuiltinType::Double) {
416 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
417 }
418 } else if (SeltTy->isPointerType()) {
419 // FIXME: It would be really nice if this could come out as
420 // the proper pointer type.
421 llvm::Type *PtrTy =
422 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
423 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000424 } else if (SeltTy->isVectorType()) {
425 // 64- and 128-bit vectors are never returned in a
426 // register when inside a structure.
427 uint64_t Size = Context.getTypeSize(RetTy);
428 if (Size == 64 || Size == 128)
429 return ABIArgInfo::getIndirect(0);
430
431 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000432 }
433 }
434
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000435 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000436 if (isRegisterSize(Size)) {
437 // Always return in register for unions for now.
438 // FIXME: This is wrong, but better than treating as a
439 // structure.
440 if (RetTy->isUnionType())
441 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
442
443 // Small structures which are register sized are generally returned
444 // in a register.
445 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context))
446 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
447 }
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000448
449 return ABIArgInfo::getIndirect(0);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000450 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000451 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000452 }
453}
454
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000455ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000456 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000457 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000458 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000459 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000460 if (const RecordType *RT = Ty->getAsStructureType())
461 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000462 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000463
Daniel Dunbar3170c932009-02-05 01:50:07 +0000464 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000465 uint64_t Size = Context.getTypeSize(Ty);
466 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000467 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000468
469 // Expand structs with size <= 128-bits which consist only of
470 // basic types (int, long long, float, double, xxx*). This is
471 // non-recursive and does not ignore empty fields.
472 if (const RecordType *RT = Ty->getAsStructureType()) {
473 if (Context.getTypeSize(Ty) <= 4*32 &&
474 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
475 return ABIArgInfo::getExpand();
476 }
477
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000478 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000479 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000480 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000481 }
482}
483
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000484llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
485 CodeGenFunction &CGF) const {
486 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
487 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
488
489 CGBuilderTy &Builder = CGF.Builder;
490 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
491 "ap");
492 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
493 llvm::Type *PTy =
494 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
495 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
496
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000497 uint64_t Offset =
498 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000499 llvm::Value *NextAddr =
500 Builder.CreateGEP(Addr,
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000501 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000502 "ap.next");
503 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
504
505 return AddrTyped;
506}
507
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000508namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000509/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000510class X86_64ABIInfo : public ABIInfo {
511 enum Class {
512 Integer = 0,
513 SSE,
514 SSEUp,
515 X87,
516 X87Up,
517 ComplexX87,
518 NoClass,
519 Memory
520 };
521
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000522 /// merge - Implement the X86_64 ABI merging algorithm.
523 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000524 /// Merge an accumulating classification \arg Accum with a field
525 /// classification \arg Field.
526 ///
527 /// \param Accum - The accumulating classification. This should
528 /// always be either NoClass or the result of a previous merge
529 /// call. In addition, this should never be Memory (the caller
530 /// should just return Memory for the aggregate).
531 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000532
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000533 /// classify - Determine the x86_64 register classes in which the
534 /// given type T should be passed.
535 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000536 /// \param Lo - The classification for the parts of the type
537 /// residing in the low word of the containing object.
538 ///
539 /// \param Hi - The classification for the parts of the type
540 /// residing in the high word of the containing object.
541 ///
542 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000543 /// containing object. Some parameters are classified different
544 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000545 ///
546 /// If a word is unused its result will be NoClass; if a type should
547 /// be passed in Memory then at least the classification of \arg Lo
548 /// will be Memory.
549 ///
550 /// The \arg Lo class will be NoClass iff the argument is ignored.
551 ///
552 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000553 /// also be ComplexX87.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000554 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000555 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000556
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000557 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
558 /// to coerce to, chose the best way to pass Ty in the same place
559 /// that \arg CoerceTo would be passed, but while keeping the
560 /// emitted code as simple as possible.
561 ///
562 /// FIXME: Note, this should be cleaned up to just take an
563 /// enumeration of all the ways we might want to pass things,
564 /// instead of constructing an LLVM type. This makes this code more
565 /// explicit, and it makes it clearer that we are also doing this
566 /// for correctness in the case of passing scalar types.
567 ABIArgInfo getCoerceResult(QualType Ty,
568 const llvm::Type *CoerceTo,
569 ASTContext &Context) const;
570
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000571 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000572 ASTContext &Context) const;
573
574 ABIArgInfo classifyArgumentType(QualType Ty,
575 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000576 unsigned &neededInt,
577 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000578
579public:
580 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000581
582 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
583 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000584};
585}
586
Daniel Dunbarc4503572009-01-31 00:06:58 +0000587X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
588 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000589 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
590 // classified recursively so that always two fields are
591 // considered. The resulting class is calculated according to
592 // the classes of the fields in the eightbyte:
593 //
594 // (a) If both classes are equal, this is the resulting class.
595 //
596 // (b) If one of the classes is NO_CLASS, the resulting class is
597 // the other class.
598 //
599 // (c) If one of the classes is MEMORY, the result is the MEMORY
600 // class.
601 //
602 // (d) If one of the classes is INTEGER, the result is the
603 // INTEGER.
604 //
605 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
606 // MEMORY is used as class.
607 //
608 // (f) Otherwise class SSE is used.
Daniel Dunbar100f4022009-03-06 17:50:25 +0000609
610 // Accum should never be memory (we should have returned) or
611 // ComplexX87 (because this cannot be passed in a structure).
612 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbarc4503572009-01-31 00:06:58 +0000613 "Invalid accumulated classification during merge.");
614 if (Accum == Field || Field == NoClass)
615 return Accum;
616 else if (Field == Memory)
617 return Memory;
618 else if (Accum == NoClass)
619 return Field;
620 else if (Accum == Integer || Field == Integer)
621 return Integer;
622 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
623 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000624 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000625 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000626}
627
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000628void X86_64ABIInfo::classify(QualType Ty,
629 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000630 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000631 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000632 // FIXME: This code can be simplified by introducing a simple value
633 // class for Class pairs with appropriate constructor methods for
634 // the various situations.
635
Daniel Dunbare28099b2009-02-22 04:48:22 +0000636 // FIXME: Some of the split computations are wrong; unaligned
637 // vectors shouldn't be passed in registers for example, so there is
638 // no chance they can straddle an eightbyte. Verify & simplify.
639
Daniel Dunbarc4503572009-01-31 00:06:58 +0000640 Lo = Hi = NoClass;
641
642 Class &Current = OffsetBase < 64 ? Lo : Hi;
643 Current = Memory;
644
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000645 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
646 BuiltinType::Kind k = BT->getKind();
647
Daniel Dunbar11434922009-01-26 21:26:08 +0000648 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000649 Current = NoClass;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000650 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattnerae69e002009-04-30 06:22:07 +0000651 Lo = Integer;
652 Hi = Integer;
Daniel Dunbar11434922009-01-26 21:26:08 +0000653 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000654 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000655 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000656 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000657 } else if (k == BuiltinType::LongDouble) {
658 Lo = X87;
659 Hi = X87Up;
660 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000661 // FIXME: _Decimal32 and _Decimal64 are SSE.
662 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson708762b2009-02-26 17:31:15 +0000663 } else if (const EnumType *ET = Ty->getAsEnumType()) {
664 // Classify the underlying integer type.
665 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbar89588912009-02-26 20:52:22 +0000666 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000667 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000668 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000669 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbare28099b2009-02-22 04:48:22 +0000670 if (Size == 32) {
671 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
672 // float> as integer.
673 Current = Integer;
674
675 // If this type crosses an eightbyte boundary, it should be
676 // split.
677 uint64_t EB_Real = (OffsetBase) / 64;
678 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
679 if (EB_Real != EB_Imag)
680 Hi = Lo;
681 } else if (Size == 64) {
Daniel Dunbar0af99292009-02-22 04:16:10 +0000682 // gcc passes <1 x double> in memory. :(
683 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000684 return;
Daniel Dunbar0af99292009-02-22 04:16:10 +0000685
686 // gcc passes <1 x long long> as INTEGER.
687 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
688 Current = Integer;
689 else
690 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000691
692 // If this type crosses an eightbyte boundary, it should be
693 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000694 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000695 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000696 } else if (Size == 128) {
697 Lo = SSE;
698 Hi = SSEUp;
699 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000700 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000701 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000702
Daniel Dunbare33edf12009-01-30 18:40:10 +0000703 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar0af99292009-02-22 04:16:10 +0000704 if (ET->isIntegralType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000705 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000706 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000707 else if (Size <= 128)
708 Lo = Hi = Integer;
709 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000710 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000711 else if (ET == Context.DoubleTy)
712 Lo = Hi = SSE;
713 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000714 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000715
716 // If this complex type crosses an eightbyte boundary then it
717 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000718 uint64_t EB_Real = (OffsetBase) / 64;
719 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000720 if (Hi == NoClass && EB_Real != EB_Imag)
721 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000722 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
723 // Arrays are treated like structures.
724
725 uint64_t Size = Context.getTypeSize(Ty);
726
727 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
728 // than two eightbytes, ..., it has class MEMORY.
729 if (Size > 128)
730 return;
731
732 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
733 // fields, it has class MEMORY.
734 //
735 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000736 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000737 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000738
739 // Otherwise implement simplified merge. We could be smarter about
740 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000741 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000742 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
743 uint64_t ArraySize = AT->getSize().getZExtValue();
744 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
745 Class FieldLo, FieldHi;
746 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000747 Lo = merge(Lo, FieldLo);
748 Hi = merge(Hi, FieldHi);
749 if (Lo == Memory || Hi == Memory)
750 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000751 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000752
753 // Do post merger cleanup (see below). Only case we worry about is Memory.
754 if (Hi == Memory)
755 Lo = Memory;
756 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000757 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000758 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000759
760 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
761 // than two eightbytes, ..., it has class MEMORY.
762 if (Size > 128)
763 return;
764
765 const RecordDecl *RD = RT->getDecl();
766
767 // Assume variable sized types are passed in memory.
768 if (RD->hasFlexibleArrayMember())
769 return;
770
771 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
772
773 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000774 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000775 unsigned idx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000776 for (RecordDecl::field_iterator i = RD->field_begin(Context),
777 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000778 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbardd81d442009-02-17 02:45:44 +0000779 bool BitField = i->isBitField();
Daniel Dunbar99037e52009-01-29 08:13:58 +0000780
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000781 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
782 // fields, it has class MEMORY.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000783 //
Daniel Dunbar8e034442009-04-27 18:31:32 +0000784 // Note, skip this test for bit-fields, see below.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000785 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar99037e52009-01-29 08:13:58 +0000786 Lo = Memory;
787 return;
788 }
789
Daniel Dunbar99037e52009-01-29 08:13:58 +0000790 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000791 //
792 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
793 // exceeds a single eightbyte, each is classified
794 // separately. Each eightbyte gets initialized to class
795 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000796 Class FieldLo, FieldHi;
Daniel Dunbardd81d442009-02-17 02:45:44 +0000797
Daniel Dunbar8e034442009-04-27 18:31:32 +0000798 // Bit-fields require special handling, they do not force the
Daniel Dunbardd81d442009-02-17 02:45:44 +0000799 // structure to be passed in memory even if unaligned, and
800 // therefore they can straddle an eightbyte.
801 if (BitField) {
802 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman9a901bb2009-04-26 19:19:15 +0000803 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbardd81d442009-02-17 02:45:44 +0000804
805 uint64_t EB_Lo = Offset / 64;
806 uint64_t EB_Hi = (Offset + Size - 1) / 64;
807 FieldLo = FieldHi = NoClass;
808 if (EB_Lo) {
809 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
810 FieldLo = NoClass;
811 FieldHi = Integer;
812 } else {
813 FieldLo = Integer;
814 FieldHi = EB_Hi ? Integer : NoClass;
815 }
816 } else
817 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000818 Lo = merge(Lo, FieldLo);
819 Hi = merge(Hi, FieldHi);
820 if (Lo == Memory || Hi == Memory)
821 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000822 }
823
824 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
825 //
826 // (a) If one of the classes is MEMORY, the whole argument is
827 // passed in memory.
828 //
829 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
830
831 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000832 // the merge (just bail).
833 //
834 // The second condition occurs in the case of unions; for example
835 // union { _Complex double; unsigned; }.
836 if (Hi == Memory)
837 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000838 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000839 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000840 }
841}
842
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000843ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
844 const llvm::Type *CoerceTo,
845 ASTContext &Context) const {
846 if (CoerceTo == llvm::Type::Int64Ty) {
847 // Integer and pointer types will end up in a general purpose
848 // register.
Daniel Dunbar0af99292009-02-22 04:16:10 +0000849 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000850 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000851
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000852 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000853 // FIXME: It would probably be better to make CGFunctionInfo only
854 // map using canonical types than to canonize here.
855 QualType CTy = Context.getCanonicalType(Ty);
856
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000857 // Float and double end up in a single SSE reg.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000858 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000859 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000860
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000861 }
862
863 return ABIArgInfo::getCoerce(CoerceTo);
864}
Daniel Dunbarc4503572009-01-31 00:06:58 +0000865
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000866ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
867 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000868 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
869 // classification algorithm.
870 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000871 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000872
Daniel Dunbarc4503572009-01-31 00:06:58 +0000873 // Check some invariants.
874 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
875 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
876 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
877
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000878 const llvm::Type *ResType = 0;
879 switch (Lo) {
880 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000881 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000882
883 case SSEUp:
884 case X87Up:
885 assert(0 && "Invalid classification for lo word.");
886
Daniel Dunbarc4503572009-01-31 00:06:58 +0000887 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000888 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000889 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000890 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000891
892 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
893 // available register of the sequence %rax, %rdx is used.
894 case Integer:
895 ResType = llvm::Type::Int64Ty; break;
896
897 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
898 // available SSE register of the sequence %xmm0, %xmm1 is used.
899 case SSE:
900 ResType = llvm::Type::DoubleTy; break;
901
902 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
903 // returned on the X87 stack in %st0 as 80-bit x87 number.
904 case X87:
905 ResType = llvm::Type::X86_FP80Ty; break;
906
Daniel Dunbarc4503572009-01-31 00:06:58 +0000907 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
908 // part of the value is returned in %st0 and the imaginary part in
909 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000910 case ComplexX87:
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000911 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar3e030b42009-02-18 03:44:19 +0000912 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
913 llvm::Type::X86_FP80Ty,
914 NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000915 break;
916 }
917
918 switch (Hi) {
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000919 // Memory was handled previously and X87 should
920 // never occur as a hi class.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000921 case Memory:
922 case X87:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000923 assert(0 && "Invalid classification for hi word.");
924
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000925 case ComplexX87: // Previously handled.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000926 case NoClass: break;
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000927
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000928 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000929 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
930 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000931 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000932 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
933 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000934
935 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
936 // is passed in the upper half of the last used SSE register.
937 //
938 // SSEUP should always be preceeded by SSE, just widen.
939 case SSEUp:
940 assert(Lo == SSE && "Unexpected SSEUp classification.");
941 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
942 break;
943
944 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000945 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000946 case X87Up:
Daniel Dunbar100f4022009-03-06 17:50:25 +0000947 // If X87Up is preceeded by X87, we don't need to do
948 // anything. However, in some cases with unions it may not be
949 // preceeded by X87. In such situations we follow gcc and pass the
950 // extra bits in an SSE reg.
951 if (Lo != X87)
952 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000953 break;
954 }
955
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000956 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000957}
958
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000959ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000960 unsigned &neededInt,
961 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000962 X86_64ABIInfo::Class Lo, Hi;
963 classify(Ty, Context, 0, Lo, Hi);
964
965 // Check some invariants.
966 // FIXME: Enforce these by construction.
967 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
968 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
969 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
970
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000971 neededInt = 0;
972 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000973 const llvm::Type *ResType = 0;
974 switch (Lo) {
975 case NoClass:
976 return ABIArgInfo::getIgnore();
977
978 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
979 // on the stack.
980 case Memory:
981
982 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
983 // COMPLEX_X87, it is passed in memory.
984 case X87:
985 case ComplexX87:
Daniel Dunbar245f5532009-02-22 08:17:51 +0000986 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000987
988 case SSEUp:
989 case X87Up:
990 assert(0 && "Invalid classification for lo word.");
991
992 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
993 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
994 // and %r9 is used.
995 case Integer:
996 ++neededInt;
997 ResType = llvm::Type::Int64Ty;
998 break;
999
1000 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1001 // available SSE register is used, the registers are taken in the
1002 // order from %xmm0 to %xmm7.
1003 case SSE:
1004 ++neededSSE;
1005 ResType = llvm::Type::DoubleTy;
1006 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001007 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001008
1009 switch (Hi) {
1010 // Memory was handled previously, ComplexX87 and X87 should
1011 // never occur as hi classes, and X87Up must be preceed by X87,
1012 // which is passed in memory.
1013 case Memory:
1014 case X87:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001015 case ComplexX87:
1016 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar100f4022009-03-06 17:50:25 +00001017 break;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001018
1019 case NoClass: break;
1020 case Integer:
1021 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1022 ++neededInt;
1023 break;
Daniel Dunbar100f4022009-03-06 17:50:25 +00001024
1025 // X87Up generally doesn't occur here (long double is passed in
1026 // memory), except in situations involving unions.
1027 case X87Up:
1028 case SSE:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001029 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1030 ++neededSSE;
1031 break;
1032
1033 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1034 // eightbyte is passed in the upper half of the last used SSE
1035 // register.
1036 case SSEUp:
1037 assert(Lo == SSE && "Unexpected SSEUp classification.");
1038 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1039 break;
1040 }
1041
Daniel Dunbar644f4c32009-02-14 02:09:24 +00001042 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001043}
1044
1045void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1046 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1047
1048 // Keep track of the number of assigned registers.
1049 unsigned freeIntRegs = 6, freeSSERegs = 8;
1050
1051 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1052 // get assigned (in left-to-right order) for passing as follows...
1053 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001054 it != ie; ++it) {
1055 unsigned neededInt, neededSSE;
1056 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1057
1058 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1059 // eightbyte of an argument, the whole argument is passed on the
1060 // stack. If registers have already been assigned for some
1061 // eightbytes of such an argument, the assignments get reverted.
1062 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1063 freeIntRegs -= neededInt;
1064 freeSSERegs -= neededSSE;
1065 } else {
Daniel Dunbar245f5532009-02-22 08:17:51 +00001066 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001067 }
1068 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001069}
1070
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001071static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1072 QualType Ty,
1073 CodeGenFunction &CGF) {
1074 llvm::Value *overflow_arg_area_p =
1075 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1076 llvm::Value *overflow_arg_area =
1077 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1078
1079 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1080 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001081 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001082 if (Align > 8) {
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001083 // Note that we follow the ABI & gcc here, even though the type
1084 // could in theory have an alignment greater than 16. This case
1085 // shouldn't ever matter in practice.
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001086
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001087 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1088 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1089 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1090 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1091 llvm::Type::Int64Ty);
1092 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1093 overflow_arg_area =
1094 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1095 overflow_arg_area->getType(),
1096 "overflow_arg_area.align");
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001097 }
1098
1099 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1100 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1101 llvm::Value *Res =
1102 CGF.Builder.CreateBitCast(overflow_arg_area,
1103 llvm::PointerType::getUnqual(LTy));
1104
1105 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1106 // l->overflow_arg_area + sizeof(type).
1107 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1108 // an 8 byte boundary.
1109
1110 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1111 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1112 (SizeInBytes + 7) & ~7);
1113 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1114 "overflow_arg_area.next");
1115 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1116
1117 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1118 return Res;
1119}
1120
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001121llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1122 CodeGenFunction &CGF) const {
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001123 // Assume that va_list type is correct; should be pointer to LLVM type:
1124 // struct {
1125 // i32 gp_offset;
1126 // i32 fp_offset;
1127 // i8* overflow_arg_area;
1128 // i8* reg_save_area;
1129 // };
1130 unsigned neededInt, neededSSE;
1131 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1132 neededInt, neededSSE);
1133
1134 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1135 // in the registers. If not go to step 7.
1136 if (!neededInt && !neededSSE)
1137 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1138
1139 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1140 // general purpose registers needed to pass type and num_fp to hold
1141 // the number of floating point registers needed.
1142
1143 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1144 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1145 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1146 //
1147 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1148 // register save space).
1149
1150 llvm::Value *InRegs = 0;
1151 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1152 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1153 if (neededInt) {
1154 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1155 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1156 InRegs =
1157 CGF.Builder.CreateICmpULE(gp_offset,
1158 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1159 48 - neededInt * 8),
1160 "fits_in_gp");
1161 }
1162
1163 if (neededSSE) {
1164 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1165 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1166 llvm::Value *FitsInFP =
1167 CGF.Builder.CreateICmpULE(fp_offset,
1168 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar90dafa12009-02-18 22:19:44 +00001169 176 - neededSSE * 16),
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001170 "fits_in_fp");
Daniel Dunbarf2313462009-02-18 22:05:01 +00001171 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001172 }
1173
1174 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1175 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1176 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1177 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1178
1179 // Emit code to load the value if it was passed in registers.
1180
1181 CGF.EmitBlock(InRegBlock);
1182
1183 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1184 // an offset of l->gp_offset and/or l->fp_offset. This may require
1185 // copying to a temporary location in case the parameter is passed
1186 // in different register classes or requires an alignment greater
1187 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001188 //
1189 // FIXME: This really results in shameful code when we end up
1190 // needing to collect arguments from different places; often what
1191 // should result in a simple assembling of a structure from
1192 // scattered addresses has many more loads than necessary. Can we
1193 // clean this up?
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001194 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1195 llvm::Value *RegAddr =
1196 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1197 "reg_save_area");
1198 if (neededInt && neededSSE) {
Daniel Dunbar55e5d892009-02-13 17:46:31 +00001199 // FIXME: Cleanup.
1200 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1201 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1202 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1203 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1204 const llvm::Type *TyLo = ST->getElementType(0);
1205 const llvm::Type *TyHi = ST->getElementType(1);
1206 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1207 "Unexpected ABI info for mixed regs");
1208 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1209 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1210 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1211 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1212 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1213 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1214 llvm::Value *V =
1215 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1216 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1217 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1218 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1219
1220 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001221 } else if (neededInt) {
1222 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1223 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1224 llvm::PointerType::getUnqual(LTy));
1225 } else {
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001226 if (neededSSE == 1) {
1227 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1228 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1229 llvm::PointerType::getUnqual(LTy));
1230 } else {
1231 assert(neededSSE == 2 && "Invalid number of needed registers!");
1232 // SSE registers are spaced 16 bytes apart in the register save
1233 // area, we need to collect the two eightbytes together.
1234 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1235 llvm::Value *RegAddrHi =
1236 CGF.Builder.CreateGEP(RegAddrLo,
1237 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1238 const llvm::Type *DblPtrTy =
1239 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1240 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1241 llvm::Type::DoubleTy,
1242 NULL);
1243 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1244 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1245 DblPtrTy));
1246 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1247 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1248 DblPtrTy));
1249 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1250 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1251 llvm::PointerType::getUnqual(LTy));
1252 }
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001253 }
1254
1255 // AMD64-ABI 3.5.7p5: Step 5. Set:
1256 // l->gp_offset = l->gp_offset + num_gp * 8
1257 // l->fp_offset = l->fp_offset + num_fp * 16.
1258 if (neededInt) {
1259 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1260 neededInt * 8);
1261 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1262 gp_offset_p);
1263 }
1264 if (neededSSE) {
1265 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1266 neededSSE * 16);
1267 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1268 fp_offset_p);
1269 }
1270 CGF.EmitBranch(ContBlock);
1271
1272 // Emit code to load the value if it was passed in memory.
1273
1274 CGF.EmitBlock(InMemBlock);
1275 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1276
1277 // Return the appropriate result.
1278
1279 CGF.EmitBlock(ContBlock);
1280 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1281 "vaarg.addr");
1282 ResAddr->reserveOperandSpace(2);
1283 ResAddr->addIncoming(RegAddr, InRegBlock);
1284 ResAddr->addIncoming(MemAddr, InMemBlock);
1285
1286 return ResAddr;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001287}
1288
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001289// ABI Info for PIC16
1290class PIC16ABIInfo : public ABIInfo {
1291 ABIArgInfo classifyReturnType(QualType RetTy,
1292 ASTContext &Context) const;
1293
1294 ABIArgInfo classifyArgumentType(QualType RetTy,
1295 ASTContext &Context) const;
1296
1297 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1298 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1299 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1300 it != ie; ++it)
1301 it->info = classifyArgumentType(it->type, Context);
1302 }
1303
1304 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1305 CodeGenFunction &CGF) const;
1306
1307};
1308
1309ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1310 ASTContext &Context) const {
1311 if (RetTy->isVoidType()) {
1312 return ABIArgInfo::getIgnore();
1313 } else {
1314 return ABIArgInfo::getDirect();
1315 }
1316}
1317
1318ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1319 ASTContext &Context) const {
1320 return ABIArgInfo::getDirect();
1321}
1322
1323llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1324 CodeGenFunction &CGF) const {
1325 return 0;
1326}
1327
Eli Friedmana027ea92009-03-29 00:15:25 +00001328class ARMABIInfo : public ABIInfo {
1329 ABIArgInfo classifyReturnType(QualType RetTy,
1330 ASTContext &Context) const;
1331
1332 ABIArgInfo classifyArgumentType(QualType RetTy,
1333 ASTContext &Context) const;
1334
1335 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1336
1337 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1338 CodeGenFunction &CGF) const;
1339};
1340
1341void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1342 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1343 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1344 it != ie; ++it) {
1345 it->info = classifyArgumentType(it->type, Context);
1346 }
1347}
1348
1349ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1350 ASTContext &Context) const {
1351 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1352 return ABIArgInfo::getDirect();
1353 }
1354 // FIXME: This is kind of nasty... but there isn't much choice
1355 // because the ARM backend doesn't support byval.
1356 // FIXME: This doesn't handle alignment > 64 bits.
1357 const llvm::Type* ElemTy;
1358 unsigned SizeRegs;
1359 if (Context.getTypeAlign(Ty) > 32) {
1360 ElemTy = llvm::Type::Int64Ty;
1361 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1362 } else {
1363 ElemTy = llvm::Type::Int32Ty;
1364 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1365 }
1366 std::vector<const llvm::Type*> LLVMFields;
1367 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1368 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1369 return ABIArgInfo::getCoerce(STy);
1370}
1371
1372ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1373 ASTContext &Context) const {
1374 if (RetTy->isVoidType()) {
1375 return ABIArgInfo::getIgnore();
1376 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1377 // Aggregates <= 4 bytes are returned in r0; other aggregates
1378 // are returned indirectly.
1379 uint64_t Size = Context.getTypeSize(RetTy);
1380 if (Size <= 32)
1381 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1382 return ABIArgInfo::getIndirect(0);
1383 } else {
1384 return ABIArgInfo::getDirect();
1385 }
1386}
1387
1388llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1389 CodeGenFunction &CGF) const {
1390 // FIXME: Need to handle alignment
1391 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1392 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1393
1394 CGBuilderTy &Builder = CGF.Builder;
1395 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1396 "ap");
1397 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1398 llvm::Type *PTy =
1399 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1400 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1401
1402 uint64_t Offset =
1403 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1404 llvm::Value *NextAddr =
1405 Builder.CreateGEP(Addr,
1406 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1407 "ap.next");
1408 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1409
1410 return AddrTyped;
1411}
1412
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001413ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001414 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001415 if (RetTy->isVoidType()) {
1416 return ABIArgInfo::getIgnore();
1417 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001418 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001419 } else {
1420 return ABIArgInfo::getDirect();
1421 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001422}
1423
1424ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001425 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001426 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001427 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001428 } else {
1429 return ABIArgInfo::getDirect();
1430 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001431}
1432
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001433llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1434 CodeGenFunction &CGF) const {
1435 return 0;
1436}
1437
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001438const ABIInfo &CodeGenTypes::getABIInfo() const {
1439 if (TheABIInfo)
1440 return *TheABIInfo;
1441
1442 // For now we just cache this in the CodeGenTypes and don't bother
1443 // to free it.
1444 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1445 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman9fd58e82009-03-23 23:26:24 +00001446 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001447 switch (getContext().Target.getPointerWidth(0)) {
1448 case 32:
Douglas Gregor6ab35242009-04-09 21:40:53 +00001449 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001450 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +00001451 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001452 }
Eli Friedmana027ea92009-03-29 00:15:25 +00001453 } else if (strcmp(TargetPrefix, "arm") == 0) {
1454 // FIXME: Support for OABI?
1455 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001456 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1457 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001458 }
1459
1460 return *(TheABIInfo = new DefaultABIInfo);
1461}
1462
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001463/***/
1464
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001465CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1466 const llvm::SmallVector<QualType, 16> &ArgTys) {
1467 NumArgs = ArgTys.size();
1468 Args = new ArgInfo[1 + NumArgs];
1469 Args[0].type = ResTy;
1470 for (unsigned i = 0; i < NumArgs; ++i)
1471 Args[1 + i].type = ArgTys[i];
1472}
1473
1474/***/
1475
Daniel Dunbar56273772008-09-17 00:51:38 +00001476void CodeGenTypes::GetExpandedTypes(QualType Ty,
1477 std::vector<const llvm::Type*> &ArgTys) {
1478 const RecordType *RT = Ty->getAsStructureType();
1479 assert(RT && "Can only expand structure types.");
1480 const RecordDecl *RD = RT->getDecl();
1481 assert(!RD->hasFlexibleArrayMember() &&
1482 "Cannot expand structure with flexible array.");
1483
Douglas Gregor6ab35242009-04-09 21:40:53 +00001484 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1485 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001486 const FieldDecl *FD = *i;
1487 assert(!FD->isBitField() &&
1488 "Cannot expand structure with bit-field members.");
1489
1490 QualType FT = FD->getType();
1491 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1492 GetExpandedTypes(FT, ArgTys);
1493 } else {
1494 ArgTys.push_back(ConvertType(FT));
1495 }
1496 }
1497}
1498
1499llvm::Function::arg_iterator
1500CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1501 llvm::Function::arg_iterator AI) {
1502 const RecordType *RT = Ty->getAsStructureType();
1503 assert(RT && "Can only expand structure types.");
1504
1505 RecordDecl *RD = RT->getDecl();
1506 assert(LV.isSimple() &&
1507 "Unexpected non-simple lvalue during struct expansion.");
1508 llvm::Value *Addr = LV.getAddress();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001509 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1510 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001511 FieldDecl *FD = *i;
1512 QualType FT = FD->getType();
1513
1514 // FIXME: What are the right qualifiers here?
1515 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1516 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1517 AI = ExpandTypeFromArgs(FT, LV, AI);
1518 } else {
1519 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1520 ++AI;
1521 }
1522 }
1523
1524 return AI;
1525}
1526
1527void
1528CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1529 llvm::SmallVector<llvm::Value*, 16> &Args) {
1530 const RecordType *RT = Ty->getAsStructureType();
1531 assert(RT && "Can only expand structure types.");
1532
1533 RecordDecl *RD = RT->getDecl();
1534 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1535 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001536 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1537 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001538 FieldDecl *FD = *i;
1539 QualType FT = FD->getType();
1540
1541 // FIXME: What are the right qualifiers here?
1542 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1543 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1544 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1545 } else {
1546 RValue RV = EmitLoadOfLValue(LV, FT);
1547 assert(RV.isScalar() &&
1548 "Unexpected non-scalar rvalue during struct expansion.");
1549 Args.push_back(RV.getScalarVal());
1550 }
1551 }
1552}
1553
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001554/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1555/// a pointer to an object of type \arg Ty.
1556///
1557/// This safely handles the case when the src type is smaller than the
1558/// destination type; in this situation the values of bits which not
1559/// present in the src are undefined.
1560static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1561 const llvm::Type *Ty,
1562 CodeGenFunction &CGF) {
1563 const llvm::Type *SrcTy =
1564 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1565 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1566 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1567
Daniel Dunbarb225be42009-02-03 05:59:18 +00001568 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001569 if (SrcSize == DstSize) {
1570 llvm::Value *Casted =
1571 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001572 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1573 // FIXME: Use better alignment / avoid requiring aligned load.
1574 Load->setAlignment(1);
1575 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001576 } else {
1577 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1578
1579 // Otherwise do coercion through memory. This is stupid, but
1580 // simple.
1581 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1582 llvm::Value *Casted =
1583 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001584 llvm::StoreInst *Store =
1585 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1586 // FIXME: Use better alignment / avoid requiring aligned store.
1587 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001588 return CGF.Builder.CreateLoad(Tmp);
1589 }
1590}
1591
1592/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1593/// where the source and destination may have different types.
1594///
1595/// This safely handles the case when the src type is larger than the
1596/// destination type; the upper bits of the src will be lost.
1597static void CreateCoercedStore(llvm::Value *Src,
1598 llvm::Value *DstPtr,
1599 CodeGenFunction &CGF) {
1600 const llvm::Type *SrcTy = Src->getType();
1601 const llvm::Type *DstTy =
1602 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1603
1604 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1605 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1606
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001607 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001608 if (SrcSize == DstSize) {
1609 llvm::Value *Casted =
1610 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001611 // FIXME: Use better alignment / avoid requiring aligned store.
1612 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001613 } else {
1614 assert(SrcSize > DstSize && "Coercion is missing bits!");
1615
1616 // Otherwise do coercion through memory. This is stupid, but
1617 // simple.
1618 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1619 CGF.Builder.CreateStore(Src, Tmp);
1620 llvm::Value *Casted =
1621 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001622 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1623 // FIXME: Use better alignment / avoid requiring aligned load.
1624 Load->setAlignment(1);
1625 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001626 }
1627}
1628
Daniel Dunbar56273772008-09-17 00:51:38 +00001629/***/
1630
Daniel Dunbar88b53962009-02-02 22:03:45 +00001631bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001632 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001633}
1634
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001635const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001636CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001637 std::vector<const llvm::Type*> ArgTys;
1638
1639 const llvm::Type *ResultType = 0;
1640
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001641 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001642 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001643 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001644 case ABIArgInfo::Expand:
1645 assert(0 && "Invalid ABI kind for return argument");
1646
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001647 case ABIArgInfo::Direct:
1648 ResultType = ConvertType(RetTy);
1649 break;
1650
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001651 case ABIArgInfo::Indirect: {
1652 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001653 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001654 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001655 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1656 break;
1657 }
1658
Daniel Dunbar11434922009-01-26 21:26:08 +00001659 case ABIArgInfo::Ignore:
1660 ResultType = llvm::Type::VoidTy;
1661 break;
1662
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001663 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001664 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001665 break;
1666 }
1667
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001668 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1669 ie = FI.arg_end(); it != ie; ++it) {
1670 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001671
1672 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001673 case ABIArgInfo::Ignore:
1674 break;
1675
Daniel Dunbar56273772008-09-17 00:51:38 +00001676 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001677 ArgTys.push_back(AI.getCoerceToType());
1678 break;
1679
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001680 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001681 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001682 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1683 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001684 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001685 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001686
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001687 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001688 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001689 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001690
1691 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001692 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001693 break;
1694 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001695 }
1696
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001697 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001698}
1699
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001700void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001701 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001702 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001703 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001704 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001705
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001706 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001707 if (TargetDecl) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001708 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001709 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001710 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001711 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001712 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001713 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001714 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +00001715 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001716 }
1717
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001718 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001719 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001720 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001721 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001722 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001723 if (RetTy->isPromotableIntegerType()) {
1724 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001725 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001726 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001727 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001728 }
1729 }
1730 break;
1731
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001732 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001733 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001734 llvm::Attribute::StructRet |
1735 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001736 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001737 // sret disables readnone and readonly
1738 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1739 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001740 break;
1741
Daniel Dunbar11434922009-01-26 21:26:08 +00001742 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001743 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001744 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001745
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001746 case ABIArgInfo::Expand:
1747 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001748 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001749
Devang Patela2c69122008-09-26 22:53:57 +00001750 if (RetAttrs)
1751 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001752
1753 // FIXME: we need to honour command line settings also...
1754 // FIXME: RegParm should be reduced in case of nested functions and/or global
1755 // register variable.
1756 signed RegParm = 0;
1757 if (TargetDecl)
1758 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1759 RegParm = RegParmAttr->getNumParams();
1760
1761 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001762 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1763 ie = FI.arg_end(); it != ie; ++it) {
1764 QualType ParamType = it->type;
1765 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001766 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001767
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001768 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001769 case ABIArgInfo::Coerce:
1770 break;
1771
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001772 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001773 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001774 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001775 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001776 // byval disables readnone and readonly.
1777 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1778 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001779 break;
1780
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001781 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001782 if (ParamType->isPromotableIntegerType()) {
1783 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001784 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001785 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001786 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001787 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001788 }
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001789 if (RegParm > 0 &&
1790 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1791 RegParm -=
1792 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1793 if (RegParm >= 0)
1794 Attributes |= llvm::Attribute::InReg;
1795 }
1796 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001797 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001798
Daniel Dunbar11434922009-01-26 21:26:08 +00001799 case ABIArgInfo::Ignore:
1800 // Skip increment, no matching LLVM parameter.
1801 continue;
1802
Daniel Dunbar56273772008-09-17 00:51:38 +00001803 case ABIArgInfo::Expand: {
1804 std::vector<const llvm::Type*> Tys;
1805 // FIXME: This is rather inefficient. Do we ever actually need
1806 // to do anything here? The result should be just reconstructed
1807 // on the other side, so extension should be a non-issue.
1808 getTypes().GetExpandedTypes(ParamType, Tys);
1809 Index += Tys.size();
1810 continue;
1811 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001812 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001813
Devang Patel761d7f72008-09-25 21:02:23 +00001814 if (Attributes)
1815 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001816 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001817 }
Devang Patela2c69122008-09-26 22:53:57 +00001818 if (FuncAttrs)
1819 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001820}
1821
Daniel Dunbar88b53962009-02-02 22:03:45 +00001822void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1823 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001824 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001825 // FIXME: We no longer need the types from FunctionArgList; lift up
1826 // and simplify.
1827
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001828 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1829 llvm::Function::arg_iterator AI = Fn->arg_begin();
1830
1831 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001832 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001833 AI->setName("agg.result");
1834 ++AI;
1835 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001836
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001837 assert(FI.arg_size() == Args.size() &&
1838 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001839 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001840 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001841 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001842 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001843 QualType Ty = info_it->type;
1844 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001845
1846 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001847 case ABIArgInfo::Indirect: {
1848 llvm::Value* V = AI;
1849 if (hasAggregateLLVMType(Ty)) {
1850 // Do nothing, aggregates and complex variables are accessed by
1851 // reference.
1852 } else {
1853 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001854 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001855 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1856 // This must be a promotion, for something like
1857 // "void a(x) short x; {..."
1858 V = EmitScalarConversion(V, Ty, Arg->getType());
1859 }
1860 }
1861 EmitParmDecl(*Arg, V);
1862 break;
1863 }
1864
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001865 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001866 assert(AI != Fn->arg_end() && "Argument mismatch!");
1867 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001868 if (hasAggregateLLVMType(Ty)) {
1869 // Create a temporary alloca to hold the argument; the rest of
1870 // codegen expects to access aggregates & complex values by
1871 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001872 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001873 Builder.CreateStore(AI, V);
1874 } else {
1875 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1876 // This must be a promotion, for something like
1877 // "void a(x) short x; {..."
1878 V = EmitScalarConversion(V, Ty, Arg->getType());
1879 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001880 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001881 EmitParmDecl(*Arg, V);
1882 break;
1883 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001884
1885 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001886 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001887 // we need to create a temporary and reconstruct it from the
1888 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001889 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001890 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001891 (Name + ".addr").c_str());
1892 // FIXME: What are the right qualifiers here?
1893 llvm::Function::arg_iterator End =
1894 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1895 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001896
Daniel Dunbar56273772008-09-17 00:51:38 +00001897 // Name the arguments used in expansion and increment AI.
1898 unsigned Index = 0;
1899 for (; AI != End; ++AI, ++Index)
1900 AI->setName(Name + "." + llvm::utostr(Index));
1901 continue;
1902 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001903
1904 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001905 // Initialize the local variable appropriately.
1906 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001907 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001908 } else {
1909 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1910 }
1911
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001912 // Skip increment, no matching LLVM parameter.
1913 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001914
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001915 case ABIArgInfo::Coerce: {
1916 assert(AI != Fn->arg_end() && "Argument mismatch!");
1917 // FIXME: This is very wasteful; EmitParmDecl is just going to
1918 // drop the result in a new alloca anyway, so we could just
1919 // store into that directly if we broke the abstraction down
1920 // more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001921 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001922 CreateCoercedStore(AI, V, *this);
1923 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001924 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001925 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001926 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1927 // This must be a promotion, for something like
1928 // "void a(x) short x; {..."
1929 V = EmitScalarConversion(V, Ty, Arg->getType());
1930 }
1931 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001932 EmitParmDecl(*Arg, V);
1933 break;
1934 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001935 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001936
1937 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001938 }
1939 assert(AI == Fn->arg_end() && "Argument mismatch!");
1940}
1941
Daniel Dunbar88b53962009-02-02 22:03:45 +00001942void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001943 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001944 llvm::Value *RV = 0;
1945
1946 // Functions with no result always return void.
1947 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001948 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001949 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001950
1951 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001952 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001953 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001954 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1955 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1956 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1957 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1958 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001959 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1960 false);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001961 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001962 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001963
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001964 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001965 // The internal return value temp always will have
1966 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001967 RV = Builder.CreateLoad(ReturnValue);
1968 break;
1969
Daniel Dunbar11434922009-01-26 21:26:08 +00001970 case ABIArgInfo::Ignore:
1971 break;
1972
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001973 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001974 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001975 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001976
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001977 case ABIArgInfo::Expand:
1978 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001979 }
1980 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001981
1982 if (RV) {
1983 Builder.CreateRet(RV);
1984 } else {
1985 Builder.CreateRetVoid();
1986 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001987}
1988
Anders Carlsson0139bb92009-04-08 20:47:54 +00001989RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
1990 return EmitAnyExprToTemp(E);
1991}
1992
Daniel Dunbar88b53962009-02-02 22:03:45 +00001993RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1994 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001995 const CallArgList &CallArgs,
1996 const Decl *TargetDecl) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001997 // FIXME: We no longer need the types from CallArgs; lift up and
1998 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001999 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002000
2001 // Handle struct-return functions by passing a pointer to the
2002 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002003 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00002004 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00002005 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002006 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002007 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002008 }
2009
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00002010 assert(CallInfo.arg_size() == CallArgs.size() &&
2011 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00002012 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002013 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00002014 I != E; ++I, ++info_it) {
2015 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002016 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00002017
2018 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002019 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00002020 if (RV.isScalar() || RV.isComplex()) {
2021 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002022 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00002023 if (RV.isScalar())
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002024 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00002025 else
2026 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2027 } else {
2028 Args.push_back(RV.getAggregateAddr());
2029 }
2030 break;
2031
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002032 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00002033 if (RV.isScalar()) {
2034 Args.push_back(RV.getScalarVal());
2035 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002036 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2037 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2038 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2039 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00002040 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002041 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00002042 }
2043 break;
2044
Daniel Dunbar11434922009-01-26 21:26:08 +00002045 case ABIArgInfo::Ignore:
2046 break;
2047
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002048 case ABIArgInfo::Coerce: {
2049 // FIXME: Avoid the conversion through memory if possible.
2050 llvm::Value *SrcPtr;
2051 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00002052 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002053 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002054 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002055 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002056 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2057 } else
2058 SrcPtr = RV.getAggregateAddr();
2059 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2060 *this));
2061 break;
2062 }
2063
Daniel Dunbar56273772008-09-17 00:51:38 +00002064 case ABIArgInfo::Expand:
2065 ExpandTypeToArgs(I->second, RV, Args);
2066 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002067 }
2068 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002069
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002070 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel761d7f72008-09-25 21:02:23 +00002071 CodeGen::AttributeListType AttributeList;
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002072 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002073 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2074 AttributeList.end());
Daniel Dunbar725ad312009-01-31 02:19:00 +00002075
Daniel Dunbard14151d2009-03-02 04:32:35 +00002076 llvm::CallSite CS;
2077 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2078 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002079 } else {
2080 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbard14151d2009-03-02 04:32:35 +00002081 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2082 &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002083 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00002084 }
2085
Daniel Dunbard14151d2009-03-02 04:32:35 +00002086 CS.setAttributes(Attrs);
2087 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2088 CS.setCallingConv(F->getCallingConv());
2089
2090 // If the call doesn't return, finish the basic block and clear the
2091 // insertion point; this allows the rest of IRgen to discard
2092 // unreachable code.
2093 if (CS.doesNotReturn()) {
2094 Builder.CreateUnreachable();
2095 Builder.ClearInsertionPoint();
2096
2097 // FIXME: For now, emit a dummy basic block because expr
2098 // emitters in generally are not ready to handle emitting
2099 // expressions at unreachable points.
2100 EnsureInsertPoint();
2101
2102 // Return a reasonable RValue.
2103 return GetUndefRValue(RetTy);
2104 }
2105
2106 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner34030842009-03-22 00:32:22 +00002107 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002108 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002109
2110 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002111 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002112 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00002113 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00002114 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00002115 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00002116 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002117
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002118 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002119 if (RetTy->isAnyComplexType()) {
2120 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2121 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2122 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00002123 }
2124 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002125 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002126 Builder.CreateStore(CI, V);
2127 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002128 }
2129 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002130
Daniel Dunbar11434922009-01-26 21:26:08 +00002131 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00002132 // If we are ignoring an argument that had a result, make sure to
2133 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00002134 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00002135
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002136 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002137 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002138 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00002139 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00002140 if (RetTy->isAnyComplexType())
2141 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner34030842009-03-22 00:32:22 +00002142 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00002143 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002144 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002145 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002146
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002147 case ABIArgInfo::Expand:
2148 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002149 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002150
2151 assert(0 && "Unhandled ABIArgInfo::Kind");
2152 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002153}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00002154
2155/* VarArg handling */
2156
2157llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2158 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2159}