blob: c6e804ea0bbb15681efec60c99d86ea234294560 [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 Dunbard283e632009-02-03 01:05:53 +000027
28#include "ABIInfo.h"
29
Daniel Dunbara8f02052008-09-08 21:33:45 +000030using namespace clang;
31using namespace CodeGen;
32
33/***/
34
Daniel Dunbara8f02052008-09-08 21:33:45 +000035// FIXME: Use iterator and sidestep silly type array creation.
36
Daniel Dunbar34bda882009-02-02 23:23:47 +000037const
38CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
39 return getFunctionInfo(FTNP->getResultType(),
40 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000041}
42
Daniel Dunbar34bda882009-02-02 23:23:47 +000043const
44CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
45 llvm::SmallVector<QualType, 16> ArgTys;
46 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000047 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000048 ArgTys.push_back(FTP->getArgType(i));
49 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000050}
51
Daniel Dunbar34bda882009-02-02 23:23:47 +000052const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000053 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar34bda882009-02-02 23:23:47 +000054 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
55 return getFunctionInfo(FTP);
56 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000057}
58
Daniel Dunbar34bda882009-02-02 23:23:47 +000059const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
60 llvm::SmallVector<QualType, 16> ArgTys;
61 ArgTys.push_back(MD->getSelfDecl()->getType());
62 ArgTys.push_back(Context.getObjCSelType());
63 // FIXME: Kill copy?
Daniel Dunbara8f02052008-09-08 21:33:45 +000064 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
65 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000066 ArgTys.push_back((*i)->getType());
67 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000068}
69
Daniel Dunbar34bda882009-02-02 23:23:47 +000070const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
71 const CallArgList &Args) {
72 // FIXME: Kill copy.
73 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000074 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
75 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000076 ArgTys.push_back(i->second);
77 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000078}
79
Daniel Dunbar34bda882009-02-02 23:23:47 +000080const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
81 const FunctionArgList &Args) {
82 // FIXME: Kill copy.
83 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +000084 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
85 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000086 ArgTys.push_back(i->second);
87 return getFunctionInfo(ResTy, ArgTys);
88}
89
90const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
91 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +000092 // Lookup or create unique function info.
93 llvm::FoldingSetNodeID ID;
94 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
95
96 void *InsertPos = 0;
97 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
98 if (FI)
99 return *FI;
100
101 FI = new CGFunctionInfo(ResTy, ArgTys);
102 FunctionInfos.InsertNode(FI, InsertPos);
103 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000104}
105
106/***/
107
108CGFunctionInfo::CGFunctionInfo(QualType ResTy,
109 const llvm::SmallVector<QualType, 16> &ArgTys) {
110 ArgTypes.push_back(ResTy);
111 ArgTypes.insert(ArgTypes.end(), ArgTys.begin(), ArgTys.end());
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000112}
113
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000114CGFunctionInfo::arg_iterator CGFunctionInfo::arg_begin() const {
115 return ArgTypes.begin()+1;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000116}
117
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000118CGFunctionInfo::arg_iterator CGFunctionInfo::arg_end() const {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000119 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +0000120}
121
122/***/
123
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000124ABIInfo::~ABIInfo() {}
125
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000126/// isEmptyStruct - Return true iff a structure has no non-empty
127/// members. Note that a structure with a flexible array member is not
128/// considered empty.
129static bool isEmptyStruct(QualType T) {
130 const RecordType *RT = T->getAsStructureType();
131 if (!RT)
132 return 0;
133 const RecordDecl *RD = RT->getDecl();
134 if (RD->hasFlexibleArrayMember())
135 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000136 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000137 e = RD->field_end(); i != e; ++i) {
138 const FieldDecl *FD = *i;
139 if (!isEmptyStruct(FD->getType()))
140 return false;
141 }
142 return true;
143}
144
145/// isSingleElementStruct - Determine if a structure is a "single
146/// element struct", i.e. it has exactly one non-empty field or
147/// exactly one field which is itself a single element
148/// struct. Structures with flexible array members are never
149/// considered single element structs.
150///
151/// \return The field declaration for the single non-empty field, if
152/// it exists.
153static const FieldDecl *isSingleElementStruct(QualType T) {
154 const RecordType *RT = T->getAsStructureType();
155 if (!RT)
156 return 0;
157
158 const RecordDecl *RD = RT->getDecl();
159 if (RD->hasFlexibleArrayMember())
160 return 0;
161
162 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000163 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000164 e = RD->field_end(); i != e; ++i) {
165 const FieldDecl *FD = *i;
166 QualType FT = FD->getType();
167
168 if (isEmptyStruct(FT)) {
169 // Ignore
170 } else if (Found) {
171 return 0;
172 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
173 Found = FD;
174 } else {
175 Found = isSingleElementStruct(FT);
176 if (!Found)
177 return 0;
178 }
179 }
180
181 return Found;
182}
183
184static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
185 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
186 return false;
187
188 uint64_t Size = Context.getTypeSize(Ty);
189 return Size == 32 || Size == 64;
190}
191
192static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
193 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000194 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000195 e = RD->field_end(); i != e; ++i) {
196 const FieldDecl *FD = *i;
197
198 if (!is32Or64BitBasicType(FD->getType(), Context))
199 return false;
200
201 // If this is a bit-field we need to make sure it is still a
202 // 32-bit or 64-bit type.
203 if (Expr *BW = FD->getBitWidth()) {
204 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
205 if (Width <= 16)
206 return false;
207 }
208 }
209 return true;
210}
211
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000212namespace {
213/// DefaultABIInfo - The default implementation for ABI specific
214/// details. This implementation provides information which results in
215/// sensible LLVM IR generation, but does not conform to any
216/// particular ABI.
217class DefaultABIInfo : public ABIInfo {
218 virtual ABIArgInfo classifyReturnType(QualType RetTy,
219 ASTContext &Context) const;
220
221 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
222 ASTContext &Context) const;
223};
224
225/// X86_32ABIInfo - The X86-32 ABI information.
226class X86_32ABIInfo : public ABIInfo {
227public:
228 virtual ABIArgInfo classifyReturnType(QualType RetTy,
229 ASTContext &Context) const;
230
231 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
232 ASTContext &Context) const;
233};
234}
235
236ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
237 ASTContext &Context) const {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000238 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000239 // Classify "single element" structs as their element type.
240 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
241 if (SeltFD) {
242 QualType SeltTy = SeltFD->getType()->getDesugaredType();
243 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
244 // FIXME: This is gross, it would be nice if we could just
245 // pass back SeltTy and have clients deal with it. Is it worth
246 // supporting coerce to both LLVM and clang Types?
247 if (BT->isIntegerType()) {
248 uint64_t Size = Context.getTypeSize(SeltTy);
249 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
250 } else if (BT->getKind() == BuiltinType::Float) {
251 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
252 } else if (BT->getKind() == BuiltinType::Double) {
253 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
254 }
255 } else if (SeltTy->isPointerType()) {
256 // FIXME: It would be really nice if this could come out as
257 // the proper pointer type.
258 llvm::Type *PtrTy =
259 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
260 return ABIArgInfo::getCoerce(PtrTy);
261 }
262 }
263
Daniel Dunbar73d66602008-09-10 07:04:09 +0000264 uint64_t Size = Context.getTypeSize(RetTy);
265 if (Size == 8) {
266 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
267 } else if (Size == 16) {
268 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
269 } else if (Size == 32) {
270 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
271 } else if (Size == 64) {
272 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
273 } else {
274 return ABIArgInfo::getStructRet();
275 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000276 } else {
277 return ABIArgInfo::getDefault();
278 }
279}
280
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000281ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
282 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000283 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000284 // Structures with flexible arrays are always byval.
285 if (const RecordType *RT = Ty->getAsStructureType())
286 if (RT->getDecl()->hasFlexibleArrayMember())
287 return ABIArgInfo::getByVal(0);
288
289 // Expand empty structs (i.e. ignore)
290 uint64_t Size = Context.getTypeSize(Ty);
291 if (Ty->isStructureType() && Size == 0)
292 return ABIArgInfo::getExpand();
293
294 // Expand structs with size <= 128-bits which consist only of
295 // basic types (int, long long, float, double, xxx*). This is
296 // non-recursive and does not ignore empty fields.
297 if (const RecordType *RT = Ty->getAsStructureType()) {
298 if (Context.getTypeSize(Ty) <= 4*32 &&
299 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
300 return ABIArgInfo::getExpand();
301 }
302
Daniel Dunbar22e30052008-09-11 01:48:57 +0000303 return ABIArgInfo::getByVal(0);
304 } else {
305 return ABIArgInfo::getDefault();
306 }
307}
308
Daniel Dunbare09a9692009-01-24 08:32:22 +0000309namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000310/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000311class X86_64ABIInfo : public ABIInfo {
312 enum Class {
313 Integer = 0,
314 SSE,
315 SSEUp,
316 X87,
317 X87Up,
318 ComplexX87,
319 NoClass,
320 Memory
321 };
322
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000323 /// merge - Implement the X86_64 ABI merging algorithm.
324 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000325 /// Merge an accumulating classification \arg Accum with a field
326 /// classification \arg Field.
327 ///
328 /// \param Accum - The accumulating classification. This should
329 /// always be either NoClass or the result of a previous merge
330 /// call. In addition, this should never be Memory (the caller
331 /// should just return Memory for the aggregate).
332 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000333
Daniel Dunbare09a9692009-01-24 08:32:22 +0000334 /// classify - Determine the x86_64 register classes in which the
335 /// given type T should be passed.
336 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000337 /// \param Lo - The classification for the parts of the type
338 /// residing in the low word of the containing object.
339 ///
340 /// \param Hi - The classification for the parts of the type
341 /// residing in the high word of the containing object.
342 ///
343 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000344 /// containing object. Some parameters are classified different
345 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000346 ///
347 /// If a word is unused its result will be NoClass; if a type should
348 /// be passed in Memory then at least the classification of \arg Lo
349 /// will be Memory.
350 ///
351 /// The \arg Lo class will be NoClass iff the argument is ignored.
352 ///
353 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
354 /// be NoClass.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000355 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000356 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000357
Daniel Dunbare09a9692009-01-24 08:32:22 +0000358public:
359 virtual ABIArgInfo classifyReturnType(QualType RetTy,
360 ASTContext &Context) const;
361
362 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
363 ASTContext &Context) const;
364};
365}
366
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000367X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
368 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000369 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
370 // classified recursively so that always two fields are
371 // considered. The resulting class is calculated according to
372 // the classes of the fields in the eightbyte:
373 //
374 // (a) If both classes are equal, this is the resulting class.
375 //
376 // (b) If one of the classes is NO_CLASS, the resulting class is
377 // the other class.
378 //
379 // (c) If one of the classes is MEMORY, the result is the MEMORY
380 // class.
381 //
382 // (d) If one of the classes is INTEGER, the result is the
383 // INTEGER.
384 //
385 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
386 // MEMORY is used as class.
387 //
388 // (f) Otherwise class SSE is used.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000389 assert((Accum == NoClass || Accum == Integer ||
390 Accum == SSE || Accum == SSEUp) &&
391 "Invalid accumulated classification during merge.");
392 if (Accum == Field || Field == NoClass)
393 return Accum;
394 else if (Field == Memory)
395 return Memory;
396 else if (Accum == NoClass)
397 return Field;
398 else if (Accum == Integer || Field == Integer)
399 return Integer;
400 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
401 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000402 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000403 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000404}
405
Daniel Dunbare09a9692009-01-24 08:32:22 +0000406void X86_64ABIInfo::classify(QualType Ty,
407 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000408 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000409 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000410 // FIXME: This code can be simplified by introducing a simple value
411 // class for Class pairs with appropriate constructor methods for
412 // the various situations.
413
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000414 Lo = Hi = NoClass;
415
416 Class &Current = OffsetBase < 64 ? Lo : Hi;
417 Current = Memory;
418
Daniel Dunbare09a9692009-01-24 08:32:22 +0000419 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
420 BuiltinType::Kind k = BT->getKind();
421
Daniel Dunbar1358b202009-01-26 21:26:08 +0000422 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000423 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000424 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000425 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000426 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000427 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000428 } else if (k == BuiltinType::LongDouble) {
429 Lo = X87;
430 Hi = X87Up;
431 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000432 // FIXME: _Decimal32 and _Decimal64 are SSE.
433 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000434 // FIXME: __int128 is (Integer, Integer).
435 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
436 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000437 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000438 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000439 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000440 if (Size == 64) {
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000441 // gcc passes <1 x double> in memory.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000442 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000443 return;
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000444
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000445 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000446
447 // If this type crosses an eightbyte boundary, it should be
448 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000449 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000450 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000451 } else if (Size == 128) {
452 Lo = SSE;
453 Hi = SSEUp;
454 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000455 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
456 QualType ET = CT->getElementType();
457
Daniel Dunbare413f532009-01-30 18:40:10 +0000458 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000459 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000460 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000461 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000462 else if (Size <= 128)
463 Lo = Hi = Integer;
464 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000465 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000466 else if (ET == Context.DoubleTy)
467 Lo = Hi = SSE;
468 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000469 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000470
471 // If this complex type crosses an eightbyte boundary then it
472 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000473 uint64_t EB_Real = (OffsetBase) / 64;
474 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000475 if (Hi == NoClass && EB_Real != EB_Imag)
476 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000477 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
478 // Arrays are treated like structures.
479
480 uint64_t Size = Context.getTypeSize(Ty);
481
482 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
483 // than two eightbytes, ..., it has class MEMORY.
484 if (Size > 128)
485 return;
486
487 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
488 // fields, it has class MEMORY.
489 //
490 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000491 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000492 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000493
494 // Otherwise implement simplified merge. We could be smarter about
495 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000496 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000497 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
498 uint64_t ArraySize = AT->getSize().getZExtValue();
499 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
500 Class FieldLo, FieldHi;
501 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000502 Lo = merge(Lo, FieldLo);
503 Hi = merge(Hi, FieldHi);
504 if (Lo == Memory || Hi == Memory)
505 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000506 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000507
508 // Do post merger cleanup (see below). Only case we worry about is Memory.
509 if (Hi == Memory)
510 Lo = Memory;
511 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000512 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000513 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000514
515 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
516 // than two eightbytes, ..., it has class MEMORY.
517 if (Size > 128)
518 return;
519
520 const RecordDecl *RD = RT->getDecl();
521
522 // Assume variable sized types are passed in memory.
523 if (RD->hasFlexibleArrayMember())
524 return;
525
526 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
527
528 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000529 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000530 unsigned idx = 0;
531 for (RecordDecl::field_iterator i = RD->field_begin(),
532 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000533 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000534
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000535 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
536 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000537 if (Offset % Context.getTypeAlign(i->getType())) {
538 Lo = Memory;
539 return;
540 }
541
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000542 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000543 //
544 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
545 // exceeds a single eightbyte, each is classified
546 // separately. Each eightbyte gets initialized to class
547 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000548 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000549 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000550 Lo = merge(Lo, FieldLo);
551 Hi = merge(Hi, FieldHi);
552 if (Lo == Memory || Hi == Memory)
553 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000554 }
555
556 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
557 //
558 // (a) If one of the classes is MEMORY, the whole argument is
559 // passed in memory.
560 //
561 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
562
563 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000564 // the merge (just bail).
565 //
566 // The second condition occurs in the case of unions; for example
567 // union { _Complex double; unsigned; }.
568 if (Hi == Memory)
569 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000570 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000571 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000572 }
573}
574
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000575
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000576ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
577 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000578 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
579 // classification algorithm.
580 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000581 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000582
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000583 // Check some invariants.
584 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
585 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
586 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
587
Daniel Dunbare09a9692009-01-24 08:32:22 +0000588 const llvm::Type *ResType = 0;
589 switch (Lo) {
590 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000591 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000592
593 case SSEUp:
594 case X87Up:
595 assert(0 && "Invalid classification for lo word.");
596
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000597 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
598 // hidden argument, i.e. structret.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000599 case Memory:
600 return ABIArgInfo::getStructRet();
601
602 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
603 // available register of the sequence %rax, %rdx is used.
604 case Integer:
605 ResType = llvm::Type::Int64Ty; break;
606
607 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
608 // available SSE register of the sequence %xmm0, %xmm1 is used.
609 case SSE:
610 ResType = llvm::Type::DoubleTy; break;
611
612 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
613 // returned on the X87 stack in %st0 as 80-bit x87 number.
614 case X87:
615 ResType = llvm::Type::X86_FP80Ty; break;
616
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000617 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
618 // part of the value is returned in %st0 and the imaginary part in
619 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000620 case ComplexX87:
621 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
622 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
623 break;
624 }
625
626 switch (Hi) {
627 // Memory was handled previously, and ComplexX87 and X87 should
628 // never occur as hi classes.
629 case Memory:
630 case X87:
631 case ComplexX87:
632 assert(0 && "Invalid classification for hi word.");
633
634 case NoClass: break;
635 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000636 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
637 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000638 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000639 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
640 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000641
642 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
643 // is passed in the upper half of the last used SSE register.
644 //
645 // SSEUP should always be preceeded by SSE, just widen.
646 case SSEUp:
647 assert(Lo == SSE && "Unexpected SSEUp classification.");
648 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
649 break;
650
651 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000652 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000653 //
654 // X87UP should always be preceeded by X87, so we don't need to do
655 // anything here.
656 case X87Up:
657 assert(Lo == X87 && "Unexpected X87Up classification.");
658 break;
659 }
660
661 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000662}
663
664ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
665 ASTContext &Context) const {
666 return ABIArgInfo::getDefault();
667}
668
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000669ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
670 ASTContext &Context) const {
671 return ABIArgInfo::getDefault();
672}
673
674ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
675 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000676 return ABIArgInfo::getDefault();
677}
678
679const ABIInfo &CodeGenTypes::getABIInfo() const {
680 if (TheABIInfo)
681 return *TheABIInfo;
682
683 // For now we just cache this in the CodeGenTypes and don't bother
684 // to free it.
685 const char *TargetPrefix = getContext().Target.getTargetPrefix();
686 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000687 switch (getContext().Target.getPointerWidth(0)) {
688 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000689 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000690 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +0000691 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000692 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000693 }
694
695 return *(TheABIInfo = new DefaultABIInfo);
696}
697
698// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
699// "default" types to StructRet when appropriate for simplicity.
700static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
701 assert(!Ty->isArrayType() &&
702 "Array types cannot be passed directly.");
703 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000704 // Ensure default on aggregate types is StructRet.
705 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
706 return ABIArgInfo::getStructRet();
707 return Info;
708}
709
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000710// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
711// "default" types to ByVal when appropriate for simplicity.
712static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
713 assert(!Ty->isArrayType() &&
714 "Array types cannot be passed directly.");
715 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000716 // Ensure default on aggregate types is ByVal.
717 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
718 return ABIArgInfo::getByVal(0);
719 return Info;
720}
721
Daniel Dunbare126ab12008-09-10 02:41:04 +0000722/***/
723
Daniel Dunbar04d35782008-09-17 00:51:38 +0000724void CodeGenTypes::GetExpandedTypes(QualType Ty,
725 std::vector<const llvm::Type*> &ArgTys) {
726 const RecordType *RT = Ty->getAsStructureType();
727 assert(RT && "Can only expand structure types.");
728 const RecordDecl *RD = RT->getDecl();
729 assert(!RD->hasFlexibleArrayMember() &&
730 "Cannot expand structure with flexible array.");
731
Douglas Gregor5d764842009-01-09 17:18:27 +0000732 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000733 e = RD->field_end(); i != e; ++i) {
734 const FieldDecl *FD = *i;
735 assert(!FD->isBitField() &&
736 "Cannot expand structure with bit-field members.");
737
738 QualType FT = FD->getType();
739 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
740 GetExpandedTypes(FT, ArgTys);
741 } else {
742 ArgTys.push_back(ConvertType(FT));
743 }
744 }
745}
746
747llvm::Function::arg_iterator
748CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
749 llvm::Function::arg_iterator AI) {
750 const RecordType *RT = Ty->getAsStructureType();
751 assert(RT && "Can only expand structure types.");
752
753 RecordDecl *RD = RT->getDecl();
754 assert(LV.isSimple() &&
755 "Unexpected non-simple lvalue during struct expansion.");
756 llvm::Value *Addr = LV.getAddress();
757 for (RecordDecl::field_iterator i = RD->field_begin(),
758 e = RD->field_end(); i != e; ++i) {
759 FieldDecl *FD = *i;
760 QualType FT = FD->getType();
761
762 // FIXME: What are the right qualifiers here?
763 LValue LV = EmitLValueForField(Addr, FD, false, 0);
764 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
765 AI = ExpandTypeFromArgs(FT, LV, AI);
766 } else {
767 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
768 ++AI;
769 }
770 }
771
772 return AI;
773}
774
775void
776CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
777 llvm::SmallVector<llvm::Value*, 16> &Args) {
778 const RecordType *RT = Ty->getAsStructureType();
779 assert(RT && "Can only expand structure types.");
780
781 RecordDecl *RD = RT->getDecl();
782 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
783 llvm::Value *Addr = RV.getAggregateAddr();
784 for (RecordDecl::field_iterator i = RD->field_begin(),
785 e = RD->field_end(); i != e; ++i) {
786 FieldDecl *FD = *i;
787 QualType FT = FD->getType();
788
789 // FIXME: What are the right qualifiers here?
790 LValue LV = EmitLValueForField(Addr, FD, false, 0);
791 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
792 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
793 } else {
794 RValue RV = EmitLoadOfLValue(LV, FT);
795 assert(RV.isScalar() &&
796 "Unexpected non-scalar rvalue during struct expansion.");
797 Args.push_back(RV.getScalarVal());
798 }
799 }
800}
801
Daniel Dunbar84379912009-02-02 19:06:38 +0000802/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
803/// a pointer to an object of type \arg Ty.
804///
805/// This safely handles the case when the src type is smaller than the
806/// destination type; in this situation the values of bits which not
807/// present in the src are undefined.
808static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
809 const llvm::Type *Ty,
810 CodeGenFunction &CGF) {
811 const llvm::Type *SrcTy =
812 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
813 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
814 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
815
816 // If load is legal, just bitcase the src pointer.
817 if (SrcSize == DstSize) {
818 llvm::Value *Casted =
819 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
820 return CGF.Builder.CreateLoad(Casted);
821 } else {
822 assert(SrcSize < DstSize && "Coercion is losing source bits!");
823
824 // Otherwise do coercion through memory. This is stupid, but
825 // simple.
826 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
827 llvm::Value *Casted =
828 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
829 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
830 return CGF.Builder.CreateLoad(Tmp);
831 }
832}
833
834/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
835/// where the source and destination may have different types.
836///
837/// This safely handles the case when the src type is larger than the
838/// destination type; the upper bits of the src will be lost.
839static void CreateCoercedStore(llvm::Value *Src,
840 llvm::Value *DstPtr,
841 CodeGenFunction &CGF) {
842 const llvm::Type *SrcTy = Src->getType();
843 const llvm::Type *DstTy =
844 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
845
846 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
847 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
848
849 // If store is legal, just bitcase the src pointer.
850 if (SrcSize == DstSize) {
851 llvm::Value *Casted =
852 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
853 CGF.Builder.CreateStore(Src, Casted);
854 } else {
855 assert(SrcSize > DstSize && "Coercion is missing bits!");
856
857 // Otherwise do coercion through memory. This is stupid, but
858 // simple.
859 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
860 CGF.Builder.CreateStore(Src, Tmp);
861 llvm::Value *Casted =
862 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
863 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
864 }
865}
866
Daniel Dunbar04d35782008-09-17 00:51:38 +0000867/***/
868
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000869bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
870 return getABIReturnInfo(FI.getReturnType(), getTypes()).isStructRet();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000871}
872
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000873const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000874CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000875 std::vector<const llvm::Type*> ArgTys;
876
877 const llvm::Type *ResultType = 0;
878
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000879 QualType RetTy = FI.getReturnType();
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000880 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000881 switch (RetAI.getKind()) {
882 case ABIArgInfo::ByVal:
883 case ABIArgInfo::Expand:
884 assert(0 && "Invalid ABI kind for return argument");
885
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000886 case ABIArgInfo::Default:
887 if (RetTy->isVoidType()) {
888 ResultType = llvm::Type::VoidTy;
889 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000890 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000891 }
892 break;
893
894 case ABIArgInfo::StructRet: {
895 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000896 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000897 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
898 break;
899 }
900
Daniel Dunbar1358b202009-01-26 21:26:08 +0000901 case ABIArgInfo::Ignore:
902 ResultType = llvm::Type::VoidTy;
903 break;
904
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000905 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000906 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000907 break;
908 }
909
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000910 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
911 it != ie; ++it) {
912 ABIArgInfo AI = getABIArgumentInfo(*it, *this);
913 const llvm::Type *Ty = ConvertType(*it);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000914
915 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +0000916 case ABIArgInfo::Ignore:
917 break;
918
Daniel Dunbar04d35782008-09-17 00:51:38 +0000919 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000920 case ABIArgInfo::StructRet:
921 assert(0 && "Invalid ABI kind for non-return argument");
922
923 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000924 // byval arguments are always on the stack, which is addr space #0.
925 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000926 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
927 break;
928
929 case ABIArgInfo::Default:
930 ArgTys.push_back(Ty);
931 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000932
933 case ABIArgInfo::Expand:
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000934 GetExpandedTypes(*it, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000935 break;
936 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000937 }
938
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000939 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000940}
941
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000942void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000943 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +0000944 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000945 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000946 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000947
948 if (TargetDecl) {
949 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000950 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000951 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000952 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000953 if (TargetDecl->getAttr<PureAttr>())
954 FuncAttrs |= llvm::Attribute::ReadOnly;
955 if (TargetDecl->getAttr<ConstAttr>())
956 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000957 }
958
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000959 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000960 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000961 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000962 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000963 case ABIArgInfo::Default:
964 if (RetTy->isPromotableIntegerType()) {
965 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000966 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000967 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000968 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000969 }
970 }
971 break;
972
973 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000974 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +0000975 llvm::Attribute::StructRet |
976 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000977 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000978 break;
979
Daniel Dunbar1358b202009-01-26 21:26:08 +0000980 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000981 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000982 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000983
984 case ABIArgInfo::ByVal:
985 case ABIArgInfo::Expand:
986 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000987 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000988
Devang Patel2bb6eb82008-09-26 22:53:57 +0000989 if (RetAttrs)
990 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000991 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
992 it != ie; ++it) {
993 QualType ParamType = *it;
Devang Patela85a9ef2008-09-25 21:02:23 +0000994 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000995 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000996
997 switch (AI.getKind()) {
998 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000999 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001000 assert(0 && "Invalid ABI kind for non-return argument");
1001
1002 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +00001003 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001004 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1005 break;
1006
1007 case ABIArgInfo::Default:
1008 if (ParamType->isPromotableIntegerType()) {
1009 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001010 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001011 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001012 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001013 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001014 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001015 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001016
Daniel Dunbar1358b202009-01-26 21:26:08 +00001017 case ABIArgInfo::Ignore:
1018 // Skip increment, no matching LLVM parameter.
1019 continue;
1020
Daniel Dunbar04d35782008-09-17 00:51:38 +00001021 case ABIArgInfo::Expand: {
1022 std::vector<const llvm::Type*> Tys;
1023 // FIXME: This is rather inefficient. Do we ever actually need
1024 // to do anything here? The result should be just reconstructed
1025 // on the other side, so extension should be a non-issue.
1026 getTypes().GetExpandedTypes(ParamType, Tys);
1027 Index += Tys.size();
1028 continue;
1029 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001030 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001031
Devang Patela85a9ef2008-09-25 21:02:23 +00001032 if (Attributes)
1033 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001034 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001035 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001036 if (FuncAttrs)
1037 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1038
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001039}
1040
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001041void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1042 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001043 const FunctionArgList &Args) {
1044 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1045 llvm::Function::arg_iterator AI = Fn->arg_begin();
1046
1047 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001048 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001049 AI->setName("agg.result");
1050 ++AI;
1051 }
1052
1053 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001054 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001055 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001056 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001057 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001058
1059 switch (ArgI.getKind()) {
1060 case ABIArgInfo::ByVal:
1061 case ABIArgInfo::Default: {
1062 assert(AI != Fn->arg_end() && "Argument mismatch!");
1063 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001064 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001065 // This must be a promotion, for something like
1066 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +00001067 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001068 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001069 EmitParmDecl(*Arg, V);
1070 break;
1071 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001072
1073 case ABIArgInfo::Expand: {
1074 // If this was structure was expand into multiple arguments then
1075 // we need to create a temporary and reconstruct it from the
1076 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001077 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001078 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1079 (Name + ".addr").c_str());
1080 // FIXME: What are the right qualifiers here?
1081 llvm::Function::arg_iterator End =
1082 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1083 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001084
Daniel Dunbar04d35782008-09-17 00:51:38 +00001085 // Name the arguments used in expansion and increment AI.
1086 unsigned Index = 0;
1087 for (; AI != End; ++AI, ++Index)
1088 AI->setName(Name + "." + llvm::utostr(Index));
1089 continue;
1090 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001091
1092 case ABIArgInfo::Ignore:
1093 break;
1094
Daniel Dunbar22e30052008-09-11 01:48:57 +00001095 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001096 case ABIArgInfo::StructRet:
1097 assert(0 && "Invalid ABI kind for non-return argument");
1098 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001099
1100 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001101 }
1102 assert(AI == Fn->arg_end() && "Argument mismatch!");
1103}
1104
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001105void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001106 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001107 llvm::Value *RV = 0;
1108
1109 // Functions with no result always return void.
1110 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001111 QualType RetTy = FI.getReturnType();
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001112 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001113
1114 switch (RetAI.getKind()) {
1115 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001116 if (RetTy->isAnyComplexType()) {
1117 // FIXME: Volatile
1118 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1119 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1120 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1121 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1122 } else {
1123 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1124 CurFn->arg_begin());
1125 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001126 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001127
Daniel Dunbare126ab12008-09-10 02:41:04 +00001128 case ABIArgInfo::Default:
1129 RV = Builder.CreateLoad(ReturnValue);
1130 break;
1131
Daniel Dunbar1358b202009-01-26 21:26:08 +00001132 case ABIArgInfo::Ignore:
1133 break;
1134
Daniel Dunbar73d66602008-09-10 07:04:09 +00001135 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001136 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001137 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001138 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001139
1140 case ABIArgInfo::ByVal:
1141 case ABIArgInfo::Expand:
1142 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001143 }
1144 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001145
1146 if (RV) {
1147 Builder.CreateRet(RV);
1148 } else {
1149 Builder.CreateRetVoid();
1150 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001151}
1152
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001153RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1154 llvm::Value *Callee,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001155 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001156 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001157
1158 // Handle struct-return functions by passing a pointer to the
1159 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001160 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001161 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001162 switch (RetAI.getKind()) {
1163 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001164 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001165 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001166 break;
1167
1168 case ABIArgInfo::Default:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001169 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001170 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001171 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001172
1173 case ABIArgInfo::ByVal:
1174 case ABIArgInfo::Expand:
1175 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001176 }
1177
1178 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1179 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001180 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001181 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001182
1183 switch (ArgInfo.getKind()) {
1184 case ABIArgInfo::ByVal: // Default is byval
1185 case ABIArgInfo::Default:
1186 if (RV.isScalar()) {
1187 Args.push_back(RV.getScalarVal());
1188 } else if (RV.isComplex()) {
1189 // Make a temporary alloca to pass the argument.
1190 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1191 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1192 } else {
1193 Args.push_back(RV.getAggregateAddr());
1194 }
1195 break;
1196
Daniel Dunbar1358b202009-01-26 21:26:08 +00001197 case ABIArgInfo::Ignore:
1198 break;
1199
Daniel Dunbar04d35782008-09-17 00:51:38 +00001200 case ABIArgInfo::StructRet:
1201 case ABIArgInfo::Coerce:
1202 assert(0 && "Invalid ABI kind for non-return argument");
1203 break;
1204
1205 case ABIArgInfo::Expand:
1206 ExpandTypeToArgs(I->second, RV, Args);
1207 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001208 }
1209 }
1210
1211 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001212
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001213 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001214 CodeGen::AttributeListType AttributeList;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001215 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patela85a9ef2008-09-25 21:02:23 +00001216 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001217 AttributeList.size()));
1218
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001219 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1220 CI->setCallingConv(F->getCallingConv());
1221 if (CI->getType() != llvm::Type::VoidTy)
1222 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001223
1224 switch (RetAI.getKind()) {
1225 case ABIArgInfo::StructRet:
1226 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001227 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001228 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001229 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001230 else
1231 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001232
Daniel Dunbare126ab12008-09-10 02:41:04 +00001233 case ABIArgInfo::Default:
1234 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1235
Daniel Dunbar1358b202009-01-26 21:26:08 +00001236 case ABIArgInfo::Ignore:
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001237 if (RetTy->isVoidType())
1238 return RValue::get(0);
1239 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1240 llvm::Value *Res =
1241 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1242 return RValue::getAggregate(Res);
1243 }
1244 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001245
Daniel Dunbar73d66602008-09-10 07:04:09 +00001246 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001247 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1248 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001249 if (RetTy->isAnyComplexType())
1250 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001251 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001252 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001253 else
1254 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001255 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001256
1257 case ABIArgInfo::ByVal:
1258 case ABIArgInfo::Expand:
1259 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001260 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001261
1262 assert(0 && "Unhandled ABIArgInfo::Kind");
1263 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001264}