blob: 28152f2de16a166f13f2ae344a5e3fc2a42343fd [file] [log] [blame]
Daniel Dunbar0dbe2272008-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 Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar99037e52009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000025#include "llvm/Support/CallSite.h"
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +000026#include "llvm/Support/CommandLine.h"
Daniel Dunbarbe9eb092009-02-12 09:04:14 +000027#include "llvm/Support/MathExtras.h"
Daniel Dunbar6f7279b2009-02-04 23:24:38 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000030
31#include "ABIInfo.h"
32
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000033using namespace clang;
34using namespace CodeGen;
35
36/***/
37
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000038// FIXME: Use iterator and sidestep silly type array creation.
39
Daniel Dunbar541b63b2009-02-02 23:23:47 +000040const
Douglas Gregor72564e72009-02-26 23:50:07 +000041CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000042 return getFunctionInfo(FTNP->getResultType(),
43 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000044}
45
Daniel Dunbar541b63b2009-02-02 23:23:47 +000046const
Douglas Gregor72564e72009-02-26 23:50:07 +000047CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000048 llvm::SmallVector<QualType, 16> ArgTys;
49 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000050 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000051 ArgTys.push_back(FTP->getArgType(i));
52 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000053}
54
Daniel Dunbar541b63b2009-02-02 23:23:47 +000055const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000056 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +000057 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar541b63b2009-02-02 23:23:47 +000058 return getFunctionInfo(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +000059 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000060}
61
Daniel Dunbar541b63b2009-02-02 23:23:47 +000062const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
63 llvm::SmallVector<QualType, 16> ArgTys;
64 ArgTys.push_back(MD->getSelfDecl()->getType());
65 ArgTys.push_back(Context.getObjCSelType());
66 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +000067 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000068 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000069 ArgTys.push_back((*i)->getType());
70 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000071}
72
Daniel Dunbar541b63b2009-02-02 23:23:47 +000073const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
74 const CallArgList &Args) {
75 // FIXME: Kill copy.
76 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000077 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
78 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079 ArgTys.push_back(i->second);
80 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000081}
82
Daniel Dunbar541b63b2009-02-02 23:23:47 +000083const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
84 const FunctionArgList &Args) {
85 // FIXME: Kill copy.
86 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +000087 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
88 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000089 ArgTys.push_back(i->second);
90 return getFunctionInfo(ResTy, ArgTys);
91}
92
93const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
94 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +000095 // Lookup or create unique function info.
96 llvm::FoldingSetNodeID ID;
97 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
98
99 void *InsertPos = 0;
100 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
101 if (FI)
102 return *FI;
103
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000104 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000105 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000106 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000107
108 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000109 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000110
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000111 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000112}
113
114/***/
115
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000116ABIInfo::~ABIInfo() {}
117
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000118void ABIArgInfo::dump() const {
119 fprintf(stderr, "(ABIArgInfo Kind=");
120 switch (TheKind) {
121 case Direct:
122 fprintf(stderr, "Direct");
123 break;
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000124 case Ignore:
125 fprintf(stderr, "Ignore");
126 break;
127 case Coerce:
128 fprintf(stderr, "Coerce Type=");
129 getCoerceToType()->print(llvm::errs());
130 // FIXME: This is ridiculous.
131 llvm::errs().flush();
132 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000133 case Indirect:
134 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000135 break;
136 case Expand:
137 fprintf(stderr, "Expand");
138 break;
139 }
140 fprintf(stderr, ")\n");
141}
142
143/***/
144
Daniel Dunbar834af452008-09-17 21:22:33 +0000145/// isEmptyStruct - Return true iff a structure has no non-empty
146/// members. Note that a structure with a flexible array member is not
147/// considered empty.
148static bool isEmptyStruct(QualType T) {
149 const RecordType *RT = T->getAsStructureType();
150 if (!RT)
151 return 0;
152 const RecordDecl *RD = RT->getDecl();
153 if (RD->hasFlexibleArrayMember())
154 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000155 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000156 e = RD->field_end(); i != e; ++i) {
157 const FieldDecl *FD = *i;
158 if (!isEmptyStruct(FD->getType()))
159 return false;
160 }
161 return true;
162}
163
164/// isSingleElementStruct - Determine if a structure is a "single
165/// element struct", i.e. it has exactly one non-empty field or
166/// exactly one field which is itself a single element
167/// struct. Structures with flexible array members are never
168/// considered single element structs.
169///
170/// \return The field declaration for the single non-empty field, if
171/// it exists.
172static const FieldDecl *isSingleElementStruct(QualType T) {
173 const RecordType *RT = T->getAsStructureType();
174 if (!RT)
175 return 0;
176
177 const RecordDecl *RD = RT->getDecl();
178 if (RD->hasFlexibleArrayMember())
179 return 0;
180
181 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000182 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000183 e = RD->field_end(); i != e; ++i) {
184 const FieldDecl *FD = *i;
185 QualType FT = FD->getType();
186
187 if (isEmptyStruct(FT)) {
188 // Ignore
189 } else if (Found) {
190 return 0;
191 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
192 Found = FD;
193 } else {
194 Found = isSingleElementStruct(FT);
195 if (!Found)
196 return 0;
197 }
198 }
199
200 return Found;
201}
202
203static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
204 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
205 return false;
206
207 uint64_t Size = Context.getTypeSize(Ty);
208 return Size == 32 || Size == 64;
209}
210
211static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
212 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000213 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000214 e = RD->field_end(); i != e; ++i) {
215 const FieldDecl *FD = *i;
216
217 if (!is32Or64BitBasicType(FD->getType(), Context))
218 return false;
219
220 // If this is a bit-field we need to make sure it is still a
221 // 32-bit or 64-bit type.
222 if (Expr *BW = FD->getBitWidth()) {
223 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
224 if (Width <= 16)
225 return false;
226 }
227 }
228 return true;
229}
230
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000231namespace {
232/// DefaultABIInfo - The default implementation for ABI specific
233/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000234/// self-consistent and sensible LLVM IR generation, but does not
235/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000236class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000237 ABIArgInfo classifyReturnType(QualType RetTy,
238 ASTContext &Context) const;
239
240 ABIArgInfo classifyArgumentType(QualType RetTy,
241 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000242
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000243 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
244 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
245 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
246 it != ie; ++it)
247 it->info = classifyArgumentType(it->type, Context);
248 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000249
250 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
251 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000252};
253
254/// X86_32ABIInfo - The X86-32 ABI information.
255class X86_32ABIInfo : public ABIInfo {
256public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000257 ABIArgInfo classifyReturnType(QualType RetTy,
258 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000259
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000260 ABIArgInfo classifyArgumentType(QualType RetTy,
261 ASTContext &Context) const;
262
263 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
264 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
265 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
266 it != ie; ++it)
267 it->info = classifyArgumentType(it->type, Context);
268 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000269
270 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
271 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000272};
273}
274
275ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
276 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000277 if (RetTy->isVoidType()) {
278 return ABIArgInfo::getIgnore();
279 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000280 // Classify "single element" structs as their element type.
281 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
282 if (SeltFD) {
283 QualType SeltTy = SeltFD->getType()->getDesugaredType();
284 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
285 // FIXME: This is gross, it would be nice if we could just
286 // pass back SeltTy and have clients deal with it. Is it worth
287 // supporting coerce to both LLVM and clang Types?
288 if (BT->isIntegerType()) {
289 uint64_t Size = Context.getTypeSize(SeltTy);
290 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
291 } else if (BT->getKind() == BuiltinType::Float) {
292 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
293 } else if (BT->getKind() == BuiltinType::Double) {
294 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
295 }
296 } else if (SeltTy->isPointerType()) {
297 // FIXME: It would be really nice if this could come out as
298 // the proper pointer type.
299 llvm::Type *PtrTy =
300 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
301 return ABIArgInfo::getCoerce(PtrTy);
302 }
303 }
304
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000305 uint64_t Size = Context.getTypeSize(RetTy);
306 if (Size == 8) {
307 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
308 } else if (Size == 16) {
309 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
310 } else if (Size == 32) {
311 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
312 } else if (Size == 64) {
313 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
314 } else {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000315 return ABIArgInfo::getIndirect(0);
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000316 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000317 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000318 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000319 }
320}
321
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000322ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000323 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000324 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000325 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000326 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000327 if (const RecordType *RT = Ty->getAsStructureType())
328 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000329 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000330
Daniel Dunbar3170c932009-02-05 01:50:07 +0000331 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000332 uint64_t Size = Context.getTypeSize(Ty);
333 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000334 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000335
336 // Expand structs with size <= 128-bits which consist only of
337 // basic types (int, long long, float, double, xxx*). This is
338 // non-recursive and does not ignore empty fields.
339 if (const RecordType *RT = Ty->getAsStructureType()) {
340 if (Context.getTypeSize(Ty) <= 4*32 &&
341 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
342 return ABIArgInfo::getExpand();
343 }
344
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000345 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000346 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000347 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000348 }
349}
350
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000351llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
352 CodeGenFunction &CGF) const {
353 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
354 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
355
356 CGBuilderTy &Builder = CGF.Builder;
357 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
358 "ap");
359 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
360 llvm::Type *PTy =
361 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
362 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
363
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000364 uint64_t Offset =
365 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000366 llvm::Value *NextAddr =
367 Builder.CreateGEP(Addr,
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000368 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000369 "ap.next");
370 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
371
372 return AddrTyped;
373}
374
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000375namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000376/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000377class X86_64ABIInfo : public ABIInfo {
378 enum Class {
379 Integer = 0,
380 SSE,
381 SSEUp,
382 X87,
383 X87Up,
384 ComplexX87,
385 NoClass,
386 Memory
387 };
388
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000389 /// merge - Implement the X86_64 ABI merging algorithm.
390 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000391 /// Merge an accumulating classification \arg Accum with a field
392 /// classification \arg Field.
393 ///
394 /// \param Accum - The accumulating classification. This should
395 /// always be either NoClass or the result of a previous merge
396 /// call. In addition, this should never be Memory (the caller
397 /// should just return Memory for the aggregate).
398 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000399
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000400 /// classify - Determine the x86_64 register classes in which the
401 /// given type T should be passed.
402 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000403 /// \param Lo - The classification for the parts of the type
404 /// residing in the low word of the containing object.
405 ///
406 /// \param Hi - The classification for the parts of the type
407 /// residing in the high word of the containing object.
408 ///
409 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000410 /// containing object. Some parameters are classified different
411 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000412 ///
413 /// If a word is unused its result will be NoClass; if a type should
414 /// be passed in Memory then at least the classification of \arg Lo
415 /// will be Memory.
416 ///
417 /// The \arg Lo class will be NoClass iff the argument is ignored.
418 ///
419 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000420 /// also be ComplexX87.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000421 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000422 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000423
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000424 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
425 /// to coerce to, chose the best way to pass Ty in the same place
426 /// that \arg CoerceTo would be passed, but while keeping the
427 /// emitted code as simple as possible.
428 ///
429 /// FIXME: Note, this should be cleaned up to just take an
430 /// enumeration of all the ways we might want to pass things,
431 /// instead of constructing an LLVM type. This makes this code more
432 /// explicit, and it makes it clearer that we are also doing this
433 /// for correctness in the case of passing scalar types.
434 ABIArgInfo getCoerceResult(QualType Ty,
435 const llvm::Type *CoerceTo,
436 ASTContext &Context) const;
437
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000438 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000439 ASTContext &Context) const;
440
441 ABIArgInfo classifyArgumentType(QualType Ty,
442 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000443 unsigned &neededInt,
444 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000445
446public:
447 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000448
449 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
450 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000451};
452}
453
Daniel Dunbarc4503572009-01-31 00:06:58 +0000454X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
455 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000456 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
457 // classified recursively so that always two fields are
458 // considered. The resulting class is calculated according to
459 // the classes of the fields in the eightbyte:
460 //
461 // (a) If both classes are equal, this is the resulting class.
462 //
463 // (b) If one of the classes is NO_CLASS, the resulting class is
464 // the other class.
465 //
466 // (c) If one of the classes is MEMORY, the result is the MEMORY
467 // class.
468 //
469 // (d) If one of the classes is INTEGER, the result is the
470 // INTEGER.
471 //
472 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
473 // MEMORY is used as class.
474 //
475 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000476 assert((Accum == NoClass || Accum == Integer ||
477 Accum == SSE || Accum == SSEUp) &&
478 "Invalid accumulated classification during merge.");
479 if (Accum == Field || Field == NoClass)
480 return Accum;
481 else if (Field == Memory)
482 return Memory;
483 else if (Accum == NoClass)
484 return Field;
485 else if (Accum == Integer || Field == Integer)
486 return Integer;
487 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
488 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000489 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000490 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000491}
492
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000493void X86_64ABIInfo::classify(QualType Ty,
494 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000495 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000496 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000497 // FIXME: This code can be simplified by introducing a simple value
498 // class for Class pairs with appropriate constructor methods for
499 // the various situations.
500
Daniel Dunbare28099b2009-02-22 04:48:22 +0000501 // FIXME: Some of the split computations are wrong; unaligned
502 // vectors shouldn't be passed in registers for example, so there is
503 // no chance they can straddle an eightbyte. Verify & simplify.
504
Daniel Dunbarc4503572009-01-31 00:06:58 +0000505 Lo = Hi = NoClass;
506
507 Class &Current = OffsetBase < 64 ? Lo : Hi;
508 Current = Memory;
509
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000510 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
511 BuiltinType::Kind k = BT->getKind();
512
Daniel Dunbar11434922009-01-26 21:26:08 +0000513 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000514 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000515 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000516 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000517 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000518 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000519 } else if (k == BuiltinType::LongDouble) {
520 Lo = X87;
521 Hi = X87Up;
522 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000523 // FIXME: _Decimal32 and _Decimal64 are SSE.
524 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000525 // FIXME: __int128 is (Integer, Integer).
Anders Carlsson708762b2009-02-26 17:31:15 +0000526 } else if (const EnumType *ET = Ty->getAsEnumType()) {
527 // Classify the underlying integer type.
528 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbar89588912009-02-26 20:52:22 +0000529 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000530 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000531 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000532 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbare28099b2009-02-22 04:48:22 +0000533 if (Size == 32) {
534 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
535 // float> as integer.
536 Current = Integer;
537
538 // If this type crosses an eightbyte boundary, it should be
539 // split.
540 uint64_t EB_Real = (OffsetBase) / 64;
541 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
542 if (EB_Real != EB_Imag)
543 Hi = Lo;
544 } else if (Size == 64) {
Daniel Dunbar0af99292009-02-22 04:16:10 +0000545 // gcc passes <1 x double> in memory. :(
546 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000547 return;
Daniel Dunbar0af99292009-02-22 04:16:10 +0000548
549 // gcc passes <1 x long long> as INTEGER.
550 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
551 Current = Integer;
552 else
553 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000554
555 // If this type crosses an eightbyte boundary, it should be
556 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000557 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000558 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000559 } else if (Size == 128) {
560 Lo = SSE;
561 Hi = SSEUp;
562 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000563 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000564 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000565
Daniel Dunbare33edf12009-01-30 18:40:10 +0000566 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar0af99292009-02-22 04:16:10 +0000567 if (ET->isIntegralType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000568 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000569 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000570 else if (Size <= 128)
571 Lo = Hi = Integer;
572 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000573 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000574 else if (ET == Context.DoubleTy)
575 Lo = Hi = SSE;
576 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000577 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000578
579 // If this complex type crosses an eightbyte boundary then it
580 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000581 uint64_t EB_Real = (OffsetBase) / 64;
582 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000583 if (Hi == NoClass && EB_Real != EB_Imag)
584 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000585 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
586 // Arrays are treated like structures.
587
588 uint64_t Size = Context.getTypeSize(Ty);
589
590 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
591 // than two eightbytes, ..., it has class MEMORY.
592 if (Size > 128)
593 return;
594
595 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
596 // fields, it has class MEMORY.
597 //
598 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000599 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000600 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000601
602 // Otherwise implement simplified merge. We could be smarter about
603 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000604 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000605 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
606 uint64_t ArraySize = AT->getSize().getZExtValue();
607 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
608 Class FieldLo, FieldHi;
609 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000610 Lo = merge(Lo, FieldLo);
611 Hi = merge(Hi, FieldHi);
612 if (Lo == Memory || Hi == Memory)
613 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000614 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000615
616 // Do post merger cleanup (see below). Only case we worry about is Memory.
617 if (Hi == Memory)
618 Lo = Memory;
619 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000620 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000621 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000622
623 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
624 // than two eightbytes, ..., it has class MEMORY.
625 if (Size > 128)
626 return;
627
628 const RecordDecl *RD = RT->getDecl();
629
630 // Assume variable sized types are passed in memory.
631 if (RD->hasFlexibleArrayMember())
632 return;
633
634 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
635
636 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000637 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000638 unsigned idx = 0;
639 for (RecordDecl::field_iterator i = RD->field_begin(),
640 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000641 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbardd81d442009-02-17 02:45:44 +0000642 bool BitField = i->isBitField();
Daniel Dunbar99037e52009-01-29 08:13:58 +0000643
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000644 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
645 // fields, it has class MEMORY.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000646 //
647 // Note, skip this test for bitfields, see below.
648 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar99037e52009-01-29 08:13:58 +0000649 Lo = Memory;
650 return;
651 }
652
Daniel Dunbar99037e52009-01-29 08:13:58 +0000653 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000654 //
655 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
656 // exceeds a single eightbyte, each is classified
657 // separately. Each eightbyte gets initialized to class
658 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000659 Class FieldLo, FieldHi;
Daniel Dunbardd81d442009-02-17 02:45:44 +0000660
661 // Bitfields require special handling, they do not force the
662 // structure to be passed in memory even if unaligned, and
663 // therefore they can straddle an eightbyte.
664 if (BitField) {
665 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
666 uint64_t Size =
667 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
668
669 uint64_t EB_Lo = Offset / 64;
670 uint64_t EB_Hi = (Offset + Size - 1) / 64;
671 FieldLo = FieldHi = NoClass;
672 if (EB_Lo) {
673 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
674 FieldLo = NoClass;
675 FieldHi = Integer;
676 } else {
677 FieldLo = Integer;
678 FieldHi = EB_Hi ? Integer : NoClass;
679 }
680 } else
681 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000682 Lo = merge(Lo, FieldLo);
683 Hi = merge(Hi, FieldHi);
684 if (Lo == Memory || Hi == Memory)
685 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000686 }
687
688 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
689 //
690 // (a) If one of the classes is MEMORY, the whole argument is
691 // passed in memory.
692 //
693 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
694
695 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000696 // the merge (just bail).
697 //
698 // The second condition occurs in the case of unions; for example
699 // union { _Complex double; unsigned; }.
700 if (Hi == Memory)
701 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000702 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000703 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000704 }
705}
706
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000707ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
708 const llvm::Type *CoerceTo,
709 ASTContext &Context) const {
710 if (CoerceTo == llvm::Type::Int64Ty) {
711 // Integer and pointer types will end up in a general purpose
712 // register.
Daniel Dunbar0af99292009-02-22 04:16:10 +0000713 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000714 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000715
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000716 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000717 // FIXME: It would probably be better to make CGFunctionInfo only
718 // map using canonical types than to canonize here.
719 QualType CTy = Context.getCanonicalType(Ty);
720
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000721 // Float and double end up in a single SSE reg.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000722 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000723 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000724
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000725 }
726
727 return ABIArgInfo::getCoerce(CoerceTo);
728}
Daniel Dunbarc4503572009-01-31 00:06:58 +0000729
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000730ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
731 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000732 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
733 // classification algorithm.
734 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000735 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000736
Daniel Dunbarc4503572009-01-31 00:06:58 +0000737 // Check some invariants.
738 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
739 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
740 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
741
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000742 const llvm::Type *ResType = 0;
743 switch (Lo) {
744 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000745 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000746
747 case SSEUp:
748 case X87Up:
749 assert(0 && "Invalid classification for lo word.");
750
Daniel Dunbarc4503572009-01-31 00:06:58 +0000751 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000752 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000753 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000754 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000755
756 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
757 // available register of the sequence %rax, %rdx is used.
758 case Integer:
759 ResType = llvm::Type::Int64Ty; break;
760
761 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
762 // available SSE register of the sequence %xmm0, %xmm1 is used.
763 case SSE:
764 ResType = llvm::Type::DoubleTy; break;
765
766 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
767 // returned on the X87 stack in %st0 as 80-bit x87 number.
768 case X87:
769 ResType = llvm::Type::X86_FP80Ty; break;
770
Daniel Dunbarc4503572009-01-31 00:06:58 +0000771 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
772 // part of the value is returned in %st0 and the imaginary part in
773 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000774 case ComplexX87:
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000775 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar3e030b42009-02-18 03:44:19 +0000776 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
777 llvm::Type::X86_FP80Ty,
778 NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000779 break;
780 }
781
782 switch (Hi) {
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000783 // Memory was handled previously and X87 should
784 // never occur as a hi class.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000785 case Memory:
786 case X87:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000787 assert(0 && "Invalid classification for hi word.");
788
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000789 case ComplexX87: // Previously handled.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000790 case NoClass: break;
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000791
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000792 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000793 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
794 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000795 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000796 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
797 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000798
799 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
800 // is passed in the upper half of the last used SSE register.
801 //
802 // SSEUP should always be preceeded by SSE, just widen.
803 case SSEUp:
804 assert(Lo == SSE && "Unexpected SSEUp classification.");
805 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
806 break;
807
808 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000809 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000810 //
811 // X87UP should always be preceeded by X87, so we don't need to do
812 // anything here.
813 case X87Up:
814 assert(Lo == X87 && "Unexpected X87Up classification.");
815 break;
816 }
817
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000818 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000819}
820
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000821ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000822 unsigned &neededInt,
823 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000824 X86_64ABIInfo::Class Lo, Hi;
825 classify(Ty, Context, 0, Lo, Hi);
826
827 // Check some invariants.
828 // FIXME: Enforce these by construction.
829 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
830 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
831 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
832
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000833 neededInt = 0;
834 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000835 const llvm::Type *ResType = 0;
836 switch (Lo) {
837 case NoClass:
838 return ABIArgInfo::getIgnore();
839
840 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
841 // on the stack.
842 case Memory:
843
844 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
845 // COMPLEX_X87, it is passed in memory.
846 case X87:
847 case ComplexX87:
Daniel Dunbar245f5532009-02-22 08:17:51 +0000848 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000849
850 case SSEUp:
851 case X87Up:
852 assert(0 && "Invalid classification for lo word.");
853
854 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
855 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
856 // and %r9 is used.
857 case Integer:
858 ++neededInt;
859 ResType = llvm::Type::Int64Ty;
860 break;
861
862 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
863 // available SSE register is used, the registers are taken in the
864 // order from %xmm0 to %xmm7.
865 case SSE:
866 ++neededSSE;
867 ResType = llvm::Type::DoubleTy;
868 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000869 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000870
871 switch (Hi) {
872 // Memory was handled previously, ComplexX87 and X87 should
873 // never occur as hi classes, and X87Up must be preceed by X87,
874 // which is passed in memory.
875 case Memory:
876 case X87:
877 case X87Up:
878 case ComplexX87:
879 assert(0 && "Invalid classification for hi word.");
880
881 case NoClass: break;
882 case Integer:
883 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
884 ++neededInt;
885 break;
886 case SSE:
887 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
888 ++neededSSE;
889 break;
890
891 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
892 // eightbyte is passed in the upper half of the last used SSE
893 // register.
894 case SSEUp:
895 assert(Lo == SSE && "Unexpected SSEUp classification.");
896 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
897 break;
898 }
899
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000900 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000901}
902
903void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
904 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
905
906 // Keep track of the number of assigned registers.
907 unsigned freeIntRegs = 6, freeSSERegs = 8;
908
909 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
910 // get assigned (in left-to-right order) for passing as follows...
911 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000912 it != ie; ++it) {
913 unsigned neededInt, neededSSE;
914 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
915
916 // AMD64-ABI 3.2.3p3: If there are no registers available for any
917 // eightbyte of an argument, the whole argument is passed on the
918 // stack. If registers have already been assigned for some
919 // eightbytes of such an argument, the assignments get reverted.
920 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
921 freeIntRegs -= neededInt;
922 freeSSERegs -= neededSSE;
923 } else {
Daniel Dunbar245f5532009-02-22 08:17:51 +0000924 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000925 }
926 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000927}
928
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000929static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
930 QualType Ty,
931 CodeGenFunction &CGF) {
932 llvm::Value *overflow_arg_area_p =
933 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
934 llvm::Value *overflow_arg_area =
935 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
936
937 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
938 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbarc5bcee42009-02-16 23:38:56 +0000939 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000940 if (Align > 8) {
Daniel Dunbarc5bcee42009-02-16 23:38:56 +0000941 // Note that we follow the ABI & gcc here, even though the type
942 // could in theory have an alignment greater than 16. This case
943 // shouldn't ever matter in practice.
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000944
Daniel Dunbarc5bcee42009-02-16 23:38:56 +0000945 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
946 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
947 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
948 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
949 llvm::Type::Int64Ty);
950 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
951 overflow_arg_area =
952 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
953 overflow_arg_area->getType(),
954 "overflow_arg_area.align");
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000955 }
956
957 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
958 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
959 llvm::Value *Res =
960 CGF.Builder.CreateBitCast(overflow_arg_area,
961 llvm::PointerType::getUnqual(LTy));
962
963 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
964 // l->overflow_arg_area + sizeof(type).
965 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
966 // an 8 byte boundary.
967
968 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
969 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
970 (SizeInBytes + 7) & ~7);
971 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
972 "overflow_arg_area.next");
973 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
974
975 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
976 return Res;
977}
978
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000979llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
980 CodeGenFunction &CGF) const {
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000981 // Assume that va_list type is correct; should be pointer to LLVM type:
982 // struct {
983 // i32 gp_offset;
984 // i32 fp_offset;
985 // i8* overflow_arg_area;
986 // i8* reg_save_area;
987 // };
988 unsigned neededInt, neededSSE;
989 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
990 neededInt, neededSSE);
991
992 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
993 // in the registers. If not go to step 7.
994 if (!neededInt && !neededSSE)
995 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
996
997 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
998 // general purpose registers needed to pass type and num_fp to hold
999 // the number of floating point registers needed.
1000
1001 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1002 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1003 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1004 //
1005 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1006 // register save space).
1007
1008 llvm::Value *InRegs = 0;
1009 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1010 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1011 if (neededInt) {
1012 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1013 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1014 InRegs =
1015 CGF.Builder.CreateICmpULE(gp_offset,
1016 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1017 48 - neededInt * 8),
1018 "fits_in_gp");
1019 }
1020
1021 if (neededSSE) {
1022 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1023 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1024 llvm::Value *FitsInFP =
1025 CGF.Builder.CreateICmpULE(fp_offset,
1026 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar90dafa12009-02-18 22:19:44 +00001027 176 - neededSSE * 16),
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001028 "fits_in_fp");
Daniel Dunbarf2313462009-02-18 22:05:01 +00001029 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001030 }
1031
1032 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1033 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1034 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1035 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1036
1037 // Emit code to load the value if it was passed in registers.
1038
1039 CGF.EmitBlock(InRegBlock);
1040
1041 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1042 // an offset of l->gp_offset and/or l->fp_offset. This may require
1043 // copying to a temporary location in case the parameter is passed
1044 // in different register classes or requires an alignment greater
1045 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001046 //
1047 // FIXME: This really results in shameful code when we end up
1048 // needing to collect arguments from different places; often what
1049 // should result in a simple assembling of a structure from
1050 // scattered addresses has many more loads than necessary. Can we
1051 // clean this up?
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001052 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1053 llvm::Value *RegAddr =
1054 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1055 "reg_save_area");
1056 if (neededInt && neededSSE) {
Daniel Dunbar55e5d892009-02-13 17:46:31 +00001057 // FIXME: Cleanup.
1058 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1059 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1060 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1061 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1062 const llvm::Type *TyLo = ST->getElementType(0);
1063 const llvm::Type *TyHi = ST->getElementType(1);
1064 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1065 "Unexpected ABI info for mixed regs");
1066 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1067 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1068 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1069 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1070 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1071 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1072 llvm::Value *V =
1073 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1074 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1075 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1076 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1077
1078 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001079 } else if (neededInt) {
1080 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1081 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1082 llvm::PointerType::getUnqual(LTy));
1083 } else {
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001084 if (neededSSE == 1) {
1085 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1086 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1087 llvm::PointerType::getUnqual(LTy));
1088 } else {
1089 assert(neededSSE == 2 && "Invalid number of needed registers!");
1090 // SSE registers are spaced 16 bytes apart in the register save
1091 // area, we need to collect the two eightbytes together.
1092 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1093 llvm::Value *RegAddrHi =
1094 CGF.Builder.CreateGEP(RegAddrLo,
1095 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1096 const llvm::Type *DblPtrTy =
1097 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1098 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1099 llvm::Type::DoubleTy,
1100 NULL);
1101 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1102 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1103 DblPtrTy));
1104 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1105 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1106 DblPtrTy));
1107 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1108 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1109 llvm::PointerType::getUnqual(LTy));
1110 }
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001111 }
1112
1113 // AMD64-ABI 3.5.7p5: Step 5. Set:
1114 // l->gp_offset = l->gp_offset + num_gp * 8
1115 // l->fp_offset = l->fp_offset + num_fp * 16.
1116 if (neededInt) {
1117 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1118 neededInt * 8);
1119 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1120 gp_offset_p);
1121 }
1122 if (neededSSE) {
1123 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1124 neededSSE * 16);
1125 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1126 fp_offset_p);
1127 }
1128 CGF.EmitBranch(ContBlock);
1129
1130 // Emit code to load the value if it was passed in memory.
1131
1132 CGF.EmitBlock(InMemBlock);
1133 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1134
1135 // Return the appropriate result.
1136
1137 CGF.EmitBlock(ContBlock);
1138 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1139 "vaarg.addr");
1140 ResAddr->reserveOperandSpace(2);
1141 ResAddr->addIncoming(RegAddr, InRegBlock);
1142 ResAddr->addIncoming(MemAddr, InMemBlock);
1143
1144 return ResAddr;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001145}
1146
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001147ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001148 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001149 if (RetTy->isVoidType()) {
1150 return ABIArgInfo::getIgnore();
1151 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001152 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001153 } else {
1154 return ABIArgInfo::getDirect();
1155 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001156}
1157
1158ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001159 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001160 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001161 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001162 } else {
1163 return ABIArgInfo::getDirect();
1164 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001165}
1166
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001167llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1168 CodeGenFunction &CGF) const {
1169 return 0;
1170}
1171
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001172const ABIInfo &CodeGenTypes::getABIInfo() const {
1173 if (TheABIInfo)
1174 return *TheABIInfo;
1175
1176 // For now we just cache this in the CodeGenTypes and don't bother
1177 // to free it.
1178 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1179 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001180 switch (getContext().Target.getPointerWidth(0)) {
1181 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001182 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001183 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +00001184 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001185 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001186 }
1187
1188 return *(TheABIInfo = new DefaultABIInfo);
1189}
1190
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001191/***/
1192
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001193CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1194 const llvm::SmallVector<QualType, 16> &ArgTys) {
1195 NumArgs = ArgTys.size();
1196 Args = new ArgInfo[1 + NumArgs];
1197 Args[0].type = ResTy;
1198 for (unsigned i = 0; i < NumArgs; ++i)
1199 Args[1 + i].type = ArgTys[i];
1200}
1201
1202/***/
1203
Daniel Dunbar56273772008-09-17 00:51:38 +00001204void CodeGenTypes::GetExpandedTypes(QualType Ty,
1205 std::vector<const llvm::Type*> &ArgTys) {
1206 const RecordType *RT = Ty->getAsStructureType();
1207 assert(RT && "Can only expand structure types.");
1208 const RecordDecl *RD = RT->getDecl();
1209 assert(!RD->hasFlexibleArrayMember() &&
1210 "Cannot expand structure with flexible array.");
1211
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001212 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +00001213 e = RD->field_end(); i != e; ++i) {
1214 const FieldDecl *FD = *i;
1215 assert(!FD->isBitField() &&
1216 "Cannot expand structure with bit-field members.");
1217
1218 QualType FT = FD->getType();
1219 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1220 GetExpandedTypes(FT, ArgTys);
1221 } else {
1222 ArgTys.push_back(ConvertType(FT));
1223 }
1224 }
1225}
1226
1227llvm::Function::arg_iterator
1228CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1229 llvm::Function::arg_iterator AI) {
1230 const RecordType *RT = Ty->getAsStructureType();
1231 assert(RT && "Can only expand structure types.");
1232
1233 RecordDecl *RD = RT->getDecl();
1234 assert(LV.isSimple() &&
1235 "Unexpected non-simple lvalue during struct expansion.");
1236 llvm::Value *Addr = LV.getAddress();
1237 for (RecordDecl::field_iterator i = RD->field_begin(),
1238 e = RD->field_end(); i != e; ++i) {
1239 FieldDecl *FD = *i;
1240 QualType FT = FD->getType();
1241
1242 // FIXME: What are the right qualifiers here?
1243 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1244 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1245 AI = ExpandTypeFromArgs(FT, LV, AI);
1246 } else {
1247 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1248 ++AI;
1249 }
1250 }
1251
1252 return AI;
1253}
1254
1255void
1256CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1257 llvm::SmallVector<llvm::Value*, 16> &Args) {
1258 const RecordType *RT = Ty->getAsStructureType();
1259 assert(RT && "Can only expand structure types.");
1260
1261 RecordDecl *RD = RT->getDecl();
1262 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1263 llvm::Value *Addr = RV.getAggregateAddr();
1264 for (RecordDecl::field_iterator i = RD->field_begin(),
1265 e = RD->field_end(); i != e; ++i) {
1266 FieldDecl *FD = *i;
1267 QualType FT = FD->getType();
1268
1269 // FIXME: What are the right qualifiers here?
1270 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1271 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1272 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1273 } else {
1274 RValue RV = EmitLoadOfLValue(LV, FT);
1275 assert(RV.isScalar() &&
1276 "Unexpected non-scalar rvalue during struct expansion.");
1277 Args.push_back(RV.getScalarVal());
1278 }
1279 }
1280}
1281
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001282/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1283/// a pointer to an object of type \arg Ty.
1284///
1285/// This safely handles the case when the src type is smaller than the
1286/// destination type; in this situation the values of bits which not
1287/// present in the src are undefined.
1288static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1289 const llvm::Type *Ty,
1290 CodeGenFunction &CGF) {
1291 const llvm::Type *SrcTy =
1292 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1293 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1294 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1295
Daniel Dunbarb225be42009-02-03 05:59:18 +00001296 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001297 if (SrcSize == DstSize) {
1298 llvm::Value *Casted =
1299 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001300 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1301 // FIXME: Use better alignment / avoid requiring aligned load.
1302 Load->setAlignment(1);
1303 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001304 } else {
1305 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1306
1307 // Otherwise do coercion through memory. This is stupid, but
1308 // simple.
1309 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1310 llvm::Value *Casted =
1311 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001312 llvm::StoreInst *Store =
1313 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1314 // FIXME: Use better alignment / avoid requiring aligned store.
1315 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001316 return CGF.Builder.CreateLoad(Tmp);
1317 }
1318}
1319
1320/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1321/// where the source and destination may have different types.
1322///
1323/// This safely handles the case when the src type is larger than the
1324/// destination type; the upper bits of the src will be lost.
1325static void CreateCoercedStore(llvm::Value *Src,
1326 llvm::Value *DstPtr,
1327 CodeGenFunction &CGF) {
1328 const llvm::Type *SrcTy = Src->getType();
1329 const llvm::Type *DstTy =
1330 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1331
1332 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1333 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1334
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001335 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001336 if (SrcSize == DstSize) {
1337 llvm::Value *Casted =
1338 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001339 // FIXME: Use better alignment / avoid requiring aligned store.
1340 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001341 } else {
1342 assert(SrcSize > DstSize && "Coercion is missing bits!");
1343
1344 // Otherwise do coercion through memory. This is stupid, but
1345 // simple.
1346 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1347 CGF.Builder.CreateStore(Src, Tmp);
1348 llvm::Value *Casted =
1349 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001350 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1351 // FIXME: Use better alignment / avoid requiring aligned load.
1352 Load->setAlignment(1);
1353 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001354 }
1355}
1356
Daniel Dunbar56273772008-09-17 00:51:38 +00001357/***/
1358
Daniel Dunbar88b53962009-02-02 22:03:45 +00001359bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001360 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001361}
1362
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001363const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001364CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001365 std::vector<const llvm::Type*> ArgTys;
1366
1367 const llvm::Type *ResultType = 0;
1368
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001369 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001370 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001371 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001372 case ABIArgInfo::Expand:
1373 assert(0 && "Invalid ABI kind for return argument");
1374
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001375 case ABIArgInfo::Direct:
1376 ResultType = ConvertType(RetTy);
1377 break;
1378
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001379 case ABIArgInfo::Indirect: {
1380 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001381 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001382 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001383 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1384 break;
1385 }
1386
Daniel Dunbar11434922009-01-26 21:26:08 +00001387 case ABIArgInfo::Ignore:
1388 ResultType = llvm::Type::VoidTy;
1389 break;
1390
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001391 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001392 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001393 break;
1394 }
1395
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001396 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1397 ie = FI.arg_end(); it != ie; ++it) {
1398 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001399
1400 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001401 case ABIArgInfo::Ignore:
1402 break;
1403
Daniel Dunbar56273772008-09-17 00:51:38 +00001404 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001405 ArgTys.push_back(AI.getCoerceToType());
1406 break;
1407
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001408 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001409 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001410 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1411 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001412 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001413 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001414
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001415 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001416 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001417 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001418
1419 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001420 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001421 break;
1422 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001423 }
1424
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001425 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001426}
1427
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001428void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001429 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001430 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001431 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001432 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001433
1434 if (TargetDecl) {
1435 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001436 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001437 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001438 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001439 if (TargetDecl->getAttr<PureAttr>())
1440 FuncAttrs |= llvm::Attribute::ReadOnly;
1441 if (TargetDecl->getAttr<ConstAttr>())
1442 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001443 }
1444
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001445 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001446 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001447 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001448 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001449 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001450 if (RetTy->isPromotableIntegerType()) {
1451 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001452 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001453 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001454 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001455 }
1456 }
1457 break;
1458
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001459 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001460 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001461 llvm::Attribute::StructRet |
1462 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001463 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001464 break;
1465
Daniel Dunbar11434922009-01-26 21:26:08 +00001466 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001467 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001468 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001469
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001470 case ABIArgInfo::Expand:
1471 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001472 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001473
Devang Patela2c69122008-09-26 22:53:57 +00001474 if (RetAttrs)
1475 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001476 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1477 ie = FI.arg_end(); it != ie; ++it) {
1478 QualType ParamType = it->type;
1479 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001480 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001481
1482 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001483 case ABIArgInfo::Coerce:
1484 break;
1485
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001486 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001487 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarca008822009-02-05 01:31:19 +00001488 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001489 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001490 break;
1491
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001492 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001493 if (ParamType->isPromotableIntegerType()) {
1494 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001495 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001496 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001497 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001498 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001499 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001500 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001501
Daniel Dunbar11434922009-01-26 21:26:08 +00001502 case ABIArgInfo::Ignore:
1503 // Skip increment, no matching LLVM parameter.
1504 continue;
1505
Daniel Dunbar56273772008-09-17 00:51:38 +00001506 case ABIArgInfo::Expand: {
1507 std::vector<const llvm::Type*> Tys;
1508 // FIXME: This is rather inefficient. Do we ever actually need
1509 // to do anything here? The result should be just reconstructed
1510 // on the other side, so extension should be a non-issue.
1511 getTypes().GetExpandedTypes(ParamType, Tys);
1512 Index += Tys.size();
1513 continue;
1514 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001515 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001516
Devang Patel761d7f72008-09-25 21:02:23 +00001517 if (Attributes)
1518 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001519 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001520 }
Devang Patela2c69122008-09-26 22:53:57 +00001521 if (FuncAttrs)
1522 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001523}
1524
Daniel Dunbar88b53962009-02-02 22:03:45 +00001525void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1526 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001527 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001528 // FIXME: We no longer need the types from FunctionArgList; lift up
1529 // and simplify.
1530
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001531 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1532 llvm::Function::arg_iterator AI = Fn->arg_begin();
1533
1534 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001535 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001536 AI->setName("agg.result");
1537 ++AI;
1538 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001539
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001540 assert(FI.arg_size() == Args.size() &&
1541 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001542 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001543 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001544 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001545 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001546 QualType Ty = info_it->type;
1547 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001548
1549 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001550 case ABIArgInfo::Indirect: {
1551 llvm::Value* V = AI;
1552 if (hasAggregateLLVMType(Ty)) {
1553 // Do nothing, aggregates and complex variables are accessed by
1554 // reference.
1555 } else {
1556 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001557 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001558 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1559 // This must be a promotion, for something like
1560 // "void a(x) short x; {..."
1561 V = EmitScalarConversion(V, Ty, Arg->getType());
1562 }
1563 }
1564 EmitParmDecl(*Arg, V);
1565 break;
1566 }
1567
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001568 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001569 assert(AI != Fn->arg_end() && "Argument mismatch!");
1570 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001571 if (hasAggregateLLVMType(Ty)) {
1572 // Create a temporary alloca to hold the argument; the rest of
1573 // codegen expects to access aggregates & complex values by
1574 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001575 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001576 Builder.CreateStore(AI, V);
1577 } else {
1578 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1579 // This must be a promotion, for something like
1580 // "void a(x) short x; {..."
1581 V = EmitScalarConversion(V, Ty, Arg->getType());
1582 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001583 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001584 EmitParmDecl(*Arg, V);
1585 break;
1586 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001587
1588 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001589 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001590 // we need to create a temporary and reconstruct it from the
1591 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001592 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001593 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001594 (Name + ".addr").c_str());
1595 // FIXME: What are the right qualifiers here?
1596 llvm::Function::arg_iterator End =
1597 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1598 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001599
Daniel Dunbar56273772008-09-17 00:51:38 +00001600 // Name the arguments used in expansion and increment AI.
1601 unsigned Index = 0;
1602 for (; AI != End; ++AI, ++Index)
1603 AI->setName(Name + "." + llvm::utostr(Index));
1604 continue;
1605 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001606
1607 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001608 // Initialize the local variable appropriately.
1609 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001610 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001611 } else {
1612 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1613 }
1614
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001615 // Skip increment, no matching LLVM parameter.
1616 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001617
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001618 case ABIArgInfo::Coerce: {
1619 assert(AI != Fn->arg_end() && "Argument mismatch!");
1620 // FIXME: This is very wasteful; EmitParmDecl is just going to
1621 // drop the result in a new alloca anyway, so we could just
1622 // store into that directly if we broke the abstraction down
1623 // more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001624 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001625 CreateCoercedStore(AI, V, *this);
1626 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001627 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001628 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001629 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1630 // This must be a promotion, for something like
1631 // "void a(x) short x; {..."
1632 V = EmitScalarConversion(V, Ty, Arg->getType());
1633 }
1634 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001635 EmitParmDecl(*Arg, V);
1636 break;
1637 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001638 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001639
1640 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001641 }
1642 assert(AI == Fn->arg_end() && "Argument mismatch!");
1643}
1644
Daniel Dunbar88b53962009-02-02 22:03:45 +00001645void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001646 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001647 llvm::Value *RV = 0;
1648
1649 // Functions with no result always return void.
1650 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001651 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001652 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001653
1654 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001655 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001656 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001657 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1658 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1659 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1660 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1661 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001662 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1663 false);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001664 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001665 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001666
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001667 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001668 // The internal return value temp always will have
1669 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001670 RV = Builder.CreateLoad(ReturnValue);
1671 break;
1672
Daniel Dunbar11434922009-01-26 21:26:08 +00001673 case ABIArgInfo::Ignore:
1674 break;
1675
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001676 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001677 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001678 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001679
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001680 case ABIArgInfo::Expand:
1681 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001682 }
1683 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001684
1685 if (RV) {
1686 Builder.CreateRet(RV);
1687 } else {
1688 Builder.CreateRetVoid();
1689 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001690}
1691
Daniel Dunbar88b53962009-02-02 22:03:45 +00001692RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1693 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001694 const CallArgList &CallArgs,
1695 const Decl *TargetDecl) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001696 // FIXME: We no longer need the types from CallArgs; lift up and
1697 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001698 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001699
1700 // Handle struct-return functions by passing a pointer to the
1701 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001702 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001703 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00001704 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001705 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001706 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001707 }
1708
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001709 assert(CallInfo.arg_size() == CallArgs.size() &&
1710 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001711 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001712 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001713 I != E; ++I, ++info_it) {
1714 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001715 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001716
1717 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001718 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001719 if (RV.isScalar() || RV.isComplex()) {
1720 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001721 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001722 if (RV.isScalar())
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001723 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001724 else
1725 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1726 } else {
1727 Args.push_back(RV.getAggregateAddr());
1728 }
1729 break;
1730
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001731 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001732 if (RV.isScalar()) {
1733 Args.push_back(RV.getScalarVal());
1734 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001735 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1736 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1737 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1738 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00001739 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001740 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00001741 }
1742 break;
1743
Daniel Dunbar11434922009-01-26 21:26:08 +00001744 case ABIArgInfo::Ignore:
1745 break;
1746
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001747 case ABIArgInfo::Coerce: {
1748 // FIXME: Avoid the conversion through memory if possible.
1749 llvm::Value *SrcPtr;
1750 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00001751 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001752 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001753 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001754 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001755 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1756 } else
1757 SrcPtr = RV.getAggregateAddr();
1758 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1759 *this));
1760 break;
1761 }
1762
Daniel Dunbar56273772008-09-17 00:51:38 +00001763 case ABIArgInfo::Expand:
1764 ExpandTypeToArgs(I->second, RV, Args);
1765 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001766 }
1767 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001768
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001769 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel761d7f72008-09-25 21:02:23 +00001770 CodeGen::AttributeListType AttributeList;
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001771 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001772 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1773 AttributeList.end());
Daniel Dunbar725ad312009-01-31 02:19:00 +00001774
Daniel Dunbard14151d2009-03-02 04:32:35 +00001775 llvm::CallSite CS;
1776 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
1777 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001778 } else {
1779 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbard14151d2009-03-02 04:32:35 +00001780 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1781 &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001782 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001783 }
1784
Daniel Dunbard14151d2009-03-02 04:32:35 +00001785 CS.setAttributes(Attrs);
1786 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1787 CS.setCallingConv(F->getCallingConv());
1788
1789 // If the call doesn't return, finish the basic block and clear the
1790 // insertion point; this allows the rest of IRgen to discard
1791 // unreachable code.
1792 if (CS.doesNotReturn()) {
1793 Builder.CreateUnreachable();
1794 Builder.ClearInsertionPoint();
1795
1796 // FIXME: For now, emit a dummy basic block because expr
1797 // emitters in generally are not ready to handle emitting
1798 // expressions at unreachable points.
1799 EnsureInsertPoint();
1800
1801 // Return a reasonable RValue.
1802 return GetUndefRValue(RetTy);
1803 }
1804
1805 llvm::Instruction *CI = CS.getInstruction();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001806 if (CI->getType() != llvm::Type::VoidTy)
1807 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001808
1809 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001810 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001811 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001812 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001813 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001814 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001815 else
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001816 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001817
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001818 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001819 if (RetTy->isAnyComplexType()) {
1820 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1821 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1822 return RValue::getComplex(std::make_pair(Real, Imag));
1823 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001824 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001825 Builder.CreateStore(CI, V);
1826 return RValue::getAggregate(V);
1827 } else
1828 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001829
Daniel Dunbar11434922009-01-26 21:26:08 +00001830 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001831 // If we are ignoring an argument that had a result, make sure to
1832 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001833 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001834
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001835 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001836 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001837 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001838 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001839 if (RetTy->isAnyComplexType())
1840 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001841 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001842 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001843 else
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001844 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001845 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001846
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001847 case ABIArgInfo::Expand:
1848 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001849 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001850
1851 assert(0 && "Unhandled ABIArgInfo::Kind");
1852 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001853}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001854
1855/* VarArg handling */
1856
1857llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1858 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1859}