blob: 6f7bea2340a9e661e24ffb00b31be6af20fff09c [file] [log] [blame]
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001//===---- TargetABIInfo.cpp - Encapsulate target ABI 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 "ABIInfo.h"
16#include "CodeGenFunction.h"
17#include "clang/AST/RecordLayout.h"
18#include "llvm/Type.h"
19
20using namespace clang;
21using namespace CodeGen;
22
23ABIInfo::~ABIInfo() {}
24
25void ABIArgInfo::dump() const {
26 fprintf(stderr, "(ABIArgInfo Kind=");
27 switch (TheKind) {
28 case Direct:
29 fprintf(stderr, "Direct");
30 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000031 case Extend:
32 fprintf(stderr, "Extend");
33 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000034 case Ignore:
35 fprintf(stderr, "Ignore");
36 break;
37 case Coerce:
38 fprintf(stderr, "Coerce Type=");
39 getCoerceToType()->print(llvm::errs());
40 break;
41 case Indirect:
42 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
43 break;
44 case Expand:
45 fprintf(stderr, "Expand");
46 break;
47 }
48 fprintf(stderr, ")\n");
49}
50
51static bool isEmptyRecord(ASTContext &Context, QualType T);
52
53/// isEmptyField - Return true iff a the field is "empty", that is it
54/// is an unnamed bit-field or an (array of) empty record(s).
55static bool isEmptyField(ASTContext &Context, const FieldDecl *FD) {
56 if (FD->isUnnamedBitfield())
57 return true;
58
59 QualType FT = FD->getType();
60 // Constant arrays of empty records count as empty, strip them off.
61 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
62 FT = AT->getElementType();
63
64 return isEmptyRecord(Context, FT);
65}
66
67/// isEmptyRecord - Return true iff a structure contains only empty
68/// fields. Note that a structure with a flexible array member is not
69/// considered empty.
70static bool isEmptyRecord(ASTContext &Context, QualType T) {
71 const RecordType *RT = T->getAsRecordType();
72 if (!RT)
73 return 0;
74 const RecordDecl *RD = RT->getDecl();
75 if (RD->hasFlexibleArrayMember())
76 return false;
77 for (RecordDecl::field_iterator i = RD->field_begin(Context),
78 e = RD->field_end(Context); i != e; ++i)
79 if (!isEmptyField(Context, *i))
80 return false;
81 return true;
82}
83
84/// isSingleElementStruct - Determine if a structure is a "single
85/// element struct", i.e. it has exactly one non-empty field or
86/// exactly one field which is itself a single element
87/// struct. Structures with flexible array members are never
88/// considered single element structs.
89///
90/// \return The field declaration for the single non-empty field, if
91/// it exists.
92static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
93 const RecordType *RT = T->getAsStructureType();
94 if (!RT)
95 return 0;
96
97 const RecordDecl *RD = RT->getDecl();
98 if (RD->hasFlexibleArrayMember())
99 return 0;
100
101 const Type *Found = 0;
102 for (RecordDecl::field_iterator i = RD->field_begin(Context),
103 e = RD->field_end(Context); i != e; ++i) {
104 const FieldDecl *FD = *i;
105 QualType FT = FD->getType();
106
107 // Ignore empty fields.
108 if (isEmptyField(Context, FD))
109 continue;
110
111 // If we already found an element then this isn't a single-element
112 // struct.
113 if (Found)
114 return 0;
115
116 // Treat single element arrays as the element.
117 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
118 if (AT->getSize().getZExtValue() != 1)
119 break;
120 FT = AT->getElementType();
121 }
122
123 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
124 Found = FT.getTypePtr();
125 } else {
126 Found = isSingleElementStruct(FT, Context);
127 if (!Found)
128 return 0;
129 }
130 }
131
132 return Found;
133}
134
135static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
136 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
137 return false;
138
139 uint64_t Size = Context.getTypeSize(Ty);
140 return Size == 32 || Size == 64;
141}
142
143static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
144 ASTContext &Context) {
145 for (RecordDecl::field_iterator i = RD->field_begin(Context),
146 e = RD->field_end(Context); i != e; ++i) {
147 const FieldDecl *FD = *i;
148
149 if (!is32Or64BitBasicType(FD->getType(), Context))
150 return false;
151
152 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
153 // how to expand them yet, and the predicate for telling if a bitfield still
154 // counts as "basic" is more complicated than what we were doing previously.
155 if (FD->isBitField())
156 return false;
157 }
158
159 return true;
160}
161
162namespace {
163/// DefaultABIInfo - The default implementation for ABI specific
164/// details. This implementation provides information which results in
165/// self-consistent and sensible LLVM IR generation, but does not
166/// conform to any particular ABI.
167class DefaultABIInfo : public ABIInfo {
168 ABIArgInfo classifyReturnType(QualType RetTy,
169 ASTContext &Context) const;
170
171 ABIArgInfo classifyArgumentType(QualType RetTy,
172 ASTContext &Context) const;
173
174 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
175 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
176 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
177 it != ie; ++it)
178 it->info = classifyArgumentType(it->type, Context);
179 }
180
181 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
182 CodeGenFunction &CGF) const;
183};
184
185/// X86_32ABIInfo - The X86-32 ABI information.
186class X86_32ABIInfo : public ABIInfo {
187 ASTContext &Context;
188 bool IsDarwin;
189
190 static bool isRegisterSize(unsigned Size) {
191 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
192 }
193
194 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
195
196public:
197 ABIArgInfo classifyReturnType(QualType RetTy,
198 ASTContext &Context) const;
199
200 ABIArgInfo classifyArgumentType(QualType RetTy,
201 ASTContext &Context) const;
202
203 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
204 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
205 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
206 it != ie; ++it)
207 it->info = classifyArgumentType(it->type, Context);
208 }
209
210 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
211 CodeGenFunction &CGF) const;
212
213 X86_32ABIInfo(ASTContext &Context, bool d)
214 : ABIInfo(), Context(Context), IsDarwin(d) {}
215};
216}
217
218
219/// shouldReturnTypeInRegister - Determine if the given type should be
220/// passed in a register (for the Darwin ABI).
221bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
222 ASTContext &Context) {
223 uint64_t Size = Context.getTypeSize(Ty);
224
225 // Type must be register sized.
226 if (!isRegisterSize(Size))
227 return false;
228
229 if (Ty->isVectorType()) {
230 // 64- and 128- bit vectors inside structures are not returned in
231 // registers.
232 if (Size == 64 || Size == 128)
233 return false;
234
235 return true;
236 }
237
238 // If this is a builtin, pointer, or complex type, it is ok.
239 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
240 return true;
241
242 // Arrays are treated like records.
243 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
244 return shouldReturnTypeInRegister(AT->getElementType(), Context);
245
246 // Otherwise, it must be a record type.
247 const RecordType *RT = Ty->getAsRecordType();
248 if (!RT) return false;
249
250 // Structure types are passed in register if all fields would be
251 // passed in a register.
252 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context),
253 e = RT->getDecl()->field_end(Context); i != e; ++i) {
254 const FieldDecl *FD = *i;
255
256 // Empty fields are ignored.
257 if (isEmptyField(Context, FD))
258 continue;
259
260 // Check fields recursively.
261 if (!shouldReturnTypeInRegister(FD->getType(), Context))
262 return false;
263 }
264
265 return true;
266}
267
268ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
269 ASTContext &Context) const {
270 if (RetTy->isVoidType()) {
271 return ABIArgInfo::getIgnore();
272 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
273 // On Darwin, some vectors are returned in registers.
274 if (IsDarwin) {
275 uint64_t Size = Context.getTypeSize(RetTy);
276
277 // 128-bit vectors are a special case; they are returned in
278 // registers and we need to make sure to pick a type the LLVM
279 // backend will like.
280 if (Size == 128)
281 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
282 2));
283
284 // Always return in register if it fits in a general purpose
285 // register, or if it is 64 bits and has a single element.
286 if ((Size == 8 || Size == 16 || Size == 32) ||
287 (Size == 64 && VT->getNumElements() == 1))
288 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
289
290 return ABIArgInfo::getIndirect(0);
291 }
292
293 return ABIArgInfo::getDirect();
294 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
295 // Structures with flexible arrays are always indirect.
296 if (const RecordType *RT = RetTy->getAsStructureType())
297 if (RT->getDecl()->hasFlexibleArrayMember())
298 return ABIArgInfo::getIndirect(0);
299
300 // Outside of Darwin, structs and unions are always indirect.
301 if (!IsDarwin && !RetTy->isAnyComplexType())
302 return ABIArgInfo::getIndirect(0);
303
304 // Classify "single element" structs as their element type.
305 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
306 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
307 if (BT->isIntegerType()) {
308 // We need to use the size of the structure, padding
309 // bit-fields can adjust that to be larger than the single
310 // element type.
311 uint64_t Size = Context.getTypeSize(RetTy);
312 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
313 } else if (BT->getKind() == BuiltinType::Float) {
314 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
315 "Unexpect single element structure size!");
316 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
317 } else if (BT->getKind() == BuiltinType::Double) {
318 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
319 "Unexpect single element structure size!");
320 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
321 }
322 } else if (SeltTy->isPointerType()) {
323 // FIXME: It would be really nice if this could come out as the proper
324 // pointer type.
325 llvm::Type *PtrTy =
326 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
327 return ABIArgInfo::getCoerce(PtrTy);
328 } else if (SeltTy->isVectorType()) {
329 // 64- and 128-bit vectors are never returned in a
330 // register when inside a structure.
331 uint64_t Size = Context.getTypeSize(RetTy);
332 if (Size == 64 || Size == 128)
333 return ABIArgInfo::getIndirect(0);
334
335 return classifyReturnType(QualType(SeltTy, 0), Context);
336 }
337 }
338
339 // Small structures which are register sized are generally returned
340 // in a register.
341 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
342 uint64_t Size = Context.getTypeSize(RetTy);
343 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
344 }
345
346 return ABIArgInfo::getIndirect(0);
347 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000348 return (RetTy->isPromotableIntegerType() ?
349 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000350 }
351}
352
353ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
354 ASTContext &Context) const {
355 // FIXME: Set alignment on indirect arguments.
356 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
357 // Structures with flexible arrays are always indirect.
358 if (const RecordType *RT = Ty->getAsStructureType())
359 if (RT->getDecl()->hasFlexibleArrayMember())
360 return ABIArgInfo::getIndirect(0);
361
362 // Ignore empty structs.
363 uint64_t Size = Context.getTypeSize(Ty);
364 if (Ty->isStructureType() && Size == 0)
365 return ABIArgInfo::getIgnore();
366
367 // Expand structs with size <= 128-bits which consist only of
368 // basic types (int, long long, float, double, xxx*). This is
369 // non-recursive and does not ignore empty fields.
370 if (const RecordType *RT = Ty->getAsStructureType()) {
371 if (Context.getTypeSize(Ty) <= 4*32 &&
372 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
373 return ABIArgInfo::getExpand();
374 }
375
376 return ABIArgInfo::getIndirect(0);
377 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000378 return (Ty->isPromotableIntegerType() ?
379 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000380 }
381}
382
383llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
384 CodeGenFunction &CGF) const {
385 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
386 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
387
388 CGBuilderTy &Builder = CGF.Builder;
389 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
390 "ap");
391 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
392 llvm::Type *PTy =
393 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
394 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
395
396 uint64_t Offset =
397 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
398 llvm::Value *NextAddr =
399 Builder.CreateGEP(Addr,
400 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
401 "ap.next");
402 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
403
404 return AddrTyped;
405}
406
407namespace {
408/// X86_64ABIInfo - The X86_64 ABI information.
409class X86_64ABIInfo : public ABIInfo {
410 enum Class {
411 Integer = 0,
412 SSE,
413 SSEUp,
414 X87,
415 X87Up,
416 ComplexX87,
417 NoClass,
418 Memory
419 };
420
421 /// merge - Implement the X86_64 ABI merging algorithm.
422 ///
423 /// Merge an accumulating classification \arg Accum with a field
424 /// classification \arg Field.
425 ///
426 /// \param Accum - The accumulating classification. This should
427 /// always be either NoClass or the result of a previous merge
428 /// call. In addition, this should never be Memory (the caller
429 /// should just return Memory for the aggregate).
430 Class merge(Class Accum, Class Field) const;
431
432 /// classify - Determine the x86_64 register classes in which the
433 /// given type T should be passed.
434 ///
435 /// \param Lo - The classification for the parts of the type
436 /// residing in the low word of the containing object.
437 ///
438 /// \param Hi - The classification for the parts of the type
439 /// residing in the high word of the containing object.
440 ///
441 /// \param OffsetBase - The bit offset of this type in the
442 /// containing object. Some parameters are classified different
443 /// depending on whether they straddle an eightbyte boundary.
444 ///
445 /// If a word is unused its result will be NoClass; if a type should
446 /// be passed in Memory then at least the classification of \arg Lo
447 /// will be Memory.
448 ///
449 /// The \arg Lo class will be NoClass iff the argument is ignored.
450 ///
451 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
452 /// also be ComplexX87.
453 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
454 Class &Lo, Class &Hi) const;
455
456 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
457 /// to coerce to, chose the best way to pass Ty in the same place
458 /// that \arg CoerceTo would be passed, but while keeping the
459 /// emitted code as simple as possible.
460 ///
461 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
462 /// the ways we might want to pass things, instead of constructing an LLVM
463 /// type. This makes this code more explicit, and it makes it clearer that we
464 /// are also doing this for correctness in the case of passing scalar types.
465 ABIArgInfo getCoerceResult(QualType Ty,
466 const llvm::Type *CoerceTo,
467 ASTContext &Context) const;
468
469 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
470 /// such that the argument will be passed in memory.
471 ABIArgInfo getIndirectResult(QualType Ty,
472 ASTContext &Context) const;
473
474 ABIArgInfo classifyReturnType(QualType RetTy,
475 ASTContext &Context) const;
476
477 ABIArgInfo classifyArgumentType(QualType Ty,
478 ASTContext &Context,
479 unsigned &neededInt,
480 unsigned &neededSSE) const;
481
482public:
483 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
484
485 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
486 CodeGenFunction &CGF) const;
487};
488}
489
490X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
491 Class Field) const {
492 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
493 // classified recursively so that always two fields are
494 // considered. The resulting class is calculated according to
495 // the classes of the fields in the eightbyte:
496 //
497 // (a) If both classes are equal, this is the resulting class.
498 //
499 // (b) If one of the classes is NO_CLASS, the resulting class is
500 // the other class.
501 //
502 // (c) If one of the classes is MEMORY, the result is the MEMORY
503 // class.
504 //
505 // (d) If one of the classes is INTEGER, the result is the
506 // INTEGER.
507 //
508 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
509 // MEMORY is used as class.
510 //
511 // (f) Otherwise class SSE is used.
512
513 // Accum should never be memory (we should have returned) or
514 // ComplexX87 (because this cannot be passed in a structure).
515 assert((Accum != Memory && Accum != ComplexX87) &&
516 "Invalid accumulated classification during merge.");
517 if (Accum == Field || Field == NoClass)
518 return Accum;
519 else if (Field == Memory)
520 return Memory;
521 else if (Accum == NoClass)
522 return Field;
523 else if (Accum == Integer || Field == Integer)
524 return Integer;
525 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
526 Accum == X87 || Accum == X87Up)
527 return Memory;
528 else
529 return SSE;
530}
531
532void X86_64ABIInfo::classify(QualType Ty,
533 ASTContext &Context,
534 uint64_t OffsetBase,
535 Class &Lo, Class &Hi) const {
536 // FIXME: This code can be simplified by introducing a simple value class for
537 // Class pairs with appropriate constructor methods for the various
538 // situations.
539
540 // FIXME: Some of the split computations are wrong; unaligned vectors
541 // shouldn't be passed in registers for example, so there is no chance they
542 // can straddle an eightbyte. Verify & simplify.
543
544 Lo = Hi = NoClass;
545
546 Class &Current = OffsetBase < 64 ? Lo : Hi;
547 Current = Memory;
548
549 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
550 BuiltinType::Kind k = BT->getKind();
551
552 if (k == BuiltinType::Void) {
553 Current = NoClass;
554 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
555 Lo = Integer;
556 Hi = Integer;
557 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
558 Current = Integer;
559 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
560 Current = SSE;
561 } else if (k == BuiltinType::LongDouble) {
562 Lo = X87;
563 Hi = X87Up;
564 }
565 // FIXME: _Decimal32 and _Decimal64 are SSE.
566 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
567 } else if (const EnumType *ET = Ty->getAsEnumType()) {
568 // Classify the underlying integer type.
569 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
570 } else if (Ty->hasPointerRepresentation()) {
571 Current = Integer;
572 } else if (const VectorType *VT = Ty->getAsVectorType()) {
573 uint64_t Size = Context.getTypeSize(VT);
574 if (Size == 32) {
575 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
576 // float> as integer.
577 Current = Integer;
578
579 // If this type crosses an eightbyte boundary, it should be
580 // split.
581 uint64_t EB_Real = (OffsetBase) / 64;
582 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
583 if (EB_Real != EB_Imag)
584 Hi = Lo;
585 } else if (Size == 64) {
586 // gcc passes <1 x double> in memory. :(
587 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
588 return;
589
590 // gcc passes <1 x long long> as INTEGER.
591 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
592 Current = Integer;
593 else
594 Current = SSE;
595
596 // If this type crosses an eightbyte boundary, it should be
597 // split.
598 if (OffsetBase && OffsetBase != 64)
599 Hi = Lo;
600 } else if (Size == 128) {
601 Lo = SSE;
602 Hi = SSEUp;
603 }
604 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
605 QualType ET = Context.getCanonicalType(CT->getElementType());
606
607 uint64_t Size = Context.getTypeSize(Ty);
608 if (ET->isIntegralType()) {
609 if (Size <= 64)
610 Current = Integer;
611 else if (Size <= 128)
612 Lo = Hi = Integer;
613 } else if (ET == Context.FloatTy)
614 Current = SSE;
615 else if (ET == Context.DoubleTy)
616 Lo = Hi = SSE;
617 else if (ET == Context.LongDoubleTy)
618 Current = ComplexX87;
619
620 // If this complex type crosses an eightbyte boundary then it
621 // should be split.
622 uint64_t EB_Real = (OffsetBase) / 64;
623 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
624 if (Hi == NoClass && EB_Real != EB_Imag)
625 Hi = Lo;
626 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
627 // Arrays are treated like structures.
628
629 uint64_t Size = Context.getTypeSize(Ty);
630
631 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
632 // than two eightbytes, ..., it has class MEMORY.
633 if (Size > 128)
634 return;
635
636 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
637 // fields, it has class MEMORY.
638 //
639 // Only need to check alignment of array base.
640 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
641 return;
642
643 // Otherwise implement simplified merge. We could be smarter about
644 // this, but it isn't worth it and would be harder to verify.
645 Current = NoClass;
646 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
647 uint64_t ArraySize = AT->getSize().getZExtValue();
648 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
649 Class FieldLo, FieldHi;
650 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
651 Lo = merge(Lo, FieldLo);
652 Hi = merge(Hi, FieldHi);
653 if (Lo == Memory || Hi == Memory)
654 break;
655 }
656
657 // Do post merger cleanup (see below). Only case we worry about is Memory.
658 if (Hi == Memory)
659 Lo = Memory;
660 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
661 } else if (const RecordType *RT = Ty->getAsRecordType()) {
662 uint64_t Size = Context.getTypeSize(Ty);
663
664 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
665 // than two eightbytes, ..., it has class MEMORY.
666 if (Size > 128)
667 return;
668
669 const RecordDecl *RD = RT->getDecl();
670
671 // Assume variable sized types are passed in memory.
672 if (RD->hasFlexibleArrayMember())
673 return;
674
675 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
676
677 // Reset Lo class, this will be recomputed.
678 Current = NoClass;
679 unsigned idx = 0;
680 for (RecordDecl::field_iterator i = RD->field_begin(Context),
681 e = RD->field_end(Context); i != e; ++i, ++idx) {
682 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
683 bool BitField = i->isBitField();
684
685 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
686 // fields, it has class MEMORY.
687 //
688 // Note, skip this test for bit-fields, see below.
689 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
690 Lo = Memory;
691 return;
692 }
693
694 // Classify this field.
695 //
696 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
697 // exceeds a single eightbyte, each is classified
698 // separately. Each eightbyte gets initialized to class
699 // NO_CLASS.
700 Class FieldLo, FieldHi;
701
702 // Bit-fields require special handling, they do not force the
703 // structure to be passed in memory even if unaligned, and
704 // therefore they can straddle an eightbyte.
705 if (BitField) {
706 // Ignore padding bit-fields.
707 if (i->isUnnamedBitfield())
708 continue;
709
710 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
711 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
712
713 uint64_t EB_Lo = Offset / 64;
714 uint64_t EB_Hi = (Offset + Size - 1) / 64;
715 FieldLo = FieldHi = NoClass;
716 if (EB_Lo) {
717 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
718 FieldLo = NoClass;
719 FieldHi = Integer;
720 } else {
721 FieldLo = Integer;
722 FieldHi = EB_Hi ? Integer : NoClass;
723 }
724 } else
725 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
726 Lo = merge(Lo, FieldLo);
727 Hi = merge(Hi, FieldHi);
728 if (Lo == Memory || Hi == Memory)
729 break;
730 }
731
732 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
733 //
734 // (a) If one of the classes is MEMORY, the whole argument is
735 // passed in memory.
736 //
737 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
738
739 // The first of these conditions is guaranteed by how we implement
740 // the merge (just bail).
741 //
742 // The second condition occurs in the case of unions; for example
743 // union { _Complex double; unsigned; }.
744 if (Hi == Memory)
745 Lo = Memory;
746 if (Hi == SSEUp && Lo != SSE)
747 Hi = SSE;
748 }
749}
750
751ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
752 const llvm::Type *CoerceTo,
753 ASTContext &Context) const {
754 if (CoerceTo == llvm::Type::Int64Ty) {
755 // Integer and pointer types will end up in a general purpose
756 // register.
757 if (Ty->isIntegralType() || Ty->isPointerType())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000758 return (Ty->isPromotableIntegerType() ?
759 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000760 } else if (CoerceTo == llvm::Type::DoubleTy) {
761 // FIXME: It would probably be better to make CGFunctionInfo only map using
762 // canonical types than to canonize here.
763 QualType CTy = Context.getCanonicalType(Ty);
764
765 // Float and double end up in a single SSE reg.
766 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
767 return ABIArgInfo::getDirect();
768
769 }
770
771 return ABIArgInfo::getCoerce(CoerceTo);
772}
773
774ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
775 ASTContext &Context) const {
776 // If this is a scalar LLVM value then assume LLVM will pass it in the right
777 // place naturally.
778 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000779 return (Ty->isPromotableIntegerType() ?
780 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000781
782 // FIXME: Set alignment correctly.
783 return ABIArgInfo::getIndirect(0);
784}
785
786ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
787 ASTContext &Context) const {
788 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
789 // classification algorithm.
790 X86_64ABIInfo::Class Lo, Hi;
791 classify(RetTy, Context, 0, Lo, Hi);
792
793 // Check some invariants.
794 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
795 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
796 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
797
798 const llvm::Type *ResType = 0;
799 switch (Lo) {
800 case NoClass:
801 return ABIArgInfo::getIgnore();
802
803 case SSEUp:
804 case X87Up:
805 assert(0 && "Invalid classification for lo word.");
806
807 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
808 // hidden argument.
809 case Memory:
810 return getIndirectResult(RetTy, Context);
811
812 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
813 // available register of the sequence %rax, %rdx is used.
814 case Integer:
815 ResType = llvm::Type::Int64Ty; break;
816
817 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
818 // available SSE register of the sequence %xmm0, %xmm1 is used.
819 case SSE:
820 ResType = llvm::Type::DoubleTy; break;
821
822 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
823 // returned on the X87 stack in %st0 as 80-bit x87 number.
824 case X87:
825 ResType = llvm::Type::X86_FP80Ty; break;
826
827 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
828 // part of the value is returned in %st0 and the imaginary part in
829 // %st1.
830 case ComplexX87:
831 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
832 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
833 llvm::Type::X86_FP80Ty,
834 NULL);
835 break;
836 }
837
838 switch (Hi) {
839 // Memory was handled previously and X87 should
840 // never occur as a hi class.
841 case Memory:
842 case X87:
843 assert(0 && "Invalid classification for hi word.");
844
845 case ComplexX87: // Previously handled.
846 case NoClass: break;
847
848 case Integer:
849 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
850 break;
851 case SSE:
852 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
853 break;
854
855 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
856 // is passed in the upper half of the last used SSE register.
857 //
858 // SSEUP should always be preceeded by SSE, just widen.
859 case SSEUp:
860 assert(Lo == SSE && "Unexpected SSEUp classification.");
861 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
862 break;
863
864 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
865 // returned together with the previous X87 value in %st0.
866 case X87Up:
867 // If X87Up is preceeded by X87, we don't need to do
868 // anything. However, in some cases with unions it may not be
869 // preceeded by X87. In such situations we follow gcc and pass the
870 // extra bits in an SSE reg.
871 if (Lo != X87)
872 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
873 break;
874 }
875
876 return getCoerceResult(RetTy, ResType, Context);
877}
878
879ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
880 unsigned &neededInt,
881 unsigned &neededSSE) const {
882 X86_64ABIInfo::Class Lo, Hi;
883 classify(Ty, Context, 0, Lo, Hi);
884
885 // Check some invariants.
886 // FIXME: Enforce these by construction.
887 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
888 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
889 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
890
891 neededInt = 0;
892 neededSSE = 0;
893 const llvm::Type *ResType = 0;
894 switch (Lo) {
895 case NoClass:
896 return ABIArgInfo::getIgnore();
897
898 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
899 // on the stack.
900 case Memory:
901
902 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
903 // COMPLEX_X87, it is passed in memory.
904 case X87:
905 case ComplexX87:
906 return getIndirectResult(Ty, Context);
907
908 case SSEUp:
909 case X87Up:
910 assert(0 && "Invalid classification for lo word.");
911
912 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
913 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
914 // and %r9 is used.
915 case Integer:
916 ++neededInt;
917 ResType = llvm::Type::Int64Ty;
918 break;
919
920 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
921 // available SSE register is used, the registers are taken in the
922 // order from %xmm0 to %xmm7.
923 case SSE:
924 ++neededSSE;
925 ResType = llvm::Type::DoubleTy;
926 break;
927 }
928
929 switch (Hi) {
930 // Memory was handled previously, ComplexX87 and X87 should
931 // never occur as hi classes, and X87Up must be preceed by X87,
932 // which is passed in memory.
933 case Memory:
934 case X87:
935 case ComplexX87:
936 assert(0 && "Invalid classification for hi word.");
937 break;
938
939 case NoClass: break;
940 case Integer:
941 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
942 ++neededInt;
943 break;
944
945 // X87Up generally doesn't occur here (long double is passed in
946 // memory), except in situations involving unions.
947 case X87Up:
948 case SSE:
949 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
950 ++neededSSE;
951 break;
952
953 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
954 // eightbyte is passed in the upper half of the last used SSE
955 // register.
956 case SSEUp:
957 assert(Lo == SSE && "Unexpected SSEUp classification.");
958 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
959 break;
960 }
961
962 return getCoerceResult(Ty, ResType, Context);
963}
964
965void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
966 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
967
968 // Keep track of the number of assigned registers.
969 unsigned freeIntRegs = 6, freeSSERegs = 8;
970
971 // If the return value is indirect, then the hidden argument is consuming one
972 // integer register.
973 if (FI.getReturnInfo().isIndirect())
974 --freeIntRegs;
975
976 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
977 // get assigned (in left-to-right order) for passing as follows...
978 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
979 it != ie; ++it) {
980 unsigned neededInt, neededSSE;
981 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
982
983 // AMD64-ABI 3.2.3p3: If there are no registers available for any
984 // eightbyte of an argument, the whole argument is passed on the
985 // stack. If registers have already been assigned for some
986 // eightbytes of such an argument, the assignments get reverted.
987 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
988 freeIntRegs -= neededInt;
989 freeSSERegs -= neededSSE;
990 } else {
991 it->info = getIndirectResult(it->type, Context);
992 }
993 }
994}
995
996static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
997 QualType Ty,
998 CodeGenFunction &CGF) {
999 llvm::Value *overflow_arg_area_p =
1000 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1001 llvm::Value *overflow_arg_area =
1002 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1003
1004 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1005 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1006 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1007 if (Align > 8) {
1008 // Note that we follow the ABI & gcc here, even though the type
1009 // could in theory have an alignment greater than 16. This case
1010 // shouldn't ever matter in practice.
1011
1012 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1013 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1014 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1015 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1016 llvm::Type::Int64Ty);
1017 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1018 overflow_arg_area =
1019 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1020 overflow_arg_area->getType(),
1021 "overflow_arg_area.align");
1022 }
1023
1024 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1025 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1026 llvm::Value *Res =
1027 CGF.Builder.CreateBitCast(overflow_arg_area,
1028 llvm::PointerType::getUnqual(LTy));
1029
1030 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1031 // l->overflow_arg_area + sizeof(type).
1032 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1033 // an 8 byte boundary.
1034
1035 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1036 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1037 (SizeInBytes + 7) & ~7);
1038 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1039 "overflow_arg_area.next");
1040 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1041
1042 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1043 return Res;
1044}
1045
1046llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1047 CodeGenFunction &CGF) const {
1048 // Assume that va_list type is correct; should be pointer to LLVM type:
1049 // struct {
1050 // i32 gp_offset;
1051 // i32 fp_offset;
1052 // i8* overflow_arg_area;
1053 // i8* reg_save_area;
1054 // };
1055 unsigned neededInt, neededSSE;
1056 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1057 neededInt, neededSSE);
1058
1059 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1060 // in the registers. If not go to step 7.
1061 if (!neededInt && !neededSSE)
1062 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1063
1064 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1065 // general purpose registers needed to pass type and num_fp to hold
1066 // the number of floating point registers needed.
1067
1068 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1069 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1070 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1071 //
1072 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1073 // register save space).
1074
1075 llvm::Value *InRegs = 0;
1076 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1077 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1078 if (neededInt) {
1079 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1080 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1081 InRegs =
1082 CGF.Builder.CreateICmpULE(gp_offset,
1083 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1084 48 - neededInt * 8),
1085 "fits_in_gp");
1086 }
1087
1088 if (neededSSE) {
1089 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1090 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1091 llvm::Value *FitsInFP =
1092 CGF.Builder.CreateICmpULE(fp_offset,
1093 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1094 176 - neededSSE * 16),
1095 "fits_in_fp");
1096 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1097 }
1098
1099 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1100 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1101 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1102 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1103
1104 // Emit code to load the value if it was passed in registers.
1105
1106 CGF.EmitBlock(InRegBlock);
1107
1108 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1109 // an offset of l->gp_offset and/or l->fp_offset. This may require
1110 // copying to a temporary location in case the parameter is passed
1111 // in different register classes or requires an alignment greater
1112 // than 8 for general purpose registers and 16 for XMM registers.
1113 //
1114 // FIXME: This really results in shameful code when we end up needing to
1115 // collect arguments from different places; often what should result in a
1116 // simple assembling of a structure from scattered addresses has many more
1117 // loads than necessary. Can we clean this up?
1118 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1119 llvm::Value *RegAddr =
1120 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1121 "reg_save_area");
1122 if (neededInt && neededSSE) {
1123 // FIXME: Cleanup.
1124 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1125 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1126 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1127 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1128 const llvm::Type *TyLo = ST->getElementType(0);
1129 const llvm::Type *TyHi = ST->getElementType(1);
1130 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1131 "Unexpected ABI info for mixed regs");
1132 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1133 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1134 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1135 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1136 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1137 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1138 llvm::Value *V =
1139 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1140 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1141 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1142 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1143
1144 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
1145 } else if (neededInt) {
1146 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1147 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1148 llvm::PointerType::getUnqual(LTy));
1149 } else {
1150 if (neededSSE == 1) {
1151 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1152 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1153 llvm::PointerType::getUnqual(LTy));
1154 } else {
1155 assert(neededSSE == 2 && "Invalid number of needed registers!");
1156 // SSE registers are spaced 16 bytes apart in the register save
1157 // area, we need to collect the two eightbytes together.
1158 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1159 llvm::Value *RegAddrHi =
1160 CGF.Builder.CreateGEP(RegAddrLo,
1161 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1162 const llvm::Type *DblPtrTy =
1163 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1164 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1165 llvm::Type::DoubleTy,
1166 NULL);
1167 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1168 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1169 DblPtrTy));
1170 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1171 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1172 DblPtrTy));
1173 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1174 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1175 llvm::PointerType::getUnqual(LTy));
1176 }
1177 }
1178
1179 // AMD64-ABI 3.5.7p5: Step 5. Set:
1180 // l->gp_offset = l->gp_offset + num_gp * 8
1181 // l->fp_offset = l->fp_offset + num_fp * 16.
1182 if (neededInt) {
1183 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1184 neededInt * 8);
1185 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1186 gp_offset_p);
1187 }
1188 if (neededSSE) {
1189 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1190 neededSSE * 16);
1191 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1192 fp_offset_p);
1193 }
1194 CGF.EmitBranch(ContBlock);
1195
1196 // Emit code to load the value if it was passed in memory.
1197
1198 CGF.EmitBlock(InMemBlock);
1199 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1200
1201 // Return the appropriate result.
1202
1203 CGF.EmitBlock(ContBlock);
1204 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1205 "vaarg.addr");
1206 ResAddr->reserveOperandSpace(2);
1207 ResAddr->addIncoming(RegAddr, InRegBlock);
1208 ResAddr->addIncoming(MemAddr, InMemBlock);
1209
1210 return ResAddr;
1211}
1212
1213// ABI Info for PIC16
1214class PIC16ABIInfo : public ABIInfo {
1215 ABIArgInfo classifyReturnType(QualType RetTy,
1216 ASTContext &Context) const;
1217
1218 ABIArgInfo classifyArgumentType(QualType RetTy,
1219 ASTContext &Context) const;
1220
1221 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1222 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1223 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1224 it != ie; ++it)
1225 it->info = classifyArgumentType(it->type, Context);
1226 }
1227
1228 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1229 CodeGenFunction &CGF) const;
1230
1231};
1232
1233ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1234 ASTContext &Context) const {
1235 if (RetTy->isVoidType()) {
1236 return ABIArgInfo::getIgnore();
1237 } else {
1238 return ABIArgInfo::getDirect();
1239 }
1240}
1241
1242ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1243 ASTContext &Context) const {
1244 return ABIArgInfo::getDirect();
1245}
1246
1247llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1248 CodeGenFunction &CGF) const {
1249 return 0;
1250}
1251
1252class ARMABIInfo : public ABIInfo {
1253 ABIArgInfo classifyReturnType(QualType RetTy,
1254 ASTContext &Context) const;
1255
1256 ABIArgInfo classifyArgumentType(QualType RetTy,
1257 ASTContext &Context) const;
1258
1259 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1260
1261 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1262 CodeGenFunction &CGF) const;
1263};
1264
1265void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1266 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1267 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1268 it != ie; ++it) {
1269 it->info = classifyArgumentType(it->type, Context);
1270 }
1271}
1272
1273ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1274 ASTContext &Context) const {
1275 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001276 return (Ty->isPromotableIntegerType() ?
1277 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001278 }
1279 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1280 // backend doesn't support byval.
1281 // FIXME: This doesn't handle alignment > 64 bits.
1282 const llvm::Type* ElemTy;
1283 unsigned SizeRegs;
1284 if (Context.getTypeAlign(Ty) > 32) {
1285 ElemTy = llvm::Type::Int64Ty;
1286 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1287 } else {
1288 ElemTy = llvm::Type::Int32Ty;
1289 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1290 }
1291 std::vector<const llvm::Type*> LLVMFields;
1292 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1293 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1294 return ABIArgInfo::getCoerce(STy);
1295}
1296
1297ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1298 ASTContext &Context) const {
1299 if (RetTy->isVoidType()) {
1300 return ABIArgInfo::getIgnore();
1301 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1302 // Aggregates <= 4 bytes are returned in r0; other aggregates
1303 // are returned indirectly.
1304 uint64_t Size = Context.getTypeSize(RetTy);
1305 if (Size <= 32)
1306 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1307 return ABIArgInfo::getIndirect(0);
1308 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001309 return (RetTy->isPromotableIntegerType() ?
1310 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001311 }
1312}
1313
1314llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1315 CodeGenFunction &CGF) const {
1316 // FIXME: Need to handle alignment
1317 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1318 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1319
1320 CGBuilderTy &Builder = CGF.Builder;
1321 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1322 "ap");
1323 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1324 llvm::Type *PTy =
1325 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1326 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1327
1328 uint64_t Offset =
1329 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1330 llvm::Value *NextAddr =
1331 Builder.CreateGEP(Addr,
1332 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1333 "ap.next");
1334 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1335
1336 return AddrTyped;
1337}
1338
1339ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
1340 ASTContext &Context) const {
1341 if (RetTy->isVoidType()) {
1342 return ABIArgInfo::getIgnore();
1343 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1344 return ABIArgInfo::getIndirect(0);
1345 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001346 return (RetTy->isPromotableIntegerType() ?
1347 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001348 }
1349}
1350
1351ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
1352 ASTContext &Context) const {
1353 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1354 return ABIArgInfo::getIndirect(0);
1355 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001356 return (Ty->isPromotableIntegerType() ?
1357 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001358 }
1359}
1360
1361llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1362 CodeGenFunction &CGF) const {
1363 return 0;
1364}
1365
1366const ABIInfo &CodeGenTypes::getABIInfo() const {
1367 if (TheABIInfo)
1368 return *TheABIInfo;
1369
1370 // For now we just cache this in the CodeGenTypes and don't bother
1371 // to free it.
1372 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1373 if (strcmp(TargetPrefix, "x86") == 0) {
1374 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
1375 switch (getContext().Target.getPointerWidth(0)) {
1376 case 32:
1377 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
1378 case 64:
1379 return *(TheABIInfo = new X86_64ABIInfo());
1380 }
1381 } else if (strcmp(TargetPrefix, "arm") == 0) {
1382 // FIXME: Support for OABI?
1383 return *(TheABIInfo = new ARMABIInfo());
1384 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1385 return *(TheABIInfo = new PIC16ABIInfo());
1386 }
1387
1388 return *(TheABIInfo = new DefaultABIInfo);
1389}