blob: 0e2bd606ebff11d1df4cf2d5fbffca28eaafb084 [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 Dunbar3cfcec72009-02-12 09:04:14 +000026#include "llvm/Support/MathExtras.h"
Daniel Dunbar9f4874e2009-02-04 23:24:38 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000029
30#include "ABIInfo.h"
31
Daniel Dunbara8f02052008-09-08 21:33:45 +000032using namespace clang;
33using namespace CodeGen;
34
35/***/
36
Daniel Dunbara8f02052008-09-08 21:33:45 +000037// FIXME: Use iterator and sidestep silly type array creation.
38
Daniel Dunbar34bda882009-02-02 23:23:47 +000039const
40CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
41 return getFunctionInfo(FTNP->getResultType(),
42 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000043}
44
Daniel Dunbar34bda882009-02-02 23:23:47 +000045const
46CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
47 llvm::SmallVector<QualType, 16> ArgTys;
48 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000049 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000050 ArgTys.push_back(FTP->getArgType(i));
51 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000052}
53
Daniel Dunbar34bda882009-02-02 23:23:47 +000054const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000055 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar34bda882009-02-02 23:23:47 +000056 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
57 return getFunctionInfo(FTP);
58 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000059}
60
Daniel Dunbar34bda882009-02-02 23:23:47 +000061const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
62 llvm::SmallVector<QualType, 16> ArgTys;
63 ArgTys.push_back(MD->getSelfDecl()->getType());
64 ArgTys.push_back(Context.getObjCSelType());
65 // FIXME: Kill copy?
Chris Lattner9408eb12009-02-20 06:23:21 +000066 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbara8f02052008-09-08 21:33:45 +000067 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000068 ArgTys.push_back((*i)->getType());
69 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000070}
71
Daniel Dunbar34bda882009-02-02 23:23:47 +000072const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
73 const CallArgList &Args) {
74 // FIXME: Kill copy.
75 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000076 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
77 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000078 ArgTys.push_back(i->second);
79 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000080}
81
Daniel Dunbar34bda882009-02-02 23:23:47 +000082const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
83 const FunctionArgList &Args) {
84 // FIXME: Kill copy.
85 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +000086 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
87 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000088 ArgTys.push_back(i->second);
89 return getFunctionInfo(ResTy, ArgTys);
90}
91
92const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
93 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +000094 // Lookup or create unique function info.
95 llvm::FoldingSetNodeID ID;
96 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
97
98 void *InsertPos = 0;
99 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
100 if (FI)
101 return *FI;
102
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000103 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000104 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000105 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000106
107 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000108 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000109
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000110 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000111}
112
113/***/
114
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000115ABIInfo::~ABIInfo() {}
116
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000117void ABIArgInfo::dump() const {
118 fprintf(stderr, "(ABIArgInfo Kind=");
119 switch (TheKind) {
120 case Direct:
121 fprintf(stderr, "Direct");
122 break;
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000123 case Ignore:
124 fprintf(stderr, "Ignore");
125 break;
126 case Coerce:
127 fprintf(stderr, "Coerce Type=");
128 getCoerceToType()->print(llvm::errs());
129 // FIXME: This is ridiculous.
130 llvm::errs().flush();
131 break;
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000132 case Indirect:
133 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000134 break;
135 case Expand:
136 fprintf(stderr, "Expand");
137 break;
138 }
139 fprintf(stderr, ")\n");
140}
141
142/***/
143
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000144/// isEmptyStruct - Return true iff a structure has no non-empty
145/// members. Note that a structure with a flexible array member is not
146/// considered empty.
147static bool isEmptyStruct(QualType T) {
148 const RecordType *RT = T->getAsStructureType();
149 if (!RT)
150 return 0;
151 const RecordDecl *RD = RT->getDecl();
152 if (RD->hasFlexibleArrayMember())
153 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000154 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000155 e = RD->field_end(); i != e; ++i) {
156 const FieldDecl *FD = *i;
157 if (!isEmptyStruct(FD->getType()))
158 return false;
159 }
160 return true;
161}
162
163/// isSingleElementStruct - Determine if a structure is a "single
164/// element struct", i.e. it has exactly one non-empty field or
165/// exactly one field which is itself a single element
166/// struct. Structures with flexible array members are never
167/// considered single element structs.
168///
169/// \return The field declaration for the single non-empty field, if
170/// it exists.
171static const FieldDecl *isSingleElementStruct(QualType T) {
172 const RecordType *RT = T->getAsStructureType();
173 if (!RT)
174 return 0;
175
176 const RecordDecl *RD = RT->getDecl();
177 if (RD->hasFlexibleArrayMember())
178 return 0;
179
180 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000181 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000182 e = RD->field_end(); i != e; ++i) {
183 const FieldDecl *FD = *i;
184 QualType FT = FD->getType();
185
186 if (isEmptyStruct(FT)) {
187 // Ignore
188 } else if (Found) {
189 return 0;
190 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
191 Found = FD;
192 } else {
193 Found = isSingleElementStruct(FT);
194 if (!Found)
195 return 0;
196 }
197 }
198
199 return Found;
200}
201
202static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
203 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
204 return false;
205
206 uint64_t Size = Context.getTypeSize(Ty);
207 return Size == 32 || Size == 64;
208}
209
210static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
211 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000212 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000213 e = RD->field_end(); i != e; ++i) {
214 const FieldDecl *FD = *i;
215
216 if (!is32Or64BitBasicType(FD->getType(), Context))
217 return false;
218
219 // If this is a bit-field we need to make sure it is still a
220 // 32-bit or 64-bit type.
221 if (Expr *BW = FD->getBitWidth()) {
222 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
223 if (Width <= 16)
224 return false;
225 }
226 }
227 return true;
228}
229
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000230namespace {
231/// DefaultABIInfo - The default implementation for ABI specific
232/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000233/// self-consistent and sensible LLVM IR generation, but does not
234/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000235class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000236 ABIArgInfo classifyReturnType(QualType RetTy,
237 ASTContext &Context) const;
238
239 ABIArgInfo classifyArgumentType(QualType RetTy,
240 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000241
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000242 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
243 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
244 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
245 it != ie; ++it)
246 it->info = classifyArgumentType(it->type, Context);
247 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000248
249 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
250 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000251};
252
253/// X86_32ABIInfo - The X86-32 ABI information.
254class X86_32ABIInfo : public ABIInfo {
255public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000256 ABIArgInfo classifyReturnType(QualType RetTy,
257 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000258
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000259 ABIArgInfo classifyArgumentType(QualType RetTy,
260 ASTContext &Context) const;
261
262 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
263 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
264 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
265 it != ie; ++it)
266 it->info = classifyArgumentType(it->type, Context);
267 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000268
269 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
270 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000271};
272}
273
274ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
275 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000276 if (RetTy->isVoidType()) {
277 return ABIArgInfo::getIgnore();
278 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000279 // Classify "single element" structs as their element type.
280 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
281 if (SeltFD) {
282 QualType SeltTy = SeltFD->getType()->getDesugaredType();
283 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
284 // FIXME: This is gross, it would be nice if we could just
285 // pass back SeltTy and have clients deal with it. Is it worth
286 // supporting coerce to both LLVM and clang Types?
287 if (BT->isIntegerType()) {
288 uint64_t Size = Context.getTypeSize(SeltTy);
289 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
290 } else if (BT->getKind() == BuiltinType::Float) {
291 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
292 } else if (BT->getKind() == BuiltinType::Double) {
293 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
294 }
295 } else if (SeltTy->isPointerType()) {
296 // FIXME: It would be really nice if this could come out as
297 // the proper pointer type.
298 llvm::Type *PtrTy =
299 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
300 return ABIArgInfo::getCoerce(PtrTy);
301 }
302 }
303
Daniel Dunbar73d66602008-09-10 07:04:09 +0000304 uint64_t Size = Context.getTypeSize(RetTy);
305 if (Size == 8) {
306 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
307 } else if (Size == 16) {
308 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
309 } else if (Size == 32) {
310 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
311 } else if (Size == 64) {
312 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
313 } else {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000314 return ABIArgInfo::getIndirect(0);
Daniel Dunbar73d66602008-09-10 07:04:09 +0000315 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000316 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000317 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000318 }
319}
320
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000321ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000322 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000323 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000324 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000325 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000326 if (const RecordType *RT = Ty->getAsStructureType())
327 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000328 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000329
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000330 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000331 uint64_t Size = Context.getTypeSize(Ty);
332 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000333 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000334
335 // Expand structs with size <= 128-bits which consist only of
336 // basic types (int, long long, float, double, xxx*). This is
337 // non-recursive and does not ignore empty fields.
338 if (const RecordType *RT = Ty->getAsStructureType()) {
339 if (Context.getTypeSize(Ty) <= 4*32 &&
340 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
341 return ABIArgInfo::getExpand();
342 }
343
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000344 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000345 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000346 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000347 }
348}
349
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000350llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
351 CodeGenFunction &CGF) const {
352 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
353 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
354
355 CGBuilderTy &Builder = CGF.Builder;
356 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
357 "ap");
358 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
359 llvm::Type *PTy =
360 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
361 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
362
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000363 uint64_t Offset =
364 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000365 llvm::Value *NextAddr =
366 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000367 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000368 "ap.next");
369 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
370
371 return AddrTyped;
372}
373
Daniel Dunbare09a9692009-01-24 08:32:22 +0000374namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000375/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000376class X86_64ABIInfo : public ABIInfo {
377 enum Class {
378 Integer = 0,
379 SSE,
380 SSEUp,
381 X87,
382 X87Up,
383 ComplexX87,
384 NoClass,
385 Memory
386 };
387
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000388 /// merge - Implement the X86_64 ABI merging algorithm.
389 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000390 /// Merge an accumulating classification \arg Accum with a field
391 /// classification \arg Field.
392 ///
393 /// \param Accum - The accumulating classification. This should
394 /// always be either NoClass or the result of a previous merge
395 /// call. In addition, this should never be Memory (the caller
396 /// should just return Memory for the aggregate).
397 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000398
Daniel Dunbare09a9692009-01-24 08:32:22 +0000399 /// classify - Determine the x86_64 register classes in which the
400 /// given type T should be passed.
401 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000402 /// \param Lo - The classification for the parts of the type
403 /// residing in the low word of the containing object.
404 ///
405 /// \param Hi - The classification for the parts of the type
406 /// residing in the high word of the containing object.
407 ///
408 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000409 /// containing object. Some parameters are classified different
410 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000411 ///
412 /// If a word is unused its result will be NoClass; if a type should
413 /// be passed in Memory then at least the classification of \arg Lo
414 /// will be Memory.
415 ///
416 /// The \arg Lo class will be NoClass iff the argument is ignored.
417 ///
418 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar92e88642009-02-17 07:55:55 +0000419 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000420 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000421 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000422
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000423 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
424 /// to coerce to, chose the best way to pass Ty in the same place
425 /// that \arg CoerceTo would be passed, but while keeping the
426 /// emitted code as simple as possible.
427 ///
428 /// FIXME: Note, this should be cleaned up to just take an
429 /// enumeration of all the ways we might want to pass things,
430 /// instead of constructing an LLVM type. This makes this code more
431 /// explicit, and it makes it clearer that we are also doing this
432 /// for correctness in the case of passing scalar types.
433 ABIArgInfo getCoerceResult(QualType Ty,
434 const llvm::Type *CoerceTo,
435 ASTContext &Context) const;
436
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000437 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000438 ASTContext &Context) const;
439
440 ABIArgInfo classifyArgumentType(QualType Ty,
441 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000442 unsigned &neededInt,
443 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000444
445public:
446 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000447
448 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
449 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000450};
451}
452
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000453X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
454 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000455 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
456 // classified recursively so that always two fields are
457 // considered. The resulting class is calculated according to
458 // the classes of the fields in the eightbyte:
459 //
460 // (a) If both classes are equal, this is the resulting class.
461 //
462 // (b) If one of the classes is NO_CLASS, the resulting class is
463 // the other class.
464 //
465 // (c) If one of the classes is MEMORY, the result is the MEMORY
466 // class.
467 //
468 // (d) If one of the classes is INTEGER, the result is the
469 // INTEGER.
470 //
471 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
472 // MEMORY is used as class.
473 //
474 // (f) Otherwise class SSE is used.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000475 assert((Accum == NoClass || Accum == Integer ||
476 Accum == SSE || Accum == SSEUp) &&
477 "Invalid accumulated classification during merge.");
478 if (Accum == Field || Field == NoClass)
479 return Accum;
480 else if (Field == Memory)
481 return Memory;
482 else if (Accum == NoClass)
483 return Field;
484 else if (Accum == Integer || Field == Integer)
485 return Integer;
486 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
487 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000488 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000489 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000490}
491
Daniel Dunbare09a9692009-01-24 08:32:22 +0000492void X86_64ABIInfo::classify(QualType Ty,
493 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000494 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000495 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000496 // FIXME: This code can be simplified by introducing a simple value
497 // class for Class pairs with appropriate constructor methods for
498 // the various situations.
499
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000500 Lo = Hi = NoClass;
501
502 Class &Current = OffsetBase < 64 ? Lo : Hi;
503 Current = Memory;
504
Daniel Dunbare09a9692009-01-24 08:32:22 +0000505 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
506 BuiltinType::Kind k = BT->getKind();
507
Daniel Dunbar1358b202009-01-26 21:26:08 +0000508 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000509 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000510 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000511 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000512 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000513 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000514 } else if (k == BuiltinType::LongDouble) {
515 Lo = X87;
516 Hi = X87Up;
517 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000518 // FIXME: _Decimal32 and _Decimal64 are SSE.
519 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000520 // FIXME: __int128 is (Integer, Integer).
521 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
522 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000523 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000524 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000525 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000526 if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000527 // gcc passes <1 x double> in memory. :(
528 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000529 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000530
531 // gcc passes <1 x long long> as INTEGER.
532 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
533 Current = Integer;
534 else
535 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000536
537 // If this type crosses an eightbyte boundary, it should be
538 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000539 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000540 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000541 } else if (Size == 128) {
542 Lo = SSE;
543 Hi = SSEUp;
544 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000545 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000546 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000547
Daniel Dunbare413f532009-01-30 18:40:10 +0000548 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000549 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000550 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000551 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000552 else if (Size <= 128)
553 Lo = Hi = Integer;
554 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000555 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000556 else if (ET == Context.DoubleTy)
557 Lo = Hi = SSE;
558 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000559 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000560
561 // If this complex type crosses an eightbyte boundary then it
562 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000563 uint64_t EB_Real = (OffsetBase) / 64;
564 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000565 if (Hi == NoClass && EB_Real != EB_Imag)
566 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000567 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
568 // Arrays are treated like structures.
569
570 uint64_t Size = Context.getTypeSize(Ty);
571
572 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
573 // than two eightbytes, ..., it has class MEMORY.
574 if (Size > 128)
575 return;
576
577 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
578 // fields, it has class MEMORY.
579 //
580 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000581 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000582 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000583
584 // Otherwise implement simplified merge. We could be smarter about
585 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000586 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000587 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
588 uint64_t ArraySize = AT->getSize().getZExtValue();
589 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
590 Class FieldLo, FieldHi;
591 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000592 Lo = merge(Lo, FieldLo);
593 Hi = merge(Hi, FieldHi);
594 if (Lo == Memory || Hi == Memory)
595 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000596 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000597
598 // Do post merger cleanup (see below). Only case we worry about is Memory.
599 if (Hi == Memory)
600 Lo = Memory;
601 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000602 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000603 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000604
605 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
606 // than two eightbytes, ..., it has class MEMORY.
607 if (Size > 128)
608 return;
609
610 const RecordDecl *RD = RT->getDecl();
611
612 // Assume variable sized types are passed in memory.
613 if (RD->hasFlexibleArrayMember())
614 return;
615
616 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
617
618 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000619 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000620 unsigned idx = 0;
621 for (RecordDecl::field_iterator i = RD->field_begin(),
622 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000623 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000624 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000625
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000626 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
627 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000628 //
629 // Note, skip this test for bitfields, see below.
630 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000631 Lo = Memory;
632 return;
633 }
634
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000635 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000636 //
637 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
638 // exceeds a single eightbyte, each is classified
639 // separately. Each eightbyte gets initialized to class
640 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000641 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000642
643 // Bitfields require special handling, they do not force the
644 // structure to be passed in memory even if unaligned, and
645 // therefore they can straddle an eightbyte.
646 if (BitField) {
647 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
648 uint64_t Size =
649 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
650
651 uint64_t EB_Lo = Offset / 64;
652 uint64_t EB_Hi = (Offset + Size - 1) / 64;
653 FieldLo = FieldHi = NoClass;
654 if (EB_Lo) {
655 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
656 FieldLo = NoClass;
657 FieldHi = Integer;
658 } else {
659 FieldLo = Integer;
660 FieldHi = EB_Hi ? Integer : NoClass;
661 }
662 } else
663 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000664 Lo = merge(Lo, FieldLo);
665 Hi = merge(Hi, FieldHi);
666 if (Lo == Memory || Hi == Memory)
667 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000668 }
669
670 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
671 //
672 // (a) If one of the classes is MEMORY, the whole argument is
673 // passed in memory.
674 //
675 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
676
677 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000678 // the merge (just bail).
679 //
680 // The second condition occurs in the case of unions; for example
681 // union { _Complex double; unsigned; }.
682 if (Hi == Memory)
683 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000684 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000685 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000686 }
687}
688
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000689ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
690 const llvm::Type *CoerceTo,
691 ASTContext &Context) const {
692 if (CoerceTo == llvm::Type::Int64Ty) {
693 // Integer and pointer types will end up in a general purpose
694 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000695 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000696 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000697
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000698 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000699 // FIXME: It would probably be better to make CGFunctionInfo only
700 // map using canonical types than to canonize here.
701 QualType CTy = Context.getCanonicalType(Ty);
702
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000703 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000704 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000705 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000706
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000707 }
708
709 return ABIArgInfo::getCoerce(CoerceTo);
710}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000711
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000712ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
713 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000714 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
715 // classification algorithm.
716 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000717 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000718
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000719 // Check some invariants.
720 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
721 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
722 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
723
Daniel Dunbare09a9692009-01-24 08:32:22 +0000724 const llvm::Type *ResType = 0;
725 switch (Lo) {
726 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000727 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000728
729 case SSEUp:
730 case X87Up:
731 assert(0 && "Invalid classification for lo word.");
732
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000733 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000734 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000735 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000736 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000737
738 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
739 // available register of the sequence %rax, %rdx is used.
740 case Integer:
741 ResType = llvm::Type::Int64Ty; break;
742
743 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
744 // available SSE register of the sequence %xmm0, %xmm1 is used.
745 case SSE:
746 ResType = llvm::Type::DoubleTy; break;
747
748 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
749 // returned on the X87 stack in %st0 as 80-bit x87 number.
750 case X87:
751 ResType = llvm::Type::X86_FP80Ty; break;
752
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000753 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
754 // part of the value is returned in %st0 and the imaginary part in
755 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000756 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000757 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000758 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
759 llvm::Type::X86_FP80Ty,
760 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000761 break;
762 }
763
764 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000765 // Memory was handled previously and X87 should
766 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000767 case Memory:
768 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000769 assert(0 && "Invalid classification for hi word.");
770
Daniel Dunbar92e88642009-02-17 07:55:55 +0000771 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000772 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000773
Daniel Dunbare09a9692009-01-24 08:32:22 +0000774 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000775 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
776 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000777 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000778 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
779 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000780
781 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
782 // is passed in the upper half of the last used SSE register.
783 //
784 // SSEUP should always be preceeded by SSE, just widen.
785 case SSEUp:
786 assert(Lo == SSE && "Unexpected SSEUp classification.");
787 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
788 break;
789
790 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000791 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000792 //
793 // X87UP should always be preceeded by X87, so we don't need to do
794 // anything here.
795 case X87Up:
796 assert(Lo == X87 && "Unexpected X87Up classification.");
797 break;
798 }
799
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000800 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000801}
802
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000803ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000804 unsigned &neededInt,
805 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000806 X86_64ABIInfo::Class Lo, Hi;
807 classify(Ty, Context, 0, Lo, Hi);
808
809 // Check some invariants.
810 // FIXME: Enforce these by construction.
811 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
812 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
813 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
814
Daniel Dunbare978cb92009-02-10 17:06:09 +0000815 neededInt = 0;
816 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000817 const llvm::Type *ResType = 0;
818 switch (Lo) {
819 case NoClass:
820 return ABIArgInfo::getIgnore();
821
822 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
823 // on the stack.
824 case Memory:
825
826 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
827 // COMPLEX_X87, it is passed in memory.
828 case X87:
829 case ComplexX87:
830 // Choose appropriate in memory type.
831 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000832 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000833 else
834 return ABIArgInfo::getDirect();
835
836 case SSEUp:
837 case X87Up:
838 assert(0 && "Invalid classification for lo word.");
839
840 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
841 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
842 // and %r9 is used.
843 case Integer:
844 ++neededInt;
845 ResType = llvm::Type::Int64Ty;
846 break;
847
848 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
849 // available SSE register is used, the registers are taken in the
850 // order from %xmm0 to %xmm7.
851 case SSE:
852 ++neededSSE;
853 ResType = llvm::Type::DoubleTy;
854 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000855 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000856
857 switch (Hi) {
858 // Memory was handled previously, ComplexX87 and X87 should
859 // never occur as hi classes, and X87Up must be preceed by X87,
860 // which is passed in memory.
861 case Memory:
862 case X87:
863 case X87Up:
864 case ComplexX87:
865 assert(0 && "Invalid classification for hi word.");
866
867 case NoClass: break;
868 case Integer:
869 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
870 ++neededInt;
871 break;
872 case SSE:
873 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
874 ++neededSSE;
875 break;
876
877 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
878 // eightbyte is passed in the upper half of the last used SSE
879 // register.
880 case SSEUp:
881 assert(Lo == SSE && "Unexpected SSEUp classification.");
882 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
883 break;
884 }
885
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000886 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000887}
888
889void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
890 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
891
892 // Keep track of the number of assigned registers.
893 unsigned freeIntRegs = 6, freeSSERegs = 8;
894
895 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
896 // get assigned (in left-to-right order) for passing as follows...
897 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +0000898 it != ie; ++it) {
899 unsigned neededInt, neededSSE;
900 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
901
902 // AMD64-ABI 3.2.3p3: If there are no registers available for any
903 // eightbyte of an argument, the whole argument is passed on the
904 // stack. If registers have already been assigned for some
905 // eightbytes of such an argument, the assignments get reverted.
906 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
907 freeIntRegs -= neededInt;
908 freeSSERegs -= neededSSE;
909 } else {
910 // Choose appropriate in memory type.
911 if (CodeGenFunction::hasAggregateLLVMType(it->type))
912 it->info = ABIArgInfo::getIndirect(0);
913 else
914 it->info = ABIArgInfo::getDirect();
915 }
916 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000917}
918
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000919static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
920 QualType Ty,
921 CodeGenFunction &CGF) {
922 llvm::Value *overflow_arg_area_p =
923 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
924 llvm::Value *overflow_arg_area =
925 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
926
927 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
928 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000929 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000930 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000931 // Note that we follow the ABI & gcc here, even though the type
932 // could in theory have an alignment greater than 16. This case
933 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000934
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000935 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
936 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
937 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
938 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
939 llvm::Type::Int64Ty);
940 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
941 overflow_arg_area =
942 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
943 overflow_arg_area->getType(),
944 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000945 }
946
947 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
948 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
949 llvm::Value *Res =
950 CGF.Builder.CreateBitCast(overflow_arg_area,
951 llvm::PointerType::getUnqual(LTy));
952
953 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
954 // l->overflow_arg_area + sizeof(type).
955 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
956 // an 8 byte boundary.
957
958 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
959 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
960 (SizeInBytes + 7) & ~7);
961 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
962 "overflow_arg_area.next");
963 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
964
965 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
966 return Res;
967}
968
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000969llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
970 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000971 // Assume that va_list type is correct; should be pointer to LLVM type:
972 // struct {
973 // i32 gp_offset;
974 // i32 fp_offset;
975 // i8* overflow_arg_area;
976 // i8* reg_save_area;
977 // };
978 unsigned neededInt, neededSSE;
979 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
980 neededInt, neededSSE);
981
982 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
983 // in the registers. If not go to step 7.
984 if (!neededInt && !neededSSE)
985 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
986
987 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
988 // general purpose registers needed to pass type and num_fp to hold
989 // the number of floating point registers needed.
990
991 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
992 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
993 // l->fp_offset > 304 - num_fp * 16 go to step 7.
994 //
995 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
996 // register save space).
997
998 llvm::Value *InRegs = 0;
999 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1000 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1001 if (neededInt) {
1002 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1003 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1004 InRegs =
1005 CGF.Builder.CreateICmpULE(gp_offset,
1006 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1007 48 - neededInt * 8),
1008 "fits_in_gp");
1009 }
1010
1011 if (neededSSE) {
1012 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1013 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1014 llvm::Value *FitsInFP =
1015 CGF.Builder.CreateICmpULE(fp_offset,
1016 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001017 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001018 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001019 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001020 }
1021
1022 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1023 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1024 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1025 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1026
1027 // Emit code to load the value if it was passed in registers.
1028
1029 CGF.EmitBlock(InRegBlock);
1030
1031 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1032 // an offset of l->gp_offset and/or l->fp_offset. This may require
1033 // copying to a temporary location in case the parameter is passed
1034 // in different register classes or requires an alignment greater
1035 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001036 //
1037 // FIXME: This really results in shameful code when we end up
1038 // needing to collect arguments from different places; often what
1039 // should result in a simple assembling of a structure from
1040 // scattered addresses has many more loads than necessary. Can we
1041 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001042 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1043 llvm::Value *RegAddr =
1044 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1045 "reg_save_area");
1046 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001047 // FIXME: Cleanup.
1048 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1049 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1050 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1051 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1052 const llvm::Type *TyLo = ST->getElementType(0);
1053 const llvm::Type *TyHi = ST->getElementType(1);
1054 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1055 "Unexpected ABI info for mixed regs");
1056 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1057 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1058 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1059 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1060 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1061 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1062 llvm::Value *V =
1063 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1064 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1065 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1066 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1067
1068 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001069 } else if (neededInt) {
1070 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1071 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1072 llvm::PointerType::getUnqual(LTy));
1073 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001074 if (neededSSE == 1) {
1075 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1076 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1077 llvm::PointerType::getUnqual(LTy));
1078 } else {
1079 assert(neededSSE == 2 && "Invalid number of needed registers!");
1080 // SSE registers are spaced 16 bytes apart in the register save
1081 // area, we need to collect the two eightbytes together.
1082 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1083 llvm::Value *RegAddrHi =
1084 CGF.Builder.CreateGEP(RegAddrLo,
1085 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1086 const llvm::Type *DblPtrTy =
1087 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1088 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1089 llvm::Type::DoubleTy,
1090 NULL);
1091 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1092 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1093 DblPtrTy));
1094 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1095 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1096 DblPtrTy));
1097 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1098 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1099 llvm::PointerType::getUnqual(LTy));
1100 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001101 }
1102
1103 // AMD64-ABI 3.5.7p5: Step 5. Set:
1104 // l->gp_offset = l->gp_offset + num_gp * 8
1105 // l->fp_offset = l->fp_offset + num_fp * 16.
1106 if (neededInt) {
1107 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1108 neededInt * 8);
1109 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1110 gp_offset_p);
1111 }
1112 if (neededSSE) {
1113 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1114 neededSSE * 16);
1115 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1116 fp_offset_p);
1117 }
1118 CGF.EmitBranch(ContBlock);
1119
1120 // Emit code to load the value if it was passed in memory.
1121
1122 CGF.EmitBlock(InMemBlock);
1123 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1124
1125 // Return the appropriate result.
1126
1127 CGF.EmitBlock(ContBlock);
1128 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1129 "vaarg.addr");
1130 ResAddr->reserveOperandSpace(2);
1131 ResAddr->addIncoming(RegAddr, InRegBlock);
1132 ResAddr->addIncoming(MemAddr, InMemBlock);
1133
1134 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001135}
1136
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001137ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001138 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001139 if (RetTy->isVoidType()) {
1140 return ABIArgInfo::getIgnore();
1141 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001142 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001143 } else {
1144 return ABIArgInfo::getDirect();
1145 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001146}
1147
1148ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001149 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001150 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001151 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001152 } else {
1153 return ABIArgInfo::getDirect();
1154 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001155}
1156
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001157llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1158 CodeGenFunction &CGF) const {
1159 return 0;
1160}
1161
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001162const ABIInfo &CodeGenTypes::getABIInfo() const {
1163 if (TheABIInfo)
1164 return *TheABIInfo;
1165
1166 // For now we just cache this in the CodeGenTypes and don't bother
1167 // to free it.
1168 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1169 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001170 switch (getContext().Target.getPointerWidth(0)) {
1171 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001172 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001173 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001174 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001175 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001176 }
1177
1178 return *(TheABIInfo = new DefaultABIInfo);
1179}
1180
Daniel Dunbare126ab12008-09-10 02:41:04 +00001181/***/
1182
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001183CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1184 const llvm::SmallVector<QualType, 16> &ArgTys) {
1185 NumArgs = ArgTys.size();
1186 Args = new ArgInfo[1 + NumArgs];
1187 Args[0].type = ResTy;
1188 for (unsigned i = 0; i < NumArgs; ++i)
1189 Args[1 + i].type = ArgTys[i];
1190}
1191
1192/***/
1193
Daniel Dunbar04d35782008-09-17 00:51:38 +00001194void CodeGenTypes::GetExpandedTypes(QualType Ty,
1195 std::vector<const llvm::Type*> &ArgTys) {
1196 const RecordType *RT = Ty->getAsStructureType();
1197 assert(RT && "Can only expand structure types.");
1198 const RecordDecl *RD = RT->getDecl();
1199 assert(!RD->hasFlexibleArrayMember() &&
1200 "Cannot expand structure with flexible array.");
1201
Douglas Gregor5d764842009-01-09 17:18:27 +00001202 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001203 e = RD->field_end(); i != e; ++i) {
1204 const FieldDecl *FD = *i;
1205 assert(!FD->isBitField() &&
1206 "Cannot expand structure with bit-field members.");
1207
1208 QualType FT = FD->getType();
1209 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1210 GetExpandedTypes(FT, ArgTys);
1211 } else {
1212 ArgTys.push_back(ConvertType(FT));
1213 }
1214 }
1215}
1216
1217llvm::Function::arg_iterator
1218CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1219 llvm::Function::arg_iterator AI) {
1220 const RecordType *RT = Ty->getAsStructureType();
1221 assert(RT && "Can only expand structure types.");
1222
1223 RecordDecl *RD = RT->getDecl();
1224 assert(LV.isSimple() &&
1225 "Unexpected non-simple lvalue during struct expansion.");
1226 llvm::Value *Addr = LV.getAddress();
1227 for (RecordDecl::field_iterator i = RD->field_begin(),
1228 e = RD->field_end(); i != e; ++i) {
1229 FieldDecl *FD = *i;
1230 QualType FT = FD->getType();
1231
1232 // FIXME: What are the right qualifiers here?
1233 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1234 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1235 AI = ExpandTypeFromArgs(FT, LV, AI);
1236 } else {
1237 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1238 ++AI;
1239 }
1240 }
1241
1242 return AI;
1243}
1244
1245void
1246CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1247 llvm::SmallVector<llvm::Value*, 16> &Args) {
1248 const RecordType *RT = Ty->getAsStructureType();
1249 assert(RT && "Can only expand structure types.");
1250
1251 RecordDecl *RD = RT->getDecl();
1252 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1253 llvm::Value *Addr = RV.getAggregateAddr();
1254 for (RecordDecl::field_iterator i = RD->field_begin(),
1255 e = RD->field_end(); i != e; ++i) {
1256 FieldDecl *FD = *i;
1257 QualType FT = FD->getType();
1258
1259 // FIXME: What are the right qualifiers here?
1260 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1261 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1262 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1263 } else {
1264 RValue RV = EmitLoadOfLValue(LV, FT);
1265 assert(RV.isScalar() &&
1266 "Unexpected non-scalar rvalue during struct expansion.");
1267 Args.push_back(RV.getScalarVal());
1268 }
1269 }
1270}
1271
Daniel Dunbar84379912009-02-02 19:06:38 +00001272/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1273/// a pointer to an object of type \arg Ty.
1274///
1275/// This safely handles the case when the src type is smaller than the
1276/// destination type; in this situation the values of bits which not
1277/// present in the src are undefined.
1278static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1279 const llvm::Type *Ty,
1280 CodeGenFunction &CGF) {
1281 const llvm::Type *SrcTy =
1282 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1283 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1284 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1285
Daniel Dunbar77071992009-02-03 05:59:18 +00001286 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001287 if (SrcSize == DstSize) {
1288 llvm::Value *Casted =
1289 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001290 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1291 // FIXME: Use better alignment / avoid requiring aligned load.
1292 Load->setAlignment(1);
1293 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001294 } else {
1295 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1296
1297 // Otherwise do coercion through memory. This is stupid, but
1298 // simple.
1299 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1300 llvm::Value *Casted =
1301 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001302 llvm::StoreInst *Store =
1303 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1304 // FIXME: Use better alignment / avoid requiring aligned store.
1305 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001306 return CGF.Builder.CreateLoad(Tmp);
1307 }
1308}
1309
1310/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1311/// where the source and destination may have different types.
1312///
1313/// This safely handles the case when the src type is larger than the
1314/// destination type; the upper bits of the src will be lost.
1315static void CreateCoercedStore(llvm::Value *Src,
1316 llvm::Value *DstPtr,
1317 CodeGenFunction &CGF) {
1318 const llvm::Type *SrcTy = Src->getType();
1319 const llvm::Type *DstTy =
1320 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1321
1322 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1323 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1324
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001325 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001326 if (SrcSize == DstSize) {
1327 llvm::Value *Casted =
1328 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001329 // FIXME: Use better alignment / avoid requiring aligned store.
1330 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001331 } else {
1332 assert(SrcSize > DstSize && "Coercion is missing bits!");
1333
1334 // Otherwise do coercion through memory. This is stupid, but
1335 // simple.
1336 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1337 CGF.Builder.CreateStore(Src, Tmp);
1338 llvm::Value *Casted =
1339 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001340 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1341 // FIXME: Use better alignment / avoid requiring aligned load.
1342 Load->setAlignment(1);
1343 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001344 }
1345}
1346
Daniel Dunbar04d35782008-09-17 00:51:38 +00001347/***/
1348
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001349bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001350 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001351}
1352
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001353const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001354CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001355 std::vector<const llvm::Type*> ArgTys;
1356
1357 const llvm::Type *ResultType = 0;
1358
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001359 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001360 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001361 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001362 case ABIArgInfo::Expand:
1363 assert(0 && "Invalid ABI kind for return argument");
1364
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001365 case ABIArgInfo::Direct:
1366 ResultType = ConvertType(RetTy);
1367 break;
1368
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001369 case ABIArgInfo::Indirect: {
1370 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001371 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001372 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001373 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1374 break;
1375 }
1376
Daniel Dunbar1358b202009-01-26 21:26:08 +00001377 case ABIArgInfo::Ignore:
1378 ResultType = llvm::Type::VoidTy;
1379 break;
1380
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001381 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001382 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001383 break;
1384 }
1385
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001386 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1387 ie = FI.arg_end(); it != ie; ++it) {
1388 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001389
1390 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001391 case ABIArgInfo::Ignore:
1392 break;
1393
Daniel Dunbar04d35782008-09-17 00:51:38 +00001394 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001395 ArgTys.push_back(AI.getCoerceToType());
1396 break;
1397
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001398 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001399 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001400 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1401 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001402 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001403 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001404
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001405 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001406 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001407 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001408
1409 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001410 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001411 break;
1412 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001413 }
1414
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001415 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001416}
1417
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001418void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001419 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001420 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001421 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001422 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001423
1424 if (TargetDecl) {
1425 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001426 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001427 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001428 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001429 if (TargetDecl->getAttr<PureAttr>())
1430 FuncAttrs |= llvm::Attribute::ReadOnly;
1431 if (TargetDecl->getAttr<ConstAttr>())
1432 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001433 }
1434
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001435 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001436 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001437 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001438 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001439 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001440 if (RetTy->isPromotableIntegerType()) {
1441 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001442 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001443 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001444 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001445 }
1446 }
1447 break;
1448
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001449 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001450 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001451 llvm::Attribute::StructRet |
1452 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001453 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001454 break;
1455
Daniel Dunbar1358b202009-01-26 21:26:08 +00001456 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001457 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001458 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001459
Daniel Dunbar22e30052008-09-11 01:48:57 +00001460 case ABIArgInfo::Expand:
1461 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001462 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001463
Devang Patel2bb6eb82008-09-26 22:53:57 +00001464 if (RetAttrs)
1465 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001466 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1467 ie = FI.arg_end(); it != ie; ++it) {
1468 QualType ParamType = it->type;
1469 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001470 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001471
1472 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001473 case ABIArgInfo::Coerce:
1474 break;
1475
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001476 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001477 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarb3f651a2009-02-05 01:31:19 +00001478 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001479 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001480 break;
1481
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001482 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001483 if (ParamType->isPromotableIntegerType()) {
1484 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001485 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001486 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001487 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001488 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001489 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001490 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001491
Daniel Dunbar1358b202009-01-26 21:26:08 +00001492 case ABIArgInfo::Ignore:
1493 // Skip increment, no matching LLVM parameter.
1494 continue;
1495
Daniel Dunbar04d35782008-09-17 00:51:38 +00001496 case ABIArgInfo::Expand: {
1497 std::vector<const llvm::Type*> Tys;
1498 // FIXME: This is rather inefficient. Do we ever actually need
1499 // to do anything here? The result should be just reconstructed
1500 // on the other side, so extension should be a non-issue.
1501 getTypes().GetExpandedTypes(ParamType, Tys);
1502 Index += Tys.size();
1503 continue;
1504 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001505 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001506
Devang Patela85a9ef2008-09-25 21:02:23 +00001507 if (Attributes)
1508 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001509 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001510 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001511 if (FuncAttrs)
1512 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1513
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001514}
1515
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001516void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1517 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001518 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001519 // FIXME: We no longer need the types from FunctionArgList; lift up
1520 // and simplify.
1521
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001522 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1523 llvm::Function::arg_iterator AI = Fn->arg_begin();
1524
1525 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001526 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001527 AI->setName("agg.result");
1528 ++AI;
1529 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001530
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001531 assert(FI.arg_size() == Args.size() &&
1532 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001533 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001534 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001535 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001536 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001537 QualType Ty = info_it->type;
1538 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001539
1540 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001541 case ABIArgInfo::Indirect: {
1542 llvm::Value* V = AI;
1543 if (hasAggregateLLVMType(Ty)) {
1544 // Do nothing, aggregates and complex variables are accessed by
1545 // reference.
1546 } else {
1547 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001548 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001549 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1550 // This must be a promotion, for something like
1551 // "void a(x) short x; {..."
1552 V = EmitScalarConversion(V, Ty, Arg->getType());
1553 }
1554 }
1555 EmitParmDecl(*Arg, V);
1556 break;
1557 }
1558
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001559 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001560 assert(AI != Fn->arg_end() && "Argument mismatch!");
1561 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001562 if (hasAggregateLLVMType(Ty)) {
1563 // Create a temporary alloca to hold the argument; the rest of
1564 // codegen expects to access aggregates & complex values by
1565 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001566 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001567 Builder.CreateStore(AI, V);
1568 } else {
1569 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1570 // This must be a promotion, for something like
1571 // "void a(x) short x; {..."
1572 V = EmitScalarConversion(V, Ty, Arg->getType());
1573 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001574 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001575 EmitParmDecl(*Arg, V);
1576 break;
1577 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001578
1579 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001580 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001581 // we need to create a temporary and reconstruct it from the
1582 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001583 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001584 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001585 (Name + ".addr").c_str());
1586 // FIXME: What are the right qualifiers here?
1587 llvm::Function::arg_iterator End =
1588 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1589 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001590
Daniel Dunbar04d35782008-09-17 00:51:38 +00001591 // Name the arguments used in expansion and increment AI.
1592 unsigned Index = 0;
1593 for (; AI != End; ++AI, ++Index)
1594 AI->setName(Name + "." + llvm::utostr(Index));
1595 continue;
1596 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001597
1598 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001599 // Initialize the local variable appropriately.
1600 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001601 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001602 } else {
1603 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1604 }
1605
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001606 // Skip increment, no matching LLVM parameter.
1607 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001608
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001609 case ABIArgInfo::Coerce: {
1610 assert(AI != Fn->arg_end() && "Argument mismatch!");
1611 // FIXME: This is very wasteful; EmitParmDecl is just going to
1612 // drop the result in a new alloca anyway, so we could just
1613 // store into that directly if we broke the abstraction down
1614 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001615 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001616 CreateCoercedStore(AI, V, *this);
1617 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001618 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001619 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001620 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1621 // This must be a promotion, for something like
1622 // "void a(x) short x; {..."
1623 V = EmitScalarConversion(V, Ty, Arg->getType());
1624 }
1625 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001626 EmitParmDecl(*Arg, V);
1627 break;
1628 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001629 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001630
1631 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001632 }
1633 assert(AI == Fn->arg_end() && "Argument mismatch!");
1634}
1635
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001636void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001637 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001638 llvm::Value *RV = 0;
1639
1640 // Functions with no result always return void.
1641 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001642 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001643 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001644
1645 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001646 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001647 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001648 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1649 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1650 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1651 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1652 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001653 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1654 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001655 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001656 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001657
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001658 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001659 // The internal return value temp always will have
1660 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001661 RV = Builder.CreateLoad(ReturnValue);
1662 break;
1663
Daniel Dunbar1358b202009-01-26 21:26:08 +00001664 case ABIArgInfo::Ignore:
1665 break;
1666
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001667 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001668 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001669 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001670
Daniel Dunbar22e30052008-09-11 01:48:57 +00001671 case ABIArgInfo::Expand:
1672 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001673 }
1674 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001675
1676 if (RV) {
1677 Builder.CreateRet(RV);
1678 } else {
1679 Builder.CreateRetVoid();
1680 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001681}
1682
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001683RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1684 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001685 const CallArgList &CallArgs,
1686 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001687 // FIXME: We no longer need the types from CallArgs; lift up and
1688 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001689 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001690
1691 // Handle struct-return functions by passing a pointer to the
1692 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001693 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001694 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001695 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001696 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001697 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001698 }
1699
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001700 assert(CallInfo.arg_size() == CallArgs.size() &&
1701 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001702 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001703 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001704 I != E; ++I, ++info_it) {
1705 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001706 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001707
1708 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001709 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001710 if (RV.isScalar() || RV.isComplex()) {
1711 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001712 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001713 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001714 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001715 else
1716 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1717 } else {
1718 Args.push_back(RV.getAggregateAddr());
1719 }
1720 break;
1721
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001722 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001723 if (RV.isScalar()) {
1724 Args.push_back(RV.getScalarVal());
1725 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001726 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1727 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1728 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1729 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00001730 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001731 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001732 }
1733 break;
1734
Daniel Dunbar1358b202009-01-26 21:26:08 +00001735 case ABIArgInfo::Ignore:
1736 break;
1737
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001738 case ABIArgInfo::Coerce: {
1739 // FIXME: Avoid the conversion through memory if possible.
1740 llvm::Value *SrcPtr;
1741 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001742 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001743 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001744 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001745 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001746 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1747 } else
1748 SrcPtr = RV.getAggregateAddr();
1749 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1750 *this));
1751 break;
1752 }
1753
Daniel Dunbar04d35782008-09-17 00:51:38 +00001754 case ABIArgInfo::Expand:
1755 ExpandTypeToArgs(I->second, RV, Args);
1756 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001757 }
1758 }
1759
1760 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001761
Devang Patela85a9ef2008-09-25 21:02:23 +00001762 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001763 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Devang Patela85a9ef2008-09-25 21:02:23 +00001764 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001765 AttributeList.size()));
1766
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001767 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1768 CI->setCallingConv(F->getCallingConv());
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00001769
1770 // If the call doesn't return, finish the basic block and clear the
1771 // insertion point; this allows the rest of IRgen to discard
1772 // unreachable code.
1773 if (CI->doesNotReturn()) {
1774 Builder.CreateUnreachable();
1775 Builder.ClearInsertionPoint();
1776
1777 // Return a reasonable RValue.
1778 return GetUndefRValue(RetTy);
1779 }
1780
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001781 if (CI->getType() != llvm::Type::VoidTy)
1782 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001783
1784 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001785 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001786 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001787 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001788 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001789 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001790 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001791 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001792
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001793 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001794 if (RetTy->isAnyComplexType()) {
1795 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1796 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1797 return RValue::getComplex(std::make_pair(Real, Imag));
1798 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001799 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00001800 Builder.CreateStore(CI, V);
1801 return RValue::getAggregate(V);
1802 } else
1803 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001804
Daniel Dunbar1358b202009-01-26 21:26:08 +00001805 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00001806 // If we are ignoring an argument that had a result, make sure to
1807 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00001808 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001809
Daniel Dunbar73d66602008-09-10 07:04:09 +00001810 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001811 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001812 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001813 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001814 if (RetTy->isAnyComplexType())
1815 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001816 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001817 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001818 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001819 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001820 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001821
Daniel Dunbar22e30052008-09-11 01:48:57 +00001822 case ABIArgInfo::Expand:
1823 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001824 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001825
1826 assert(0 && "Unhandled ABIArgInfo::Kind");
1827 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001828}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001829
1830/* VarArg handling */
1831
1832llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1833 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1834}