blob: fbaffad73fd3942230f450da8b4ff1cd66510ebf [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 Dunbar9f4874e2009-02-04 23:24:38 +000026#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000027#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000028
29#include "ABIInfo.h"
30
Daniel Dunbara8f02052008-09-08 21:33:45 +000031using namespace clang;
32using namespace CodeGen;
33
34/***/
35
Daniel Dunbara8f02052008-09-08 21:33:45 +000036// FIXME: Use iterator and sidestep silly type array creation.
37
Daniel Dunbar34bda882009-02-02 23:23:47 +000038const
39CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
40 return getFunctionInfo(FTNP->getResultType(),
41 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000042}
43
Daniel Dunbar34bda882009-02-02 23:23:47 +000044const
45CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
46 llvm::SmallVector<QualType, 16> ArgTys;
47 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000048 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000049 ArgTys.push_back(FTP->getArgType(i));
50 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000051}
52
Daniel Dunbar34bda882009-02-02 23:23:47 +000053const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000054 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar34bda882009-02-02 23:23:47 +000055 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
56 return getFunctionInfo(FTP);
57 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000058}
59
Daniel Dunbar34bda882009-02-02 23:23:47 +000060const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
61 llvm::SmallVector<QualType, 16> ArgTys;
62 ArgTys.push_back(MD->getSelfDecl()->getType());
63 ArgTys.push_back(Context.getObjCSelType());
64 // FIXME: Kill copy?
Daniel Dunbara8f02052008-09-08 21:33:45 +000065 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
66 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000067 ArgTys.push_back((*i)->getType());
68 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000069}
70
Daniel Dunbar34bda882009-02-02 23:23:47 +000071const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
72 const CallArgList &Args) {
73 // FIXME: Kill copy.
74 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000075 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
76 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000077 ArgTys.push_back(i->second);
78 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000079}
80
Daniel Dunbar34bda882009-02-02 23:23:47 +000081const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
82 const FunctionArgList &Args) {
83 // FIXME: Kill copy.
84 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +000085 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
86 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000087 ArgTys.push_back(i->second);
88 return getFunctionInfo(ResTy, ArgTys);
89}
90
91const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
92 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +000093 // Lookup or create unique function info.
94 llvm::FoldingSetNodeID ID;
95 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
96
97 void *InsertPos = 0;
98 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
99 if (FI)
100 return *FI;
101
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000102 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000103 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000104 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000105
106 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000107 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000108
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000109 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000110}
111
112/***/
113
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000114ABIInfo::~ABIInfo() {}
115
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000116void ABIArgInfo::dump() const {
117 fprintf(stderr, "(ABIArgInfo Kind=");
118 switch (TheKind) {
119 case Direct:
120 fprintf(stderr, "Direct");
121 break;
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000122 case Ignore:
123 fprintf(stderr, "Ignore");
124 break;
125 case Coerce:
126 fprintf(stderr, "Coerce Type=");
127 getCoerceToType()->print(llvm::errs());
128 // FIXME: This is ridiculous.
129 llvm::errs().flush();
130 break;
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000131 case Indirect:
132 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000133 break;
134 case Expand:
135 fprintf(stderr, "Expand");
136 break;
137 }
138 fprintf(stderr, ")\n");
139}
140
141/***/
142
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000143/// isEmptyStruct - Return true iff a structure has no non-empty
144/// members. Note that a structure with a flexible array member is not
145/// considered empty.
146static bool isEmptyStruct(QualType T) {
147 const RecordType *RT = T->getAsStructureType();
148 if (!RT)
149 return 0;
150 const RecordDecl *RD = RT->getDecl();
151 if (RD->hasFlexibleArrayMember())
152 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000153 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000154 e = RD->field_end(); i != e; ++i) {
155 const FieldDecl *FD = *i;
156 if (!isEmptyStruct(FD->getType()))
157 return false;
158 }
159 return true;
160}
161
162/// isSingleElementStruct - Determine if a structure is a "single
163/// element struct", i.e. it has exactly one non-empty field or
164/// exactly one field which is itself a single element
165/// struct. Structures with flexible array members are never
166/// considered single element structs.
167///
168/// \return The field declaration for the single non-empty field, if
169/// it exists.
170static const FieldDecl *isSingleElementStruct(QualType T) {
171 const RecordType *RT = T->getAsStructureType();
172 if (!RT)
173 return 0;
174
175 const RecordDecl *RD = RT->getDecl();
176 if (RD->hasFlexibleArrayMember())
177 return 0;
178
179 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000180 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000181 e = RD->field_end(); i != e; ++i) {
182 const FieldDecl *FD = *i;
183 QualType FT = FD->getType();
184
185 if (isEmptyStruct(FT)) {
186 // Ignore
187 } else if (Found) {
188 return 0;
189 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
190 Found = FD;
191 } else {
192 Found = isSingleElementStruct(FT);
193 if (!Found)
194 return 0;
195 }
196 }
197
198 return Found;
199}
200
201static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
202 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
203 return false;
204
205 uint64_t Size = Context.getTypeSize(Ty);
206 return Size == 32 || Size == 64;
207}
208
209static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
210 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000212 e = RD->field_end(); i != e; ++i) {
213 const FieldDecl *FD = *i;
214
215 if (!is32Or64BitBasicType(FD->getType(), Context))
216 return false;
217
218 // If this is a bit-field we need to make sure it is still a
219 // 32-bit or 64-bit type.
220 if (Expr *BW = FD->getBitWidth()) {
221 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
222 if (Width <= 16)
223 return false;
224 }
225 }
226 return true;
227}
228
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000229namespace {
230/// DefaultABIInfo - The default implementation for ABI specific
231/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000232/// self-consistent and sensible LLVM IR generation, but does not
233/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000234class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000235 ABIArgInfo classifyReturnType(QualType RetTy,
236 ASTContext &Context) const;
237
238 ABIArgInfo classifyArgumentType(QualType RetTy,
239 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000240
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000241 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
242 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
243 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
244 it != ie; ++it)
245 it->info = classifyArgumentType(it->type, Context);
246 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000247};
248
249/// X86_32ABIInfo - The X86-32 ABI information.
250class X86_32ABIInfo : public ABIInfo {
251public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000252 ABIArgInfo classifyReturnType(QualType RetTy,
253 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000254
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000255 ABIArgInfo classifyArgumentType(QualType RetTy,
256 ASTContext &Context) const;
257
258 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
259 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
260 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
261 it != ie; ++it)
262 it->info = classifyArgumentType(it->type, Context);
263 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000264};
265}
266
267ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
268 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000269 if (RetTy->isVoidType()) {
270 return ABIArgInfo::getIgnore();
271 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000272 // Classify "single element" structs as their element type.
273 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
274 if (SeltFD) {
275 QualType SeltTy = SeltFD->getType()->getDesugaredType();
276 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
277 // FIXME: This is gross, it would be nice if we could just
278 // pass back SeltTy and have clients deal with it. Is it worth
279 // supporting coerce to both LLVM and clang Types?
280 if (BT->isIntegerType()) {
281 uint64_t Size = Context.getTypeSize(SeltTy);
282 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
283 } else if (BT->getKind() == BuiltinType::Float) {
284 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
285 } else if (BT->getKind() == BuiltinType::Double) {
286 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
287 }
288 } else if (SeltTy->isPointerType()) {
289 // FIXME: It would be really nice if this could come out as
290 // the proper pointer type.
291 llvm::Type *PtrTy =
292 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
293 return ABIArgInfo::getCoerce(PtrTy);
294 }
295 }
296
Daniel Dunbar73d66602008-09-10 07:04:09 +0000297 uint64_t Size = Context.getTypeSize(RetTy);
298 if (Size == 8) {
299 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
300 } else if (Size == 16) {
301 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
302 } else if (Size == 32) {
303 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
304 } else if (Size == 64) {
305 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
306 } else {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000307 return ABIArgInfo::getIndirect(0);
Daniel Dunbar73d66602008-09-10 07:04:09 +0000308 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000309 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000310 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000311 }
312}
313
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000314ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
315 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000316 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000317 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000318 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000319 if (const RecordType *RT = Ty->getAsStructureType())
320 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000321 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000322
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000323 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000324 uint64_t Size = Context.getTypeSize(Ty);
325 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000326 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000327
328 // Expand structs with size <= 128-bits which consist only of
329 // basic types (int, long long, float, double, xxx*). This is
330 // non-recursive and does not ignore empty fields.
331 if (const RecordType *RT = Ty->getAsStructureType()) {
332 if (Context.getTypeSize(Ty) <= 4*32 &&
333 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
334 return ABIArgInfo::getExpand();
335 }
336
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000337 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000338 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000339 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000340 }
341}
342
Daniel Dunbare09a9692009-01-24 08:32:22 +0000343namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000344/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000345class X86_64ABIInfo : public ABIInfo {
346 enum Class {
347 Integer = 0,
348 SSE,
349 SSEUp,
350 X87,
351 X87Up,
352 ComplexX87,
353 NoClass,
354 Memory
355 };
356
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000357 /// merge - Implement the X86_64 ABI merging algorithm.
358 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000359 /// Merge an accumulating classification \arg Accum with a field
360 /// classification \arg Field.
361 ///
362 /// \param Accum - The accumulating classification. This should
363 /// always be either NoClass or the result of a previous merge
364 /// call. In addition, this should never be Memory (the caller
365 /// should just return Memory for the aggregate).
366 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000367
Daniel Dunbare09a9692009-01-24 08:32:22 +0000368 /// classify - Determine the x86_64 register classes in which the
369 /// given type T should be passed.
370 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000371 /// \param Lo - The classification for the parts of the type
372 /// residing in the low word of the containing object.
373 ///
374 /// \param Hi - The classification for the parts of the type
375 /// residing in the high word of the containing object.
376 ///
377 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000378 /// containing object. Some parameters are classified different
379 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000380 ///
381 /// If a word is unused its result will be NoClass; if a type should
382 /// be passed in Memory then at least the classification of \arg Lo
383 /// will be Memory.
384 ///
385 /// The \arg Lo class will be NoClass iff the argument is ignored.
386 ///
387 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
388 /// be NoClass.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000389 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000390 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000391
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000392 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000393 ASTContext &Context) const;
394
395 ABIArgInfo classifyArgumentType(QualType Ty,
396 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000397 unsigned &neededInt,
398 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000399
400public:
401 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000402};
403}
404
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000405X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
406 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000407 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
408 // classified recursively so that always two fields are
409 // considered. The resulting class is calculated according to
410 // the classes of the fields in the eightbyte:
411 //
412 // (a) If both classes are equal, this is the resulting class.
413 //
414 // (b) If one of the classes is NO_CLASS, the resulting class is
415 // the other class.
416 //
417 // (c) If one of the classes is MEMORY, the result is the MEMORY
418 // class.
419 //
420 // (d) If one of the classes is INTEGER, the result is the
421 // INTEGER.
422 //
423 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
424 // MEMORY is used as class.
425 //
426 // (f) Otherwise class SSE is used.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000427 assert((Accum == NoClass || Accum == Integer ||
428 Accum == SSE || Accum == SSEUp) &&
429 "Invalid accumulated classification during merge.");
430 if (Accum == Field || Field == NoClass)
431 return Accum;
432 else if (Field == Memory)
433 return Memory;
434 else if (Accum == NoClass)
435 return Field;
436 else if (Accum == Integer || Field == Integer)
437 return Integer;
438 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
439 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000440 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000441 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000442}
443
Daniel Dunbare09a9692009-01-24 08:32:22 +0000444void X86_64ABIInfo::classify(QualType Ty,
445 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000446 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000447 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000448 // FIXME: This code can be simplified by introducing a simple value
449 // class for Class pairs with appropriate constructor methods for
450 // the various situations.
451
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000452 Lo = Hi = NoClass;
453
454 Class &Current = OffsetBase < 64 ? Lo : Hi;
455 Current = Memory;
456
Daniel Dunbare09a9692009-01-24 08:32:22 +0000457 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
458 BuiltinType::Kind k = BT->getKind();
459
Daniel Dunbar1358b202009-01-26 21:26:08 +0000460 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000461 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000462 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000463 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000464 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000465 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000466 } else if (k == BuiltinType::LongDouble) {
467 Lo = X87;
468 Hi = X87Up;
469 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000470 // FIXME: _Decimal32 and _Decimal64 are SSE.
471 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000472 // FIXME: __int128 is (Integer, Integer).
473 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
474 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000475 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000476 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000477 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000478 if (Size == 64) {
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000479 // gcc passes <1 x double> in memory.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000480 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000481 return;
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000482
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000483 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000484
485 // If this type crosses an eightbyte boundary, it should be
486 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000487 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000488 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000489 } else if (Size == 128) {
490 Lo = SSE;
491 Hi = SSEUp;
492 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000493 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
494 QualType ET = CT->getElementType();
495
Daniel Dunbare413f532009-01-30 18:40:10 +0000496 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000497 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000498 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000499 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000500 else if (Size <= 128)
501 Lo = Hi = Integer;
502 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000503 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000504 else if (ET == Context.DoubleTy)
505 Lo = Hi = SSE;
506 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000507 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000508
509 // If this complex type crosses an eightbyte boundary then it
510 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000511 uint64_t EB_Real = (OffsetBase) / 64;
512 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000513 if (Hi == NoClass && EB_Real != EB_Imag)
514 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000515 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
516 // Arrays are treated like structures.
517
518 uint64_t Size = Context.getTypeSize(Ty);
519
520 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
521 // than two eightbytes, ..., it has class MEMORY.
522 if (Size > 128)
523 return;
524
525 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
526 // fields, it has class MEMORY.
527 //
528 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000529 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000530 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000531
532 // Otherwise implement simplified merge. We could be smarter about
533 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000534 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000535 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
536 uint64_t ArraySize = AT->getSize().getZExtValue();
537 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
538 Class FieldLo, FieldHi;
539 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000540 Lo = merge(Lo, FieldLo);
541 Hi = merge(Hi, FieldHi);
542 if (Lo == Memory || Hi == Memory)
543 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000544 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000545
546 // Do post merger cleanup (see below). Only case we worry about is Memory.
547 if (Hi == Memory)
548 Lo = Memory;
549 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000550 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000551 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000552
553 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
554 // than two eightbytes, ..., it has class MEMORY.
555 if (Size > 128)
556 return;
557
558 const RecordDecl *RD = RT->getDecl();
559
560 // Assume variable sized types are passed in memory.
561 if (RD->hasFlexibleArrayMember())
562 return;
563
564 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
565
566 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000567 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000568 unsigned idx = 0;
569 for (RecordDecl::field_iterator i = RD->field_begin(),
570 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000571 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000572
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000573 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
574 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000575 if (Offset % Context.getTypeAlign(i->getType())) {
576 Lo = Memory;
577 return;
578 }
579
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000580 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000581 //
582 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
583 // exceeds a single eightbyte, each is classified
584 // separately. Each eightbyte gets initialized to class
585 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000586 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000587 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000588 Lo = merge(Lo, FieldLo);
589 Hi = merge(Hi, FieldHi);
590 if (Lo == Memory || Hi == Memory)
591 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000592 }
593
594 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
595 //
596 // (a) If one of the classes is MEMORY, the whole argument is
597 // passed in memory.
598 //
599 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
600
601 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000602 // the merge (just bail).
603 //
604 // The second condition occurs in the case of unions; for example
605 // union { _Complex double; unsigned; }.
606 if (Hi == Memory)
607 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000608 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000609 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000610 }
611}
612
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000613
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000614ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
615 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000616 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
617 // classification algorithm.
618 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000619 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000620
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000621 // Check some invariants.
622 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
623 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
624 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
625
Daniel Dunbare09a9692009-01-24 08:32:22 +0000626 const llvm::Type *ResType = 0;
627 switch (Lo) {
628 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000629 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000630
631 case SSEUp:
632 case X87Up:
633 assert(0 && "Invalid classification for lo word.");
634
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000635 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000636 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000637 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000638 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000639
640 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
641 // available register of the sequence %rax, %rdx is used.
642 case Integer:
643 ResType = llvm::Type::Int64Ty; break;
644
645 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
646 // available SSE register of the sequence %xmm0, %xmm1 is used.
647 case SSE:
648 ResType = llvm::Type::DoubleTy; break;
649
650 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
651 // returned on the X87 stack in %st0 as 80-bit x87 number.
652 case X87:
653 ResType = llvm::Type::X86_FP80Ty; break;
654
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000655 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
656 // part of the value is returned in %st0 and the imaginary part in
657 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000658 case ComplexX87:
659 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
660 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
661 break;
662 }
663
664 switch (Hi) {
665 // Memory was handled previously, and ComplexX87 and X87 should
666 // never occur as hi classes.
667 case Memory:
668 case X87:
669 case ComplexX87:
670 assert(0 && "Invalid classification for hi word.");
671
672 case NoClass: break;
673 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000674 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
675 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000676 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000677 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
678 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000679
680 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
681 // is passed in the upper half of the last used SSE register.
682 //
683 // SSEUP should always be preceeded by SSE, just widen.
684 case SSEUp:
685 assert(Lo == SSE && "Unexpected SSEUp classification.");
686 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
687 break;
688
689 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000690 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000691 //
692 // X87UP should always be preceeded by X87, so we don't need to do
693 // anything here.
694 case X87Up:
695 assert(Lo == X87 && "Unexpected X87Up classification.");
696 break;
697 }
698
699 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000700}
701
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000702ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000703 unsigned &neededInt,
704 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000705 X86_64ABIInfo::Class Lo, Hi;
706 classify(Ty, Context, 0, Lo, Hi);
707
708 // Check some invariants.
709 // FIXME: Enforce these by construction.
710 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
711 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
712 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
713
Daniel Dunbare978cb92009-02-10 17:06:09 +0000714 neededInt = 0;
715 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000716 const llvm::Type *ResType = 0;
717 switch (Lo) {
718 case NoClass:
719 return ABIArgInfo::getIgnore();
720
721 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
722 // on the stack.
723 case Memory:
724
725 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
726 // COMPLEX_X87, it is passed in memory.
727 case X87:
728 case ComplexX87:
729 // Choose appropriate in memory type.
730 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000731 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000732 else
733 return ABIArgInfo::getDirect();
734
735 case SSEUp:
736 case X87Up:
737 assert(0 && "Invalid classification for lo word.");
738
739 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
740 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
741 // and %r9 is used.
742 case Integer:
743 ++neededInt;
744 ResType = llvm::Type::Int64Ty;
745 break;
746
747 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
748 // available SSE register is used, the registers are taken in the
749 // order from %xmm0 to %xmm7.
750 case SSE:
751 ++neededSSE;
752 ResType = llvm::Type::DoubleTy;
753 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000754 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000755
756 switch (Hi) {
757 // Memory was handled previously, ComplexX87 and X87 should
758 // never occur as hi classes, and X87Up must be preceed by X87,
759 // which is passed in memory.
760 case Memory:
761 case X87:
762 case X87Up:
763 case ComplexX87:
764 assert(0 && "Invalid classification for hi word.");
765
766 case NoClass: break;
767 case Integer:
768 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
769 ++neededInt;
770 break;
771 case SSE:
772 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
773 ++neededSSE;
774 break;
775
776 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
777 // eightbyte is passed in the upper half of the last used SSE
778 // register.
779 case SSEUp:
780 assert(Lo == SSE && "Unexpected SSEUp classification.");
781 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
782 break;
783 }
784
Daniel Dunbare978cb92009-02-10 17:06:09 +0000785 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000786}
787
788void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
789 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
790
791 // Keep track of the number of assigned registers.
792 unsigned freeIntRegs = 6, freeSSERegs = 8;
793
794 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
795 // get assigned (in left-to-right order) for passing as follows...
796 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +0000797 it != ie; ++it) {
798 unsigned neededInt, neededSSE;
799 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
800
801 // AMD64-ABI 3.2.3p3: If there are no registers available for any
802 // eightbyte of an argument, the whole argument is passed on the
803 // stack. If registers have already been assigned for some
804 // eightbytes of such an argument, the assignments get reverted.
805 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
806 freeIntRegs -= neededInt;
807 freeSSERegs -= neededSSE;
808 } else {
809 // Choose appropriate in memory type.
810 if (CodeGenFunction::hasAggregateLLVMType(it->type))
811 it->info = ABIArgInfo::getIndirect(0);
812 else
813 it->info = ABIArgInfo::getDirect();
814 }
815 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000816}
817
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000818ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
819 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000820 if (RetTy->isVoidType()) {
821 return ABIArgInfo::getIgnore();
822 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000823 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +0000824 } else {
825 return ABIArgInfo::getDirect();
826 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000827}
828
829ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
830 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000831 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000832 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +0000833 } else {
834 return ABIArgInfo::getDirect();
835 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000836}
837
838const ABIInfo &CodeGenTypes::getABIInfo() const {
839 if (TheABIInfo)
840 return *TheABIInfo;
841
842 // For now we just cache this in the CodeGenTypes and don't bother
843 // to free it.
844 const char *TargetPrefix = getContext().Target.getTargetPrefix();
845 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000846 switch (getContext().Target.getPointerWidth(0)) {
847 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000848 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000849 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +0000850 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000851 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000852 }
853
854 return *(TheABIInfo = new DefaultABIInfo);
855}
856
Daniel Dunbare126ab12008-09-10 02:41:04 +0000857/***/
858
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000859CGFunctionInfo::CGFunctionInfo(QualType ResTy,
860 const llvm::SmallVector<QualType, 16> &ArgTys) {
861 NumArgs = ArgTys.size();
862 Args = new ArgInfo[1 + NumArgs];
863 Args[0].type = ResTy;
864 for (unsigned i = 0; i < NumArgs; ++i)
865 Args[1 + i].type = ArgTys[i];
866}
867
868/***/
869
Daniel Dunbar04d35782008-09-17 00:51:38 +0000870void CodeGenTypes::GetExpandedTypes(QualType Ty,
871 std::vector<const llvm::Type*> &ArgTys) {
872 const RecordType *RT = Ty->getAsStructureType();
873 assert(RT && "Can only expand structure types.");
874 const RecordDecl *RD = RT->getDecl();
875 assert(!RD->hasFlexibleArrayMember() &&
876 "Cannot expand structure with flexible array.");
877
Douglas Gregor5d764842009-01-09 17:18:27 +0000878 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000879 e = RD->field_end(); i != e; ++i) {
880 const FieldDecl *FD = *i;
881 assert(!FD->isBitField() &&
882 "Cannot expand structure with bit-field members.");
883
884 QualType FT = FD->getType();
885 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
886 GetExpandedTypes(FT, ArgTys);
887 } else {
888 ArgTys.push_back(ConvertType(FT));
889 }
890 }
891}
892
893llvm::Function::arg_iterator
894CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
895 llvm::Function::arg_iterator AI) {
896 const RecordType *RT = Ty->getAsStructureType();
897 assert(RT && "Can only expand structure types.");
898
899 RecordDecl *RD = RT->getDecl();
900 assert(LV.isSimple() &&
901 "Unexpected non-simple lvalue during struct expansion.");
902 llvm::Value *Addr = LV.getAddress();
903 for (RecordDecl::field_iterator i = RD->field_begin(),
904 e = RD->field_end(); i != e; ++i) {
905 FieldDecl *FD = *i;
906 QualType FT = FD->getType();
907
908 // FIXME: What are the right qualifiers here?
909 LValue LV = EmitLValueForField(Addr, FD, false, 0);
910 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
911 AI = ExpandTypeFromArgs(FT, LV, AI);
912 } else {
913 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
914 ++AI;
915 }
916 }
917
918 return AI;
919}
920
921void
922CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
923 llvm::SmallVector<llvm::Value*, 16> &Args) {
924 const RecordType *RT = Ty->getAsStructureType();
925 assert(RT && "Can only expand structure types.");
926
927 RecordDecl *RD = RT->getDecl();
928 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
929 llvm::Value *Addr = RV.getAggregateAddr();
930 for (RecordDecl::field_iterator i = RD->field_begin(),
931 e = RD->field_end(); i != e; ++i) {
932 FieldDecl *FD = *i;
933 QualType FT = FD->getType();
934
935 // FIXME: What are the right qualifiers here?
936 LValue LV = EmitLValueForField(Addr, FD, false, 0);
937 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
938 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
939 } else {
940 RValue RV = EmitLoadOfLValue(LV, FT);
941 assert(RV.isScalar() &&
942 "Unexpected non-scalar rvalue during struct expansion.");
943 Args.push_back(RV.getScalarVal());
944 }
945 }
946}
947
Daniel Dunbar84379912009-02-02 19:06:38 +0000948/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
949/// a pointer to an object of type \arg Ty.
950///
951/// This safely handles the case when the src type is smaller than the
952/// destination type; in this situation the values of bits which not
953/// present in the src are undefined.
954static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
955 const llvm::Type *Ty,
956 CodeGenFunction &CGF) {
957 const llvm::Type *SrcTy =
958 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
959 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
960 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
961
Daniel Dunbar77071992009-02-03 05:59:18 +0000962 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +0000963 if (SrcSize == DstSize) {
964 llvm::Value *Casted =
965 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +0000966 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
967 // FIXME: Use better alignment / avoid requiring aligned load.
968 Load->setAlignment(1);
969 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +0000970 } else {
971 assert(SrcSize < DstSize && "Coercion is losing source bits!");
972
973 // Otherwise do coercion through memory. This is stupid, but
974 // simple.
975 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
976 llvm::Value *Casted =
977 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +0000978 llvm::StoreInst *Store =
979 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
980 // FIXME: Use better alignment / avoid requiring aligned store.
981 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +0000982 return CGF.Builder.CreateLoad(Tmp);
983 }
984}
985
986/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
987/// where the source and destination may have different types.
988///
989/// This safely handles the case when the src type is larger than the
990/// destination type; the upper bits of the src will be lost.
991static void CreateCoercedStore(llvm::Value *Src,
992 llvm::Value *DstPtr,
993 CodeGenFunction &CGF) {
994 const llvm::Type *SrcTy = Src->getType();
995 const llvm::Type *DstTy =
996 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
997
998 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
999 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1000
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001001 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001002 if (SrcSize == DstSize) {
1003 llvm::Value *Casted =
1004 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001005 // FIXME: Use better alignment / avoid requiring aligned store.
1006 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001007 } else {
1008 assert(SrcSize > DstSize && "Coercion is missing bits!");
1009
1010 // Otherwise do coercion through memory. This is stupid, but
1011 // simple.
1012 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1013 CGF.Builder.CreateStore(Src, Tmp);
1014 llvm::Value *Casted =
1015 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001016 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1017 // FIXME: Use better alignment / avoid requiring aligned load.
1018 Load->setAlignment(1);
1019 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001020 }
1021}
1022
Daniel Dunbar04d35782008-09-17 00:51:38 +00001023/***/
1024
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001025bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001026 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001027}
1028
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001029const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001030CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001031 std::vector<const llvm::Type*> ArgTys;
1032
1033 const llvm::Type *ResultType = 0;
1034
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001035 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001036 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001037 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001038 case ABIArgInfo::Expand:
1039 assert(0 && "Invalid ABI kind for return argument");
1040
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001041 case ABIArgInfo::Direct:
1042 ResultType = ConvertType(RetTy);
1043 break;
1044
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001045 case ABIArgInfo::Indirect: {
1046 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001047 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001048 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001049 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1050 break;
1051 }
1052
Daniel Dunbar1358b202009-01-26 21:26:08 +00001053 case ABIArgInfo::Ignore:
1054 ResultType = llvm::Type::VoidTy;
1055 break;
1056
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001057 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001058 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001059 break;
1060 }
1061
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001062 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1063 ie = FI.arg_end(); it != ie; ++it) {
1064 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001065
1066 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001067 case ABIArgInfo::Ignore:
1068 break;
1069
Daniel Dunbar04d35782008-09-17 00:51:38 +00001070 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001071 ArgTys.push_back(AI.getCoerceToType());
1072 break;
1073
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001074 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001075 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001076 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1077 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001078 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001079 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001080
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001081 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001082 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001083 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001084
1085 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001086 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001087 break;
1088 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001089 }
1090
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001091 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001092}
1093
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001094void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001095 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001096 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001097 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001098 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001099
1100 if (TargetDecl) {
1101 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001102 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001103 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001104 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001105 if (TargetDecl->getAttr<PureAttr>())
1106 FuncAttrs |= llvm::Attribute::ReadOnly;
1107 if (TargetDecl->getAttr<ConstAttr>())
1108 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001109 }
1110
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001111 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001112 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001113 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001114 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001115 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001116 if (RetTy->isPromotableIntegerType()) {
1117 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001118 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001119 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001120 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001121 }
1122 }
1123 break;
1124
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001125 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001126 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001127 llvm::Attribute::StructRet |
1128 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001129 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001130 break;
1131
Daniel Dunbar1358b202009-01-26 21:26:08 +00001132 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001133 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001134 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001135
Daniel Dunbar22e30052008-09-11 01:48:57 +00001136 case ABIArgInfo::Expand:
1137 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001138 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001139
Devang Patel2bb6eb82008-09-26 22:53:57 +00001140 if (RetAttrs)
1141 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001142 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1143 ie = FI.arg_end(); it != ie; ++it) {
1144 QualType ParamType = it->type;
1145 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001146 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001147
1148 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001149 case ABIArgInfo::Coerce:
1150 break;
1151
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001152 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001153 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarb3f651a2009-02-05 01:31:19 +00001154 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001155 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001156 break;
1157
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001158 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001159 if (ParamType->isPromotableIntegerType()) {
1160 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001161 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001162 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001163 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001164 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001165 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001166 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001167
Daniel Dunbar1358b202009-01-26 21:26:08 +00001168 case ABIArgInfo::Ignore:
1169 // Skip increment, no matching LLVM parameter.
1170 continue;
1171
Daniel Dunbar04d35782008-09-17 00:51:38 +00001172 case ABIArgInfo::Expand: {
1173 std::vector<const llvm::Type*> Tys;
1174 // FIXME: This is rather inefficient. Do we ever actually need
1175 // to do anything here? The result should be just reconstructed
1176 // on the other side, so extension should be a non-issue.
1177 getTypes().GetExpandedTypes(ParamType, Tys);
1178 Index += Tys.size();
1179 continue;
1180 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001181 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001182
Devang Patela85a9ef2008-09-25 21:02:23 +00001183 if (Attributes)
1184 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001185 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001186 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001187 if (FuncAttrs)
1188 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1189
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001190}
1191
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001192void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1193 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001194 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001195 // FIXME: We no longer need the types from FunctionArgList; lift up
1196 // and simplify.
1197
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001198 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1199 llvm::Function::arg_iterator AI = Fn->arg_begin();
1200
1201 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001202 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001203 AI->setName("agg.result");
1204 ++AI;
1205 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001206
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001207 assert(FI.arg_size() == Args.size() &&
1208 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001209 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001210 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001211 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001212 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001213 QualType Ty = info_it->type;
1214 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001215
1216 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001217 case ABIArgInfo::Indirect: {
1218 llvm::Value* V = AI;
1219 if (hasAggregateLLVMType(Ty)) {
1220 // Do nothing, aggregates and complex variables are accessed by
1221 // reference.
1222 } else {
1223 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001224 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001225 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1226 // This must be a promotion, for something like
1227 // "void a(x) short x; {..."
1228 V = EmitScalarConversion(V, Ty, Arg->getType());
1229 }
1230 }
1231 EmitParmDecl(*Arg, V);
1232 break;
1233 }
1234
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001235 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001236 assert(AI != Fn->arg_end() && "Argument mismatch!");
1237 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001238 if (hasAggregateLLVMType(Ty)) {
1239 // Create a temporary alloca to hold the argument; the rest of
1240 // codegen expects to access aggregates & complex values by
1241 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001242 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001243 Builder.CreateStore(AI, V);
1244 } else {
1245 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1246 // This must be a promotion, for something like
1247 // "void a(x) short x; {..."
1248 V = EmitScalarConversion(V, Ty, Arg->getType());
1249 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001250 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001251 EmitParmDecl(*Arg, V);
1252 break;
1253 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001254
1255 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001256 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001257 // we need to create a temporary and reconstruct it from the
1258 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001259 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001260 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001261 (Name + ".addr").c_str());
1262 // FIXME: What are the right qualifiers here?
1263 llvm::Function::arg_iterator End =
1264 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1265 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001266
Daniel Dunbar04d35782008-09-17 00:51:38 +00001267 // Name the arguments used in expansion and increment AI.
1268 unsigned Index = 0;
1269 for (; AI != End; ++AI, ++Index)
1270 AI->setName(Name + "." + llvm::utostr(Index));
1271 continue;
1272 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001273
1274 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001275 // Initialize the local variable appropriately.
1276 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001277 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001278 } else {
1279 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1280 }
1281
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001282 // Skip increment, no matching LLVM parameter.
1283 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001284
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001285 case ABIArgInfo::Coerce: {
1286 assert(AI != Fn->arg_end() && "Argument mismatch!");
1287 // FIXME: This is very wasteful; EmitParmDecl is just going to
1288 // drop the result in a new alloca anyway, so we could just
1289 // store into that directly if we broke the abstraction down
1290 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001291 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001292 CreateCoercedStore(AI, V, *this);
1293 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001294 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001295 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001296 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1297 // This must be a promotion, for something like
1298 // "void a(x) short x; {..."
1299 V = EmitScalarConversion(V, Ty, Arg->getType());
1300 }
1301 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001302 EmitParmDecl(*Arg, V);
1303 break;
1304 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001305 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001306
1307 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001308 }
1309 assert(AI == Fn->arg_end() && "Argument mismatch!");
1310}
1311
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001312void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001313 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001314 llvm::Value *RV = 0;
1315
1316 // Functions with no result always return void.
1317 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001318 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001319 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001320
1321 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001322 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001323 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001324 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1325 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1326 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1327 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1328 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001329 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1330 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001331 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001332 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001333
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001334 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001335 // The internal return value temp always will have
1336 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001337 RV = Builder.CreateLoad(ReturnValue);
1338 break;
1339
Daniel Dunbar1358b202009-01-26 21:26:08 +00001340 case ABIArgInfo::Ignore:
1341 break;
1342
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001343 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001344 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001345 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001346
Daniel Dunbar22e30052008-09-11 01:48:57 +00001347 case ABIArgInfo::Expand:
1348 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001349 }
1350 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001351
1352 if (RV) {
1353 Builder.CreateRet(RV);
1354 } else {
1355 Builder.CreateRetVoid();
1356 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001357}
1358
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001359RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1360 llvm::Value *Callee,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001361 const CallArgList &CallArgs) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001362 // FIXME: We no longer need the types from CallArgs; lift up and
1363 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001364 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001365
1366 // Handle struct-return functions by passing a pointer to the
1367 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001368 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001369 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001370 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001371 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001372 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001373 }
1374
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001375 assert(CallInfo.arg_size() == CallArgs.size() &&
1376 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001377 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001378 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001379 I != E; ++I, ++info_it) {
1380 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001381 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001382
1383 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001384 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001385 if (RV.isScalar() || RV.isComplex()) {
1386 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001387 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001388 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001389 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001390 else
1391 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1392 } else {
1393 Args.push_back(RV.getAggregateAddr());
1394 }
1395 break;
1396
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001397 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001398 if (RV.isScalar()) {
1399 Args.push_back(RV.getScalarVal());
1400 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001401 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1402 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1403 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1404 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00001405 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001406 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001407 }
1408 break;
1409
Daniel Dunbar1358b202009-01-26 21:26:08 +00001410 case ABIArgInfo::Ignore:
1411 break;
1412
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001413 case ABIArgInfo::Coerce: {
1414 // FIXME: Avoid the conversion through memory if possible.
1415 llvm::Value *SrcPtr;
1416 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001417 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001418 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001419 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001420 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001421 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1422 } else
1423 SrcPtr = RV.getAggregateAddr();
1424 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1425 *this));
1426 break;
1427 }
1428
Daniel Dunbar04d35782008-09-17 00:51:38 +00001429 case ABIArgInfo::Expand:
1430 ExpandTypeToArgs(I->second, RV, Args);
1431 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001432 }
1433 }
1434
1435 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001436
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001437 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001438 CodeGen::AttributeListType AttributeList;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001439 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patela85a9ef2008-09-25 21:02:23 +00001440 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001441 AttributeList.size()));
1442
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001443 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1444 CI->setCallingConv(F->getCallingConv());
1445 if (CI->getType() != llvm::Type::VoidTy)
1446 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001447
1448 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001449 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001450 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001451 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001452 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001453 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001454 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001455 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001456
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001457 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001458 if (RetTy->isAnyComplexType()) {
1459 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1460 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1461 return RValue::getComplex(std::make_pair(Real, Imag));
1462 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001463 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00001464 Builder.CreateStore(CI, V);
1465 return RValue::getAggregate(V);
1466 } else
1467 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001468
Daniel Dunbar1358b202009-01-26 21:26:08 +00001469 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00001470 // If we are ignoring an argument that had a result, make sure to
1471 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00001472 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001473
Daniel Dunbar73d66602008-09-10 07:04:09 +00001474 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001475 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001476 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001477 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001478 if (RetTy->isAnyComplexType())
1479 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001480 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001481 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001482 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001483 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001484 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001485
Daniel Dunbar22e30052008-09-11 01:48:57 +00001486 case ABIArgInfo::Expand:
1487 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001488 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001489
1490 assert(0 && "Unhandled ABIArgInfo::Kind");
1491 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001492}