blob: 945d52faa3225e643e373398fca7aeeb42715aea [file] [log] [blame]
Daniel Dunbara8f02052008-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 Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar51a2d192009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Patel98bfe502008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbar90e43452009-03-02 04:32:35 +000025#include "llvm/Support/CallSite.h"
Daniel Dunbare09a9692009-01-24 08:32:22 +000026#include "llvm/Support/CommandLine.h"
Daniel Dunbar3cfcec72009-02-12 09:04:14 +000027#include "llvm/Support/MathExtras.h"
Daniel Dunbar9f4874e2009-02-04 23:24:38 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000030
31#include "ABIInfo.h"
32
Daniel Dunbara8f02052008-09-08 21:33:45 +000033using namespace clang;
34using namespace CodeGen;
35
36/***/
37
Daniel Dunbara8f02052008-09-08 21:33:45 +000038// FIXME: Use iterator and sidestep silly type array creation.
39
Daniel Dunbar34bda882009-02-02 23:23:47 +000040const
Douglas Gregor4fa58902009-02-26 23:50:07 +000041CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000042 return getFunctionInfo(FTNP->getResultType(),
43 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000044}
45
Daniel Dunbar34bda882009-02-02 23:23:47 +000046const
Douglas Gregor4fa58902009-02-26 23:50:07 +000047CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000048 llvm::SmallVector<QualType, 16> ArgTys;
49 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000050 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000051 ArgTys.push_back(FTP->getArgType(i));
52 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000053}
54
Daniel Dunbar34bda882009-02-02 23:23:47 +000055const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000056 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +000057 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar34bda882009-02-02 23:23:47 +000058 return getFunctionInfo(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +000059 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000060}
61
Daniel Dunbar34bda882009-02-02 23:23:47 +000062const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
63 llvm::SmallVector<QualType, 16> ArgTys;
64 ArgTys.push_back(MD->getSelfDecl()->getType());
65 ArgTys.push_back(Context.getObjCSelType());
66 // FIXME: Kill copy?
Chris Lattner9408eb12009-02-20 06:23:21 +000067 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbara8f02052008-09-08 21:33:45 +000068 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000069 ArgTys.push_back((*i)->getType());
70 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000071}
72
Daniel Dunbar34bda882009-02-02 23:23:47 +000073const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
74 const CallArgList &Args) {
75 // FIXME: Kill copy.
76 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000077 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
78 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000079 ArgTys.push_back(i->second);
80 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000081}
82
Daniel Dunbar34bda882009-02-02 23:23:47 +000083const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
84 const FunctionArgList &Args) {
85 // FIXME: Kill copy.
86 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +000087 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
88 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000089 ArgTys.push_back(i->second);
90 return getFunctionInfo(ResTy, ArgTys);
91}
92
93const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
94 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +000095 // Lookup or create unique function info.
96 llvm::FoldingSetNodeID ID;
97 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
98
99 void *InsertPos = 0;
100 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
101 if (FI)
102 return *FI;
103
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000104 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000105 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000106 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000107
108 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000109 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000110
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000111 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000112}
113
114/***/
115
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000116ABIInfo::~ABIInfo() {}
117
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000118void ABIArgInfo::dump() const {
119 fprintf(stderr, "(ABIArgInfo Kind=");
120 switch (TheKind) {
121 case Direct:
122 fprintf(stderr, "Direct");
123 break;
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000124 case Ignore:
125 fprintf(stderr, "Ignore");
126 break;
127 case Coerce:
128 fprintf(stderr, "Coerce Type=");
129 getCoerceToType()->print(llvm::errs());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000130 break;
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000131 case Indirect:
132 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000133 break;
134 case Expand:
135 fprintf(stderr, "Expand");
136 break;
137 }
138 fprintf(stderr, ")\n");
139}
140
141/***/
142
Daniel Dunbara7446422009-03-31 19:01:39 +0000143/// isEmptyRecord - Return true iff a structure has no non-empty
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000144/// members. Note that a structure with a flexible array member is not
145/// considered empty.
Daniel Dunbara7446422009-03-31 19:01:39 +0000146static bool isEmptyRecord(QualType T) {
147 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000148 if (!RT)
149 return 0;
150 const RecordDecl *RD = RT->getDecl();
151 if (RD->hasFlexibleArrayMember())
152 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000153 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000154 e = RD->field_end(); i != e; ++i) {
155 const FieldDecl *FD = *i;
Daniel Dunbara7446422009-03-31 19:01:39 +0000156 if (!isEmptyRecord(FD->getType()))
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000157 return false;
158 }
159 return true;
160}
161
162/// isSingleElementStruct - Determine if a structure is a "single
163/// element struct", i.e. it has exactly one non-empty field or
164/// exactly one field which is itself a single element
165/// struct. Structures with flexible array members are never
166/// considered single element structs.
167///
168/// \return The field declaration for the single non-empty field, if
169/// it exists.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000170static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000171 const RecordType *RT = T->getAsStructureType();
172 if (!RT)
173 return 0;
174
175 const RecordDecl *RD = RT->getDecl();
176 if (RD->hasFlexibleArrayMember())
177 return 0;
178
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000179 const Type *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000180 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000181 e = RD->field_end(); i != e; ++i) {
182 const FieldDecl *FD = *i;
183 QualType FT = FD->getType();
184
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000185 // Treat single element arrays as the element
186 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
187 if (AT->getSize().getZExtValue() == 1)
188 FT = AT->getElementType();
189
Daniel Dunbara7446422009-03-31 19:01:39 +0000190 if (isEmptyRecord(FT)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000191 // Ignore
192 } else if (Found) {
193 return 0;
194 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000195 Found = FT.getTypePtr();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000196 } else {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000197 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000198 if (!Found)
199 return 0;
200 }
201 }
202
203 return Found;
204}
205
206static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
207 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
208 return false;
209
210 uint64_t Size = Context.getTypeSize(Ty);
211 return Size == 32 || Size == 64;
212}
213
214static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
215 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000216 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000217 e = RD->field_end(); i != e; ++i) {
218 const FieldDecl *FD = *i;
219
220 if (!is32Or64BitBasicType(FD->getType(), Context))
221 return false;
222
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000223 // FIXME: Reject bitfields wholesale; there are two problems, we
224 // don't know how to expand them yet, and the predicate for
225 // telling if a bitfield still counts as "basic" is more
226 // complicated than what we were doing previously.
227 if (FD->isBitField())
228 return false;
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000229 }
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000230
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000231 return true;
232}
233
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000234namespace {
235/// DefaultABIInfo - The default implementation for ABI specific
236/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000237/// self-consistent and sensible LLVM IR generation, but does not
238/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000239class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000240 ABIArgInfo classifyReturnType(QualType RetTy,
241 ASTContext &Context) const;
242
243 ABIArgInfo classifyArgumentType(QualType RetTy,
244 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000245
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000246 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
247 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
248 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
249 it != ie; ++it)
250 it->info = classifyArgumentType(it->type, Context);
251 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000252
253 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
254 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000255};
256
257/// X86_32ABIInfo - The X86-32 ABI information.
258class X86_32ABIInfo : public ABIInfo {
Eli Friedman5e175802009-03-23 23:26:24 +0000259 bool IsDarwin;
260
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000261 static bool isRegisterSize(unsigned Size) {
262 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
263 }
264
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000265public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000266 ABIArgInfo classifyReturnType(QualType RetTy,
267 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000268
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000269 ABIArgInfo classifyArgumentType(QualType RetTy,
270 ASTContext &Context) const;
271
272 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
273 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
274 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
275 it != ie; ++it)
276 it->info = classifyArgumentType(it->type, Context);
277 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000278
279 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
280 CodeGenFunction &CGF) const;
Eli Friedman5e175802009-03-23 23:26:24 +0000281
282 X86_32ABIInfo(bool d) : ABIInfo(), IsDarwin(d) {}
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000283};
284}
285
286ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
287 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000288 if (RetTy->isVoidType()) {
289 return ABIArgInfo::getIgnore();
Daniel Dunbar2a7bb3f2009-04-01 06:13:08 +0000290 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
291 // On Darwin, some vectors are returned in registers.
292 if (IsDarwin) {
293 uint64_t Size = Context.getTypeSize(RetTy);
294
295 // 128-bit vectors are a special case; they are returned in
296 // registers and we need to make sure to pick a type the LLVM
297 // backend will like.
298 if (Size == 128)
299 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
300 2));
301
302 // Always return in register if it fits in a general purpose
303 // register, or if it is 64 bits and has a single element.
304 if ((Size == 8 || Size == 16 || Size == 32) ||
305 (Size == 64 && VT->getNumElements() == 1))
306 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
307
308 return ABIArgInfo::getIndirect(0);
309 }
310
311 return ABIArgInfo::getDirect();
Daniel Dunbareec02622009-02-03 06:30:17 +0000312 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedman5e175802009-03-23 23:26:24 +0000313 // Outside of Darwin, structs and unions are always indirect.
314 if (!IsDarwin && !RetTy->isAnyComplexType())
315 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000316 // Classify "single element" structs as their element type.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000317 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000318 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
319 // FIXME: This is gross, it would be nice if we could just
320 // pass back SeltTy and have clients deal with it. Is it worth
321 // supporting coerce to both LLVM and clang Types?
322 if (BT->isIntegerType()) {
323 uint64_t Size = Context.getTypeSize(SeltTy);
324 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
325 } else if (BT->getKind() == BuiltinType::Float) {
326 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
327 } else if (BT->getKind() == BuiltinType::Double) {
328 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
329 }
330 } else if (SeltTy->isPointerType()) {
331 // FIXME: It would be really nice if this could come out as
332 // the proper pointer type.
333 llvm::Type *PtrTy =
334 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
335 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000336 } else if (SeltTy->isVectorType()) {
337 // 64- and 128-bit vectors are never returned in a
338 // register when inside a structure.
339 uint64_t Size = Context.getTypeSize(RetTy);
340 if (Size == 64 || Size == 128)
341 return ABIArgInfo::getIndirect(0);
342
343 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000344 }
345 }
346
Daniel Dunbar73d66602008-09-10 07:04:09 +0000347 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000348 if (isRegisterSize(Size))
349 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
350
351 return ABIArgInfo::getIndirect(0);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000352 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000353 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000354 }
355}
356
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000357ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000358 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000359 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000360 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000361 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000362 if (const RecordType *RT = Ty->getAsStructureType())
363 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000364 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000365
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000366 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000367 uint64_t Size = Context.getTypeSize(Ty);
368 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000369 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000370
371 // Expand structs with size <= 128-bits which consist only of
372 // basic types (int, long long, float, double, xxx*). This is
373 // non-recursive and does not ignore empty fields.
374 if (const RecordType *RT = Ty->getAsStructureType()) {
375 if (Context.getTypeSize(Ty) <= 4*32 &&
376 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
377 return ABIArgInfo::getExpand();
378 }
379
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000380 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000381 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000382 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000383 }
384}
385
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000386llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
387 CodeGenFunction &CGF) const {
388 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
389 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
390
391 CGBuilderTy &Builder = CGF.Builder;
392 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
393 "ap");
394 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
395 llvm::Type *PTy =
396 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
397 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
398
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000399 uint64_t Offset =
400 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000401 llvm::Value *NextAddr =
402 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000403 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000404 "ap.next");
405 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
406
407 return AddrTyped;
408}
409
Daniel Dunbare09a9692009-01-24 08:32:22 +0000410namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000411/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000412class X86_64ABIInfo : public ABIInfo {
413 enum Class {
414 Integer = 0,
415 SSE,
416 SSEUp,
417 X87,
418 X87Up,
419 ComplexX87,
420 NoClass,
421 Memory
422 };
423
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000424 /// merge - Implement the X86_64 ABI merging algorithm.
425 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000426 /// Merge an accumulating classification \arg Accum with a field
427 /// classification \arg Field.
428 ///
429 /// \param Accum - The accumulating classification. This should
430 /// always be either NoClass or the result of a previous merge
431 /// call. In addition, this should never be Memory (the caller
432 /// should just return Memory for the aggregate).
433 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000434
Daniel Dunbare09a9692009-01-24 08:32:22 +0000435 /// classify - Determine the x86_64 register classes in which the
436 /// given type T should be passed.
437 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000438 /// \param Lo - The classification for the parts of the type
439 /// residing in the low word of the containing object.
440 ///
441 /// \param Hi - The classification for the parts of the type
442 /// residing in the high word of the containing object.
443 ///
444 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000445 /// containing object. Some parameters are classified different
446 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000447 ///
448 /// If a word is unused its result will be NoClass; if a type should
449 /// be passed in Memory then at least the classification of \arg Lo
450 /// will be Memory.
451 ///
452 /// The \arg Lo class will be NoClass iff the argument is ignored.
453 ///
454 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar92e88642009-02-17 07:55:55 +0000455 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000456 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000457 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000458
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000459 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
460 /// to coerce to, chose the best way to pass Ty in the same place
461 /// that \arg CoerceTo would be passed, but while keeping the
462 /// emitted code as simple as possible.
463 ///
464 /// FIXME: Note, this should be cleaned up to just take an
465 /// enumeration of all the ways we might want to pass things,
466 /// instead of constructing an LLVM type. This makes this code more
467 /// explicit, and it makes it clearer that we are also doing this
468 /// for correctness in the case of passing scalar types.
469 ABIArgInfo getCoerceResult(QualType Ty,
470 const llvm::Type *CoerceTo,
471 ASTContext &Context) const;
472
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000473 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000474 ASTContext &Context) const;
475
476 ABIArgInfo classifyArgumentType(QualType Ty,
477 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000478 unsigned &neededInt,
479 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000480
481public:
482 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000483
484 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
485 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000486};
487}
488
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000489X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
490 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000491 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
492 // classified recursively so that always two fields are
493 // considered. The resulting class is calculated according to
494 // the classes of the fields in the eightbyte:
495 //
496 // (a) If both classes are equal, this is the resulting class.
497 //
498 // (b) If one of the classes is NO_CLASS, the resulting class is
499 // the other class.
500 //
501 // (c) If one of the classes is MEMORY, the result is the MEMORY
502 // class.
503 //
504 // (d) If one of the classes is INTEGER, the result is the
505 // INTEGER.
506 //
507 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
508 // MEMORY is used as class.
509 //
510 // (f) Otherwise class SSE is used.
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000511
512 // Accum should never be memory (we should have returned) or
513 // ComplexX87 (because this cannot be passed in a structure).
514 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000515 "Invalid accumulated classification during merge.");
516 if (Accum == Field || Field == NoClass)
517 return Accum;
518 else if (Field == Memory)
519 return Memory;
520 else if (Accum == NoClass)
521 return Field;
522 else if (Accum == Integer || Field == Integer)
523 return Integer;
524 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
525 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000526 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000527 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000528}
529
Daniel Dunbare09a9692009-01-24 08:32:22 +0000530void X86_64ABIInfo::classify(QualType Ty,
531 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000532 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000533 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000534 // FIXME: This code can be simplified by introducing a simple value
535 // class for Class pairs with appropriate constructor methods for
536 // the various situations.
537
Daniel Dunbard97f5952009-02-22 04:48:22 +0000538 // FIXME: Some of the split computations are wrong; unaligned
539 // vectors shouldn't be passed in registers for example, so there is
540 // no chance they can straddle an eightbyte. Verify & simplify.
541
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000542 Lo = Hi = NoClass;
543
544 Class &Current = OffsetBase < 64 ? Lo : Hi;
545 Current = Memory;
546
Daniel Dunbare09a9692009-01-24 08:32:22 +0000547 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
548 BuiltinType::Kind k = BT->getKind();
549
Daniel Dunbar1358b202009-01-26 21:26:08 +0000550 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000551 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000552 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000553 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000554 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000555 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000556 } else if (k == BuiltinType::LongDouble) {
557 Lo = X87;
558 Hi = X87Up;
559 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000560 // FIXME: _Decimal32 and _Decimal64 are SSE.
561 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000562 // FIXME: __int128 is (Integer, Integer).
Anders Carlsson1d234462009-02-26 17:31:15 +0000563 } else if (const EnumType *ET = Ty->getAsEnumType()) {
564 // Classify the underlying integer type.
565 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000566 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000567 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000568 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000569 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000570 if (Size == 32) {
571 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
572 // float> as integer.
573 Current = Integer;
574
575 // If this type crosses an eightbyte boundary, it should be
576 // split.
577 uint64_t EB_Real = (OffsetBase) / 64;
578 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
579 if (EB_Real != EB_Imag)
580 Hi = Lo;
581 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000582 // gcc passes <1 x double> in memory. :(
583 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000584 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000585
586 // gcc passes <1 x long long> as INTEGER.
587 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
588 Current = Integer;
589 else
590 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000591
592 // If this type crosses an eightbyte boundary, it should be
593 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000594 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000595 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000596 } else if (Size == 128) {
597 Lo = SSE;
598 Hi = SSEUp;
599 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000600 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000601 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000602
Daniel Dunbare413f532009-01-30 18:40:10 +0000603 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000604 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000605 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000606 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000607 else if (Size <= 128)
608 Lo = Hi = Integer;
609 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000610 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000611 else if (ET == Context.DoubleTy)
612 Lo = Hi = SSE;
613 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000614 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000615
616 // If this complex type crosses an eightbyte boundary then it
617 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000618 uint64_t EB_Real = (OffsetBase) / 64;
619 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000620 if (Hi == NoClass && EB_Real != EB_Imag)
621 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000622 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
623 // Arrays are treated like structures.
624
625 uint64_t Size = Context.getTypeSize(Ty);
626
627 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
628 // than two eightbytes, ..., it has class MEMORY.
629 if (Size > 128)
630 return;
631
632 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
633 // fields, it has class MEMORY.
634 //
635 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000636 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000637 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000638
639 // Otherwise implement simplified merge. We could be smarter about
640 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000641 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000642 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
643 uint64_t ArraySize = AT->getSize().getZExtValue();
644 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
645 Class FieldLo, FieldHi;
646 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000647 Lo = merge(Lo, FieldLo);
648 Hi = merge(Hi, FieldHi);
649 if (Lo == Memory || Hi == Memory)
650 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000651 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000652
653 // Do post merger cleanup (see below). Only case we worry about is Memory.
654 if (Hi == Memory)
655 Lo = Memory;
656 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000657 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000658 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000659
660 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
661 // than two eightbytes, ..., it has class MEMORY.
662 if (Size > 128)
663 return;
664
665 const RecordDecl *RD = RT->getDecl();
666
667 // Assume variable sized types are passed in memory.
668 if (RD->hasFlexibleArrayMember())
669 return;
670
671 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
672
673 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000674 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000675 unsigned idx = 0;
676 for (RecordDecl::field_iterator i = RD->field_begin(),
677 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000678 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000679 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000680
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000681 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
682 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000683 //
684 // Note, skip this test for bitfields, see below.
685 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000686 Lo = Memory;
687 return;
688 }
689
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000690 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000691 //
692 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
693 // exceeds a single eightbyte, each is classified
694 // separately. Each eightbyte gets initialized to class
695 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000696 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000697
698 // Bitfields require special handling, they do not force the
699 // structure to be passed in memory even if unaligned, and
700 // therefore they can straddle an eightbyte.
701 if (BitField) {
702 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
703 uint64_t Size =
704 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
705
706 uint64_t EB_Lo = Offset / 64;
707 uint64_t EB_Hi = (Offset + Size - 1) / 64;
708 FieldLo = FieldHi = NoClass;
709 if (EB_Lo) {
710 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
711 FieldLo = NoClass;
712 FieldHi = Integer;
713 } else {
714 FieldLo = Integer;
715 FieldHi = EB_Hi ? Integer : NoClass;
716 }
717 } else
718 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000719 Lo = merge(Lo, FieldLo);
720 Hi = merge(Hi, FieldHi);
721 if (Lo == Memory || Hi == Memory)
722 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000723 }
724
725 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
726 //
727 // (a) If one of the classes is MEMORY, the whole argument is
728 // passed in memory.
729 //
730 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
731
732 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000733 // the merge (just bail).
734 //
735 // The second condition occurs in the case of unions; for example
736 // union { _Complex double; unsigned; }.
737 if (Hi == Memory)
738 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000739 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000740 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000741 }
742}
743
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000744ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
745 const llvm::Type *CoerceTo,
746 ASTContext &Context) const {
747 if (CoerceTo == llvm::Type::Int64Ty) {
748 // Integer and pointer types will end up in a general purpose
749 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000750 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000751 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000752
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000753 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000754 // FIXME: It would probably be better to make CGFunctionInfo only
755 // map using canonical types than to canonize here.
756 QualType CTy = Context.getCanonicalType(Ty);
757
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000758 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000759 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000760 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000761
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000762 }
763
764 return ABIArgInfo::getCoerce(CoerceTo);
765}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000766
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000767ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
768 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000769 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
770 // classification algorithm.
771 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000772 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000773
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000774 // Check some invariants.
775 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
776 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
777 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
778
Daniel Dunbare09a9692009-01-24 08:32:22 +0000779 const llvm::Type *ResType = 0;
780 switch (Lo) {
781 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000782 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000783
784 case SSEUp:
785 case X87Up:
786 assert(0 && "Invalid classification for lo word.");
787
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000788 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000789 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000790 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000791 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000792
793 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
794 // available register of the sequence %rax, %rdx is used.
795 case Integer:
796 ResType = llvm::Type::Int64Ty; break;
797
798 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
799 // available SSE register of the sequence %xmm0, %xmm1 is used.
800 case SSE:
801 ResType = llvm::Type::DoubleTy; break;
802
803 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
804 // returned on the X87 stack in %st0 as 80-bit x87 number.
805 case X87:
806 ResType = llvm::Type::X86_FP80Ty; break;
807
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000808 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
809 // part of the value is returned in %st0 and the imaginary part in
810 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000811 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000812 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000813 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
814 llvm::Type::X86_FP80Ty,
815 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000816 break;
817 }
818
819 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000820 // Memory was handled previously and X87 should
821 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000822 case Memory:
823 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000824 assert(0 && "Invalid classification for hi word.");
825
Daniel Dunbar92e88642009-02-17 07:55:55 +0000826 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000827 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000828
Daniel Dunbare09a9692009-01-24 08:32:22 +0000829 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000830 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
831 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000832 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000833 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
834 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000835
836 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
837 // is passed in the upper half of the last used SSE register.
838 //
839 // SSEUP should always be preceeded by SSE, just widen.
840 case SSEUp:
841 assert(Lo == SSE && "Unexpected SSEUp classification.");
842 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
843 break;
844
845 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000846 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000847 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000848 // If X87Up is preceeded by X87, we don't need to do
849 // anything. However, in some cases with unions it may not be
850 // preceeded by X87. In such situations we follow gcc and pass the
851 // extra bits in an SSE reg.
852 if (Lo != X87)
853 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000854 break;
855 }
856
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000857 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000858}
859
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000860ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000861 unsigned &neededInt,
862 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000863 X86_64ABIInfo::Class Lo, Hi;
864 classify(Ty, Context, 0, Lo, Hi);
865
866 // Check some invariants.
867 // FIXME: Enforce these by construction.
868 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
869 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
870 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
871
Daniel Dunbare978cb92009-02-10 17:06:09 +0000872 neededInt = 0;
873 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000874 const llvm::Type *ResType = 0;
875 switch (Lo) {
876 case NoClass:
877 return ABIArgInfo::getIgnore();
878
879 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
880 // on the stack.
881 case Memory:
882
883 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
884 // COMPLEX_X87, it is passed in memory.
885 case X87:
886 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000887 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000888
889 case SSEUp:
890 case X87Up:
891 assert(0 && "Invalid classification for lo word.");
892
893 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
894 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
895 // and %r9 is used.
896 case Integer:
897 ++neededInt;
898 ResType = llvm::Type::Int64Ty;
899 break;
900
901 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
902 // available SSE register is used, the registers are taken in the
903 // order from %xmm0 to %xmm7.
904 case SSE:
905 ++neededSSE;
906 ResType = llvm::Type::DoubleTy;
907 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000908 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000909
910 switch (Hi) {
911 // Memory was handled previously, ComplexX87 and X87 should
912 // never occur as hi classes, and X87Up must be preceed by X87,
913 // which is passed in memory.
914 case Memory:
915 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000916 case ComplexX87:
917 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000918 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000919
920 case NoClass: break;
921 case Integer:
922 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
923 ++neededInt;
924 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000925
926 // X87Up generally doesn't occur here (long double is passed in
927 // memory), except in situations involving unions.
928 case X87Up:
929 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000930 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
931 ++neededSSE;
932 break;
933
934 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
935 // eightbyte is passed in the upper half of the last used SSE
936 // register.
937 case SSEUp:
938 assert(Lo == SSE && "Unexpected SSEUp classification.");
939 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
940 break;
941 }
942
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000943 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000944}
945
946void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
947 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
948
949 // Keep track of the number of assigned registers.
950 unsigned freeIntRegs = 6, freeSSERegs = 8;
951
952 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
953 // get assigned (in left-to-right order) for passing as follows...
954 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +0000955 it != ie; ++it) {
956 unsigned neededInt, neededSSE;
957 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
958
959 // AMD64-ABI 3.2.3p3: If there are no registers available for any
960 // eightbyte of an argument, the whole argument is passed on the
961 // stack. If registers have already been assigned for some
962 // eightbytes of such an argument, the assignments get reverted.
963 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
964 freeIntRegs -= neededInt;
965 freeSSERegs -= neededSSE;
966 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000967 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +0000968 }
969 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000970}
971
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000972static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
973 QualType Ty,
974 CodeGenFunction &CGF) {
975 llvm::Value *overflow_arg_area_p =
976 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
977 llvm::Value *overflow_arg_area =
978 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
979
980 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
981 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000982 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000983 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000984 // Note that we follow the ABI & gcc here, even though the type
985 // could in theory have an alignment greater than 16. This case
986 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000987
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000988 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
989 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
990 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
991 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
992 llvm::Type::Int64Ty);
993 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
994 overflow_arg_area =
995 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
996 overflow_arg_area->getType(),
997 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000998 }
999
1000 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1001 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1002 llvm::Value *Res =
1003 CGF.Builder.CreateBitCast(overflow_arg_area,
1004 llvm::PointerType::getUnqual(LTy));
1005
1006 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1007 // l->overflow_arg_area + sizeof(type).
1008 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1009 // an 8 byte boundary.
1010
1011 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1012 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1013 (SizeInBytes + 7) & ~7);
1014 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1015 "overflow_arg_area.next");
1016 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1017
1018 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1019 return Res;
1020}
1021
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001022llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1023 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001024 // Assume that va_list type is correct; should be pointer to LLVM type:
1025 // struct {
1026 // i32 gp_offset;
1027 // i32 fp_offset;
1028 // i8* overflow_arg_area;
1029 // i8* reg_save_area;
1030 // };
1031 unsigned neededInt, neededSSE;
1032 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1033 neededInt, neededSSE);
1034
1035 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1036 // in the registers. If not go to step 7.
1037 if (!neededInt && !neededSSE)
1038 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1039
1040 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1041 // general purpose registers needed to pass type and num_fp to hold
1042 // the number of floating point registers needed.
1043
1044 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1045 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1046 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1047 //
1048 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1049 // register save space).
1050
1051 llvm::Value *InRegs = 0;
1052 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1053 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1054 if (neededInt) {
1055 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1056 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1057 InRegs =
1058 CGF.Builder.CreateICmpULE(gp_offset,
1059 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1060 48 - neededInt * 8),
1061 "fits_in_gp");
1062 }
1063
1064 if (neededSSE) {
1065 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1066 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1067 llvm::Value *FitsInFP =
1068 CGF.Builder.CreateICmpULE(fp_offset,
1069 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001070 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001071 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001072 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001073 }
1074
1075 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1076 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1077 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1078 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1079
1080 // Emit code to load the value if it was passed in registers.
1081
1082 CGF.EmitBlock(InRegBlock);
1083
1084 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1085 // an offset of l->gp_offset and/or l->fp_offset. This may require
1086 // copying to a temporary location in case the parameter is passed
1087 // in different register classes or requires an alignment greater
1088 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001089 //
1090 // FIXME: This really results in shameful code when we end up
1091 // needing to collect arguments from different places; often what
1092 // should result in a simple assembling of a structure from
1093 // scattered addresses has many more loads than necessary. Can we
1094 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001095 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1096 llvm::Value *RegAddr =
1097 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1098 "reg_save_area");
1099 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001100 // FIXME: Cleanup.
1101 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1102 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1103 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1104 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1105 const llvm::Type *TyLo = ST->getElementType(0);
1106 const llvm::Type *TyHi = ST->getElementType(1);
1107 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1108 "Unexpected ABI info for mixed regs");
1109 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1110 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1111 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1112 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1113 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1114 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1115 llvm::Value *V =
1116 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1117 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1118 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1119 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1120
1121 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001122 } else if (neededInt) {
1123 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1124 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1125 llvm::PointerType::getUnqual(LTy));
1126 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001127 if (neededSSE == 1) {
1128 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1129 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1130 llvm::PointerType::getUnqual(LTy));
1131 } else {
1132 assert(neededSSE == 2 && "Invalid number of needed registers!");
1133 // SSE registers are spaced 16 bytes apart in the register save
1134 // area, we need to collect the two eightbytes together.
1135 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1136 llvm::Value *RegAddrHi =
1137 CGF.Builder.CreateGEP(RegAddrLo,
1138 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1139 const llvm::Type *DblPtrTy =
1140 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1141 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1142 llvm::Type::DoubleTy,
1143 NULL);
1144 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1145 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1146 DblPtrTy));
1147 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1148 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1149 DblPtrTy));
1150 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1151 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1152 llvm::PointerType::getUnqual(LTy));
1153 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001154 }
1155
1156 // AMD64-ABI 3.5.7p5: Step 5. Set:
1157 // l->gp_offset = l->gp_offset + num_gp * 8
1158 // l->fp_offset = l->fp_offset + num_fp * 16.
1159 if (neededInt) {
1160 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1161 neededInt * 8);
1162 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1163 gp_offset_p);
1164 }
1165 if (neededSSE) {
1166 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1167 neededSSE * 16);
1168 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1169 fp_offset_p);
1170 }
1171 CGF.EmitBranch(ContBlock);
1172
1173 // Emit code to load the value if it was passed in memory.
1174
1175 CGF.EmitBlock(InMemBlock);
1176 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1177
1178 // Return the appropriate result.
1179
1180 CGF.EmitBlock(ContBlock);
1181 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1182 "vaarg.addr");
1183 ResAddr->reserveOperandSpace(2);
1184 ResAddr->addIncoming(RegAddr, InRegBlock);
1185 ResAddr->addIncoming(MemAddr, InMemBlock);
1186
1187 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001188}
1189
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001190class ARMABIInfo : public ABIInfo {
1191 ABIArgInfo classifyReturnType(QualType RetTy,
1192 ASTContext &Context) const;
1193
1194 ABIArgInfo classifyArgumentType(QualType RetTy,
1195 ASTContext &Context) const;
1196
1197 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1198
1199 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1200 CodeGenFunction &CGF) const;
1201};
1202
1203void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1204 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1205 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1206 it != ie; ++it) {
1207 it->info = classifyArgumentType(it->type, Context);
1208 }
1209}
1210
1211ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1212 ASTContext &Context) const {
1213 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1214 return ABIArgInfo::getDirect();
1215 }
1216 // FIXME: This is kind of nasty... but there isn't much choice
1217 // because the ARM backend doesn't support byval.
1218 // FIXME: This doesn't handle alignment > 64 bits.
1219 const llvm::Type* ElemTy;
1220 unsigned SizeRegs;
1221 if (Context.getTypeAlign(Ty) > 32) {
1222 ElemTy = llvm::Type::Int64Ty;
1223 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1224 } else {
1225 ElemTy = llvm::Type::Int32Ty;
1226 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1227 }
1228 std::vector<const llvm::Type*> LLVMFields;
1229 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1230 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1231 return ABIArgInfo::getCoerce(STy);
1232}
1233
1234ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1235 ASTContext &Context) const {
1236 if (RetTy->isVoidType()) {
1237 return ABIArgInfo::getIgnore();
1238 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1239 // Aggregates <= 4 bytes are returned in r0; other aggregates
1240 // are returned indirectly.
1241 uint64_t Size = Context.getTypeSize(RetTy);
1242 if (Size <= 32)
1243 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1244 return ABIArgInfo::getIndirect(0);
1245 } else {
1246 return ABIArgInfo::getDirect();
1247 }
1248}
1249
1250llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1251 CodeGenFunction &CGF) const {
1252 // FIXME: Need to handle alignment
1253 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1254 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1255
1256 CGBuilderTy &Builder = CGF.Builder;
1257 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1258 "ap");
1259 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1260 llvm::Type *PTy =
1261 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1262 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1263
1264 uint64_t Offset =
1265 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1266 llvm::Value *NextAddr =
1267 Builder.CreateGEP(Addr,
1268 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1269 "ap.next");
1270 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1271
1272 return AddrTyped;
1273}
1274
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001275ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001276 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001277 if (RetTy->isVoidType()) {
1278 return ABIArgInfo::getIgnore();
1279 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001280 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001281 } else {
1282 return ABIArgInfo::getDirect();
1283 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001284}
1285
1286ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001287 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001288 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001289 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001290 } else {
1291 return ABIArgInfo::getDirect();
1292 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001293}
1294
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001295llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1296 CodeGenFunction &CGF) const {
1297 return 0;
1298}
1299
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001300const ABIInfo &CodeGenTypes::getABIInfo() const {
1301 if (TheABIInfo)
1302 return *TheABIInfo;
1303
1304 // For now we just cache this in the CodeGenTypes and don't bother
1305 // to free it.
1306 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1307 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman5e175802009-03-23 23:26:24 +00001308 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001309 switch (getContext().Target.getPointerWidth(0)) {
1310 case 32:
Eli Friedman5e175802009-03-23 23:26:24 +00001311 return *(TheABIInfo = new X86_32ABIInfo(IsDarwin));
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001312 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001313 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001314 }
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001315 } else if (strcmp(TargetPrefix, "arm") == 0) {
1316 // FIXME: Support for OABI?
1317 return *(TheABIInfo = new ARMABIInfo());
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001318 }
1319
1320 return *(TheABIInfo = new DefaultABIInfo);
1321}
1322
Daniel Dunbare126ab12008-09-10 02:41:04 +00001323/***/
1324
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001325CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1326 const llvm::SmallVector<QualType, 16> &ArgTys) {
1327 NumArgs = ArgTys.size();
1328 Args = new ArgInfo[1 + NumArgs];
1329 Args[0].type = ResTy;
1330 for (unsigned i = 0; i < NumArgs; ++i)
1331 Args[1 + i].type = ArgTys[i];
1332}
1333
1334/***/
1335
Daniel Dunbar04d35782008-09-17 00:51:38 +00001336void CodeGenTypes::GetExpandedTypes(QualType Ty,
1337 std::vector<const llvm::Type*> &ArgTys) {
1338 const RecordType *RT = Ty->getAsStructureType();
1339 assert(RT && "Can only expand structure types.");
1340 const RecordDecl *RD = RT->getDecl();
1341 assert(!RD->hasFlexibleArrayMember() &&
1342 "Cannot expand structure with flexible array.");
1343
Douglas Gregor5d764842009-01-09 17:18:27 +00001344 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001345 e = RD->field_end(); i != e; ++i) {
1346 const FieldDecl *FD = *i;
1347 assert(!FD->isBitField() &&
1348 "Cannot expand structure with bit-field members.");
1349
1350 QualType FT = FD->getType();
1351 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1352 GetExpandedTypes(FT, ArgTys);
1353 } else {
1354 ArgTys.push_back(ConvertType(FT));
1355 }
1356 }
1357}
1358
1359llvm::Function::arg_iterator
1360CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1361 llvm::Function::arg_iterator AI) {
1362 const RecordType *RT = Ty->getAsStructureType();
1363 assert(RT && "Can only expand structure types.");
1364
1365 RecordDecl *RD = RT->getDecl();
1366 assert(LV.isSimple() &&
1367 "Unexpected non-simple lvalue during struct expansion.");
1368 llvm::Value *Addr = LV.getAddress();
1369 for (RecordDecl::field_iterator i = RD->field_begin(),
1370 e = RD->field_end(); i != e; ++i) {
1371 FieldDecl *FD = *i;
1372 QualType FT = FD->getType();
1373
1374 // FIXME: What are the right qualifiers here?
1375 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1376 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1377 AI = ExpandTypeFromArgs(FT, LV, AI);
1378 } else {
1379 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1380 ++AI;
1381 }
1382 }
1383
1384 return AI;
1385}
1386
1387void
1388CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1389 llvm::SmallVector<llvm::Value*, 16> &Args) {
1390 const RecordType *RT = Ty->getAsStructureType();
1391 assert(RT && "Can only expand structure types.");
1392
1393 RecordDecl *RD = RT->getDecl();
1394 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1395 llvm::Value *Addr = RV.getAggregateAddr();
1396 for (RecordDecl::field_iterator i = RD->field_begin(),
1397 e = RD->field_end(); i != e; ++i) {
1398 FieldDecl *FD = *i;
1399 QualType FT = FD->getType();
1400
1401 // FIXME: What are the right qualifiers here?
1402 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1403 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1404 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1405 } else {
1406 RValue RV = EmitLoadOfLValue(LV, FT);
1407 assert(RV.isScalar() &&
1408 "Unexpected non-scalar rvalue during struct expansion.");
1409 Args.push_back(RV.getScalarVal());
1410 }
1411 }
1412}
1413
Daniel Dunbar84379912009-02-02 19:06:38 +00001414/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1415/// a pointer to an object of type \arg Ty.
1416///
1417/// This safely handles the case when the src type is smaller than the
1418/// destination type; in this situation the values of bits which not
1419/// present in the src are undefined.
1420static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1421 const llvm::Type *Ty,
1422 CodeGenFunction &CGF) {
1423 const llvm::Type *SrcTy =
1424 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1425 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1426 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1427
Daniel Dunbar77071992009-02-03 05:59:18 +00001428 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001429 if (SrcSize == DstSize) {
1430 llvm::Value *Casted =
1431 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001432 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1433 // FIXME: Use better alignment / avoid requiring aligned load.
1434 Load->setAlignment(1);
1435 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001436 } else {
1437 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1438
1439 // Otherwise do coercion through memory. This is stupid, but
1440 // simple.
1441 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1442 llvm::Value *Casted =
1443 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001444 llvm::StoreInst *Store =
1445 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1446 // FIXME: Use better alignment / avoid requiring aligned store.
1447 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001448 return CGF.Builder.CreateLoad(Tmp);
1449 }
1450}
1451
1452/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1453/// where the source and destination may have different types.
1454///
1455/// This safely handles the case when the src type is larger than the
1456/// destination type; the upper bits of the src will be lost.
1457static void CreateCoercedStore(llvm::Value *Src,
1458 llvm::Value *DstPtr,
1459 CodeGenFunction &CGF) {
1460 const llvm::Type *SrcTy = Src->getType();
1461 const llvm::Type *DstTy =
1462 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1463
1464 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1465 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1466
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001467 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001468 if (SrcSize == DstSize) {
1469 llvm::Value *Casted =
1470 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001471 // FIXME: Use better alignment / avoid requiring aligned store.
1472 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001473 } else {
1474 assert(SrcSize > DstSize && "Coercion is missing bits!");
1475
1476 // Otherwise do coercion through memory. This is stupid, but
1477 // simple.
1478 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1479 CGF.Builder.CreateStore(Src, Tmp);
1480 llvm::Value *Casted =
1481 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001482 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1483 // FIXME: Use better alignment / avoid requiring aligned load.
1484 Load->setAlignment(1);
1485 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001486 }
1487}
1488
Daniel Dunbar04d35782008-09-17 00:51:38 +00001489/***/
1490
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001491bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001492 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001493}
1494
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001495const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001496CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001497 std::vector<const llvm::Type*> ArgTys;
1498
1499 const llvm::Type *ResultType = 0;
1500
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001501 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001502 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001503 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001504 case ABIArgInfo::Expand:
1505 assert(0 && "Invalid ABI kind for return argument");
1506
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001507 case ABIArgInfo::Direct:
1508 ResultType = ConvertType(RetTy);
1509 break;
1510
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001511 case ABIArgInfo::Indirect: {
1512 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001513 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001514 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001515 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1516 break;
1517 }
1518
Daniel Dunbar1358b202009-01-26 21:26:08 +00001519 case ABIArgInfo::Ignore:
1520 ResultType = llvm::Type::VoidTy;
1521 break;
1522
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001523 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001524 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001525 break;
1526 }
1527
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001528 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1529 ie = FI.arg_end(); it != ie; ++it) {
1530 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001531
1532 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001533 case ABIArgInfo::Ignore:
1534 break;
1535
Daniel Dunbar04d35782008-09-17 00:51:38 +00001536 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001537 ArgTys.push_back(AI.getCoerceToType());
1538 break;
1539
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001540 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001541 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001542 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1543 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001544 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001545 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001546
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001547 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001548 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001549 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001550
1551 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001552 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001553 break;
1554 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001555 }
1556
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001557 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001558}
1559
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001560void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001561 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001562 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001563 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001564 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001565
1566 if (TargetDecl) {
1567 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001568 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001569 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001570 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001571 if (TargetDecl->getAttr<PureAttr>())
1572 FuncAttrs |= llvm::Attribute::ReadOnly;
1573 if (TargetDecl->getAttr<ConstAttr>())
1574 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001575 }
1576
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001577 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001578 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001579 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001580 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001581 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001582 if (RetTy->isPromotableIntegerType()) {
1583 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001584 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001585 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001586 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001587 }
1588 }
1589 break;
1590
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001591 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001592 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001593 llvm::Attribute::StructRet |
1594 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001595 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001596 // sret disables readnone and readonly
1597 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1598 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001599 break;
1600
Daniel Dunbar1358b202009-01-26 21:26:08 +00001601 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001602 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001603 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001604
Daniel Dunbar22e30052008-09-11 01:48:57 +00001605 case ABIArgInfo::Expand:
1606 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001607 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001608
Devang Patel2bb6eb82008-09-26 22:53:57 +00001609 if (RetAttrs)
1610 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001611 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1612 ie = FI.arg_end(); it != ie; ++it) {
1613 QualType ParamType = it->type;
1614 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001615 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001616
1617 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001618 case ABIArgInfo::Coerce:
1619 break;
1620
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001621 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001622 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarb3f651a2009-02-05 01:31:19 +00001623 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001624 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001625 // byval disables readnone and readonly.
1626 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1627 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001628 break;
1629
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001630 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001631 if (ParamType->isPromotableIntegerType()) {
1632 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001633 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001634 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001635 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001636 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001637 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001638 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001639
Daniel Dunbar1358b202009-01-26 21:26:08 +00001640 case ABIArgInfo::Ignore:
1641 // Skip increment, no matching LLVM parameter.
1642 continue;
1643
Daniel Dunbar04d35782008-09-17 00:51:38 +00001644 case ABIArgInfo::Expand: {
1645 std::vector<const llvm::Type*> Tys;
1646 // FIXME: This is rather inefficient. Do we ever actually need
1647 // to do anything here? The result should be just reconstructed
1648 // on the other side, so extension should be a non-issue.
1649 getTypes().GetExpandedTypes(ParamType, Tys);
1650 Index += Tys.size();
1651 continue;
1652 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001653 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001654
Devang Patela85a9ef2008-09-25 21:02:23 +00001655 if (Attributes)
1656 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001657 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001658 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001659 if (FuncAttrs)
1660 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001661}
1662
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001663void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1664 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001665 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001666 // FIXME: We no longer need the types from FunctionArgList; lift up
1667 // and simplify.
1668
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001669 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1670 llvm::Function::arg_iterator AI = Fn->arg_begin();
1671
1672 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001673 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001674 AI->setName("agg.result");
1675 ++AI;
1676 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001677
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001678 assert(FI.arg_size() == Args.size() &&
1679 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001680 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001681 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001682 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001683 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001684 QualType Ty = info_it->type;
1685 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001686
1687 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001688 case ABIArgInfo::Indirect: {
1689 llvm::Value* V = AI;
1690 if (hasAggregateLLVMType(Ty)) {
1691 // Do nothing, aggregates and complex variables are accessed by
1692 // reference.
1693 } else {
1694 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001695 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001696 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1697 // This must be a promotion, for something like
1698 // "void a(x) short x; {..."
1699 V = EmitScalarConversion(V, Ty, Arg->getType());
1700 }
1701 }
1702 EmitParmDecl(*Arg, V);
1703 break;
1704 }
1705
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001706 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001707 assert(AI != Fn->arg_end() && "Argument mismatch!");
1708 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001709 if (hasAggregateLLVMType(Ty)) {
1710 // Create a temporary alloca to hold the argument; the rest of
1711 // codegen expects to access aggregates & complex values by
1712 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001713 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001714 Builder.CreateStore(AI, V);
1715 } else {
1716 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1717 // This must be a promotion, for something like
1718 // "void a(x) short x; {..."
1719 V = EmitScalarConversion(V, Ty, Arg->getType());
1720 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001721 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001722 EmitParmDecl(*Arg, V);
1723 break;
1724 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001725
1726 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001727 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001728 // we need to create a temporary and reconstruct it from the
1729 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001730 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001731 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001732 (Name + ".addr").c_str());
1733 // FIXME: What are the right qualifiers here?
1734 llvm::Function::arg_iterator End =
1735 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1736 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001737
Daniel Dunbar04d35782008-09-17 00:51:38 +00001738 // Name the arguments used in expansion and increment AI.
1739 unsigned Index = 0;
1740 for (; AI != End; ++AI, ++Index)
1741 AI->setName(Name + "." + llvm::utostr(Index));
1742 continue;
1743 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001744
1745 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001746 // Initialize the local variable appropriately.
1747 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001748 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001749 } else {
1750 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1751 }
1752
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001753 // Skip increment, no matching LLVM parameter.
1754 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001755
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001756 case ABIArgInfo::Coerce: {
1757 assert(AI != Fn->arg_end() && "Argument mismatch!");
1758 // FIXME: This is very wasteful; EmitParmDecl is just going to
1759 // drop the result in a new alloca anyway, so we could just
1760 // store into that directly if we broke the abstraction down
1761 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001762 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001763 CreateCoercedStore(AI, V, *this);
1764 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001765 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001766 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001767 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1768 // This must be a promotion, for something like
1769 // "void a(x) short x; {..."
1770 V = EmitScalarConversion(V, Ty, Arg->getType());
1771 }
1772 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001773 EmitParmDecl(*Arg, V);
1774 break;
1775 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001776 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001777
1778 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001779 }
1780 assert(AI == Fn->arg_end() && "Argument mismatch!");
1781}
1782
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001783void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001784 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001785 llvm::Value *RV = 0;
1786
1787 // Functions with no result always return void.
1788 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001789 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001790 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001791
1792 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001793 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001794 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001795 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1796 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1797 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1798 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1799 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001800 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1801 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001802 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001803 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001804
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001805 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001806 // The internal return value temp always will have
1807 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001808 RV = Builder.CreateLoad(ReturnValue);
1809 break;
1810
Daniel Dunbar1358b202009-01-26 21:26:08 +00001811 case ABIArgInfo::Ignore:
1812 break;
1813
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001814 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001815 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001816 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001817
Daniel Dunbar22e30052008-09-11 01:48:57 +00001818 case ABIArgInfo::Expand:
1819 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001820 }
1821 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001822
1823 if (RV) {
1824 Builder.CreateRet(RV);
1825 } else {
1826 Builder.CreateRetVoid();
1827 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001828}
1829
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001830RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1831 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001832 const CallArgList &CallArgs,
1833 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001834 // FIXME: We no longer need the types from CallArgs; lift up and
1835 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001836 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001837
1838 // Handle struct-return functions by passing a pointer to the
1839 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001840 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001841 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001842 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001843 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001844 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001845 }
1846
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001847 assert(CallInfo.arg_size() == CallArgs.size() &&
1848 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001849 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001850 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001851 I != E; ++I, ++info_it) {
1852 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001853 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001854
1855 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001856 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001857 if (RV.isScalar() || RV.isComplex()) {
1858 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001859 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001860 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001861 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001862 else
1863 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1864 } else {
1865 Args.push_back(RV.getAggregateAddr());
1866 }
1867 break;
1868
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001869 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001870 if (RV.isScalar()) {
1871 Args.push_back(RV.getScalarVal());
1872 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001873 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1874 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1875 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1876 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00001877 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001878 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001879 }
1880 break;
1881
Daniel Dunbar1358b202009-01-26 21:26:08 +00001882 case ABIArgInfo::Ignore:
1883 break;
1884
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001885 case ABIArgInfo::Coerce: {
1886 // FIXME: Avoid the conversion through memory if possible.
1887 llvm::Value *SrcPtr;
1888 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001889 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001890 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001891 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001892 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001893 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1894 } else
1895 SrcPtr = RV.getAggregateAddr();
1896 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1897 *this));
1898 break;
1899 }
1900
Daniel Dunbar04d35782008-09-17 00:51:38 +00001901 case ABIArgInfo::Expand:
1902 ExpandTypeToArgs(I->second, RV, Args);
1903 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001904 }
1905 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001906
Daniel Dunbar0a067402009-02-23 17:26:39 +00001907 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00001908 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001909 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00001910 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1911 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001912
Daniel Dunbar90e43452009-03-02 04:32:35 +00001913 llvm::CallSite CS;
1914 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
1915 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00001916 } else {
1917 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00001918 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1919 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00001920 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00001921 }
1922
Daniel Dunbar90e43452009-03-02 04:32:35 +00001923 CS.setAttributes(Attrs);
1924 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1925 CS.setCallingConv(F->getCallingConv());
1926
1927 // If the call doesn't return, finish the basic block and clear the
1928 // insertion point; this allows the rest of IRgen to discard
1929 // unreachable code.
1930 if (CS.doesNotReturn()) {
1931 Builder.CreateUnreachable();
1932 Builder.ClearInsertionPoint();
1933
1934 // FIXME: For now, emit a dummy basic block because expr
1935 // emitters in generally are not ready to handle emitting
1936 // expressions at unreachable points.
1937 EnsureInsertPoint();
1938
1939 // Return a reasonable RValue.
1940 return GetUndefRValue(RetTy);
1941 }
1942
1943 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +00001944 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001945 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001946
1947 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001948 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001949 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001950 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +00001951 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001952 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +00001953 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001954
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001955 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001956 if (RetTy->isAnyComplexType()) {
1957 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1958 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1959 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +00001960 }
1961 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001962 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00001963 Builder.CreateStore(CI, V);
1964 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00001965 }
1966 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001967
Daniel Dunbar1358b202009-01-26 21:26:08 +00001968 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00001969 // If we are ignoring an argument that had a result, make sure to
1970 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00001971 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001972
Daniel Dunbar73d66602008-09-10 07:04:09 +00001973 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001974 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001975 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001976 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001977 if (RetTy->isAnyComplexType())
1978 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +00001979 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001980 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00001981 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001982 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001983
Daniel Dunbar22e30052008-09-11 01:48:57 +00001984 case ABIArgInfo::Expand:
1985 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001986 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001987
1988 assert(0 && "Unhandled ABIArgInfo::Kind");
1989 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001990}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001991
1992/* VarArg handling */
1993
1994llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1995 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1996}