blob: 666d427a254f6776efa66288ac1481656ba8dc45 [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
30/***/
31
Daniel Dunbara8f02052008-09-08 21:33:45 +000032// FIXME: Use iterator and sidestep silly type array creation.
33
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000034CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
35 : IsVariadic(true)
36{
37 ArgTypes.push_back(FTNP->getResultType());
38}
39
40CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
41 : IsVariadic(FTP->isVariadic())
42{
43 ArgTypes.push_back(FTP->getResultType());
44 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
45 ArgTypes.push_back(FTP->getArgType(i));
46}
47
48// FIXME: Is there really any reason to have this still?
Daniel Dunbara8f02052008-09-08 21:33:45 +000049CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbara8f02052008-09-08 21:33:45 +000050{
51 const FunctionType *FTy = FD->getType()->getAsFunctionType();
52 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000053
Daniel Dunbara8f02052008-09-08 21:33:45 +000054 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000055 if (FTP) {
56 IsVariadic = FTP->isVariadic();
Daniel Dunbara8f02052008-09-08 21:33:45 +000057 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
58 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000059 } else {
60 IsVariadic = true;
61 }
Daniel Dunbara8f02052008-09-08 21:33:45 +000062}
63
64CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
65 const ASTContext &Context)
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000066 : IsVariadic(MD->isVariadic())
Daniel Dunbara8f02052008-09-08 21:33:45 +000067{
68 ArgTypes.push_back(MD->getResultType());
69 ArgTypes.push_back(MD->getSelfDecl()->getType());
70 ArgTypes.push_back(Context.getObjCSelType());
71 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
72 e = MD->param_end(); i != e; ++i)
73 ArgTypes.push_back((*i)->getType());
74}
75
Daniel Dunbarbccb0682008-09-10 00:32:18 +000076ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
77 return ArgTypes.begin();
78}
79
80ArgTypeIterator CGFunctionInfo::argtypes_end() const {
81 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000082}
83
84/***/
85
Daniel Dunbarbccb0682008-09-10 00:32:18 +000086CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
87 ArgTypes.push_back(_ResultType);
88 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbara8f02052008-09-08 21:33:45 +000089 ArgTypes.push_back(i->second);
90}
91
Daniel Dunbarbccb0682008-09-10 00:32:18 +000092ArgTypeIterator CGCallInfo::argtypes_begin() const {
93 return ArgTypes.begin();
94}
95
96ArgTypeIterator CGCallInfo::argtypes_end() const {
97 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000098}
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +000099
100/***/
101
Daniel Dunbar22e30052008-09-11 01:48:57 +0000102/// ABIArgInfo - Helper class to encapsulate information about how a
103/// specific C type should be passed to or returned from a function.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000104class ABIArgInfo {
105public:
106 enum Kind {
107 Default,
Daniel Dunbar17d35372008-12-18 04:52:14 +0000108 StructRet, /// Only valid for return values. The return value
109 /// should be passed through a pointer to a caller
110 /// allocated location passed as an implicit first
111 /// argument to the function.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000112
Daniel Dunbar1358b202009-01-26 21:26:08 +0000113 Ignore, /// Ignore the argument (treat as void). Useful for
114 /// void and empty structs.
115
Daniel Dunbar04d35782008-09-17 00:51:38 +0000116 Coerce, /// Only valid for aggregate return types, the argument
117 /// should be accessed by coercion to a provided type.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000118
119 ByVal, /// Only valid for aggregate argument types. The
120 /// structure should be passed "byval" with the
121 /// specified alignment (0 indicates default
122 /// alignment).
123
124 Expand, /// Only valid for aggregate argument types. The
125 /// structure should be expanded into consecutive
Daniel Dunbar04d35782008-09-17 00:51:38 +0000126 /// arguments for its constituent fields. Currently
127 /// expand is only allowed on structures whose fields
128 /// are all scalar types or are themselves expandable
129 /// types.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000130
131 KindFirst=Default, KindLast=Expand
Daniel Dunbare126ab12008-09-10 02:41:04 +0000132 };
133
134private:
135 Kind TheKind;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000136 const llvm::Type *TypeData;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000137 unsigned UIntData;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000138
Daniel Dunbar22e30052008-09-11 01:48:57 +0000139 ABIArgInfo(Kind K, const llvm::Type *TD=0,
140 unsigned UI=0) : TheKind(K),
141 TypeData(TD),
142 UIntData(0) {}
Daniel Dunbare126ab12008-09-10 02:41:04 +0000143public:
144 static ABIArgInfo getDefault() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000145 return ABIArgInfo(Default);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000146 }
147 static ABIArgInfo getStructRet() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000148 return ABIArgInfo(StructRet);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000149 }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000150 static ABIArgInfo getIgnore() {
151 return ABIArgInfo(Ignore);
152 }
Daniel Dunbar73d66602008-09-10 07:04:09 +0000153 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000154 return ABIArgInfo(Coerce, T);
155 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000156 static ABIArgInfo getByVal(unsigned Alignment) {
157 return ABIArgInfo(ByVal, 0, Alignment);
158 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000159 static ABIArgInfo getExpand() {
160 return ABIArgInfo(Expand);
161 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000162
163 Kind getKind() const { return TheKind; }
164 bool isDefault() const { return TheKind == Default; }
165 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000166 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000167 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000168 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000169 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000170
171 // Coerce accessors
Daniel Dunbar73d66602008-09-10 07:04:09 +0000172 const llvm::Type *getCoerceToType() const {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000173 assert(TheKind == Coerce && "Invalid kind!");
174 return TypeData;
175 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000176
177 // ByVal accessors
178 unsigned getByValAlignment() const {
179 assert(TheKind == ByVal && "Invalid kind!");
180 return UIntData;
181 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000182};
183
184/***/
185
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000186/* FIXME: All of this stuff should be part of the target interface
187 somehow. It is currently here because it is not clear how to factor
188 the targets to support this, since the Targets currently live in a
189 layer below types n'stuff.
190 */
191
192/// ABIInfo - Target specific hooks for defining how a type should be
193/// passed or returned from functions.
194class clang::ABIInfo {
195public:
196 virtual ~ABIInfo();
197
198 virtual ABIArgInfo classifyReturnType(QualType RetTy,
199 ASTContext &Context) const = 0;
200
201 virtual ABIArgInfo classifyArgumentType(QualType Ty,
202 ASTContext &Context) const = 0;
203};
204
205ABIInfo::~ABIInfo() {}
206
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000207/// isEmptyStruct - Return true iff a structure has no non-empty
208/// members. Note that a structure with a flexible array member is not
209/// considered empty.
210static bool isEmptyStruct(QualType T) {
211 const RecordType *RT = T->getAsStructureType();
212 if (!RT)
213 return 0;
214 const RecordDecl *RD = RT->getDecl();
215 if (RD->hasFlexibleArrayMember())
216 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000217 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000218 e = RD->field_end(); i != e; ++i) {
219 const FieldDecl *FD = *i;
220 if (!isEmptyStruct(FD->getType()))
221 return false;
222 }
223 return true;
224}
225
226/// isSingleElementStruct - Determine if a structure is a "single
227/// element struct", i.e. it has exactly one non-empty field or
228/// exactly one field which is itself a single element
229/// struct. Structures with flexible array members are never
230/// considered single element structs.
231///
232/// \return The field declaration for the single non-empty field, if
233/// it exists.
234static const FieldDecl *isSingleElementStruct(QualType T) {
235 const RecordType *RT = T->getAsStructureType();
236 if (!RT)
237 return 0;
238
239 const RecordDecl *RD = RT->getDecl();
240 if (RD->hasFlexibleArrayMember())
241 return 0;
242
243 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000244 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000245 e = RD->field_end(); i != e; ++i) {
246 const FieldDecl *FD = *i;
247 QualType FT = FD->getType();
248
249 if (isEmptyStruct(FT)) {
250 // Ignore
251 } else if (Found) {
252 return 0;
253 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
254 Found = FD;
255 } else {
256 Found = isSingleElementStruct(FT);
257 if (!Found)
258 return 0;
259 }
260 }
261
262 return Found;
263}
264
265static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
266 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
267 return false;
268
269 uint64_t Size = Context.getTypeSize(Ty);
270 return Size == 32 || Size == 64;
271}
272
273static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
274 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000275 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000276 e = RD->field_end(); i != e; ++i) {
277 const FieldDecl *FD = *i;
278
279 if (!is32Or64BitBasicType(FD->getType(), Context))
280 return false;
281
282 // If this is a bit-field we need to make sure it is still a
283 // 32-bit or 64-bit type.
284 if (Expr *BW = FD->getBitWidth()) {
285 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
286 if (Width <= 16)
287 return false;
288 }
289 }
290 return true;
291}
292
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000293namespace {
294/// DefaultABIInfo - The default implementation for ABI specific
295/// details. This implementation provides information which results in
296/// sensible LLVM IR generation, but does not conform to any
297/// particular ABI.
298class DefaultABIInfo : public ABIInfo {
299 virtual ABIArgInfo classifyReturnType(QualType RetTy,
300 ASTContext &Context) const;
301
302 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
303 ASTContext &Context) const;
304};
305
306/// X86_32ABIInfo - The X86-32 ABI information.
307class X86_32ABIInfo : public ABIInfo {
308public:
309 virtual ABIArgInfo classifyReturnType(QualType RetTy,
310 ASTContext &Context) const;
311
312 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
313 ASTContext &Context) const;
314};
315}
316
317ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
318 ASTContext &Context) const {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000319 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000320 // Classify "single element" structs as their element type.
321 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
322 if (SeltFD) {
323 QualType SeltTy = SeltFD->getType()->getDesugaredType();
324 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
325 // FIXME: This is gross, it would be nice if we could just
326 // pass back SeltTy and have clients deal with it. Is it worth
327 // supporting coerce to both LLVM and clang Types?
328 if (BT->isIntegerType()) {
329 uint64_t Size = Context.getTypeSize(SeltTy);
330 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
331 } else if (BT->getKind() == BuiltinType::Float) {
332 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
333 } else if (BT->getKind() == BuiltinType::Double) {
334 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
335 }
336 } else if (SeltTy->isPointerType()) {
337 // FIXME: It would be really nice if this could come out as
338 // the proper pointer type.
339 llvm::Type *PtrTy =
340 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
341 return ABIArgInfo::getCoerce(PtrTy);
342 }
343 }
344
Daniel Dunbar73d66602008-09-10 07:04:09 +0000345 uint64_t Size = Context.getTypeSize(RetTy);
346 if (Size == 8) {
347 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
348 } else if (Size == 16) {
349 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
350 } else if (Size == 32) {
351 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
352 } else if (Size == 64) {
353 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
354 } else {
355 return ABIArgInfo::getStructRet();
356 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000357 } else {
358 return ABIArgInfo::getDefault();
359 }
360}
361
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000362ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
363 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000364 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000365 // Structures with flexible arrays are always byval.
366 if (const RecordType *RT = Ty->getAsStructureType())
367 if (RT->getDecl()->hasFlexibleArrayMember())
368 return ABIArgInfo::getByVal(0);
369
370 // Expand empty structs (i.e. ignore)
371 uint64_t Size = Context.getTypeSize(Ty);
372 if (Ty->isStructureType() && Size == 0)
373 return ABIArgInfo::getExpand();
374
375 // Expand structs with size <= 128-bits which consist only of
376 // basic types (int, long long, float, double, xxx*). This is
377 // non-recursive and does not ignore empty fields.
378 if (const RecordType *RT = Ty->getAsStructureType()) {
379 if (Context.getTypeSize(Ty) <= 4*32 &&
380 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
381 return ABIArgInfo::getExpand();
382 }
383
Daniel Dunbar22e30052008-09-11 01:48:57 +0000384 return ABIArgInfo::getByVal(0);
385 } else {
386 return ABIArgInfo::getDefault();
387 }
388}
389
Daniel Dunbare09a9692009-01-24 08:32:22 +0000390namespace {
391/// X86_32ABIInfo - The X86_64 ABI information.
392class X86_64ABIInfo : public ABIInfo {
393 enum Class {
394 Integer = 0,
395 SSE,
396 SSEUp,
397 X87,
398 X87Up,
399 ComplexX87,
400 NoClass,
401 Memory
402 };
403
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000404 /// merge - Implement the X86_64 ABI merging algorithm.
405 ///
406 /// \param Offset - The offset of the current field.
407 /// \param FieldLo - The low classification of the current field.
408 /// \param FieldHi - The high classification of the current field.
409 /// \param Lo [in] [out] - The accumulated low classification.
410 /// \param Lo [in] [out] - The accumulated high classification.
411 void merge(uint64_t Offset, Class FieldLo, Class FieldHi,
412 Class &Lo, Class &Hi) const;
413
Daniel Dunbare09a9692009-01-24 08:32:22 +0000414 /// classify - Determine the x86_64 register classes in which the
415 /// given type T should be passed.
416 ///
417 /// \param Lo - The classification for the low word of the type.
418 /// \param Hi - The classification for the high word of the type.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000419 /// \param OffsetBase - The bit offset of the field in the
420 /// containing object. Some parameters are classified different
421 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000422 ///
423 /// If a word is unused its result will be NoClass; if a type should
424 /// be passed in Memory then at least the classification of \arg Lo
425 /// will be Memory.
426 ///
427 /// The \arg Lo class will be NoClass iff the argument is ignored.
428 ///
429 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
430 /// be NoClass.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000431 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000432 Class &Lo, Class &Hi) const;
433
434public:
435 virtual ABIArgInfo classifyReturnType(QualType RetTy,
436 ASTContext &Context) const;
437
438 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
439 ASTContext &Context) const;
440};
441}
442
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000443void X86_64ABIInfo::merge(uint64_t Offset, Class FieldLo, Class FieldHi,
444 Class &Lo, Class &Hi) const {
445 // Determine which half of the structure we are classifying.
446 //
447 // AMD64-ABI 3.2.3p2: Rule 3. f the size of the aggregate
448 // exceeds a single eightbyte, each is classified
449 // separately. Each eightbyte gets initialized to class
450 // NO_CLASS.
451 Class &Target = Offset < 64 ? Lo : Hi;
452
453 // Merge the lo field classifcation.
454 //
455 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
456 // classified recursively so that always two fields are
457 // considered. The resulting class is calculated according to
458 // the classes of the fields in the eightbyte:
459 //
460 // (a) If both classes are equal, this is the resulting class.
461 //
462 // (b) If one of the classes is NO_CLASS, the resulting class is
463 // the other class.
464 //
465 // (c) If one of the classes is MEMORY, the result is the MEMORY
466 // class.
467 //
468 // (d) If one of the classes is INTEGER, the result is the
469 // INTEGER.
470 //
471 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
472 // MEMORY is used as class.
473 //
474 // (f) Otherwise class SSE is used.
475 if (Target == FieldLo || FieldLo == NoClass) ;
476 else if (FieldLo == Memory)
477 Lo = Memory;
478 else if (Target == NoClass)
479 Target = FieldLo;
480 else if (Target == Integer || FieldLo == Integer)
481 Target = Integer;
482 else if (FieldLo == X87 || FieldLo == X87Up || FieldLo == ComplexX87)
483 Lo = Memory;
484 else
485 Target = SSE;
486
487 // It isn't clear from the ABI spec what the role of the high
488 // classification is here, but since this should only happen
489 // when we have a struct with a two eightbyte member, we can
490 // just push the field high class into the overall high class.
491 if (FieldHi != NoClass)
492 Hi = FieldHi;
493}
494
Daniel Dunbare09a9692009-01-24 08:32:22 +0000495void X86_64ABIInfo::classify(QualType Ty,
496 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000497 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000498 Class &Lo, Class &Hi) const {
499 Lo = Memory;
500 Hi = NoClass;
501 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
502 BuiltinType::Kind k = BT->getKind();
503
Daniel Dunbar1358b202009-01-26 21:26:08 +0000504 if (k == BuiltinType::Void) {
505 Lo = NoClass;
506 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000507 Lo = Integer;
508 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
509 Lo = SSE;
510 } else if (k == BuiltinType::LongDouble) {
511 Lo = X87;
512 Hi = X87Up;
513 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000514
515 // FIXME: _Decimal32 and _Decimal64 are SSE.
516 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000517 // FIXME: __int128 is (Integer, Integer).
518 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
519 Ty->isObjCQualifiedInterfaceType()) {
520 Lo = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000521 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000522 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000523 if (Size == 64) {
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000524 // gcc passes <1 x double> in memory.
525 if (VT->getElementType() == Context.DoubleTy) {
526 Lo = Memory;
527 return;
528 }
529
Daniel Dunbaracf7dbd2009-01-30 21:50:20 +0000530 Lo = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000531
532 // If this type crosses an eightbyte boundary, it should be
533 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000534 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000535 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000536 } else if (Size == 128) {
537 Lo = SSE;
538 Hi = SSEUp;
539 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000540 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
541 QualType ET = CT->getElementType();
542
Daniel Dunbare413f532009-01-30 18:40:10 +0000543 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000544 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000545 if (Size <= 64)
546 Lo = Integer;
547 else if (Size <= 128)
548 Lo = Hi = Integer;
549 } else if (ET == Context.FloatTy)
Daniel Dunbare09a9692009-01-24 08:32:22 +0000550 Lo = SSE;
551 else if (ET == Context.DoubleTy)
552 Lo = Hi = SSE;
553 else if (ET == Context.LongDoubleTy)
554 Lo = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000555
556 // If this complex type crosses an eightbyte boundary then it
557 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000558 uint64_t EB_Real = (OffsetBase) / 64;
559 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000560 if (Hi == NoClass && EB_Real != EB_Imag)
561 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000562 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
563 // Arrays are treated like structures.
564
565 uint64_t Size = Context.getTypeSize(Ty);
566
567 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
568 // than two eightbytes, ..., it has class MEMORY.
569 if (Size > 128)
570 return;
571
572 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
573 // fields, it has class MEMORY.
574 //
575 // Only need to check alignment of array base.
576 if (OffsetBase % Context.getTypeAlign(AT->getElementType())) {
577 Lo = Memory;
578 return;
579 }
580
581 // Otherwise implement simplified merge. We could be smarter about
582 // this, but it isn't worth it and would be harder to verify.
583 Lo = NoClass;
584 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
585 uint64_t ArraySize = AT->getSize().getZExtValue();
586 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
587 Class FieldLo, FieldHi;
588 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
589
590 merge(Offset, FieldLo, FieldHi, Lo, Hi);
591 // Memory is never over-ridden, exit early if we see it.
592 if (Lo == Memory)
593 return;
594 }
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000595 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000596 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000597
598 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
599 // than two eightbytes, ..., it has class MEMORY.
600 if (Size > 128)
601 return;
602
603 const RecordDecl *RD = RT->getDecl();
604
605 // Assume variable sized types are passed in memory.
606 if (RD->hasFlexibleArrayMember())
607 return;
608
609 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
610
611 // Reset Lo class, this will be recomputed.
612 Lo = NoClass;
613 unsigned idx = 0;
614 for (RecordDecl::field_iterator i = RD->field_begin(),
615 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000616 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000617
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000618 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
619 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000620 if (Offset % Context.getTypeAlign(i->getType())) {
621 Lo = Memory;
622 return;
623 }
624
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000625 // Classify this field.
626 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000627 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000628
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000629 merge(Offset, FieldLo, FieldHi, Lo, Hi);
630 // Memory is never over-ridden, exit early if we see it.
631 if (Lo == Memory)
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000632 return;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000633 }
634
635 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
636 //
637 // (a) If one of the classes is MEMORY, the whole argument is
638 // passed in memory.
639 //
640 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
641
642 // The first of these conditions is guaranteed by how we implement
643 // the merge (just bail). I don't believe the second is actually
644 // possible at all.
645 assert(Lo != Memory && "Unexpected memory classification.");
646 if (Hi == SSEUp && Lo != SSE)
647 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000648 }
649}
650
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000651ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
652 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000653 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
654 // classification algorithm.
655 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000656 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000657
658 const llvm::Type *ResType = 0;
659 switch (Lo) {
660 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000661 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000662
663 case SSEUp:
664 case X87Up:
665 assert(0 && "Invalid classification for lo word.");
666
667 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
668 // hidden argument, i.e. structret.
669 case Memory:
670 return ABIArgInfo::getStructRet();
671
672 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
673 // available register of the sequence %rax, %rdx is used.
674 case Integer:
675 ResType = llvm::Type::Int64Ty; break;
676
677 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
678 // available SSE register of the sequence %xmm0, %xmm1 is used.
679 case SSE:
680 ResType = llvm::Type::DoubleTy; break;
681
682 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
683 // returned on the X87 stack in %st0 as 80-bit x87 number.
684 case X87:
685 ResType = llvm::Type::X86_FP80Ty; break;
686
687 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
688 // part of the value is returned in %st0 and the imaginary part in
689 // %st1.
690 case ComplexX87:
691 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
692 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
693 break;
694 }
695
696 switch (Hi) {
697 // Memory was handled previously, and ComplexX87 and X87 should
698 // never occur as hi classes.
699 case Memory:
700 case X87:
701 case ComplexX87:
702 assert(0 && "Invalid classification for hi word.");
703
704 case NoClass: break;
705 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000706 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
707 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000708 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000709 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
710 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000711
712 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
713 // is passed in the upper half of the last used SSE register.
714 //
715 // SSEUP should always be preceeded by SSE, just widen.
716 case SSEUp:
717 assert(Lo == SSE && "Unexpected SSEUp classification.");
718 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
719 break;
720
721 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000722 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000723 //
724 // X87UP should always be preceeded by X87, so we don't need to do
725 // anything here.
726 case X87Up:
727 assert(Lo == X87 && "Unexpected X87Up classification.");
728 break;
729 }
730
731 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000732}
733
734ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
735 ASTContext &Context) const {
736 return ABIArgInfo::getDefault();
737}
738
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000739ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
740 ASTContext &Context) const {
741 return ABIArgInfo::getDefault();
742}
743
744ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
745 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000746 return ABIArgInfo::getDefault();
747}
748
749const ABIInfo &CodeGenTypes::getABIInfo() const {
750 if (TheABIInfo)
751 return *TheABIInfo;
752
753 // For now we just cache this in the CodeGenTypes and don't bother
754 // to free it.
755 const char *TargetPrefix = getContext().Target.getTargetPrefix();
756 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000757 switch (getContext().Target.getPointerWidth(0)) {
758 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000759 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000760 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +0000761 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000762 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000763 }
764
765 return *(TheABIInfo = new DefaultABIInfo);
766}
767
768// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
769// "default" types to StructRet when appropriate for simplicity.
770static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
771 assert(!Ty->isArrayType() &&
772 "Array types cannot be passed directly.");
773 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000774 // Ensure default on aggregate types is StructRet.
775 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
776 return ABIArgInfo::getStructRet();
777 return Info;
778}
779
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000780// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
781// "default" types to ByVal when appropriate for simplicity.
782static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
783 assert(!Ty->isArrayType() &&
784 "Array types cannot be passed directly.");
785 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000786 // Ensure default on aggregate types is ByVal.
787 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
788 return ABIArgInfo::getByVal(0);
789 return Info;
790}
791
Daniel Dunbare126ab12008-09-10 02:41:04 +0000792/***/
793
Daniel Dunbar04d35782008-09-17 00:51:38 +0000794void CodeGenTypes::GetExpandedTypes(QualType Ty,
795 std::vector<const llvm::Type*> &ArgTys) {
796 const RecordType *RT = Ty->getAsStructureType();
797 assert(RT && "Can only expand structure types.");
798 const RecordDecl *RD = RT->getDecl();
799 assert(!RD->hasFlexibleArrayMember() &&
800 "Cannot expand structure with flexible array.");
801
Douglas Gregor5d764842009-01-09 17:18:27 +0000802 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000803 e = RD->field_end(); i != e; ++i) {
804 const FieldDecl *FD = *i;
805 assert(!FD->isBitField() &&
806 "Cannot expand structure with bit-field members.");
807
808 QualType FT = FD->getType();
809 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
810 GetExpandedTypes(FT, ArgTys);
811 } else {
812 ArgTys.push_back(ConvertType(FT));
813 }
814 }
815}
816
817llvm::Function::arg_iterator
818CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
819 llvm::Function::arg_iterator AI) {
820 const RecordType *RT = Ty->getAsStructureType();
821 assert(RT && "Can only expand structure types.");
822
823 RecordDecl *RD = RT->getDecl();
824 assert(LV.isSimple() &&
825 "Unexpected non-simple lvalue during struct expansion.");
826 llvm::Value *Addr = LV.getAddress();
827 for (RecordDecl::field_iterator i = RD->field_begin(),
828 e = RD->field_end(); i != e; ++i) {
829 FieldDecl *FD = *i;
830 QualType FT = FD->getType();
831
832 // FIXME: What are the right qualifiers here?
833 LValue LV = EmitLValueForField(Addr, FD, false, 0);
834 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
835 AI = ExpandTypeFromArgs(FT, LV, AI);
836 } else {
837 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
838 ++AI;
839 }
840 }
841
842 return AI;
843}
844
845void
846CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
847 llvm::SmallVector<llvm::Value*, 16> &Args) {
848 const RecordType *RT = Ty->getAsStructureType();
849 assert(RT && "Can only expand structure types.");
850
851 RecordDecl *RD = RT->getDecl();
852 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
853 llvm::Value *Addr = RV.getAggregateAddr();
854 for (RecordDecl::field_iterator i = RD->field_begin(),
855 e = RD->field_end(); i != e; ++i) {
856 FieldDecl *FD = *i;
857 QualType FT = FD->getType();
858
859 // FIXME: What are the right qualifiers here?
860 LValue LV = EmitLValueForField(Addr, FD, false, 0);
861 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
862 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
863 } else {
864 RValue RV = EmitLoadOfLValue(LV, FT);
865 assert(RV.isScalar() &&
866 "Unexpected non-scalar rvalue during struct expansion.");
867 Args.push_back(RV.getScalarVal());
868 }
869 }
870}
871
872/***/
873
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000874const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000875CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
876 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
877}
878
879const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000880CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000881 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
882}
883
884const llvm::FunctionType *
885CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
886 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000887 std::vector<const llvm::Type*> ArgTys;
888
889 const llvm::Type *ResultType = 0;
890
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000891 QualType RetTy = *begin;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000892 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000893 switch (RetAI.getKind()) {
894 case ABIArgInfo::ByVal:
895 case ABIArgInfo::Expand:
896 assert(0 && "Invalid ABI kind for return argument");
897
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000898 case ABIArgInfo::Default:
899 if (RetTy->isVoidType()) {
900 ResultType = llvm::Type::VoidTy;
901 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000902 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000903 }
904 break;
905
906 case ABIArgInfo::StructRet: {
907 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000908 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000909 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
910 break;
911 }
912
Daniel Dunbar1358b202009-01-26 21:26:08 +0000913 case ABIArgInfo::Ignore:
914 ResultType = llvm::Type::VoidTy;
915 break;
916
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000917 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000918 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000919 break;
920 }
921
922 for (++begin; begin != end; ++begin) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000923 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbara9976a22008-09-10 07:00:50 +0000924 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000925
926 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +0000927 case ABIArgInfo::Ignore:
928 break;
929
Daniel Dunbar04d35782008-09-17 00:51:38 +0000930 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000931 case ABIArgInfo::StructRet:
932 assert(0 && "Invalid ABI kind for non-return argument");
933
934 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000935 // byval arguments are always on the stack, which is addr space #0.
936 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000937 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
938 break;
939
940 case ABIArgInfo::Default:
941 ArgTys.push_back(Ty);
942 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000943
944 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000945 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000946 break;
947 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000948 }
949
Daniel Dunbara9976a22008-09-10 07:00:50 +0000950 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000951}
952
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000953bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000954 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000955}
956
Devang Patela85a9ef2008-09-25 21:02:23 +0000957void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000958 ArgTypeIterator begin,
959 ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000960 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000961 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000962 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000963
964 if (TargetDecl) {
965 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000966 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000967 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000968 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000969 if (TargetDecl->getAttr<PureAttr>())
970 FuncAttrs |= llvm::Attribute::ReadOnly;
971 if (TargetDecl->getAttr<ConstAttr>())
972 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000973 }
974
Daniel Dunbare126ab12008-09-10 02:41:04 +0000975 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000976 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000977 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000978 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000979 case ABIArgInfo::Default:
980 if (RetTy->isPromotableIntegerType()) {
981 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000982 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000983 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000984 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000985 }
986 }
987 break;
988
989 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000990 PAL.push_back(llvm::AttributeWithIndex::get(Index,
991 llvm::Attribute::StructRet|
992 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000993 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000994 break;
995
Daniel Dunbar1358b202009-01-26 21:26:08 +0000996 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000997 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000998 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000999
1000 case ABIArgInfo::ByVal:
1001 case ABIArgInfo::Expand:
1002 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001003 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001004
Devang Patel2bb6eb82008-09-26 22:53:57 +00001005 if (RetAttrs)
1006 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001007 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001008 QualType ParamType = *begin;
Devang Patela85a9ef2008-09-25 21:02:23 +00001009 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001010 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001011
1012 switch (AI.getKind()) {
1013 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001014 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001015 assert(0 && "Invalid ABI kind for non-return argument");
1016
1017 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +00001018 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001019 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1020 break;
1021
1022 case ABIArgInfo::Default:
1023 if (ParamType->isPromotableIntegerType()) {
1024 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001025 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001026 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001027 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001028 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001029 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001030 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001031
Daniel Dunbar1358b202009-01-26 21:26:08 +00001032 case ABIArgInfo::Ignore:
1033 // Skip increment, no matching LLVM parameter.
1034 continue;
1035
Daniel Dunbar04d35782008-09-17 00:51:38 +00001036 case ABIArgInfo::Expand: {
1037 std::vector<const llvm::Type*> Tys;
1038 // FIXME: This is rather inefficient. Do we ever actually need
1039 // to do anything here? The result should be just reconstructed
1040 // on the other side, so extension should be a non-issue.
1041 getTypes().GetExpandedTypes(ParamType, Tys);
1042 Index += Tys.size();
1043 continue;
1044 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001045 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001046
Devang Patela85a9ef2008-09-25 21:02:23 +00001047 if (Attributes)
1048 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001049 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001050 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001051 if (FuncAttrs)
1052 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1053
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001054}
1055
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001056void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
1057 QualType RetTy,
1058 const FunctionArgList &Args) {
1059 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1060 llvm::Function::arg_iterator AI = Fn->arg_begin();
1061
1062 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001063 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001064 AI->setName("agg.result");
1065 ++AI;
1066 }
1067
1068 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001069 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001070 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001071 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001072 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001073
1074 switch (ArgI.getKind()) {
1075 case ABIArgInfo::ByVal:
1076 case ABIArgInfo::Default: {
1077 assert(AI != Fn->arg_end() && "Argument mismatch!");
1078 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001079 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001080 // This must be a promotion, for something like
1081 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +00001082 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001083 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001084 EmitParmDecl(*Arg, V);
1085 break;
1086 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001087
1088 case ABIArgInfo::Expand: {
1089 // If this was structure was expand into multiple arguments then
1090 // we need to create a temporary and reconstruct it from the
1091 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001092 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001093 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1094 (Name + ".addr").c_str());
1095 // FIXME: What are the right qualifiers here?
1096 llvm::Function::arg_iterator End =
1097 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1098 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001099
Daniel Dunbar04d35782008-09-17 00:51:38 +00001100 // Name the arguments used in expansion and increment AI.
1101 unsigned Index = 0;
1102 for (; AI != End; ++AI, ++Index)
1103 AI->setName(Name + "." + llvm::utostr(Index));
1104 continue;
1105 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001106
1107 case ABIArgInfo::Ignore:
1108 break;
1109
Daniel Dunbar22e30052008-09-11 01:48:57 +00001110 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001111 case ABIArgInfo::StructRet:
1112 assert(0 && "Invalid ABI kind for non-return argument");
1113 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001114
1115 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001116 }
1117 assert(AI == Fn->arg_end() && "Argument mismatch!");
1118}
1119
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001120/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1121/// a pointer to an object of type \arg Ty.
1122///
1123/// This safely handles the case when the src type is smaller than the
1124/// destination type; in this situation the values of bits which not
1125/// present in the src are undefined.
1126static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1127 const llvm::Type *Ty,
1128 CodeGenFunction &CGF) {
1129 const llvm::Type *SrcTy =
1130 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Daniel Dunbar1aa2be92009-01-30 00:47:38 +00001131 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1132 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001133
1134 // If load is legal, just bitcase the src pointer.
1135 if (SrcSize == DstSize) {
1136 llvm::Value *Casted =
1137 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
1138 return CGF.Builder.CreateLoad(Casted);
1139 } else {
1140 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1141
1142 // Otherwise do coercion through memory. This is stupid, but
1143 // simple.
1144 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1145 llvm::Value *Casted =
1146 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
1147 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1148 return CGF.Builder.CreateLoad(Tmp);
1149 }
1150}
1151
1152/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1153/// where the source and destination may have different types.
1154///
1155/// This safely handles the case when the src type is larger than the
1156/// destination type; the upper bits of the src will be lost.
1157static void CreateCoercedStore(llvm::Value *Src,
1158 llvm::Value *DstPtr,
1159 CodeGenFunction &CGF) {
1160 const llvm::Type *SrcTy = Src->getType();
1161 const llvm::Type *DstTy =
1162 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1163
Daniel Dunbar1aa2be92009-01-30 00:47:38 +00001164 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1165 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001166
1167 // If store is legal, just bitcase the src pointer.
1168 if (SrcSize == DstSize) {
1169 llvm::Value *Casted =
1170 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1171 CGF.Builder.CreateStore(Src, Casted);
1172 } else {
1173 assert(SrcSize > DstSize && "Coercion is missing bits!");
1174
1175 // Otherwise do coercion through memory. This is stupid, but
1176 // simple.
1177 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1178 CGF.Builder.CreateStore(Src, Tmp);
1179 llvm::Value *Casted =
1180 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1181 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1182 }
1183}
1184
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001185void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1186 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001187 llvm::Value *RV = 0;
1188
1189 // Functions with no result always return void.
1190 if (ReturnValue) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001191 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001192
1193 switch (RetAI.getKind()) {
1194 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001195 if (RetTy->isAnyComplexType()) {
1196 // FIXME: Volatile
1197 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1198 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1199 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1200 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1201 } else {
1202 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1203 CurFn->arg_begin());
1204 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001205 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001206
Daniel Dunbare126ab12008-09-10 02:41:04 +00001207 case ABIArgInfo::Default:
1208 RV = Builder.CreateLoad(ReturnValue);
1209 break;
1210
Daniel Dunbar1358b202009-01-26 21:26:08 +00001211 case ABIArgInfo::Ignore:
1212 break;
1213
Daniel Dunbar73d66602008-09-10 07:04:09 +00001214 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001215 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001216 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001217 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001218
1219 case ABIArgInfo::ByVal:
1220 case ABIArgInfo::Expand:
1221 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001222 }
1223 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001224
1225 if (RV) {
1226 Builder.CreateRet(RV);
1227 } else {
1228 Builder.CreateRetVoid();
1229 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001230}
1231
1232RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +00001233 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001234 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001235 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001236
1237 // Handle struct-return functions by passing a pointer to the
1238 // location that we would like to return into.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001239 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001240 switch (RetAI.getKind()) {
1241 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001242 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001243 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001244 break;
1245
1246 case ABIArgInfo::Default:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001247 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001248 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001249 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001250
1251 case ABIArgInfo::ByVal:
1252 case ABIArgInfo::Expand:
1253 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001254 }
1255
1256 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1257 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001258 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001259 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001260
1261 switch (ArgInfo.getKind()) {
1262 case ABIArgInfo::ByVal: // Default is byval
1263 case ABIArgInfo::Default:
1264 if (RV.isScalar()) {
1265 Args.push_back(RV.getScalarVal());
1266 } else if (RV.isComplex()) {
1267 // Make a temporary alloca to pass the argument.
1268 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1269 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1270 } else {
1271 Args.push_back(RV.getAggregateAddr());
1272 }
1273 break;
1274
Daniel Dunbar1358b202009-01-26 21:26:08 +00001275 case ABIArgInfo::Ignore:
1276 break;
1277
Daniel Dunbar04d35782008-09-17 00:51:38 +00001278 case ABIArgInfo::StructRet:
1279 case ABIArgInfo::Coerce:
1280 assert(0 && "Invalid ABI kind for non-return argument");
1281 break;
1282
1283 case ABIArgInfo::Expand:
1284 ExpandTypeToArgs(I->second, RV, Args);
1285 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001286 }
1287 }
1288
1289 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001290 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001291
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001292 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001293 CodeGen::AttributeListType AttributeList;
1294 CGM.ConstructAttributeList(0,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +00001295 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patela85a9ef2008-09-25 21:02:23 +00001296 AttributeList);
1297 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1298 AttributeList.size()));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001299
1300 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1301 CI->setCallingConv(F->getCallingConv());
1302 if (CI->getType() != llvm::Type::VoidTy)
1303 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001304
1305 switch (RetAI.getKind()) {
1306 case ABIArgInfo::StructRet:
1307 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001308 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001309 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001310 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001311 else
1312 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001313
Daniel Dunbare126ab12008-09-10 02:41:04 +00001314 case ABIArgInfo::Default:
1315 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1316
Daniel Dunbar1358b202009-01-26 21:26:08 +00001317 case ABIArgInfo::Ignore:
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001318 if (RetTy->isVoidType())
1319 return RValue::get(0);
1320 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1321 llvm::Value *Res =
1322 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1323 return RValue::getAggregate(Res);
1324 }
1325 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001326
Daniel Dunbar73d66602008-09-10 07:04:09 +00001327 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001328 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1329 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001330 if (RetTy->isAnyComplexType())
1331 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001332 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001333 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001334 else
1335 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001336 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001337
1338 case ABIArgInfo::ByVal:
1339 case ABIArgInfo::Expand:
1340 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001341 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001342
1343 assert(0 && "Unhandled ABIArgInfo::Kind");
1344 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001345}