blob: 58e448949e70411d6bada75c7a5995f5a69d9aa9 [file] [log] [blame]
Daniel Dunbara8f02052008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar51a2d192009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Patel98bfe502008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbar90e43452009-03-02 04:32:35 +000025#include "llvm/Support/CallSite.h"
Daniel Dunbare09a9692009-01-24 08:32:22 +000026#include "llvm/Support/CommandLine.h"
Daniel Dunbar3cfcec72009-02-12 09:04:14 +000027#include "llvm/Support/MathExtras.h"
Daniel Dunbar9f4874e2009-02-04 23:24:38 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000030
31#include "ABIInfo.h"
32
Daniel Dunbara8f02052008-09-08 21:33:45 +000033using namespace clang;
34using namespace CodeGen;
35
36/***/
37
Daniel Dunbara8f02052008-09-08 21:33:45 +000038// FIXME: Use iterator and sidestep silly type array creation.
39
Daniel Dunbar34bda882009-02-02 23:23:47 +000040const
Douglas Gregor4fa58902009-02-26 23:50:07 +000041CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000042 return getFunctionInfo(FTNP->getResultType(),
43 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000044}
45
Daniel Dunbar34bda882009-02-02 23:23:47 +000046const
Douglas Gregor4fa58902009-02-26 23:50:07 +000047CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000048 llvm::SmallVector<QualType, 16> ArgTys;
49 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000050 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000051 ArgTys.push_back(FTP->getArgType(i));
52 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000053}
54
Daniel Dunbar34bda882009-02-02 23:23:47 +000055const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000056 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +000057 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar34bda882009-02-02 23:23:47 +000058 return getFunctionInfo(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +000059 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000060}
61
Daniel Dunbar34bda882009-02-02 23:23:47 +000062const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
63 llvm::SmallVector<QualType, 16> ArgTys;
64 ArgTys.push_back(MD->getSelfDecl()->getType());
65 ArgTys.push_back(Context.getObjCSelType());
66 // FIXME: Kill copy?
Chris Lattner9408eb12009-02-20 06:23:21 +000067 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbara8f02052008-09-08 21:33:45 +000068 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000069 ArgTys.push_back((*i)->getType());
70 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000071}
72
Daniel Dunbar34bda882009-02-02 23:23:47 +000073const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
74 const CallArgList &Args) {
75 // FIXME: Kill copy.
76 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000077 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
78 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000079 ArgTys.push_back(i->second);
80 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000081}
82
Daniel Dunbar34bda882009-02-02 23:23:47 +000083const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
84 const FunctionArgList &Args) {
85 // FIXME: Kill copy.
86 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +000087 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
88 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000089 ArgTys.push_back(i->second);
90 return getFunctionInfo(ResTy, ArgTys);
91}
92
93const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
94 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +000095 // Lookup or create unique function info.
96 llvm::FoldingSetNodeID ID;
97 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
98
99 void *InsertPos = 0;
100 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
101 if (FI)
102 return *FI;
103
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000104 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000105 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000106 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000107
108 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000109 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000110
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000111 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000112}
113
114/***/
115
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000116ABIInfo::~ABIInfo() {}
117
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000118void ABIArgInfo::dump() const {
119 fprintf(stderr, "(ABIArgInfo Kind=");
120 switch (TheKind) {
121 case Direct:
122 fprintf(stderr, "Direct");
123 break;
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000124 case Ignore:
125 fprintf(stderr, "Ignore");
126 break;
127 case Coerce:
128 fprintf(stderr, "Coerce Type=");
129 getCoerceToType()->print(llvm::errs());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000130 break;
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000131 case Indirect:
132 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar9f4874e2009-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 Dunbara7446422009-03-31 19:01:39 +0000143/// isEmptyRecord - Return true iff a structure has no non-empty
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000144/// members. Note that a structure with a flexible array member is not
145/// considered empty.
Daniel Dunbara7446422009-03-31 19:01:39 +0000146static bool isEmptyRecord(QualType T) {
147 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000148 if (!RT)
149 return 0;
150 const RecordDecl *RD = RT->getDecl();
151 if (RD->hasFlexibleArrayMember())
152 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000153 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000154 e = RD->field_end(); i != e; ++i) {
155 const FieldDecl *FD = *i;
Daniel Dunbara7446422009-03-31 19:01:39 +0000156 if (!isEmptyRecord(FD->getType()))
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000157 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 Gregor5d764842009-01-09 17:18:27 +0000180 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000181 e = RD->field_end(); i != e; ++i) {
182 const FieldDecl *FD = *i;
183 QualType FT = FD->getType();
184
Daniel Dunbara7446422009-03-31 19:01:39 +0000185 if (isEmptyRecord(FT)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000186 // 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 Gregor5d764842009-01-09 17:18:27 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-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 Dunbar9f052cb2009-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 Dunbar99eebc62008-09-17 21:22:33 +0000224 }
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000225
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000226 return true;
227}
228
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000229namespace {
230/// DefaultABIInfo - The default implementation for ABI specific
231/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000232/// self-consistent and sensible LLVM IR generation, but does not
233/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000234class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-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 Dunbarf98eeff2008-10-13 17:02:26 +0000240
Daniel Dunbar749e36b2009-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 Dunbar7fbcf9c2009-02-10 20:44:09 +0000247
248 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
249 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000250};
251
252/// X86_32ABIInfo - The X86-32 ABI information.
253class X86_32ABIInfo : public ABIInfo {
Eli Friedman5e175802009-03-23 23:26:24 +0000254 bool IsDarwin;
255
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000256public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000257 ABIArgInfo classifyReturnType(QualType RetTy,
258 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000259
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000260 ABIArgInfo classifyArgumentType(QualType RetTy,
261 ASTContext &Context) const;
262
263 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
264 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
265 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
266 it != ie; ++it)
267 it->info = classifyArgumentType(it->type, Context);
268 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000269
270 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
271 CodeGenFunction &CGF) const;
Eli Friedman5e175802009-03-23 23:26:24 +0000272
273 X86_32ABIInfo(bool d) : ABIInfo(), IsDarwin(d) {}
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000274};
275}
276
277ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
278 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000279 if (RetTy->isVoidType()) {
280 return ABIArgInfo::getIgnore();
Daniel Dunbar2a7bb3f2009-04-01 06:13:08 +0000281 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
282 // On Darwin, some vectors are returned in registers.
283 if (IsDarwin) {
284 uint64_t Size = Context.getTypeSize(RetTy);
285
286 // 128-bit vectors are a special case; they are returned in
287 // registers and we need to make sure to pick a type the LLVM
288 // backend will like.
289 if (Size == 128)
290 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
291 2));
292
293 // Always return in register if it fits in a general purpose
294 // register, or if it is 64 bits and has a single element.
295 if ((Size == 8 || Size == 16 || Size == 32) ||
296 (Size == 64 && VT->getNumElements() == 1))
297 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
298
299 return ABIArgInfo::getIndirect(0);
300 }
301
302 return ABIArgInfo::getDirect();
Daniel Dunbareec02622009-02-03 06:30:17 +0000303 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedman5e175802009-03-23 23:26:24 +0000304 // Outside of Darwin, structs and unions are always indirect.
305 if (!IsDarwin && !RetTy->isAnyComplexType())
306 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000307 // Classify "single element" structs as their element type.
308 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
309 if (SeltFD) {
310 QualType SeltTy = SeltFD->getType()->getDesugaredType();
311 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
312 // FIXME: This is gross, it would be nice if we could just
313 // pass back SeltTy and have clients deal with it. Is it worth
314 // supporting coerce to both LLVM and clang Types?
315 if (BT->isIntegerType()) {
316 uint64_t Size = Context.getTypeSize(SeltTy);
317 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
318 } else if (BT->getKind() == BuiltinType::Float) {
319 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
320 } else if (BT->getKind() == BuiltinType::Double) {
321 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
322 }
323 } else if (SeltTy->isPointerType()) {
324 // FIXME: It would be really nice if this could come out as
325 // the proper pointer type.
326 llvm::Type *PtrTy =
327 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
328 return ABIArgInfo::getCoerce(PtrTy);
329 }
330 }
331
Daniel Dunbar73d66602008-09-10 07:04:09 +0000332 uint64_t Size = Context.getTypeSize(RetTy);
333 if (Size == 8) {
334 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
335 } else if (Size == 16) {
336 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
337 } else if (Size == 32) {
338 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
339 } else if (Size == 64) {
340 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
341 } else {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000342 return ABIArgInfo::getIndirect(0);
Daniel Dunbar73d66602008-09-10 07:04:09 +0000343 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000344 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000345 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000346 }
347}
348
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000349ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000350 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000351 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000352 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000353 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000354 if (const RecordType *RT = Ty->getAsStructureType())
355 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000356 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000357
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000358 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000359 uint64_t Size = Context.getTypeSize(Ty);
360 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000361 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000362
363 // Expand structs with size <= 128-bits which consist only of
364 // basic types (int, long long, float, double, xxx*). This is
365 // non-recursive and does not ignore empty fields.
366 if (const RecordType *RT = Ty->getAsStructureType()) {
367 if (Context.getTypeSize(Ty) <= 4*32 &&
368 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
369 return ABIArgInfo::getExpand();
370 }
371
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000372 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000373 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000374 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000375 }
376}
377
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000378llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
379 CodeGenFunction &CGF) const {
380 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
381 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
382
383 CGBuilderTy &Builder = CGF.Builder;
384 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
385 "ap");
386 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
387 llvm::Type *PTy =
388 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
389 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
390
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000391 uint64_t Offset =
392 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000393 llvm::Value *NextAddr =
394 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000395 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000396 "ap.next");
397 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
398
399 return AddrTyped;
400}
401
Daniel Dunbare09a9692009-01-24 08:32:22 +0000402namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000403/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000404class X86_64ABIInfo : public ABIInfo {
405 enum Class {
406 Integer = 0,
407 SSE,
408 SSEUp,
409 X87,
410 X87Up,
411 ComplexX87,
412 NoClass,
413 Memory
414 };
415
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000416 /// merge - Implement the X86_64 ABI merging algorithm.
417 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000418 /// Merge an accumulating classification \arg Accum with a field
419 /// classification \arg Field.
420 ///
421 /// \param Accum - The accumulating classification. This should
422 /// always be either NoClass or the result of a previous merge
423 /// call. In addition, this should never be Memory (the caller
424 /// should just return Memory for the aggregate).
425 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000426
Daniel Dunbare09a9692009-01-24 08:32:22 +0000427 /// classify - Determine the x86_64 register classes in which the
428 /// given type T should be passed.
429 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000430 /// \param Lo - The classification for the parts of the type
431 /// residing in the low word of the containing object.
432 ///
433 /// \param Hi - The classification for the parts of the type
434 /// residing in the high word of the containing object.
435 ///
436 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000437 /// containing object. Some parameters are classified different
438 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000439 ///
440 /// If a word is unused its result will be NoClass; if a type should
441 /// be passed in Memory then at least the classification of \arg Lo
442 /// will be Memory.
443 ///
444 /// The \arg Lo class will be NoClass iff the argument is ignored.
445 ///
446 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar92e88642009-02-17 07:55:55 +0000447 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000448 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000449 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000450
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000451 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
452 /// to coerce to, chose the best way to pass Ty in the same place
453 /// that \arg CoerceTo would be passed, but while keeping the
454 /// emitted code as simple as possible.
455 ///
456 /// FIXME: Note, this should be cleaned up to just take an
457 /// enumeration of all the ways we might want to pass things,
458 /// instead of constructing an LLVM type. This makes this code more
459 /// explicit, and it makes it clearer that we are also doing this
460 /// for correctness in the case of passing scalar types.
461 ABIArgInfo getCoerceResult(QualType Ty,
462 const llvm::Type *CoerceTo,
463 ASTContext &Context) const;
464
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000465 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000466 ASTContext &Context) const;
467
468 ABIArgInfo classifyArgumentType(QualType Ty,
469 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000470 unsigned &neededInt,
471 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000472
473public:
474 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000475
476 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
477 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000478};
479}
480
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000481X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
482 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000483 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
484 // classified recursively so that always two fields are
485 // considered. The resulting class is calculated according to
486 // the classes of the fields in the eightbyte:
487 //
488 // (a) If both classes are equal, this is the resulting class.
489 //
490 // (b) If one of the classes is NO_CLASS, the resulting class is
491 // the other class.
492 //
493 // (c) If one of the classes is MEMORY, the result is the MEMORY
494 // class.
495 //
496 // (d) If one of the classes is INTEGER, the result is the
497 // INTEGER.
498 //
499 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
500 // MEMORY is used as class.
501 //
502 // (f) Otherwise class SSE is used.
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000503
504 // Accum should never be memory (we should have returned) or
505 // ComplexX87 (because this cannot be passed in a structure).
506 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000507 "Invalid accumulated classification during merge.");
508 if (Accum == Field || Field == NoClass)
509 return Accum;
510 else if (Field == Memory)
511 return Memory;
512 else if (Accum == NoClass)
513 return Field;
514 else if (Accum == Integer || Field == Integer)
515 return Integer;
516 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
517 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000518 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000519 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000520}
521
Daniel Dunbare09a9692009-01-24 08:32:22 +0000522void X86_64ABIInfo::classify(QualType Ty,
523 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000524 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000525 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000526 // FIXME: This code can be simplified by introducing a simple value
527 // class for Class pairs with appropriate constructor methods for
528 // the various situations.
529
Daniel Dunbard97f5952009-02-22 04:48:22 +0000530 // FIXME: Some of the split computations are wrong; unaligned
531 // vectors shouldn't be passed in registers for example, so there is
532 // no chance they can straddle an eightbyte. Verify & simplify.
533
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000534 Lo = Hi = NoClass;
535
536 Class &Current = OffsetBase < 64 ? Lo : Hi;
537 Current = Memory;
538
Daniel Dunbare09a9692009-01-24 08:32:22 +0000539 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
540 BuiltinType::Kind k = BT->getKind();
541
Daniel Dunbar1358b202009-01-26 21:26:08 +0000542 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000543 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000544 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000545 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000546 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000547 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000548 } else if (k == BuiltinType::LongDouble) {
549 Lo = X87;
550 Hi = X87Up;
551 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000552 // FIXME: _Decimal32 and _Decimal64 are SSE.
553 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000554 // FIXME: __int128 is (Integer, Integer).
Anders Carlsson1d234462009-02-26 17:31:15 +0000555 } else if (const EnumType *ET = Ty->getAsEnumType()) {
556 // Classify the underlying integer type.
557 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000558 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000559 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000560 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000561 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000562 if (Size == 32) {
563 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
564 // float> as integer.
565 Current = Integer;
566
567 // If this type crosses an eightbyte boundary, it should be
568 // split.
569 uint64_t EB_Real = (OffsetBase) / 64;
570 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
571 if (EB_Real != EB_Imag)
572 Hi = Lo;
573 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000574 // gcc passes <1 x double> in memory. :(
575 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000576 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000577
578 // gcc passes <1 x long long> as INTEGER.
579 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
580 Current = Integer;
581 else
582 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000583
584 // If this type crosses an eightbyte boundary, it should be
585 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000586 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000587 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000588 } else if (Size == 128) {
589 Lo = SSE;
590 Hi = SSEUp;
591 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000592 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000593 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000594
Daniel Dunbare413f532009-01-30 18:40:10 +0000595 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000596 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000597 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000598 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000599 else if (Size <= 128)
600 Lo = Hi = Integer;
601 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000602 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000603 else if (ET == Context.DoubleTy)
604 Lo = Hi = SSE;
605 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000606 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000607
608 // If this complex type crosses an eightbyte boundary then it
609 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000610 uint64_t EB_Real = (OffsetBase) / 64;
611 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000612 if (Hi == NoClass && EB_Real != EB_Imag)
613 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000614 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
615 // Arrays are treated like structures.
616
617 uint64_t Size = Context.getTypeSize(Ty);
618
619 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
620 // than two eightbytes, ..., it has class MEMORY.
621 if (Size > 128)
622 return;
623
624 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
625 // fields, it has class MEMORY.
626 //
627 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000628 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000629 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000630
631 // Otherwise implement simplified merge. We could be smarter about
632 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000633 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000634 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
635 uint64_t ArraySize = AT->getSize().getZExtValue();
636 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
637 Class FieldLo, FieldHi;
638 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000639 Lo = merge(Lo, FieldLo);
640 Hi = merge(Hi, FieldHi);
641 if (Lo == Memory || Hi == Memory)
642 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000643 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000644
645 // Do post merger cleanup (see below). Only case we worry about is Memory.
646 if (Hi == Memory)
647 Lo = Memory;
648 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000649 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000650 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000651
652 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
653 // than two eightbytes, ..., it has class MEMORY.
654 if (Size > 128)
655 return;
656
657 const RecordDecl *RD = RT->getDecl();
658
659 // Assume variable sized types are passed in memory.
660 if (RD->hasFlexibleArrayMember())
661 return;
662
663 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
664
665 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000666 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000667 unsigned idx = 0;
668 for (RecordDecl::field_iterator i = RD->field_begin(),
669 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000670 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000671 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000672
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000673 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
674 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000675 //
676 // Note, skip this test for bitfields, see below.
677 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000678 Lo = Memory;
679 return;
680 }
681
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000682 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000683 //
684 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
685 // exceeds a single eightbyte, each is classified
686 // separately. Each eightbyte gets initialized to class
687 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000688 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000689
690 // Bitfields require special handling, they do not force the
691 // structure to be passed in memory even if unaligned, and
692 // therefore they can straddle an eightbyte.
693 if (BitField) {
694 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
695 uint64_t Size =
696 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
697
698 uint64_t EB_Lo = Offset / 64;
699 uint64_t EB_Hi = (Offset + Size - 1) / 64;
700 FieldLo = FieldHi = NoClass;
701 if (EB_Lo) {
702 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
703 FieldLo = NoClass;
704 FieldHi = Integer;
705 } else {
706 FieldLo = Integer;
707 FieldHi = EB_Hi ? Integer : NoClass;
708 }
709 } else
710 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000711 Lo = merge(Lo, FieldLo);
712 Hi = merge(Hi, FieldHi);
713 if (Lo == Memory || Hi == Memory)
714 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000715 }
716
717 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
718 //
719 // (a) If one of the classes is MEMORY, the whole argument is
720 // passed in memory.
721 //
722 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
723
724 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000725 // the merge (just bail).
726 //
727 // The second condition occurs in the case of unions; for example
728 // union { _Complex double; unsigned; }.
729 if (Hi == Memory)
730 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000731 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000732 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000733 }
734}
735
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000736ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
737 const llvm::Type *CoerceTo,
738 ASTContext &Context) const {
739 if (CoerceTo == llvm::Type::Int64Ty) {
740 // Integer and pointer types will end up in a general purpose
741 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000742 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000743 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000744
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000745 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000746 // FIXME: It would probably be better to make CGFunctionInfo only
747 // map using canonical types than to canonize here.
748 QualType CTy = Context.getCanonicalType(Ty);
749
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000750 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000751 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000752 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000753
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000754 }
755
756 return ABIArgInfo::getCoerce(CoerceTo);
757}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000758
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000759ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
760 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000761 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
762 // classification algorithm.
763 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000764 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000765
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000766 // Check some invariants.
767 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
768 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
769 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
770
Daniel Dunbare09a9692009-01-24 08:32:22 +0000771 const llvm::Type *ResType = 0;
772 switch (Lo) {
773 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000774 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000775
776 case SSEUp:
777 case X87Up:
778 assert(0 && "Invalid classification for lo word.");
779
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000780 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000781 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000782 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000783 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000784
785 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
786 // available register of the sequence %rax, %rdx is used.
787 case Integer:
788 ResType = llvm::Type::Int64Ty; break;
789
790 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
791 // available SSE register of the sequence %xmm0, %xmm1 is used.
792 case SSE:
793 ResType = llvm::Type::DoubleTy; break;
794
795 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
796 // returned on the X87 stack in %st0 as 80-bit x87 number.
797 case X87:
798 ResType = llvm::Type::X86_FP80Ty; break;
799
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000800 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
801 // part of the value is returned in %st0 and the imaginary part in
802 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000803 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000804 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000805 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
806 llvm::Type::X86_FP80Ty,
807 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000808 break;
809 }
810
811 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000812 // Memory was handled previously and X87 should
813 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000814 case Memory:
815 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000816 assert(0 && "Invalid classification for hi word.");
817
Daniel Dunbar92e88642009-02-17 07:55:55 +0000818 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000819 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000820
Daniel Dunbare09a9692009-01-24 08:32:22 +0000821 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000822 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
823 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000824 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000825 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
826 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000827
828 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
829 // is passed in the upper half of the last used SSE register.
830 //
831 // SSEUP should always be preceeded by SSE, just widen.
832 case SSEUp:
833 assert(Lo == SSE && "Unexpected SSEUp classification.");
834 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
835 break;
836
837 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000838 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000839 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000840 // If X87Up is preceeded by X87, we don't need to do
841 // anything. However, in some cases with unions it may not be
842 // preceeded by X87. In such situations we follow gcc and pass the
843 // extra bits in an SSE reg.
844 if (Lo != X87)
845 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000846 break;
847 }
848
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000849 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000850}
851
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000852ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000853 unsigned &neededInt,
854 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000855 X86_64ABIInfo::Class Lo, Hi;
856 classify(Ty, Context, 0, Lo, Hi);
857
858 // Check some invariants.
859 // FIXME: Enforce these by construction.
860 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
861 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
862 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
863
Daniel Dunbare978cb92009-02-10 17:06:09 +0000864 neededInt = 0;
865 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000866 const llvm::Type *ResType = 0;
867 switch (Lo) {
868 case NoClass:
869 return ABIArgInfo::getIgnore();
870
871 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
872 // on the stack.
873 case Memory:
874
875 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
876 // COMPLEX_X87, it is passed in memory.
877 case X87:
878 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000879 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000880
881 case SSEUp:
882 case X87Up:
883 assert(0 && "Invalid classification for lo word.");
884
885 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
886 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
887 // and %r9 is used.
888 case Integer:
889 ++neededInt;
890 ResType = llvm::Type::Int64Ty;
891 break;
892
893 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
894 // available SSE register is used, the registers are taken in the
895 // order from %xmm0 to %xmm7.
896 case SSE:
897 ++neededSSE;
898 ResType = llvm::Type::DoubleTy;
899 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000900 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000901
902 switch (Hi) {
903 // Memory was handled previously, ComplexX87 and X87 should
904 // never occur as hi classes, and X87Up must be preceed by X87,
905 // which is passed in memory.
906 case Memory:
907 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000908 case ComplexX87:
909 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000910 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000911
912 case NoClass: break;
913 case Integer:
914 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
915 ++neededInt;
916 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000917
918 // X87Up generally doesn't occur here (long double is passed in
919 // memory), except in situations involving unions.
920 case X87Up:
921 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000922 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
923 ++neededSSE;
924 break;
925
926 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
927 // eightbyte is passed in the upper half of the last used SSE
928 // register.
929 case SSEUp:
930 assert(Lo == SSE && "Unexpected SSEUp classification.");
931 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
932 break;
933 }
934
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000935 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000936}
937
938void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
939 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
940
941 // Keep track of the number of assigned registers.
942 unsigned freeIntRegs = 6, freeSSERegs = 8;
943
944 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
945 // get assigned (in left-to-right order) for passing as follows...
946 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +0000947 it != ie; ++it) {
948 unsigned neededInt, neededSSE;
949 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
950
951 // AMD64-ABI 3.2.3p3: If there are no registers available for any
952 // eightbyte of an argument, the whole argument is passed on the
953 // stack. If registers have already been assigned for some
954 // eightbytes of such an argument, the assignments get reverted.
955 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
956 freeIntRegs -= neededInt;
957 freeSSERegs -= neededSSE;
958 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000959 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +0000960 }
961 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000962}
963
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000964static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
965 QualType Ty,
966 CodeGenFunction &CGF) {
967 llvm::Value *overflow_arg_area_p =
968 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
969 llvm::Value *overflow_arg_area =
970 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
971
972 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
973 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000974 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000975 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000976 // Note that we follow the ABI & gcc here, even though the type
977 // could in theory have an alignment greater than 16. This case
978 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000979
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +0000980 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
981 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
982 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
983 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
984 llvm::Type::Int64Ty);
985 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
986 overflow_arg_area =
987 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
988 overflow_arg_area->getType(),
989 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000990 }
991
992 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
993 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
994 llvm::Value *Res =
995 CGF.Builder.CreateBitCast(overflow_arg_area,
996 llvm::PointerType::getUnqual(LTy));
997
998 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
999 // l->overflow_arg_area + sizeof(type).
1000 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1001 // an 8 byte boundary.
1002
1003 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1004 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1005 (SizeInBytes + 7) & ~7);
1006 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1007 "overflow_arg_area.next");
1008 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1009
1010 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1011 return Res;
1012}
1013
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001014llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1015 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001016 // Assume that va_list type is correct; should be pointer to LLVM type:
1017 // struct {
1018 // i32 gp_offset;
1019 // i32 fp_offset;
1020 // i8* overflow_arg_area;
1021 // i8* reg_save_area;
1022 // };
1023 unsigned neededInt, neededSSE;
1024 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1025 neededInt, neededSSE);
1026
1027 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1028 // in the registers. If not go to step 7.
1029 if (!neededInt && !neededSSE)
1030 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1031
1032 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1033 // general purpose registers needed to pass type and num_fp to hold
1034 // the number of floating point registers needed.
1035
1036 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1037 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1038 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1039 //
1040 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1041 // register save space).
1042
1043 llvm::Value *InRegs = 0;
1044 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1045 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1046 if (neededInt) {
1047 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1048 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1049 InRegs =
1050 CGF.Builder.CreateICmpULE(gp_offset,
1051 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1052 48 - neededInt * 8),
1053 "fits_in_gp");
1054 }
1055
1056 if (neededSSE) {
1057 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1058 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1059 llvm::Value *FitsInFP =
1060 CGF.Builder.CreateICmpULE(fp_offset,
1061 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001062 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001063 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001064 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001065 }
1066
1067 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1068 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1069 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1070 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1071
1072 // Emit code to load the value if it was passed in registers.
1073
1074 CGF.EmitBlock(InRegBlock);
1075
1076 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1077 // an offset of l->gp_offset and/or l->fp_offset. This may require
1078 // copying to a temporary location in case the parameter is passed
1079 // in different register classes or requires an alignment greater
1080 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001081 //
1082 // FIXME: This really results in shameful code when we end up
1083 // needing to collect arguments from different places; often what
1084 // should result in a simple assembling of a structure from
1085 // scattered addresses has many more loads than necessary. Can we
1086 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001087 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1088 llvm::Value *RegAddr =
1089 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1090 "reg_save_area");
1091 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001092 // FIXME: Cleanup.
1093 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1094 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1095 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1096 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1097 const llvm::Type *TyLo = ST->getElementType(0);
1098 const llvm::Type *TyHi = ST->getElementType(1);
1099 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1100 "Unexpected ABI info for mixed regs");
1101 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1102 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1103 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1104 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1105 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1106 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1107 llvm::Value *V =
1108 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1109 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1110 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1111 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1112
1113 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001114 } else if (neededInt) {
1115 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1116 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1117 llvm::PointerType::getUnqual(LTy));
1118 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001119 if (neededSSE == 1) {
1120 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1121 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1122 llvm::PointerType::getUnqual(LTy));
1123 } else {
1124 assert(neededSSE == 2 && "Invalid number of needed registers!");
1125 // SSE registers are spaced 16 bytes apart in the register save
1126 // area, we need to collect the two eightbytes together.
1127 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1128 llvm::Value *RegAddrHi =
1129 CGF.Builder.CreateGEP(RegAddrLo,
1130 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1131 const llvm::Type *DblPtrTy =
1132 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1133 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1134 llvm::Type::DoubleTy,
1135 NULL);
1136 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1137 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1138 DblPtrTy));
1139 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1140 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1141 DblPtrTy));
1142 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1143 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1144 llvm::PointerType::getUnqual(LTy));
1145 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001146 }
1147
1148 // AMD64-ABI 3.5.7p5: Step 5. Set:
1149 // l->gp_offset = l->gp_offset + num_gp * 8
1150 // l->fp_offset = l->fp_offset + num_fp * 16.
1151 if (neededInt) {
1152 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1153 neededInt * 8);
1154 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1155 gp_offset_p);
1156 }
1157 if (neededSSE) {
1158 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1159 neededSSE * 16);
1160 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1161 fp_offset_p);
1162 }
1163 CGF.EmitBranch(ContBlock);
1164
1165 // Emit code to load the value if it was passed in memory.
1166
1167 CGF.EmitBlock(InMemBlock);
1168 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1169
1170 // Return the appropriate result.
1171
1172 CGF.EmitBlock(ContBlock);
1173 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1174 "vaarg.addr");
1175 ResAddr->reserveOperandSpace(2);
1176 ResAddr->addIncoming(RegAddr, InRegBlock);
1177 ResAddr->addIncoming(MemAddr, InMemBlock);
1178
1179 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001180}
1181
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001182class ARMABIInfo : public ABIInfo {
1183 ABIArgInfo classifyReturnType(QualType RetTy,
1184 ASTContext &Context) const;
1185
1186 ABIArgInfo classifyArgumentType(QualType RetTy,
1187 ASTContext &Context) const;
1188
1189 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1190
1191 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1192 CodeGenFunction &CGF) const;
1193};
1194
1195void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1196 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1197 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1198 it != ie; ++it) {
1199 it->info = classifyArgumentType(it->type, Context);
1200 }
1201}
1202
1203ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1204 ASTContext &Context) const {
1205 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1206 return ABIArgInfo::getDirect();
1207 }
1208 // FIXME: This is kind of nasty... but there isn't much choice
1209 // because the ARM backend doesn't support byval.
1210 // FIXME: This doesn't handle alignment > 64 bits.
1211 const llvm::Type* ElemTy;
1212 unsigned SizeRegs;
1213 if (Context.getTypeAlign(Ty) > 32) {
1214 ElemTy = llvm::Type::Int64Ty;
1215 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1216 } else {
1217 ElemTy = llvm::Type::Int32Ty;
1218 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1219 }
1220 std::vector<const llvm::Type*> LLVMFields;
1221 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1222 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1223 return ABIArgInfo::getCoerce(STy);
1224}
1225
1226ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1227 ASTContext &Context) const {
1228 if (RetTy->isVoidType()) {
1229 return ABIArgInfo::getIgnore();
1230 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1231 // Aggregates <= 4 bytes are returned in r0; other aggregates
1232 // are returned indirectly.
1233 uint64_t Size = Context.getTypeSize(RetTy);
1234 if (Size <= 32)
1235 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1236 return ABIArgInfo::getIndirect(0);
1237 } else {
1238 return ABIArgInfo::getDirect();
1239 }
1240}
1241
1242llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1243 CodeGenFunction &CGF) const {
1244 // FIXME: Need to handle alignment
1245 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1246 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1247
1248 CGBuilderTy &Builder = CGF.Builder;
1249 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1250 "ap");
1251 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1252 llvm::Type *PTy =
1253 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1254 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1255
1256 uint64_t Offset =
1257 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1258 llvm::Value *NextAddr =
1259 Builder.CreateGEP(Addr,
1260 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1261 "ap.next");
1262 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1263
1264 return AddrTyped;
1265}
1266
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001267ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001268 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001269 if (RetTy->isVoidType()) {
1270 return ABIArgInfo::getIgnore();
1271 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001272 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001273 } else {
1274 return ABIArgInfo::getDirect();
1275 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001276}
1277
1278ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001279 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001280 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001281 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001282 } else {
1283 return ABIArgInfo::getDirect();
1284 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001285}
1286
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001287llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1288 CodeGenFunction &CGF) const {
1289 return 0;
1290}
1291
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001292const ABIInfo &CodeGenTypes::getABIInfo() const {
1293 if (TheABIInfo)
1294 return *TheABIInfo;
1295
1296 // For now we just cache this in the CodeGenTypes and don't bother
1297 // to free it.
1298 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1299 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman5e175802009-03-23 23:26:24 +00001300 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001301 switch (getContext().Target.getPointerWidth(0)) {
1302 case 32:
Eli Friedman5e175802009-03-23 23:26:24 +00001303 return *(TheABIInfo = new X86_32ABIInfo(IsDarwin));
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001304 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001305 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001306 }
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001307 } else if (strcmp(TargetPrefix, "arm") == 0) {
1308 // FIXME: Support for OABI?
1309 return *(TheABIInfo = new ARMABIInfo());
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001310 }
1311
1312 return *(TheABIInfo = new DefaultABIInfo);
1313}
1314
Daniel Dunbare126ab12008-09-10 02:41:04 +00001315/***/
1316
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001317CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1318 const llvm::SmallVector<QualType, 16> &ArgTys) {
1319 NumArgs = ArgTys.size();
1320 Args = new ArgInfo[1 + NumArgs];
1321 Args[0].type = ResTy;
1322 for (unsigned i = 0; i < NumArgs; ++i)
1323 Args[1 + i].type = ArgTys[i];
1324}
1325
1326/***/
1327
Daniel Dunbar04d35782008-09-17 00:51:38 +00001328void CodeGenTypes::GetExpandedTypes(QualType Ty,
1329 std::vector<const llvm::Type*> &ArgTys) {
1330 const RecordType *RT = Ty->getAsStructureType();
1331 assert(RT && "Can only expand structure types.");
1332 const RecordDecl *RD = RT->getDecl();
1333 assert(!RD->hasFlexibleArrayMember() &&
1334 "Cannot expand structure with flexible array.");
1335
Douglas Gregor5d764842009-01-09 17:18:27 +00001336 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001337 e = RD->field_end(); i != e; ++i) {
1338 const FieldDecl *FD = *i;
1339 assert(!FD->isBitField() &&
1340 "Cannot expand structure with bit-field members.");
1341
1342 QualType FT = FD->getType();
1343 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1344 GetExpandedTypes(FT, ArgTys);
1345 } else {
1346 ArgTys.push_back(ConvertType(FT));
1347 }
1348 }
1349}
1350
1351llvm::Function::arg_iterator
1352CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1353 llvm::Function::arg_iterator AI) {
1354 const RecordType *RT = Ty->getAsStructureType();
1355 assert(RT && "Can only expand structure types.");
1356
1357 RecordDecl *RD = RT->getDecl();
1358 assert(LV.isSimple() &&
1359 "Unexpected non-simple lvalue during struct expansion.");
1360 llvm::Value *Addr = LV.getAddress();
1361 for (RecordDecl::field_iterator i = RD->field_begin(),
1362 e = RD->field_end(); i != e; ++i) {
1363 FieldDecl *FD = *i;
1364 QualType FT = FD->getType();
1365
1366 // FIXME: What are the right qualifiers here?
1367 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1368 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1369 AI = ExpandTypeFromArgs(FT, LV, AI);
1370 } else {
1371 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1372 ++AI;
1373 }
1374 }
1375
1376 return AI;
1377}
1378
1379void
1380CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1381 llvm::SmallVector<llvm::Value*, 16> &Args) {
1382 const RecordType *RT = Ty->getAsStructureType();
1383 assert(RT && "Can only expand structure types.");
1384
1385 RecordDecl *RD = RT->getDecl();
1386 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1387 llvm::Value *Addr = RV.getAggregateAddr();
1388 for (RecordDecl::field_iterator i = RD->field_begin(),
1389 e = RD->field_end(); i != e; ++i) {
1390 FieldDecl *FD = *i;
1391 QualType FT = FD->getType();
1392
1393 // FIXME: What are the right qualifiers here?
1394 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1395 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1396 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1397 } else {
1398 RValue RV = EmitLoadOfLValue(LV, FT);
1399 assert(RV.isScalar() &&
1400 "Unexpected non-scalar rvalue during struct expansion.");
1401 Args.push_back(RV.getScalarVal());
1402 }
1403 }
1404}
1405
Daniel Dunbar84379912009-02-02 19:06:38 +00001406/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1407/// a pointer to an object of type \arg Ty.
1408///
1409/// This safely handles the case when the src type is smaller than the
1410/// destination type; in this situation the values of bits which not
1411/// present in the src are undefined.
1412static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1413 const llvm::Type *Ty,
1414 CodeGenFunction &CGF) {
1415 const llvm::Type *SrcTy =
1416 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1417 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1418 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1419
Daniel Dunbar77071992009-02-03 05:59:18 +00001420 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001421 if (SrcSize == DstSize) {
1422 llvm::Value *Casted =
1423 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001424 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1425 // FIXME: Use better alignment / avoid requiring aligned load.
1426 Load->setAlignment(1);
1427 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001428 } else {
1429 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1430
1431 // Otherwise do coercion through memory. This is stupid, but
1432 // simple.
1433 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1434 llvm::Value *Casted =
1435 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001436 llvm::StoreInst *Store =
1437 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1438 // FIXME: Use better alignment / avoid requiring aligned store.
1439 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001440 return CGF.Builder.CreateLoad(Tmp);
1441 }
1442}
1443
1444/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1445/// where the source and destination may have different types.
1446///
1447/// This safely handles the case when the src type is larger than the
1448/// destination type; the upper bits of the src will be lost.
1449static void CreateCoercedStore(llvm::Value *Src,
1450 llvm::Value *DstPtr,
1451 CodeGenFunction &CGF) {
1452 const llvm::Type *SrcTy = Src->getType();
1453 const llvm::Type *DstTy =
1454 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1455
1456 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1457 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1458
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001459 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001460 if (SrcSize == DstSize) {
1461 llvm::Value *Casted =
1462 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001463 // FIXME: Use better alignment / avoid requiring aligned store.
1464 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001465 } else {
1466 assert(SrcSize > DstSize && "Coercion is missing bits!");
1467
1468 // Otherwise do coercion through memory. This is stupid, but
1469 // simple.
1470 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1471 CGF.Builder.CreateStore(Src, Tmp);
1472 llvm::Value *Casted =
1473 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001474 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1475 // FIXME: Use better alignment / avoid requiring aligned load.
1476 Load->setAlignment(1);
1477 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001478 }
1479}
1480
Daniel Dunbar04d35782008-09-17 00:51:38 +00001481/***/
1482
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001483bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001484 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001485}
1486
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001487const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001488CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001489 std::vector<const llvm::Type*> ArgTys;
1490
1491 const llvm::Type *ResultType = 0;
1492
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001493 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001494 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001495 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001496 case ABIArgInfo::Expand:
1497 assert(0 && "Invalid ABI kind for return argument");
1498
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001499 case ABIArgInfo::Direct:
1500 ResultType = ConvertType(RetTy);
1501 break;
1502
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001503 case ABIArgInfo::Indirect: {
1504 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001505 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001506 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001507 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1508 break;
1509 }
1510
Daniel Dunbar1358b202009-01-26 21:26:08 +00001511 case ABIArgInfo::Ignore:
1512 ResultType = llvm::Type::VoidTy;
1513 break;
1514
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001515 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001516 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001517 break;
1518 }
1519
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001520 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1521 ie = FI.arg_end(); it != ie; ++it) {
1522 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001523
1524 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001525 case ABIArgInfo::Ignore:
1526 break;
1527
Daniel Dunbar04d35782008-09-17 00:51:38 +00001528 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001529 ArgTys.push_back(AI.getCoerceToType());
1530 break;
1531
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001532 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001533 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001534 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1535 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001536 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001537 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001538
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001539 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001540 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001541 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001542
1543 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001544 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001545 break;
1546 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001547 }
1548
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001549 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001550}
1551
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001552void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001553 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001554 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001555 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001556 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001557
1558 if (TargetDecl) {
1559 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001560 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001561 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001562 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001563 if (TargetDecl->getAttr<PureAttr>())
1564 FuncAttrs |= llvm::Attribute::ReadOnly;
1565 if (TargetDecl->getAttr<ConstAttr>())
1566 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001567 }
1568
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001569 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001570 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001571 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001572 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001573 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001574 if (RetTy->isPromotableIntegerType()) {
1575 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001576 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001577 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001578 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001579 }
1580 }
1581 break;
1582
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001583 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001584 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001585 llvm::Attribute::StructRet |
1586 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001587 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001588 // sret disables readnone and readonly
1589 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1590 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001591 break;
1592
Daniel Dunbar1358b202009-01-26 21:26:08 +00001593 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001594 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001595 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001596
Daniel Dunbar22e30052008-09-11 01:48:57 +00001597 case ABIArgInfo::Expand:
1598 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001599 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001600
Devang Patel2bb6eb82008-09-26 22:53:57 +00001601 if (RetAttrs)
1602 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001603 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1604 ie = FI.arg_end(); it != ie; ++it) {
1605 QualType ParamType = it->type;
1606 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001607 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001608
1609 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001610 case ABIArgInfo::Coerce:
1611 break;
1612
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001613 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001614 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarb3f651a2009-02-05 01:31:19 +00001615 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001616 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001617 // byval disables readnone and readonly.
1618 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1619 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001620 break;
1621
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001622 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001623 if (ParamType->isPromotableIntegerType()) {
1624 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001625 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001626 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001627 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001628 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001629 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001630 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001631
Daniel Dunbar1358b202009-01-26 21:26:08 +00001632 case ABIArgInfo::Ignore:
1633 // Skip increment, no matching LLVM parameter.
1634 continue;
1635
Daniel Dunbar04d35782008-09-17 00:51:38 +00001636 case ABIArgInfo::Expand: {
1637 std::vector<const llvm::Type*> Tys;
1638 // FIXME: This is rather inefficient. Do we ever actually need
1639 // to do anything here? The result should be just reconstructed
1640 // on the other side, so extension should be a non-issue.
1641 getTypes().GetExpandedTypes(ParamType, Tys);
1642 Index += Tys.size();
1643 continue;
1644 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001645 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001646
Devang Patela85a9ef2008-09-25 21:02:23 +00001647 if (Attributes)
1648 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001649 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001650 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001651 if (FuncAttrs)
1652 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001653}
1654
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001655void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1656 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001657 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001658 // FIXME: We no longer need the types from FunctionArgList; lift up
1659 // and simplify.
1660
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001661 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1662 llvm::Function::arg_iterator AI = Fn->arg_begin();
1663
1664 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001665 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001666 AI->setName("agg.result");
1667 ++AI;
1668 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001669
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001670 assert(FI.arg_size() == Args.size() &&
1671 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001672 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001673 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001674 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001675 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001676 QualType Ty = info_it->type;
1677 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001678
1679 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001680 case ABIArgInfo::Indirect: {
1681 llvm::Value* V = AI;
1682 if (hasAggregateLLVMType(Ty)) {
1683 // Do nothing, aggregates and complex variables are accessed by
1684 // reference.
1685 } else {
1686 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001687 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001688 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1689 // This must be a promotion, for something like
1690 // "void a(x) short x; {..."
1691 V = EmitScalarConversion(V, Ty, Arg->getType());
1692 }
1693 }
1694 EmitParmDecl(*Arg, V);
1695 break;
1696 }
1697
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001698 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001699 assert(AI != Fn->arg_end() && "Argument mismatch!");
1700 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001701 if (hasAggregateLLVMType(Ty)) {
1702 // Create a temporary alloca to hold the argument; the rest of
1703 // codegen expects to access aggregates & complex values by
1704 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001705 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001706 Builder.CreateStore(AI, V);
1707 } else {
1708 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1709 // This must be a promotion, for something like
1710 // "void a(x) short x; {..."
1711 V = EmitScalarConversion(V, Ty, Arg->getType());
1712 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001713 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001714 EmitParmDecl(*Arg, V);
1715 break;
1716 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001717
1718 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001719 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001720 // we need to create a temporary and reconstruct it from the
1721 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001722 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001723 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001724 (Name + ".addr").c_str());
1725 // FIXME: What are the right qualifiers here?
1726 llvm::Function::arg_iterator End =
1727 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1728 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001729
Daniel Dunbar04d35782008-09-17 00:51:38 +00001730 // Name the arguments used in expansion and increment AI.
1731 unsigned Index = 0;
1732 for (; AI != End; ++AI, ++Index)
1733 AI->setName(Name + "." + llvm::utostr(Index));
1734 continue;
1735 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001736
1737 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001738 // Initialize the local variable appropriately.
1739 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001740 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001741 } else {
1742 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1743 }
1744
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001745 // Skip increment, no matching LLVM parameter.
1746 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001747
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001748 case ABIArgInfo::Coerce: {
1749 assert(AI != Fn->arg_end() && "Argument mismatch!");
1750 // FIXME: This is very wasteful; EmitParmDecl is just going to
1751 // drop the result in a new alloca anyway, so we could just
1752 // store into that directly if we broke the abstraction down
1753 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001754 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001755 CreateCoercedStore(AI, V, *this);
1756 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001757 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001758 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001759 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1760 // This must be a promotion, for something like
1761 // "void a(x) short x; {..."
1762 V = EmitScalarConversion(V, Ty, Arg->getType());
1763 }
1764 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001765 EmitParmDecl(*Arg, V);
1766 break;
1767 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001768 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001769
1770 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001771 }
1772 assert(AI == Fn->arg_end() && "Argument mismatch!");
1773}
1774
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001775void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001776 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001777 llvm::Value *RV = 0;
1778
1779 // Functions with no result always return void.
1780 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001781 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001782 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001783
1784 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001785 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001786 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001787 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1788 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1789 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1790 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1791 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001792 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1793 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001794 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001795 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001796
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001797 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001798 // The internal return value temp always will have
1799 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001800 RV = Builder.CreateLoad(ReturnValue);
1801 break;
1802
Daniel Dunbar1358b202009-01-26 21:26:08 +00001803 case ABIArgInfo::Ignore:
1804 break;
1805
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001806 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001807 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001808 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001809
Daniel Dunbar22e30052008-09-11 01:48:57 +00001810 case ABIArgInfo::Expand:
1811 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001812 }
1813 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001814
1815 if (RV) {
1816 Builder.CreateRet(RV);
1817 } else {
1818 Builder.CreateRetVoid();
1819 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001820}
1821
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001822RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1823 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001824 const CallArgList &CallArgs,
1825 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001826 // FIXME: We no longer need the types from CallArgs; lift up and
1827 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001828 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001829
1830 // Handle struct-return functions by passing a pointer to the
1831 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001832 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001833 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001834 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001835 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001836 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001837 }
1838
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001839 assert(CallInfo.arg_size() == CallArgs.size() &&
1840 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001841 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001842 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001843 I != E; ++I, ++info_it) {
1844 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001845 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001846
1847 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001848 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001849 if (RV.isScalar() || RV.isComplex()) {
1850 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001851 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001852 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001853 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001854 else
1855 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1856 } else {
1857 Args.push_back(RV.getAggregateAddr());
1858 }
1859 break;
1860
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001861 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001862 if (RV.isScalar()) {
1863 Args.push_back(RV.getScalarVal());
1864 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001865 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1866 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1867 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1868 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00001869 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001870 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001871 }
1872 break;
1873
Daniel Dunbar1358b202009-01-26 21:26:08 +00001874 case ABIArgInfo::Ignore:
1875 break;
1876
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001877 case ABIArgInfo::Coerce: {
1878 // FIXME: Avoid the conversion through memory if possible.
1879 llvm::Value *SrcPtr;
1880 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001881 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001882 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001883 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001884 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001885 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1886 } else
1887 SrcPtr = RV.getAggregateAddr();
1888 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1889 *this));
1890 break;
1891 }
1892
Daniel Dunbar04d35782008-09-17 00:51:38 +00001893 case ABIArgInfo::Expand:
1894 ExpandTypeToArgs(I->second, RV, Args);
1895 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001896 }
1897 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001898
Daniel Dunbar0a067402009-02-23 17:26:39 +00001899 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00001900 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001901 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00001902 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1903 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001904
Daniel Dunbar90e43452009-03-02 04:32:35 +00001905 llvm::CallSite CS;
1906 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
1907 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00001908 } else {
1909 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00001910 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1911 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00001912 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00001913 }
1914
Daniel Dunbar90e43452009-03-02 04:32:35 +00001915 CS.setAttributes(Attrs);
1916 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1917 CS.setCallingConv(F->getCallingConv());
1918
1919 // If the call doesn't return, finish the basic block and clear the
1920 // insertion point; this allows the rest of IRgen to discard
1921 // unreachable code.
1922 if (CS.doesNotReturn()) {
1923 Builder.CreateUnreachable();
1924 Builder.ClearInsertionPoint();
1925
1926 // FIXME: For now, emit a dummy basic block because expr
1927 // emitters in generally are not ready to handle emitting
1928 // expressions at unreachable points.
1929 EnsureInsertPoint();
1930
1931 // Return a reasonable RValue.
1932 return GetUndefRValue(RetTy);
1933 }
1934
1935 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +00001936 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001937 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001938
1939 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001940 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001941 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001942 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +00001943 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001944 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +00001945 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001946
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001947 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001948 if (RetTy->isAnyComplexType()) {
1949 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1950 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1951 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +00001952 }
1953 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001954 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00001955 Builder.CreateStore(CI, V);
1956 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00001957 }
1958 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001959
Daniel Dunbar1358b202009-01-26 21:26:08 +00001960 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00001961 // If we are ignoring an argument that had a result, make sure to
1962 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00001963 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001964
Daniel Dunbar73d66602008-09-10 07:04:09 +00001965 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001966 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001967 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001968 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001969 if (RetTy->isAnyComplexType())
1970 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +00001971 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001972 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00001973 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001974 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001975
Daniel Dunbar22e30052008-09-11 01:48:57 +00001976 case ABIArgInfo::Expand:
1977 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001978 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001979
1980 assert(0 && "Unhandled ABIArgInfo::Kind");
1981 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001982}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001983
1984/* VarArg handling */
1985
1986llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1987 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1988}