blob: ea0b887c64c60b1af840242d15307455ede54241 [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 Dunbarbe9eb092009-02-12 09:04:14 +000027#include "llvm/Support/MathExtras.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000029
30#include "ABIInfo.h"
31
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000032using namespace clang;
33using namespace CodeGen;
34
35/***/
36
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000037// FIXME: Use iterator and sidestep silly type array creation.
38
Daniel Dunbar541b63b2009-02-02 23:23:47 +000039const
Douglas Gregor72564e72009-02-26 23:50:07 +000040CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000041 return getFunctionInfo(FTNP->getResultType(),
42 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000043}
44
Daniel Dunbar541b63b2009-02-02 23:23:47 +000045const
Douglas Gregor72564e72009-02-26 23:50:07 +000046CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000047 llvm::SmallVector<QualType, 16> ArgTys;
48 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000049 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000050 ArgTys.push_back(FTP->getArgType(i));
51 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000052}
53
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000054const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
55 llvm::SmallVector<QualType, 16> ArgTys;
Chris Lattner3eb67ca2009-05-12 20:27:19 +000056 // Add the 'this' pointer unless this is a static method.
57 if (MD->isInstance())
58 ArgTys.push_back(MD->getThisType(Context));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000059
60 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType();
61 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
62 ArgTys.push_back(FTP->getArgType(i));
63 return getFunctionInfo(FTP->getResultType(), ArgTys);
64}
65
Daniel Dunbar541b63b2009-02-02 23:23:47 +000066const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +000067 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000068 if (MD->isInstance())
69 return getFunctionInfo(MD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000070
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000071 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +000072 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar541b63b2009-02-02 23:23:47 +000073 return getFunctionInfo(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +000074 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000075}
76
Daniel Dunbar541b63b2009-02-02 23:23:47 +000077const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
78 llvm::SmallVector<QualType, 16> ArgTys;
79 ArgTys.push_back(MD->getSelfDecl()->getType());
80 ArgTys.push_back(Context.getObjCSelType());
81 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +000082 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000083 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000084 ArgTys.push_back((*i)->getType());
85 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000086}
87
Daniel Dunbar541b63b2009-02-02 23:23:47 +000088const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
89 const CallArgList &Args) {
90 // FIXME: Kill copy.
91 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000092 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
93 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000094 ArgTys.push_back(i->second);
95 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000096}
97
Daniel Dunbar541b63b2009-02-02 23:23:47 +000098const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
99 const FunctionArgList &Args) {
100 // FIXME: Kill copy.
101 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000102 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
103 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000104 ArgTys.push_back(i->second);
105 return getFunctionInfo(ResTy, ArgTys);
106}
107
108const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
109 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000110 // Lookup or create unique function info.
111 llvm::FoldingSetNodeID ID;
112 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
113
114 void *InsertPos = 0;
115 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
116 if (FI)
117 return *FI;
118
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000119 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000120 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000121 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000122
123 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000124 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000125
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000126 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000127}
128
129/***/
130
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000131ABIInfo::~ABIInfo() {}
132
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000133void ABIArgInfo::dump() const {
134 fprintf(stderr, "(ABIArgInfo Kind=");
135 switch (TheKind) {
136 case Direct:
137 fprintf(stderr, "Direct");
138 break;
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000139 case Ignore:
140 fprintf(stderr, "Ignore");
141 break;
142 case Coerce:
143 fprintf(stderr, "Coerce Type=");
144 getCoerceToType()->print(llvm::errs());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000145 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000146 case Indirect:
147 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000148 break;
149 case Expand:
150 fprintf(stderr, "Expand");
151 break;
152 }
153 fprintf(stderr, ")\n");
154}
155
156/***/
157
Daniel Dunbar573b9072009-05-11 18:58:49 +0000158static bool isEmptyRecord(ASTContext &Context, QualType T);
159
160/// isEmptyField - Return true iff a the field is "empty", that is it
161/// is an unnamed bit-field or an (array of) empty record(s).
162static bool isEmptyField(ASTContext &Context, const FieldDecl *FD) {
163 if (FD->isUnnamedBitfield())
164 return true;
165
166 QualType FT = FD->getType();
Daniel Dunbarcc401dc2009-05-11 23:01:34 +0000167 // Constant arrays of empty records count as empty, strip them off.
168 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
169 FT = AT->getElementType();
Daniel Dunbar573b9072009-05-11 18:58:49 +0000170
171 return isEmptyRecord(Context, FT);
172}
173
174/// isEmptyRecord - Return true iff a structure contains only empty
175/// fields. Note that a structure with a flexible array member is not
Daniel Dunbar834af452008-09-17 21:22:33 +0000176/// considered empty.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000177static bool isEmptyRecord(ASTContext &Context, QualType T) {
Daniel Dunbar5bde6f42009-03-31 19:01:39 +0000178 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar834af452008-09-17 21:22:33 +0000179 if (!RT)
180 return 0;
181 const RecordDecl *RD = RT->getDecl();
182 if (RD->hasFlexibleArrayMember())
183 return false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000184 for (RecordDecl::field_iterator i = RD->field_begin(Context),
Daniel Dunbar573b9072009-05-11 18:58:49 +0000185 e = RD->field_end(Context); i != e; ++i)
186 if (!isEmptyField(Context, *i))
Daniel Dunbar834af452008-09-17 21:22:33 +0000187 return false;
Daniel Dunbar834af452008-09-17 21:22:33 +0000188 return true;
189}
190
191/// isSingleElementStruct - Determine if a structure is a "single
192/// element struct", i.e. it has exactly one non-empty field or
193/// exactly one field which is itself a single element
194/// struct. Structures with flexible array members are never
195/// considered single element structs.
196///
197/// \return The field declaration for the single non-empty field, if
198/// it exists.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000199static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000200 const RecordType *RT = T->getAsStructureType();
201 if (!RT)
202 return 0;
203
204 const RecordDecl *RD = RT->getDecl();
205 if (RD->hasFlexibleArrayMember())
206 return 0;
207
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000208 const Type *Found = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000209 for (RecordDecl::field_iterator i = RD->field_begin(Context),
210 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000211 const FieldDecl *FD = *i;
212 QualType FT = FD->getType();
213
Daniel Dunbar573b9072009-05-11 18:58:49 +0000214 // Ignore empty fields.
215 if (isEmptyField(Context, FD))
216 continue;
217
Daniel Dunbarcc401dc2009-05-11 23:01:34 +0000218 // If we already found an element then this isn't a single-element
219 // struct.
Daniel Dunbarfcab2ca2009-05-08 21:04:47 +0000220 if (Found)
Daniel Dunbar834af452008-09-17 21:22:33 +0000221 return 0;
Daniel Dunbarfcab2ca2009-05-08 21:04:47 +0000222
Daniel Dunbarcc401dc2009-05-11 23:01:34 +0000223 // Treat single element arrays as the element.
224 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
225 if (AT->getSize().getZExtValue() != 1)
226 break;
227 FT = AT->getElementType();
228 }
229
Daniel Dunbarfcab2ca2009-05-08 21:04:47 +0000230 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000231 Found = FT.getTypePtr();
Daniel Dunbar834af452008-09-17 21:22:33 +0000232 } else {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000233 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000234 if (!Found)
235 return 0;
236 }
237 }
238
239 return Found;
240}
241
242static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
243 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
244 return false;
245
246 uint64_t Size = Context.getTypeSize(Ty);
247 return Size == 32 || Size == 64;
248}
249
250static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
251 ASTContext &Context) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000252 for (RecordDecl::field_iterator i = RD->field_begin(Context),
253 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000254 const FieldDecl *FD = *i;
255
256 if (!is32Or64BitBasicType(FD->getType(), Context))
257 return false;
258
Mike Stumpf5408fe2009-05-16 07:57:57 +0000259 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
260 // how to expand them yet, and the predicate for telling if a bitfield still
261 // counts as "basic" is more complicated than what we were doing previously.
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000262 if (FD->isBitField())
263 return false;
Daniel Dunbar834af452008-09-17 21:22:33 +0000264 }
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000265
Daniel Dunbar834af452008-09-17 21:22:33 +0000266 return true;
267}
268
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000269namespace {
270/// DefaultABIInfo - The default implementation for ABI specific
271/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000272/// self-consistent and sensible LLVM IR generation, but does not
273/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000274class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000275 ABIArgInfo classifyReturnType(QualType RetTy,
276 ASTContext &Context) const;
277
278 ABIArgInfo classifyArgumentType(QualType RetTy,
279 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000280
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000281 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
282 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
283 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
284 it != ie; ++it)
285 it->info = classifyArgumentType(it->type, Context);
286 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000287
288 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
289 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000290};
291
292/// X86_32ABIInfo - The X86-32 ABI information.
293class X86_32ABIInfo : public ABIInfo {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000294 ASTContext &Context;
Eli Friedman9fd58e82009-03-23 23:26:24 +0000295 bool IsDarwin;
296
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000297 static bool isRegisterSize(unsigned Size) {
298 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
299 }
300
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000301 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
302
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000303public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000304 ABIArgInfo classifyReturnType(QualType RetTy,
305 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000306
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000307 ABIArgInfo classifyArgumentType(QualType RetTy,
308 ASTContext &Context) const;
309
310 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
311 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
312 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
313 it != ie; ++it)
314 it->info = classifyArgumentType(it->type, Context);
315 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000316
317 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
318 CodeGenFunction &CGF) const;
Eli Friedman9fd58e82009-03-23 23:26:24 +0000319
Douglas Gregor6ab35242009-04-09 21:40:53 +0000320 X86_32ABIInfo(ASTContext &Context, bool d)
321 : ABIInfo(), Context(Context), IsDarwin(d) {}
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000322};
323}
324
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000325
326/// shouldReturnTypeInRegister - Determine if the given type should be
327/// passed in a register (for the Darwin ABI).
328bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
329 ASTContext &Context) {
330 uint64_t Size = Context.getTypeSize(Ty);
331
332 // Type must be register sized.
333 if (!isRegisterSize(Size))
334 return false;
335
336 if (Ty->isVectorType()) {
337 // 64- and 128- bit vectors inside structures are not returned in
338 // registers.
339 if (Size == 64 || Size == 128)
340 return false;
341
342 return true;
343 }
344
345 // If this is a builtin, pointer, or complex type, it is ok.
346 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
347 return true;
348
349 // Arrays are treated like records.
350 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
351 return shouldReturnTypeInRegister(AT->getElementType(), Context);
352
353 // Otherwise, it must be a record type.
354 const RecordType *RT = Ty->getAsRecordType();
355 if (!RT) return false;
356
357 // Structure types are passed in register if all fields would be
358 // passed in a register.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000359 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context),
360 e = RT->getDecl()->field_end(Context); i != e; ++i) {
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000361 const FieldDecl *FD = *i;
362
Daniel Dunbar573b9072009-05-11 18:58:49 +0000363 // Empty fields are ignored.
364 if (isEmptyField(Context, FD))
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000365 continue;
366
367 // Check fields recursively.
368 if (!shouldReturnTypeInRegister(FD->getType(), Context))
369 return false;
370 }
371
372 return true;
373}
374
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000375ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
376 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000377 if (RetTy->isVoidType()) {
378 return ABIArgInfo::getIgnore();
Daniel Dunbar36043162009-04-01 06:13:08 +0000379 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
380 // On Darwin, some vectors are returned in registers.
381 if (IsDarwin) {
382 uint64_t Size = Context.getTypeSize(RetTy);
383
384 // 128-bit vectors are a special case; they are returned in
385 // registers and we need to make sure to pick a type the LLVM
386 // backend will like.
387 if (Size == 128)
388 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
389 2));
390
391 // Always return in register if it fits in a general purpose
392 // register, or if it is 64 bits and has a single element.
393 if ((Size == 8 || Size == 16 || Size == 32) ||
394 (Size == 64 && VT->getNumElements() == 1))
395 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
396
397 return ABIArgInfo::getIndirect(0);
398 }
399
400 return ABIArgInfo::getDirect();
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000401 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8e034442009-04-27 18:31:32 +0000402 // Structures with flexible arrays are always indirect.
403 if (const RecordType *RT = RetTy->getAsStructureType())
404 if (RT->getDecl()->hasFlexibleArrayMember())
405 return ABIArgInfo::getIndirect(0);
406
Eli Friedman9fd58e82009-03-23 23:26:24 +0000407 // Outside of Darwin, structs and unions are always indirect.
408 if (!IsDarwin && !RetTy->isAnyComplexType())
409 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8e034442009-04-27 18:31:32 +0000410
Daniel Dunbar834af452008-09-17 21:22:33 +0000411 // Classify "single element" structs as their element type.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000412 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000413 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000414 if (BT->isIntegerType()) {
Daniel Dunbar2e001162009-05-08 21:30:11 +0000415 // We need to use the size of the structure, padding
416 // bit-fields can adjust that to be larger than the single
417 // element type.
418 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar834af452008-09-17 21:22:33 +0000419 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
420 } else if (BT->getKind() == BuiltinType::Float) {
Daniel Dunbar2e001162009-05-08 21:30:11 +0000421 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
422 "Unexpect single element structure size!");
Daniel Dunbar834af452008-09-17 21:22:33 +0000423 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
424 } else if (BT->getKind() == BuiltinType::Double) {
Daniel Dunbar2e001162009-05-08 21:30:11 +0000425 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
426 "Unexpect single element structure size!");
Daniel Dunbar834af452008-09-17 21:22:33 +0000427 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
428 }
429 } else if (SeltTy->isPointerType()) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000430 // FIXME: It would be really nice if this could come out as the proper
431 // pointer type.
Daniel Dunbar834af452008-09-17 21:22:33 +0000432 llvm::Type *PtrTy =
433 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
434 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000435 } else if (SeltTy->isVectorType()) {
436 // 64- and 128-bit vectors are never returned in a
437 // register when inside a structure.
438 uint64_t Size = Context.getTypeSize(RetTy);
439 if (Size == 64 || Size == 128)
440 return ABIArgInfo::getIndirect(0);
441
442 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000443 }
444 }
445
Daniel Dunbar836a0642009-05-12 17:00:20 +0000446 // Small structures which are register sized are generally returned
447 // in a register.
448 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
449 uint64_t Size = Context.getTypeSize(RetTy);
450 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000451 }
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000452
453 return ABIArgInfo::getIndirect(0);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000454 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000455 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000456 }
457}
458
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000459ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000460 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000461 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000462 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000463 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000464 if (const RecordType *RT = Ty->getAsStructureType())
465 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000466 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000467
Daniel Dunbar3170c932009-02-05 01:50:07 +0000468 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000469 uint64_t Size = Context.getTypeSize(Ty);
470 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000471 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000472
473 // Expand structs with size <= 128-bits which consist only of
474 // basic types (int, long long, float, double, xxx*). This is
475 // non-recursive and does not ignore empty fields.
476 if (const RecordType *RT = Ty->getAsStructureType()) {
477 if (Context.getTypeSize(Ty) <= 4*32 &&
478 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
479 return ABIArgInfo::getExpand();
480 }
481
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000482 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000483 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000484 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000485 }
486}
487
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000488llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
489 CodeGenFunction &CGF) const {
490 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
491 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
492
493 CGBuilderTy &Builder = CGF.Builder;
494 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
495 "ap");
496 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
497 llvm::Type *PTy =
498 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
499 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
500
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000501 uint64_t Offset =
502 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000503 llvm::Value *NextAddr =
504 Builder.CreateGEP(Addr,
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000505 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000506 "ap.next");
507 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
508
509 return AddrTyped;
510}
511
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000512namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000513/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000514class X86_64ABIInfo : public ABIInfo {
515 enum Class {
516 Integer = 0,
517 SSE,
518 SSEUp,
519 X87,
520 X87Up,
521 ComplexX87,
522 NoClass,
523 Memory
524 };
525
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000526 /// merge - Implement the X86_64 ABI merging algorithm.
527 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000528 /// Merge an accumulating classification \arg Accum with a field
529 /// classification \arg Field.
530 ///
531 /// \param Accum - The accumulating classification. This should
532 /// always be either NoClass or the result of a previous merge
533 /// call. In addition, this should never be Memory (the caller
534 /// should just return Memory for the aggregate).
535 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000536
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000537 /// classify - Determine the x86_64 register classes in which the
538 /// given type T should be passed.
539 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000540 /// \param Lo - The classification for the parts of the type
541 /// residing in the low word of the containing object.
542 ///
543 /// \param Hi - The classification for the parts of the type
544 /// residing in the high word of the containing object.
545 ///
546 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000547 /// containing object. Some parameters are classified different
548 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000549 ///
550 /// If a word is unused its result will be NoClass; if a type should
551 /// be passed in Memory then at least the classification of \arg Lo
552 /// will be Memory.
553 ///
554 /// The \arg Lo class will be NoClass iff the argument is ignored.
555 ///
556 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000557 /// also be ComplexX87.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000558 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000559 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000560
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000561 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
562 /// to coerce to, chose the best way to pass Ty in the same place
563 /// that \arg CoerceTo would be passed, but while keeping the
564 /// emitted code as simple as possible.
565 ///
Mike Stumpf5408fe2009-05-16 07:57:57 +0000566 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
567 /// the ways we might want to pass things, instead of constructing an LLVM
568 /// type. This makes this code more explicit, and it makes it clearer that we
569 /// are also doing this for correctness in the case of passing scalar types.
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000570 ABIArgInfo getCoerceResult(QualType Ty,
571 const llvm::Type *CoerceTo,
572 ASTContext &Context) const;
573
Daniel Dunbar86e13ee2009-05-26 16:37:37 +0000574 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
575 /// such that the argument will be passed in memory.
576 ABIArgInfo getIndirectResult(QualType Ty,
577 ASTContext &Context) const;
578
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000579 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000580 ASTContext &Context) const;
581
582 ABIArgInfo classifyArgumentType(QualType Ty,
583 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000584 unsigned &neededInt,
585 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000586
587public:
588 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000589
590 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
591 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000592};
593}
594
Daniel Dunbarc4503572009-01-31 00:06:58 +0000595X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
596 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000597 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
598 // classified recursively so that always two fields are
599 // considered. The resulting class is calculated according to
600 // the classes of the fields in the eightbyte:
601 //
602 // (a) If both classes are equal, this is the resulting class.
603 //
604 // (b) If one of the classes is NO_CLASS, the resulting class is
605 // the other class.
606 //
607 // (c) If one of the classes is MEMORY, the result is the MEMORY
608 // class.
609 //
610 // (d) If one of the classes is INTEGER, the result is the
611 // INTEGER.
612 //
613 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
614 // MEMORY is used as class.
615 //
616 // (f) Otherwise class SSE is used.
Daniel Dunbar100f4022009-03-06 17:50:25 +0000617
618 // Accum should never be memory (we should have returned) or
619 // ComplexX87 (because this cannot be passed in a structure).
620 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbarc4503572009-01-31 00:06:58 +0000621 "Invalid accumulated classification during merge.");
622 if (Accum == Field || Field == NoClass)
623 return Accum;
624 else if (Field == Memory)
625 return Memory;
626 else if (Accum == NoClass)
627 return Field;
628 else if (Accum == Integer || Field == Integer)
629 return Integer;
Daniel Dunbar20e95c52009-05-12 15:22:40 +0000630 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
631 Accum == X87 || Accum == X87Up)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000632 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000633 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000634 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000635}
636
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000637void X86_64ABIInfo::classify(QualType Ty,
638 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000639 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000640 Class &Lo, Class &Hi) const {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000641 // FIXME: This code can be simplified by introducing a simple value class for
642 // Class pairs with appropriate constructor methods for the various
643 // situations.
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000644
Mike Stumpf5408fe2009-05-16 07:57:57 +0000645 // FIXME: Some of the split computations are wrong; unaligned vectors
646 // shouldn't be passed in registers for example, so there is no chance they
647 // can straddle an eightbyte. Verify & simplify.
Daniel Dunbare28099b2009-02-22 04:48:22 +0000648
Daniel Dunbarc4503572009-01-31 00:06:58 +0000649 Lo = Hi = NoClass;
650
651 Class &Current = OffsetBase < 64 ? Lo : Hi;
652 Current = Memory;
653
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000654 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
655 BuiltinType::Kind k = BT->getKind();
656
Daniel Dunbar11434922009-01-26 21:26:08 +0000657 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000658 Current = NoClass;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000659 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattnerae69e002009-04-30 06:22:07 +0000660 Lo = Integer;
661 Hi = Integer;
Daniel Dunbar11434922009-01-26 21:26:08 +0000662 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000663 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000664 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000665 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000666 } else if (k == BuiltinType::LongDouble) {
667 Lo = X87;
668 Hi = X87Up;
669 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000670 // FIXME: _Decimal32 and _Decimal64 are SSE.
671 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson708762b2009-02-26 17:31:15 +0000672 } else if (const EnumType *ET = Ty->getAsEnumType()) {
673 // Classify the underlying integer type.
674 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbar89588912009-02-26 20:52:22 +0000675 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000676 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000677 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000678 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbare28099b2009-02-22 04:48:22 +0000679 if (Size == 32) {
680 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
681 // float> as integer.
682 Current = Integer;
683
684 // If this type crosses an eightbyte boundary, it should be
685 // split.
686 uint64_t EB_Real = (OffsetBase) / 64;
687 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
688 if (EB_Real != EB_Imag)
689 Hi = Lo;
690 } else if (Size == 64) {
Daniel Dunbar0af99292009-02-22 04:16:10 +0000691 // gcc passes <1 x double> in memory. :(
692 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000693 return;
Daniel Dunbar0af99292009-02-22 04:16:10 +0000694
695 // gcc passes <1 x long long> as INTEGER.
696 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
697 Current = Integer;
698 else
699 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000700
701 // If this type crosses an eightbyte boundary, it should be
702 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000703 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000704 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000705 } else if (Size == 128) {
706 Lo = SSE;
707 Hi = SSEUp;
708 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000709 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000710 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000711
Daniel Dunbare33edf12009-01-30 18:40:10 +0000712 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar0af99292009-02-22 04:16:10 +0000713 if (ET->isIntegralType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000714 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000715 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000716 else if (Size <= 128)
717 Lo = Hi = Integer;
718 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000719 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000720 else if (ET == Context.DoubleTy)
721 Lo = Hi = SSE;
722 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000723 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000724
725 // If this complex type crosses an eightbyte boundary then it
726 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000727 uint64_t EB_Real = (OffsetBase) / 64;
728 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000729 if (Hi == NoClass && EB_Real != EB_Imag)
730 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000731 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
732 // Arrays are treated like structures.
733
734 uint64_t Size = Context.getTypeSize(Ty);
735
736 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
737 // than two eightbytes, ..., it has class MEMORY.
738 if (Size > 128)
739 return;
740
741 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
742 // fields, it has class MEMORY.
743 //
744 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000745 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000746 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000747
748 // Otherwise implement simplified merge. We could be smarter about
749 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000750 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000751 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
752 uint64_t ArraySize = AT->getSize().getZExtValue();
753 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
754 Class FieldLo, FieldHi;
755 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000756 Lo = merge(Lo, FieldLo);
757 Hi = merge(Hi, FieldHi);
758 if (Lo == Memory || Hi == Memory)
759 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000760 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000761
762 // Do post merger cleanup (see below). Only case we worry about is Memory.
763 if (Hi == Memory)
764 Lo = Memory;
765 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000766 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000767 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000768
769 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
770 // than two eightbytes, ..., it has class MEMORY.
771 if (Size > 128)
772 return;
773
774 const RecordDecl *RD = RT->getDecl();
775
776 // Assume variable sized types are passed in memory.
777 if (RD->hasFlexibleArrayMember())
778 return;
779
780 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
781
782 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000783 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000784 unsigned idx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000785 for (RecordDecl::field_iterator i = RD->field_begin(Context),
786 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000787 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbardd81d442009-02-17 02:45:44 +0000788 bool BitField = i->isBitField();
Daniel Dunbar99037e52009-01-29 08:13:58 +0000789
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000790 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
791 // fields, it has class MEMORY.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000792 //
Daniel Dunbar8e034442009-04-27 18:31:32 +0000793 // Note, skip this test for bit-fields, see below.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000794 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar99037e52009-01-29 08:13:58 +0000795 Lo = Memory;
796 return;
797 }
798
Daniel Dunbar99037e52009-01-29 08:13:58 +0000799 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000800 //
801 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
802 // exceeds a single eightbyte, each is classified
803 // separately. Each eightbyte gets initialized to class
804 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000805 Class FieldLo, FieldHi;
Daniel Dunbardd81d442009-02-17 02:45:44 +0000806
Daniel Dunbar8e034442009-04-27 18:31:32 +0000807 // Bit-fields require special handling, they do not force the
Daniel Dunbardd81d442009-02-17 02:45:44 +0000808 // structure to be passed in memory even if unaligned, and
809 // therefore they can straddle an eightbyte.
810 if (BitField) {
Daniel Dunbar8236bf12009-05-08 22:26:44 +0000811 // Ignore padding bit-fields.
812 if (i->isUnnamedBitfield())
813 continue;
814
Daniel Dunbardd81d442009-02-17 02:45:44 +0000815 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman9a901bb2009-04-26 19:19:15 +0000816 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbardd81d442009-02-17 02:45:44 +0000817
818 uint64_t EB_Lo = Offset / 64;
819 uint64_t EB_Hi = (Offset + Size - 1) / 64;
820 FieldLo = FieldHi = NoClass;
821 if (EB_Lo) {
822 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
823 FieldLo = NoClass;
824 FieldHi = Integer;
825 } else {
826 FieldLo = Integer;
827 FieldHi = EB_Hi ? Integer : NoClass;
828 }
829 } else
830 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000831 Lo = merge(Lo, FieldLo);
832 Hi = merge(Hi, FieldHi);
833 if (Lo == Memory || Hi == Memory)
834 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000835 }
836
837 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
838 //
839 // (a) If one of the classes is MEMORY, the whole argument is
840 // passed in memory.
841 //
842 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
843
844 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000845 // the merge (just bail).
846 //
847 // The second condition occurs in the case of unions; for example
848 // union { _Complex double; unsigned; }.
849 if (Hi == Memory)
850 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000851 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000852 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000853 }
854}
855
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000856ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
857 const llvm::Type *CoerceTo,
858 ASTContext &Context) const {
859 if (CoerceTo == llvm::Type::Int64Ty) {
860 // Integer and pointer types will end up in a general purpose
861 // register.
Daniel Dunbar0af99292009-02-22 04:16:10 +0000862 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000863 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000864
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000865 } else if (CoerceTo == llvm::Type::DoubleTy) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000866 // FIXME: It would probably be better to make CGFunctionInfo only map using
867 // canonical types than to canonize here.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000868 QualType CTy = Context.getCanonicalType(Ty);
869
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000870 // Float and double end up in a single SSE reg.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000871 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000872 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000873
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000874 }
875
876 return ABIArgInfo::getCoerce(CoerceTo);
877}
Daniel Dunbarc4503572009-01-31 00:06:58 +0000878
Daniel Dunbar86e13ee2009-05-26 16:37:37 +0000879ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
880 ASTContext &Context) const {
881 // If this is a scalar LLVM value then assume LLVM will pass it in the right
882 // place naturally.
883 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
884 return ABIArgInfo::getDirect();
885
886 // FIXME: Set alignment correctly.
887 return ABIArgInfo::getIndirect(0);
888}
889
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000890ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
891 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000892 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
893 // classification algorithm.
894 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000895 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000896
Daniel Dunbarc4503572009-01-31 00:06:58 +0000897 // Check some invariants.
898 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
899 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
900 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
901
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000902 const llvm::Type *ResType = 0;
903 switch (Lo) {
904 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000905 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000906
907 case SSEUp:
908 case X87Up:
909 assert(0 && "Invalid classification for lo word.");
910
Daniel Dunbarc4503572009-01-31 00:06:58 +0000911 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000912 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000913 case Memory:
Daniel Dunbar86e13ee2009-05-26 16:37:37 +0000914 return getIndirectResult(RetTy, Context);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000915
916 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
917 // available register of the sequence %rax, %rdx is used.
918 case Integer:
919 ResType = llvm::Type::Int64Ty; break;
920
921 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
922 // available SSE register of the sequence %xmm0, %xmm1 is used.
923 case SSE:
924 ResType = llvm::Type::DoubleTy; break;
925
926 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
927 // returned on the X87 stack in %st0 as 80-bit x87 number.
928 case X87:
929 ResType = llvm::Type::X86_FP80Ty; break;
930
Daniel Dunbarc4503572009-01-31 00:06:58 +0000931 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
932 // part of the value is returned in %st0 and the imaginary part in
933 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000934 case ComplexX87:
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000935 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar3e030b42009-02-18 03:44:19 +0000936 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
937 llvm::Type::X86_FP80Ty,
938 NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000939 break;
940 }
941
942 switch (Hi) {
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000943 // Memory was handled previously and X87 should
944 // never occur as a hi class.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000945 case Memory:
946 case X87:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000947 assert(0 && "Invalid classification for hi word.");
948
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000949 case ComplexX87: // Previously handled.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000950 case NoClass: break;
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000951
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000952 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000953 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
954 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000955 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000956 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
957 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000958
959 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
960 // is passed in the upper half of the last used SSE register.
961 //
962 // SSEUP should always be preceeded by SSE, just widen.
963 case SSEUp:
964 assert(Lo == SSE && "Unexpected SSEUp classification.");
965 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
966 break;
967
968 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000969 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000970 case X87Up:
Daniel Dunbar100f4022009-03-06 17:50:25 +0000971 // If X87Up is preceeded by X87, we don't need to do
972 // anything. However, in some cases with unions it may not be
973 // preceeded by X87. In such situations we follow gcc and pass the
974 // extra bits in an SSE reg.
975 if (Lo != X87)
976 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000977 break;
978 }
979
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000980 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000981}
982
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000983ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000984 unsigned &neededInt,
985 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000986 X86_64ABIInfo::Class Lo, Hi;
987 classify(Ty, Context, 0, Lo, Hi);
988
989 // Check some invariants.
990 // FIXME: Enforce these by construction.
991 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
992 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
993 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
994
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000995 neededInt = 0;
996 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000997 const llvm::Type *ResType = 0;
998 switch (Lo) {
999 case NoClass:
1000 return ABIArgInfo::getIgnore();
1001
1002 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1003 // on the stack.
1004 case Memory:
1005
1006 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1007 // COMPLEX_X87, it is passed in memory.
1008 case X87:
1009 case ComplexX87:
Daniel Dunbar86e13ee2009-05-26 16:37:37 +00001010 return getIndirectResult(Ty, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001011
1012 case SSEUp:
1013 case X87Up:
1014 assert(0 && "Invalid classification for lo word.");
1015
1016 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1017 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1018 // and %r9 is used.
1019 case Integer:
1020 ++neededInt;
1021 ResType = llvm::Type::Int64Ty;
1022 break;
1023
1024 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1025 // available SSE register is used, the registers are taken in the
1026 // order from %xmm0 to %xmm7.
1027 case SSE:
1028 ++neededSSE;
1029 ResType = llvm::Type::DoubleTy;
1030 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001031 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001032
1033 switch (Hi) {
1034 // Memory was handled previously, ComplexX87 and X87 should
1035 // never occur as hi classes, and X87Up must be preceed by X87,
1036 // which is passed in memory.
1037 case Memory:
1038 case X87:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001039 case ComplexX87:
1040 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar100f4022009-03-06 17:50:25 +00001041 break;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001042
1043 case NoClass: break;
1044 case Integer:
1045 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1046 ++neededInt;
1047 break;
Daniel Dunbar100f4022009-03-06 17:50:25 +00001048
1049 // X87Up generally doesn't occur here (long double is passed in
1050 // memory), except in situations involving unions.
1051 case X87Up:
1052 case SSE:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001053 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1054 ++neededSSE;
1055 break;
1056
1057 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1058 // eightbyte is passed in the upper half of the last used SSE
1059 // register.
1060 case SSEUp:
1061 assert(Lo == SSE && "Unexpected SSEUp classification.");
1062 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1063 break;
1064 }
1065
Daniel Dunbar644f4c32009-02-14 02:09:24 +00001066 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001067}
1068
1069void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1070 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1071
1072 // Keep track of the number of assigned registers.
1073 unsigned freeIntRegs = 6, freeSSERegs = 8;
Daniel Dunbar3a5f5c52009-05-22 17:33:44 +00001074
1075 // If the return value is indirect, then the hidden argument is consuming one
1076 // integer register.
1077 if (FI.getReturnInfo().isIndirect())
1078 --freeIntRegs;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001079
1080 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1081 // get assigned (in left-to-right order) for passing as follows...
1082 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001083 it != ie; ++it) {
1084 unsigned neededInt, neededSSE;
1085 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1086
1087 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1088 // eightbyte of an argument, the whole argument is passed on the
1089 // stack. If registers have already been assigned for some
1090 // eightbytes of such an argument, the assignments get reverted.
1091 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1092 freeIntRegs -= neededInt;
1093 freeSSERegs -= neededSSE;
1094 } else {
Daniel Dunbar86e13ee2009-05-26 16:37:37 +00001095 it->info = getIndirectResult(it->type, Context);
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001096 }
1097 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001098}
1099
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001100static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1101 QualType Ty,
1102 CodeGenFunction &CGF) {
1103 llvm::Value *overflow_arg_area_p =
1104 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1105 llvm::Value *overflow_arg_area =
1106 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1107
1108 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1109 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001110 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001111 if (Align > 8) {
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001112 // Note that we follow the ABI & gcc here, even though the type
1113 // could in theory have an alignment greater than 16. This case
1114 // shouldn't ever matter in practice.
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001115
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001116 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1117 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1118 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1119 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1120 llvm::Type::Int64Ty);
1121 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1122 overflow_arg_area =
1123 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1124 overflow_arg_area->getType(),
1125 "overflow_arg_area.align");
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001126 }
1127
1128 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1129 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1130 llvm::Value *Res =
1131 CGF.Builder.CreateBitCast(overflow_arg_area,
1132 llvm::PointerType::getUnqual(LTy));
1133
1134 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1135 // l->overflow_arg_area + sizeof(type).
1136 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1137 // an 8 byte boundary.
1138
1139 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1140 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1141 (SizeInBytes + 7) & ~7);
1142 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1143 "overflow_arg_area.next");
1144 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1145
1146 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1147 return Res;
1148}
1149
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001150llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1151 CodeGenFunction &CGF) const {
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001152 // Assume that va_list type is correct; should be pointer to LLVM type:
1153 // struct {
1154 // i32 gp_offset;
1155 // i32 fp_offset;
1156 // i8* overflow_arg_area;
1157 // i8* reg_save_area;
1158 // };
1159 unsigned neededInt, neededSSE;
1160 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1161 neededInt, neededSSE);
1162
1163 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1164 // in the registers. If not go to step 7.
1165 if (!neededInt && !neededSSE)
1166 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1167
1168 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1169 // general purpose registers needed to pass type and num_fp to hold
1170 // the number of floating point registers needed.
1171
1172 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1173 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1174 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1175 //
1176 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1177 // register save space).
1178
1179 llvm::Value *InRegs = 0;
1180 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1181 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1182 if (neededInt) {
1183 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1184 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1185 InRegs =
1186 CGF.Builder.CreateICmpULE(gp_offset,
1187 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1188 48 - neededInt * 8),
1189 "fits_in_gp");
1190 }
1191
1192 if (neededSSE) {
1193 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1194 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1195 llvm::Value *FitsInFP =
1196 CGF.Builder.CreateICmpULE(fp_offset,
1197 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar90dafa12009-02-18 22:19:44 +00001198 176 - neededSSE * 16),
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001199 "fits_in_fp");
Daniel Dunbarf2313462009-02-18 22:05:01 +00001200 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001201 }
1202
1203 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1204 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1205 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1206 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1207
1208 // Emit code to load the value if it was passed in registers.
1209
1210 CGF.EmitBlock(InRegBlock);
1211
1212 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1213 // an offset of l->gp_offset and/or l->fp_offset. This may require
1214 // copying to a temporary location in case the parameter is passed
1215 // in different register classes or requires an alignment greater
1216 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001217 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00001218 // FIXME: This really results in shameful code when we end up needing to
1219 // collect arguments from different places; often what should result in a
1220 // simple assembling of a structure from scattered addresses has many more
1221 // loads than necessary. Can we clean this up?
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001222 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1223 llvm::Value *RegAddr =
1224 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1225 "reg_save_area");
1226 if (neededInt && neededSSE) {
Daniel Dunbar55e5d892009-02-13 17:46:31 +00001227 // FIXME: Cleanup.
1228 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1229 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1230 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1231 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1232 const llvm::Type *TyLo = ST->getElementType(0);
1233 const llvm::Type *TyHi = ST->getElementType(1);
1234 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1235 "Unexpected ABI info for mixed regs");
1236 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1237 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1238 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1239 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1240 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1241 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1242 llvm::Value *V =
1243 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1244 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1245 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1246 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1247
1248 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001249 } else if (neededInt) {
1250 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1251 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1252 llvm::PointerType::getUnqual(LTy));
1253 } else {
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001254 if (neededSSE == 1) {
1255 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1256 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1257 llvm::PointerType::getUnqual(LTy));
1258 } else {
1259 assert(neededSSE == 2 && "Invalid number of needed registers!");
1260 // SSE registers are spaced 16 bytes apart in the register save
1261 // area, we need to collect the two eightbytes together.
1262 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1263 llvm::Value *RegAddrHi =
1264 CGF.Builder.CreateGEP(RegAddrLo,
1265 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1266 const llvm::Type *DblPtrTy =
1267 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1268 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1269 llvm::Type::DoubleTy,
1270 NULL);
1271 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1272 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1273 DblPtrTy));
1274 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1275 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1276 DblPtrTy));
1277 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1278 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1279 llvm::PointerType::getUnqual(LTy));
1280 }
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001281 }
1282
1283 // AMD64-ABI 3.5.7p5: Step 5. Set:
1284 // l->gp_offset = l->gp_offset + num_gp * 8
1285 // l->fp_offset = l->fp_offset + num_fp * 16.
1286 if (neededInt) {
1287 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1288 neededInt * 8);
1289 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1290 gp_offset_p);
1291 }
1292 if (neededSSE) {
1293 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1294 neededSSE * 16);
1295 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1296 fp_offset_p);
1297 }
1298 CGF.EmitBranch(ContBlock);
1299
1300 // Emit code to load the value if it was passed in memory.
1301
1302 CGF.EmitBlock(InMemBlock);
1303 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1304
1305 // Return the appropriate result.
1306
1307 CGF.EmitBlock(ContBlock);
1308 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1309 "vaarg.addr");
1310 ResAddr->reserveOperandSpace(2);
1311 ResAddr->addIncoming(RegAddr, InRegBlock);
1312 ResAddr->addIncoming(MemAddr, InMemBlock);
1313
1314 return ResAddr;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001315}
1316
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001317// ABI Info for PIC16
1318class PIC16ABIInfo : public ABIInfo {
1319 ABIArgInfo classifyReturnType(QualType RetTy,
1320 ASTContext &Context) const;
1321
1322 ABIArgInfo classifyArgumentType(QualType RetTy,
1323 ASTContext &Context) const;
1324
1325 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1326 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1327 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1328 it != ie; ++it)
1329 it->info = classifyArgumentType(it->type, Context);
1330 }
1331
1332 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1333 CodeGenFunction &CGF) const;
1334
1335};
1336
1337ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1338 ASTContext &Context) const {
1339 if (RetTy->isVoidType()) {
1340 return ABIArgInfo::getIgnore();
1341 } else {
1342 return ABIArgInfo::getDirect();
1343 }
1344}
1345
1346ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1347 ASTContext &Context) const {
1348 return ABIArgInfo::getDirect();
1349}
1350
1351llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1352 CodeGenFunction &CGF) const {
1353 return 0;
1354}
1355
Eli Friedmana027ea92009-03-29 00:15:25 +00001356class ARMABIInfo : public ABIInfo {
1357 ABIArgInfo classifyReturnType(QualType RetTy,
1358 ASTContext &Context) const;
1359
1360 ABIArgInfo classifyArgumentType(QualType RetTy,
1361 ASTContext &Context) const;
1362
1363 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1364
1365 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1366 CodeGenFunction &CGF) const;
1367};
1368
1369void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1370 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1371 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1372 it != ie; ++it) {
1373 it->info = classifyArgumentType(it->type, Context);
1374 }
1375}
1376
1377ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1378 ASTContext &Context) const {
1379 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1380 return ABIArgInfo::getDirect();
1381 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001382 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1383 // backend doesn't support byval.
Eli Friedmana027ea92009-03-29 00:15:25 +00001384 // FIXME: This doesn't handle alignment > 64 bits.
1385 const llvm::Type* ElemTy;
1386 unsigned SizeRegs;
1387 if (Context.getTypeAlign(Ty) > 32) {
1388 ElemTy = llvm::Type::Int64Ty;
1389 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1390 } else {
1391 ElemTy = llvm::Type::Int32Ty;
1392 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1393 }
1394 std::vector<const llvm::Type*> LLVMFields;
1395 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1396 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1397 return ABIArgInfo::getCoerce(STy);
1398}
1399
1400ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1401 ASTContext &Context) const {
1402 if (RetTy->isVoidType()) {
1403 return ABIArgInfo::getIgnore();
1404 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1405 // Aggregates <= 4 bytes are returned in r0; other aggregates
1406 // are returned indirectly.
1407 uint64_t Size = Context.getTypeSize(RetTy);
1408 if (Size <= 32)
1409 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1410 return ABIArgInfo::getIndirect(0);
1411 } else {
1412 return ABIArgInfo::getDirect();
1413 }
1414}
1415
1416llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1417 CodeGenFunction &CGF) const {
1418 // FIXME: Need to handle alignment
1419 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1420 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1421
1422 CGBuilderTy &Builder = CGF.Builder;
1423 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1424 "ap");
1425 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1426 llvm::Type *PTy =
1427 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1428 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1429
1430 uint64_t Offset =
1431 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1432 llvm::Value *NextAddr =
1433 Builder.CreateGEP(Addr,
1434 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1435 "ap.next");
1436 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1437
1438 return AddrTyped;
1439}
1440
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001441ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001442 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001443 if (RetTy->isVoidType()) {
1444 return ABIArgInfo::getIgnore();
1445 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001446 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001447 } else {
1448 return ABIArgInfo::getDirect();
1449 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001450}
1451
1452ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001453 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001454 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001455 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001456 } else {
1457 return ABIArgInfo::getDirect();
1458 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001459}
1460
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001461llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1462 CodeGenFunction &CGF) const {
1463 return 0;
1464}
1465
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001466const ABIInfo &CodeGenTypes::getABIInfo() const {
1467 if (TheABIInfo)
1468 return *TheABIInfo;
1469
1470 // For now we just cache this in the CodeGenTypes and don't bother
1471 // to free it.
1472 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1473 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman9fd58e82009-03-23 23:26:24 +00001474 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001475 switch (getContext().Target.getPointerWidth(0)) {
1476 case 32:
Douglas Gregor6ab35242009-04-09 21:40:53 +00001477 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001478 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +00001479 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001480 }
Eli Friedmana027ea92009-03-29 00:15:25 +00001481 } else if (strcmp(TargetPrefix, "arm") == 0) {
1482 // FIXME: Support for OABI?
1483 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001484 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1485 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001486 }
1487
1488 return *(TheABIInfo = new DefaultABIInfo);
1489}
1490
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001491/***/
1492
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001493CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1494 const llvm::SmallVector<QualType, 16> &ArgTys) {
1495 NumArgs = ArgTys.size();
1496 Args = new ArgInfo[1 + NumArgs];
1497 Args[0].type = ResTy;
1498 for (unsigned i = 0; i < NumArgs; ++i)
1499 Args[1 + i].type = ArgTys[i];
1500}
1501
1502/***/
1503
Daniel Dunbar56273772008-09-17 00:51:38 +00001504void CodeGenTypes::GetExpandedTypes(QualType Ty,
1505 std::vector<const llvm::Type*> &ArgTys) {
1506 const RecordType *RT = Ty->getAsStructureType();
1507 assert(RT && "Can only expand structure types.");
1508 const RecordDecl *RD = RT->getDecl();
1509 assert(!RD->hasFlexibleArrayMember() &&
1510 "Cannot expand structure with flexible array.");
1511
Douglas Gregor6ab35242009-04-09 21:40:53 +00001512 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1513 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001514 const FieldDecl *FD = *i;
1515 assert(!FD->isBitField() &&
1516 "Cannot expand structure with bit-field members.");
1517
1518 QualType FT = FD->getType();
1519 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1520 GetExpandedTypes(FT, ArgTys);
1521 } else {
1522 ArgTys.push_back(ConvertType(FT));
1523 }
1524 }
1525}
1526
1527llvm::Function::arg_iterator
1528CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1529 llvm::Function::arg_iterator AI) {
1530 const RecordType *RT = Ty->getAsStructureType();
1531 assert(RT && "Can only expand structure types.");
1532
1533 RecordDecl *RD = RT->getDecl();
1534 assert(LV.isSimple() &&
1535 "Unexpected non-simple lvalue during struct expansion.");
1536 llvm::Value *Addr = LV.getAddress();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001537 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1538 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001539 FieldDecl *FD = *i;
1540 QualType FT = FD->getType();
1541
1542 // FIXME: What are the right qualifiers here?
1543 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1544 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1545 AI = ExpandTypeFromArgs(FT, LV, AI);
1546 } else {
1547 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1548 ++AI;
1549 }
1550 }
1551
1552 return AI;
1553}
1554
1555void
1556CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1557 llvm::SmallVector<llvm::Value*, 16> &Args) {
1558 const RecordType *RT = Ty->getAsStructureType();
1559 assert(RT && "Can only expand structure types.");
1560
1561 RecordDecl *RD = RT->getDecl();
1562 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1563 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001564 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1565 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001566 FieldDecl *FD = *i;
1567 QualType FT = FD->getType();
1568
1569 // FIXME: What are the right qualifiers here?
1570 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1571 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1572 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1573 } else {
1574 RValue RV = EmitLoadOfLValue(LV, FT);
1575 assert(RV.isScalar() &&
1576 "Unexpected non-scalar rvalue during struct expansion.");
1577 Args.push_back(RV.getScalarVal());
1578 }
1579 }
1580}
1581
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001582/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1583/// a pointer to an object of type \arg Ty.
1584///
1585/// This safely handles the case when the src type is smaller than the
1586/// destination type; in this situation the values of bits which not
1587/// present in the src are undefined.
1588static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1589 const llvm::Type *Ty,
1590 CodeGenFunction &CGF) {
1591 const llvm::Type *SrcTy =
1592 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +00001593 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1594 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001595
Daniel Dunbarb225be42009-02-03 05:59:18 +00001596 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +00001597 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001598 // Generally SrcSize is never greater than DstSize, since this means we are
1599 // losing bits. However, this can happen in cases where the structure has
1600 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +00001601 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00001602 // FIXME: Assert that we aren't truncating non-padding bits when have access
1603 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001604 llvm::Value *Casted =
1605 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001606 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1607 // FIXME: Use better alignment / avoid requiring aligned load.
1608 Load->setAlignment(1);
1609 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001610 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001611 // Otherwise do coercion through memory. This is stupid, but
1612 // simple.
1613 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1614 llvm::Value *Casted =
1615 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001616 llvm::StoreInst *Store =
1617 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1618 // FIXME: Use better alignment / avoid requiring aligned store.
1619 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001620 return CGF.Builder.CreateLoad(Tmp);
1621 }
1622}
1623
1624/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1625/// where the source and destination may have different types.
1626///
1627/// This safely handles the case when the src type is larger than the
1628/// destination type; the upper bits of the src will be lost.
1629static void CreateCoercedStore(llvm::Value *Src,
1630 llvm::Value *DstPtr,
1631 CodeGenFunction &CGF) {
1632 const llvm::Type *SrcTy = Src->getType();
1633 const llvm::Type *DstTy =
1634 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1635
Duncan Sands9408c452009-05-09 07:08:47 +00001636 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1637 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001638
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001639 // If store is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +00001640 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001641 // Generally SrcSize is never greater than DstSize, since this means we are
1642 // losing bits. However, this can happen in cases where the structure has
1643 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +00001644 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00001645 // FIXME: Assert that we aren't truncating non-padding bits when have access
1646 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001647 llvm::Value *Casted =
1648 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001649 // FIXME: Use better alignment / avoid requiring aligned store.
1650 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001651 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001652 // Otherwise do coercion through memory. This is stupid, but
1653 // simple.
1654 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1655 CGF.Builder.CreateStore(Src, Tmp);
1656 llvm::Value *Casted =
1657 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001658 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1659 // FIXME: Use better alignment / avoid requiring aligned load.
1660 Load->setAlignment(1);
1661 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001662 }
1663}
1664
Daniel Dunbar56273772008-09-17 00:51:38 +00001665/***/
1666
Daniel Dunbar88b53962009-02-02 22:03:45 +00001667bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001668 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001669}
1670
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001671const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001672CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001673 std::vector<const llvm::Type*> ArgTys;
1674
1675 const llvm::Type *ResultType = 0;
1676
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001677 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001678 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001679 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001680 case ABIArgInfo::Expand:
1681 assert(0 && "Invalid ABI kind for return argument");
1682
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001683 case ABIArgInfo::Direct:
1684 ResultType = ConvertType(RetTy);
1685 break;
1686
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001687 case ABIArgInfo::Indirect: {
1688 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001689 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001690 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001691 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1692 break;
1693 }
1694
Daniel Dunbar11434922009-01-26 21:26:08 +00001695 case ABIArgInfo::Ignore:
1696 ResultType = llvm::Type::VoidTy;
1697 break;
1698
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001699 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001700 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001701 break;
1702 }
1703
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001704 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1705 ie = FI.arg_end(); it != ie; ++it) {
1706 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001707
1708 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001709 case ABIArgInfo::Ignore:
1710 break;
1711
Daniel Dunbar56273772008-09-17 00:51:38 +00001712 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001713 ArgTys.push_back(AI.getCoerceToType());
1714 break;
1715
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001716 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001717 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001718 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1719 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001720 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001721 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001722
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001723 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001724 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001725 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001726
1727 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001728 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001729 break;
1730 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001731 }
1732
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001733 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001734}
1735
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001736void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001737 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001738 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001739 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001740 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001741
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001742 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001743 if (TargetDecl) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001744 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001745 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001746 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001747 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001748 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001749 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001750 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +00001751 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001752 }
1753
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001754 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001755 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001756 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001757 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001758 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001759 if (RetTy->isPromotableIntegerType()) {
1760 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001761 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001762 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001763 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001764 }
1765 }
1766 break;
1767
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001768 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001769 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001770 llvm::Attribute::StructRet |
1771 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001772 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001773 // sret disables readnone and readonly
1774 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1775 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001776 break;
1777
Daniel Dunbar11434922009-01-26 21:26:08 +00001778 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001779 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001780 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001781
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001782 case ABIArgInfo::Expand:
1783 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001784 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001785
Devang Patela2c69122008-09-26 22:53:57 +00001786 if (RetAttrs)
1787 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001788
1789 // FIXME: we need to honour command line settings also...
1790 // FIXME: RegParm should be reduced in case of nested functions and/or global
1791 // register variable.
1792 signed RegParm = 0;
1793 if (TargetDecl)
1794 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1795 RegParm = RegParmAttr->getNumParams();
1796
1797 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001798 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1799 ie = FI.arg_end(); it != ie; ++it) {
1800 QualType ParamType = it->type;
1801 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001802 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001803
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001804 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001805 case ABIArgInfo::Coerce:
1806 break;
1807
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001808 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001809 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001810 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001811 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001812 // byval disables readnone and readonly.
1813 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1814 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001815 break;
1816
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001817 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001818 if (ParamType->isPromotableIntegerType()) {
1819 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001820 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001821 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001822 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001823 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001824 }
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001825 if (RegParm > 0 &&
1826 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1827 RegParm -=
1828 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1829 if (RegParm >= 0)
1830 Attributes |= llvm::Attribute::InReg;
1831 }
1832 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001833 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001834
Daniel Dunbar11434922009-01-26 21:26:08 +00001835 case ABIArgInfo::Ignore:
1836 // Skip increment, no matching LLVM parameter.
1837 continue;
1838
Daniel Dunbar56273772008-09-17 00:51:38 +00001839 case ABIArgInfo::Expand: {
1840 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +00001841 // FIXME: This is rather inefficient. Do we ever actually need to do
1842 // anything here? The result should be just reconstructed on the other
1843 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +00001844 getTypes().GetExpandedTypes(ParamType, Tys);
1845 Index += Tys.size();
1846 continue;
1847 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001848 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001849
Devang Patel761d7f72008-09-25 21:02:23 +00001850 if (Attributes)
1851 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001852 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001853 }
Devang Patela2c69122008-09-26 22:53:57 +00001854 if (FuncAttrs)
1855 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001856}
1857
Daniel Dunbar88b53962009-02-02 22:03:45 +00001858void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1859 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001860 const FunctionArgList &Args) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001861 // FIXME: We no longer need the types from FunctionArgList; lift up and
1862 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001863
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001864 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1865 llvm::Function::arg_iterator AI = Fn->arg_begin();
1866
1867 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001868 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001869 AI->setName("agg.result");
1870 ++AI;
1871 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001872
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001873 assert(FI.arg_size() == Args.size() &&
1874 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001875 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001876 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001877 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001878 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001879 QualType Ty = info_it->type;
1880 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001881
1882 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001883 case ABIArgInfo::Indirect: {
1884 llvm::Value* V = AI;
1885 if (hasAggregateLLVMType(Ty)) {
1886 // Do nothing, aggregates and complex variables are accessed by
1887 // reference.
1888 } else {
1889 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001890 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001891 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1892 // This must be a promotion, for something like
1893 // "void a(x) short x; {..."
1894 V = EmitScalarConversion(V, Ty, Arg->getType());
1895 }
1896 }
1897 EmitParmDecl(*Arg, V);
1898 break;
1899 }
1900
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001901 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001902 assert(AI != Fn->arg_end() && "Argument mismatch!");
1903 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001904 if (hasAggregateLLVMType(Ty)) {
1905 // Create a temporary alloca to hold the argument; the rest of
1906 // codegen expects to access aggregates & complex values by
1907 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001908 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001909 Builder.CreateStore(AI, V);
1910 } else {
1911 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1912 // This must be a promotion, for something like
1913 // "void a(x) short x; {..."
1914 V = EmitScalarConversion(V, Ty, Arg->getType());
1915 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001916 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001917 EmitParmDecl(*Arg, V);
1918 break;
1919 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001920
1921 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001922 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001923 // we need to create a temporary and reconstruct it from the
1924 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001925 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001926 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001927 (Name + ".addr").c_str());
1928 // FIXME: What are the right qualifiers here?
1929 llvm::Function::arg_iterator End =
1930 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1931 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001932
Daniel Dunbar56273772008-09-17 00:51:38 +00001933 // Name the arguments used in expansion and increment AI.
1934 unsigned Index = 0;
1935 for (; AI != End; ++AI, ++Index)
1936 AI->setName(Name + "." + llvm::utostr(Index));
1937 continue;
1938 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001939
1940 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001941 // Initialize the local variable appropriately.
1942 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001943 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001944 } else {
1945 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1946 }
1947
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001948 // Skip increment, no matching LLVM parameter.
1949 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001950
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001951 case ABIArgInfo::Coerce: {
1952 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +00001953 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
1954 // result in a new alloca anyway, so we could just store into that
1955 // directly if we broke the abstraction down more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001956 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001957 CreateCoercedStore(AI, V, *this);
1958 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001959 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001960 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001961 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1962 // This must be a promotion, for something like
1963 // "void a(x) short x; {..."
1964 V = EmitScalarConversion(V, Ty, Arg->getType());
1965 }
1966 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001967 EmitParmDecl(*Arg, V);
1968 break;
1969 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001970 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001971
1972 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001973 }
1974 assert(AI == Fn->arg_end() && "Argument mismatch!");
1975}
1976
Daniel Dunbar88b53962009-02-02 22:03:45 +00001977void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001978 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001979 llvm::Value *RV = 0;
1980
1981 // Functions with no result always return void.
1982 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001983 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001984 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001985
1986 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001987 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001988 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001989 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1990 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1991 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1992 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1993 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001994 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +00001995 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001996 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001997 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001998
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001999 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002000 // The internal return value temp always will have
2001 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002002 RV = Builder.CreateLoad(ReturnValue);
2003 break;
2004
Daniel Dunbar11434922009-01-26 21:26:08 +00002005 case ABIArgInfo::Ignore:
2006 break;
2007
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002008 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00002009 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002010 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002011
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002012 case ABIArgInfo::Expand:
2013 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002014 }
2015 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002016
2017 if (RV) {
2018 Builder.CreateRet(RV);
2019 } else {
2020 Builder.CreateRetVoid();
2021 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002022}
2023
Anders Carlsson0139bb92009-04-08 20:47:54 +00002024RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +00002025 if (ArgType->isReferenceType())
2026 return EmitReferenceBindingToExpr(E, ArgType);
2027
Anders Carlsson0139bb92009-04-08 20:47:54 +00002028 return EmitAnyExprToTemp(E);
2029}
2030
Daniel Dunbar88b53962009-02-02 22:03:45 +00002031RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
2032 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002033 const CallArgList &CallArgs,
2034 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00002035 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002036 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002037
2038 // Handle struct-return functions by passing a pointer to the
2039 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002040 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00002041 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00002042 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002043 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002044 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002045 }
2046
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00002047 assert(CallInfo.arg_size() == CallArgs.size() &&
2048 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00002049 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002050 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00002051 I != E; ++I, ++info_it) {
2052 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002053 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00002054
2055 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002056 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00002057 if (RV.isScalar() || RV.isComplex()) {
2058 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002059 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00002060 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +00002061 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00002062 else
2063 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2064 } else {
2065 Args.push_back(RV.getAggregateAddr());
2066 }
2067 break;
2068
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002069 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00002070 if (RV.isScalar()) {
2071 Args.push_back(RV.getScalarVal());
2072 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002073 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2074 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2075 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2076 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00002077 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002078 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00002079 }
2080 break;
2081
Daniel Dunbar11434922009-01-26 21:26:08 +00002082 case ABIArgInfo::Ignore:
2083 break;
2084
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002085 case ABIArgInfo::Coerce: {
2086 // FIXME: Avoid the conversion through memory if possible.
2087 llvm::Value *SrcPtr;
2088 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00002089 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +00002090 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002091 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002092 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002093 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2094 } else
2095 SrcPtr = RV.getAggregateAddr();
2096 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2097 *this));
2098 break;
2099 }
2100
Daniel Dunbar56273772008-09-17 00:51:38 +00002101 case ABIArgInfo::Expand:
2102 ExpandTypeToArgs(I->second, RV, Args);
2103 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002104 }
2105 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002106
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002107 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel761d7f72008-09-25 21:02:23 +00002108 CodeGen::AttributeListType AttributeList;
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002109 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002110 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2111 AttributeList.end());
Daniel Dunbar725ad312009-01-31 02:19:00 +00002112
Daniel Dunbard14151d2009-03-02 04:32:35 +00002113 llvm::CallSite CS;
2114 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00002115 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002116 } else {
2117 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbard14151d2009-03-02 04:32:35 +00002118 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002119 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002120 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00002121 }
2122
Daniel Dunbard14151d2009-03-02 04:32:35 +00002123 CS.setAttributes(Attrs);
Torok Edwin6857d9d2009-05-22 07:25:06 +00002124 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
Daniel Dunbard14151d2009-03-02 04:32:35 +00002125 CS.setCallingConv(F->getCallingConv());
2126
2127 // If the call doesn't return, finish the basic block and clear the
2128 // insertion point; this allows the rest of IRgen to discard
2129 // unreachable code.
2130 if (CS.doesNotReturn()) {
2131 Builder.CreateUnreachable();
2132 Builder.ClearInsertionPoint();
2133
Mike Stumpf5408fe2009-05-16 07:57:57 +00002134 // FIXME: For now, emit a dummy basic block because expr emitters in
2135 // generally are not ready to handle emitting expressions at unreachable
2136 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00002137 EnsureInsertPoint();
2138
2139 // Return a reasonable RValue.
2140 return GetUndefRValue(RetTy);
2141 }
2142
2143 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner34030842009-03-22 00:32:22 +00002144 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002145 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002146
2147 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002148 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002149 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00002150 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00002151 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00002152 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00002153 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002154
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002155 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002156 if (RetTy->isAnyComplexType()) {
2157 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2158 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2159 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00002160 }
2161 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002162 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002163 Builder.CreateStore(CI, V);
2164 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002165 }
2166 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002167
Daniel Dunbar11434922009-01-26 21:26:08 +00002168 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00002169 // If we are ignoring an argument that had a result, make sure to
2170 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00002171 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00002172
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002173 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002174 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002175 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00002176 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00002177 if (RetTy->isAnyComplexType())
2178 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner34030842009-03-22 00:32:22 +00002179 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00002180 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002181 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002182 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002183
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002184 case ABIArgInfo::Expand:
2185 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002186 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002187
2188 assert(0 && "Unhandled ABIArgInfo::Kind");
2189 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002190}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00002191
2192/* VarArg handling */
2193
2194llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2195 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2196}