blob: 45b386f4261cd79f9170919e3d6461f32a6c482a [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 Dunbar6f3e7fa2009-01-24 08:32:22 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbarbe9eb092009-02-12 09:04:14 +000026#include "llvm/Support/MathExtras.h"
Daniel Dunbar6f7279b2009-02-04 23:24:38 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000029
30#include "ABIInfo.h"
31
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000032using namespace clang;
33using namespace CodeGen;
34
35/***/
36
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000037// FIXME: Use iterator and sidestep silly type array creation.
38
Daniel Dunbar541b63b2009-02-02 23:23:47 +000039const
40CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
41 return getFunctionInfo(FTNP->getResultType(),
42 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000043}
44
Daniel Dunbar541b63b2009-02-02 23:23:47 +000045const
46CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
47 llvm::SmallVector<QualType, 16> ArgTys;
48 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000049 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000050 ArgTys.push_back(FTP->getArgType(i));
51 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000052}
53
Daniel Dunbar541b63b2009-02-02 23:23:47 +000054const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000055 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar541b63b2009-02-02 23:23:47 +000056 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
57 return getFunctionInfo(FTP);
58 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000059}
60
Daniel Dunbar541b63b2009-02-02 23:23:47 +000061const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
62 llvm::SmallVector<QualType, 16> ArgTys;
63 ArgTys.push_back(MD->getSelfDecl()->getType());
64 ArgTys.push_back(Context.getObjCSelType());
65 // FIXME: Kill copy?
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000066 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
67 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000068 ArgTys.push_back((*i)->getType());
69 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000070}
71
Daniel Dunbar541b63b2009-02-02 23:23:47 +000072const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
73 const CallArgList &Args) {
74 // FIXME: Kill copy.
75 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000076 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
77 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000078 ArgTys.push_back(i->second);
79 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000080}
81
Daniel Dunbar541b63b2009-02-02 23:23:47 +000082const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
83 const FunctionArgList &Args) {
84 // FIXME: Kill copy.
85 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +000086 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
87 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000088 ArgTys.push_back(i->second);
89 return getFunctionInfo(ResTy, ArgTys);
90}
91
92const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
93 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +000094 // Lookup or create unique function info.
95 llvm::FoldingSetNodeID ID;
96 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
97
98 void *InsertPos = 0;
99 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
100 if (FI)
101 return *FI;
102
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000103 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000104 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000105 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000106
107 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000108 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000109
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000110 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000111}
112
113/***/
114
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000115ABIInfo::~ABIInfo() {}
116
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000117void ABIArgInfo::dump() const {
118 fprintf(stderr, "(ABIArgInfo Kind=");
119 switch (TheKind) {
120 case Direct:
121 fprintf(stderr, "Direct");
122 break;
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000123 case Ignore:
124 fprintf(stderr, "Ignore");
125 break;
126 case Coerce:
127 fprintf(stderr, "Coerce Type=");
128 getCoerceToType()->print(llvm::errs());
129 // FIXME: This is ridiculous.
130 llvm::errs().flush();
131 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000132 case Indirect:
133 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000134 break;
135 case Expand:
136 fprintf(stderr, "Expand");
137 break;
138 }
139 fprintf(stderr, ")\n");
140}
141
142/***/
143
Daniel Dunbar834af452008-09-17 21:22:33 +0000144/// isEmptyStruct - Return true iff a structure has no non-empty
145/// members. Note that a structure with a flexible array member is not
146/// considered empty.
147static bool isEmptyStruct(QualType T) {
148 const RecordType *RT = T->getAsStructureType();
149 if (!RT)
150 return 0;
151 const RecordDecl *RD = RT->getDecl();
152 if (RD->hasFlexibleArrayMember())
153 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000154 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000155 e = RD->field_end(); i != e; ++i) {
156 const FieldDecl *FD = *i;
157 if (!isEmptyStruct(FD->getType()))
158 return false;
159 }
160 return true;
161}
162
163/// isSingleElementStruct - Determine if a structure is a "single
164/// element struct", i.e. it has exactly one non-empty field or
165/// exactly one field which is itself a single element
166/// struct. Structures with flexible array members are never
167/// considered single element structs.
168///
169/// \return The field declaration for the single non-empty field, if
170/// it exists.
171static const FieldDecl *isSingleElementStruct(QualType T) {
172 const RecordType *RT = T->getAsStructureType();
173 if (!RT)
174 return 0;
175
176 const RecordDecl *RD = RT->getDecl();
177 if (RD->hasFlexibleArrayMember())
178 return 0;
179
180 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000181 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000182 e = RD->field_end(); i != e; ++i) {
183 const FieldDecl *FD = *i;
184 QualType FT = FD->getType();
185
186 if (isEmptyStruct(FT)) {
187 // Ignore
188 } else if (Found) {
189 return 0;
190 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
191 Found = FD;
192 } else {
193 Found = isSingleElementStruct(FT);
194 if (!Found)
195 return 0;
196 }
197 }
198
199 return Found;
200}
201
202static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
203 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
204 return false;
205
206 uint64_t Size = Context.getTypeSize(Ty);
207 return Size == 32 || Size == 64;
208}
209
210static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
211 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000212 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000213 e = RD->field_end(); i != e; ++i) {
214 const FieldDecl *FD = *i;
215
216 if (!is32Or64BitBasicType(FD->getType(), Context))
217 return false;
218
219 // If this is a bit-field we need to make sure it is still a
220 // 32-bit or 64-bit type.
221 if (Expr *BW = FD->getBitWidth()) {
222 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
223 if (Width <= 16)
224 return false;
225 }
226 }
227 return true;
228}
229
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000230namespace {
231/// DefaultABIInfo - The default implementation for ABI specific
232/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000233/// self-consistent and sensible LLVM IR generation, but does not
234/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000235class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000236 ABIArgInfo classifyReturnType(QualType RetTy,
237 ASTContext &Context) const;
238
239 ABIArgInfo classifyArgumentType(QualType RetTy,
240 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000241
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000242 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
243 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
244 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
245 it != ie; ++it)
246 it->info = classifyArgumentType(it->type, Context);
247 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000248
249 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
250 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000251};
252
253/// X86_32ABIInfo - The X86-32 ABI information.
254class X86_32ABIInfo : public ABIInfo {
255public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000256 ABIArgInfo classifyReturnType(QualType RetTy,
257 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000258
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000259 ABIArgInfo classifyArgumentType(QualType RetTy,
260 ASTContext &Context) const;
261
262 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
263 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
264 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
265 it != ie; ++it)
266 it->info = classifyArgumentType(it->type, Context);
267 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000268
269 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
270 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000271};
272}
273
274ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
275 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000276 if (RetTy->isVoidType()) {
277 return ABIArgInfo::getIgnore();
278 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000279 // Classify "single element" structs as their element type.
280 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
281 if (SeltFD) {
282 QualType SeltTy = SeltFD->getType()->getDesugaredType();
283 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
284 // FIXME: This is gross, it would be nice if we could just
285 // pass back SeltTy and have clients deal with it. Is it worth
286 // supporting coerce to both LLVM and clang Types?
287 if (BT->isIntegerType()) {
288 uint64_t Size = Context.getTypeSize(SeltTy);
289 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
290 } else if (BT->getKind() == BuiltinType::Float) {
291 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
292 } else if (BT->getKind() == BuiltinType::Double) {
293 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
294 }
295 } else if (SeltTy->isPointerType()) {
296 // FIXME: It would be really nice if this could come out as
297 // the proper pointer type.
298 llvm::Type *PtrTy =
299 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
300 return ABIArgInfo::getCoerce(PtrTy);
301 }
302 }
303
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000304 uint64_t Size = Context.getTypeSize(RetTy);
305 if (Size == 8) {
306 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
307 } else if (Size == 16) {
308 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
309 } else if (Size == 32) {
310 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
311 } else if (Size == 64) {
312 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
313 } else {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000314 return ABIArgInfo::getIndirect(0);
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000315 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000316 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000317 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000318 }
319}
320
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000321ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000322 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000323 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000324 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000325 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000326 if (const RecordType *RT = Ty->getAsStructureType())
327 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000328 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000329
Daniel Dunbar3170c932009-02-05 01:50:07 +0000330 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000331 uint64_t Size = Context.getTypeSize(Ty);
332 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000333 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000334
335 // Expand structs with size <= 128-bits which consist only of
336 // basic types (int, long long, float, double, xxx*). This is
337 // non-recursive and does not ignore empty fields.
338 if (const RecordType *RT = Ty->getAsStructureType()) {
339 if (Context.getTypeSize(Ty) <= 4*32 &&
340 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
341 return ABIArgInfo::getExpand();
342 }
343
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000344 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000345 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000346 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000347 }
348}
349
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000350llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
351 CodeGenFunction &CGF) const {
352 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
353 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
354
355 CGBuilderTy &Builder = CGF.Builder;
356 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
357 "ap");
358 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
359 llvm::Type *PTy =
360 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
361 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
362
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000363 uint64_t Offset =
364 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000365 llvm::Value *NextAddr =
366 Builder.CreateGEP(Addr,
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000367 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000368 "ap.next");
369 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
370
371 return AddrTyped;
372}
373
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000374namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000375/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000376class X86_64ABIInfo : public ABIInfo {
377 enum Class {
378 Integer = 0,
379 SSE,
380 SSEUp,
381 X87,
382 X87Up,
383 ComplexX87,
384 NoClass,
385 Memory
386 };
387
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000388 /// merge - Implement the X86_64 ABI merging algorithm.
389 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000390 /// Merge an accumulating classification \arg Accum with a field
391 /// classification \arg Field.
392 ///
393 /// \param Accum - The accumulating classification. This should
394 /// always be either NoClass or the result of a previous merge
395 /// call. In addition, this should never be Memory (the caller
396 /// should just return Memory for the aggregate).
397 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000398
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000399 /// classify - Determine the x86_64 register classes in which the
400 /// given type T should be passed.
401 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000402 /// \param Lo - The classification for the parts of the type
403 /// residing in the low word of the containing object.
404 ///
405 /// \param Hi - The classification for the parts of the type
406 /// residing in the high word of the containing object.
407 ///
408 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000409 /// containing object. Some parameters are classified different
410 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000411 ///
412 /// If a word is unused its result will be NoClass; if a type should
413 /// be passed in Memory then at least the classification of \arg Lo
414 /// will be Memory.
415 ///
416 /// The \arg Lo class will be NoClass iff the argument is ignored.
417 ///
418 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000419 /// also be ComplexX87.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000420 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000421 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000422
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000423 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
424 /// to coerce to, chose the best way to pass Ty in the same place
425 /// that \arg CoerceTo would be passed, but while keeping the
426 /// emitted code as simple as possible.
427 ///
428 /// FIXME: Note, this should be cleaned up to just take an
429 /// enumeration of all the ways we might want to pass things,
430 /// instead of constructing an LLVM type. This makes this code more
431 /// explicit, and it makes it clearer that we are also doing this
432 /// for correctness in the case of passing scalar types.
433 ABIArgInfo getCoerceResult(QualType Ty,
434 const llvm::Type *CoerceTo,
435 ASTContext &Context) const;
436
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000437 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000438 ASTContext &Context) const;
439
440 ABIArgInfo classifyArgumentType(QualType Ty,
441 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000442 unsigned &neededInt,
443 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000444
445public:
446 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000447
448 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
449 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000450};
451}
452
Daniel Dunbarc4503572009-01-31 00:06:58 +0000453X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
454 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000455 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
456 // classified recursively so that always two fields are
457 // considered. The resulting class is calculated according to
458 // the classes of the fields in the eightbyte:
459 //
460 // (a) If both classes are equal, this is the resulting class.
461 //
462 // (b) If one of the classes is NO_CLASS, the resulting class is
463 // the other class.
464 //
465 // (c) If one of the classes is MEMORY, the result is the MEMORY
466 // class.
467 //
468 // (d) If one of the classes is INTEGER, the result is the
469 // INTEGER.
470 //
471 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
472 // MEMORY is used as class.
473 //
474 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000475 assert((Accum == NoClass || Accum == Integer ||
476 Accum == SSE || Accum == SSEUp) &&
477 "Invalid accumulated classification during merge.");
478 if (Accum == Field || Field == NoClass)
479 return Accum;
480 else if (Field == Memory)
481 return Memory;
482 else if (Accum == NoClass)
483 return Field;
484 else if (Accum == Integer || Field == Integer)
485 return Integer;
486 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
487 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000488 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000489 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000490}
491
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000492void X86_64ABIInfo::classify(QualType Ty,
493 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000494 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000495 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000496 // FIXME: This code can be simplified by introducing a simple value
497 // class for Class pairs with appropriate constructor methods for
498 // the various situations.
499
Daniel Dunbarc4503572009-01-31 00:06:58 +0000500 Lo = Hi = NoClass;
501
502 Class &Current = OffsetBase < 64 ? Lo : Hi;
503 Current = Memory;
504
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000505 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
506 BuiltinType::Kind k = BT->getKind();
507
Daniel Dunbar11434922009-01-26 21:26:08 +0000508 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000509 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000510 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000511 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000512 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000513 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000514 } else if (k == BuiltinType::LongDouble) {
515 Lo = X87;
516 Hi = X87Up;
517 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000518 // FIXME: _Decimal32 and _Decimal64 are SSE.
519 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000520 // FIXME: __int128 is (Integer, Integer).
521 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
522 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000523 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000524 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000525 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000526 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000527 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000528 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000529 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000530
Daniel Dunbarc4503572009-01-31 00:06:58 +0000531 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000532
533 // If this type crosses an eightbyte boundary, it should be
534 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000535 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000536 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000537 } else if (Size == 128) {
538 Lo = SSE;
539 Hi = SSEUp;
540 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000541 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000542 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000543
Daniel Dunbare33edf12009-01-30 18:40:10 +0000544 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000545 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000546 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000547 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000548 else if (Size <= 128)
549 Lo = Hi = Integer;
550 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000551 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000552 else if (ET == Context.DoubleTy)
553 Lo = Hi = SSE;
554 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000555 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000556
557 // If this complex type crosses an eightbyte boundary then it
558 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000559 uint64_t EB_Real = (OffsetBase) / 64;
560 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000561 if (Hi == NoClass && EB_Real != EB_Imag)
562 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000563 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
564 // Arrays are treated like structures.
565
566 uint64_t Size = Context.getTypeSize(Ty);
567
568 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
569 // than two eightbytes, ..., it has class MEMORY.
570 if (Size > 128)
571 return;
572
573 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
574 // fields, it has class MEMORY.
575 //
576 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000577 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000578 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000579
580 // Otherwise implement simplified merge. We could be smarter about
581 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000582 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000583 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
584 uint64_t ArraySize = AT->getSize().getZExtValue();
585 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
586 Class FieldLo, FieldHi;
587 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000588 Lo = merge(Lo, FieldLo);
589 Hi = merge(Hi, FieldHi);
590 if (Lo == Memory || Hi == Memory)
591 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000592 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000593
594 // Do post merger cleanup (see below). Only case we worry about is Memory.
595 if (Hi == Memory)
596 Lo = Memory;
597 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000598 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000599 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000600
601 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
602 // than two eightbytes, ..., it has class MEMORY.
603 if (Size > 128)
604 return;
605
606 const RecordDecl *RD = RT->getDecl();
607
608 // Assume variable sized types are passed in memory.
609 if (RD->hasFlexibleArrayMember())
610 return;
611
612 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
613
614 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000615 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000616 unsigned idx = 0;
617 for (RecordDecl::field_iterator i = RD->field_begin(),
618 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000619 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbardd81d442009-02-17 02:45:44 +0000620 bool BitField = i->isBitField();
Daniel Dunbar99037e52009-01-29 08:13:58 +0000621
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000622 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
623 // fields, it has class MEMORY.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000624 //
625 // Note, skip this test for bitfields, see below.
626 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar99037e52009-01-29 08:13:58 +0000627 Lo = Memory;
628 return;
629 }
630
Daniel Dunbar99037e52009-01-29 08:13:58 +0000631 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000632 //
633 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
634 // exceeds a single eightbyte, each is classified
635 // separately. Each eightbyte gets initialized to class
636 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000637 Class FieldLo, FieldHi;
Daniel Dunbardd81d442009-02-17 02:45:44 +0000638
639 // Bitfields require special handling, they do not force the
640 // structure to be passed in memory even if unaligned, and
641 // therefore they can straddle an eightbyte.
642 if (BitField) {
643 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
644 uint64_t Size =
645 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
646
647 uint64_t EB_Lo = Offset / 64;
648 uint64_t EB_Hi = (Offset + Size - 1) / 64;
649 FieldLo = FieldHi = NoClass;
650 if (EB_Lo) {
651 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
652 FieldLo = NoClass;
653 FieldHi = Integer;
654 } else {
655 FieldLo = Integer;
656 FieldHi = EB_Hi ? Integer : NoClass;
657 }
658 } else
659 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000660 Lo = merge(Lo, FieldLo);
661 Hi = merge(Hi, FieldHi);
662 if (Lo == Memory || Hi == Memory)
663 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000664 }
665
666 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
667 //
668 // (a) If one of the classes is MEMORY, the whole argument is
669 // passed in memory.
670 //
671 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
672
673 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000674 // the merge (just bail).
675 //
676 // The second condition occurs in the case of unions; for example
677 // union { _Complex double; unsigned; }.
678 if (Hi == Memory)
679 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000680 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000681 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000682 }
683}
684
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000685ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
686 const llvm::Type *CoerceTo,
687 ASTContext &Context) const {
688 if (CoerceTo == llvm::Type::Int64Ty) {
689 // Integer and pointer types will end up in a general purpose
690 // register.
691 if (Ty->isIntegerType() || Ty->isPointerType())
692 return ABIArgInfo::getDirect();
693 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000694 // FIXME: It would probably be better to make CGFunctionInfo only
695 // map using canonical types than to canonize here.
696 QualType CTy = Context.getCanonicalType(Ty);
697
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000698 // Float and double end up in a single SSE reg.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000699 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000700 return ABIArgInfo::getDirect();
701 }
702
703 return ABIArgInfo::getCoerce(CoerceTo);
704}
Daniel Dunbarc4503572009-01-31 00:06:58 +0000705
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000706ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
707 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000708 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
709 // classification algorithm.
710 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000711 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000712
Daniel Dunbarc4503572009-01-31 00:06:58 +0000713 // Check some invariants.
714 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
715 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
716 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
717
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000718 const llvm::Type *ResType = 0;
719 switch (Lo) {
720 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000721 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000722
723 case SSEUp:
724 case X87Up:
725 assert(0 && "Invalid classification for lo word.");
726
Daniel Dunbarc4503572009-01-31 00:06:58 +0000727 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000728 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000729 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000730 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000731
732 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
733 // available register of the sequence %rax, %rdx is used.
734 case Integer:
735 ResType = llvm::Type::Int64Ty; break;
736
737 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
738 // available SSE register of the sequence %xmm0, %xmm1 is used.
739 case SSE:
740 ResType = llvm::Type::DoubleTy; break;
741
742 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
743 // returned on the X87 stack in %st0 as 80-bit x87 number.
744 case X87:
745 ResType = llvm::Type::X86_FP80Ty; break;
746
Daniel Dunbarc4503572009-01-31 00:06:58 +0000747 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
748 // part of the value is returned in %st0 and the imaginary part in
749 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000750 case ComplexX87:
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000751 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar3e030b42009-02-18 03:44:19 +0000752 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
753 llvm::Type::X86_FP80Ty,
754 NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000755 break;
756 }
757
758 switch (Hi) {
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000759 // Memory was handled previously and X87 should
760 // never occur as a hi class.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000761 case Memory:
762 case X87:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000763 assert(0 && "Invalid classification for hi word.");
764
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000765 case ComplexX87: // Previously handled.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000766 case NoClass: break;
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000767
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000768 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000769 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
770 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000771 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000772 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
773 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000774
775 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
776 // is passed in the upper half of the last used SSE register.
777 //
778 // SSEUP should always be preceeded by SSE, just widen.
779 case SSEUp:
780 assert(Lo == SSE && "Unexpected SSEUp classification.");
781 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
782 break;
783
784 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000785 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000786 //
787 // X87UP should always be preceeded by X87, so we don't need to do
788 // anything here.
789 case X87Up:
790 assert(Lo == X87 && "Unexpected X87Up classification.");
791 break;
792 }
793
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000794 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000795}
796
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000797ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000798 unsigned &neededInt,
799 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000800 X86_64ABIInfo::Class Lo, Hi;
801 classify(Ty, Context, 0, Lo, Hi);
802
803 // Check some invariants.
804 // FIXME: Enforce these by construction.
805 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
806 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
807 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
808
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000809 neededInt = 0;
810 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000811 const llvm::Type *ResType = 0;
812 switch (Lo) {
813 case NoClass:
814 return ABIArgInfo::getIgnore();
815
816 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
817 // on the stack.
818 case Memory:
819
820 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
821 // COMPLEX_X87, it is passed in memory.
822 case X87:
823 case ComplexX87:
824 // Choose appropriate in memory type.
825 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000826 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000827 else
828 return ABIArgInfo::getDirect();
829
830 case SSEUp:
831 case X87Up:
832 assert(0 && "Invalid classification for lo word.");
833
834 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
835 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
836 // and %r9 is used.
837 case Integer:
838 ++neededInt;
839 ResType = llvm::Type::Int64Ty;
840 break;
841
842 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
843 // available SSE register is used, the registers are taken in the
844 // order from %xmm0 to %xmm7.
845 case SSE:
846 ++neededSSE;
847 ResType = llvm::Type::DoubleTy;
848 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000849 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000850
851 switch (Hi) {
852 // Memory was handled previously, ComplexX87 and X87 should
853 // never occur as hi classes, and X87Up must be preceed by X87,
854 // which is passed in memory.
855 case Memory:
856 case X87:
857 case X87Up:
858 case ComplexX87:
859 assert(0 && "Invalid classification for hi word.");
860
861 case NoClass: break;
862 case Integer:
863 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
864 ++neededInt;
865 break;
866 case SSE:
867 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
868 ++neededSSE;
869 break;
870
871 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
872 // eightbyte is passed in the upper half of the last used SSE
873 // register.
874 case SSEUp:
875 assert(Lo == SSE && "Unexpected SSEUp classification.");
876 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
877 break;
878 }
879
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000880 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000881}
882
883void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
884 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
885
886 // Keep track of the number of assigned registers.
887 unsigned freeIntRegs = 6, freeSSERegs = 8;
888
889 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
890 // get assigned (in left-to-right order) for passing as follows...
891 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000892 it != ie; ++it) {
893 unsigned neededInt, neededSSE;
894 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
895
896 // AMD64-ABI 3.2.3p3: If there are no registers available for any
897 // eightbyte of an argument, the whole argument is passed on the
898 // stack. If registers have already been assigned for some
899 // eightbytes of such an argument, the assignments get reverted.
900 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
901 freeIntRegs -= neededInt;
902 freeSSERegs -= neededSSE;
903 } else {
904 // Choose appropriate in memory type.
905 if (CodeGenFunction::hasAggregateLLVMType(it->type))
906 it->info = ABIArgInfo::getIndirect(0);
907 else
908 it->info = ABIArgInfo::getDirect();
909 }
910 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000911}
912
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000913static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
914 QualType Ty,
915 CodeGenFunction &CGF) {
916 llvm::Value *overflow_arg_area_p =
917 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
918 llvm::Value *overflow_arg_area =
919 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
920
921 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
922 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbarc5bcee42009-02-16 23:38:56 +0000923 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000924 if (Align > 8) {
Daniel Dunbarc5bcee42009-02-16 23:38:56 +0000925 // Note that we follow the ABI & gcc here, even though the type
926 // could in theory have an alignment greater than 16. This case
927 // shouldn't ever matter in practice.
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000928
Daniel Dunbarc5bcee42009-02-16 23:38:56 +0000929 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
930 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
931 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
932 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
933 llvm::Type::Int64Ty);
934 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
935 overflow_arg_area =
936 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
937 overflow_arg_area->getType(),
938 "overflow_arg_area.align");
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000939 }
940
941 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
942 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
943 llvm::Value *Res =
944 CGF.Builder.CreateBitCast(overflow_arg_area,
945 llvm::PointerType::getUnqual(LTy));
946
947 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
948 // l->overflow_arg_area + sizeof(type).
949 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
950 // an 8 byte boundary.
951
952 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
953 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
954 (SizeInBytes + 7) & ~7);
955 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
956 "overflow_arg_area.next");
957 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
958
959 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
960 return Res;
961}
962
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000963llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
964 CodeGenFunction &CGF) const {
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000965 // Assume that va_list type is correct; should be pointer to LLVM type:
966 // struct {
967 // i32 gp_offset;
968 // i32 fp_offset;
969 // i8* overflow_arg_area;
970 // i8* reg_save_area;
971 // };
972 unsigned neededInt, neededSSE;
973 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
974 neededInt, neededSSE);
975
976 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
977 // in the registers. If not go to step 7.
978 if (!neededInt && !neededSSE)
979 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
980
981 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
982 // general purpose registers needed to pass type and num_fp to hold
983 // the number of floating point registers needed.
984
985 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
986 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
987 // l->fp_offset > 304 - num_fp * 16 go to step 7.
988 //
989 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
990 // register save space).
991
992 llvm::Value *InRegs = 0;
993 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
994 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
995 if (neededInt) {
996 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
997 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
998 InRegs =
999 CGF.Builder.CreateICmpULE(gp_offset,
1000 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1001 48 - neededInt * 8),
1002 "fits_in_gp");
1003 }
1004
1005 if (neededSSE) {
1006 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1007 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1008 llvm::Value *FitsInFP =
1009 CGF.Builder.CreateICmpULE(fp_offset,
1010 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar90dafa12009-02-18 22:19:44 +00001011 176 - neededSSE * 16),
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001012 "fits_in_fp");
Daniel Dunbarf2313462009-02-18 22:05:01 +00001013 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001014 }
1015
1016 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1017 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1018 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1019 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1020
1021 // Emit code to load the value if it was passed in registers.
1022
1023 CGF.EmitBlock(InRegBlock);
1024
1025 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1026 // an offset of l->gp_offset and/or l->fp_offset. This may require
1027 // copying to a temporary location in case the parameter is passed
1028 // in different register classes or requires an alignment greater
1029 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001030 //
1031 // FIXME: This really results in shameful code when we end up
1032 // needing to collect arguments from different places; often what
1033 // should result in a simple assembling of a structure from
1034 // scattered addresses has many more loads than necessary. Can we
1035 // clean this up?
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001036 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1037 llvm::Value *RegAddr =
1038 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1039 "reg_save_area");
1040 if (neededInt && neededSSE) {
Daniel Dunbar55e5d892009-02-13 17:46:31 +00001041 // FIXME: Cleanup.
1042 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1043 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1044 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1045 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1046 const llvm::Type *TyLo = ST->getElementType(0);
1047 const llvm::Type *TyHi = ST->getElementType(1);
1048 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1049 "Unexpected ABI info for mixed regs");
1050 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1051 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1052 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1053 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1054 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1055 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1056 llvm::Value *V =
1057 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1058 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1059 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1060 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1061
1062 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001063 } else if (neededInt) {
1064 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1065 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1066 llvm::PointerType::getUnqual(LTy));
1067 } else {
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001068 if (neededSSE == 1) {
1069 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1070 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1071 llvm::PointerType::getUnqual(LTy));
1072 } else {
1073 assert(neededSSE == 2 && "Invalid number of needed registers!");
1074 // SSE registers are spaced 16 bytes apart in the register save
1075 // area, we need to collect the two eightbytes together.
1076 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1077 llvm::Value *RegAddrHi =
1078 CGF.Builder.CreateGEP(RegAddrLo,
1079 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1080 const llvm::Type *DblPtrTy =
1081 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1082 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1083 llvm::Type::DoubleTy,
1084 NULL);
1085 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1086 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1087 DblPtrTy));
1088 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1089 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1090 DblPtrTy));
1091 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1092 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1093 llvm::PointerType::getUnqual(LTy));
1094 }
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001095 }
1096
1097 // AMD64-ABI 3.5.7p5: Step 5. Set:
1098 // l->gp_offset = l->gp_offset + num_gp * 8
1099 // l->fp_offset = l->fp_offset + num_fp * 16.
1100 if (neededInt) {
1101 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1102 neededInt * 8);
1103 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1104 gp_offset_p);
1105 }
1106 if (neededSSE) {
1107 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1108 neededSSE * 16);
1109 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1110 fp_offset_p);
1111 }
1112 CGF.EmitBranch(ContBlock);
1113
1114 // Emit code to load the value if it was passed in memory.
1115
1116 CGF.EmitBlock(InMemBlock);
1117 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1118
1119 // Return the appropriate result.
1120
1121 CGF.EmitBlock(ContBlock);
1122 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1123 "vaarg.addr");
1124 ResAddr->reserveOperandSpace(2);
1125 ResAddr->addIncoming(RegAddr, InRegBlock);
1126 ResAddr->addIncoming(MemAddr, InMemBlock);
1127
1128 return ResAddr;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001129}
1130
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001131ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001132 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001133 if (RetTy->isVoidType()) {
1134 return ABIArgInfo::getIgnore();
1135 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001136 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001137 } else {
1138 return ABIArgInfo::getDirect();
1139 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001140}
1141
1142ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001143 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001144 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001145 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001146 } else {
1147 return ABIArgInfo::getDirect();
1148 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001149}
1150
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001151llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1152 CodeGenFunction &CGF) const {
1153 return 0;
1154}
1155
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001156const ABIInfo &CodeGenTypes::getABIInfo() const {
1157 if (TheABIInfo)
1158 return *TheABIInfo;
1159
1160 // For now we just cache this in the CodeGenTypes and don't bother
1161 // to free it.
1162 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1163 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001164 switch (getContext().Target.getPointerWidth(0)) {
1165 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001166 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001167 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +00001168 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001169 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001170 }
1171
1172 return *(TheABIInfo = new DefaultABIInfo);
1173}
1174
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001175/***/
1176
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001177CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1178 const llvm::SmallVector<QualType, 16> &ArgTys) {
1179 NumArgs = ArgTys.size();
1180 Args = new ArgInfo[1 + NumArgs];
1181 Args[0].type = ResTy;
1182 for (unsigned i = 0; i < NumArgs; ++i)
1183 Args[1 + i].type = ArgTys[i];
1184}
1185
1186/***/
1187
Daniel Dunbar56273772008-09-17 00:51:38 +00001188void CodeGenTypes::GetExpandedTypes(QualType Ty,
1189 std::vector<const llvm::Type*> &ArgTys) {
1190 const RecordType *RT = Ty->getAsStructureType();
1191 assert(RT && "Can only expand structure types.");
1192 const RecordDecl *RD = RT->getDecl();
1193 assert(!RD->hasFlexibleArrayMember() &&
1194 "Cannot expand structure with flexible array.");
1195
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001196 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +00001197 e = RD->field_end(); i != e; ++i) {
1198 const FieldDecl *FD = *i;
1199 assert(!FD->isBitField() &&
1200 "Cannot expand structure with bit-field members.");
1201
1202 QualType FT = FD->getType();
1203 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1204 GetExpandedTypes(FT, ArgTys);
1205 } else {
1206 ArgTys.push_back(ConvertType(FT));
1207 }
1208 }
1209}
1210
1211llvm::Function::arg_iterator
1212CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1213 llvm::Function::arg_iterator AI) {
1214 const RecordType *RT = Ty->getAsStructureType();
1215 assert(RT && "Can only expand structure types.");
1216
1217 RecordDecl *RD = RT->getDecl();
1218 assert(LV.isSimple() &&
1219 "Unexpected non-simple lvalue during struct expansion.");
1220 llvm::Value *Addr = LV.getAddress();
1221 for (RecordDecl::field_iterator i = RD->field_begin(),
1222 e = RD->field_end(); i != e; ++i) {
1223 FieldDecl *FD = *i;
1224 QualType FT = FD->getType();
1225
1226 // FIXME: What are the right qualifiers here?
1227 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1228 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1229 AI = ExpandTypeFromArgs(FT, LV, AI);
1230 } else {
1231 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1232 ++AI;
1233 }
1234 }
1235
1236 return AI;
1237}
1238
1239void
1240CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1241 llvm::SmallVector<llvm::Value*, 16> &Args) {
1242 const RecordType *RT = Ty->getAsStructureType();
1243 assert(RT && "Can only expand structure types.");
1244
1245 RecordDecl *RD = RT->getDecl();
1246 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1247 llvm::Value *Addr = RV.getAggregateAddr();
1248 for (RecordDecl::field_iterator i = RD->field_begin(),
1249 e = RD->field_end(); i != e; ++i) {
1250 FieldDecl *FD = *i;
1251 QualType FT = FD->getType();
1252
1253 // FIXME: What are the right qualifiers here?
1254 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1255 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1256 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1257 } else {
1258 RValue RV = EmitLoadOfLValue(LV, FT);
1259 assert(RV.isScalar() &&
1260 "Unexpected non-scalar rvalue during struct expansion.");
1261 Args.push_back(RV.getScalarVal());
1262 }
1263 }
1264}
1265
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001266/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1267/// a pointer to an object of type \arg Ty.
1268///
1269/// This safely handles the case when the src type is smaller than the
1270/// destination type; in this situation the values of bits which not
1271/// present in the src are undefined.
1272static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1273 const llvm::Type *Ty,
1274 CodeGenFunction &CGF) {
1275 const llvm::Type *SrcTy =
1276 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1277 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1278 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1279
Daniel Dunbarb225be42009-02-03 05:59:18 +00001280 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001281 if (SrcSize == DstSize) {
1282 llvm::Value *Casted =
1283 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001284 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1285 // FIXME: Use better alignment / avoid requiring aligned load.
1286 Load->setAlignment(1);
1287 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001288 } else {
1289 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1290
1291 // Otherwise do coercion through memory. This is stupid, but
1292 // simple.
1293 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1294 llvm::Value *Casted =
1295 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001296 llvm::StoreInst *Store =
1297 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1298 // FIXME: Use better alignment / avoid requiring aligned store.
1299 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001300 return CGF.Builder.CreateLoad(Tmp);
1301 }
1302}
1303
1304/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1305/// where the source and destination may have different types.
1306///
1307/// This safely handles the case when the src type is larger than the
1308/// destination type; the upper bits of the src will be lost.
1309static void CreateCoercedStore(llvm::Value *Src,
1310 llvm::Value *DstPtr,
1311 CodeGenFunction &CGF) {
1312 const llvm::Type *SrcTy = Src->getType();
1313 const llvm::Type *DstTy =
1314 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1315
1316 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1317 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1318
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001319 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001320 if (SrcSize == DstSize) {
1321 llvm::Value *Casted =
1322 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001323 // FIXME: Use better alignment / avoid requiring aligned store.
1324 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001325 } else {
1326 assert(SrcSize > DstSize && "Coercion is missing bits!");
1327
1328 // Otherwise do coercion through memory. This is stupid, but
1329 // simple.
1330 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1331 CGF.Builder.CreateStore(Src, Tmp);
1332 llvm::Value *Casted =
1333 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001334 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1335 // FIXME: Use better alignment / avoid requiring aligned load.
1336 Load->setAlignment(1);
1337 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001338 }
1339}
1340
Daniel Dunbar56273772008-09-17 00:51:38 +00001341/***/
1342
Daniel Dunbar88b53962009-02-02 22:03:45 +00001343bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001344 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001345}
1346
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001347const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001348CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001349 std::vector<const llvm::Type*> ArgTys;
1350
1351 const llvm::Type *ResultType = 0;
1352
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001353 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001354 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001355 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001356 case ABIArgInfo::Expand:
1357 assert(0 && "Invalid ABI kind for return argument");
1358
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001359 case ABIArgInfo::Direct:
1360 ResultType = ConvertType(RetTy);
1361 break;
1362
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001363 case ABIArgInfo::Indirect: {
1364 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001365 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001366 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001367 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1368 break;
1369 }
1370
Daniel Dunbar11434922009-01-26 21:26:08 +00001371 case ABIArgInfo::Ignore:
1372 ResultType = llvm::Type::VoidTy;
1373 break;
1374
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001375 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001376 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001377 break;
1378 }
1379
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001380 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1381 ie = FI.arg_end(); it != ie; ++it) {
1382 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001383
1384 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001385 case ABIArgInfo::Ignore:
1386 break;
1387
Daniel Dunbar56273772008-09-17 00:51:38 +00001388 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001389 ArgTys.push_back(AI.getCoerceToType());
1390 break;
1391
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001392 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001393 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001394 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1395 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001396 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001397 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001398
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001399 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001400 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001401 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001402
1403 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001404 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001405 break;
1406 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001407 }
1408
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001409 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001410}
1411
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001412void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001413 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001414 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001415 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001416 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001417
1418 if (TargetDecl) {
1419 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001420 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001421 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001422 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001423 if (TargetDecl->getAttr<PureAttr>())
1424 FuncAttrs |= llvm::Attribute::ReadOnly;
1425 if (TargetDecl->getAttr<ConstAttr>())
1426 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001427 }
1428
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001429 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001430 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001431 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001432 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001433 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001434 if (RetTy->isPromotableIntegerType()) {
1435 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001436 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001437 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001438 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001439 }
1440 }
1441 break;
1442
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001443 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001444 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001445 llvm::Attribute::StructRet |
1446 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001447 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001448 break;
1449
Daniel Dunbar11434922009-01-26 21:26:08 +00001450 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001451 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001452 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001453
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001454 case ABIArgInfo::Expand:
1455 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001456 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001457
Devang Patela2c69122008-09-26 22:53:57 +00001458 if (RetAttrs)
1459 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001460 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1461 ie = FI.arg_end(); it != ie; ++it) {
1462 QualType ParamType = it->type;
1463 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001464 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001465
1466 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001467 case ABIArgInfo::Coerce:
1468 break;
1469
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001470 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001471 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarca008822009-02-05 01:31:19 +00001472 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001473 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001474 break;
1475
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001476 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001477 if (ParamType->isPromotableIntegerType()) {
1478 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001479 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001480 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001481 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001482 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001483 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001484 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001485
Daniel Dunbar11434922009-01-26 21:26:08 +00001486 case ABIArgInfo::Ignore:
1487 // Skip increment, no matching LLVM parameter.
1488 continue;
1489
Daniel Dunbar56273772008-09-17 00:51:38 +00001490 case ABIArgInfo::Expand: {
1491 std::vector<const llvm::Type*> Tys;
1492 // FIXME: This is rather inefficient. Do we ever actually need
1493 // to do anything here? The result should be just reconstructed
1494 // on the other side, so extension should be a non-issue.
1495 getTypes().GetExpandedTypes(ParamType, Tys);
1496 Index += Tys.size();
1497 continue;
1498 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001499 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001500
Devang Patel761d7f72008-09-25 21:02:23 +00001501 if (Attributes)
1502 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001503 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001504 }
Devang Patela2c69122008-09-26 22:53:57 +00001505 if (FuncAttrs)
1506 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1507
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001508}
1509
Daniel Dunbar88b53962009-02-02 22:03:45 +00001510void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1511 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001512 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001513 // FIXME: We no longer need the types from FunctionArgList; lift up
1514 // and simplify.
1515
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001516 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1517 llvm::Function::arg_iterator AI = Fn->arg_begin();
1518
1519 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001520 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001521 AI->setName("agg.result");
1522 ++AI;
1523 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001524
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001525 assert(FI.arg_size() == Args.size() &&
1526 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001527 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001528 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001529 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001530 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001531 QualType Ty = info_it->type;
1532 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001533
1534 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001535 case ABIArgInfo::Indirect: {
1536 llvm::Value* V = AI;
1537 if (hasAggregateLLVMType(Ty)) {
1538 // Do nothing, aggregates and complex variables are accessed by
1539 // reference.
1540 } else {
1541 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001542 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001543 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1544 // This must be a promotion, for something like
1545 // "void a(x) short x; {..."
1546 V = EmitScalarConversion(V, Ty, Arg->getType());
1547 }
1548 }
1549 EmitParmDecl(*Arg, V);
1550 break;
1551 }
1552
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001553 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001554 assert(AI != Fn->arg_end() && "Argument mismatch!");
1555 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001556 if (hasAggregateLLVMType(Ty)) {
1557 // Create a temporary alloca to hold the argument; the rest of
1558 // codegen expects to access aggregates & complex values by
1559 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001560 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001561 Builder.CreateStore(AI, V);
1562 } else {
1563 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1564 // This must be a promotion, for something like
1565 // "void a(x) short x; {..."
1566 V = EmitScalarConversion(V, Ty, Arg->getType());
1567 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001568 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001569 EmitParmDecl(*Arg, V);
1570 break;
1571 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001572
1573 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001574 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001575 // we need to create a temporary and reconstruct it from the
1576 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001577 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001578 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001579 (Name + ".addr").c_str());
1580 // FIXME: What are the right qualifiers here?
1581 llvm::Function::arg_iterator End =
1582 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1583 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001584
Daniel Dunbar56273772008-09-17 00:51:38 +00001585 // Name the arguments used in expansion and increment AI.
1586 unsigned Index = 0;
1587 for (; AI != End; ++AI, ++Index)
1588 AI->setName(Name + "." + llvm::utostr(Index));
1589 continue;
1590 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001591
1592 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001593 // Initialize the local variable appropriately.
1594 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001595 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001596 } else {
1597 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1598 }
1599
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001600 // Skip increment, no matching LLVM parameter.
1601 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001602
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001603 case ABIArgInfo::Coerce: {
1604 assert(AI != Fn->arg_end() && "Argument mismatch!");
1605 // FIXME: This is very wasteful; EmitParmDecl is just going to
1606 // drop the result in a new alloca anyway, so we could just
1607 // store into that directly if we broke the abstraction down
1608 // more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001609 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001610 CreateCoercedStore(AI, V, *this);
1611 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001612 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001613 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001614 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1615 // This must be a promotion, for something like
1616 // "void a(x) short x; {..."
1617 V = EmitScalarConversion(V, Ty, Arg->getType());
1618 }
1619 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001620 EmitParmDecl(*Arg, V);
1621 break;
1622 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001623 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001624
1625 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001626 }
1627 assert(AI == Fn->arg_end() && "Argument mismatch!");
1628}
1629
Daniel Dunbar88b53962009-02-02 22:03:45 +00001630void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001631 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001632 llvm::Value *RV = 0;
1633
1634 // Functions with no result always return void.
1635 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001636 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001637 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001638
1639 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001640 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001641 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001642 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1643 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1644 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1645 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1646 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001647 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1648 false);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001649 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001650 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001651
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001652 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001653 // The internal return value temp always will have
1654 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001655 RV = Builder.CreateLoad(ReturnValue);
1656 break;
1657
Daniel Dunbar11434922009-01-26 21:26:08 +00001658 case ABIArgInfo::Ignore:
1659 break;
1660
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001661 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001662 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001663 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001664
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001665 case ABIArgInfo::Expand:
1666 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001667 }
1668 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001669
1670 if (RV) {
1671 Builder.CreateRet(RV);
1672 } else {
1673 Builder.CreateRetVoid();
1674 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001675}
1676
Daniel Dunbar88b53962009-02-02 22:03:45 +00001677RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1678 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001679 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001680 // FIXME: We no longer need the types from CallArgs; lift up and
1681 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001682 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001683
1684 // Handle struct-return functions by passing a pointer to the
1685 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001686 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001687 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00001688 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001689 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001690 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001691 }
1692
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001693 assert(CallInfo.arg_size() == CallArgs.size() &&
1694 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001695 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001696 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001697 I != E; ++I, ++info_it) {
1698 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001699 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001700
1701 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001702 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001703 if (RV.isScalar() || RV.isComplex()) {
1704 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001705 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001706 if (RV.isScalar())
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001707 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001708 else
1709 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1710 } else {
1711 Args.push_back(RV.getAggregateAddr());
1712 }
1713 break;
1714
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001715 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001716 if (RV.isScalar()) {
1717 Args.push_back(RV.getScalarVal());
1718 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001719 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1720 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1721 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1722 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00001723 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001724 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00001725 }
1726 break;
1727
Daniel Dunbar11434922009-01-26 21:26:08 +00001728 case ABIArgInfo::Ignore:
1729 break;
1730
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001731 case ABIArgInfo::Coerce: {
1732 // FIXME: Avoid the conversion through memory if possible.
1733 llvm::Value *SrcPtr;
1734 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00001735 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001736 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001737 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001738 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001739 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1740 } else
1741 SrcPtr = RV.getAggregateAddr();
1742 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1743 *this));
1744 break;
1745 }
1746
Daniel Dunbar56273772008-09-17 00:51:38 +00001747 case ABIArgInfo::Expand:
1748 ExpandTypeToArgs(I->second, RV, Args);
1749 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001750 }
1751 }
1752
1753 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001754
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001755 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001756 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001757 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001758 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001759 AttributeList.size()));
1760
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001761 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1762 CI->setCallingConv(F->getCallingConv());
1763 if (CI->getType() != llvm::Type::VoidTy)
1764 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001765
1766 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001767 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001768 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001769 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001770 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001771 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001772 else
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001773 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001774
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001775 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001776 if (RetTy->isAnyComplexType()) {
1777 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1778 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1779 return RValue::getComplex(std::make_pair(Real, Imag));
1780 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001781 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001782 Builder.CreateStore(CI, V);
1783 return RValue::getAggregate(V);
1784 } else
1785 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001786
Daniel Dunbar11434922009-01-26 21:26:08 +00001787 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001788 // If we are ignoring an argument that had a result, make sure to
1789 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001790 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001791
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001792 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001793 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001794 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001795 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001796 if (RetTy->isAnyComplexType())
1797 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001798 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001799 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001800 else
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001801 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001802 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001803
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001804 case ABIArgInfo::Expand:
1805 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001806 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001807
1808 assert(0 && "Unhandled ABIArgInfo::Kind");
1809 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001810}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001811
1812/* VarArg handling */
1813
1814llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1815 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1816}