blob: 708c7171daf1d502b4af32fdaeca981d71739e79 [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 Dunbare09a9692009-01-24 08:32:22 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbar3cfcec72009-02-12 09:04:14 +000026#include "llvm/Support/MathExtras.h"
Daniel Dunbar9f4874e2009-02-04 23:24:38 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000029
30#include "ABIInfo.h"
31
Daniel Dunbara8f02052008-09-08 21:33:45 +000032using namespace clang;
33using namespace CodeGen;
34
35/***/
36
Daniel Dunbara8f02052008-09-08 21:33:45 +000037// FIXME: Use iterator and sidestep silly type array creation.
38
Daniel Dunbar34bda882009-02-02 23:23:47 +000039const
40CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
41 return getFunctionInfo(FTNP->getResultType(),
42 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000043}
44
Daniel Dunbar34bda882009-02-02 23:23:47 +000045const
46CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
47 llvm::SmallVector<QualType, 16> ArgTys;
48 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000049 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000050 ArgTys.push_back(FTP->getArgType(i));
51 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000052}
53
Daniel Dunbar34bda882009-02-02 23:23:47 +000054const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000055 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar34bda882009-02-02 23:23:47 +000056 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
57 return getFunctionInfo(FTP);
58 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000059}
60
Daniel Dunbar34bda882009-02-02 23:23:47 +000061const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
62 llvm::SmallVector<QualType, 16> ArgTys;
63 ArgTys.push_back(MD->getSelfDecl()->getType());
64 ArgTys.push_back(Context.getObjCSelType());
65 // FIXME: Kill copy?
Daniel Dunbara8f02052008-09-08 21:33:45 +000066 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
67 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000068 ArgTys.push_back((*i)->getType());
69 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000070}
71
Daniel Dunbar34bda882009-02-02 23:23:47 +000072const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
73 const CallArgList &Args) {
74 // FIXME: Kill copy.
75 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000076 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
77 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000078 ArgTys.push_back(i->second);
79 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000080}
81
Daniel Dunbar34bda882009-02-02 23:23:47 +000082const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
83 const FunctionArgList &Args) {
84 // FIXME: Kill copy.
85 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +000086 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
87 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000088 ArgTys.push_back(i->second);
89 return getFunctionInfo(ResTy, ArgTys);
90}
91
92const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
93 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +000094 // Lookup or create unique function info.
95 llvm::FoldingSetNodeID ID;
96 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
97
98 void *InsertPos = 0;
99 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
100 if (FI)
101 return *FI;
102
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000103 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000104 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000105 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000106
107 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000108 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000109
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000110 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000111}
112
113/***/
114
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000115ABIInfo::~ABIInfo() {}
116
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000117void ABIArgInfo::dump() const {
118 fprintf(stderr, "(ABIArgInfo Kind=");
119 switch (TheKind) {
120 case Direct:
121 fprintf(stderr, "Direct");
122 break;
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000123 case Ignore:
124 fprintf(stderr, "Ignore");
125 break;
126 case Coerce:
127 fprintf(stderr, "Coerce Type=");
128 getCoerceToType()->print(llvm::errs());
129 // FIXME: This is ridiculous.
130 llvm::errs().flush();
131 break;
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000132 case Indirect:
133 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000134 break;
135 case Expand:
136 fprintf(stderr, "Expand");
137 break;
138 }
139 fprintf(stderr, ")\n");
140}
141
142/***/
143
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000144/// isEmptyStruct - Return true iff a structure has no non-empty
145/// members. Note that a structure with a flexible array member is not
146/// considered empty.
147static bool isEmptyStruct(QualType T) {
148 const RecordType *RT = T->getAsStructureType();
149 if (!RT)
150 return 0;
151 const RecordDecl *RD = RT->getDecl();
152 if (RD->hasFlexibleArrayMember())
153 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000154 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000155 e = RD->field_end(); i != e; ++i) {
156 const FieldDecl *FD = *i;
157 if (!isEmptyStruct(FD->getType()))
158 return false;
159 }
160 return true;
161}
162
163/// isSingleElementStruct - Determine if a structure is a "single
164/// element struct", i.e. it has exactly one non-empty field or
165/// exactly one field which is itself a single element
166/// struct. Structures with flexible array members are never
167/// considered single element structs.
168///
169/// \return The field declaration for the single non-empty field, if
170/// it exists.
171static const FieldDecl *isSingleElementStruct(QualType T) {
172 const RecordType *RT = T->getAsStructureType();
173 if (!RT)
174 return 0;
175
176 const RecordDecl *RD = RT->getDecl();
177 if (RD->hasFlexibleArrayMember())
178 return 0;
179
180 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000181 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000182 e = RD->field_end(); i != e; ++i) {
183 const FieldDecl *FD = *i;
184 QualType FT = FD->getType();
185
186 if (isEmptyStruct(FT)) {
187 // Ignore
188 } else if (Found) {
189 return 0;
190 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
191 Found = FD;
192 } else {
193 Found = isSingleElementStruct(FT);
194 if (!Found)
195 return 0;
196 }
197 }
198
199 return Found;
200}
201
202static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
203 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
204 return false;
205
206 uint64_t Size = Context.getTypeSize(Ty);
207 return Size == 32 || Size == 64;
208}
209
210static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
211 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000212 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000213 e = RD->field_end(); i != e; ++i) {
214 const FieldDecl *FD = *i;
215
216 if (!is32Or64BitBasicType(FD->getType(), Context))
217 return false;
218
219 // If this is a bit-field we need to make sure it is still a
220 // 32-bit or 64-bit type.
221 if (Expr *BW = FD->getBitWidth()) {
222 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
223 if (Width <= 16)
224 return false;
225 }
226 }
227 return true;
228}
229
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000230namespace {
231/// DefaultABIInfo - The default implementation for ABI specific
232/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000233/// self-consistent and sensible LLVM IR generation, but does not
234/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000235class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000236 ABIArgInfo classifyReturnType(QualType RetTy,
237 ASTContext &Context) const;
238
239 ABIArgInfo classifyArgumentType(QualType RetTy,
240 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000241
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000242 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
243 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
244 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
245 it != ie; ++it)
246 it->info = classifyArgumentType(it->type, Context);
247 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000248
249 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
250 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000251};
252
253/// X86_32ABIInfo - The X86-32 ABI information.
254class X86_32ABIInfo : public ABIInfo {
255public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000256 ABIArgInfo classifyReturnType(QualType RetTy,
257 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000258
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000259 ABIArgInfo classifyArgumentType(QualType RetTy,
260 ASTContext &Context) const;
261
262 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
263 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
264 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
265 it != ie; ++it)
266 it->info = classifyArgumentType(it->type, Context);
267 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000268
269 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
270 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000271};
272}
273
274ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
275 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000276 if (RetTy->isVoidType()) {
277 return ABIArgInfo::getIgnore();
278 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000279 // Classify "single element" structs as their element type.
280 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
281 if (SeltFD) {
282 QualType SeltTy = SeltFD->getType()->getDesugaredType();
283 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
284 // FIXME: This is gross, it would be nice if we could just
285 // pass back SeltTy and have clients deal with it. Is it worth
286 // supporting coerce to both LLVM and clang Types?
287 if (BT->isIntegerType()) {
288 uint64_t Size = Context.getTypeSize(SeltTy);
289 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
290 } else if (BT->getKind() == BuiltinType::Float) {
291 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
292 } else if (BT->getKind() == BuiltinType::Double) {
293 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
294 }
295 } else if (SeltTy->isPointerType()) {
296 // FIXME: It would be really nice if this could come out as
297 // the proper pointer type.
298 llvm::Type *PtrTy =
299 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
300 return ABIArgInfo::getCoerce(PtrTy);
301 }
302 }
303
Daniel Dunbar73d66602008-09-10 07:04:09 +0000304 uint64_t Size = Context.getTypeSize(RetTy);
305 if (Size == 8) {
306 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
307 } else if (Size == 16) {
308 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
309 } else if (Size == 32) {
310 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
311 } else if (Size == 64) {
312 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
313 } else {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000314 return ABIArgInfo::getIndirect(0);
Daniel Dunbar73d66602008-09-10 07:04:09 +0000315 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000316 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000317 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000318 }
319}
320
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000321ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000322 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000323 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000324 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000325 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000326 if (const RecordType *RT = Ty->getAsStructureType())
327 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000328 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000329
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000330 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000331 uint64_t Size = Context.getTypeSize(Ty);
332 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000333 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000334
335 // Expand structs with size <= 128-bits which consist only of
336 // basic types (int, long long, float, double, xxx*). This is
337 // non-recursive and does not ignore empty fields.
338 if (const RecordType *RT = Ty->getAsStructureType()) {
339 if (Context.getTypeSize(Ty) <= 4*32 &&
340 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
341 return ABIArgInfo::getExpand();
342 }
343
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000344 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000345 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000346 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000347 }
348}
349
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000350llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
351 CodeGenFunction &CGF) const {
352 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
353 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
354
355 CGBuilderTy &Builder = CGF.Builder;
356 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
357 "ap");
358 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
359 llvm::Type *PTy =
360 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
361 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
362
363 uint64_t SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
364 const unsigned ArgumentSizeInBytes = 4;
365 if (SizeInBytes < ArgumentSizeInBytes)
366 SizeInBytes = ArgumentSizeInBytes;
367
368 llvm::Value *NextAddr =
369 Builder.CreateGEP(Addr,
370 llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes),
371 "ap.next");
372 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
373
374 return AddrTyped;
375}
376
Daniel Dunbare09a9692009-01-24 08:32:22 +0000377namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000378/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000379class X86_64ABIInfo : public ABIInfo {
380 enum Class {
381 Integer = 0,
382 SSE,
383 SSEUp,
384 X87,
385 X87Up,
386 ComplexX87,
387 NoClass,
388 Memory
389 };
390
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000391 /// merge - Implement the X86_64 ABI merging algorithm.
392 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000393 /// Merge an accumulating classification \arg Accum with a field
394 /// classification \arg Field.
395 ///
396 /// \param Accum - The accumulating classification. This should
397 /// always be either NoClass or the result of a previous merge
398 /// call. In addition, this should never be Memory (the caller
399 /// should just return Memory for the aggregate).
400 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000401
Daniel Dunbare09a9692009-01-24 08:32:22 +0000402 /// classify - Determine the x86_64 register classes in which the
403 /// given type T should be passed.
404 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000405 /// \param Lo - The classification for the parts of the type
406 /// residing in the low word of the containing object.
407 ///
408 /// \param Hi - The classification for the parts of the type
409 /// residing in the high word of the containing object.
410 ///
411 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000412 /// containing object. Some parameters are classified different
413 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000414 ///
415 /// If a word is unused its result will be NoClass; if a type should
416 /// be passed in Memory then at least the classification of \arg Lo
417 /// will be Memory.
418 ///
419 /// The \arg Lo class will be NoClass iff the argument is ignored.
420 ///
421 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
422 /// be NoClass.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000423 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000424 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000425
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000426 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
427 /// to coerce to, chose the best way to pass Ty in the same place
428 /// that \arg CoerceTo would be passed, but while keeping the
429 /// emitted code as simple as possible.
430 ///
431 /// FIXME: Note, this should be cleaned up to just take an
432 /// enumeration of all the ways we might want to pass things,
433 /// instead of constructing an LLVM type. This makes this code more
434 /// explicit, and it makes it clearer that we are also doing this
435 /// for correctness in the case of passing scalar types.
436 ABIArgInfo getCoerceResult(QualType Ty,
437 const llvm::Type *CoerceTo,
438 ASTContext &Context) const;
439
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000440 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000441 ASTContext &Context) const;
442
443 ABIArgInfo classifyArgumentType(QualType Ty,
444 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000445 unsigned &neededInt,
446 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000447
448public:
449 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000450
451 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
452 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000453};
454}
455
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000456X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
457 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000458 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
459 // classified recursively so that always two fields are
460 // considered. The resulting class is calculated according to
461 // the classes of the fields in the eightbyte:
462 //
463 // (a) If both classes are equal, this is the resulting class.
464 //
465 // (b) If one of the classes is NO_CLASS, the resulting class is
466 // the other class.
467 //
468 // (c) If one of the classes is MEMORY, the result is the MEMORY
469 // class.
470 //
471 // (d) If one of the classes is INTEGER, the result is the
472 // INTEGER.
473 //
474 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
475 // MEMORY is used as class.
476 //
477 // (f) Otherwise class SSE is used.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000478 assert((Accum == NoClass || Accum == Integer ||
479 Accum == SSE || Accum == SSEUp) &&
480 "Invalid accumulated classification during merge.");
481 if (Accum == Field || Field == NoClass)
482 return Accum;
483 else if (Field == Memory)
484 return Memory;
485 else if (Accum == NoClass)
486 return Field;
487 else if (Accum == Integer || Field == Integer)
488 return Integer;
489 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
490 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000491 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000492 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000493}
494
Daniel Dunbare09a9692009-01-24 08:32:22 +0000495void X86_64ABIInfo::classify(QualType Ty,
496 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000497 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000498 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000499 // FIXME: This code can be simplified by introducing a simple value
500 // class for Class pairs with appropriate constructor methods for
501 // the various situations.
502
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000503 Lo = Hi = NoClass;
504
505 Class &Current = OffsetBase < 64 ? Lo : Hi;
506 Current = Memory;
507
Daniel Dunbare09a9692009-01-24 08:32:22 +0000508 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
509 BuiltinType::Kind k = BT->getKind();
510
Daniel Dunbar1358b202009-01-26 21:26:08 +0000511 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000512 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000513 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000514 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000515 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000516 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000517 } else if (k == BuiltinType::LongDouble) {
518 Lo = X87;
519 Hi = X87Up;
520 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000521 // FIXME: _Decimal32 and _Decimal64 are SSE.
522 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000523 // FIXME: __int128 is (Integer, Integer).
524 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
525 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000526 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000527 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000528 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000529 if (Size == 64) {
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000530 // gcc passes <1 x double> in memory.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000531 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000532 return;
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000533
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000534 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000535
536 // If this type crosses an eightbyte boundary, it should be
537 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000538 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000539 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000540 } else if (Size == 128) {
541 Lo = SSE;
542 Hi = SSEUp;
543 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000544 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000545 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000546
Daniel Dunbare413f532009-01-30 18:40:10 +0000547 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000548 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000549 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000550 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000551 else if (Size <= 128)
552 Lo = Hi = Integer;
553 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000554 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000555 else if (ET == Context.DoubleTy)
556 Lo = Hi = SSE;
557 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000558 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000559
560 // If this complex type crosses an eightbyte boundary then it
561 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000562 uint64_t EB_Real = (OffsetBase) / 64;
563 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000564 if (Hi == NoClass && EB_Real != EB_Imag)
565 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000566 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
567 // Arrays are treated like structures.
568
569 uint64_t Size = Context.getTypeSize(Ty);
570
571 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
572 // than two eightbytes, ..., it has class MEMORY.
573 if (Size > 128)
574 return;
575
576 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
577 // fields, it has class MEMORY.
578 //
579 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000580 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000581 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000582
583 // Otherwise implement simplified merge. We could be smarter about
584 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000585 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000586 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
587 uint64_t ArraySize = AT->getSize().getZExtValue();
588 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
589 Class FieldLo, FieldHi;
590 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000591 Lo = merge(Lo, FieldLo);
592 Hi = merge(Hi, FieldHi);
593 if (Lo == Memory || Hi == Memory)
594 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000595 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000596
597 // Do post merger cleanup (see below). Only case we worry about is Memory.
598 if (Hi == Memory)
599 Lo = Memory;
600 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000601 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000602 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000603
604 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
605 // than two eightbytes, ..., it has class MEMORY.
606 if (Size > 128)
607 return;
608
609 const RecordDecl *RD = RT->getDecl();
610
611 // Assume variable sized types are passed in memory.
612 if (RD->hasFlexibleArrayMember())
613 return;
614
615 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
616
617 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000618 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000619 unsigned idx = 0;
620 for (RecordDecl::field_iterator i = RD->field_begin(),
621 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000622 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000623
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000624 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
625 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000626 if (Offset % Context.getTypeAlign(i->getType())) {
627 Lo = Memory;
628 return;
629 }
630
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000631 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000632 //
633 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
634 // exceeds a single eightbyte, each is classified
635 // separately. Each eightbyte gets initialized to class
636 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000637 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000638 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000639 Lo = merge(Lo, FieldLo);
640 Hi = merge(Hi, FieldHi);
641 if (Lo == Memory || Hi == Memory)
642 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000643 }
644
645 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
646 //
647 // (a) If one of the classes is MEMORY, the whole argument is
648 // passed in memory.
649 //
650 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
651
652 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000653 // the merge (just bail).
654 //
655 // The second condition occurs in the case of unions; for example
656 // union { _Complex double; unsigned; }.
657 if (Hi == Memory)
658 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000659 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000660 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000661 }
662}
663
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000664ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
665 const llvm::Type *CoerceTo,
666 ASTContext &Context) const {
667 if (CoerceTo == llvm::Type::Int64Ty) {
668 // Integer and pointer types will end up in a general purpose
669 // register.
670 if (Ty->isIntegerType() || Ty->isPointerType())
671 return ABIArgInfo::getDirect();
672 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000673 // FIXME: It would probably be better to make CGFunctionInfo only
674 // map using canonical types than to canonize here.
675 QualType CTy = Context.getCanonicalType(Ty);
676
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000677 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000678 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000679 return ABIArgInfo::getDirect();
680 }
681
682 return ABIArgInfo::getCoerce(CoerceTo);
683}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000684
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000685ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
686 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000687 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
688 // classification algorithm.
689 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000690 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000691
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000692 // Check some invariants.
693 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
694 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
695 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
696
Daniel Dunbare09a9692009-01-24 08:32:22 +0000697 const llvm::Type *ResType = 0;
698 switch (Lo) {
699 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000700 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000701
702 case SSEUp:
703 case X87Up:
704 assert(0 && "Invalid classification for lo word.");
705
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000706 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000707 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000708 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000709 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000710
711 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
712 // available register of the sequence %rax, %rdx is used.
713 case Integer:
714 ResType = llvm::Type::Int64Ty; break;
715
716 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
717 // available SSE register of the sequence %xmm0, %xmm1 is used.
718 case SSE:
719 ResType = llvm::Type::DoubleTy; break;
720
721 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
722 // returned on the X87 stack in %st0 as 80-bit x87 number.
723 case X87:
724 ResType = llvm::Type::X86_FP80Ty; break;
725
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000726 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
727 // part of the value is returned in %st0 and the imaginary part in
728 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000729 case ComplexX87:
730 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
731 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
732 break;
733 }
734
735 switch (Hi) {
736 // Memory was handled previously, and ComplexX87 and X87 should
737 // never occur as hi classes.
738 case Memory:
739 case X87:
740 case ComplexX87:
741 assert(0 && "Invalid classification for hi word.");
742
743 case NoClass: break;
744 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000745 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
746 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000747 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000748 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
749 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000750
751 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
752 // is passed in the upper half of the last used SSE register.
753 //
754 // SSEUP should always be preceeded by SSE, just widen.
755 case SSEUp:
756 assert(Lo == SSE && "Unexpected SSEUp classification.");
757 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
758 break;
759
760 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000761 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000762 //
763 // X87UP should always be preceeded by X87, so we don't need to do
764 // anything here.
765 case X87Up:
766 assert(Lo == X87 && "Unexpected X87Up classification.");
767 break;
768 }
769
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000770 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000771}
772
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000773ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000774 unsigned &neededInt,
775 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000776 X86_64ABIInfo::Class Lo, Hi;
777 classify(Ty, Context, 0, Lo, Hi);
778
779 // Check some invariants.
780 // FIXME: Enforce these by construction.
781 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
782 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
783 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
784
Daniel Dunbare978cb92009-02-10 17:06:09 +0000785 neededInt = 0;
786 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000787 const llvm::Type *ResType = 0;
788 switch (Lo) {
789 case NoClass:
790 return ABIArgInfo::getIgnore();
791
792 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
793 // on the stack.
794 case Memory:
795
796 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
797 // COMPLEX_X87, it is passed in memory.
798 case X87:
799 case ComplexX87:
800 // Choose appropriate in memory type.
801 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000802 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000803 else
804 return ABIArgInfo::getDirect();
805
806 case SSEUp:
807 case X87Up:
808 assert(0 && "Invalid classification for lo word.");
809
810 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
811 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
812 // and %r9 is used.
813 case Integer:
814 ++neededInt;
815 ResType = llvm::Type::Int64Ty;
816 break;
817
818 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
819 // available SSE register is used, the registers are taken in the
820 // order from %xmm0 to %xmm7.
821 case SSE:
822 ++neededSSE;
823 ResType = llvm::Type::DoubleTy;
824 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000825 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000826
827 switch (Hi) {
828 // Memory was handled previously, ComplexX87 and X87 should
829 // never occur as hi classes, and X87Up must be preceed by X87,
830 // which is passed in memory.
831 case Memory:
832 case X87:
833 case X87Up:
834 case ComplexX87:
835 assert(0 && "Invalid classification for hi word.");
836
837 case NoClass: break;
838 case Integer:
839 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
840 ++neededInt;
841 break;
842 case SSE:
843 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
844 ++neededSSE;
845 break;
846
847 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
848 // eightbyte is passed in the upper half of the last used SSE
849 // register.
850 case SSEUp:
851 assert(Lo == SSE && "Unexpected SSEUp classification.");
852 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
853 break;
854 }
855
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000856 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000857}
858
859void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
860 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
861
862 // Keep track of the number of assigned registers.
863 unsigned freeIntRegs = 6, freeSSERegs = 8;
864
865 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
866 // get assigned (in left-to-right order) for passing as follows...
867 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +0000868 it != ie; ++it) {
869 unsigned neededInt, neededSSE;
870 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
871
872 // AMD64-ABI 3.2.3p3: If there are no registers available for any
873 // eightbyte of an argument, the whole argument is passed on the
874 // stack. If registers have already been assigned for some
875 // eightbytes of such an argument, the assignments get reverted.
876 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
877 freeIntRegs -= neededInt;
878 freeSSERegs -= neededSSE;
879 } else {
880 // Choose appropriate in memory type.
881 if (CodeGenFunction::hasAggregateLLVMType(it->type))
882 it->info = ABIArgInfo::getIndirect(0);
883 else
884 it->info = ABIArgInfo::getDirect();
885 }
886 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000887}
888
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000889static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
890 QualType Ty,
891 CodeGenFunction &CGF) {
892 llvm::Value *overflow_arg_area_p =
893 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
894 llvm::Value *overflow_arg_area =
895 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
896
897 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
898 // byte boundary if alignment needed by type exceeds 8 byte boundary.
899 uint64_t Align = llvm::NextPowerOf2(CGF.getContext().getTypeAlign(Ty) / 8);
900 if (Align > 8) {
901 // Note align to type alignment instead of assuming it must be 16.
902
903 // FIXME: Implement alignment in x86_64 va_arg.
904 }
905
906 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
907 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
908 llvm::Value *Res =
909 CGF.Builder.CreateBitCast(overflow_arg_area,
910 llvm::PointerType::getUnqual(LTy));
911
912 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
913 // l->overflow_arg_area + sizeof(type).
914 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
915 // an 8 byte boundary.
916
917 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
918 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
919 (SizeInBytes + 7) & ~7);
920 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
921 "overflow_arg_area.next");
922 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
923
924 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
925 return Res;
926}
927
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000928llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
929 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000930 // Assume that va_list type is correct; should be pointer to LLVM type:
931 // struct {
932 // i32 gp_offset;
933 // i32 fp_offset;
934 // i8* overflow_arg_area;
935 // i8* reg_save_area;
936 // };
937 unsigned neededInt, neededSSE;
938 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
939 neededInt, neededSSE);
940
941 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
942 // in the registers. If not go to step 7.
943 if (!neededInt && !neededSSE)
944 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
945
946 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
947 // general purpose registers needed to pass type and num_fp to hold
948 // the number of floating point registers needed.
949
950 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
951 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
952 // l->fp_offset > 304 - num_fp * 16 go to step 7.
953 //
954 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
955 // register save space).
956
957 llvm::Value *InRegs = 0;
958 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
959 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
960 if (neededInt) {
961 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
962 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
963 InRegs =
964 CGF.Builder.CreateICmpULE(gp_offset,
965 llvm::ConstantInt::get(llvm::Type::Int32Ty,
966 48 - neededInt * 8),
967 "fits_in_gp");
968 }
969
970 if (neededSSE) {
971 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
972 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
973 llvm::Value *FitsInFP =
974 CGF.Builder.CreateICmpULE(fp_offset,
975 llvm::ConstantInt::get(llvm::Type::Int32Ty,
976 176 - neededSSE * 8),
977 "fits_in_fp");
978 InRegs = InRegs ? CGF.Builder.CreateOr(InRegs, FitsInFP) : FitsInFP;
979 }
980
981 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
982 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
983 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
984 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
985
986 // Emit code to load the value if it was passed in registers.
987
988 CGF.EmitBlock(InRegBlock);
989
990 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
991 // an offset of l->gp_offset and/or l->fp_offset. This may require
992 // copying to a temporary location in case the parameter is passed
993 // in different register classes or requires an alignment greater
994 // than 8 for general purpose registers and 16 for XMM registers.
995 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
996 llvm::Value *RegAddr =
997 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
998 "reg_save_area");
999 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001000 // FIXME: Cleanup.
1001 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1002 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1003 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1004 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1005 const llvm::Type *TyLo = ST->getElementType(0);
1006 const llvm::Type *TyHi = ST->getElementType(1);
1007 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1008 "Unexpected ABI info for mixed regs");
1009 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1010 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1011 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1012 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1013 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1014 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1015 llvm::Value *V =
1016 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1017 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1018 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1019 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1020
1021 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001022 } else if (neededInt) {
1023 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1024 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1025 llvm::PointerType::getUnqual(LTy));
1026 } else {
1027 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1028 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1029 llvm::PointerType::getUnqual(LTy));
1030 }
1031
1032 // AMD64-ABI 3.5.7p5: Step 5. Set:
1033 // l->gp_offset = l->gp_offset + num_gp * 8
1034 // l->fp_offset = l->fp_offset + num_fp * 16.
1035 if (neededInt) {
1036 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1037 neededInt * 8);
1038 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1039 gp_offset_p);
1040 }
1041 if (neededSSE) {
1042 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1043 neededSSE * 16);
1044 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1045 fp_offset_p);
1046 }
1047 CGF.EmitBranch(ContBlock);
1048
1049 // Emit code to load the value if it was passed in memory.
1050
1051 CGF.EmitBlock(InMemBlock);
1052 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1053
1054 // Return the appropriate result.
1055
1056 CGF.EmitBlock(ContBlock);
1057 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1058 "vaarg.addr");
1059 ResAddr->reserveOperandSpace(2);
1060 ResAddr->addIncoming(RegAddr, InRegBlock);
1061 ResAddr->addIncoming(MemAddr, InMemBlock);
1062
1063 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001064}
1065
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001066ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001067 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001068 if (RetTy->isVoidType()) {
1069 return ABIArgInfo::getIgnore();
1070 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001071 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001072 } else {
1073 return ABIArgInfo::getDirect();
1074 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001075}
1076
1077ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001078 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001079 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001080 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001081 } else {
1082 return ABIArgInfo::getDirect();
1083 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001084}
1085
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001086llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1087 CodeGenFunction &CGF) const {
1088 return 0;
1089}
1090
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001091const ABIInfo &CodeGenTypes::getABIInfo() const {
1092 if (TheABIInfo)
1093 return *TheABIInfo;
1094
1095 // For now we just cache this in the CodeGenTypes and don't bother
1096 // to free it.
1097 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1098 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001099 switch (getContext().Target.getPointerWidth(0)) {
1100 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001101 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001102 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001103 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001104 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001105 }
1106
1107 return *(TheABIInfo = new DefaultABIInfo);
1108}
1109
Daniel Dunbare126ab12008-09-10 02:41:04 +00001110/***/
1111
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001112CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1113 const llvm::SmallVector<QualType, 16> &ArgTys) {
1114 NumArgs = ArgTys.size();
1115 Args = new ArgInfo[1 + NumArgs];
1116 Args[0].type = ResTy;
1117 for (unsigned i = 0; i < NumArgs; ++i)
1118 Args[1 + i].type = ArgTys[i];
1119}
1120
1121/***/
1122
Daniel Dunbar04d35782008-09-17 00:51:38 +00001123void CodeGenTypes::GetExpandedTypes(QualType Ty,
1124 std::vector<const llvm::Type*> &ArgTys) {
1125 const RecordType *RT = Ty->getAsStructureType();
1126 assert(RT && "Can only expand structure types.");
1127 const RecordDecl *RD = RT->getDecl();
1128 assert(!RD->hasFlexibleArrayMember() &&
1129 "Cannot expand structure with flexible array.");
1130
Douglas Gregor5d764842009-01-09 17:18:27 +00001131 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001132 e = RD->field_end(); i != e; ++i) {
1133 const FieldDecl *FD = *i;
1134 assert(!FD->isBitField() &&
1135 "Cannot expand structure with bit-field members.");
1136
1137 QualType FT = FD->getType();
1138 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1139 GetExpandedTypes(FT, ArgTys);
1140 } else {
1141 ArgTys.push_back(ConvertType(FT));
1142 }
1143 }
1144}
1145
1146llvm::Function::arg_iterator
1147CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1148 llvm::Function::arg_iterator AI) {
1149 const RecordType *RT = Ty->getAsStructureType();
1150 assert(RT && "Can only expand structure types.");
1151
1152 RecordDecl *RD = RT->getDecl();
1153 assert(LV.isSimple() &&
1154 "Unexpected non-simple lvalue during struct expansion.");
1155 llvm::Value *Addr = LV.getAddress();
1156 for (RecordDecl::field_iterator i = RD->field_begin(),
1157 e = RD->field_end(); i != e; ++i) {
1158 FieldDecl *FD = *i;
1159 QualType FT = FD->getType();
1160
1161 // FIXME: What are the right qualifiers here?
1162 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1163 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1164 AI = ExpandTypeFromArgs(FT, LV, AI);
1165 } else {
1166 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1167 ++AI;
1168 }
1169 }
1170
1171 return AI;
1172}
1173
1174void
1175CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1176 llvm::SmallVector<llvm::Value*, 16> &Args) {
1177 const RecordType *RT = Ty->getAsStructureType();
1178 assert(RT && "Can only expand structure types.");
1179
1180 RecordDecl *RD = RT->getDecl();
1181 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1182 llvm::Value *Addr = RV.getAggregateAddr();
1183 for (RecordDecl::field_iterator i = RD->field_begin(),
1184 e = RD->field_end(); i != e; ++i) {
1185 FieldDecl *FD = *i;
1186 QualType FT = FD->getType();
1187
1188 // FIXME: What are the right qualifiers here?
1189 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1190 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1191 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1192 } else {
1193 RValue RV = EmitLoadOfLValue(LV, FT);
1194 assert(RV.isScalar() &&
1195 "Unexpected non-scalar rvalue during struct expansion.");
1196 Args.push_back(RV.getScalarVal());
1197 }
1198 }
1199}
1200
Daniel Dunbar84379912009-02-02 19:06:38 +00001201/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1202/// a pointer to an object of type \arg Ty.
1203///
1204/// This safely handles the case when the src type is smaller than the
1205/// destination type; in this situation the values of bits which not
1206/// present in the src are undefined.
1207static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1208 const llvm::Type *Ty,
1209 CodeGenFunction &CGF) {
1210 const llvm::Type *SrcTy =
1211 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1212 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1213 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1214
Daniel Dunbar77071992009-02-03 05:59:18 +00001215 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001216 if (SrcSize == DstSize) {
1217 llvm::Value *Casted =
1218 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001219 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1220 // FIXME: Use better alignment / avoid requiring aligned load.
1221 Load->setAlignment(1);
1222 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001223 } else {
1224 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1225
1226 // Otherwise do coercion through memory. This is stupid, but
1227 // simple.
1228 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1229 llvm::Value *Casted =
1230 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001231 llvm::StoreInst *Store =
1232 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1233 // FIXME: Use better alignment / avoid requiring aligned store.
1234 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001235 return CGF.Builder.CreateLoad(Tmp);
1236 }
1237}
1238
1239/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1240/// where the source and destination may have different types.
1241///
1242/// This safely handles the case when the src type is larger than the
1243/// destination type; the upper bits of the src will be lost.
1244static void CreateCoercedStore(llvm::Value *Src,
1245 llvm::Value *DstPtr,
1246 CodeGenFunction &CGF) {
1247 const llvm::Type *SrcTy = Src->getType();
1248 const llvm::Type *DstTy =
1249 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1250
1251 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1252 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1253
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001254 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001255 if (SrcSize == DstSize) {
1256 llvm::Value *Casted =
1257 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001258 // FIXME: Use better alignment / avoid requiring aligned store.
1259 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001260 } else {
1261 assert(SrcSize > DstSize && "Coercion is missing bits!");
1262
1263 // Otherwise do coercion through memory. This is stupid, but
1264 // simple.
1265 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1266 CGF.Builder.CreateStore(Src, Tmp);
1267 llvm::Value *Casted =
1268 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001269 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1270 // FIXME: Use better alignment / avoid requiring aligned load.
1271 Load->setAlignment(1);
1272 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001273 }
1274}
1275
Daniel Dunbar04d35782008-09-17 00:51:38 +00001276/***/
1277
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001278bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001279 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001280}
1281
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001282const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001283CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001284 std::vector<const llvm::Type*> ArgTys;
1285
1286 const llvm::Type *ResultType = 0;
1287
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001288 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001289 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001290 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001291 case ABIArgInfo::Expand:
1292 assert(0 && "Invalid ABI kind for return argument");
1293
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001294 case ABIArgInfo::Direct:
1295 ResultType = ConvertType(RetTy);
1296 break;
1297
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001298 case ABIArgInfo::Indirect: {
1299 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001300 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001301 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001302 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1303 break;
1304 }
1305
Daniel Dunbar1358b202009-01-26 21:26:08 +00001306 case ABIArgInfo::Ignore:
1307 ResultType = llvm::Type::VoidTy;
1308 break;
1309
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001310 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001311 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001312 break;
1313 }
1314
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001315 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1316 ie = FI.arg_end(); it != ie; ++it) {
1317 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001318
1319 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001320 case ABIArgInfo::Ignore:
1321 break;
1322
Daniel Dunbar04d35782008-09-17 00:51:38 +00001323 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001324 ArgTys.push_back(AI.getCoerceToType());
1325 break;
1326
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001327 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001328 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001329 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1330 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001331 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001332 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001333
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001334 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001335 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001336 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001337
1338 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001339 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001340 break;
1341 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001342 }
1343
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001344 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001345}
1346
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001347void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001348 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001349 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001350 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001351 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001352
1353 if (TargetDecl) {
1354 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001355 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001356 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001357 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001358 if (TargetDecl->getAttr<PureAttr>())
1359 FuncAttrs |= llvm::Attribute::ReadOnly;
1360 if (TargetDecl->getAttr<ConstAttr>())
1361 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001362 }
1363
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001364 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001365 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001366 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001367 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001368 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001369 if (RetTy->isPromotableIntegerType()) {
1370 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001371 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001372 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001373 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001374 }
1375 }
1376 break;
1377
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001378 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001379 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001380 llvm::Attribute::StructRet |
1381 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001382 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001383 break;
1384
Daniel Dunbar1358b202009-01-26 21:26:08 +00001385 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001386 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001387 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001388
Daniel Dunbar22e30052008-09-11 01:48:57 +00001389 case ABIArgInfo::Expand:
1390 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001391 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001392
Devang Patel2bb6eb82008-09-26 22:53:57 +00001393 if (RetAttrs)
1394 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001395 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1396 ie = FI.arg_end(); it != ie; ++it) {
1397 QualType ParamType = it->type;
1398 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001399 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001400
1401 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001402 case ABIArgInfo::Coerce:
1403 break;
1404
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001405 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001406 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarb3f651a2009-02-05 01:31:19 +00001407 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001408 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001409 break;
1410
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001411 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001412 if (ParamType->isPromotableIntegerType()) {
1413 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001414 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001415 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001416 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001417 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001418 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001419 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001420
Daniel Dunbar1358b202009-01-26 21:26:08 +00001421 case ABIArgInfo::Ignore:
1422 // Skip increment, no matching LLVM parameter.
1423 continue;
1424
Daniel Dunbar04d35782008-09-17 00:51:38 +00001425 case ABIArgInfo::Expand: {
1426 std::vector<const llvm::Type*> Tys;
1427 // FIXME: This is rather inefficient. Do we ever actually need
1428 // to do anything here? The result should be just reconstructed
1429 // on the other side, so extension should be a non-issue.
1430 getTypes().GetExpandedTypes(ParamType, Tys);
1431 Index += Tys.size();
1432 continue;
1433 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001434 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001435
Devang Patela85a9ef2008-09-25 21:02:23 +00001436 if (Attributes)
1437 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001438 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001439 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001440 if (FuncAttrs)
1441 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1442
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001443}
1444
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001445void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1446 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001447 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001448 // FIXME: We no longer need the types from FunctionArgList; lift up
1449 // and simplify.
1450
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001451 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1452 llvm::Function::arg_iterator AI = Fn->arg_begin();
1453
1454 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001455 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001456 AI->setName("agg.result");
1457 ++AI;
1458 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001459
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001460 assert(FI.arg_size() == Args.size() &&
1461 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001462 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001463 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001464 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001465 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001466 QualType Ty = info_it->type;
1467 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001468
1469 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001470 case ABIArgInfo::Indirect: {
1471 llvm::Value* V = AI;
1472 if (hasAggregateLLVMType(Ty)) {
1473 // Do nothing, aggregates and complex variables are accessed by
1474 // reference.
1475 } else {
1476 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001477 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001478 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1479 // This must be a promotion, for something like
1480 // "void a(x) short x; {..."
1481 V = EmitScalarConversion(V, Ty, Arg->getType());
1482 }
1483 }
1484 EmitParmDecl(*Arg, V);
1485 break;
1486 }
1487
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001488 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001489 assert(AI != Fn->arg_end() && "Argument mismatch!");
1490 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001491 if (hasAggregateLLVMType(Ty)) {
1492 // Create a temporary alloca to hold the argument; the rest of
1493 // codegen expects to access aggregates & complex values by
1494 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001495 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001496 Builder.CreateStore(AI, V);
1497 } else {
1498 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1499 // This must be a promotion, for something like
1500 // "void a(x) short x; {..."
1501 V = EmitScalarConversion(V, Ty, Arg->getType());
1502 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001503 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001504 EmitParmDecl(*Arg, V);
1505 break;
1506 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001507
1508 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001509 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001510 // we need to create a temporary and reconstruct it from the
1511 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001512 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001513 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001514 (Name + ".addr").c_str());
1515 // FIXME: What are the right qualifiers here?
1516 llvm::Function::arg_iterator End =
1517 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1518 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001519
Daniel Dunbar04d35782008-09-17 00:51:38 +00001520 // Name the arguments used in expansion and increment AI.
1521 unsigned Index = 0;
1522 for (; AI != End; ++AI, ++Index)
1523 AI->setName(Name + "." + llvm::utostr(Index));
1524 continue;
1525 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001526
1527 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001528 // Initialize the local variable appropriately.
1529 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001530 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001531 } else {
1532 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1533 }
1534
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001535 // Skip increment, no matching LLVM parameter.
1536 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001537
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001538 case ABIArgInfo::Coerce: {
1539 assert(AI != Fn->arg_end() && "Argument mismatch!");
1540 // FIXME: This is very wasteful; EmitParmDecl is just going to
1541 // drop the result in a new alloca anyway, so we could just
1542 // store into that directly if we broke the abstraction down
1543 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001544 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001545 CreateCoercedStore(AI, V, *this);
1546 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001547 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001548 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001549 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1550 // This must be a promotion, for something like
1551 // "void a(x) short x; {..."
1552 V = EmitScalarConversion(V, Ty, Arg->getType());
1553 }
1554 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001555 EmitParmDecl(*Arg, V);
1556 break;
1557 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001558 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001559
1560 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001561 }
1562 assert(AI == Fn->arg_end() && "Argument mismatch!");
1563}
1564
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001565void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001566 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001567 llvm::Value *RV = 0;
1568
1569 // Functions with no result always return void.
1570 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001571 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001572 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001573
1574 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001575 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001576 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001577 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1578 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1579 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1580 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1581 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001582 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1583 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001584 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001585 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001586
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001587 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001588 // The internal return value temp always will have
1589 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001590 RV = Builder.CreateLoad(ReturnValue);
1591 break;
1592
Daniel Dunbar1358b202009-01-26 21:26:08 +00001593 case ABIArgInfo::Ignore:
1594 break;
1595
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001596 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001597 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001598 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001599
Daniel Dunbar22e30052008-09-11 01:48:57 +00001600 case ABIArgInfo::Expand:
1601 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001602 }
1603 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001604
1605 if (RV) {
1606 Builder.CreateRet(RV);
1607 } else {
1608 Builder.CreateRetVoid();
1609 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001610}
1611
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001612RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1613 llvm::Value *Callee,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001614 const CallArgList &CallArgs) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001615 // FIXME: We no longer need the types from CallArgs; lift up and
1616 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001617 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001618
1619 // Handle struct-return functions by passing a pointer to the
1620 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001621 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001622 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001623 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001624 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001625 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001626 }
1627
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001628 assert(CallInfo.arg_size() == CallArgs.size() &&
1629 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001630 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001631 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001632 I != E; ++I, ++info_it) {
1633 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001634 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001635
1636 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001637 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001638 if (RV.isScalar() || RV.isComplex()) {
1639 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001640 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001641 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001642 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001643 else
1644 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1645 } else {
1646 Args.push_back(RV.getAggregateAddr());
1647 }
1648 break;
1649
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001650 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001651 if (RV.isScalar()) {
1652 Args.push_back(RV.getScalarVal());
1653 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001654 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1655 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1656 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1657 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00001658 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001659 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001660 }
1661 break;
1662
Daniel Dunbar1358b202009-01-26 21:26:08 +00001663 case ABIArgInfo::Ignore:
1664 break;
1665
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001666 case ABIArgInfo::Coerce: {
1667 // FIXME: Avoid the conversion through memory if possible.
1668 llvm::Value *SrcPtr;
1669 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001670 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001671 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001672 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001673 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001674 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1675 } else
1676 SrcPtr = RV.getAggregateAddr();
1677 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1678 *this));
1679 break;
1680 }
1681
Daniel Dunbar04d35782008-09-17 00:51:38 +00001682 case ABIArgInfo::Expand:
1683 ExpandTypeToArgs(I->second, RV, Args);
1684 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001685 }
1686 }
1687
1688 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001689
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001690 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001691 CodeGen::AttributeListType AttributeList;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001692 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patela85a9ef2008-09-25 21:02:23 +00001693 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001694 AttributeList.size()));
1695
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001696 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1697 CI->setCallingConv(F->getCallingConv());
1698 if (CI->getType() != llvm::Type::VoidTy)
1699 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001700
1701 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001702 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001703 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001704 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001705 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001706 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001707 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001708 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001709
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001710 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001711 if (RetTy->isAnyComplexType()) {
1712 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1713 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1714 return RValue::getComplex(std::make_pair(Real, Imag));
1715 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001716 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00001717 Builder.CreateStore(CI, V);
1718 return RValue::getAggregate(V);
1719 } else
1720 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001721
Daniel Dunbar1358b202009-01-26 21:26:08 +00001722 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00001723 // If we are ignoring an argument that had a result, make sure to
1724 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00001725 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001726
Daniel Dunbar73d66602008-09-10 07:04:09 +00001727 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001728 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001729 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001730 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001731 if (RetTy->isAnyComplexType())
1732 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001733 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001734 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001735 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001736 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001737 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001738
Daniel Dunbar22e30052008-09-11 01:48:57 +00001739 case ABIArgInfo::Expand:
1740 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001741 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001742
1743 assert(0 && "Unhandled ABIArgInfo::Kind");
1744 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001745}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001746
1747/* VarArg handling */
1748
1749llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1750 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1751}