blob: 9e52cbb79cf81ca9146a0271c85012cc1b52c3f3 [file] [log] [blame]
Daniel Dunbar0dbe2272008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar99037e52009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000025#include "llvm/Support/CallSite.h"
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +000026#include "llvm/Support/CommandLine.h"
Daniel Dunbarbe9eb092009-02-12 09:04:14 +000027#include "llvm/Support/MathExtras.h"
Daniel Dunbar6f7279b2009-02-04 23:24:38 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000030
31#include "ABIInfo.h"
32
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000033using namespace clang;
34using namespace CodeGen;
35
36/***/
37
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000038// FIXME: Use iterator and sidestep silly type array creation.
39
Daniel Dunbar541b63b2009-02-02 23:23:47 +000040const
Douglas Gregor72564e72009-02-26 23:50:07 +000041CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000042 return getFunctionInfo(FTNP->getResultType(),
43 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000044}
45
Daniel Dunbar541b63b2009-02-02 23:23:47 +000046const
Douglas Gregor72564e72009-02-26 23:50:07 +000047CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000048 llvm::SmallVector<QualType, 16> ArgTys;
49 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000050 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000051 ArgTys.push_back(FTP->getArgType(i));
52 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000053}
54
Daniel Dunbar541b63b2009-02-02 23:23:47 +000055const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000056 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +000057 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar541b63b2009-02-02 23:23:47 +000058 return getFunctionInfo(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +000059 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000060}
61
Daniel Dunbar541b63b2009-02-02 23:23:47 +000062const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
63 llvm::SmallVector<QualType, 16> ArgTys;
64 ArgTys.push_back(MD->getSelfDecl()->getType());
65 ArgTys.push_back(Context.getObjCSelType());
66 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +000067 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000068 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000069 ArgTys.push_back((*i)->getType());
70 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000071}
72
Daniel Dunbar541b63b2009-02-02 23:23:47 +000073const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
74 const CallArgList &Args) {
75 // FIXME: Kill copy.
76 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000077 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
78 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079 ArgTys.push_back(i->second);
80 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000081}
82
Daniel Dunbar541b63b2009-02-02 23:23:47 +000083const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
84 const FunctionArgList &Args) {
85 // FIXME: Kill copy.
86 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +000087 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
88 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000089 ArgTys.push_back(i->second);
90 return getFunctionInfo(ResTy, ArgTys);
91}
92
93const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
94 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +000095 // Lookup or create unique function info.
96 llvm::FoldingSetNodeID ID;
97 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
98
99 void *InsertPos = 0;
100 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
101 if (FI)
102 return *FI;
103
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000104 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000105 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000106 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000107
108 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000109 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000110
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000111 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000112}
113
114/***/
115
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000116ABIInfo::~ABIInfo() {}
117
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000118void ABIArgInfo::dump() const {
119 fprintf(stderr, "(ABIArgInfo Kind=");
120 switch (TheKind) {
121 case Direct:
122 fprintf(stderr, "Direct");
123 break;
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000124 case Ignore:
125 fprintf(stderr, "Ignore");
126 break;
127 case Coerce:
128 fprintf(stderr, "Coerce Type=");
129 getCoerceToType()->print(llvm::errs());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000130 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000131 case Indirect:
132 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000133 break;
134 case Expand:
135 fprintf(stderr, "Expand");
136 break;
137 }
138 fprintf(stderr, ")\n");
139}
140
141/***/
142
Daniel Dunbar834af452008-09-17 21:22:33 +0000143/// isEmptyStruct - Return true iff a structure has no non-empty
144/// members. Note that a structure with a flexible array member is not
145/// considered empty.
146static bool isEmptyStruct(QualType T) {
147 const RecordType *RT = T->getAsStructureType();
148 if (!RT)
149 return 0;
150 const RecordDecl *RD = RT->getDecl();
151 if (RD->hasFlexibleArrayMember())
152 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000153 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000154 e = RD->field_end(); i != e; ++i) {
155 const FieldDecl *FD = *i;
156 if (!isEmptyStruct(FD->getType()))
157 return false;
158 }
159 return true;
160}
161
162/// isSingleElementStruct - Determine if a structure is a "single
163/// element struct", i.e. it has exactly one non-empty field or
164/// exactly one field which is itself a single element
165/// struct. Structures with flexible array members are never
166/// considered single element structs.
167///
168/// \return The field declaration for the single non-empty field, if
169/// it exists.
170static const FieldDecl *isSingleElementStruct(QualType T) {
171 const RecordType *RT = T->getAsStructureType();
172 if (!RT)
173 return 0;
174
175 const RecordDecl *RD = RT->getDecl();
176 if (RD->hasFlexibleArrayMember())
177 return 0;
178
179 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000180 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000181 e = RD->field_end(); i != e; ++i) {
182 const FieldDecl *FD = *i;
183 QualType FT = FD->getType();
184
185 if (isEmptyStruct(FT)) {
186 // Ignore
187 } else if (Found) {
188 return 0;
189 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
190 Found = FD;
191 } else {
192 Found = isSingleElementStruct(FT);
193 if (!Found)
194 return 0;
195 }
196 }
197
198 return Found;
199}
200
201static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
202 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
203 return false;
204
205 uint64_t Size = Context.getTypeSize(Ty);
206 return Size == 32 || Size == 64;
207}
208
209static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
210 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000212 e = RD->field_end(); i != e; ++i) {
213 const FieldDecl *FD = *i;
214
215 if (!is32Or64BitBasicType(FD->getType(), Context))
216 return false;
217
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000218 // FIXME: Reject bitfields wholesale; there are two problems, we
219 // don't know how to expand them yet, and the predicate for
220 // telling if a bitfield still counts as "basic" is more
221 // complicated than what we were doing previously.
222 if (FD->isBitField())
223 return false;
Daniel Dunbar834af452008-09-17 21:22:33 +0000224 }
Daniel Dunbare06a75f2009-03-11 22:05:26 +0000225
Daniel Dunbar834af452008-09-17 21:22:33 +0000226 return true;
227}
228
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000229namespace {
230/// DefaultABIInfo - The default implementation for ABI specific
231/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000232/// self-consistent and sensible LLVM IR generation, but does not
233/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000234class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000235 ABIArgInfo classifyReturnType(QualType RetTy,
236 ASTContext &Context) const;
237
238 ABIArgInfo classifyArgumentType(QualType RetTy,
239 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000240
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000241 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
242 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
243 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
244 it != ie; ++it)
245 it->info = classifyArgumentType(it->type, Context);
246 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000247
248 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
249 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000250};
251
252/// X86_32ABIInfo - The X86-32 ABI information.
253class X86_32ABIInfo : public ABIInfo {
254public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000255 ABIArgInfo classifyReturnType(QualType RetTy,
256 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000257
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000258 ABIArgInfo classifyArgumentType(QualType RetTy,
259 ASTContext &Context) const;
260
261 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
262 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
263 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
264 it != ie; ++it)
265 it->info = classifyArgumentType(it->type, Context);
266 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000267
268 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
269 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000270};
271}
272
273ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
274 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000275 if (RetTy->isVoidType()) {
276 return ABIArgInfo::getIgnore();
277 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000278 // Classify "single element" structs as their element type.
279 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
280 if (SeltFD) {
281 QualType SeltTy = SeltFD->getType()->getDesugaredType();
282 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
283 // FIXME: This is gross, it would be nice if we could just
284 // pass back SeltTy and have clients deal with it. Is it worth
285 // supporting coerce to both LLVM and clang Types?
286 if (BT->isIntegerType()) {
287 uint64_t Size = Context.getTypeSize(SeltTy);
288 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
289 } else if (BT->getKind() == BuiltinType::Float) {
290 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
291 } else if (BT->getKind() == BuiltinType::Double) {
292 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
293 }
294 } else if (SeltTy->isPointerType()) {
295 // FIXME: It would be really nice if this could come out as
296 // the proper pointer type.
297 llvm::Type *PtrTy =
298 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
299 return ABIArgInfo::getCoerce(PtrTy);
300 }
301 }
302
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000303 uint64_t Size = Context.getTypeSize(RetTy);
304 if (Size == 8) {
305 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
306 } else if (Size == 16) {
307 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
308 } else if (Size == 32) {
309 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
310 } else if (Size == 64) {
311 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
312 } else {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000313 return ABIArgInfo::getIndirect(0);
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000314 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000315 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000316 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000317 }
318}
319
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000320ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000321 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000322 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000323 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000324 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000325 if (const RecordType *RT = Ty->getAsStructureType())
326 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000327 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000328
Daniel Dunbar3170c932009-02-05 01:50:07 +0000329 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000330 uint64_t Size = Context.getTypeSize(Ty);
331 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000332 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000333
334 // Expand structs with size <= 128-bits which consist only of
335 // basic types (int, long long, float, double, xxx*). This is
336 // non-recursive and does not ignore empty fields.
337 if (const RecordType *RT = Ty->getAsStructureType()) {
338 if (Context.getTypeSize(Ty) <= 4*32 &&
339 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
340 return ABIArgInfo::getExpand();
341 }
342
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000343 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000344 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000345 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000346 }
347}
348
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000349llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
350 CodeGenFunction &CGF) const {
351 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
352 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
353
354 CGBuilderTy &Builder = CGF.Builder;
355 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
356 "ap");
357 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
358 llvm::Type *PTy =
359 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
360 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
361
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000362 uint64_t Offset =
363 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000364 llvm::Value *NextAddr =
365 Builder.CreateGEP(Addr,
Daniel Dunbar570f0cf2009-02-18 22:28:45 +0000366 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000367 "ap.next");
368 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
369
370 return AddrTyped;
371}
372
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000373namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000374/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000375class X86_64ABIInfo : public ABIInfo {
376 enum Class {
377 Integer = 0,
378 SSE,
379 SSEUp,
380 X87,
381 X87Up,
382 ComplexX87,
383 NoClass,
384 Memory
385 };
386
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000387 /// merge - Implement the X86_64 ABI merging algorithm.
388 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000389 /// Merge an accumulating classification \arg Accum with a field
390 /// classification \arg Field.
391 ///
392 /// \param Accum - The accumulating classification. This should
393 /// always be either NoClass or the result of a previous merge
394 /// call. In addition, this should never be Memory (the caller
395 /// should just return Memory for the aggregate).
396 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000397
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000398 /// classify - Determine the x86_64 register classes in which the
399 /// given type T should be passed.
400 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000401 /// \param Lo - The classification for the parts of the type
402 /// residing in the low word of the containing object.
403 ///
404 /// \param Hi - The classification for the parts of the type
405 /// residing in the high word of the containing object.
406 ///
407 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000408 /// containing object. Some parameters are classified different
409 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000410 ///
411 /// If a word is unused its result will be NoClass; if a type should
412 /// be passed in Memory then at least the classification of \arg Lo
413 /// will be Memory.
414 ///
415 /// The \arg Lo class will be NoClass iff the argument is ignored.
416 ///
417 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000418 /// also be ComplexX87.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000419 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000420 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000421
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000422 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
423 /// to coerce to, chose the best way to pass Ty in the same place
424 /// that \arg CoerceTo would be passed, but while keeping the
425 /// emitted code as simple as possible.
426 ///
427 /// FIXME: Note, this should be cleaned up to just take an
428 /// enumeration of all the ways we might want to pass things,
429 /// instead of constructing an LLVM type. This makes this code more
430 /// explicit, and it makes it clearer that we are also doing this
431 /// for correctness in the case of passing scalar types.
432 ABIArgInfo getCoerceResult(QualType Ty,
433 const llvm::Type *CoerceTo,
434 ASTContext &Context) const;
435
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000436 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000437 ASTContext &Context) const;
438
439 ABIArgInfo classifyArgumentType(QualType Ty,
440 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000441 unsigned &neededInt,
442 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000443
444public:
445 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000446
447 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
448 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000449};
450}
451
Daniel Dunbarc4503572009-01-31 00:06:58 +0000452X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
453 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000454 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
455 // classified recursively so that always two fields are
456 // considered. The resulting class is calculated according to
457 // the classes of the fields in the eightbyte:
458 //
459 // (a) If both classes are equal, this is the resulting class.
460 //
461 // (b) If one of the classes is NO_CLASS, the resulting class is
462 // the other class.
463 //
464 // (c) If one of the classes is MEMORY, the result is the MEMORY
465 // class.
466 //
467 // (d) If one of the classes is INTEGER, the result is the
468 // INTEGER.
469 //
470 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
471 // MEMORY is used as class.
472 //
473 // (f) Otherwise class SSE is used.
Daniel Dunbar100f4022009-03-06 17:50:25 +0000474
475 // Accum should never be memory (we should have returned) or
476 // ComplexX87 (because this cannot be passed in a structure).
477 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbarc4503572009-01-31 00:06:58 +0000478 "Invalid accumulated classification during merge.");
479 if (Accum == Field || Field == NoClass)
480 return Accum;
481 else if (Field == Memory)
482 return Memory;
483 else if (Accum == NoClass)
484 return Field;
485 else if (Accum == Integer || Field == Integer)
486 return Integer;
487 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
488 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000489 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000490 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000491}
492
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000493void X86_64ABIInfo::classify(QualType Ty,
494 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000495 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000496 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000497 // FIXME: This code can be simplified by introducing a simple value
498 // class for Class pairs with appropriate constructor methods for
499 // the various situations.
500
Daniel Dunbare28099b2009-02-22 04:48:22 +0000501 // FIXME: Some of the split computations are wrong; unaligned
502 // vectors shouldn't be passed in registers for example, so there is
503 // no chance they can straddle an eightbyte. Verify & simplify.
504
Daniel Dunbarc4503572009-01-31 00:06:58 +0000505 Lo = Hi = NoClass;
506
507 Class &Current = OffsetBase < 64 ? Lo : Hi;
508 Current = Memory;
509
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000510 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
511 BuiltinType::Kind k = BT->getKind();
512
Daniel Dunbar11434922009-01-26 21:26:08 +0000513 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000514 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000515 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000516 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000517 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000518 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000519 } else if (k == BuiltinType::LongDouble) {
520 Lo = X87;
521 Hi = X87Up;
522 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000523 // FIXME: _Decimal32 and _Decimal64 are SSE.
524 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000525 // FIXME: __int128 is (Integer, Integer).
Anders Carlsson708762b2009-02-26 17:31:15 +0000526 } else if (const EnumType *ET = Ty->getAsEnumType()) {
527 // Classify the underlying integer type.
528 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbar89588912009-02-26 20:52:22 +0000529 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000530 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000531 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000532 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbare28099b2009-02-22 04:48:22 +0000533 if (Size == 32) {
534 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
535 // float> as integer.
536 Current = Integer;
537
538 // If this type crosses an eightbyte boundary, it should be
539 // split.
540 uint64_t EB_Real = (OffsetBase) / 64;
541 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
542 if (EB_Real != EB_Imag)
543 Hi = Lo;
544 } else if (Size == 64) {
Daniel Dunbar0af99292009-02-22 04:16:10 +0000545 // gcc passes <1 x double> in memory. :(
546 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000547 return;
Daniel Dunbar0af99292009-02-22 04:16:10 +0000548
549 // gcc passes <1 x long long> as INTEGER.
550 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
551 Current = Integer;
552 else
553 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000554
555 // If this type crosses an eightbyte boundary, it should be
556 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000557 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000558 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000559 } else if (Size == 128) {
560 Lo = SSE;
561 Hi = SSEUp;
562 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000563 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000564 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000565
Daniel Dunbare33edf12009-01-30 18:40:10 +0000566 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar0af99292009-02-22 04:16:10 +0000567 if (ET->isIntegralType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000568 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000569 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000570 else if (Size <= 128)
571 Lo = Hi = Integer;
572 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000573 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000574 else if (ET == Context.DoubleTy)
575 Lo = Hi = SSE;
576 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000577 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000578
579 // If this complex type crosses an eightbyte boundary then it
580 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000581 uint64_t EB_Real = (OffsetBase) / 64;
582 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000583 if (Hi == NoClass && EB_Real != EB_Imag)
584 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000585 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
586 // Arrays are treated like structures.
587
588 uint64_t Size = Context.getTypeSize(Ty);
589
590 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
591 // than two eightbytes, ..., it has class MEMORY.
592 if (Size > 128)
593 return;
594
595 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
596 // fields, it has class MEMORY.
597 //
598 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000599 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000600 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000601
602 // Otherwise implement simplified merge. We could be smarter about
603 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000604 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000605 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
606 uint64_t ArraySize = AT->getSize().getZExtValue();
607 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
608 Class FieldLo, FieldHi;
609 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000610 Lo = merge(Lo, FieldLo);
611 Hi = merge(Hi, FieldHi);
612 if (Lo == Memory || Hi == Memory)
613 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000614 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000615
616 // Do post merger cleanup (see below). Only case we worry about is Memory.
617 if (Hi == Memory)
618 Lo = Memory;
619 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000620 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000621 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000622
623 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
624 // than two eightbytes, ..., it has class MEMORY.
625 if (Size > 128)
626 return;
627
628 const RecordDecl *RD = RT->getDecl();
629
630 // Assume variable sized types are passed in memory.
631 if (RD->hasFlexibleArrayMember())
632 return;
633
634 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
635
636 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000637 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000638 unsigned idx = 0;
639 for (RecordDecl::field_iterator i = RD->field_begin(),
640 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000641 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbardd81d442009-02-17 02:45:44 +0000642 bool BitField = i->isBitField();
Daniel Dunbar99037e52009-01-29 08:13:58 +0000643
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000644 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
645 // fields, it has class MEMORY.
Daniel Dunbardd81d442009-02-17 02:45:44 +0000646 //
647 // Note, skip this test for bitfields, see below.
648 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar99037e52009-01-29 08:13:58 +0000649 Lo = Memory;
650 return;
651 }
652
Daniel Dunbar99037e52009-01-29 08:13:58 +0000653 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000654 //
655 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
656 // exceeds a single eightbyte, each is classified
657 // separately. Each eightbyte gets initialized to class
658 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000659 Class FieldLo, FieldHi;
Daniel Dunbardd81d442009-02-17 02:45:44 +0000660
661 // Bitfields require special handling, they do not force the
662 // structure to be passed in memory even if unaligned, and
663 // therefore they can straddle an eightbyte.
664 if (BitField) {
665 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
666 uint64_t Size =
667 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
668
669 uint64_t EB_Lo = Offset / 64;
670 uint64_t EB_Hi = (Offset + Size - 1) / 64;
671 FieldLo = FieldHi = NoClass;
672 if (EB_Lo) {
673 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
674 FieldLo = NoClass;
675 FieldHi = Integer;
676 } else {
677 FieldLo = Integer;
678 FieldHi = EB_Hi ? Integer : NoClass;
679 }
680 } else
681 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000682 Lo = merge(Lo, FieldLo);
683 Hi = merge(Hi, FieldHi);
684 if (Lo == Memory || Hi == Memory)
685 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000686 }
687
688 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
689 //
690 // (a) If one of the classes is MEMORY, the whole argument is
691 // passed in memory.
692 //
693 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
694
695 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000696 // the merge (just bail).
697 //
698 // The second condition occurs in the case of unions; for example
699 // union { _Complex double; unsigned; }.
700 if (Hi == Memory)
701 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000702 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000703 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000704 }
705}
706
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000707ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
708 const llvm::Type *CoerceTo,
709 ASTContext &Context) const {
710 if (CoerceTo == llvm::Type::Int64Ty) {
711 // Integer and pointer types will end up in a general purpose
712 // register.
Daniel Dunbar0af99292009-02-22 04:16:10 +0000713 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000714 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000715
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000716 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000717 // FIXME: It would probably be better to make CGFunctionInfo only
718 // map using canonical types than to canonize here.
719 QualType CTy = Context.getCanonicalType(Ty);
720
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000721 // Float and double end up in a single SSE reg.
Daniel Dunbar3327f6e2009-02-14 02:45:45 +0000722 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000723 return ABIArgInfo::getDirect();
Daniel Dunbar0af99292009-02-22 04:16:10 +0000724
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000725 }
726
727 return ABIArgInfo::getCoerce(CoerceTo);
728}
Daniel Dunbarc4503572009-01-31 00:06:58 +0000729
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000730ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
731 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000732 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
733 // classification algorithm.
734 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000735 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000736
Daniel Dunbarc4503572009-01-31 00:06:58 +0000737 // Check some invariants.
738 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
739 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
740 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
741
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000742 const llvm::Type *ResType = 0;
743 switch (Lo) {
744 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000745 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000746
747 case SSEUp:
748 case X87Up:
749 assert(0 && "Invalid classification for lo word.");
750
Daniel Dunbarc4503572009-01-31 00:06:58 +0000751 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000752 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000753 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000754 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000755
756 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
757 // available register of the sequence %rax, %rdx is used.
758 case Integer:
759 ResType = llvm::Type::Int64Ty; break;
760
761 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
762 // available SSE register of the sequence %xmm0, %xmm1 is used.
763 case SSE:
764 ResType = llvm::Type::DoubleTy; break;
765
766 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
767 // returned on the X87 stack in %st0 as 80-bit x87 number.
768 case X87:
769 ResType = llvm::Type::X86_FP80Ty; break;
770
Daniel Dunbarc4503572009-01-31 00:06:58 +0000771 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
772 // part of the value is returned in %st0 and the imaginary part in
773 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000774 case ComplexX87:
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000775 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar3e030b42009-02-18 03:44:19 +0000776 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
777 llvm::Type::X86_FP80Ty,
778 NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000779 break;
780 }
781
782 switch (Hi) {
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000783 // Memory was handled previously and X87 should
784 // never occur as a hi class.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000785 case Memory:
786 case X87:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000787 assert(0 && "Invalid classification for hi word.");
788
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000789 case ComplexX87: // Previously handled.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000790 case NoClass: break;
Daniel Dunbar6e53e9b2009-02-17 07:55:55 +0000791
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000792 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000793 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
794 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000795 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000796 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
797 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000798
799 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
800 // is passed in the upper half of the last used SSE register.
801 //
802 // SSEUP should always be preceeded by SSE, just widen.
803 case SSEUp:
804 assert(Lo == SSE && "Unexpected SSEUp classification.");
805 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
806 break;
807
808 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000809 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000810 case X87Up:
Daniel Dunbar100f4022009-03-06 17:50:25 +0000811 // If X87Up is preceeded by X87, we don't need to do
812 // anything. However, in some cases with unions it may not be
813 // preceeded by X87. In such situations we follow gcc and pass the
814 // extra bits in an SSE reg.
815 if (Lo != X87)
816 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000817 break;
818 }
819
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000820 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000821}
822
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000823ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000824 unsigned &neededInt,
825 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000826 X86_64ABIInfo::Class Lo, Hi;
827 classify(Ty, Context, 0, Lo, Hi);
828
829 // Check some invariants.
830 // FIXME: Enforce these by construction.
831 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
832 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
833 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
834
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000835 neededInt = 0;
836 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000837 const llvm::Type *ResType = 0;
838 switch (Lo) {
839 case NoClass:
840 return ABIArgInfo::getIgnore();
841
842 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
843 // on the stack.
844 case Memory:
845
846 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
847 // COMPLEX_X87, it is passed in memory.
848 case X87:
849 case ComplexX87:
Daniel Dunbar245f5532009-02-22 08:17:51 +0000850 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000851
852 case SSEUp:
853 case X87Up:
854 assert(0 && "Invalid classification for lo word.");
855
856 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
857 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
858 // and %r9 is used.
859 case Integer:
860 ++neededInt;
861 ResType = llvm::Type::Int64Ty;
862 break;
863
864 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
865 // available SSE register is used, the registers are taken in the
866 // order from %xmm0 to %xmm7.
867 case SSE:
868 ++neededSSE;
869 ResType = llvm::Type::DoubleTy;
870 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000871 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000872
873 switch (Hi) {
874 // Memory was handled previously, ComplexX87 and X87 should
875 // never occur as hi classes, and X87Up must be preceed by X87,
876 // which is passed in memory.
877 case Memory:
878 case X87:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000879 case ComplexX87:
880 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar100f4022009-03-06 17:50:25 +0000881 break;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000882
883 case NoClass: break;
884 case Integer:
885 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
886 ++neededInt;
887 break;
Daniel Dunbar100f4022009-03-06 17:50:25 +0000888
889 // X87Up generally doesn't occur here (long double is passed in
890 // memory), except in situations involving unions.
891 case X87Up:
892 case SSE:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000893 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
894 ++neededSSE;
895 break;
896
897 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
898 // eightbyte is passed in the upper half of the last used SSE
899 // register.
900 case SSEUp:
901 assert(Lo == SSE && "Unexpected SSEUp classification.");
902 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
903 break;
904 }
905
Daniel Dunbar644f4c32009-02-14 02:09:24 +0000906 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000907}
908
909void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
910 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
911
912 // Keep track of the number of assigned registers.
913 unsigned freeIntRegs = 6, freeSSERegs = 8;
914
915 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
916 // get assigned (in left-to-right order) for passing as follows...
917 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000918 it != ie; ++it) {
919 unsigned neededInt, neededSSE;
920 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
921
922 // AMD64-ABI 3.2.3p3: If there are no registers available for any
923 // eightbyte of an argument, the whole argument is passed on the
924 // stack. If registers have already been assigned for some
925 // eightbytes of such an argument, the assignments get reverted.
926 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
927 freeIntRegs -= neededInt;
928 freeSSERegs -= neededSSE;
929 } else {
Daniel Dunbar245f5532009-02-22 08:17:51 +0000930 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000931 }
932 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000933}
934
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000935static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
936 QualType Ty,
937 CodeGenFunction &CGF) {
938 llvm::Value *overflow_arg_area_p =
939 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
940 llvm::Value *overflow_arg_area =
941 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
942
943 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
944 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbarc5bcee42009-02-16 23:38:56 +0000945 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000946 if (Align > 8) {
Daniel Dunbarc5bcee42009-02-16 23:38:56 +0000947 // Note that we follow the ABI & gcc here, even though the type
948 // could in theory have an alignment greater than 16. This case
949 // shouldn't ever matter in practice.
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000950
Daniel Dunbarc5bcee42009-02-16 23:38:56 +0000951 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
952 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
953 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
954 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
955 llvm::Type::Int64Ty);
956 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
957 overflow_arg_area =
958 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
959 overflow_arg_area->getType(),
960 "overflow_arg_area.align");
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000961 }
962
963 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
964 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
965 llvm::Value *Res =
966 CGF.Builder.CreateBitCast(overflow_arg_area,
967 llvm::PointerType::getUnqual(LTy));
968
969 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
970 // l->overflow_arg_area + sizeof(type).
971 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
972 // an 8 byte boundary.
973
974 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
975 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
976 (SizeInBytes + 7) & ~7);
977 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
978 "overflow_arg_area.next");
979 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
980
981 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
982 return Res;
983}
984
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000985llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
986 CodeGenFunction &CGF) const {
Daniel Dunbarbe9eb092009-02-12 09:04:14 +0000987 // Assume that va_list type is correct; should be pointer to LLVM type:
988 // struct {
989 // i32 gp_offset;
990 // i32 fp_offset;
991 // i8* overflow_arg_area;
992 // i8* reg_save_area;
993 // };
994 unsigned neededInt, neededSSE;
995 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
996 neededInt, neededSSE);
997
998 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
999 // in the registers. If not go to step 7.
1000 if (!neededInt && !neededSSE)
1001 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1002
1003 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1004 // general purpose registers needed to pass type and num_fp to hold
1005 // the number of floating point registers needed.
1006
1007 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1008 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1009 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1010 //
1011 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1012 // register save space).
1013
1014 llvm::Value *InRegs = 0;
1015 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1016 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1017 if (neededInt) {
1018 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1019 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1020 InRegs =
1021 CGF.Builder.CreateICmpULE(gp_offset,
1022 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1023 48 - neededInt * 8),
1024 "fits_in_gp");
1025 }
1026
1027 if (neededSSE) {
1028 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1029 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1030 llvm::Value *FitsInFP =
1031 CGF.Builder.CreateICmpULE(fp_offset,
1032 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar90dafa12009-02-18 22:19:44 +00001033 176 - neededSSE * 16),
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001034 "fits_in_fp");
Daniel Dunbarf2313462009-02-18 22:05:01 +00001035 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001036 }
1037
1038 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1039 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1040 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1041 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1042
1043 // Emit code to load the value if it was passed in registers.
1044
1045 CGF.EmitBlock(InRegBlock);
1046
1047 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1048 // an offset of l->gp_offset and/or l->fp_offset. This may require
1049 // copying to a temporary location in case the parameter is passed
1050 // in different register classes or requires an alignment greater
1051 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001052 //
1053 // FIXME: This really results in shameful code when we end up
1054 // needing to collect arguments from different places; often what
1055 // should result in a simple assembling of a structure from
1056 // scattered addresses has many more loads than necessary. Can we
1057 // clean this up?
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001058 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1059 llvm::Value *RegAddr =
1060 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1061 "reg_save_area");
1062 if (neededInt && neededSSE) {
Daniel Dunbar55e5d892009-02-13 17:46:31 +00001063 // FIXME: Cleanup.
1064 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1065 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1066 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1067 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1068 const llvm::Type *TyLo = ST->getElementType(0);
1069 const llvm::Type *TyHi = ST->getElementType(1);
1070 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1071 "Unexpected ABI info for mixed regs");
1072 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1073 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1074 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1075 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1076 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1077 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1078 llvm::Value *V =
1079 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1080 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1081 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1082 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1083
1084 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001085 } else if (neededInt) {
1086 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1087 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1088 llvm::PointerType::getUnqual(LTy));
1089 } else {
Daniel Dunbar3e030b42009-02-18 03:44:19 +00001090 if (neededSSE == 1) {
1091 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1092 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1093 llvm::PointerType::getUnqual(LTy));
1094 } else {
1095 assert(neededSSE == 2 && "Invalid number of needed registers!");
1096 // SSE registers are spaced 16 bytes apart in the register save
1097 // area, we need to collect the two eightbytes together.
1098 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1099 llvm::Value *RegAddrHi =
1100 CGF.Builder.CreateGEP(RegAddrLo,
1101 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1102 const llvm::Type *DblPtrTy =
1103 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1104 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1105 llvm::Type::DoubleTy,
1106 NULL);
1107 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1108 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1109 DblPtrTy));
1110 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1111 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1112 DblPtrTy));
1113 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1114 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1115 llvm::PointerType::getUnqual(LTy));
1116 }
Daniel Dunbarbe9eb092009-02-12 09:04:14 +00001117 }
1118
1119 // AMD64-ABI 3.5.7p5: Step 5. Set:
1120 // l->gp_offset = l->gp_offset + num_gp * 8
1121 // l->fp_offset = l->fp_offset + num_fp * 16.
1122 if (neededInt) {
1123 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1124 neededInt * 8);
1125 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1126 gp_offset_p);
1127 }
1128 if (neededSSE) {
1129 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1130 neededSSE * 16);
1131 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1132 fp_offset_p);
1133 }
1134 CGF.EmitBranch(ContBlock);
1135
1136 // Emit code to load the value if it was passed in memory.
1137
1138 CGF.EmitBlock(InMemBlock);
1139 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1140
1141 // Return the appropriate result.
1142
1143 CGF.EmitBlock(ContBlock);
1144 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1145 "vaarg.addr");
1146 ResAddr->reserveOperandSpace(2);
1147 ResAddr->addIncoming(RegAddr, InRegBlock);
1148 ResAddr->addIncoming(MemAddr, InMemBlock);
1149
1150 return ResAddr;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001151}
1152
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001153ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001154 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001155 if (RetTy->isVoidType()) {
1156 return ABIArgInfo::getIgnore();
1157 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001158 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001159 } else {
1160 return ABIArgInfo::getDirect();
1161 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001162}
1163
1164ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001165 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001166 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001167 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001168 } else {
1169 return ABIArgInfo::getDirect();
1170 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001171}
1172
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001173llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1174 CodeGenFunction &CGF) const {
1175 return 0;
1176}
1177
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001178const ABIInfo &CodeGenTypes::getABIInfo() const {
1179 if (TheABIInfo)
1180 return *TheABIInfo;
1181
1182 // For now we just cache this in the CodeGenTypes and don't bother
1183 // to free it.
1184 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1185 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001186 switch (getContext().Target.getPointerWidth(0)) {
1187 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001188 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001189 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +00001190 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +00001191 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001192 }
1193
1194 return *(TheABIInfo = new DefaultABIInfo);
1195}
1196
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001197/***/
1198
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001199CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1200 const llvm::SmallVector<QualType, 16> &ArgTys) {
1201 NumArgs = ArgTys.size();
1202 Args = new ArgInfo[1 + NumArgs];
1203 Args[0].type = ResTy;
1204 for (unsigned i = 0; i < NumArgs; ++i)
1205 Args[1 + i].type = ArgTys[i];
1206}
1207
1208/***/
1209
Daniel Dunbar56273772008-09-17 00:51:38 +00001210void CodeGenTypes::GetExpandedTypes(QualType Ty,
1211 std::vector<const llvm::Type*> &ArgTys) {
1212 const RecordType *RT = Ty->getAsStructureType();
1213 assert(RT && "Can only expand structure types.");
1214 const RecordDecl *RD = RT->getDecl();
1215 assert(!RD->hasFlexibleArrayMember() &&
1216 "Cannot expand structure with flexible array.");
1217
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001218 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +00001219 e = RD->field_end(); i != e; ++i) {
1220 const FieldDecl *FD = *i;
1221 assert(!FD->isBitField() &&
1222 "Cannot expand structure with bit-field members.");
1223
1224 QualType FT = FD->getType();
1225 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1226 GetExpandedTypes(FT, ArgTys);
1227 } else {
1228 ArgTys.push_back(ConvertType(FT));
1229 }
1230 }
1231}
1232
1233llvm::Function::arg_iterator
1234CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1235 llvm::Function::arg_iterator AI) {
1236 const RecordType *RT = Ty->getAsStructureType();
1237 assert(RT && "Can only expand structure types.");
1238
1239 RecordDecl *RD = RT->getDecl();
1240 assert(LV.isSimple() &&
1241 "Unexpected non-simple lvalue during struct expansion.");
1242 llvm::Value *Addr = LV.getAddress();
1243 for (RecordDecl::field_iterator i = RD->field_begin(),
1244 e = RD->field_end(); i != e; ++i) {
1245 FieldDecl *FD = *i;
1246 QualType FT = FD->getType();
1247
1248 // FIXME: What are the right qualifiers here?
1249 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1250 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1251 AI = ExpandTypeFromArgs(FT, LV, AI);
1252 } else {
1253 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1254 ++AI;
1255 }
1256 }
1257
1258 return AI;
1259}
1260
1261void
1262CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1263 llvm::SmallVector<llvm::Value*, 16> &Args) {
1264 const RecordType *RT = Ty->getAsStructureType();
1265 assert(RT && "Can only expand structure types.");
1266
1267 RecordDecl *RD = RT->getDecl();
1268 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1269 llvm::Value *Addr = RV.getAggregateAddr();
1270 for (RecordDecl::field_iterator i = RD->field_begin(),
1271 e = RD->field_end(); i != e; ++i) {
1272 FieldDecl *FD = *i;
1273 QualType FT = FD->getType();
1274
1275 // FIXME: What are the right qualifiers here?
1276 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1277 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1278 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1279 } else {
1280 RValue RV = EmitLoadOfLValue(LV, FT);
1281 assert(RV.isScalar() &&
1282 "Unexpected non-scalar rvalue during struct expansion.");
1283 Args.push_back(RV.getScalarVal());
1284 }
1285 }
1286}
1287
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001288/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1289/// a pointer to an object of type \arg Ty.
1290///
1291/// This safely handles the case when the src type is smaller than the
1292/// destination type; in this situation the values of bits which not
1293/// present in the src are undefined.
1294static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1295 const llvm::Type *Ty,
1296 CodeGenFunction &CGF) {
1297 const llvm::Type *SrcTy =
1298 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1299 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1300 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1301
Daniel Dunbarb225be42009-02-03 05:59:18 +00001302 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001303 if (SrcSize == DstSize) {
1304 llvm::Value *Casted =
1305 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001306 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1307 // FIXME: Use better alignment / avoid requiring aligned load.
1308 Load->setAlignment(1);
1309 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001310 } else {
1311 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1312
1313 // Otherwise do coercion through memory. This is stupid, but
1314 // simple.
1315 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1316 llvm::Value *Casted =
1317 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001318 llvm::StoreInst *Store =
1319 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1320 // FIXME: Use better alignment / avoid requiring aligned store.
1321 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001322 return CGF.Builder.CreateLoad(Tmp);
1323 }
1324}
1325
1326/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1327/// where the source and destination may have different types.
1328///
1329/// This safely handles the case when the src type is larger than the
1330/// destination type; the upper bits of the src will be lost.
1331static void CreateCoercedStore(llvm::Value *Src,
1332 llvm::Value *DstPtr,
1333 CodeGenFunction &CGF) {
1334 const llvm::Type *SrcTy = Src->getType();
1335 const llvm::Type *DstTy =
1336 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1337
1338 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1339 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1340
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001341 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001342 if (SrcSize == DstSize) {
1343 llvm::Value *Casted =
1344 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001345 // FIXME: Use better alignment / avoid requiring aligned store.
1346 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001347 } else {
1348 assert(SrcSize > DstSize && "Coercion is missing bits!");
1349
1350 // Otherwise do coercion through memory. This is stupid, but
1351 // simple.
1352 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1353 CGF.Builder.CreateStore(Src, Tmp);
1354 llvm::Value *Casted =
1355 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001356 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1357 // FIXME: Use better alignment / avoid requiring aligned load.
1358 Load->setAlignment(1);
1359 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001360 }
1361}
1362
Daniel Dunbar56273772008-09-17 00:51:38 +00001363/***/
1364
Daniel Dunbar88b53962009-02-02 22:03:45 +00001365bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001366 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001367}
1368
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001369const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001370CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001371 std::vector<const llvm::Type*> ArgTys;
1372
1373 const llvm::Type *ResultType = 0;
1374
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001375 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001376 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001377 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001378 case ABIArgInfo::Expand:
1379 assert(0 && "Invalid ABI kind for return argument");
1380
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001381 case ABIArgInfo::Direct:
1382 ResultType = ConvertType(RetTy);
1383 break;
1384
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001385 case ABIArgInfo::Indirect: {
1386 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001387 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001388 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001389 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1390 break;
1391 }
1392
Daniel Dunbar11434922009-01-26 21:26:08 +00001393 case ABIArgInfo::Ignore:
1394 ResultType = llvm::Type::VoidTy;
1395 break;
1396
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001397 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001398 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001399 break;
1400 }
1401
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001402 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1403 ie = FI.arg_end(); it != ie; ++it) {
1404 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001405
1406 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001407 case ABIArgInfo::Ignore:
1408 break;
1409
Daniel Dunbar56273772008-09-17 00:51:38 +00001410 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001411 ArgTys.push_back(AI.getCoerceToType());
1412 break;
1413
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001414 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001415 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001416 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1417 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001418 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001419 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001420
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001421 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001422 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001423 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001424
1425 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001426 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001427 break;
1428 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001429 }
1430
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001431 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001432}
1433
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001434void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001435 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001436 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001437 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001438 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001439
1440 if (TargetDecl) {
1441 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001442 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001443 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001444 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001445 if (TargetDecl->getAttr<PureAttr>())
1446 FuncAttrs |= llvm::Attribute::ReadOnly;
1447 if (TargetDecl->getAttr<ConstAttr>())
1448 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001449 }
1450
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001451 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001452 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001453 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001454 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001455 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001456 if (RetTy->isPromotableIntegerType()) {
1457 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001458 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001459 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001460 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001461 }
1462 }
1463 break;
1464
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001465 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001466 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001467 llvm::Attribute::StructRet |
1468 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001469 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001470 break;
1471
Daniel Dunbar11434922009-01-26 21:26:08 +00001472 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001473 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001474 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001475
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001476 case ABIArgInfo::Expand:
1477 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001478 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001479
Devang Patela2c69122008-09-26 22:53:57 +00001480 if (RetAttrs)
1481 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001482 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1483 ie = FI.arg_end(); it != ie; ++it) {
1484 QualType ParamType = it->type;
1485 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001486 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001487
1488 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001489 case ABIArgInfo::Coerce:
1490 break;
1491
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001492 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001493 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarca008822009-02-05 01:31:19 +00001494 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001495 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001496 break;
1497
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001498 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001499 if (ParamType->isPromotableIntegerType()) {
1500 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001501 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001502 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001503 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001504 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001505 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001506 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001507
Daniel Dunbar11434922009-01-26 21:26:08 +00001508 case ABIArgInfo::Ignore:
1509 // Skip increment, no matching LLVM parameter.
1510 continue;
1511
Daniel Dunbar56273772008-09-17 00:51:38 +00001512 case ABIArgInfo::Expand: {
1513 std::vector<const llvm::Type*> Tys;
1514 // FIXME: This is rather inefficient. Do we ever actually need
1515 // to do anything here? The result should be just reconstructed
1516 // on the other side, so extension should be a non-issue.
1517 getTypes().GetExpandedTypes(ParamType, Tys);
1518 Index += Tys.size();
1519 continue;
1520 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001521 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001522
Devang Patel761d7f72008-09-25 21:02:23 +00001523 if (Attributes)
1524 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001525 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001526 }
Devang Patela2c69122008-09-26 22:53:57 +00001527 if (FuncAttrs)
1528 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001529}
1530
Daniel Dunbar88b53962009-02-02 22:03:45 +00001531void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1532 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001533 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001534 // FIXME: We no longer need the types from FunctionArgList; lift up
1535 // and simplify.
1536
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001537 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1538 llvm::Function::arg_iterator AI = Fn->arg_begin();
1539
1540 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001541 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001542 AI->setName("agg.result");
1543 ++AI;
1544 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001545
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001546 assert(FI.arg_size() == Args.size() &&
1547 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001548 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001549 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001550 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001551 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001552 QualType Ty = info_it->type;
1553 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001554
1555 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001556 case ABIArgInfo::Indirect: {
1557 llvm::Value* V = AI;
1558 if (hasAggregateLLVMType(Ty)) {
1559 // Do nothing, aggregates and complex variables are accessed by
1560 // reference.
1561 } else {
1562 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001563 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001564 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1565 // This must be a promotion, for something like
1566 // "void a(x) short x; {..."
1567 V = EmitScalarConversion(V, Ty, Arg->getType());
1568 }
1569 }
1570 EmitParmDecl(*Arg, V);
1571 break;
1572 }
1573
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001574 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001575 assert(AI != Fn->arg_end() && "Argument mismatch!");
1576 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001577 if (hasAggregateLLVMType(Ty)) {
1578 // Create a temporary alloca to hold the argument; the rest of
1579 // codegen expects to access aggregates & complex values by
1580 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001581 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001582 Builder.CreateStore(AI, V);
1583 } else {
1584 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1585 // This must be a promotion, for something like
1586 // "void a(x) short x; {..."
1587 V = EmitScalarConversion(V, Ty, Arg->getType());
1588 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001589 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001590 EmitParmDecl(*Arg, V);
1591 break;
1592 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001593
1594 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001595 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001596 // we need to create a temporary and reconstruct it from the
1597 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001598 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001599 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001600 (Name + ".addr").c_str());
1601 // FIXME: What are the right qualifiers here?
1602 llvm::Function::arg_iterator End =
1603 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1604 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001605
Daniel Dunbar56273772008-09-17 00:51:38 +00001606 // Name the arguments used in expansion and increment AI.
1607 unsigned Index = 0;
1608 for (; AI != End; ++AI, ++Index)
1609 AI->setName(Name + "." + llvm::utostr(Index));
1610 continue;
1611 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001612
1613 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001614 // Initialize the local variable appropriately.
1615 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001616 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001617 } else {
1618 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1619 }
1620
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001621 // Skip increment, no matching LLVM parameter.
1622 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001623
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001624 case ABIArgInfo::Coerce: {
1625 assert(AI != Fn->arg_end() && "Argument mismatch!");
1626 // FIXME: This is very wasteful; EmitParmDecl is just going to
1627 // drop the result in a new alloca anyway, so we could just
1628 // store into that directly if we broke the abstraction down
1629 // more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001630 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001631 CreateCoercedStore(AI, V, *this);
1632 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001633 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001634 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001635 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1636 // This must be a promotion, for something like
1637 // "void a(x) short x; {..."
1638 V = EmitScalarConversion(V, Ty, Arg->getType());
1639 }
1640 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001641 EmitParmDecl(*Arg, V);
1642 break;
1643 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001644 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001645
1646 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001647 }
1648 assert(AI == Fn->arg_end() && "Argument mismatch!");
1649}
1650
Daniel Dunbar88b53962009-02-02 22:03:45 +00001651void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001652 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001653 llvm::Value *RV = 0;
1654
1655 // Functions with no result always return void.
1656 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001657 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001658 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001659
1660 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001661 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001662 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001663 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1664 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1665 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1666 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1667 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001668 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1669 false);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001670 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001671 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001672
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001673 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001674 // The internal return value temp always will have
1675 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001676 RV = Builder.CreateLoad(ReturnValue);
1677 break;
1678
Daniel Dunbar11434922009-01-26 21:26:08 +00001679 case ABIArgInfo::Ignore:
1680 break;
1681
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001682 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001683 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001684 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001685
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001686 case ABIArgInfo::Expand:
1687 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001688 }
1689 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001690
1691 if (RV) {
1692 Builder.CreateRet(RV);
1693 } else {
1694 Builder.CreateRetVoid();
1695 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001696}
1697
Daniel Dunbar88b53962009-02-02 22:03:45 +00001698RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1699 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001700 const CallArgList &CallArgs,
1701 const Decl *TargetDecl) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001702 // FIXME: We no longer need the types from CallArgs; lift up and
1703 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001704 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001705
1706 // Handle struct-return functions by passing a pointer to the
1707 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001708 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001709 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00001710 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001711 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001712 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001713 }
1714
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001715 assert(CallInfo.arg_size() == CallArgs.size() &&
1716 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001717 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001718 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001719 I != E; ++I, ++info_it) {
1720 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001721 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001722
1723 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001724 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001725 if (RV.isScalar() || RV.isComplex()) {
1726 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001727 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001728 if (RV.isScalar())
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001729 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001730 else
1731 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1732 } else {
1733 Args.push_back(RV.getAggregateAddr());
1734 }
1735 break;
1736
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001737 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001738 if (RV.isScalar()) {
1739 Args.push_back(RV.getScalarVal());
1740 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001741 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1742 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1743 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1744 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00001745 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001746 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00001747 }
1748 break;
1749
Daniel Dunbar11434922009-01-26 21:26:08 +00001750 case ABIArgInfo::Ignore:
1751 break;
1752
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001753 case ABIArgInfo::Coerce: {
1754 // FIXME: Avoid the conversion through memory if possible.
1755 llvm::Value *SrcPtr;
1756 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00001757 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001758 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001759 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001760 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001761 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1762 } else
1763 SrcPtr = RV.getAggregateAddr();
1764 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1765 *this));
1766 break;
1767 }
1768
Daniel Dunbar56273772008-09-17 00:51:38 +00001769 case ABIArgInfo::Expand:
1770 ExpandTypeToArgs(I->second, RV, Args);
1771 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001772 }
1773 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001774
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001775 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel761d7f72008-09-25 21:02:23 +00001776 CodeGen::AttributeListType AttributeList;
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001777 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001778 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1779 AttributeList.end());
Daniel Dunbar725ad312009-01-31 02:19:00 +00001780
Daniel Dunbard14151d2009-03-02 04:32:35 +00001781 llvm::CallSite CS;
1782 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
1783 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001784 } else {
1785 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbard14151d2009-03-02 04:32:35 +00001786 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1787 &Args[0], &Args[0]+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001788 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001789 }
1790
Daniel Dunbard14151d2009-03-02 04:32:35 +00001791 CS.setAttributes(Attrs);
1792 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1793 CS.setCallingConv(F->getCallingConv());
1794
1795 // If the call doesn't return, finish the basic block and clear the
1796 // insertion point; this allows the rest of IRgen to discard
1797 // unreachable code.
1798 if (CS.doesNotReturn()) {
1799 Builder.CreateUnreachable();
1800 Builder.ClearInsertionPoint();
1801
1802 // FIXME: For now, emit a dummy basic block because expr
1803 // emitters in generally are not ready to handle emitting
1804 // expressions at unreachable points.
1805 EnsureInsertPoint();
1806
1807 // Return a reasonable RValue.
1808 return GetUndefRValue(RetTy);
1809 }
1810
1811 llvm::Instruction *CI = CS.getInstruction();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001812 if (CI->getType() != llvm::Type::VoidTy)
1813 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001814
1815 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001816 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001817 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001818 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001819 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001820 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001821 else
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001822 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001823
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001824 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001825 if (RetTy->isAnyComplexType()) {
1826 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1827 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1828 return RValue::getComplex(std::make_pair(Real, Imag));
1829 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001830 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001831 Builder.CreateStore(CI, V);
1832 return RValue::getAggregate(V);
1833 } else
1834 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001835
Daniel Dunbar11434922009-01-26 21:26:08 +00001836 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001837 // If we are ignoring an argument that had a result, make sure to
1838 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001839 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001840
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001841 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001842 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001843 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001844 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001845 if (RetTy->isAnyComplexType())
1846 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001847 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001848 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001849 else
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001850 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001851 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001852
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001853 case ABIArgInfo::Expand:
1854 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001855 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001856
1857 assert(0 && "Unhandled ABIArgInfo::Kind");
1858 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001859}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001860
1861/* VarArg handling */
1862
1863llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1864 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1865}