blob: 072cc58e08fc2890302502689f49436ffc602a83 [file] [log] [blame]
Daniel Dunbar0dbe2272008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000021#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000022#include "clang/AST/DeclObjC.h"
Daniel Dunbar99037e52009-01-29 08:13:58 +000023#include "clang/AST/RecordLayout.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000024#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000025#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000026#include "llvm/Support/CallSite.h"
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +000027#include "llvm/Support/CommandLine.h"
Daniel Dunbarbe9eb092009-02-12 09:04:14 +000028#include "llvm/Support/MathExtras.h"
Daniel Dunbar6f7279b2009-02-04 23:24:38 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000030#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000031
32#include "ABIInfo.h"
33
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000034using namespace clang;
35using namespace CodeGen;
36
37/***/
38
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000039// FIXME: Use iterator and sidestep silly type array creation.
40
Daniel Dunbar541b63b2009-02-02 23:23:47 +000041const
Douglas Gregor72564e72009-02-26 23:50:07 +000042CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000043 return getFunctionInfo(FTNP->getResultType(),
44 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000045}
46
Daniel Dunbar541b63b2009-02-02 23:23:47 +000047const
Douglas Gregor72564e72009-02-26 23:50:07 +000048CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000049 llvm::SmallVector<QualType, 16> ArgTys;
50 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000051 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000052 ArgTys.push_back(FTP->getArgType(i));
53 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000054}
55
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000056const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
57 llvm::SmallVector<QualType, 16> ArgTys;
Chris Lattner3eb67ca2009-05-12 20:27:19 +000058 // Add the 'this' pointer unless this is a static method.
59 if (MD->isInstance())
60 ArgTys.push_back(MD->getThisType(Context));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000061
62 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType();
63 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
64 ArgTys.push_back(FTP->getArgType(i));
65 return getFunctionInfo(FTP->getResultType(), ArgTys);
66}
67
Daniel Dunbar541b63b2009-02-02 23:23:47 +000068const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +000069 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000070 if (MD->isInstance())
71 return getFunctionInfo(MD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000072
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000073 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +000074 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar541b63b2009-02-02 23:23:47 +000075 return getFunctionInfo(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +000076 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000077}
78
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
80 llvm::SmallVector<QualType, 16> ArgTys;
81 ArgTys.push_back(MD->getSelfDecl()->getType());
82 ArgTys.push_back(Context.getObjCSelType());
83 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +000084 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000085 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000086 ArgTys.push_back((*i)->getType());
87 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000088}
89
Daniel Dunbar541b63b2009-02-02 23:23:47 +000090const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
91 const CallArgList &Args) {
92 // FIXME: Kill copy.
93 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000094 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
95 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000096 ArgTys.push_back(i->second);
97 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000098}
99
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000100const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
101 const FunctionArgList &Args) {
102 // FIXME: Kill copy.
103 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000104 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
105 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000106 ArgTys.push_back(i->second);
107 return getFunctionInfo(ResTy, ArgTys);
108}
109
110const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
111 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000112 // Lookup or create unique function info.
113 llvm::FoldingSetNodeID ID;
114 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
115
116 void *InsertPos = 0;
117 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
118 if (FI)
119 return *FI;
120
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000121 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000122 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000123 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000124
125 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000126 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000127
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000128 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000129}
130
131/***/
132
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000133ABIInfo::~ABIInfo() {}
134
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000135void ABIArgInfo::dump() const {
136 fprintf(stderr, "(ABIArgInfo Kind=");
137 switch (TheKind) {
138 case Direct:
139 fprintf(stderr, "Direct");
140 break;
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000141 case Ignore:
142 fprintf(stderr, "Ignore");
143 break;
144 case Coerce:
145 fprintf(stderr, "Coerce Type=");
146 getCoerceToType()->print(llvm::errs());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000147 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000148 case Indirect:
149 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000150 break;
151 case Expand:
152 fprintf(stderr, "Expand");
153 break;
154 }
155 fprintf(stderr, ")\n");
156}
157
158/***/
159
Daniel Dunbar573b9072009-05-11 18:58:49 +0000160static bool isEmptyRecord(ASTContext &Context, QualType T);
161
162/// isEmptyField - Return true iff a the field is "empty", that is it
163/// is an unnamed bit-field or an (array of) empty record(s).
164static bool isEmptyField(ASTContext &Context, const FieldDecl *FD) {
165 if (FD->isUnnamedBitfield())
166 return true;
167
168 QualType FT = FD->getType();
Daniel Dunbarcc401dc2009-05-11 23:01:34 +0000169 // Constant arrays of empty records count as empty, strip them off.
170 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
171 FT = AT->getElementType();
Daniel Dunbar573b9072009-05-11 18:58:49 +0000172
173 return isEmptyRecord(Context, FT);
174}
175
176/// isEmptyRecord - Return true iff a structure contains only empty
177/// fields. Note that a structure with a flexible array member is not
Daniel Dunbar834af452008-09-17 21:22:33 +0000178/// considered empty.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000179static bool isEmptyRecord(ASTContext &Context, QualType T) {
Daniel Dunbar5bde6f42009-03-31 19:01:39 +0000180 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar834af452008-09-17 21:22:33 +0000181 if (!RT)
182 return 0;
183 const RecordDecl *RD = RT->getDecl();
184 if (RD->hasFlexibleArrayMember())
185 return false;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000186 for (RecordDecl::field_iterator i = RD->field_begin(Context),
Daniel Dunbar573b9072009-05-11 18:58:49 +0000187 e = RD->field_end(Context); i != e; ++i)
188 if (!isEmptyField(Context, *i))
Daniel Dunbar834af452008-09-17 21:22:33 +0000189 return false;
Daniel Dunbar834af452008-09-17 21:22:33 +0000190 return true;
191}
192
193/// isSingleElementStruct - Determine if a structure is a "single
194/// element struct", i.e. it has exactly one non-empty field or
195/// exactly one field which is itself a single element
196/// struct. Structures with flexible array members are never
197/// considered single element structs.
198///
199/// \return The field declaration for the single non-empty field, if
200/// it exists.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000201static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000202 const RecordType *RT = T->getAsStructureType();
203 if (!RT)
204 return 0;
205
206 const RecordDecl *RD = RT->getDecl();
207 if (RD->hasFlexibleArrayMember())
208 return 0;
209
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000210 const Type *Found = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(Context),
212 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000213 const FieldDecl *FD = *i;
214 QualType FT = FD->getType();
215
Daniel Dunbar573b9072009-05-11 18:58:49 +0000216 // Ignore empty fields.
217 if (isEmptyField(Context, FD))
218 continue;
219
Daniel Dunbarcc401dc2009-05-11 23:01:34 +0000220 // If we already found an element then this isn't a single-element
221 // struct.
Daniel Dunbarfcab2ca2009-05-08 21:04:47 +0000222 if (Found)
Daniel Dunbar834af452008-09-17 21:22:33 +0000223 return 0;
Daniel Dunbarfcab2ca2009-05-08 21:04:47 +0000224
Daniel Dunbarcc401dc2009-05-11 23:01:34 +0000225 // Treat single element arrays as the element.
226 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
227 if (AT->getSize().getZExtValue() != 1)
228 break;
229 FT = AT->getElementType();
230 }
231
Daniel Dunbarfcab2ca2009-05-08 21:04:47 +0000232 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000233 Found = FT.getTypePtr();
Daniel Dunbar834af452008-09-17 21:22:33 +0000234 } else {
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000235 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000236 if (!Found)
237 return 0;
238 }
239 }
240
241 return Found;
242}
243
244static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
245 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
246 return false;
247
248 uint64_t Size = Context.getTypeSize(Ty);
249 return Size == 32 || Size == 64;
250}
251
252static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
253 ASTContext &Context) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000254 for (RecordDecl::field_iterator i = RD->field_begin(Context),
255 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000256 const FieldDecl *FD = *i;
257
258 if (!is32Or64BitBasicType(FD->getType(), Context))
259 return false;
260
Daniel Dunbar8e034442009-04-27 18:31:32 +0000261 // FIXME: Reject bit-fields wholesale; there are two problems, we
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000262 // don't know how to expand them yet, and the predicate for
263 // telling if a bitfield still counts as "basic" is more
264 // complicated than what we were doing previously.
265 if (FD->isBitField())
266 return false;
Daniel Dunbar834af452008-09-17 21:22:33 +0000267 }
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000268
Daniel Dunbar834af452008-09-17 21:22:33 +0000269 return true;
270}
271
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000272namespace {
273/// DefaultABIInfo - The default implementation for ABI specific
274/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000275/// self-consistent and sensible LLVM IR generation, but does not
276/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000277class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000278 ABIArgInfo classifyReturnType(QualType RetTy,
279 ASTContext &Context) const;
280
281 ABIArgInfo classifyArgumentType(QualType RetTy,
282 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000283
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000284 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
285 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
286 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
287 it != ie; ++it)
288 it->info = classifyArgumentType(it->type, Context);
289 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000290
291 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
292 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000293};
294
295/// X86_32ABIInfo - The X86-32 ABI information.
296class X86_32ABIInfo : public ABIInfo {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000297 ASTContext &Context;
Eli Friedman9fd58e82009-03-23 23:26:24 +0000298 bool IsDarwin;
299
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000300 static bool isRegisterSize(unsigned Size) {
301 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
302 }
303
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000304 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
305
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000306public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000307 ABIArgInfo classifyReturnType(QualType RetTy,
308 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000309
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000310 ABIArgInfo classifyArgumentType(QualType RetTy,
311 ASTContext &Context) const;
312
313 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
314 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
315 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
316 it != ie; ++it)
317 it->info = classifyArgumentType(it->type, Context);
318 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000319
320 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
321 CodeGenFunction &CGF) const;
Eli Friedman9fd58e82009-03-23 23:26:24 +0000322
Douglas Gregor6ab35242009-04-09 21:40:53 +0000323 X86_32ABIInfo(ASTContext &Context, bool d)
324 : ABIInfo(), Context(Context), IsDarwin(d) {}
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000325};
326}
327
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000328
329/// shouldReturnTypeInRegister - Determine if the given type should be
330/// passed in a register (for the Darwin ABI).
331bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
332 ASTContext &Context) {
333 uint64_t Size = Context.getTypeSize(Ty);
334
335 // Type must be register sized.
336 if (!isRegisterSize(Size))
337 return false;
338
339 if (Ty->isVectorType()) {
340 // 64- and 128- bit vectors inside structures are not returned in
341 // registers.
342 if (Size == 64 || Size == 128)
343 return false;
344
345 return true;
346 }
347
348 // If this is a builtin, pointer, or complex type, it is ok.
349 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
350 return true;
351
352 // Arrays are treated like records.
353 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
354 return shouldReturnTypeInRegister(AT->getElementType(), Context);
355
356 // Otherwise, it must be a record type.
357 const RecordType *RT = Ty->getAsRecordType();
358 if (!RT) return false;
359
360 // Structure types are passed in register if all fields would be
361 // passed in a register.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000362 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context),
363 e = RT->getDecl()->field_end(Context); i != e; ++i) {
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000364 const FieldDecl *FD = *i;
365
Daniel Dunbar573b9072009-05-11 18:58:49 +0000366 // Empty fields are ignored.
367 if (isEmptyField(Context, FD))
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000368 continue;
369
370 // Check fields recursively.
371 if (!shouldReturnTypeInRegister(FD->getType(), Context))
372 return false;
373 }
374
375 return true;
376}
377
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000378ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
379 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000380 if (RetTy->isVoidType()) {
381 return ABIArgInfo::getIgnore();
Daniel Dunbar36043162009-04-01 06:13:08 +0000382 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
383 // On Darwin, some vectors are returned in registers.
384 if (IsDarwin) {
385 uint64_t Size = Context.getTypeSize(RetTy);
386
387 // 128-bit vectors are a special case; they are returned in
388 // registers and we need to make sure to pick a type the LLVM
389 // backend will like.
390 if (Size == 128)
391 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
392 2));
393
394 // Always return in register if it fits in a general purpose
395 // register, or if it is 64 bits and has a single element.
396 if ((Size == 8 || Size == 16 || Size == 32) ||
397 (Size == 64 && VT->getNumElements() == 1))
398 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
399
400 return ABIArgInfo::getIndirect(0);
401 }
402
403 return ABIArgInfo::getDirect();
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000404 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8e034442009-04-27 18:31:32 +0000405 // Structures with flexible arrays are always indirect.
406 if (const RecordType *RT = RetTy->getAsStructureType())
407 if (RT->getDecl()->hasFlexibleArrayMember())
408 return ABIArgInfo::getIndirect(0);
409
Eli Friedman9fd58e82009-03-23 23:26:24 +0000410 // Outside of Darwin, structs and unions are always indirect.
411 if (!IsDarwin && !RetTy->isAnyComplexType())
412 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8e034442009-04-27 18:31:32 +0000413
Daniel Dunbar834af452008-09-17 21:22:33 +0000414 // Classify "single element" structs as their element type.
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000415 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000416 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000417 if (BT->isIntegerType()) {
Daniel Dunbar2e001162009-05-08 21:30:11 +0000418 // We need to use the size of the structure, padding
419 // bit-fields can adjust that to be larger than the single
420 // element type.
421 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar834af452008-09-17 21:22:33 +0000422 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
423 } else if (BT->getKind() == BuiltinType::Float) {
Daniel Dunbar2e001162009-05-08 21:30:11 +0000424 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
425 "Unexpect single element structure size!");
Daniel Dunbar834af452008-09-17 21:22:33 +0000426 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
427 } else if (BT->getKind() == BuiltinType::Double) {
Daniel Dunbar2e001162009-05-08 21:30:11 +0000428 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
429 "Unexpect single element structure size!");
Daniel Dunbar834af452008-09-17 21:22:33 +0000430 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
431 }
432 } else if (SeltTy->isPointerType()) {
433 // FIXME: It would be really nice if this could come out as
434 // the proper pointer type.
435 llvm::Type *PtrTy =
436 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
437 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000438 } else if (SeltTy->isVectorType()) {
439 // 64- and 128-bit vectors are never returned in a
440 // register when inside a structure.
441 uint64_t Size = Context.getTypeSize(RetTy);
442 if (Size == 64 || Size == 128)
443 return ABIArgInfo::getIndirect(0);
444
445 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar834af452008-09-17 21:22:33 +0000446 }
447 }
448
Daniel Dunbar836a0642009-05-12 17:00:20 +0000449 // Small structures which are register sized are generally returned
450 // in a register.
451 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
452 uint64_t Size = Context.getTypeSize(RetTy);
453 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
Daniel Dunbarcf6bde32009-04-01 07:45:00 +0000454 }
Daniel Dunbardfc6b802009-04-01 07:08:38 +0000455
456 return ABIArgInfo::getIndirect(0);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000457 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000458 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000459 }
460}
461
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000462ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000463 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000464 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000465 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000466 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000467 if (const RecordType *RT = Ty->getAsStructureType())
468 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000469 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000470
Daniel Dunbar3170c932009-02-05 01:50:07 +0000471 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000472 uint64_t Size = Context.getTypeSize(Ty);
473 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000474 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000475
476 // Expand structs with size <= 128-bits which consist only of
477 // basic types (int, long long, float, double, xxx*). This is
478 // non-recursive and does not ignore empty fields.
479 if (const RecordType *RT = Ty->getAsStructureType()) {
480 if (Context.getTypeSize(Ty) <= 4*32 &&
481 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
482 return ABIArgInfo::getExpand();
483 }
484
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000485 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000486 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000487 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000488 }
489}
490
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000491llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
492 CodeGenFunction &CGF) const {
493 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
494 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
495
496 CGBuilderTy &Builder = CGF.Builder;
497 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
498 "ap");
499 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
500 llvm::Type *PTy =
501 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
502 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
503
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000504 uint64_t Offset =
505 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000506 llvm::Value *NextAddr =
507 Builder.CreateGEP(Addr,
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000508 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000509 "ap.next");
510 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
511
512 return AddrTyped;
513}
514
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000515namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000516/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000517class X86_64ABIInfo : public ABIInfo {
518 enum Class {
519 Integer = 0,
520 SSE,
521 SSEUp,
522 X87,
523 X87Up,
524 ComplexX87,
525 NoClass,
526 Memory
527 };
528
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000529 /// merge - Implement the X86_64 ABI merging algorithm.
530 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000531 /// Merge an accumulating classification \arg Accum with a field
532 /// classification \arg Field.
533 ///
534 /// \param Accum - The accumulating classification. This should
535 /// always be either NoClass or the result of a previous merge
536 /// call. In addition, this should never be Memory (the caller
537 /// should just return Memory for the aggregate).
538 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000539
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000540 /// classify - Determine the x86_64 register classes in which the
541 /// given type T should be passed.
542 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000543 /// \param Lo - The classification for the parts of the type
544 /// residing in the low word of the containing object.
545 ///
546 /// \param Hi - The classification for the parts of the type
547 /// residing in the high word of the containing object.
548 ///
549 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000550 /// containing object. Some parameters are classified different
551 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000552 ///
553 /// If a word is unused its result will be NoClass; if a type should
554 /// be passed in Memory then at least the classification of \arg Lo
555 /// will be Memory.
556 ///
557 /// The \arg Lo class will be NoClass iff the argument is ignored.
558 ///
559 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000560 /// also be ComplexX87.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000561 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000562 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000563
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000564 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
565 /// to coerce to, chose the best way to pass Ty in the same place
566 /// that \arg CoerceTo would be passed, but while keeping the
567 /// emitted code as simple as possible.
568 ///
569 /// FIXME: Note, this should be cleaned up to just take an
570 /// enumeration of all the ways we might want to pass things,
571 /// instead of constructing an LLVM type. This makes this code more
572 /// explicit, and it makes it clearer that we are also doing this
573 /// for correctness in the case of passing scalar types.
574 ABIArgInfo getCoerceResult(QualType Ty,
575 const llvm::Type *CoerceTo,
576 ASTContext &Context) const;
577
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000578 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000579 ASTContext &Context) const;
580
581 ABIArgInfo classifyArgumentType(QualType Ty,
582 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000583 unsigned &neededInt,
584 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000585
586public:
587 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000588
589 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
590 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000591};
592}
593
Daniel Dunbarc4503572009-01-31 00:06:58 +0000594X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
595 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000596 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
597 // classified recursively so that always two fields are
598 // considered. The resulting class is calculated according to
599 // the classes of the fields in the eightbyte:
600 //
601 // (a) If both classes are equal, this is the resulting class.
602 //
603 // (b) If one of the classes is NO_CLASS, the resulting class is
604 // the other class.
605 //
606 // (c) If one of the classes is MEMORY, the result is the MEMORY
607 // class.
608 //
609 // (d) If one of the classes is INTEGER, the result is the
610 // INTEGER.
611 //
612 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
613 // MEMORY is used as class.
614 //
615 // (f) Otherwise class SSE is used.
Daniel Dunbar100f4022009-03-06 17:50:25 +0000616
617 // Accum should never be memory (we should have returned) or
618 // ComplexX87 (because this cannot be passed in a structure).
619 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbarc4503572009-01-31 00:06:58 +0000620 "Invalid accumulated classification during merge.");
621 if (Accum == Field || Field == NoClass)
622 return Accum;
623 else if (Field == Memory)
624 return Memory;
625 else if (Accum == NoClass)
626 return Field;
627 else if (Accum == Integer || Field == Integer)
628 return Integer;
Daniel Dunbar20e95c52009-05-12 15:22:40 +0000629 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
630 Accum == X87 || Accum == X87Up)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000631 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000632 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000633 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000634}
635
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000636void X86_64ABIInfo::classify(QualType Ty,
637 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000638 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000639 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000640 // FIXME: This code can be simplified by introducing a simple value
641 // class for Class pairs with appropriate constructor methods for
642 // the various situations.
643
Daniel Dunbare28099b2009-02-22 04:48:22 +0000644 // FIXME: Some of the split computations are wrong; unaligned
645 // vectors shouldn't be passed in registers for example, so there is
646 // no chance they can straddle an eightbyte. Verify & simplify.
647
Daniel Dunbarc4503572009-01-31 00:06:58 +0000648 Lo = Hi = NoClass;
649
650 Class &Current = OffsetBase < 64 ? Lo : Hi;
651 Current = Memory;
652
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000653 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
654 BuiltinType::Kind k = BT->getKind();
655
Daniel Dunbar11434922009-01-26 21:26:08 +0000656 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000657 Current = NoClass;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000658 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
Chris Lattnerae69e002009-04-30 06:22:07 +0000659 Lo = Integer;
660 Hi = Integer;
Daniel Dunbar11434922009-01-26 21:26:08 +0000661 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000662 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000663 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000664 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000665 } else if (k == BuiltinType::LongDouble) {
666 Lo = X87;
667 Hi = X87Up;
668 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000669 // FIXME: _Decimal32 and _Decimal64 are SSE.
670 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Anders Carlsson708762b2009-02-26 17:31:15 +0000671 } else if (const EnumType *ET = Ty->getAsEnumType()) {
672 // Classify the underlying integer type.
673 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbar89588912009-02-26 20:52:22 +0000674 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000675 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000676 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000677 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbare28099b2009-02-22 04:48:22 +0000678 if (Size == 32) {
679 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
680 // float> as integer.
681 Current = Integer;
682
683 // If this type crosses an eightbyte boundary, it should be
684 // split.
685 uint64_t EB_Real = (OffsetBase) / 64;
686 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
687 if (EB_Real != EB_Imag)
688 Hi = Lo;
689 } else if (Size == 64) {
Daniel Dunbar0af99292009-02-22 04:16:10 +0000690 // gcc passes <1 x double> in memory. :(
691 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000692 return;
Daniel Dunbar0af99292009-02-22 04:16:10 +0000693
694 // gcc passes <1 x long long> as INTEGER.
695 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
696 Current = Integer;
697 else
698 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000699
700 // If this type crosses an eightbyte boundary, it should be
701 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000702 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000703 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000704 } else if (Size == 128) {
705 Lo = SSE;
706 Hi = SSEUp;
707 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000708 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000709 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000710
Daniel Dunbare33edf12009-01-30 18:40:10 +0000711 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar0af99292009-02-22 04:16:10 +0000712 if (ET->isIntegralType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000713 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000714 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000715 else if (Size <= 128)
716 Lo = Hi = Integer;
717 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000718 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000719 else if (ET == Context.DoubleTy)
720 Lo = Hi = SSE;
721 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000722 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000723
724 // If this complex type crosses an eightbyte boundary then it
725 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000726 uint64_t EB_Real = (OffsetBase) / 64;
727 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000728 if (Hi == NoClass && EB_Real != EB_Imag)
729 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000730 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
731 // Arrays are treated like structures.
732
733 uint64_t Size = Context.getTypeSize(Ty);
734
735 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
736 // than two eightbytes, ..., it has class MEMORY.
737 if (Size > 128)
738 return;
739
740 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
741 // fields, it has class MEMORY.
742 //
743 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000744 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000745 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000746
747 // Otherwise implement simplified merge. We could be smarter about
748 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000749 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000750 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
751 uint64_t ArraySize = AT->getSize().getZExtValue();
752 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
753 Class FieldLo, FieldHi;
754 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000755 Lo = merge(Lo, FieldLo);
756 Hi = merge(Hi, FieldHi);
757 if (Lo == Memory || Hi == Memory)
758 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000759 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000760
761 // Do post merger cleanup (see below). Only case we worry about is Memory.
762 if (Hi == Memory)
763 Lo = Memory;
764 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000765 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000766 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000767
768 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
769 // than two eightbytes, ..., it has class MEMORY.
770 if (Size > 128)
771 return;
772
773 const RecordDecl *RD = RT->getDecl();
774
775 // Assume variable sized types are passed in memory.
776 if (RD->hasFlexibleArrayMember())
777 return;
778
779 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
780
781 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000782 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000783 unsigned idx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000784 for (RecordDecl::field_iterator i = RD->field_begin(Context),
785 e = RD->field_end(Context); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000786 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbardd81d442009-02-17 02:45:44 +0000787 bool BitField = i->isBitField();
Daniel Dunbar99037e52009-01-29 08:13:58 +0000788
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000789 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
790 // fields, it has class MEMORY.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000791 //
Daniel Dunbar8e034442009-04-27 18:31:32 +0000792 // Note, skip this test for bit-fields, see below.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000793 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar99037e52009-01-29 08:13:58 +0000794 Lo = Memory;
795 return;
796 }
797
Daniel Dunbar99037e52009-01-29 08:13:58 +0000798 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000799 //
800 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
801 // exceeds a single eightbyte, each is classified
802 // separately. Each eightbyte gets initialized to class
803 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000804 Class FieldLo, FieldHi;
Daniel Dunbardd81d442009-02-17 02:45:44 +0000805
Daniel Dunbar8e034442009-04-27 18:31:32 +0000806 // Bit-fields require special handling, they do not force the
Daniel Dunbardd81d442009-02-17 02:45:44 +0000807 // structure to be passed in memory even if unaligned, and
808 // therefore they can straddle an eightbyte.
809 if (BitField) {
Daniel Dunbar8236bf12009-05-08 22:26:44 +0000810 // Ignore padding bit-fields.
811 if (i->isUnnamedBitfield())
812 continue;
813
Daniel Dunbardd81d442009-02-17 02:45:44 +0000814 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Eli Friedman9a901bb2009-04-26 19:19:15 +0000815 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbardd81d442009-02-17 02:45:44 +0000816
817 uint64_t EB_Lo = Offset / 64;
818 uint64_t EB_Hi = (Offset + Size - 1) / 64;
819 FieldLo = FieldHi = NoClass;
820 if (EB_Lo) {
821 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
822 FieldLo = NoClass;
823 FieldHi = Integer;
824 } else {
825 FieldLo = Integer;
826 FieldHi = EB_Hi ? Integer : NoClass;
827 }
828 } else
829 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000830 Lo = merge(Lo, FieldLo);
831 Hi = merge(Hi, FieldHi);
832 if (Lo == Memory || Hi == Memory)
833 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000834 }
835
836 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
837 //
838 // (a) If one of the classes is MEMORY, the whole argument is
839 // passed in memory.
840 //
841 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
842
843 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000844 // the merge (just bail).
845 //
846 // The second condition occurs in the case of unions; for example
847 // union { _Complex double; unsigned; }.
848 if (Hi == Memory)
849 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000850 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000851 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000852 }
853}
854
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000855ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
856 const llvm::Type *CoerceTo,
857 ASTContext &Context) const {
858 if (CoerceTo == llvm::Type::Int64Ty) {
859 // Integer and pointer types will end up in a general purpose
860 // register.
Daniel Dunbar0af99292009-02-22 04:16:10 +0000861 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000862 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000863
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000864 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000865 // FIXME: It would probably be better to make CGFunctionInfo only
866 // map using canonical types than to canonize here.
867 QualType CTy = Context.getCanonicalType(Ty);
868
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000869 // Float and double end up in a single SSE reg.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000870 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000871 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000872
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000873 }
874
875 return ABIArgInfo::getCoerce(CoerceTo);
876}
Daniel Dunbarc4503572009-01-31 00:06:58 +0000877
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000878ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
879 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000880 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
881 // classification algorithm.
882 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000883 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000884
Daniel Dunbarc4503572009-01-31 00:06:58 +0000885 // Check some invariants.
886 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
887 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
888 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
889
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000890 const llvm::Type *ResType = 0;
891 switch (Lo) {
892 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000893 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000894
895 case SSEUp:
896 case X87Up:
897 assert(0 && "Invalid classification for lo word.");
898
Daniel Dunbarc4503572009-01-31 00:06:58 +0000899 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000900 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000901 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000902 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000903
904 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
905 // available register of the sequence %rax, %rdx is used.
906 case Integer:
907 ResType = llvm::Type::Int64Ty; break;
908
909 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
910 // available SSE register of the sequence %xmm0, %xmm1 is used.
911 case SSE:
912 ResType = llvm::Type::DoubleTy; break;
913
914 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
915 // returned on the X87 stack in %st0 as 80-bit x87 number.
916 case X87:
917 ResType = llvm::Type::X86_FP80Ty; break;
918
Daniel Dunbarc4503572009-01-31 00:06:58 +0000919 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
920 // part of the value is returned in %st0 and the imaginary part in
921 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000922 case ComplexX87:
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000923 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar3e030b42009-02-18 03:44:19 +0000924 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
925 llvm::Type::X86_FP80Ty,
926 NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000927 break;
928 }
929
930 switch (Hi) {
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000931 // Memory was handled previously and X87 should
932 // never occur as a hi class.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000933 case Memory:
934 case X87:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000935 assert(0 && "Invalid classification for hi word.");
936
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000937 case ComplexX87: // Previously handled.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000938 case NoClass: break;
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000939
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000940 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000941 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
942 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000943 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000944 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
945 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000946
947 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
948 // is passed in the upper half of the last used SSE register.
949 //
950 // SSEUP should always be preceeded by SSE, just widen.
951 case SSEUp:
952 assert(Lo == SSE && "Unexpected SSEUp classification.");
953 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
954 break;
955
956 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000957 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000958 case X87Up:
Daniel Dunbar100f4022009-03-06 17:50:25 +0000959 // If X87Up is preceeded by X87, we don't need to do
960 // anything. However, in some cases with unions it may not be
961 // preceeded by X87. In such situations we follow gcc and pass the
962 // extra bits in an SSE reg.
963 if (Lo != X87)
964 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000965 break;
966 }
967
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000968 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000969}
970
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000971ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000972 unsigned &neededInt,
973 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000974 X86_64ABIInfo::Class Lo, Hi;
975 classify(Ty, Context, 0, Lo, Hi);
976
977 // Check some invariants.
978 // FIXME: Enforce these by construction.
979 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
980 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
981 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
982
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000983 neededInt = 0;
984 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000985 const llvm::Type *ResType = 0;
986 switch (Lo) {
987 case NoClass:
988 return ABIArgInfo::getIgnore();
989
990 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
991 // on the stack.
992 case Memory:
993
994 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
995 // COMPLEX_X87, it is passed in memory.
996 case X87:
997 case ComplexX87:
Daniel Dunbar245f5532009-02-22 08:17:51 +0000998 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000999
1000 case SSEUp:
1001 case X87Up:
1002 assert(0 && "Invalid classification for lo word.");
1003
1004 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1005 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1006 // and %r9 is used.
1007 case Integer:
1008 ++neededInt;
1009 ResType = llvm::Type::Int64Ty;
1010 break;
1011
1012 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1013 // available SSE register is used, the registers are taken in the
1014 // order from %xmm0 to %xmm7.
1015 case SSE:
1016 ++neededSSE;
1017 ResType = llvm::Type::DoubleTy;
1018 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001019 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001020
1021 switch (Hi) {
1022 // Memory was handled previously, ComplexX87 and X87 should
1023 // never occur as hi classes, and X87Up must be preceed by X87,
1024 // which is passed in memory.
1025 case Memory:
1026 case X87:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001027 case ComplexX87:
1028 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar100f4022009-03-06 17:50:25 +00001029 break;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001030
1031 case NoClass: break;
1032 case Integer:
1033 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
1034 ++neededInt;
1035 break;
Daniel Dunbar100f4022009-03-06 17:50:25 +00001036
1037 // X87Up generally doesn't occur here (long double is passed in
1038 // memory), except in situations involving unions.
1039 case X87Up:
1040 case SSE:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001041 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
1042 ++neededSSE;
1043 break;
1044
1045 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1046 // eightbyte is passed in the upper half of the last used SSE
1047 // register.
1048 case SSEUp:
1049 assert(Lo == SSE && "Unexpected SSEUp classification.");
1050 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1051 break;
1052 }
1053
Daniel Dunbar644f4c32009-02-14 02:09:24 +00001054 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001055}
1056
1057void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1058 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1059
1060 // Keep track of the number of assigned registers.
1061 unsigned freeIntRegs = 6, freeSSERegs = 8;
1062
1063 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1064 // get assigned (in left-to-right order) for passing as follows...
1065 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001066 it != ie; ++it) {
1067 unsigned neededInt, neededSSE;
1068 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1069
1070 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1071 // eightbyte of an argument, the whole argument is passed on the
1072 // stack. If registers have already been assigned for some
1073 // eightbytes of such an argument, the assignments get reverted.
1074 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1075 freeIntRegs -= neededInt;
1076 freeSSERegs -= neededSSE;
1077 } else {
Daniel Dunbar245f5532009-02-22 08:17:51 +00001078 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +00001079 }
1080 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001081}
1082
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001083static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1084 QualType Ty,
1085 CodeGenFunction &CGF) {
1086 llvm::Value *overflow_arg_area_p =
1087 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1088 llvm::Value *overflow_arg_area =
1089 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1090
1091 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1092 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001093 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001094 if (Align > 8) {
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001095 // Note that we follow the ABI & gcc here, even though the type
1096 // could in theory have an alignment greater than 16. This case
1097 // shouldn't ever matter in practice.
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001098
Daniel Dunbarc5bcee42009-02-16 23:38:56 +00001099 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1100 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1101 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1102 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1103 llvm::Type::Int64Ty);
1104 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1105 overflow_arg_area =
1106 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1107 overflow_arg_area->getType(),
1108 "overflow_arg_area.align");
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001109 }
1110
1111 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1112 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1113 llvm::Value *Res =
1114 CGF.Builder.CreateBitCast(overflow_arg_area,
1115 llvm::PointerType::getUnqual(LTy));
1116
1117 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1118 // l->overflow_arg_area + sizeof(type).
1119 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1120 // an 8 byte boundary.
1121
1122 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1123 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1124 (SizeInBytes + 7) & ~7);
1125 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1126 "overflow_arg_area.next");
1127 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1128
1129 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1130 return Res;
1131}
1132
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001133llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1134 CodeGenFunction &CGF) const {
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001135 // Assume that va_list type is correct; should be pointer to LLVM type:
1136 // struct {
1137 // i32 gp_offset;
1138 // i32 fp_offset;
1139 // i8* overflow_arg_area;
1140 // i8* reg_save_area;
1141 // };
1142 unsigned neededInt, neededSSE;
1143 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1144 neededInt, neededSSE);
1145
1146 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1147 // in the registers. If not go to step 7.
1148 if (!neededInt && !neededSSE)
1149 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1150
1151 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1152 // general purpose registers needed to pass type and num_fp to hold
1153 // the number of floating point registers needed.
1154
1155 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1156 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1157 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1158 //
1159 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1160 // register save space).
1161
1162 llvm::Value *InRegs = 0;
1163 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1164 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1165 if (neededInt) {
1166 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1167 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1168 InRegs =
1169 CGF.Builder.CreateICmpULE(gp_offset,
1170 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1171 48 - neededInt * 8),
1172 "fits_in_gp");
1173 }
1174
1175 if (neededSSE) {
1176 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1177 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1178 llvm::Value *FitsInFP =
1179 CGF.Builder.CreateICmpULE(fp_offset,
1180 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar90dafa12009-02-18 22:19:44 +00001181 176 - neededSSE * 16),
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001182 "fits_in_fp");
Daniel Dunbarf2313462009-02-18 22:05:01 +00001183 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001184 }
1185
1186 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1187 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1188 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1189 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1190
1191 // Emit code to load the value if it was passed in registers.
1192
1193 CGF.EmitBlock(InRegBlock);
1194
1195 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1196 // an offset of l->gp_offset and/or l->fp_offset. This may require
1197 // copying to a temporary location in case the parameter is passed
1198 // in different register classes or requires an alignment greater
1199 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001200 //
1201 // FIXME: This really results in shameful code when we end up
1202 // needing to collect arguments from different places; often what
1203 // should result in a simple assembling of a structure from
1204 // scattered addresses has many more loads than necessary. Can we
1205 // clean this up?
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001206 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1207 llvm::Value *RegAddr =
1208 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1209 "reg_save_area");
1210 if (neededInt && neededSSE) {
Daniel Dunbar55e5d892009-02-13 17:46:31 +00001211 // FIXME: Cleanup.
1212 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1213 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1214 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1215 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1216 const llvm::Type *TyLo = ST->getElementType(0);
1217 const llvm::Type *TyHi = ST->getElementType(1);
1218 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1219 "Unexpected ABI info for mixed regs");
1220 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1221 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1222 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1223 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1224 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1225 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1226 llvm::Value *V =
1227 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1228 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1229 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1230 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1231
1232 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001233 } else if (neededInt) {
1234 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1235 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1236 llvm::PointerType::getUnqual(LTy));
1237 } else {
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001238 if (neededSSE == 1) {
1239 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1240 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1241 llvm::PointerType::getUnqual(LTy));
1242 } else {
1243 assert(neededSSE == 2 && "Invalid number of needed registers!");
1244 // SSE registers are spaced 16 bytes apart in the register save
1245 // area, we need to collect the two eightbytes together.
1246 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1247 llvm::Value *RegAddrHi =
1248 CGF.Builder.CreateGEP(RegAddrLo,
1249 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1250 const llvm::Type *DblPtrTy =
1251 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1252 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1253 llvm::Type::DoubleTy,
1254 NULL);
1255 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1256 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1257 DblPtrTy));
1258 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1259 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1260 DblPtrTy));
1261 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1262 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1263 llvm::PointerType::getUnqual(LTy));
1264 }
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001265 }
1266
1267 // AMD64-ABI 3.5.7p5: Step 5. Set:
1268 // l->gp_offset = l->gp_offset + num_gp * 8
1269 // l->fp_offset = l->fp_offset + num_fp * 16.
1270 if (neededInt) {
1271 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1272 neededInt * 8);
1273 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1274 gp_offset_p);
1275 }
1276 if (neededSSE) {
1277 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1278 neededSSE * 16);
1279 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1280 fp_offset_p);
1281 }
1282 CGF.EmitBranch(ContBlock);
1283
1284 // Emit code to load the value if it was passed in memory.
1285
1286 CGF.EmitBlock(InMemBlock);
1287 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1288
1289 // Return the appropriate result.
1290
1291 CGF.EmitBlock(ContBlock);
1292 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1293 "vaarg.addr");
1294 ResAddr->reserveOperandSpace(2);
1295 ResAddr->addIncoming(RegAddr, InRegBlock);
1296 ResAddr->addIncoming(MemAddr, InMemBlock);
1297
1298 return ResAddr;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001299}
1300
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001301// ABI Info for PIC16
1302class PIC16ABIInfo : public ABIInfo {
1303 ABIArgInfo classifyReturnType(QualType RetTy,
1304 ASTContext &Context) const;
1305
1306 ABIArgInfo classifyArgumentType(QualType RetTy,
1307 ASTContext &Context) const;
1308
1309 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1310 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1311 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1312 it != ie; ++it)
1313 it->info = classifyArgumentType(it->type, Context);
1314 }
1315
1316 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1317 CodeGenFunction &CGF) const;
1318
1319};
1320
1321ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1322 ASTContext &Context) const {
1323 if (RetTy->isVoidType()) {
1324 return ABIArgInfo::getIgnore();
1325 } else {
1326 return ABIArgInfo::getDirect();
1327 }
1328}
1329
1330ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1331 ASTContext &Context) const {
1332 return ABIArgInfo::getDirect();
1333}
1334
1335llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1336 CodeGenFunction &CGF) const {
1337 return 0;
1338}
1339
Eli Friedmana027ea92009-03-29 00:15:25 +00001340class ARMABIInfo : public ABIInfo {
1341 ABIArgInfo classifyReturnType(QualType RetTy,
1342 ASTContext &Context) const;
1343
1344 ABIArgInfo classifyArgumentType(QualType RetTy,
1345 ASTContext &Context) const;
1346
1347 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1348
1349 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1350 CodeGenFunction &CGF) const;
1351};
1352
1353void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1354 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1355 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1356 it != ie; ++it) {
1357 it->info = classifyArgumentType(it->type, Context);
1358 }
1359}
1360
1361ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1362 ASTContext &Context) const {
1363 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1364 return ABIArgInfo::getDirect();
1365 }
1366 // FIXME: This is kind of nasty... but there isn't much choice
1367 // because the ARM backend doesn't support byval.
1368 // FIXME: This doesn't handle alignment > 64 bits.
1369 const llvm::Type* ElemTy;
1370 unsigned SizeRegs;
1371 if (Context.getTypeAlign(Ty) > 32) {
1372 ElemTy = llvm::Type::Int64Ty;
1373 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1374 } else {
1375 ElemTy = llvm::Type::Int32Ty;
1376 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1377 }
1378 std::vector<const llvm::Type*> LLVMFields;
1379 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1380 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1381 return ABIArgInfo::getCoerce(STy);
1382}
1383
1384ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1385 ASTContext &Context) const {
1386 if (RetTy->isVoidType()) {
1387 return ABIArgInfo::getIgnore();
1388 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1389 // Aggregates <= 4 bytes are returned in r0; other aggregates
1390 // are returned indirectly.
1391 uint64_t Size = Context.getTypeSize(RetTy);
1392 if (Size <= 32)
1393 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1394 return ABIArgInfo::getIndirect(0);
1395 } else {
1396 return ABIArgInfo::getDirect();
1397 }
1398}
1399
1400llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1401 CodeGenFunction &CGF) const {
1402 // FIXME: Need to handle alignment
1403 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1404 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1405
1406 CGBuilderTy &Builder = CGF.Builder;
1407 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1408 "ap");
1409 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1410 llvm::Type *PTy =
1411 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1412 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1413
1414 uint64_t Offset =
1415 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1416 llvm::Value *NextAddr =
1417 Builder.CreateGEP(Addr,
1418 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1419 "ap.next");
1420 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1421
1422 return AddrTyped;
1423}
1424
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001425ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001426 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001427 if (RetTy->isVoidType()) {
1428 return ABIArgInfo::getIgnore();
1429 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001430 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001431 } else {
1432 return ABIArgInfo::getDirect();
1433 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001434}
1435
1436ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001437 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001438 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001439 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001440 } else {
1441 return ABIArgInfo::getDirect();
1442 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001443}
1444
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001445llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1446 CodeGenFunction &CGF) const {
1447 return 0;
1448}
1449
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001450const ABIInfo &CodeGenTypes::getABIInfo() const {
1451 if (TheABIInfo)
1452 return *TheABIInfo;
1453
1454 // For now we just cache this in the CodeGenTypes and don't bother
1455 // to free it.
1456 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1457 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman9fd58e82009-03-23 23:26:24 +00001458 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001459 switch (getContext().Target.getPointerWidth(0)) {
1460 case 32:
Douglas Gregor6ab35242009-04-09 21:40:53 +00001461 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001462 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +00001463 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001464 }
Eli Friedmana027ea92009-03-29 00:15:25 +00001465 } else if (strcmp(TargetPrefix, "arm") == 0) {
1466 // FIXME: Support for OABI?
1467 return *(TheABIInfo = new ARMABIInfo());
Sanjiv Gupta70aa5f92009-04-21 06:01:16 +00001468 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1469 return *(TheABIInfo = new PIC16ABIInfo());
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001470 }
1471
1472 return *(TheABIInfo = new DefaultABIInfo);
1473}
1474
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001475/***/
1476
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001477CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1478 const llvm::SmallVector<QualType, 16> &ArgTys) {
1479 NumArgs = ArgTys.size();
1480 Args = new ArgInfo[1 + NumArgs];
1481 Args[0].type = ResTy;
1482 for (unsigned i = 0; i < NumArgs; ++i)
1483 Args[1 + i].type = ArgTys[i];
1484}
1485
1486/***/
1487
Daniel Dunbar56273772008-09-17 00:51:38 +00001488void CodeGenTypes::GetExpandedTypes(QualType Ty,
1489 std::vector<const llvm::Type*> &ArgTys) {
1490 const RecordType *RT = Ty->getAsStructureType();
1491 assert(RT && "Can only expand structure types.");
1492 const RecordDecl *RD = RT->getDecl();
1493 assert(!RD->hasFlexibleArrayMember() &&
1494 "Cannot expand structure with flexible array.");
1495
Douglas Gregor6ab35242009-04-09 21:40:53 +00001496 for (RecordDecl::field_iterator i = RD->field_begin(Context),
1497 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001498 const FieldDecl *FD = *i;
1499 assert(!FD->isBitField() &&
1500 "Cannot expand structure with bit-field members.");
1501
1502 QualType FT = FD->getType();
1503 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1504 GetExpandedTypes(FT, ArgTys);
1505 } else {
1506 ArgTys.push_back(ConvertType(FT));
1507 }
1508 }
1509}
1510
1511llvm::Function::arg_iterator
1512CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1513 llvm::Function::arg_iterator AI) {
1514 const RecordType *RT = Ty->getAsStructureType();
1515 assert(RT && "Can only expand structure types.");
1516
1517 RecordDecl *RD = RT->getDecl();
1518 assert(LV.isSimple() &&
1519 "Unexpected non-simple lvalue during struct expansion.");
1520 llvm::Value *Addr = LV.getAddress();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001521 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1522 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001523 FieldDecl *FD = *i;
1524 QualType FT = FD->getType();
1525
1526 // FIXME: What are the right qualifiers here?
1527 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1528 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1529 AI = ExpandTypeFromArgs(FT, LV, AI);
1530 } else {
1531 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1532 ++AI;
1533 }
1534 }
1535
1536 return AI;
1537}
1538
1539void
1540CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1541 llvm::SmallVector<llvm::Value*, 16> &Args) {
1542 const RecordType *RT = Ty->getAsStructureType();
1543 assert(RT && "Can only expand structure types.");
1544
1545 RecordDecl *RD = RT->getDecl();
1546 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1547 llvm::Value *Addr = RV.getAggregateAddr();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001548 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
1549 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +00001550 FieldDecl *FD = *i;
1551 QualType FT = FD->getType();
1552
1553 // FIXME: What are the right qualifiers here?
1554 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1555 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1556 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1557 } else {
1558 RValue RV = EmitLoadOfLValue(LV, FT);
1559 assert(RV.isScalar() &&
1560 "Unexpected non-scalar rvalue during struct expansion.");
1561 Args.push_back(RV.getScalarVal());
1562 }
1563 }
1564}
1565
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001566/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1567/// a pointer to an object of type \arg Ty.
1568///
1569/// This safely handles the case when the src type is smaller than the
1570/// destination type; in this situation the values of bits which not
1571/// present in the src are undefined.
1572static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1573 const llvm::Type *Ty,
1574 CodeGenFunction &CGF) {
1575 const llvm::Type *SrcTy =
1576 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +00001577 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1578 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001579
Daniel Dunbarb225be42009-02-03 05:59:18 +00001580 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001581 if (SrcSize == DstSize) {
1582 llvm::Value *Casted =
1583 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001584 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1585 // FIXME: Use better alignment / avoid requiring aligned load.
1586 Load->setAlignment(1);
1587 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001588 } else {
1589 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1590
1591 // Otherwise do coercion through memory. This is stupid, but
1592 // simple.
1593 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1594 llvm::Value *Casted =
1595 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001596 llvm::StoreInst *Store =
1597 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1598 // FIXME: Use better alignment / avoid requiring aligned store.
1599 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001600 return CGF.Builder.CreateLoad(Tmp);
1601 }
1602}
1603
1604/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1605/// where the source and destination may have different types.
1606///
1607/// This safely handles the case when the src type is larger than the
1608/// destination type; the upper bits of the src will be lost.
1609static void CreateCoercedStore(llvm::Value *Src,
1610 llvm::Value *DstPtr,
1611 CodeGenFunction &CGF) {
1612 const llvm::Type *SrcTy = Src->getType();
1613 const llvm::Type *DstTy =
1614 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1615
Duncan Sands9408c452009-05-09 07:08:47 +00001616 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
1617 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001618
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001619 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001620 if (SrcSize == DstSize) {
1621 llvm::Value *Casted =
1622 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001623 // FIXME: Use better alignment / avoid requiring aligned store.
1624 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001625 } else {
1626 assert(SrcSize > DstSize && "Coercion is missing bits!");
1627
1628 // Otherwise do coercion through memory. This is stupid, but
1629 // simple.
1630 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1631 CGF.Builder.CreateStore(Src, Tmp);
1632 llvm::Value *Casted =
1633 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001634 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1635 // FIXME: Use better alignment / avoid requiring aligned load.
1636 Load->setAlignment(1);
1637 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001638 }
1639}
1640
Daniel Dunbar56273772008-09-17 00:51:38 +00001641/***/
1642
Daniel Dunbar88b53962009-02-02 22:03:45 +00001643bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001644 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001645}
1646
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001647const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001648CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001649 std::vector<const llvm::Type*> ArgTys;
1650
1651 const llvm::Type *ResultType = 0;
1652
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001653 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001654 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001655 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001656 case ABIArgInfo::Expand:
1657 assert(0 && "Invalid ABI kind for return argument");
1658
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001659 case ABIArgInfo::Direct:
1660 ResultType = ConvertType(RetTy);
1661 break;
1662
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001663 case ABIArgInfo::Indirect: {
1664 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001665 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001666 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001667 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1668 break;
1669 }
1670
Daniel Dunbar11434922009-01-26 21:26:08 +00001671 case ABIArgInfo::Ignore:
1672 ResultType = llvm::Type::VoidTy;
1673 break;
1674
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001675 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001676 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001677 break;
1678 }
1679
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001680 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1681 ie = FI.arg_end(); it != ie; ++it) {
1682 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001683
1684 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001685 case ABIArgInfo::Ignore:
1686 break;
1687
Daniel Dunbar56273772008-09-17 00:51:38 +00001688 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001689 ArgTys.push_back(AI.getCoerceToType());
1690 break;
1691
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001692 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001693 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001694 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1695 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001696 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001697 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001698
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001699 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001700 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001701 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001702
1703 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001704 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001705 break;
1706 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001707 }
1708
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001709 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001710}
1711
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001712void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001713 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001714 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001715 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001716 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001717
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001718 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001719 if (TargetDecl) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001720 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001721 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001722 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001723 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001724 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001725 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +00001726 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +00001727 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001728 }
1729
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001730 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001731 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001732 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001733 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001734 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001735 if (RetTy->isPromotableIntegerType()) {
1736 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001737 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001738 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001739 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001740 }
1741 }
1742 break;
1743
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001744 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001745 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001746 llvm::Attribute::StructRet |
1747 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001748 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001749 // sret disables readnone and readonly
1750 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1751 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001752 break;
1753
Daniel Dunbar11434922009-01-26 21:26:08 +00001754 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001755 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001756 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001757
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001758 case ABIArgInfo::Expand:
1759 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001760 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001761
Devang Patela2c69122008-09-26 22:53:57 +00001762 if (RetAttrs)
1763 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001764
1765 // FIXME: we need to honour command line settings also...
1766 // FIXME: RegParm should be reduced in case of nested functions and/or global
1767 // register variable.
1768 signed RegParm = 0;
1769 if (TargetDecl)
1770 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
1771 RegParm = RegParmAttr->getNumParams();
1772
1773 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001774 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1775 ie = FI.arg_end(); it != ie; ++it) {
1776 QualType ParamType = it->type;
1777 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001778 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001779
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001780 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001781 case ABIArgInfo::Coerce:
1782 break;
1783
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001784 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001785 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001786 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001787 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +00001788 // byval disables readnone and readonly.
1789 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1790 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001791 break;
1792
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001793 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001794 if (ParamType->isPromotableIntegerType()) {
1795 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001796 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001797 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001798 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001799 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001800 }
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001801 if (RegParm > 0 &&
1802 (ParamType->isIntegerType() || ParamType->isPointerType())) {
1803 RegParm -=
1804 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
1805 if (RegParm >= 0)
1806 Attributes |= llvm::Attribute::InReg;
1807 }
1808 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001809 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +00001810
Daniel Dunbar11434922009-01-26 21:26:08 +00001811 case ABIArgInfo::Ignore:
1812 // Skip increment, no matching LLVM parameter.
1813 continue;
1814
Daniel Dunbar56273772008-09-17 00:51:38 +00001815 case ABIArgInfo::Expand: {
1816 std::vector<const llvm::Type*> Tys;
1817 // FIXME: This is rather inefficient. Do we ever actually need
1818 // to do anything here? The result should be just reconstructed
1819 // on the other side, so extension should be a non-issue.
1820 getTypes().GetExpandedTypes(ParamType, Tys);
1821 Index += Tys.size();
1822 continue;
1823 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001824 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001825
Devang Patel761d7f72008-09-25 21:02:23 +00001826 if (Attributes)
1827 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001828 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001829 }
Devang Patela2c69122008-09-26 22:53:57 +00001830 if (FuncAttrs)
1831 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001832}
1833
Daniel Dunbar88b53962009-02-02 22:03:45 +00001834void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1835 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001836 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001837 // FIXME: We no longer need the types from FunctionArgList; lift up
1838 // and simplify.
1839
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001840 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1841 llvm::Function::arg_iterator AI = Fn->arg_begin();
1842
1843 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001844 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001845 AI->setName("agg.result");
1846 ++AI;
1847 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001848
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001849 assert(FI.arg_size() == Args.size() &&
1850 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001851 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001852 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001853 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001854 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001855 QualType Ty = info_it->type;
1856 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001857
1858 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001859 case ABIArgInfo::Indirect: {
1860 llvm::Value* V = AI;
1861 if (hasAggregateLLVMType(Ty)) {
1862 // Do nothing, aggregates and complex variables are accessed by
1863 // reference.
1864 } else {
1865 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001866 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001867 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1868 // This must be a promotion, for something like
1869 // "void a(x) short x; {..."
1870 V = EmitScalarConversion(V, Ty, Arg->getType());
1871 }
1872 }
1873 EmitParmDecl(*Arg, V);
1874 break;
1875 }
1876
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001877 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001878 assert(AI != Fn->arg_end() && "Argument mismatch!");
1879 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001880 if (hasAggregateLLVMType(Ty)) {
1881 // Create a temporary alloca to hold the argument; the rest of
1882 // codegen expects to access aggregates & complex values by
1883 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001884 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001885 Builder.CreateStore(AI, V);
1886 } else {
1887 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1888 // This must be a promotion, for something like
1889 // "void a(x) short x; {..."
1890 V = EmitScalarConversion(V, Ty, Arg->getType());
1891 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001892 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001893 EmitParmDecl(*Arg, V);
1894 break;
1895 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001896
1897 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001898 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001899 // we need to create a temporary and reconstruct it from the
1900 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001901 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001902 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001903 (Name + ".addr").c_str());
1904 // FIXME: What are the right qualifiers here?
1905 llvm::Function::arg_iterator End =
1906 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1907 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001908
Daniel Dunbar56273772008-09-17 00:51:38 +00001909 // Name the arguments used in expansion and increment AI.
1910 unsigned Index = 0;
1911 for (; AI != End; ++AI, ++Index)
1912 AI->setName(Name + "." + llvm::utostr(Index));
1913 continue;
1914 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001915
1916 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001917 // Initialize the local variable appropriately.
1918 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001919 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001920 } else {
1921 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1922 }
1923
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001924 // Skip increment, no matching LLVM parameter.
1925 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001926
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001927 case ABIArgInfo::Coerce: {
1928 assert(AI != Fn->arg_end() && "Argument mismatch!");
1929 // FIXME: This is very wasteful; EmitParmDecl is just going to
1930 // drop the result in a new alloca anyway, so we could just
1931 // store into that directly if we broke the abstraction down
1932 // more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001933 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001934 CreateCoercedStore(AI, V, *this);
1935 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001936 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001937 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001938 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1939 // This must be a promotion, for something like
1940 // "void a(x) short x; {..."
1941 V = EmitScalarConversion(V, Ty, Arg->getType());
1942 }
1943 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001944 EmitParmDecl(*Arg, V);
1945 break;
1946 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001947 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001948
1949 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001950 }
1951 assert(AI == Fn->arg_end() && "Argument mismatch!");
1952}
1953
Daniel Dunbar88b53962009-02-02 22:03:45 +00001954void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001955 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001956 llvm::Value *RV = 0;
1957
1958 // Functions with no result always return void.
1959 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001960 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001961 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001962
1963 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001964 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001965 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001966 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1967 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1968 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1969 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1970 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001971 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1972 false);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001973 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001974 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001975
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001976 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001977 // The internal return value temp always will have
1978 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001979 RV = Builder.CreateLoad(ReturnValue);
1980 break;
1981
Daniel Dunbar11434922009-01-26 21:26:08 +00001982 case ABIArgInfo::Ignore:
1983 break;
1984
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001985 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001986 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001987 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001988
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001989 case ABIArgInfo::Expand:
1990 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001991 }
1992 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001993
1994 if (RV) {
1995 Builder.CreateRet(RV);
1996 } else {
1997 Builder.CreateRetVoid();
1998 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001999}
2000
Anders Carlsson0139bb92009-04-08 20:47:54 +00002001RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
2002 return EmitAnyExprToTemp(E);
2003}
2004
Daniel Dunbar88b53962009-02-02 22:03:45 +00002005RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
2006 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002007 const CallArgList &CallArgs,
2008 const Decl *TargetDecl) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00002009 // FIXME: We no longer need the types from CallArgs; lift up and
2010 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002011 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002012
2013 // Handle struct-return functions by passing a pointer to the
2014 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002015 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00002016 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00002017 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002018 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002019 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002020 }
2021
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00002022 assert(CallInfo.arg_size() == CallArgs.size() &&
2023 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00002024 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002025 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00002026 I != E; ++I, ++info_it) {
2027 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002028 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00002029
2030 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002031 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00002032 if (RV.isScalar() || RV.isComplex()) {
2033 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002034 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00002035 if (RV.isScalar())
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002036 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00002037 else
2038 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
2039 } else {
2040 Args.push_back(RV.getAggregateAddr());
2041 }
2042 break;
2043
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002044 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00002045 if (RV.isScalar()) {
2046 Args.push_back(RV.getScalarVal());
2047 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002048 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
2049 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
2050 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
2051 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00002052 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002053 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00002054 }
2055 break;
2056
Daniel Dunbar11434922009-01-26 21:26:08 +00002057 case ABIArgInfo::Ignore:
2058 break;
2059
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002060 case ABIArgInfo::Coerce: {
2061 // FIXME: Avoid the conversion through memory if possible.
2062 llvm::Value *SrcPtr;
2063 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00002064 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002065 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002066 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002067 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002068 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
2069 } else
2070 SrcPtr = RV.getAggregateAddr();
2071 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2072 *this));
2073 break;
2074 }
2075
Daniel Dunbar56273772008-09-17 00:51:38 +00002076 case ABIArgInfo::Expand:
2077 ExpandTypeToArgs(I->second, RV, Args);
2078 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002079 }
2080 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002081
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002082 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel761d7f72008-09-25 21:02:23 +00002083 CodeGen::AttributeListType AttributeList;
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002084 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002085 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
2086 AttributeList.end());
Daniel Dunbar725ad312009-01-31 02:19:00 +00002087
Daniel Dunbard14151d2009-03-02 04:32:35 +00002088 llvm::CallSite CS;
2089 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
2090 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002091 } else {
2092 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbard14151d2009-03-02 04:32:35 +00002093 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
2094 &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00002095 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00002096 }
2097
Daniel Dunbard14151d2009-03-02 04:32:35 +00002098 CS.setAttributes(Attrs);
2099 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
2100 CS.setCallingConv(F->getCallingConv());
2101
2102 // If the call doesn't return, finish the basic block and clear the
2103 // insertion point; this allows the rest of IRgen to discard
2104 // unreachable code.
2105 if (CS.doesNotReturn()) {
2106 Builder.CreateUnreachable();
2107 Builder.ClearInsertionPoint();
2108
2109 // FIXME: For now, emit a dummy basic block because expr
2110 // emitters in generally are not ready to handle emitting
2111 // expressions at unreachable points.
2112 EnsureInsertPoint();
2113
2114 // Return a reasonable RValue.
2115 return GetUndefRValue(RetTy);
2116 }
2117
2118 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner34030842009-03-22 00:32:22 +00002119 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002120 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002121
2122 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00002123 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002124 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00002125 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00002126 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00002127 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00002128 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002129
Daniel Dunbar46327aa2009-02-03 06:17:37 +00002130 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002131 if (RetTy->isAnyComplexType()) {
2132 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2133 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2134 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00002135 }
2136 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002137 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00002138 Builder.CreateStore(CI, V);
2139 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002140 }
2141 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002142
Daniel Dunbar11434922009-01-26 21:26:08 +00002143 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00002144 // If we are ignoring an argument that had a result, make sure to
2145 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00002146 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00002147
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002148 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00002149 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00002150 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00002151 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00002152 if (RetTy->isAnyComplexType())
2153 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner34030842009-03-22 00:32:22 +00002154 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00002155 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +00002156 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00002157 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002158
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00002159 case ABIArgInfo::Expand:
2160 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002161 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00002162
2163 assert(0 && "Unhandled ABIArgInfo::Kind");
2164 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00002165}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00002166
2167/* VarArg handling */
2168
2169llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2170 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2171}