blob: b8f0ae868f06a4fd8ece377e1fee9a0efe26dc89 [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;
533 } else if (Size == 128) {
534 Lo = SSE;
535 Hi = SSEUp;
536 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000537 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
538 QualType ET = CT->getElementType();
539
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000540 if (ET->isIntegerType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000541 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000542 if (Size <= 64)
543 Lo = Integer;
544 else if (Size <= 128)
545 Lo = Hi = Integer;
546 } else if (ET == Context.FloatTy)
Daniel Dunbare09a9692009-01-24 08:32:22 +0000547 Lo = SSE;
548 else if (ET == Context.DoubleTy)
549 Lo = Hi = SSE;
550 else if (ET == Context.LongDoubleTy)
551 Lo = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000552
553 // If this complex type crosses an eightbyte boundary then it
554 // should be split.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000555 uint64_t EB_Real = (OffsetBase) >> 3;
556 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) >> 3;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000557 if (Hi == NoClass && EB_Real != EB_Imag)
558 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000559 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
560 // Arrays are treated like structures.
561
562 uint64_t Size = Context.getTypeSize(Ty);
563
564 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
565 // than two eightbytes, ..., it has class MEMORY.
566 if (Size > 128)
567 return;
568
569 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
570 // fields, it has class MEMORY.
571 //
572 // Only need to check alignment of array base.
573 if (OffsetBase % Context.getTypeAlign(AT->getElementType())) {
574 Lo = Memory;
575 return;
576 }
577
578 // Otherwise implement simplified merge. We could be smarter about
579 // this, but it isn't worth it and would be harder to verify.
580 Lo = NoClass;
581 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
582 uint64_t ArraySize = AT->getSize().getZExtValue();
583 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
584 Class FieldLo, FieldHi;
585 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
586
587 merge(Offset, FieldLo, FieldHi, Lo, Hi);
588 // Memory is never over-ridden, exit early if we see it.
589 if (Lo == Memory)
590 return;
591 }
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000592 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000593 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000594
595 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
596 // than two eightbytes, ..., it has class MEMORY.
597 if (Size > 128)
598 return;
599
600 const RecordDecl *RD = RT->getDecl();
601
602 // Assume variable sized types are passed in memory.
603 if (RD->hasFlexibleArrayMember())
604 return;
605
606 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
607
608 // Reset Lo class, this will be recomputed.
609 Lo = NoClass;
610 unsigned idx = 0;
611 for (RecordDecl::field_iterator i = RD->field_begin(),
612 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000613 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000614
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000615 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
616 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000617 if (Offset % Context.getTypeAlign(i->getType())) {
618 Lo = Memory;
619 return;
620 }
621
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000622 // Classify this field.
623 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000624 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000625
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000626 merge(Offset, FieldLo, FieldHi, Lo, Hi);
627 // Memory is never over-ridden, exit early if we see it.
628 if (Lo == Memory)
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000629 return;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000630 }
631
632 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
633 //
634 // (a) If one of the classes is MEMORY, the whole argument is
635 // passed in memory.
636 //
637 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
638
639 // The first of these conditions is guaranteed by how we implement
640 // the merge (just bail). I don't believe the second is actually
641 // possible at all.
642 assert(Lo != Memory && "Unexpected memory classification.");
643 if (Hi == SSEUp && Lo != SSE)
644 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000645 }
646}
647
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000648ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
649 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000650 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
651 // classification algorithm.
652 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000653 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000654
655 const llvm::Type *ResType = 0;
656 switch (Lo) {
657 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000658 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000659
660 case SSEUp:
661 case X87Up:
662 assert(0 && "Invalid classification for lo word.");
663
664 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
665 // hidden argument, i.e. structret.
666 case Memory:
667 return ABIArgInfo::getStructRet();
668
669 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
670 // available register of the sequence %rax, %rdx is used.
671 case Integer:
672 ResType = llvm::Type::Int64Ty; break;
673
674 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
675 // available SSE register of the sequence %xmm0, %xmm1 is used.
676 case SSE:
677 ResType = llvm::Type::DoubleTy; break;
678
679 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
680 // returned on the X87 stack in %st0 as 80-bit x87 number.
681 case X87:
682 ResType = llvm::Type::X86_FP80Ty; break;
683
684 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
685 // part of the value is returned in %st0 and the imaginary part in
686 // %st1.
687 case ComplexX87:
688 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
689 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
690 break;
691 }
692
693 switch (Hi) {
694 // Memory was handled previously, and ComplexX87 and X87 should
695 // never occur as hi classes.
696 case Memory:
697 case X87:
698 case ComplexX87:
699 assert(0 && "Invalid classification for hi word.");
700
701 case NoClass: break;
702 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000703 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
704 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000705 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000706 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
707 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000708
709 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
710 // is passed in the upper half of the last used SSE register.
711 //
712 // SSEUP should always be preceeded by SSE, just widen.
713 case SSEUp:
714 assert(Lo == SSE && "Unexpected SSEUp classification.");
715 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
716 break;
717
718 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000719 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000720 //
721 // X87UP should always be preceeded by X87, so we don't need to do
722 // anything here.
723 case X87Up:
724 assert(Lo == X87 && "Unexpected X87Up classification.");
725 break;
726 }
727
728 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000729}
730
731ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
732 ASTContext &Context) const {
733 return ABIArgInfo::getDefault();
734}
735
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000736ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
737 ASTContext &Context) const {
738 return ABIArgInfo::getDefault();
739}
740
741ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
742 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000743 return ABIArgInfo::getDefault();
744}
745
746const ABIInfo &CodeGenTypes::getABIInfo() const {
747 if (TheABIInfo)
748 return *TheABIInfo;
749
750 // For now we just cache this in the CodeGenTypes and don't bother
751 // to free it.
752 const char *TargetPrefix = getContext().Target.getTargetPrefix();
753 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000754 switch (getContext().Target.getPointerWidth(0)) {
755 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000756 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000757 case 64:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000758 if (UseX86_64ABI)
759 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000760 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000761 }
762
763 return *(TheABIInfo = new DefaultABIInfo);
764}
765
766// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
767// "default" types to StructRet when appropriate for simplicity.
768static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
769 assert(!Ty->isArrayType() &&
770 "Array types cannot be passed directly.");
771 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000772 // Ensure default on aggregate types is StructRet.
773 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
774 return ABIArgInfo::getStructRet();
775 return Info;
776}
777
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000778// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
779// "default" types to ByVal when appropriate for simplicity.
780static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
781 assert(!Ty->isArrayType() &&
782 "Array types cannot be passed directly.");
783 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000784 // Ensure default on aggregate types is ByVal.
785 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
786 return ABIArgInfo::getByVal(0);
787 return Info;
788}
789
Daniel Dunbare126ab12008-09-10 02:41:04 +0000790/***/
791
Daniel Dunbar04d35782008-09-17 00:51:38 +0000792void CodeGenTypes::GetExpandedTypes(QualType Ty,
793 std::vector<const llvm::Type*> &ArgTys) {
794 const RecordType *RT = Ty->getAsStructureType();
795 assert(RT && "Can only expand structure types.");
796 const RecordDecl *RD = RT->getDecl();
797 assert(!RD->hasFlexibleArrayMember() &&
798 "Cannot expand structure with flexible array.");
799
Douglas Gregor5d764842009-01-09 17:18:27 +0000800 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000801 e = RD->field_end(); i != e; ++i) {
802 const FieldDecl *FD = *i;
803 assert(!FD->isBitField() &&
804 "Cannot expand structure with bit-field members.");
805
806 QualType FT = FD->getType();
807 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
808 GetExpandedTypes(FT, ArgTys);
809 } else {
810 ArgTys.push_back(ConvertType(FT));
811 }
812 }
813}
814
815llvm::Function::arg_iterator
816CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
817 llvm::Function::arg_iterator AI) {
818 const RecordType *RT = Ty->getAsStructureType();
819 assert(RT && "Can only expand structure types.");
820
821 RecordDecl *RD = RT->getDecl();
822 assert(LV.isSimple() &&
823 "Unexpected non-simple lvalue during struct expansion.");
824 llvm::Value *Addr = LV.getAddress();
825 for (RecordDecl::field_iterator i = RD->field_begin(),
826 e = RD->field_end(); i != e; ++i) {
827 FieldDecl *FD = *i;
828 QualType FT = FD->getType();
829
830 // FIXME: What are the right qualifiers here?
831 LValue LV = EmitLValueForField(Addr, FD, false, 0);
832 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
833 AI = ExpandTypeFromArgs(FT, LV, AI);
834 } else {
835 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
836 ++AI;
837 }
838 }
839
840 return AI;
841}
842
843void
844CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
845 llvm::SmallVector<llvm::Value*, 16> &Args) {
846 const RecordType *RT = Ty->getAsStructureType();
847 assert(RT && "Can only expand structure types.");
848
849 RecordDecl *RD = RT->getDecl();
850 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
851 llvm::Value *Addr = RV.getAggregateAddr();
852 for (RecordDecl::field_iterator i = RD->field_begin(),
853 e = RD->field_end(); i != e; ++i) {
854 FieldDecl *FD = *i;
855 QualType FT = FD->getType();
856
857 // FIXME: What are the right qualifiers here?
858 LValue LV = EmitLValueForField(Addr, FD, false, 0);
859 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
860 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
861 } else {
862 RValue RV = EmitLoadOfLValue(LV, FT);
863 assert(RV.isScalar() &&
864 "Unexpected non-scalar rvalue during struct expansion.");
865 Args.push_back(RV.getScalarVal());
866 }
867 }
868}
869
870/***/
871
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000872const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000873CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
874 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
875}
876
877const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000878CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000879 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
880}
881
882const llvm::FunctionType *
883CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
884 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000885 std::vector<const llvm::Type*> ArgTys;
886
887 const llvm::Type *ResultType = 0;
888
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000889 QualType RetTy = *begin;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000890 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000891 switch (RetAI.getKind()) {
892 case ABIArgInfo::ByVal:
893 case ABIArgInfo::Expand:
894 assert(0 && "Invalid ABI kind for return argument");
895
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000896 case ABIArgInfo::Default:
897 if (RetTy->isVoidType()) {
898 ResultType = llvm::Type::VoidTy;
899 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000900 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000901 }
902 break;
903
904 case ABIArgInfo::StructRet: {
905 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000906 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000907 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
908 break;
909 }
910
Daniel Dunbar1358b202009-01-26 21:26:08 +0000911 case ABIArgInfo::Ignore:
912 ResultType = llvm::Type::VoidTy;
913 break;
914
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000915 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000916 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000917 break;
918 }
919
920 for (++begin; begin != end; ++begin) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000921 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbara9976a22008-09-10 07:00:50 +0000922 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000923
924 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +0000925 case ABIArgInfo::Ignore:
926 break;
927
Daniel Dunbar04d35782008-09-17 00:51:38 +0000928 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000929 case ABIArgInfo::StructRet:
930 assert(0 && "Invalid ABI kind for non-return argument");
931
932 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000933 // byval arguments are always on the stack, which is addr space #0.
934 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000935 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
936 break;
937
938 case ABIArgInfo::Default:
939 ArgTys.push_back(Ty);
940 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000941
942 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000943 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000944 break;
945 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000946 }
947
Daniel Dunbara9976a22008-09-10 07:00:50 +0000948 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000949}
950
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000951bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000952 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000953}
954
Devang Patela85a9ef2008-09-25 21:02:23 +0000955void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000956 ArgTypeIterator begin,
957 ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000958 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000959 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000960 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000961
962 if (TargetDecl) {
963 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000964 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000965 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000966 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000967 if (TargetDecl->getAttr<PureAttr>())
968 FuncAttrs |= llvm::Attribute::ReadOnly;
969 if (TargetDecl->getAttr<ConstAttr>())
970 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000971 }
972
Daniel Dunbare126ab12008-09-10 02:41:04 +0000973 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000974 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000975 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000976 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000977 case ABIArgInfo::Default:
978 if (RetTy->isPromotableIntegerType()) {
979 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000980 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000981 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000982 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000983 }
984 }
985 break;
986
987 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000988 PAL.push_back(llvm::AttributeWithIndex::get(Index,
989 llvm::Attribute::StructRet|
990 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000991 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000992 break;
993
Daniel Dunbar1358b202009-01-26 21:26:08 +0000994 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000995 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000996 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000997
998 case ABIArgInfo::ByVal:
999 case ABIArgInfo::Expand:
1000 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001001 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001002
Devang Patel2bb6eb82008-09-26 22:53:57 +00001003 if (RetAttrs)
1004 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001005 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001006 QualType ParamType = *begin;
Devang Patela85a9ef2008-09-25 21:02:23 +00001007 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001008 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001009
1010 switch (AI.getKind()) {
1011 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001012 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001013 assert(0 && "Invalid ABI kind for non-return argument");
1014
1015 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +00001016 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001017 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1018 break;
1019
1020 case ABIArgInfo::Default:
1021 if (ParamType->isPromotableIntegerType()) {
1022 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001023 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001024 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001025 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001026 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001027 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001028 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001029
Daniel Dunbar1358b202009-01-26 21:26:08 +00001030 case ABIArgInfo::Ignore:
1031 // Skip increment, no matching LLVM parameter.
1032 continue;
1033
Daniel Dunbar04d35782008-09-17 00:51:38 +00001034 case ABIArgInfo::Expand: {
1035 std::vector<const llvm::Type*> Tys;
1036 // FIXME: This is rather inefficient. Do we ever actually need
1037 // to do anything here? The result should be just reconstructed
1038 // on the other side, so extension should be a non-issue.
1039 getTypes().GetExpandedTypes(ParamType, Tys);
1040 Index += Tys.size();
1041 continue;
1042 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001043 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001044
Devang Patela85a9ef2008-09-25 21:02:23 +00001045 if (Attributes)
1046 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001047 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001048 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001049 if (FuncAttrs)
1050 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1051
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001052}
1053
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001054void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
1055 QualType RetTy,
1056 const FunctionArgList &Args) {
1057 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1058 llvm::Function::arg_iterator AI = Fn->arg_begin();
1059
1060 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001061 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001062 AI->setName("agg.result");
1063 ++AI;
1064 }
1065
1066 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001067 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001068 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001069 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001070 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001071
1072 switch (ArgI.getKind()) {
1073 case ABIArgInfo::ByVal:
1074 case ABIArgInfo::Default: {
1075 assert(AI != Fn->arg_end() && "Argument mismatch!");
1076 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001077 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001078 // This must be a promotion, for something like
1079 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +00001080 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001081 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001082 EmitParmDecl(*Arg, V);
1083 break;
1084 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001085
1086 case ABIArgInfo::Expand: {
1087 // If this was structure was expand into multiple arguments then
1088 // we need to create a temporary and reconstruct it from the
1089 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001090 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001091 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1092 (Name + ".addr").c_str());
1093 // FIXME: What are the right qualifiers here?
1094 llvm::Function::arg_iterator End =
1095 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1096 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001097
Daniel Dunbar04d35782008-09-17 00:51:38 +00001098 // Name the arguments used in expansion and increment AI.
1099 unsigned Index = 0;
1100 for (; AI != End; ++AI, ++Index)
1101 AI->setName(Name + "." + llvm::utostr(Index));
1102 continue;
1103 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001104
1105 case ABIArgInfo::Ignore:
1106 break;
1107
Daniel Dunbar22e30052008-09-11 01:48:57 +00001108 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001109 case ABIArgInfo::StructRet:
1110 assert(0 && "Invalid ABI kind for non-return argument");
1111 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001112
1113 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001114 }
1115 assert(AI == Fn->arg_end() && "Argument mismatch!");
1116}
1117
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001118/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1119/// a pointer to an object of type \arg Ty.
1120///
1121/// This safely handles the case when the src type is smaller than the
1122/// destination type; in this situation the values of bits which not
1123/// present in the src are undefined.
1124static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1125 const llvm::Type *Ty,
1126 CodeGenFunction &CGF) {
1127 const llvm::Type *SrcTy =
1128 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Daniel Dunbar1aa2be92009-01-30 00:47:38 +00001129 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1130 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001131
1132 // If load is legal, just bitcase the src pointer.
1133 if (SrcSize == DstSize) {
1134 llvm::Value *Casted =
1135 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
1136 return CGF.Builder.CreateLoad(Casted);
1137 } else {
1138 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1139
1140 // Otherwise do coercion through memory. This is stupid, but
1141 // simple.
1142 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1143 llvm::Value *Casted =
1144 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
1145 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1146 return CGF.Builder.CreateLoad(Tmp);
1147 }
1148}
1149
1150/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1151/// where the source and destination may have different types.
1152///
1153/// This safely handles the case when the src type is larger than the
1154/// destination type; the upper bits of the src will be lost.
1155static void CreateCoercedStore(llvm::Value *Src,
1156 llvm::Value *DstPtr,
1157 CodeGenFunction &CGF) {
1158 const llvm::Type *SrcTy = Src->getType();
1159 const llvm::Type *DstTy =
1160 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1161
Daniel Dunbar1aa2be92009-01-30 00:47:38 +00001162 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1163 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001164
1165 // If store is legal, just bitcase the src pointer.
1166 if (SrcSize == DstSize) {
1167 llvm::Value *Casted =
1168 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1169 CGF.Builder.CreateStore(Src, Casted);
1170 } else {
1171 assert(SrcSize > DstSize && "Coercion is missing bits!");
1172
1173 // Otherwise do coercion through memory. This is stupid, but
1174 // simple.
1175 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1176 CGF.Builder.CreateStore(Src, Tmp);
1177 llvm::Value *Casted =
1178 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1179 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1180 }
1181}
1182
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001183void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1184 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001185 llvm::Value *RV = 0;
1186
1187 // Functions with no result always return void.
1188 if (ReturnValue) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001189 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001190
1191 switch (RetAI.getKind()) {
1192 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001193 if (RetTy->isAnyComplexType()) {
1194 // FIXME: Volatile
1195 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1196 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1197 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1198 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1199 } else {
1200 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1201 CurFn->arg_begin());
1202 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001203 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001204
Daniel Dunbare126ab12008-09-10 02:41:04 +00001205 case ABIArgInfo::Default:
1206 RV = Builder.CreateLoad(ReturnValue);
1207 break;
1208
Daniel Dunbar1358b202009-01-26 21:26:08 +00001209 case ABIArgInfo::Ignore:
1210 break;
1211
Daniel Dunbar73d66602008-09-10 07:04:09 +00001212 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001213 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001214 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001215 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001216
1217 case ABIArgInfo::ByVal:
1218 case ABIArgInfo::Expand:
1219 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001220 }
1221 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001222
1223 if (RV) {
1224 Builder.CreateRet(RV);
1225 } else {
1226 Builder.CreateRetVoid();
1227 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001228}
1229
1230RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +00001231 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001232 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001233 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001234
1235 // Handle struct-return functions by passing a pointer to the
1236 // location that we would like to return into.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001237 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001238 switch (RetAI.getKind()) {
1239 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001240 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001241 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001242 break;
1243
1244 case ABIArgInfo::Default:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001245 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001246 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001247 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001248
1249 case ABIArgInfo::ByVal:
1250 case ABIArgInfo::Expand:
1251 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001252 }
1253
1254 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1255 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001256 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001257 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001258
1259 switch (ArgInfo.getKind()) {
1260 case ABIArgInfo::ByVal: // Default is byval
1261 case ABIArgInfo::Default:
1262 if (RV.isScalar()) {
1263 Args.push_back(RV.getScalarVal());
1264 } else if (RV.isComplex()) {
1265 // Make a temporary alloca to pass the argument.
1266 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1267 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1268 } else {
1269 Args.push_back(RV.getAggregateAddr());
1270 }
1271 break;
1272
Daniel Dunbar1358b202009-01-26 21:26:08 +00001273 case ABIArgInfo::Ignore:
1274 break;
1275
Daniel Dunbar04d35782008-09-17 00:51:38 +00001276 case ABIArgInfo::StructRet:
1277 case ABIArgInfo::Coerce:
1278 assert(0 && "Invalid ABI kind for non-return argument");
1279 break;
1280
1281 case ABIArgInfo::Expand:
1282 ExpandTypeToArgs(I->second, RV, Args);
1283 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001284 }
1285 }
1286
1287 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001288 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001289
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001290 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001291 CodeGen::AttributeListType AttributeList;
1292 CGM.ConstructAttributeList(0,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +00001293 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patela85a9ef2008-09-25 21:02:23 +00001294 AttributeList);
1295 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1296 AttributeList.size()));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001297
1298 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1299 CI->setCallingConv(F->getCallingConv());
1300 if (CI->getType() != llvm::Type::VoidTy)
1301 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001302
1303 switch (RetAI.getKind()) {
1304 case ABIArgInfo::StructRet:
1305 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001306 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001307 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001308 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001309 else
1310 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001311
Daniel Dunbare126ab12008-09-10 02:41:04 +00001312 case ABIArgInfo::Default:
1313 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1314
Daniel Dunbar1358b202009-01-26 21:26:08 +00001315 case ABIArgInfo::Ignore:
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001316 if (RetTy->isVoidType())
1317 return RValue::get(0);
1318 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1319 llvm::Value *Res =
1320 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1321 return RValue::getAggregate(Res);
1322 }
1323 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001324
Daniel Dunbar73d66602008-09-10 07:04:09 +00001325 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001326 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1327 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001328 if (RetTy->isAnyComplexType())
1329 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001330 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001331 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001332 else
1333 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001334 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001335
1336 case ABIArgInfo::ByVal:
1337 case ABIArgInfo::Expand:
1338 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001339 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001340
1341 assert(0 && "Unhandled ABIArgInfo::Kind");
1342 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001343}