blob: 1ae4a87606c0011d183058fef28d2d63f404ef97 [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 Dunbar90e43452009-03-02 04:32:35 +000025#include "llvm/Support/CallSite.h"
Daniel Dunbare09a9692009-01-24 08:32:22 +000026#include "llvm/Support/CommandLine.h"
Daniel Dunbar3cfcec72009-02-12 09:04:14 +000027#include "llvm/Support/MathExtras.h"
Daniel Dunbar9f4874e2009-02-04 23:24:38 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000030
31#include "ABIInfo.h"
32
Daniel Dunbara8f02052008-09-08 21:33:45 +000033using namespace clang;
34using namespace CodeGen;
35
36/***/
37
Daniel Dunbara8f02052008-09-08 21:33:45 +000038// FIXME: Use iterator and sidestep silly type array creation.
39
Daniel Dunbar34bda882009-02-02 23:23:47 +000040const
Douglas Gregor4fa58902009-02-26 23:50:07 +000041CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000042 return getFunctionInfo(FTNP->getResultType(),
43 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000044}
45
Daniel Dunbar34bda882009-02-02 23:23:47 +000046const
Douglas Gregor4fa58902009-02-26 23:50:07 +000047CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000048 llvm::SmallVector<QualType, 16> ArgTys;
49 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000050 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000051 ArgTys.push_back(FTP->getArgType(i));
52 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000053}
54
Daniel Dunbar34bda882009-02-02 23:23:47 +000055const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000056 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +000057 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar34bda882009-02-02 23:23:47 +000058 return getFunctionInfo(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +000059 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000060}
61
Daniel Dunbar34bda882009-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 Lattner9408eb12009-02-20 06:23:21 +000067 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbara8f02052008-09-08 21:33:45 +000068 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000069 ArgTys.push_back((*i)->getType());
70 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000071}
72
Daniel Dunbar34bda882009-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 Dunbarebbb8f32009-01-31 02:19:00 +000077 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
78 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000079 ArgTys.push_back(i->second);
80 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000081}
82
Daniel Dunbar34bda882009-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 Dunbar9fc15a82009-02-02 21:43:58 +000087 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
88 i != e; ++i)
Daniel Dunbar34bda882009-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 Dunbardcf19d12009-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 Dunbare92e0ab2009-02-03 05:31:23 +0000104 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000105 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000106 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000107
108 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000109 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000110
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000111 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000112}
113
114/***/
115
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000116ABIInfo::~ABIInfo() {}
117
Daniel Dunbar9f4874e2009-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 Dunbar9f4874e2009-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 Dunbar88dde9b2009-02-05 08:00:50 +0000133 case Indirect:
134 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar9f4874e2009-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 Dunbar99eebc62008-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 Gregor5d764842009-01-09 17:18:27 +0000155 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-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 Gregor5d764842009-01-09 17:18:27 +0000182 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-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 Gregor5d764842009-01-09 17:18:27 +0000213 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-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 Dunbarf98eeff2008-10-13 17:02:26 +0000231namespace {
232/// DefaultABIInfo - The default implementation for ABI specific
233/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000234/// self-consistent and sensible LLVM IR generation, but does not
235/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000236class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-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 Dunbarf98eeff2008-10-13 17:02:26 +0000242
Daniel Dunbar749e36b2009-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 Dunbar7fbcf9c2009-02-10 20:44:09 +0000249
250 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
251 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000252};
253
254/// X86_32ABIInfo - The X86-32 ABI information.
255class X86_32ABIInfo : public ABIInfo {
256public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000257 ABIArgInfo classifyReturnType(QualType RetTy,
258 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000259
Daniel Dunbar749e36b2009-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 Dunbar7fbcf9c2009-02-10 20:44:09 +0000269
270 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
271 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000272};
273}
274
275ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
276 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000277 if (RetTy->isVoidType()) {
278 return ABIArgInfo::getIgnore();
279 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-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 Dunbar73d66602008-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 Dunbar88dde9b2009-02-05 08:00:50 +0000315 return ABIArgInfo::getIndirect(0);
Daniel Dunbar73d66602008-09-10 07:04:09 +0000316 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000317 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000318 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000319 }
320}
321
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000322ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000323 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000324 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000325 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000326 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000327 if (const RecordType *RT = Ty->getAsStructureType())
328 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000329 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000330
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000331 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000332 uint64_t Size = Context.getTypeSize(Ty);
333 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000334 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-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 Dunbar88dde9b2009-02-05 08:00:50 +0000345 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000346 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000347 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000348 }
349}
350
Daniel Dunbar7fbcf9c2009-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 Dunbarbae4b662009-02-18 22:28:45 +0000364 uint64_t Offset =
365 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000366 llvm::Value *NextAddr =
367 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000368 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000369 "ap.next");
370 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
371
372 return AddrTyped;
373}
374
Daniel Dunbare09a9692009-01-24 08:32:22 +0000375namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000376/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-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 Dunbar11dc6772009-01-30 08:09:32 +0000389 /// merge - Implement the X86_64 ABI merging algorithm.
390 ///
Daniel Dunbar64b132f2009-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 Dunbar11dc6772009-01-30 08:09:32 +0000399
Daniel Dunbare09a9692009-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 Dunbar64b132f2009-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 Dunbar2a2dce32009-01-30 22:40:15 +0000410 /// containing object. Some parameters are classified different
411 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-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 Dunbar92e88642009-02-17 07:55:55 +0000420 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000421 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000422 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000423
Daniel Dunbar87c4dc92009-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 Dunbar749e36b2009-02-03 06:51:18 +0000438 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000439 ASTContext &Context) const;
440
441 ABIArgInfo classifyArgumentType(QualType Ty,
442 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000443 unsigned &neededInt,
444 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000445
446public:
447 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000448
449 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
450 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000451};
452}
453
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000454X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
455 Class Field) const {
Daniel Dunbar11dc6772009-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 Dunbar78d7d452009-03-06 17:50:25 +0000476
477 // Accum should never be memory (we should have returned) or
478 // ComplexX87 (because this cannot be passed in a structure).
479 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000480 "Invalid accumulated classification during merge.");
481 if (Accum == Field || Field == NoClass)
482 return Accum;
483 else if (Field == Memory)
484 return Memory;
485 else if (Accum == NoClass)
486 return Field;
487 else if (Accum == Integer || Field == Integer)
488 return Integer;
489 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
490 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000491 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000492 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000493}
494
Daniel Dunbare09a9692009-01-24 08:32:22 +0000495void X86_64ABIInfo::classify(QualType Ty,
496 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000497 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000498 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000499 // FIXME: This code can be simplified by introducing a simple value
500 // class for Class pairs with appropriate constructor methods for
501 // the various situations.
502
Daniel Dunbard97f5952009-02-22 04:48:22 +0000503 // FIXME: Some of the split computations are wrong; unaligned
504 // vectors shouldn't be passed in registers for example, so there is
505 // no chance they can straddle an eightbyte. Verify & simplify.
506
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000507 Lo = Hi = NoClass;
508
509 Class &Current = OffsetBase < 64 ? Lo : Hi;
510 Current = Memory;
511
Daniel Dunbare09a9692009-01-24 08:32:22 +0000512 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
513 BuiltinType::Kind k = BT->getKind();
514
Daniel Dunbar1358b202009-01-26 21:26:08 +0000515 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000516 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000517 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000518 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000519 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000520 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000521 } else if (k == BuiltinType::LongDouble) {
522 Lo = X87;
523 Hi = X87Up;
524 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000525 // FIXME: _Decimal32 and _Decimal64 are SSE.
526 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000527 // FIXME: __int128 is (Integer, Integer).
Anders Carlsson1d234462009-02-26 17:31:15 +0000528 } else if (const EnumType *ET = Ty->getAsEnumType()) {
529 // Classify the underlying integer type.
530 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000531 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000532 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000533 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000534 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000535 if (Size == 32) {
536 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
537 // float> as integer.
538 Current = Integer;
539
540 // If this type crosses an eightbyte boundary, it should be
541 // split.
542 uint64_t EB_Real = (OffsetBase) / 64;
543 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
544 if (EB_Real != EB_Imag)
545 Hi = Lo;
546 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000547 // gcc passes <1 x double> in memory. :(
548 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000549 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000550
551 // gcc passes <1 x long long> as INTEGER.
552 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
553 Current = Integer;
554 else
555 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000556
557 // If this type crosses an eightbyte boundary, it should be
558 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000559 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000560 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000561 } else if (Size == 128) {
562 Lo = SSE;
563 Hi = SSEUp;
564 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000565 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000566 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000567
Daniel Dunbare413f532009-01-30 18:40:10 +0000568 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000569 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000570 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000571 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000572 else if (Size <= 128)
573 Lo = Hi = Integer;
574 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000575 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000576 else if (ET == Context.DoubleTy)
577 Lo = Hi = SSE;
578 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000579 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000580
581 // If this complex type crosses an eightbyte boundary then it
582 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000583 uint64_t EB_Real = (OffsetBase) / 64;
584 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000585 if (Hi == NoClass && EB_Real != EB_Imag)
586 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000587 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
588 // Arrays are treated like structures.
589
590 uint64_t Size = Context.getTypeSize(Ty);
591
592 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
593 // than two eightbytes, ..., it has class MEMORY.
594 if (Size > 128)
595 return;
596
597 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
598 // fields, it has class MEMORY.
599 //
600 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000601 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000602 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000603
604 // Otherwise implement simplified merge. We could be smarter about
605 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000606 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000607 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
608 uint64_t ArraySize = AT->getSize().getZExtValue();
609 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
610 Class FieldLo, FieldHi;
611 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000612 Lo = merge(Lo, FieldLo);
613 Hi = merge(Hi, FieldHi);
614 if (Lo == Memory || Hi == Memory)
615 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000616 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000617
618 // Do post merger cleanup (see below). Only case we worry about is Memory.
619 if (Hi == Memory)
620 Lo = Memory;
621 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000622 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000623 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000624
625 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
626 // than two eightbytes, ..., it has class MEMORY.
627 if (Size > 128)
628 return;
629
630 const RecordDecl *RD = RT->getDecl();
631
632 // Assume variable sized types are passed in memory.
633 if (RD->hasFlexibleArrayMember())
634 return;
635
636 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
637
638 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000639 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000640 unsigned idx = 0;
641 for (RecordDecl::field_iterator i = RD->field_begin(),
642 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000643 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000644 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000645
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000646 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
647 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000648 //
649 // Note, skip this test for bitfields, see below.
650 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000651 Lo = Memory;
652 return;
653 }
654
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000655 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000656 //
657 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
658 // exceeds a single eightbyte, each is classified
659 // separately. Each eightbyte gets initialized to class
660 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000661 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000662
663 // Bitfields require special handling, they do not force the
664 // structure to be passed in memory even if unaligned, and
665 // therefore they can straddle an eightbyte.
666 if (BitField) {
667 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
668 uint64_t Size =
669 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
670
671 uint64_t EB_Lo = Offset / 64;
672 uint64_t EB_Hi = (Offset + Size - 1) / 64;
673 FieldLo = FieldHi = NoClass;
674 if (EB_Lo) {
675 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
676 FieldLo = NoClass;
677 FieldHi = Integer;
678 } else {
679 FieldLo = Integer;
680 FieldHi = EB_Hi ? Integer : NoClass;
681 }
682 } else
683 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000684 Lo = merge(Lo, FieldLo);
685 Hi = merge(Hi, FieldHi);
686 if (Lo == Memory || Hi == Memory)
687 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000688 }
689
690 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
691 //
692 // (a) If one of the classes is MEMORY, the whole argument is
693 // passed in memory.
694 //
695 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
696
697 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000698 // the merge (just bail).
699 //
700 // The second condition occurs in the case of unions; for example
701 // union { _Complex double; unsigned; }.
702 if (Hi == Memory)
703 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000704 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000705 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000706 }
707}
708
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000709ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
710 const llvm::Type *CoerceTo,
711 ASTContext &Context) const {
712 if (CoerceTo == llvm::Type::Int64Ty) {
713 // Integer and pointer types will end up in a general purpose
714 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000715 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000716 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000717
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000718 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000719 // FIXME: It would probably be better to make CGFunctionInfo only
720 // map using canonical types than to canonize here.
721 QualType CTy = Context.getCanonicalType(Ty);
722
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000723 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000724 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000725 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000726
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000727 }
728
729 return ABIArgInfo::getCoerce(CoerceTo);
730}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000731
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000732ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
733 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000734 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
735 // classification algorithm.
736 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000737 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000738
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000739 // Check some invariants.
740 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
741 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
742 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
743
Daniel Dunbare09a9692009-01-24 08:32:22 +0000744 const llvm::Type *ResType = 0;
745 switch (Lo) {
746 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000747 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000748
749 case SSEUp:
750 case X87Up:
751 assert(0 && "Invalid classification for lo word.");
752
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000753 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000754 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000755 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000756 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000757
758 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
759 // available register of the sequence %rax, %rdx is used.
760 case Integer:
761 ResType = llvm::Type::Int64Ty; break;
762
763 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
764 // available SSE register of the sequence %xmm0, %xmm1 is used.
765 case SSE:
766 ResType = llvm::Type::DoubleTy; break;
767
768 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
769 // returned on the X87 stack in %st0 as 80-bit x87 number.
770 case X87:
771 ResType = llvm::Type::X86_FP80Ty; break;
772
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000773 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
774 // part of the value is returned in %st0 and the imaginary part in
775 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000776 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000777 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000778 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
779 llvm::Type::X86_FP80Ty,
780 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000781 break;
782 }
783
784 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000785 // Memory was handled previously and X87 should
786 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000787 case Memory:
788 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000789 assert(0 && "Invalid classification for hi word.");
790
Daniel Dunbar92e88642009-02-17 07:55:55 +0000791 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000792 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000793
Daniel Dunbare09a9692009-01-24 08:32:22 +0000794 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000795 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
796 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000797 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000798 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
799 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000800
801 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
802 // is passed in the upper half of the last used SSE register.
803 //
804 // SSEUP should always be preceeded by SSE, just widen.
805 case SSEUp:
806 assert(Lo == SSE && "Unexpected SSEUp classification.");
807 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
808 break;
809
810 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000811 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000812 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000813 // If X87Up is preceeded by X87, we don't need to do
814 // anything. However, in some cases with unions it may not be
815 // preceeded by X87. In such situations we follow gcc and pass the
816 // extra bits in an SSE reg.
817 if (Lo != X87)
818 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000819 break;
820 }
821
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000822 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000823}
824
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000825ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000826 unsigned &neededInt,
827 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000828 X86_64ABIInfo::Class Lo, Hi;
829 classify(Ty, Context, 0, Lo, Hi);
830
831 // Check some invariants.
832 // FIXME: Enforce these by construction.
833 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
834 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
835 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
836
Daniel Dunbare978cb92009-02-10 17:06:09 +0000837 neededInt = 0;
838 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000839 const llvm::Type *ResType = 0;
840 switch (Lo) {
841 case NoClass:
842 return ABIArgInfo::getIgnore();
843
844 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
845 // on the stack.
846 case Memory:
847
848 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
849 // COMPLEX_X87, it is passed in memory.
850 case X87:
851 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000852 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000853
854 case SSEUp:
855 case X87Up:
856 assert(0 && "Invalid classification for lo word.");
857
858 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
859 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
860 // and %r9 is used.
861 case Integer:
862 ++neededInt;
863 ResType = llvm::Type::Int64Ty;
864 break;
865
866 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
867 // available SSE register is used, the registers are taken in the
868 // order from %xmm0 to %xmm7.
869 case SSE:
870 ++neededSSE;
871 ResType = llvm::Type::DoubleTy;
872 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000873 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000874
875 switch (Hi) {
876 // Memory was handled previously, ComplexX87 and X87 should
877 // never occur as hi classes, and X87Up must be preceed by X87,
878 // which is passed in memory.
879 case Memory:
880 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000881 case ComplexX87:
882 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000883 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000884
885 case NoClass: break;
886 case Integer:
887 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
888 ++neededInt;
889 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000890
891 // X87Up generally doesn't occur here (long double is passed in
892 // memory), except in situations involving unions.
893 case X87Up:
894 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000895 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
896 ++neededSSE;
897 break;
898
899 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
900 // eightbyte is passed in the upper half of the last used SSE
901 // register.
902 case SSEUp:
903 assert(Lo == SSE && "Unexpected SSEUp classification.");
904 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
905 break;
906 }
907
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000908 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000909}
910
911void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
912 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
913
914 // Keep track of the number of assigned registers.
915 unsigned freeIntRegs = 6, freeSSERegs = 8;
916
917 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
918 // get assigned (in left-to-right order) for passing as follows...
919 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +0000920 it != ie; ++it) {
921 unsigned neededInt, neededSSE;
922 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
923
924 // AMD64-ABI 3.2.3p3: If there are no registers available for any
925 // eightbyte of an argument, the whole argument is passed on the
926 // stack. If registers have already been assigned for some
927 // eightbytes of such an argument, the assignments get reverted.
928 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
929 freeIntRegs -= neededInt;
930 freeSSERegs -= neededSSE;
931 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000932 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +0000933 }
934 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000935}
936
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000937static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
938 QualType Ty,
939 CodeGenFunction &CGF) {
940 llvm::Value *overflow_arg_area_p =
941 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
942 llvm::Value *overflow_arg_area =
943 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
944
945 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
946 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000947 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000948 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000949 // Note that we follow the ABI & gcc here, even though the type
950 // could in theory have an alignment greater than 16. This case
951 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000952
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000953 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
954 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
955 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
956 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
957 llvm::Type::Int64Ty);
958 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
959 overflow_arg_area =
960 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
961 overflow_arg_area->getType(),
962 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000963 }
964
965 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
966 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
967 llvm::Value *Res =
968 CGF.Builder.CreateBitCast(overflow_arg_area,
969 llvm::PointerType::getUnqual(LTy));
970
971 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
972 // l->overflow_arg_area + sizeof(type).
973 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
974 // an 8 byte boundary.
975
976 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
977 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
978 (SizeInBytes + 7) & ~7);
979 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
980 "overflow_arg_area.next");
981 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
982
983 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
984 return Res;
985}
986
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000987llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
988 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000989 // Assume that va_list type is correct; should be pointer to LLVM type:
990 // struct {
991 // i32 gp_offset;
992 // i32 fp_offset;
993 // i8* overflow_arg_area;
994 // i8* reg_save_area;
995 // };
996 unsigned neededInt, neededSSE;
997 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
998 neededInt, neededSSE);
999
1000 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1001 // in the registers. If not go to step 7.
1002 if (!neededInt && !neededSSE)
1003 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1004
1005 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1006 // general purpose registers needed to pass type and num_fp to hold
1007 // the number of floating point registers needed.
1008
1009 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1010 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1011 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1012 //
1013 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1014 // register save space).
1015
1016 llvm::Value *InRegs = 0;
1017 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1018 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1019 if (neededInt) {
1020 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1021 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1022 InRegs =
1023 CGF.Builder.CreateICmpULE(gp_offset,
1024 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1025 48 - neededInt * 8),
1026 "fits_in_gp");
1027 }
1028
1029 if (neededSSE) {
1030 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1031 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1032 llvm::Value *FitsInFP =
1033 CGF.Builder.CreateICmpULE(fp_offset,
1034 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001035 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001036 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001037 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001038 }
1039
1040 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1041 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1042 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1043 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1044
1045 // Emit code to load the value if it was passed in registers.
1046
1047 CGF.EmitBlock(InRegBlock);
1048
1049 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1050 // an offset of l->gp_offset and/or l->fp_offset. This may require
1051 // copying to a temporary location in case the parameter is passed
1052 // in different register classes or requires an alignment greater
1053 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001054 //
1055 // FIXME: This really results in shameful code when we end up
1056 // needing to collect arguments from different places; often what
1057 // should result in a simple assembling of a structure from
1058 // scattered addresses has many more loads than necessary. Can we
1059 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001060 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1061 llvm::Value *RegAddr =
1062 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1063 "reg_save_area");
1064 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001065 // FIXME: Cleanup.
1066 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1067 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1068 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1069 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1070 const llvm::Type *TyLo = ST->getElementType(0);
1071 const llvm::Type *TyHi = ST->getElementType(1);
1072 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1073 "Unexpected ABI info for mixed regs");
1074 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1075 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1076 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1077 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1078 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1079 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1080 llvm::Value *V =
1081 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1082 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1083 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1084 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1085
1086 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001087 } else if (neededInt) {
1088 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1089 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1090 llvm::PointerType::getUnqual(LTy));
1091 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001092 if (neededSSE == 1) {
1093 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1094 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1095 llvm::PointerType::getUnqual(LTy));
1096 } else {
1097 assert(neededSSE == 2 && "Invalid number of needed registers!");
1098 // SSE registers are spaced 16 bytes apart in the register save
1099 // area, we need to collect the two eightbytes together.
1100 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1101 llvm::Value *RegAddrHi =
1102 CGF.Builder.CreateGEP(RegAddrLo,
1103 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1104 const llvm::Type *DblPtrTy =
1105 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1106 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1107 llvm::Type::DoubleTy,
1108 NULL);
1109 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1110 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1111 DblPtrTy));
1112 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1113 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1114 DblPtrTy));
1115 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1116 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1117 llvm::PointerType::getUnqual(LTy));
1118 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001119 }
1120
1121 // AMD64-ABI 3.5.7p5: Step 5. Set:
1122 // l->gp_offset = l->gp_offset + num_gp * 8
1123 // l->fp_offset = l->fp_offset + num_fp * 16.
1124 if (neededInt) {
1125 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1126 neededInt * 8);
1127 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1128 gp_offset_p);
1129 }
1130 if (neededSSE) {
1131 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1132 neededSSE * 16);
1133 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1134 fp_offset_p);
1135 }
1136 CGF.EmitBranch(ContBlock);
1137
1138 // Emit code to load the value if it was passed in memory.
1139
1140 CGF.EmitBlock(InMemBlock);
1141 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1142
1143 // Return the appropriate result.
1144
1145 CGF.EmitBlock(ContBlock);
1146 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1147 "vaarg.addr");
1148 ResAddr->reserveOperandSpace(2);
1149 ResAddr->addIncoming(RegAddr, InRegBlock);
1150 ResAddr->addIncoming(MemAddr, InMemBlock);
1151
1152 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001153}
1154
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001155ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001156 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001157 if (RetTy->isVoidType()) {
1158 return ABIArgInfo::getIgnore();
1159 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001160 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001161 } else {
1162 return ABIArgInfo::getDirect();
1163 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001164}
1165
1166ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001167 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001168 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001169 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001170 } else {
1171 return ABIArgInfo::getDirect();
1172 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001173}
1174
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001175llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1176 CodeGenFunction &CGF) const {
1177 return 0;
1178}
1179
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001180const ABIInfo &CodeGenTypes::getABIInfo() const {
1181 if (TheABIInfo)
1182 return *TheABIInfo;
1183
1184 // For now we just cache this in the CodeGenTypes and don't bother
1185 // to free it.
1186 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1187 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001188 switch (getContext().Target.getPointerWidth(0)) {
1189 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001190 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001191 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001192 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001193 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001194 }
1195
1196 return *(TheABIInfo = new DefaultABIInfo);
1197}
1198
Daniel Dunbare126ab12008-09-10 02:41:04 +00001199/***/
1200
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001201CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1202 const llvm::SmallVector<QualType, 16> &ArgTys) {
1203 NumArgs = ArgTys.size();
1204 Args = new ArgInfo[1 + NumArgs];
1205 Args[0].type = ResTy;
1206 for (unsigned i = 0; i < NumArgs; ++i)
1207 Args[1 + i].type = ArgTys[i];
1208}
1209
1210/***/
1211
Daniel Dunbar04d35782008-09-17 00:51:38 +00001212void CodeGenTypes::GetExpandedTypes(QualType Ty,
1213 std::vector<const llvm::Type*> &ArgTys) {
1214 const RecordType *RT = Ty->getAsStructureType();
1215 assert(RT && "Can only expand structure types.");
1216 const RecordDecl *RD = RT->getDecl();
1217 assert(!RD->hasFlexibleArrayMember() &&
1218 "Cannot expand structure with flexible array.");
1219
Douglas Gregor5d764842009-01-09 17:18:27 +00001220 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001221 e = RD->field_end(); i != e; ++i) {
1222 const FieldDecl *FD = *i;
1223 assert(!FD->isBitField() &&
1224 "Cannot expand structure with bit-field members.");
1225
1226 QualType FT = FD->getType();
1227 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1228 GetExpandedTypes(FT, ArgTys);
1229 } else {
1230 ArgTys.push_back(ConvertType(FT));
1231 }
1232 }
1233}
1234
1235llvm::Function::arg_iterator
1236CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1237 llvm::Function::arg_iterator AI) {
1238 const RecordType *RT = Ty->getAsStructureType();
1239 assert(RT && "Can only expand structure types.");
1240
1241 RecordDecl *RD = RT->getDecl();
1242 assert(LV.isSimple() &&
1243 "Unexpected non-simple lvalue during struct expansion.");
1244 llvm::Value *Addr = LV.getAddress();
1245 for (RecordDecl::field_iterator i = RD->field_begin(),
1246 e = RD->field_end(); i != e; ++i) {
1247 FieldDecl *FD = *i;
1248 QualType FT = FD->getType();
1249
1250 // FIXME: What are the right qualifiers here?
1251 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1252 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1253 AI = ExpandTypeFromArgs(FT, LV, AI);
1254 } else {
1255 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1256 ++AI;
1257 }
1258 }
1259
1260 return AI;
1261}
1262
1263void
1264CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1265 llvm::SmallVector<llvm::Value*, 16> &Args) {
1266 const RecordType *RT = Ty->getAsStructureType();
1267 assert(RT && "Can only expand structure types.");
1268
1269 RecordDecl *RD = RT->getDecl();
1270 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1271 llvm::Value *Addr = RV.getAggregateAddr();
1272 for (RecordDecl::field_iterator i = RD->field_begin(),
1273 e = RD->field_end(); i != e; ++i) {
1274 FieldDecl *FD = *i;
1275 QualType FT = FD->getType();
1276
1277 // FIXME: What are the right qualifiers here?
1278 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1279 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1280 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1281 } else {
1282 RValue RV = EmitLoadOfLValue(LV, FT);
1283 assert(RV.isScalar() &&
1284 "Unexpected non-scalar rvalue during struct expansion.");
1285 Args.push_back(RV.getScalarVal());
1286 }
1287 }
1288}
1289
Daniel Dunbar84379912009-02-02 19:06:38 +00001290/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1291/// a pointer to an object of type \arg Ty.
1292///
1293/// This safely handles the case when the src type is smaller than the
1294/// destination type; in this situation the values of bits which not
1295/// present in the src are undefined.
1296static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1297 const llvm::Type *Ty,
1298 CodeGenFunction &CGF) {
1299 const llvm::Type *SrcTy =
1300 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1301 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1302 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1303
Daniel Dunbar77071992009-02-03 05:59:18 +00001304 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001305 if (SrcSize == DstSize) {
1306 llvm::Value *Casted =
1307 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001308 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1309 // FIXME: Use better alignment / avoid requiring aligned load.
1310 Load->setAlignment(1);
1311 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001312 } else {
1313 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1314
1315 // Otherwise do coercion through memory. This is stupid, but
1316 // simple.
1317 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1318 llvm::Value *Casted =
1319 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001320 llvm::StoreInst *Store =
1321 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1322 // FIXME: Use better alignment / avoid requiring aligned store.
1323 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001324 return CGF.Builder.CreateLoad(Tmp);
1325 }
1326}
1327
1328/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1329/// where the source and destination may have different types.
1330///
1331/// This safely handles the case when the src type is larger than the
1332/// destination type; the upper bits of the src will be lost.
1333static void CreateCoercedStore(llvm::Value *Src,
1334 llvm::Value *DstPtr,
1335 CodeGenFunction &CGF) {
1336 const llvm::Type *SrcTy = Src->getType();
1337 const llvm::Type *DstTy =
1338 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1339
1340 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1341 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1342
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001343 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001344 if (SrcSize == DstSize) {
1345 llvm::Value *Casted =
1346 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001347 // FIXME: Use better alignment / avoid requiring aligned store.
1348 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001349 } else {
1350 assert(SrcSize > DstSize && "Coercion is missing bits!");
1351
1352 // Otherwise do coercion through memory. This is stupid, but
1353 // simple.
1354 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1355 CGF.Builder.CreateStore(Src, Tmp);
1356 llvm::Value *Casted =
1357 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001358 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1359 // FIXME: Use better alignment / avoid requiring aligned load.
1360 Load->setAlignment(1);
1361 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001362 }
1363}
1364
Daniel Dunbar04d35782008-09-17 00:51:38 +00001365/***/
1366
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001367bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001368 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001369}
1370
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001371const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001372CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001373 std::vector<const llvm::Type*> ArgTys;
1374
1375 const llvm::Type *ResultType = 0;
1376
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001377 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001378 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001379 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001380 case ABIArgInfo::Expand:
1381 assert(0 && "Invalid ABI kind for return argument");
1382
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001383 case ABIArgInfo::Direct:
1384 ResultType = ConvertType(RetTy);
1385 break;
1386
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001387 case ABIArgInfo::Indirect: {
1388 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001389 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001390 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001391 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1392 break;
1393 }
1394
Daniel Dunbar1358b202009-01-26 21:26:08 +00001395 case ABIArgInfo::Ignore:
1396 ResultType = llvm::Type::VoidTy;
1397 break;
1398
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001399 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001400 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001401 break;
1402 }
1403
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001404 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1405 ie = FI.arg_end(); it != ie; ++it) {
1406 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001407
1408 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001409 case ABIArgInfo::Ignore:
1410 break;
1411
Daniel Dunbar04d35782008-09-17 00:51:38 +00001412 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001413 ArgTys.push_back(AI.getCoerceToType());
1414 break;
1415
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001416 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001417 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001418 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1419 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001420 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001421 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001422
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001423 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001424 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001425 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001426
1427 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001428 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001429 break;
1430 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001431 }
1432
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001433 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001434}
1435
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001436void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001437 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001438 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001439 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001440 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001441
1442 if (TargetDecl) {
1443 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001444 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001445 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001446 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001447 if (TargetDecl->getAttr<PureAttr>())
1448 FuncAttrs |= llvm::Attribute::ReadOnly;
1449 if (TargetDecl->getAttr<ConstAttr>())
1450 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001451 }
1452
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001453 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001454 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001455 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001456 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001457 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001458 if (RetTy->isPromotableIntegerType()) {
1459 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001460 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001461 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001462 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001463 }
1464 }
1465 break;
1466
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001467 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001468 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001469 llvm::Attribute::StructRet |
1470 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001471 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001472 break;
1473
Daniel Dunbar1358b202009-01-26 21:26:08 +00001474 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001475 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001476 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001477
Daniel Dunbar22e30052008-09-11 01:48:57 +00001478 case ABIArgInfo::Expand:
1479 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001480 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001481
Devang Patel2bb6eb82008-09-26 22:53:57 +00001482 if (RetAttrs)
1483 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001484 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1485 ie = FI.arg_end(); it != ie; ++it) {
1486 QualType ParamType = it->type;
1487 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001488 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001489
1490 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001491 case ABIArgInfo::Coerce:
1492 break;
1493
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001494 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001495 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarb3f651a2009-02-05 01:31:19 +00001496 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001497 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001498 break;
1499
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001500 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001501 if (ParamType->isPromotableIntegerType()) {
1502 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001503 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001504 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001505 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001506 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001507 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001508 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001509
Daniel Dunbar1358b202009-01-26 21:26:08 +00001510 case ABIArgInfo::Ignore:
1511 // Skip increment, no matching LLVM parameter.
1512 continue;
1513
Daniel Dunbar04d35782008-09-17 00:51:38 +00001514 case ABIArgInfo::Expand: {
1515 std::vector<const llvm::Type*> Tys;
1516 // FIXME: This is rather inefficient. Do we ever actually need
1517 // to do anything here? The result should be just reconstructed
1518 // on the other side, so extension should be a non-issue.
1519 getTypes().GetExpandedTypes(ParamType, Tys);
1520 Index += Tys.size();
1521 continue;
1522 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001523 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001524
Devang Patela85a9ef2008-09-25 21:02:23 +00001525 if (Attributes)
1526 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001527 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001528 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001529 if (FuncAttrs)
1530 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001531}
1532
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001533void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1534 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001535 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001536 // FIXME: We no longer need the types from FunctionArgList; lift up
1537 // and simplify.
1538
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001539 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1540 llvm::Function::arg_iterator AI = Fn->arg_begin();
1541
1542 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001543 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001544 AI->setName("agg.result");
1545 ++AI;
1546 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001547
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001548 assert(FI.arg_size() == Args.size() &&
1549 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001550 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001551 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001552 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001553 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001554 QualType Ty = info_it->type;
1555 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001556
1557 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001558 case ABIArgInfo::Indirect: {
1559 llvm::Value* V = AI;
1560 if (hasAggregateLLVMType(Ty)) {
1561 // Do nothing, aggregates and complex variables are accessed by
1562 // reference.
1563 } else {
1564 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001565 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001566 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1567 // This must be a promotion, for something like
1568 // "void a(x) short x; {..."
1569 V = EmitScalarConversion(V, Ty, Arg->getType());
1570 }
1571 }
1572 EmitParmDecl(*Arg, V);
1573 break;
1574 }
1575
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001576 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001577 assert(AI != Fn->arg_end() && "Argument mismatch!");
1578 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001579 if (hasAggregateLLVMType(Ty)) {
1580 // Create a temporary alloca to hold the argument; the rest of
1581 // codegen expects to access aggregates & complex values by
1582 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001583 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001584 Builder.CreateStore(AI, V);
1585 } else {
1586 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1587 // This must be a promotion, for something like
1588 // "void a(x) short x; {..."
1589 V = EmitScalarConversion(V, Ty, Arg->getType());
1590 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001591 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001592 EmitParmDecl(*Arg, V);
1593 break;
1594 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001595
1596 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001597 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001598 // we need to create a temporary and reconstruct it from the
1599 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001600 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001601 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001602 (Name + ".addr").c_str());
1603 // FIXME: What are the right qualifiers here?
1604 llvm::Function::arg_iterator End =
1605 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1606 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001607
Daniel Dunbar04d35782008-09-17 00:51:38 +00001608 // Name the arguments used in expansion and increment AI.
1609 unsigned Index = 0;
1610 for (; AI != End; ++AI, ++Index)
1611 AI->setName(Name + "." + llvm::utostr(Index));
1612 continue;
1613 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001614
1615 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001616 // Initialize the local variable appropriately.
1617 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001618 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001619 } else {
1620 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1621 }
1622
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001623 // Skip increment, no matching LLVM parameter.
1624 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001625
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001626 case ABIArgInfo::Coerce: {
1627 assert(AI != Fn->arg_end() && "Argument mismatch!");
1628 // FIXME: This is very wasteful; EmitParmDecl is just going to
1629 // drop the result in a new alloca anyway, so we could just
1630 // store into that directly if we broke the abstraction down
1631 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001632 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001633 CreateCoercedStore(AI, V, *this);
1634 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001635 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001636 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001637 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1638 // This must be a promotion, for something like
1639 // "void a(x) short x; {..."
1640 V = EmitScalarConversion(V, Ty, Arg->getType());
1641 }
1642 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001643 EmitParmDecl(*Arg, V);
1644 break;
1645 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001646 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001647
1648 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001649 }
1650 assert(AI == Fn->arg_end() && "Argument mismatch!");
1651}
1652
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001653void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001654 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001655 llvm::Value *RV = 0;
1656
1657 // Functions with no result always return void.
1658 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001659 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001660 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001661
1662 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001663 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001664 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001665 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1666 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1667 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1668 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1669 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001670 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1671 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001672 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001673 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001674
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001675 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001676 // The internal return value temp always will have
1677 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001678 RV = Builder.CreateLoad(ReturnValue);
1679 break;
1680
Daniel Dunbar1358b202009-01-26 21:26:08 +00001681 case ABIArgInfo::Ignore:
1682 break;
1683
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001684 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001685 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001686 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001687
Daniel Dunbar22e30052008-09-11 01:48:57 +00001688 case ABIArgInfo::Expand:
1689 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001690 }
1691 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001692
1693 if (RV) {
1694 Builder.CreateRet(RV);
1695 } else {
1696 Builder.CreateRetVoid();
1697 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001698}
1699
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001700RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1701 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001702 const CallArgList &CallArgs,
1703 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001704 // FIXME: We no longer need the types from CallArgs; lift up and
1705 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001706 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001707
1708 // Handle struct-return functions by passing a pointer to the
1709 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001710 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001711 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001712 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001713 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001714 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001715 }
1716
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001717 assert(CallInfo.arg_size() == CallArgs.size() &&
1718 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001719 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001720 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001721 I != E; ++I, ++info_it) {
1722 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001723 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001724
1725 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001726 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001727 if (RV.isScalar() || RV.isComplex()) {
1728 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001729 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001730 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001731 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001732 else
1733 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1734 } else {
1735 Args.push_back(RV.getAggregateAddr());
1736 }
1737 break;
1738
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001739 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001740 if (RV.isScalar()) {
1741 Args.push_back(RV.getScalarVal());
1742 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001743 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1744 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1745 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1746 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00001747 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001748 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001749 }
1750 break;
1751
Daniel Dunbar1358b202009-01-26 21:26:08 +00001752 case ABIArgInfo::Ignore:
1753 break;
1754
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001755 case ABIArgInfo::Coerce: {
1756 // FIXME: Avoid the conversion through memory if possible.
1757 llvm::Value *SrcPtr;
1758 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001759 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001760 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001761 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001762 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001763 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1764 } else
1765 SrcPtr = RV.getAggregateAddr();
1766 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1767 *this));
1768 break;
1769 }
1770
Daniel Dunbar04d35782008-09-17 00:51:38 +00001771 case ABIArgInfo::Expand:
1772 ExpandTypeToArgs(I->second, RV, Args);
1773 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001774 }
1775 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001776
Daniel Dunbar0a067402009-02-23 17:26:39 +00001777 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00001778 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001779 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00001780 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1781 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001782
Daniel Dunbar90e43452009-03-02 04:32:35 +00001783 llvm::CallSite CS;
1784 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
1785 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00001786 } else {
1787 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00001788 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1789 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00001790 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00001791 }
1792
Daniel Dunbar90e43452009-03-02 04:32:35 +00001793 CS.setAttributes(Attrs);
1794 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1795 CS.setCallingConv(F->getCallingConv());
1796
1797 // If the call doesn't return, finish the basic block and clear the
1798 // insertion point; this allows the rest of IRgen to discard
1799 // unreachable code.
1800 if (CS.doesNotReturn()) {
1801 Builder.CreateUnreachable();
1802 Builder.ClearInsertionPoint();
1803
1804 // FIXME: For now, emit a dummy basic block because expr
1805 // emitters in generally are not ready to handle emitting
1806 // expressions at unreachable points.
1807 EnsureInsertPoint();
1808
1809 // Return a reasonable RValue.
1810 return GetUndefRValue(RetTy);
1811 }
1812
1813 llvm::Instruction *CI = CS.getInstruction();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001814 if (CI->getType() != llvm::Type::VoidTy)
1815 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001816
1817 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001818 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001819 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001820 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001821 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001822 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001823 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001824 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001825
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001826 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001827 if (RetTy->isAnyComplexType()) {
1828 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1829 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1830 return RValue::getComplex(std::make_pair(Real, Imag));
1831 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001832 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00001833 Builder.CreateStore(CI, V);
1834 return RValue::getAggregate(V);
1835 } else
1836 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001837
Daniel Dunbar1358b202009-01-26 21:26:08 +00001838 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00001839 // If we are ignoring an argument that had a result, make sure to
1840 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00001841 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001842
Daniel Dunbar73d66602008-09-10 07:04:09 +00001843 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001844 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001845 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001846 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001847 if (RetTy->isAnyComplexType())
1848 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001849 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001850 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001851 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001852 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001853 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001854
Daniel Dunbar22e30052008-09-11 01:48:57 +00001855 case ABIArgInfo::Expand:
1856 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001857 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001858
1859 assert(0 && "Unhandled ABIArgInfo::Kind");
1860 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001861}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001862
1863/* VarArg handling */
1864
1865llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1866 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1867}