blob: 8027612cd4b3f478bba55c7bc8fddfcb0e2ecfb6 [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 Dunbar708d8a82009-01-27 01:36:03 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000027using namespace clang;
28using namespace CodeGen;
29
Daniel Dunbare09a9692009-01-24 08:32:22 +000030static llvm::cl::opt<bool>
31UseX86_64ABI("use-x86_64-abi",
32 llvm::cl::desc("Enable use of experimental x86_64 ABI."),
33 llvm::cl::init(false));
34
Daniel Dunbara8f02052008-09-08 21:33:45 +000035/***/
36
Daniel Dunbara8f02052008-09-08 21:33:45 +000037// FIXME: Use iterator and sidestep silly type array creation.
38
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000039CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
40 : IsVariadic(true)
41{
42 ArgTypes.push_back(FTNP->getResultType());
43}
44
45CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
46 : IsVariadic(FTP->isVariadic())
47{
48 ArgTypes.push_back(FTP->getResultType());
49 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
50 ArgTypes.push_back(FTP->getArgType(i));
51}
52
53// FIXME: Is there really any reason to have this still?
Daniel Dunbara8f02052008-09-08 21:33:45 +000054CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbara8f02052008-09-08 21:33:45 +000055{
56 const FunctionType *FTy = FD->getType()->getAsFunctionType();
57 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000058
Daniel Dunbara8f02052008-09-08 21:33:45 +000059 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000060 if (FTP) {
61 IsVariadic = FTP->isVariadic();
Daniel Dunbara8f02052008-09-08 21:33:45 +000062 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
63 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000064 } else {
65 IsVariadic = true;
66 }
Daniel Dunbara8f02052008-09-08 21:33:45 +000067}
68
69CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
70 const ASTContext &Context)
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000071 : IsVariadic(MD->isVariadic())
Daniel Dunbara8f02052008-09-08 21:33:45 +000072{
73 ArgTypes.push_back(MD->getResultType());
74 ArgTypes.push_back(MD->getSelfDecl()->getType());
75 ArgTypes.push_back(Context.getObjCSelType());
76 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
77 e = MD->param_end(); i != e; ++i)
78 ArgTypes.push_back((*i)->getType());
79}
80
Daniel Dunbarbccb0682008-09-10 00:32:18 +000081ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
82 return ArgTypes.begin();
83}
84
85ArgTypeIterator CGFunctionInfo::argtypes_end() const {
86 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000087}
88
89/***/
90
Daniel Dunbarbccb0682008-09-10 00:32:18 +000091CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
92 ArgTypes.push_back(_ResultType);
93 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbara8f02052008-09-08 21:33:45 +000094 ArgTypes.push_back(i->second);
95}
96
Daniel Dunbarbccb0682008-09-10 00:32:18 +000097ArgTypeIterator CGCallInfo::argtypes_begin() const {
98 return ArgTypes.begin();
99}
100
101ArgTypeIterator CGCallInfo::argtypes_end() const {
102 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +0000103}
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000104
105/***/
106
Daniel Dunbar22e30052008-09-11 01:48:57 +0000107/// ABIArgInfo - Helper class to encapsulate information about how a
108/// specific C type should be passed to or returned from a function.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000109class ABIArgInfo {
110public:
111 enum Kind {
112 Default,
Daniel Dunbar17d35372008-12-18 04:52:14 +0000113 StructRet, /// Only valid for return values. The return value
114 /// should be passed through a pointer to a caller
115 /// allocated location passed as an implicit first
116 /// argument to the function.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000117
Daniel Dunbar1358b202009-01-26 21:26:08 +0000118 Ignore, /// Ignore the argument (treat as void). Useful for
119 /// void and empty structs.
120
Daniel Dunbar04d35782008-09-17 00:51:38 +0000121 Coerce, /// Only valid for aggregate return types, the argument
122 /// should be accessed by coercion to a provided type.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000123
124 ByVal, /// Only valid for aggregate argument types. The
125 /// structure should be passed "byval" with the
126 /// specified alignment (0 indicates default
127 /// alignment).
128
129 Expand, /// Only valid for aggregate argument types. The
130 /// structure should be expanded into consecutive
Daniel Dunbar04d35782008-09-17 00:51:38 +0000131 /// arguments for its constituent fields. Currently
132 /// expand is only allowed on structures whose fields
133 /// are all scalar types or are themselves expandable
134 /// types.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000135
136 KindFirst=Default, KindLast=Expand
Daniel Dunbare126ab12008-09-10 02:41:04 +0000137 };
138
139private:
140 Kind TheKind;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000141 const llvm::Type *TypeData;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000142 unsigned UIntData;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000143
Daniel Dunbar22e30052008-09-11 01:48:57 +0000144 ABIArgInfo(Kind K, const llvm::Type *TD=0,
145 unsigned UI=0) : TheKind(K),
146 TypeData(TD),
147 UIntData(0) {}
Daniel Dunbare126ab12008-09-10 02:41:04 +0000148public:
149 static ABIArgInfo getDefault() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000150 return ABIArgInfo(Default);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000151 }
152 static ABIArgInfo getStructRet() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000153 return ABIArgInfo(StructRet);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000154 }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000155 static ABIArgInfo getIgnore() {
156 return ABIArgInfo(Ignore);
157 }
Daniel Dunbar73d66602008-09-10 07:04:09 +0000158 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000159 return ABIArgInfo(Coerce, T);
160 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000161 static ABIArgInfo getByVal(unsigned Alignment) {
162 return ABIArgInfo(ByVal, 0, Alignment);
163 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000164 static ABIArgInfo getExpand() {
165 return ABIArgInfo(Expand);
166 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000167
168 Kind getKind() const { return TheKind; }
169 bool isDefault() const { return TheKind == Default; }
170 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000171 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000172 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000173 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000174 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000175
176 // Coerce accessors
Daniel Dunbar73d66602008-09-10 07:04:09 +0000177 const llvm::Type *getCoerceToType() const {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000178 assert(TheKind == Coerce && "Invalid kind!");
179 return TypeData;
180 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000181
182 // ByVal accessors
183 unsigned getByValAlignment() const {
184 assert(TheKind == ByVal && "Invalid kind!");
185 return UIntData;
186 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000187};
188
189/***/
190
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000191/* FIXME: All of this stuff should be part of the target interface
192 somehow. It is currently here because it is not clear how to factor
193 the targets to support this, since the Targets currently live in a
194 layer below types n'stuff.
195 */
196
197/// ABIInfo - Target specific hooks for defining how a type should be
198/// passed or returned from functions.
199class clang::ABIInfo {
200public:
201 virtual ~ABIInfo();
202
203 virtual ABIArgInfo classifyReturnType(QualType RetTy,
204 ASTContext &Context) const = 0;
205
206 virtual ABIArgInfo classifyArgumentType(QualType Ty,
207 ASTContext &Context) const = 0;
208};
209
210ABIInfo::~ABIInfo() {}
211
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000212/// isEmptyStruct - Return true iff a structure has no non-empty
213/// members. Note that a structure with a flexible array member is not
214/// considered empty.
215static bool isEmptyStruct(QualType T) {
216 const RecordType *RT = T->getAsStructureType();
217 if (!RT)
218 return 0;
219 const RecordDecl *RD = RT->getDecl();
220 if (RD->hasFlexibleArrayMember())
221 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000222 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000223 e = RD->field_end(); i != e; ++i) {
224 const FieldDecl *FD = *i;
225 if (!isEmptyStruct(FD->getType()))
226 return false;
227 }
228 return true;
229}
230
231/// isSingleElementStruct - Determine if a structure is a "single
232/// element struct", i.e. it has exactly one non-empty field or
233/// exactly one field which is itself a single element
234/// struct. Structures with flexible array members are never
235/// considered single element structs.
236///
237/// \return The field declaration for the single non-empty field, if
238/// it exists.
239static const FieldDecl *isSingleElementStruct(QualType T) {
240 const RecordType *RT = T->getAsStructureType();
241 if (!RT)
242 return 0;
243
244 const RecordDecl *RD = RT->getDecl();
245 if (RD->hasFlexibleArrayMember())
246 return 0;
247
248 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000249 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000250 e = RD->field_end(); i != e; ++i) {
251 const FieldDecl *FD = *i;
252 QualType FT = FD->getType();
253
254 if (isEmptyStruct(FT)) {
255 // Ignore
256 } else if (Found) {
257 return 0;
258 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
259 Found = FD;
260 } else {
261 Found = isSingleElementStruct(FT);
262 if (!Found)
263 return 0;
264 }
265 }
266
267 return Found;
268}
269
270static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
271 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
272 return false;
273
274 uint64_t Size = Context.getTypeSize(Ty);
275 return Size == 32 || Size == 64;
276}
277
278static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
279 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000280 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000281 e = RD->field_end(); i != e; ++i) {
282 const FieldDecl *FD = *i;
283
284 if (!is32Or64BitBasicType(FD->getType(), Context))
285 return false;
286
287 // If this is a bit-field we need to make sure it is still a
288 // 32-bit or 64-bit type.
289 if (Expr *BW = FD->getBitWidth()) {
290 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
291 if (Width <= 16)
292 return false;
293 }
294 }
295 return true;
296}
297
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000298namespace {
299/// DefaultABIInfo - The default implementation for ABI specific
300/// details. This implementation provides information which results in
301/// sensible LLVM IR generation, but does not conform to any
302/// particular ABI.
303class DefaultABIInfo : public ABIInfo {
304 virtual ABIArgInfo classifyReturnType(QualType RetTy,
305 ASTContext &Context) const;
306
307 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
308 ASTContext &Context) const;
309};
310
311/// X86_32ABIInfo - The X86-32 ABI information.
312class X86_32ABIInfo : public ABIInfo {
313public:
314 virtual ABIArgInfo classifyReturnType(QualType RetTy,
315 ASTContext &Context) const;
316
317 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
318 ASTContext &Context) const;
319};
320}
321
322ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
323 ASTContext &Context) const {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000324 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000325 // Classify "single element" structs as their element type.
326 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
327 if (SeltFD) {
328 QualType SeltTy = SeltFD->getType()->getDesugaredType();
329 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
330 // FIXME: This is gross, it would be nice if we could just
331 // pass back SeltTy and have clients deal with it. Is it worth
332 // supporting coerce to both LLVM and clang Types?
333 if (BT->isIntegerType()) {
334 uint64_t Size = Context.getTypeSize(SeltTy);
335 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
336 } else if (BT->getKind() == BuiltinType::Float) {
337 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
338 } else if (BT->getKind() == BuiltinType::Double) {
339 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
340 }
341 } else if (SeltTy->isPointerType()) {
342 // FIXME: It would be really nice if this could come out as
343 // the proper pointer type.
344 llvm::Type *PtrTy =
345 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
346 return ABIArgInfo::getCoerce(PtrTy);
347 }
348 }
349
Daniel Dunbar73d66602008-09-10 07:04:09 +0000350 uint64_t Size = Context.getTypeSize(RetTy);
351 if (Size == 8) {
352 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
353 } else if (Size == 16) {
354 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
355 } else if (Size == 32) {
356 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
357 } else if (Size == 64) {
358 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
359 } else {
360 return ABIArgInfo::getStructRet();
361 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000362 } else {
363 return ABIArgInfo::getDefault();
364 }
365}
366
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000367ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
368 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000369 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000370 // Structures with flexible arrays are always byval.
371 if (const RecordType *RT = Ty->getAsStructureType())
372 if (RT->getDecl()->hasFlexibleArrayMember())
373 return ABIArgInfo::getByVal(0);
374
375 // Expand empty structs (i.e. ignore)
376 uint64_t Size = Context.getTypeSize(Ty);
377 if (Ty->isStructureType() && Size == 0)
378 return ABIArgInfo::getExpand();
379
380 // Expand structs with size <= 128-bits which consist only of
381 // basic types (int, long long, float, double, xxx*). This is
382 // non-recursive and does not ignore empty fields.
383 if (const RecordType *RT = Ty->getAsStructureType()) {
384 if (Context.getTypeSize(Ty) <= 4*32 &&
385 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
386 return ABIArgInfo::getExpand();
387 }
388
Daniel Dunbar22e30052008-09-11 01:48:57 +0000389 return ABIArgInfo::getByVal(0);
390 } else {
391 return ABIArgInfo::getDefault();
392 }
393}
394
Daniel Dunbare09a9692009-01-24 08:32:22 +0000395namespace {
396/// X86_32ABIInfo - The X86_64 ABI information.
397class X86_64ABIInfo : public ABIInfo {
398 enum Class {
399 Integer = 0,
400 SSE,
401 SSEUp,
402 X87,
403 X87Up,
404 ComplexX87,
405 NoClass,
406 Memory
407 };
408
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000409 /// merge - Implement the X86_64 ABI merging algorithm.
410 ///
411 /// \param Offset - The offset of the current field.
412 /// \param FieldLo - The low classification of the current field.
413 /// \param FieldHi - The high classification of the current field.
414 /// \param Lo [in] [out] - The accumulated low classification.
415 /// \param Lo [in] [out] - The accumulated high classification.
416 void merge(uint64_t Offset, Class FieldLo, Class FieldHi,
417 Class &Lo, Class &Hi) const;
418
Daniel Dunbare09a9692009-01-24 08:32:22 +0000419 /// classify - Determine the x86_64 register classes in which the
420 /// given type T should be passed.
421 ///
422 /// \param Lo - The classification for the low word of the type.
423 /// \param Hi - The classification for the high word of the type.
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000424 /// \param OffsetBase - The byte position of the type in the root
425 /// structure. Some parameters are classified different depending on
426 /// whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000427 ///
428 /// If a word is unused its result will be NoClass; if a type should
429 /// be passed in Memory then at least the classification of \arg Lo
430 /// will be Memory.
431 ///
432 /// The \arg Lo class will be NoClass iff the argument is ignored.
433 ///
434 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
435 /// be NoClass.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000436 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000437 Class &Lo, Class &Hi) const;
438
439public:
440 virtual ABIArgInfo classifyReturnType(QualType RetTy,
441 ASTContext &Context) const;
442
443 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
444 ASTContext &Context) const;
445};
446}
447
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000448void X86_64ABIInfo::merge(uint64_t Offset, Class FieldLo, Class FieldHi,
449 Class &Lo, Class &Hi) const {
450 // Determine which half of the structure we are classifying.
451 //
452 // AMD64-ABI 3.2.3p2: Rule 3. f the size of the aggregate
453 // exceeds a single eightbyte, each is classified
454 // separately. Each eightbyte gets initialized to class
455 // NO_CLASS.
456 Class &Target = Offset < 64 ? Lo : Hi;
457
458 // Merge the lo field classifcation.
459 //
460 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
461 // classified recursively so that always two fields are
462 // considered. The resulting class is calculated according to
463 // the classes of the fields in the eightbyte:
464 //
465 // (a) If both classes are equal, this is the resulting class.
466 //
467 // (b) If one of the classes is NO_CLASS, the resulting class is
468 // the other class.
469 //
470 // (c) If one of the classes is MEMORY, the result is the MEMORY
471 // class.
472 //
473 // (d) If one of the classes is INTEGER, the result is the
474 // INTEGER.
475 //
476 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
477 // MEMORY is used as class.
478 //
479 // (f) Otherwise class SSE is used.
480 if (Target == FieldLo || FieldLo == NoClass) ;
481 else if (FieldLo == Memory)
482 Lo = Memory;
483 else if (Target == NoClass)
484 Target = FieldLo;
485 else if (Target == Integer || FieldLo == Integer)
486 Target = Integer;
487 else if (FieldLo == X87 || FieldLo == X87Up || FieldLo == ComplexX87)
488 Lo = Memory;
489 else
490 Target = SSE;
491
492 // It isn't clear from the ABI spec what the role of the high
493 // classification is here, but since this should only happen
494 // when we have a struct with a two eightbyte member, we can
495 // just push the field high class into the overall high class.
496 if (FieldHi != NoClass)
497 Hi = FieldHi;
498}
499
Daniel Dunbare09a9692009-01-24 08:32:22 +0000500void X86_64ABIInfo::classify(QualType Ty,
501 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000502 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000503 Class &Lo, Class &Hi) const {
504 Lo = Memory;
505 Hi = NoClass;
506 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
507 BuiltinType::Kind k = BT->getKind();
508
Daniel Dunbar1358b202009-01-26 21:26:08 +0000509 if (k == BuiltinType::Void) {
510 Lo = NoClass;
511 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000512 Lo = Integer;
513 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
514 Lo = SSE;
515 } else if (k == BuiltinType::LongDouble) {
516 Lo = X87;
517 Hi = X87Up;
518 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000519
520 // FIXME: _Decimal32 and _Decimal64 are SSE.
521 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000522 // FIXME: __int128 is (Integer, Integer).
523 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
524 Ty->isObjCQualifiedInterfaceType()) {
525 Lo = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000526 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000527 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000528 if (Size == 64) {
529 // FIXME: For some reason, gcc appears to be treating <1 x
530 // double> as INTEGER; this seems wrong, but we will match for
531 // now (icc rejects <1 x double>, so...).
532 Lo = (VT->getElementType() == Context.DoubleTy) ? Integer : SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000533
534 // If this type crosses an eightbyte boundary, it should be
535 // split.
536 if (OffsetBase && OffsetBase != 8)
537 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000538 } else if (Size == 128) {
539 Lo = SSE;
540 Hi = SSEUp;
541 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000542 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
543 QualType ET = CT->getElementType();
544
Daniel Dunbare413f532009-01-30 18:40:10 +0000545 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000546 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000547 if (Size <= 64)
548 Lo = Integer;
549 else if (Size <= 128)
550 Lo = Hi = Integer;
551 } else if (ET == Context.FloatTy)
Daniel Dunbare09a9692009-01-24 08:32:22 +0000552 Lo = SSE;
553 else if (ET == Context.DoubleTy)
554 Lo = Hi = SSE;
555 else if (ET == Context.LongDoubleTy)
556 Lo = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000557
558 // If this complex type crosses an eightbyte boundary then it
559 // should be split.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000560 uint64_t EB_Real = (OffsetBase) >> 3;
Daniel Dunbare413f532009-01-30 18:40:10 +0000561 uint64_t EB_Imag = (OffsetBase + Size) >> 3;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000562 if (Hi == NoClass && EB_Real != EB_Imag)
563 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000564 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
565 // Arrays are treated like structures.
566
567 uint64_t Size = Context.getTypeSize(Ty);
568
569 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
570 // than two eightbytes, ..., it has class MEMORY.
571 if (Size > 128)
572 return;
573
574 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
575 // fields, it has class MEMORY.
576 //
577 // Only need to check alignment of array base.
578 if (OffsetBase % Context.getTypeAlign(AT->getElementType())) {
579 Lo = Memory;
580 return;
581 }
582
583 // Otherwise implement simplified merge. We could be smarter about
584 // this, but it isn't worth it and would be harder to verify.
585 Lo = NoClass;
586 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);
591
592 merge(Offset, FieldLo, FieldHi, Lo, Hi);
593 // Memory is never over-ridden, exit early if we see it.
594 if (Lo == Memory)
595 return;
596 }
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000597 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000598 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000599
600 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
601 // than two eightbytes, ..., it has class MEMORY.
602 if (Size > 128)
603 return;
604
605 const RecordDecl *RD = RT->getDecl();
606
607 // Assume variable sized types are passed in memory.
608 if (RD->hasFlexibleArrayMember())
609 return;
610
611 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
612
613 // Reset Lo class, this will be recomputed.
614 Lo = NoClass;
615 unsigned idx = 0;
616 for (RecordDecl::field_iterator i = RD->field_begin(),
617 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000618 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000619
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000620 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
621 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000622 if (Offset % Context.getTypeAlign(i->getType())) {
623 Lo = Memory;
624 return;
625 }
626
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000627 // Classify this field.
628 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000629 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000630
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000631 merge(Offset, FieldLo, FieldHi, Lo, Hi);
632 // Memory is never over-ridden, exit early if we see it.
633 if (Lo == Memory)
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000634 return;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000635 }
636
637 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
638 //
639 // (a) If one of the classes is MEMORY, the whole argument is
640 // passed in memory.
641 //
642 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
643
644 // The first of these conditions is guaranteed by how we implement
645 // the merge (just bail). I don't believe the second is actually
646 // possible at all.
647 assert(Lo != Memory && "Unexpected memory classification.");
648 if (Hi == SSEUp && Lo != SSE)
649 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000650 }
651}
652
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000653ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
654 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000655 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
656 // classification algorithm.
657 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000658 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000659
660 const llvm::Type *ResType = 0;
661 switch (Lo) {
662 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000663 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000664
665 case SSEUp:
666 case X87Up:
667 assert(0 && "Invalid classification for lo word.");
668
669 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
670 // hidden argument, i.e. structret.
671 case Memory:
672 return ABIArgInfo::getStructRet();
673
674 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
675 // available register of the sequence %rax, %rdx is used.
676 case Integer:
677 ResType = llvm::Type::Int64Ty; break;
678
679 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
680 // available SSE register of the sequence %xmm0, %xmm1 is used.
681 case SSE:
682 ResType = llvm::Type::DoubleTy; break;
683
684 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
685 // returned on the X87 stack in %st0 as 80-bit x87 number.
686 case X87:
687 ResType = llvm::Type::X86_FP80Ty; break;
688
689 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
690 // part of the value is returned in %st0 and the imaginary part in
691 // %st1.
692 case ComplexX87:
693 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
694 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
695 break;
696 }
697
698 switch (Hi) {
699 // Memory was handled previously, and ComplexX87 and X87 should
700 // never occur as hi classes.
701 case Memory:
702 case X87:
703 case ComplexX87:
704 assert(0 && "Invalid classification for hi word.");
705
706 case NoClass: break;
707 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000708 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
709 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000710 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000711 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
712 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000713
714 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
715 // is passed in the upper half of the last used SSE register.
716 //
717 // SSEUP should always be preceeded by SSE, just widen.
718 case SSEUp:
719 assert(Lo == SSE && "Unexpected SSEUp classification.");
720 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
721 break;
722
723 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000724 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000725 //
726 // X87UP should always be preceeded by X87, so we don't need to do
727 // anything here.
728 case X87Up:
729 assert(Lo == X87 && "Unexpected X87Up classification.");
730 break;
731 }
732
733 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000734}
735
736ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
737 ASTContext &Context) const {
738 return ABIArgInfo::getDefault();
739}
740
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000741ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
742 ASTContext &Context) const {
743 return ABIArgInfo::getDefault();
744}
745
746ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
747 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000748 return ABIArgInfo::getDefault();
749}
750
751const ABIInfo &CodeGenTypes::getABIInfo() const {
752 if (TheABIInfo)
753 return *TheABIInfo;
754
755 // For now we just cache this in the CodeGenTypes and don't bother
756 // to free it.
757 const char *TargetPrefix = getContext().Target.getTargetPrefix();
758 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000759 switch (getContext().Target.getPointerWidth(0)) {
760 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000761 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000762 case 64:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000763 if (UseX86_64ABI)
764 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000765 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000766 }
767
768 return *(TheABIInfo = new DefaultABIInfo);
769}
770
771// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
772// "default" types to StructRet when appropriate for simplicity.
773static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
774 assert(!Ty->isArrayType() &&
775 "Array types cannot be passed directly.");
776 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000777 // Ensure default on aggregate types is StructRet.
778 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
779 return ABIArgInfo::getStructRet();
780 return Info;
781}
782
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000783// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
784// "default" types to ByVal when appropriate for simplicity.
785static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
786 assert(!Ty->isArrayType() &&
787 "Array types cannot be passed directly.");
788 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000789 // Ensure default on aggregate types is ByVal.
790 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
791 return ABIArgInfo::getByVal(0);
792 return Info;
793}
794
Daniel Dunbare126ab12008-09-10 02:41:04 +0000795/***/
796
Daniel Dunbar04d35782008-09-17 00:51:38 +0000797void CodeGenTypes::GetExpandedTypes(QualType Ty,
798 std::vector<const llvm::Type*> &ArgTys) {
799 const RecordType *RT = Ty->getAsStructureType();
800 assert(RT && "Can only expand structure types.");
801 const RecordDecl *RD = RT->getDecl();
802 assert(!RD->hasFlexibleArrayMember() &&
803 "Cannot expand structure with flexible array.");
804
Douglas Gregor5d764842009-01-09 17:18:27 +0000805 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000806 e = RD->field_end(); i != e; ++i) {
807 const FieldDecl *FD = *i;
808 assert(!FD->isBitField() &&
809 "Cannot expand structure with bit-field members.");
810
811 QualType FT = FD->getType();
812 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
813 GetExpandedTypes(FT, ArgTys);
814 } else {
815 ArgTys.push_back(ConvertType(FT));
816 }
817 }
818}
819
820llvm::Function::arg_iterator
821CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
822 llvm::Function::arg_iterator AI) {
823 const RecordType *RT = Ty->getAsStructureType();
824 assert(RT && "Can only expand structure types.");
825
826 RecordDecl *RD = RT->getDecl();
827 assert(LV.isSimple() &&
828 "Unexpected non-simple lvalue during struct expansion.");
829 llvm::Value *Addr = LV.getAddress();
830 for (RecordDecl::field_iterator i = RD->field_begin(),
831 e = RD->field_end(); i != e; ++i) {
832 FieldDecl *FD = *i;
833 QualType FT = FD->getType();
834
835 // FIXME: What are the right qualifiers here?
836 LValue LV = EmitLValueForField(Addr, FD, false, 0);
837 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
838 AI = ExpandTypeFromArgs(FT, LV, AI);
839 } else {
840 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
841 ++AI;
842 }
843 }
844
845 return AI;
846}
847
848void
849CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
850 llvm::SmallVector<llvm::Value*, 16> &Args) {
851 const RecordType *RT = Ty->getAsStructureType();
852 assert(RT && "Can only expand structure types.");
853
854 RecordDecl *RD = RT->getDecl();
855 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
856 llvm::Value *Addr = RV.getAggregateAddr();
857 for (RecordDecl::field_iterator i = RD->field_begin(),
858 e = RD->field_end(); i != e; ++i) {
859 FieldDecl *FD = *i;
860 QualType FT = FD->getType();
861
862 // FIXME: What are the right qualifiers here?
863 LValue LV = EmitLValueForField(Addr, FD, false, 0);
864 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
865 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
866 } else {
867 RValue RV = EmitLoadOfLValue(LV, FT);
868 assert(RV.isScalar() &&
869 "Unexpected non-scalar rvalue during struct expansion.");
870 Args.push_back(RV.getScalarVal());
871 }
872 }
873}
874
875/***/
876
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000877const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000878CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
879 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
880}
881
882const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000883CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000884 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
885}
886
887const llvm::FunctionType *
888CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
889 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000890 std::vector<const llvm::Type*> ArgTys;
891
892 const llvm::Type *ResultType = 0;
893
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000894 QualType RetTy = *begin;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000895 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000896 switch (RetAI.getKind()) {
897 case ABIArgInfo::ByVal:
898 case ABIArgInfo::Expand:
899 assert(0 && "Invalid ABI kind for return argument");
900
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000901 case ABIArgInfo::Default:
902 if (RetTy->isVoidType()) {
903 ResultType = llvm::Type::VoidTy;
904 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000905 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000906 }
907 break;
908
909 case ABIArgInfo::StructRet: {
910 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000911 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000912 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
913 break;
914 }
915
Daniel Dunbar1358b202009-01-26 21:26:08 +0000916 case ABIArgInfo::Ignore:
917 ResultType = llvm::Type::VoidTy;
918 break;
919
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000920 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000921 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000922 break;
923 }
924
925 for (++begin; begin != end; ++begin) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000926 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbara9976a22008-09-10 07:00:50 +0000927 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000928
929 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +0000930 case ABIArgInfo::Ignore:
931 break;
932
Daniel Dunbar04d35782008-09-17 00:51:38 +0000933 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000934 case ABIArgInfo::StructRet:
935 assert(0 && "Invalid ABI kind for non-return argument");
936
937 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000938 // byval arguments are always on the stack, which is addr space #0.
939 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000940 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
941 break;
942
943 case ABIArgInfo::Default:
944 ArgTys.push_back(Ty);
945 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000946
947 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000948 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000949 break;
950 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000951 }
952
Daniel Dunbara9976a22008-09-10 07:00:50 +0000953 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000954}
955
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000956bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000957 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000958}
959
Devang Patela85a9ef2008-09-25 21:02:23 +0000960void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000961 ArgTypeIterator begin,
962 ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000963 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000964 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000965 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000966
967 if (TargetDecl) {
968 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000969 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000970 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000971 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000972 if (TargetDecl->getAttr<PureAttr>())
973 FuncAttrs |= llvm::Attribute::ReadOnly;
974 if (TargetDecl->getAttr<ConstAttr>())
975 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000976 }
977
Daniel Dunbare126ab12008-09-10 02:41:04 +0000978 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000979 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000980 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000981 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000982 case ABIArgInfo::Default:
983 if (RetTy->isPromotableIntegerType()) {
984 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000985 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000986 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000987 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000988 }
989 }
990 break;
991
992 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000993 PAL.push_back(llvm::AttributeWithIndex::get(Index,
994 llvm::Attribute::StructRet|
995 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000996 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000997 break;
998
Daniel Dunbar1358b202009-01-26 21:26:08 +0000999 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001000 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001001 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001002
1003 case ABIArgInfo::ByVal:
1004 case ABIArgInfo::Expand:
1005 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001006 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001007
Devang Patel2bb6eb82008-09-26 22:53:57 +00001008 if (RetAttrs)
1009 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001010 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001011 QualType ParamType = *begin;
Devang Patela85a9ef2008-09-25 21:02:23 +00001012 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001013 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001014
1015 switch (AI.getKind()) {
1016 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001017 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001018 assert(0 && "Invalid ABI kind for non-return argument");
1019
1020 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +00001021 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001022 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1023 break;
1024
1025 case ABIArgInfo::Default:
1026 if (ParamType->isPromotableIntegerType()) {
1027 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001028 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001029 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001030 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001031 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001032 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001033 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001034
Daniel Dunbar1358b202009-01-26 21:26:08 +00001035 case ABIArgInfo::Ignore:
1036 // Skip increment, no matching LLVM parameter.
1037 continue;
1038
Daniel Dunbar04d35782008-09-17 00:51:38 +00001039 case ABIArgInfo::Expand: {
1040 std::vector<const llvm::Type*> Tys;
1041 // FIXME: This is rather inefficient. Do we ever actually need
1042 // to do anything here? The result should be just reconstructed
1043 // on the other side, so extension should be a non-issue.
1044 getTypes().GetExpandedTypes(ParamType, Tys);
1045 Index += Tys.size();
1046 continue;
1047 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001048 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001049
Devang Patela85a9ef2008-09-25 21:02:23 +00001050 if (Attributes)
1051 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001052 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001053 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001054 if (FuncAttrs)
1055 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1056
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001057}
1058
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001059void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
1060 QualType RetTy,
1061 const FunctionArgList &Args) {
1062 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1063 llvm::Function::arg_iterator AI = Fn->arg_begin();
1064
1065 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001066 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001067 AI->setName("agg.result");
1068 ++AI;
1069 }
1070
1071 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001072 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001073 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001074 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001075 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001076
1077 switch (ArgI.getKind()) {
1078 case ABIArgInfo::ByVal:
1079 case ABIArgInfo::Default: {
1080 assert(AI != Fn->arg_end() && "Argument mismatch!");
1081 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001082 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001083 // This must be a promotion, for something like
1084 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +00001085 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001086 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001087 EmitParmDecl(*Arg, V);
1088 break;
1089 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001090
1091 case ABIArgInfo::Expand: {
1092 // If this was structure was expand into multiple arguments then
1093 // we need to create a temporary and reconstruct it from the
1094 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001095 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001096 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1097 (Name + ".addr").c_str());
1098 // FIXME: What are the right qualifiers here?
1099 llvm::Function::arg_iterator End =
1100 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1101 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001102
Daniel Dunbar04d35782008-09-17 00:51:38 +00001103 // Name the arguments used in expansion and increment AI.
1104 unsigned Index = 0;
1105 for (; AI != End; ++AI, ++Index)
1106 AI->setName(Name + "." + llvm::utostr(Index));
1107 continue;
1108 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001109
1110 case ABIArgInfo::Ignore:
1111 break;
1112
Daniel Dunbar22e30052008-09-11 01:48:57 +00001113 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001114 case ABIArgInfo::StructRet:
1115 assert(0 && "Invalid ABI kind for non-return argument");
1116 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001117
1118 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001119 }
1120 assert(AI == Fn->arg_end() && "Argument mismatch!");
1121}
1122
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001123/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1124/// a pointer to an object of type \arg Ty.
1125///
1126/// This safely handles the case when the src type is smaller than the
1127/// destination type; in this situation the values of bits which not
1128/// present in the src are undefined.
1129static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1130 const llvm::Type *Ty,
1131 CodeGenFunction &CGF) {
1132 const llvm::Type *SrcTy =
1133 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Daniel Dunbar1aa2be92009-01-30 00:47:38 +00001134 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1135 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001136
1137 // If load is legal, just bitcase the src pointer.
1138 if (SrcSize == DstSize) {
1139 llvm::Value *Casted =
1140 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
1141 return CGF.Builder.CreateLoad(Casted);
1142 } else {
1143 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1144
1145 // Otherwise do coercion through memory. This is stupid, but
1146 // simple.
1147 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1148 llvm::Value *Casted =
1149 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
1150 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1151 return CGF.Builder.CreateLoad(Tmp);
1152 }
1153}
1154
1155/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1156/// where the source and destination may have different types.
1157///
1158/// This safely handles the case when the src type is larger than the
1159/// destination type; the upper bits of the src will be lost.
1160static void CreateCoercedStore(llvm::Value *Src,
1161 llvm::Value *DstPtr,
1162 CodeGenFunction &CGF) {
1163 const llvm::Type *SrcTy = Src->getType();
1164 const llvm::Type *DstTy =
1165 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1166
Daniel Dunbar1aa2be92009-01-30 00:47:38 +00001167 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1168 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001169
1170 // If store is legal, just bitcase the src pointer.
1171 if (SrcSize == DstSize) {
1172 llvm::Value *Casted =
1173 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1174 CGF.Builder.CreateStore(Src, Casted);
1175 } else {
1176 assert(SrcSize > DstSize && "Coercion is missing bits!");
1177
1178 // Otherwise do coercion through memory. This is stupid, but
1179 // simple.
1180 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1181 CGF.Builder.CreateStore(Src, Tmp);
1182 llvm::Value *Casted =
1183 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1184 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1185 }
1186}
1187
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001188void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1189 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001190 llvm::Value *RV = 0;
1191
1192 // Functions with no result always return void.
1193 if (ReturnValue) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001194 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001195
1196 switch (RetAI.getKind()) {
1197 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001198 if (RetTy->isAnyComplexType()) {
1199 // FIXME: Volatile
1200 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1201 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1202 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1203 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1204 } else {
1205 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1206 CurFn->arg_begin());
1207 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001208 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001209
Daniel Dunbare126ab12008-09-10 02:41:04 +00001210 case ABIArgInfo::Default:
1211 RV = Builder.CreateLoad(ReturnValue);
1212 break;
1213
Daniel Dunbar1358b202009-01-26 21:26:08 +00001214 case ABIArgInfo::Ignore:
1215 break;
1216
Daniel Dunbar73d66602008-09-10 07:04:09 +00001217 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001218 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001219 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001220 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001221
1222 case ABIArgInfo::ByVal:
1223 case ABIArgInfo::Expand:
1224 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001225 }
1226 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001227
1228 if (RV) {
1229 Builder.CreateRet(RV);
1230 } else {
1231 Builder.CreateRetVoid();
1232 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001233}
1234
1235RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +00001236 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001237 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001238 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001239
1240 // Handle struct-return functions by passing a pointer to the
1241 // location that we would like to return into.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001242 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001243 switch (RetAI.getKind()) {
1244 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001245 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001246 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001247 break;
1248
1249 case ABIArgInfo::Default:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001250 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001251 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001252 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001253
1254 case ABIArgInfo::ByVal:
1255 case ABIArgInfo::Expand:
1256 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001257 }
1258
1259 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1260 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001261 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001262 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001263
1264 switch (ArgInfo.getKind()) {
1265 case ABIArgInfo::ByVal: // Default is byval
1266 case ABIArgInfo::Default:
1267 if (RV.isScalar()) {
1268 Args.push_back(RV.getScalarVal());
1269 } else if (RV.isComplex()) {
1270 // Make a temporary alloca to pass the argument.
1271 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1272 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1273 } else {
1274 Args.push_back(RV.getAggregateAddr());
1275 }
1276 break;
1277
Daniel Dunbar1358b202009-01-26 21:26:08 +00001278 case ABIArgInfo::Ignore:
1279 break;
1280
Daniel Dunbar04d35782008-09-17 00:51:38 +00001281 case ABIArgInfo::StructRet:
1282 case ABIArgInfo::Coerce:
1283 assert(0 && "Invalid ABI kind for non-return argument");
1284 break;
1285
1286 case ABIArgInfo::Expand:
1287 ExpandTypeToArgs(I->second, RV, Args);
1288 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001289 }
1290 }
1291
1292 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001293 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001294
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001295 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001296 CodeGen::AttributeListType AttributeList;
1297 CGM.ConstructAttributeList(0,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +00001298 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patela85a9ef2008-09-25 21:02:23 +00001299 AttributeList);
1300 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1301 AttributeList.size()));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001302
1303 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1304 CI->setCallingConv(F->getCallingConv());
1305 if (CI->getType() != llvm::Type::VoidTy)
1306 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001307
1308 switch (RetAI.getKind()) {
1309 case ABIArgInfo::StructRet:
1310 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001311 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001312 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001313 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001314 else
1315 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001316
Daniel Dunbare126ab12008-09-10 02:41:04 +00001317 case ABIArgInfo::Default:
1318 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1319
Daniel Dunbar1358b202009-01-26 21:26:08 +00001320 case ABIArgInfo::Ignore:
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001321 if (RetTy->isVoidType())
1322 return RValue::get(0);
1323 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1324 llvm::Value *Res =
1325 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1326 return RValue::getAggregate(Res);
1327 }
1328 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001329
Daniel Dunbar73d66602008-09-10 07:04:09 +00001330 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001331 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1332 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001333 if (RetTy->isAnyComplexType())
1334 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001335 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001336 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001337 else
1338 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001339 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001340
1341 case ABIArgInfo::ByVal:
1342 case ABIArgInfo::Expand:
1343 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001344 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001345
1346 assert(0 && "Unhandled ABIArgInfo::Kind");
1347 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001348}