blob: f687a5bc46cd13ac642292b0e0462dc6ef12a829 [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
Daniel Dunbar92e88642009-02-17 07:55:55 +0000422 /// also be ComplexX87.
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 Dunbard6fb35c2009-02-17 02:45:44 +0000623 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000624
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000625 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
626 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000627 //
628 // Note, skip this test for bitfields, see below.
629 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000630 Lo = Memory;
631 return;
632 }
633
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000634 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000635 //
636 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
637 // exceeds a single eightbyte, each is classified
638 // separately. Each eightbyte gets initialized to class
639 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000640 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000641
642 // Bitfields require special handling, they do not force the
643 // structure to be passed in memory even if unaligned, and
644 // therefore they can straddle an eightbyte.
645 if (BitField) {
646 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
647 uint64_t Size =
648 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
649
650 uint64_t EB_Lo = Offset / 64;
651 uint64_t EB_Hi = (Offset + Size - 1) / 64;
652 FieldLo = FieldHi = NoClass;
653 if (EB_Lo) {
654 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
655 FieldLo = NoClass;
656 FieldHi = Integer;
657 } else {
658 FieldLo = Integer;
659 FieldHi = EB_Hi ? Integer : NoClass;
660 }
661 } else
662 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000663 Lo = merge(Lo, FieldLo);
664 Hi = merge(Hi, FieldHi);
665 if (Lo == Memory || Hi == Memory)
666 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000667 }
668
669 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
670 //
671 // (a) If one of the classes is MEMORY, the whole argument is
672 // passed in memory.
673 //
674 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
675
676 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000677 // the merge (just bail).
678 //
679 // The second condition occurs in the case of unions; for example
680 // union { _Complex double; unsigned; }.
681 if (Hi == Memory)
682 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000683 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000684 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000685 }
686}
687
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000688ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
689 const llvm::Type *CoerceTo,
690 ASTContext &Context) const {
691 if (CoerceTo == llvm::Type::Int64Ty) {
692 // Integer and pointer types will end up in a general purpose
693 // register.
694 if (Ty->isIntegerType() || Ty->isPointerType())
695 return ABIArgInfo::getDirect();
696 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000697 // FIXME: It would probably be better to make CGFunctionInfo only
698 // map using canonical types than to canonize here.
699 QualType CTy = Context.getCanonicalType(Ty);
700
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000701 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000702 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000703 return ABIArgInfo::getDirect();
704 }
705
706 return ABIArgInfo::getCoerce(CoerceTo);
707}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000708
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000709ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
710 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000711 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
712 // classification algorithm.
713 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000714 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000715
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000716 // Check some invariants.
717 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
718 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
719 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
720
Daniel Dunbare09a9692009-01-24 08:32:22 +0000721 const llvm::Type *ResType = 0;
722 switch (Lo) {
723 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000724 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000725
726 case SSEUp:
727 case X87Up:
728 assert(0 && "Invalid classification for lo word.");
729
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000730 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000731 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000732 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000733 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000734
735 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
736 // available register of the sequence %rax, %rdx is used.
737 case Integer:
738 ResType = llvm::Type::Int64Ty; break;
739
740 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
741 // available SSE register of the sequence %xmm0, %xmm1 is used.
742 case SSE:
743 ResType = llvm::Type::DoubleTy; break;
744
745 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
746 // returned on the X87 stack in %st0 as 80-bit x87 number.
747 case X87:
748 ResType = llvm::Type::X86_FP80Ty; break;
749
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000750 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
751 // part of the value is returned in %st0 and the imaginary part in
752 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000753 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000754 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbare09a9692009-01-24 08:32:22 +0000755 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
756 break;
757 }
758
759 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000760 // Memory was handled previously and X87 should
761 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000762 case Memory:
763 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000764 assert(0 && "Invalid classification for hi word.");
765
Daniel Dunbar92e88642009-02-17 07:55:55 +0000766 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000767 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000768
Daniel Dunbare09a9692009-01-24 08:32:22 +0000769 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000770 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
771 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000772 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000773 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
774 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000775
776 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
777 // is passed in the upper half of the last used SSE register.
778 //
779 // SSEUP should always be preceeded by SSE, just widen.
780 case SSEUp:
781 assert(Lo == SSE && "Unexpected SSEUp classification.");
782 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
783 break;
784
785 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000786 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000787 //
788 // X87UP should always be preceeded by X87, so we don't need to do
789 // anything here.
790 case X87Up:
791 assert(Lo == X87 && "Unexpected X87Up classification.");
792 break;
793 }
794
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000795 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000796}
797
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000798ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000799 unsigned &neededInt,
800 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000801 X86_64ABIInfo::Class Lo, Hi;
802 classify(Ty, Context, 0, Lo, Hi);
803
804 // Check some invariants.
805 // FIXME: Enforce these by construction.
806 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
807 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
808 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
809
Daniel Dunbare978cb92009-02-10 17:06:09 +0000810 neededInt = 0;
811 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000812 const llvm::Type *ResType = 0;
813 switch (Lo) {
814 case NoClass:
815 return ABIArgInfo::getIgnore();
816
817 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
818 // on the stack.
819 case Memory:
820
821 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
822 // COMPLEX_X87, it is passed in memory.
823 case X87:
824 case ComplexX87:
825 // Choose appropriate in memory type.
826 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000827 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000828 else
829 return ABIArgInfo::getDirect();
830
831 case SSEUp:
832 case X87Up:
833 assert(0 && "Invalid classification for lo word.");
834
835 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
836 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
837 // and %r9 is used.
838 case Integer:
839 ++neededInt;
840 ResType = llvm::Type::Int64Ty;
841 break;
842
843 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
844 // available SSE register is used, the registers are taken in the
845 // order from %xmm0 to %xmm7.
846 case SSE:
847 ++neededSSE;
848 ResType = llvm::Type::DoubleTy;
849 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000850 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000851
852 switch (Hi) {
853 // Memory was handled previously, ComplexX87 and X87 should
854 // never occur as hi classes, and X87Up must be preceed by X87,
855 // which is passed in memory.
856 case Memory:
857 case X87:
858 case X87Up:
859 case ComplexX87:
860 assert(0 && "Invalid classification for hi word.");
861
862 case NoClass: break;
863 case Integer:
864 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
865 ++neededInt;
866 break;
867 case SSE:
868 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
869 ++neededSSE;
870 break;
871
872 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
873 // eightbyte is passed in the upper half of the last used SSE
874 // register.
875 case SSEUp:
876 assert(Lo == SSE && "Unexpected SSEUp classification.");
877 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
878 break;
879 }
880
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000881 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000882}
883
884void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
885 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
886
887 // Keep track of the number of assigned registers.
888 unsigned freeIntRegs = 6, freeSSERegs = 8;
889
890 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
891 // get assigned (in left-to-right order) for passing as follows...
892 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +0000893 it != ie; ++it) {
894 unsigned neededInt, neededSSE;
895 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
896
897 // AMD64-ABI 3.2.3p3: If there are no registers available for any
898 // eightbyte of an argument, the whole argument is passed on the
899 // stack. If registers have already been assigned for some
900 // eightbytes of such an argument, the assignments get reverted.
901 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
902 freeIntRegs -= neededInt;
903 freeSSERegs -= neededSSE;
904 } else {
905 // Choose appropriate in memory type.
906 if (CodeGenFunction::hasAggregateLLVMType(it->type))
907 it->info = ABIArgInfo::getIndirect(0);
908 else
909 it->info = ABIArgInfo::getDirect();
910 }
911 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000912}
913
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000914static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
915 QualType Ty,
916 CodeGenFunction &CGF) {
917 llvm::Value *overflow_arg_area_p =
918 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
919 llvm::Value *overflow_arg_area =
920 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
921
922 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
923 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000924 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000925 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000926 // Note that we follow the ABI & gcc here, even though the type
927 // could in theory have an alignment greater than 16. This case
928 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000929
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000930 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
931 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
932 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
933 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
934 llvm::Type::Int64Ty);
935 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
936 overflow_arg_area =
937 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
938 overflow_arg_area->getType(),
939 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000940 }
941
942 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
943 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
944 llvm::Value *Res =
945 CGF.Builder.CreateBitCast(overflow_arg_area,
946 llvm::PointerType::getUnqual(LTy));
947
948 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
949 // l->overflow_arg_area + sizeof(type).
950 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
951 // an 8 byte boundary.
952
953 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
954 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
955 (SizeInBytes + 7) & ~7);
956 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
957 "overflow_arg_area.next");
958 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
959
960 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
961 return Res;
962}
963
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000964llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
965 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000966 // Assume that va_list type is correct; should be pointer to LLVM type:
967 // struct {
968 // i32 gp_offset;
969 // i32 fp_offset;
970 // i8* overflow_arg_area;
971 // i8* reg_save_area;
972 // };
973 unsigned neededInt, neededSSE;
974 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
975 neededInt, neededSSE);
976
977 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
978 // in the registers. If not go to step 7.
979 if (!neededInt && !neededSSE)
980 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
981
982 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
983 // general purpose registers needed to pass type and num_fp to hold
984 // the number of floating point registers needed.
985
986 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
987 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
988 // l->fp_offset > 304 - num_fp * 16 go to step 7.
989 //
990 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
991 // register save space).
992
993 llvm::Value *InRegs = 0;
994 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
995 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
996 if (neededInt) {
997 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
998 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
999 InRegs =
1000 CGF.Builder.CreateICmpULE(gp_offset,
1001 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1002 48 - neededInt * 8),
1003 "fits_in_gp");
1004 }
1005
1006 if (neededSSE) {
1007 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1008 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1009 llvm::Value *FitsInFP =
1010 CGF.Builder.CreateICmpULE(fp_offset,
1011 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1012 176 - neededSSE * 8),
1013 "fits_in_fp");
1014 InRegs = InRegs ? CGF.Builder.CreateOr(InRegs, FitsInFP) : FitsInFP;
1015 }
1016
1017 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1018 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1019 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1020 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1021
1022 // Emit code to load the value if it was passed in registers.
1023
1024 CGF.EmitBlock(InRegBlock);
1025
1026 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1027 // an offset of l->gp_offset and/or l->fp_offset. This may require
1028 // copying to a temporary location in case the parameter is passed
1029 // in different register classes or requires an alignment greater
1030 // than 8 for general purpose registers and 16 for XMM registers.
1031 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1032 llvm::Value *RegAddr =
1033 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1034 "reg_save_area");
1035 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001036 // FIXME: Cleanup.
1037 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1038 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1039 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1040 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1041 const llvm::Type *TyLo = ST->getElementType(0);
1042 const llvm::Type *TyHi = ST->getElementType(1);
1043 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1044 "Unexpected ABI info for mixed regs");
1045 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1046 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1047 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1048 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1049 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1050 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1051 llvm::Value *V =
1052 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1053 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1054 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1055 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1056
1057 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001058 } else if (neededInt) {
1059 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1060 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1061 llvm::PointerType::getUnqual(LTy));
1062 } else {
1063 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1064 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1065 llvm::PointerType::getUnqual(LTy));
1066 }
1067
1068 // AMD64-ABI 3.5.7p5: Step 5. Set:
1069 // l->gp_offset = l->gp_offset + num_gp * 8
1070 // l->fp_offset = l->fp_offset + num_fp * 16.
1071 if (neededInt) {
1072 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1073 neededInt * 8);
1074 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1075 gp_offset_p);
1076 }
1077 if (neededSSE) {
1078 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1079 neededSSE * 16);
1080 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1081 fp_offset_p);
1082 }
1083 CGF.EmitBranch(ContBlock);
1084
1085 // Emit code to load the value if it was passed in memory.
1086
1087 CGF.EmitBlock(InMemBlock);
1088 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1089
1090 // Return the appropriate result.
1091
1092 CGF.EmitBlock(ContBlock);
1093 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1094 "vaarg.addr");
1095 ResAddr->reserveOperandSpace(2);
1096 ResAddr->addIncoming(RegAddr, InRegBlock);
1097 ResAddr->addIncoming(MemAddr, InMemBlock);
1098
1099 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001100}
1101
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001102ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001103 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001104 if (RetTy->isVoidType()) {
1105 return ABIArgInfo::getIgnore();
1106 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001107 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001108 } else {
1109 return ABIArgInfo::getDirect();
1110 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001111}
1112
1113ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001114 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001115 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001116 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001117 } else {
1118 return ABIArgInfo::getDirect();
1119 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001120}
1121
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001122llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1123 CodeGenFunction &CGF) const {
1124 return 0;
1125}
1126
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001127const ABIInfo &CodeGenTypes::getABIInfo() const {
1128 if (TheABIInfo)
1129 return *TheABIInfo;
1130
1131 // For now we just cache this in the CodeGenTypes and don't bother
1132 // to free it.
1133 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1134 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001135 switch (getContext().Target.getPointerWidth(0)) {
1136 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001137 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001138 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001139 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001140 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001141 }
1142
1143 return *(TheABIInfo = new DefaultABIInfo);
1144}
1145
Daniel Dunbare126ab12008-09-10 02:41:04 +00001146/***/
1147
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001148CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1149 const llvm::SmallVector<QualType, 16> &ArgTys) {
1150 NumArgs = ArgTys.size();
1151 Args = new ArgInfo[1 + NumArgs];
1152 Args[0].type = ResTy;
1153 for (unsigned i = 0; i < NumArgs; ++i)
1154 Args[1 + i].type = ArgTys[i];
1155}
1156
1157/***/
1158
Daniel Dunbar04d35782008-09-17 00:51:38 +00001159void CodeGenTypes::GetExpandedTypes(QualType Ty,
1160 std::vector<const llvm::Type*> &ArgTys) {
1161 const RecordType *RT = Ty->getAsStructureType();
1162 assert(RT && "Can only expand structure types.");
1163 const RecordDecl *RD = RT->getDecl();
1164 assert(!RD->hasFlexibleArrayMember() &&
1165 "Cannot expand structure with flexible array.");
1166
Douglas Gregor5d764842009-01-09 17:18:27 +00001167 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001168 e = RD->field_end(); i != e; ++i) {
1169 const FieldDecl *FD = *i;
1170 assert(!FD->isBitField() &&
1171 "Cannot expand structure with bit-field members.");
1172
1173 QualType FT = FD->getType();
1174 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1175 GetExpandedTypes(FT, ArgTys);
1176 } else {
1177 ArgTys.push_back(ConvertType(FT));
1178 }
1179 }
1180}
1181
1182llvm::Function::arg_iterator
1183CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1184 llvm::Function::arg_iterator AI) {
1185 const RecordType *RT = Ty->getAsStructureType();
1186 assert(RT && "Can only expand structure types.");
1187
1188 RecordDecl *RD = RT->getDecl();
1189 assert(LV.isSimple() &&
1190 "Unexpected non-simple lvalue during struct expansion.");
1191 llvm::Value *Addr = LV.getAddress();
1192 for (RecordDecl::field_iterator i = RD->field_begin(),
1193 e = RD->field_end(); i != e; ++i) {
1194 FieldDecl *FD = *i;
1195 QualType FT = FD->getType();
1196
1197 // FIXME: What are the right qualifiers here?
1198 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1199 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1200 AI = ExpandTypeFromArgs(FT, LV, AI);
1201 } else {
1202 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1203 ++AI;
1204 }
1205 }
1206
1207 return AI;
1208}
1209
1210void
1211CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1212 llvm::SmallVector<llvm::Value*, 16> &Args) {
1213 const RecordType *RT = Ty->getAsStructureType();
1214 assert(RT && "Can only expand structure types.");
1215
1216 RecordDecl *RD = RT->getDecl();
1217 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1218 llvm::Value *Addr = RV.getAggregateAddr();
1219 for (RecordDecl::field_iterator i = RD->field_begin(),
1220 e = RD->field_end(); i != e; ++i) {
1221 FieldDecl *FD = *i;
1222 QualType FT = FD->getType();
1223
1224 // FIXME: What are the right qualifiers here?
1225 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1226 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1227 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1228 } else {
1229 RValue RV = EmitLoadOfLValue(LV, FT);
1230 assert(RV.isScalar() &&
1231 "Unexpected non-scalar rvalue during struct expansion.");
1232 Args.push_back(RV.getScalarVal());
1233 }
1234 }
1235}
1236
Daniel Dunbar84379912009-02-02 19:06:38 +00001237/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1238/// a pointer to an object of type \arg Ty.
1239///
1240/// This safely handles the case when the src type is smaller than the
1241/// destination type; in this situation the values of bits which not
1242/// present in the src are undefined.
1243static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1244 const llvm::Type *Ty,
1245 CodeGenFunction &CGF) {
1246 const llvm::Type *SrcTy =
1247 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1248 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1249 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1250
Daniel Dunbar77071992009-02-03 05:59:18 +00001251 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001252 if (SrcSize == DstSize) {
1253 llvm::Value *Casted =
1254 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001255 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1256 // FIXME: Use better alignment / avoid requiring aligned load.
1257 Load->setAlignment(1);
1258 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001259 } else {
1260 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1261
1262 // Otherwise do coercion through memory. This is stupid, but
1263 // simple.
1264 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1265 llvm::Value *Casted =
1266 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001267 llvm::StoreInst *Store =
1268 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1269 // FIXME: Use better alignment / avoid requiring aligned store.
1270 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001271 return CGF.Builder.CreateLoad(Tmp);
1272 }
1273}
1274
1275/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1276/// where the source and destination may have different types.
1277///
1278/// This safely handles the case when the src type is larger than the
1279/// destination type; the upper bits of the src will be lost.
1280static void CreateCoercedStore(llvm::Value *Src,
1281 llvm::Value *DstPtr,
1282 CodeGenFunction &CGF) {
1283 const llvm::Type *SrcTy = Src->getType();
1284 const llvm::Type *DstTy =
1285 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1286
1287 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1288 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1289
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001290 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001291 if (SrcSize == DstSize) {
1292 llvm::Value *Casted =
1293 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001294 // FIXME: Use better alignment / avoid requiring aligned store.
1295 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001296 } else {
1297 assert(SrcSize > DstSize && "Coercion is missing bits!");
1298
1299 // Otherwise do coercion through memory. This is stupid, but
1300 // simple.
1301 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1302 CGF.Builder.CreateStore(Src, Tmp);
1303 llvm::Value *Casted =
1304 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001305 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1306 // FIXME: Use better alignment / avoid requiring aligned load.
1307 Load->setAlignment(1);
1308 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001309 }
1310}
1311
Daniel Dunbar04d35782008-09-17 00:51:38 +00001312/***/
1313
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001314bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001315 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001316}
1317
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001318const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001319CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001320 std::vector<const llvm::Type*> ArgTys;
1321
1322 const llvm::Type *ResultType = 0;
1323
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001324 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001325 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001326 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001327 case ABIArgInfo::Expand:
1328 assert(0 && "Invalid ABI kind for return argument");
1329
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001330 case ABIArgInfo::Direct:
1331 ResultType = ConvertType(RetTy);
1332 break;
1333
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001334 case ABIArgInfo::Indirect: {
1335 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001336 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001337 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001338 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1339 break;
1340 }
1341
Daniel Dunbar1358b202009-01-26 21:26:08 +00001342 case ABIArgInfo::Ignore:
1343 ResultType = llvm::Type::VoidTy;
1344 break;
1345
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001346 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001347 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001348 break;
1349 }
1350
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001351 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1352 ie = FI.arg_end(); it != ie; ++it) {
1353 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001354
1355 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001356 case ABIArgInfo::Ignore:
1357 break;
1358
Daniel Dunbar04d35782008-09-17 00:51:38 +00001359 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001360 ArgTys.push_back(AI.getCoerceToType());
1361 break;
1362
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001363 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001364 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001365 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1366 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001367 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001368 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001369
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001370 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001371 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001372 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001373
1374 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001375 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001376 break;
1377 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001378 }
1379
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001380 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001381}
1382
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001383void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001384 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001385 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001386 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001387 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001388
1389 if (TargetDecl) {
1390 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001391 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001392 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001393 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001394 if (TargetDecl->getAttr<PureAttr>())
1395 FuncAttrs |= llvm::Attribute::ReadOnly;
1396 if (TargetDecl->getAttr<ConstAttr>())
1397 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001398 }
1399
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001400 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001401 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001402 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001403 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001404 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001405 if (RetTy->isPromotableIntegerType()) {
1406 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001407 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001408 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001409 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001410 }
1411 }
1412 break;
1413
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001414 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001415 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001416 llvm::Attribute::StructRet |
1417 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001418 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001419 break;
1420
Daniel Dunbar1358b202009-01-26 21:26:08 +00001421 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001422 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001423 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001424
Daniel Dunbar22e30052008-09-11 01:48:57 +00001425 case ABIArgInfo::Expand:
1426 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001427 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001428
Devang Patel2bb6eb82008-09-26 22:53:57 +00001429 if (RetAttrs)
1430 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001431 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1432 ie = FI.arg_end(); it != ie; ++it) {
1433 QualType ParamType = it->type;
1434 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001435 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001436
1437 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001438 case ABIArgInfo::Coerce:
1439 break;
1440
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001441 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001442 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarb3f651a2009-02-05 01:31:19 +00001443 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001444 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001445 break;
1446
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001447 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001448 if (ParamType->isPromotableIntegerType()) {
1449 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001450 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001451 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001452 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001453 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001454 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001455 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001456
Daniel Dunbar1358b202009-01-26 21:26:08 +00001457 case ABIArgInfo::Ignore:
1458 // Skip increment, no matching LLVM parameter.
1459 continue;
1460
Daniel Dunbar04d35782008-09-17 00:51:38 +00001461 case ABIArgInfo::Expand: {
1462 std::vector<const llvm::Type*> Tys;
1463 // FIXME: This is rather inefficient. Do we ever actually need
1464 // to do anything here? The result should be just reconstructed
1465 // on the other side, so extension should be a non-issue.
1466 getTypes().GetExpandedTypes(ParamType, Tys);
1467 Index += Tys.size();
1468 continue;
1469 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001470 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001471
Devang Patela85a9ef2008-09-25 21:02:23 +00001472 if (Attributes)
1473 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001474 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001475 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001476 if (FuncAttrs)
1477 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1478
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001479}
1480
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001481void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1482 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001483 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001484 // FIXME: We no longer need the types from FunctionArgList; lift up
1485 // and simplify.
1486
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001487 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1488 llvm::Function::arg_iterator AI = Fn->arg_begin();
1489
1490 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001491 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001492 AI->setName("agg.result");
1493 ++AI;
1494 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001495
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001496 assert(FI.arg_size() == Args.size() &&
1497 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001498 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001499 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001500 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001501 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001502 QualType Ty = info_it->type;
1503 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001504
1505 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001506 case ABIArgInfo::Indirect: {
1507 llvm::Value* V = AI;
1508 if (hasAggregateLLVMType(Ty)) {
1509 // Do nothing, aggregates and complex variables are accessed by
1510 // reference.
1511 } else {
1512 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001513 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001514 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1515 // This must be a promotion, for something like
1516 // "void a(x) short x; {..."
1517 V = EmitScalarConversion(V, Ty, Arg->getType());
1518 }
1519 }
1520 EmitParmDecl(*Arg, V);
1521 break;
1522 }
1523
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001524 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001525 assert(AI != Fn->arg_end() && "Argument mismatch!");
1526 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001527 if (hasAggregateLLVMType(Ty)) {
1528 // Create a temporary alloca to hold the argument; the rest of
1529 // codegen expects to access aggregates & complex values by
1530 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001531 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001532 Builder.CreateStore(AI, V);
1533 } else {
1534 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1535 // This must be a promotion, for something like
1536 // "void a(x) short x; {..."
1537 V = EmitScalarConversion(V, Ty, Arg->getType());
1538 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001539 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001540 EmitParmDecl(*Arg, V);
1541 break;
1542 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001543
1544 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001545 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001546 // we need to create a temporary and reconstruct it from the
1547 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001548 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001549 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001550 (Name + ".addr").c_str());
1551 // FIXME: What are the right qualifiers here?
1552 llvm::Function::arg_iterator End =
1553 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1554 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001555
Daniel Dunbar04d35782008-09-17 00:51:38 +00001556 // Name the arguments used in expansion and increment AI.
1557 unsigned Index = 0;
1558 for (; AI != End; ++AI, ++Index)
1559 AI->setName(Name + "." + llvm::utostr(Index));
1560 continue;
1561 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001562
1563 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001564 // Initialize the local variable appropriately.
1565 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001566 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001567 } else {
1568 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1569 }
1570
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001571 // Skip increment, no matching LLVM parameter.
1572 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001573
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001574 case ABIArgInfo::Coerce: {
1575 assert(AI != Fn->arg_end() && "Argument mismatch!");
1576 // FIXME: This is very wasteful; EmitParmDecl is just going to
1577 // drop the result in a new alloca anyway, so we could just
1578 // store into that directly if we broke the abstraction down
1579 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001580 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001581 CreateCoercedStore(AI, V, *this);
1582 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001583 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001584 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001585 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1586 // This must be a promotion, for something like
1587 // "void a(x) short x; {..."
1588 V = EmitScalarConversion(V, Ty, Arg->getType());
1589 }
1590 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001591 EmitParmDecl(*Arg, V);
1592 break;
1593 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001594 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001595
1596 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001597 }
1598 assert(AI == Fn->arg_end() && "Argument mismatch!");
1599}
1600
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001601void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001602 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001603 llvm::Value *RV = 0;
1604
1605 // Functions with no result always return void.
1606 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001607 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001608 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001609
1610 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001611 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001612 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001613 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1614 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1615 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1616 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1617 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001618 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1619 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001620 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001621 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001622
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001623 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001624 // The internal return value temp always will have
1625 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001626 RV = Builder.CreateLoad(ReturnValue);
1627 break;
1628
Daniel Dunbar1358b202009-01-26 21:26:08 +00001629 case ABIArgInfo::Ignore:
1630 break;
1631
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001632 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001633 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001634 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001635
Daniel Dunbar22e30052008-09-11 01:48:57 +00001636 case ABIArgInfo::Expand:
1637 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001638 }
1639 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001640
1641 if (RV) {
1642 Builder.CreateRet(RV);
1643 } else {
1644 Builder.CreateRetVoid();
1645 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001646}
1647
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001648RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1649 llvm::Value *Callee,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001650 const CallArgList &CallArgs) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001651 // FIXME: We no longer need the types from CallArgs; lift up and
1652 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001653 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001654
1655 // Handle struct-return functions by passing a pointer to the
1656 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001657 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001658 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001659 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001660 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001661 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001662 }
1663
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001664 assert(CallInfo.arg_size() == CallArgs.size() &&
1665 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001666 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001667 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001668 I != E; ++I, ++info_it) {
1669 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001670 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001671
1672 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001673 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001674 if (RV.isScalar() || RV.isComplex()) {
1675 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001676 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001677 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001678 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001679 else
1680 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1681 } else {
1682 Args.push_back(RV.getAggregateAddr());
1683 }
1684 break;
1685
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001686 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001687 if (RV.isScalar()) {
1688 Args.push_back(RV.getScalarVal());
1689 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001690 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1691 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1692 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1693 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00001694 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001695 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001696 }
1697 break;
1698
Daniel Dunbar1358b202009-01-26 21:26:08 +00001699 case ABIArgInfo::Ignore:
1700 break;
1701
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001702 case ABIArgInfo::Coerce: {
1703 // FIXME: Avoid the conversion through memory if possible.
1704 llvm::Value *SrcPtr;
1705 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001706 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001707 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001708 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001709 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001710 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1711 } else
1712 SrcPtr = RV.getAggregateAddr();
1713 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1714 *this));
1715 break;
1716 }
1717
Daniel Dunbar04d35782008-09-17 00:51:38 +00001718 case ABIArgInfo::Expand:
1719 ExpandTypeToArgs(I->second, RV, Args);
1720 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001721 }
1722 }
1723
1724 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001725
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001726 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001727 CodeGen::AttributeListType AttributeList;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001728 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patela85a9ef2008-09-25 21:02:23 +00001729 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001730 AttributeList.size()));
1731
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001732 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1733 CI->setCallingConv(F->getCallingConv());
1734 if (CI->getType() != llvm::Type::VoidTy)
1735 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001736
1737 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001738 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001739 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001740 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001741 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001742 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001743 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001744 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001745
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001746 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001747 if (RetTy->isAnyComplexType()) {
1748 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1749 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1750 return RValue::getComplex(std::make_pair(Real, Imag));
1751 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001752 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00001753 Builder.CreateStore(CI, V);
1754 return RValue::getAggregate(V);
1755 } else
1756 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001757
Daniel Dunbar1358b202009-01-26 21:26:08 +00001758 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00001759 // If we are ignoring an argument that had a result, make sure to
1760 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00001761 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001762
Daniel Dunbar73d66602008-09-10 07:04:09 +00001763 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001764 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001765 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001766 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001767 if (RetTy->isAnyComplexType())
1768 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001769 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001770 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001771 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001772 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001773 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001774
Daniel Dunbar22e30052008-09-11 01:48:57 +00001775 case ABIArgInfo::Expand:
1776 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001777 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001778
1779 assert(0 && "Unhandled ABIArgInfo::Kind");
1780 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001781}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001782
1783/* VarArg handling */
1784
1785llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1786 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1787}