Add include needed for MSVC.
[clang/acc.git] / lib / CodeGen / TargetABIInfo.cpp
blob328f0ae3d2d3b866432acf680f18bb58299fd8e3
1 //===---- 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.
13 //===----------------------------------------------------------------------===//
15 #include "ABIInfo.h"
16 #include "CodeGenFunction.h"
17 #include "clang/AST/RecordLayout.h"
18 #include "llvm/Type.h"
20 using namespace clang;
21 using namespace CodeGen;
23 ABIInfo::~ABIInfo() {}
25 void ABIArgInfo::dump() const {
26 fprintf(stderr, "(ABIArgInfo Kind=");
27 switch (TheKind) {
28 case Direct:
29 fprintf(stderr, "Direct");
30 break;
31 case Extend:
32 fprintf(stderr, "Extend");
33 break;
34 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;
48 fprintf(stderr, ")\n");
51 static bool isEmptyRecord(ASTContext &Context, QualType T);
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).
55 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD) {
56 if (FD->isUnnamedBitfield())
57 return true;
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();
64 return isEmptyRecord(Context, FT);
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.
70 static 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(), e = RD->field_end();
78 i != e; ++i)
79 if (!isEmptyField(Context, *i))
80 return false;
81 return true;
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.
92 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
93 const RecordType *RT = T->getAsStructureType();
94 if (!RT)
95 return 0;
97 const RecordDecl *RD = RT->getDecl();
98 if (RD->hasFlexibleArrayMember())
99 return 0;
101 const Type *Found = 0;
102 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
103 i != e; ++i) {
104 const FieldDecl *FD = *i;
105 QualType FT = FD->getType();
107 // Ignore empty fields.
108 if (isEmptyField(Context, FD))
109 continue;
111 // If we already found an element then this isn't a single-element
112 // struct.
113 if (Found)
114 return 0;
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();
123 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
124 Found = FT.getTypePtr();
125 } else {
126 Found = isSingleElementStruct(FT, Context);
127 if (!Found)
128 return 0;
132 return Found;
135 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
136 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
137 return false;
139 uint64_t Size = Context.getTypeSize(Ty);
140 return Size == 32 || Size == 64;
143 static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
144 ASTContext &Context) {
145 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
146 i != e; ++i) {
147 const FieldDecl *FD = *i;
149 if (!is32Or64BitBasicType(FD->getType(), Context))
150 return false;
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;
159 return true;
162 static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
163 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
164 i != e; ++i) {
165 const FieldDecl *FD = *i;
167 if (FD->getType()->isVectorType() &&
168 Context.getTypeSize(FD->getType()) >= 128)
169 return true;
171 if (const RecordType* RT = FD->getType()->getAsRecordType())
172 if (typeContainsSSEVector(RT->getDecl(), Context))
173 return true;
176 return false;
179 namespace {
180 /// DefaultABIInfo - The default implementation for ABI specific
181 /// details. This implementation provides information which results in
182 /// self-consistent and sensible LLVM IR generation, but does not
183 /// conform to any particular ABI.
184 class DefaultABIInfo : public ABIInfo {
185 ABIArgInfo classifyReturnType(QualType RetTy,
186 ASTContext &Context,
187 llvm::LLVMContext &VMContext) const;
189 ABIArgInfo classifyArgumentType(QualType RetTy,
190 ASTContext &Context,
191 llvm::LLVMContext &VMContext) const;
193 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
194 llvm::LLVMContext &VMContext) const {
195 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
196 VMContext);
197 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
198 it != ie; ++it)
199 it->info = classifyArgumentType(it->type, Context, VMContext);
202 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
203 CodeGenFunction &CGF) const;
206 /// X86_32ABIInfo - The X86-32 ABI information.
207 class X86_32ABIInfo : public ABIInfo {
208 ASTContext &Context;
209 bool IsDarwin;
211 static bool isRegisterSize(unsigned Size) {
212 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
215 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
217 static unsigned getIndirectArgumentAlignment(QualType Ty,
218 ASTContext &Context);
220 public:
221 ABIArgInfo classifyReturnType(QualType RetTy,
222 ASTContext &Context,
223 llvm::LLVMContext &VMContext) const;
225 ABIArgInfo classifyArgumentType(QualType RetTy,
226 ASTContext &Context,
227 llvm::LLVMContext &VMContext) const;
229 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
230 llvm::LLVMContext &VMContext) const {
231 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
232 VMContext);
233 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
234 it != ie; ++it)
235 it->info = classifyArgumentType(it->type, Context, VMContext);
238 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
239 CodeGenFunction &CGF) const;
241 X86_32ABIInfo(ASTContext &Context, bool d)
242 : ABIInfo(), Context(Context), IsDarwin(d) {}
247 /// shouldReturnTypeInRegister - Determine if the given type should be
248 /// passed in a register (for the Darwin ABI).
249 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
250 ASTContext &Context) {
251 uint64_t Size = Context.getTypeSize(Ty);
253 // Type must be register sized.
254 if (!isRegisterSize(Size))
255 return false;
257 if (Ty->isVectorType()) {
258 // 64- and 128- bit vectors inside structures are not returned in
259 // registers.
260 if (Size == 64 || Size == 128)
261 return false;
263 return true;
266 // If this is a builtin, pointer, or complex type, it is ok.
267 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
268 return true;
270 // Arrays are treated like records.
271 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
272 return shouldReturnTypeInRegister(AT->getElementType(), Context);
274 // Otherwise, it must be a record type.
275 const RecordType *RT = Ty->getAsRecordType();
276 if (!RT) return false;
278 // Structure types are passed in register if all fields would be
279 // passed in a register.
280 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
281 e = RT->getDecl()->field_end(); i != e; ++i) {
282 const FieldDecl *FD = *i;
284 // Empty fields are ignored.
285 if (isEmptyField(Context, FD))
286 continue;
288 // Check fields recursively.
289 if (!shouldReturnTypeInRegister(FD->getType(), Context))
290 return false;
293 return true;
296 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
297 ASTContext &Context,
298 llvm::LLVMContext &VMContext) const {
299 if (RetTy->isVoidType()) {
300 return ABIArgInfo::getIgnore();
301 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
302 // On Darwin, some vectors are returned in registers.
303 if (IsDarwin) {
304 uint64_t Size = Context.getTypeSize(RetTy);
306 // 128-bit vectors are a special case; they are returned in
307 // registers and we need to make sure to pick a type the LLVM
308 // backend will like.
309 if (Size == 128)
310 return
311 ABIArgInfo::getCoerce(VMContext.getVectorType(llvm::Type::Int64Ty,
312 2));
314 // Always return in register if it fits in a general purpose
315 // register, or if it is 64 bits and has a single element.
316 if ((Size == 8 || Size == 16 || Size == 32) ||
317 (Size == 64 && VT->getNumElements() == 1))
318 return ABIArgInfo::getCoerce(VMContext.getIntegerType(Size));
320 return ABIArgInfo::getIndirect(0);
323 return ABIArgInfo::getDirect();
324 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
325 // Structures with flexible arrays are always indirect.
326 if (const RecordType *RT = RetTy->getAsStructureType())
327 if (RT->getDecl()->hasFlexibleArrayMember())
328 return ABIArgInfo::getIndirect(0);
330 // Outside of Darwin, structs and unions are always indirect.
331 if (!IsDarwin && !RetTy->isAnyComplexType())
332 return ABIArgInfo::getIndirect(0);
334 // Classify "single element" structs as their element type.
335 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
336 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
337 if (BT->isIntegerType()) {
338 // We need to use the size of the structure, padding
339 // bit-fields can adjust that to be larger than the single
340 // element type.
341 uint64_t Size = Context.getTypeSize(RetTy);
342 return ABIArgInfo::getCoerce(
343 VMContext.getIntegerType((unsigned) Size));
344 } else if (BT->getKind() == BuiltinType::Float) {
345 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
346 "Unexpect single element structure size!");
347 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
348 } else if (BT->getKind() == BuiltinType::Double) {
349 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
350 "Unexpect single element structure size!");
351 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
353 } else if (SeltTy->isPointerType()) {
354 // FIXME: It would be really nice if this could come out as the proper
355 // pointer type.
356 llvm::Type *PtrTy =
357 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
358 return ABIArgInfo::getCoerce(PtrTy);
359 } else if (SeltTy->isVectorType()) {
360 // 64- and 128-bit vectors are never returned in a
361 // register when inside a structure.
362 uint64_t Size = Context.getTypeSize(RetTy);
363 if (Size == 64 || Size == 128)
364 return ABIArgInfo::getIndirect(0);
366 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
370 // Small structures which are register sized are generally returned
371 // in a register.
372 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
373 uint64_t Size = Context.getTypeSize(RetTy);
374 return ABIArgInfo::getCoerce(VMContext.getIntegerType(Size));
377 return ABIArgInfo::getIndirect(0);
378 } else {
379 return (RetTy->isPromotableIntegerType() ?
380 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
384 unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
385 ASTContext &Context) {
386 unsigned Align = Context.getTypeAlign(Ty);
387 if (Align < 128) return 0;
388 if (const RecordType* RT = Ty->getAsRecordType())
389 if (typeContainsSSEVector(RT->getDecl(), Context))
390 return 16;
391 return 0;
394 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
395 ASTContext &Context,
396 llvm::LLVMContext &VMContext) const {
397 // FIXME: Set alignment on indirect arguments.
398 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
399 // Structures with flexible arrays are always indirect.
400 if (const RecordType *RT = Ty->getAsStructureType())
401 if (RT->getDecl()->hasFlexibleArrayMember())
402 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
403 Context));
405 // Ignore empty structs.
406 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
407 return ABIArgInfo::getIgnore();
409 // Expand structs with size <= 128-bits which consist only of
410 // basic types (int, long long, float, double, xxx*). This is
411 // non-recursive and does not ignore empty fields.
412 if (const RecordType *RT = Ty->getAsStructureType()) {
413 if (Context.getTypeSize(Ty) <= 4*32 &&
414 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
415 return ABIArgInfo::getExpand();
418 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
419 } else {
420 return (Ty->isPromotableIntegerType() ?
421 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
425 llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
426 CodeGenFunction &CGF) const {
427 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
428 const llvm::Type *BP = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
429 const llvm::Type *BPP = VMContext.getPointerTypeUnqual(BP);
431 CGBuilderTy &Builder = CGF.Builder;
432 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
433 "ap");
434 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
435 llvm::Type *PTy =
436 VMContext.getPointerTypeUnqual(CGF.ConvertType(Ty));
437 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
439 uint64_t Offset =
440 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
441 llvm::Value *NextAddr =
442 Builder.CreateGEP(Addr,
443 VMContext.getConstantInt(llvm::Type::Int32Ty, Offset),
444 "ap.next");
445 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
447 return AddrTyped;
450 namespace {
451 /// X86_64ABIInfo - The X86_64 ABI information.
452 class X86_64ABIInfo : public ABIInfo {
453 enum Class {
454 Integer = 0,
455 SSE,
456 SSEUp,
457 X87,
458 X87Up,
459 ComplexX87,
460 NoClass,
461 Memory
464 /// merge - Implement the X86_64 ABI merging algorithm.
466 /// Merge an accumulating classification \arg Accum with a field
467 /// classification \arg Field.
469 /// \param Accum - The accumulating classification. This should
470 /// always be either NoClass or the result of a previous merge
471 /// call. In addition, this should never be Memory (the caller
472 /// should just return Memory for the aggregate).
473 Class merge(Class Accum, Class Field) const;
475 /// classify - Determine the x86_64 register classes in which the
476 /// given type T should be passed.
478 /// \param Lo - The classification for the parts of the type
479 /// residing in the low word of the containing object.
481 /// \param Hi - The classification for the parts of the type
482 /// residing in the high word of the containing object.
484 /// \param OffsetBase - The bit offset of this type in the
485 /// containing object. Some parameters are classified different
486 /// depending on whether they straddle an eightbyte boundary.
488 /// If a word is unused its result will be NoClass; if a type should
489 /// be passed in Memory then at least the classification of \arg Lo
490 /// will be Memory.
492 /// The \arg Lo class will be NoClass iff the argument is ignored.
494 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
495 /// also be ComplexX87.
496 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
497 Class &Lo, Class &Hi) const;
499 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
500 /// to coerce to, chose the best way to pass Ty in the same place
501 /// that \arg CoerceTo would be passed, but while keeping the
502 /// emitted code as simple as possible.
504 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
505 /// the ways we might want to pass things, instead of constructing an LLVM
506 /// type. This makes this code more explicit, and it makes it clearer that we
507 /// are also doing this for correctness in the case of passing scalar types.
508 ABIArgInfo getCoerceResult(QualType Ty,
509 const llvm::Type *CoerceTo,
510 ASTContext &Context) const;
512 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
513 /// such that the argument will be passed in memory.
514 ABIArgInfo getIndirectResult(QualType Ty,
515 ASTContext &Context) const;
517 ABIArgInfo classifyReturnType(QualType RetTy,
518 ASTContext &Context,
519 llvm::LLVMContext &VMContext) const;
521 ABIArgInfo classifyArgumentType(QualType Ty,
522 ASTContext &Context,
523 llvm::LLVMContext &VMContext,
524 unsigned &neededInt,
525 unsigned &neededSSE) const;
527 public:
528 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
529 llvm::LLVMContext &VMContext) const;
531 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
532 CodeGenFunction &CGF) const;
536 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
537 Class Field) const {
538 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
539 // classified recursively so that always two fields are
540 // considered. The resulting class is calculated according to
541 // the classes of the fields in the eightbyte:
543 // (a) If both classes are equal, this is the resulting class.
545 // (b) If one of the classes is NO_CLASS, the resulting class is
546 // the other class.
548 // (c) If one of the classes is MEMORY, the result is the MEMORY
549 // class.
551 // (d) If one of the classes is INTEGER, the result is the
552 // INTEGER.
554 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
555 // MEMORY is used as class.
557 // (f) Otherwise class SSE is used.
559 // Accum should never be memory (we should have returned) or
560 // ComplexX87 (because this cannot be passed in a structure).
561 assert((Accum != Memory && Accum != ComplexX87) &&
562 "Invalid accumulated classification during merge.");
563 if (Accum == Field || Field == NoClass)
564 return Accum;
565 else if (Field == Memory)
566 return Memory;
567 else if (Accum == NoClass)
568 return Field;
569 else if (Accum == Integer || Field == Integer)
570 return Integer;
571 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
572 Accum == X87 || Accum == X87Up)
573 return Memory;
574 else
575 return SSE;
578 void X86_64ABIInfo::classify(QualType Ty,
579 ASTContext &Context,
580 uint64_t OffsetBase,
581 Class &Lo, Class &Hi) const {
582 // FIXME: This code can be simplified by introducing a simple value class for
583 // Class pairs with appropriate constructor methods for the various
584 // situations.
586 // FIXME: Some of the split computations are wrong; unaligned vectors
587 // shouldn't be passed in registers for example, so there is no chance they
588 // can straddle an eightbyte. Verify & simplify.
590 Lo = Hi = NoClass;
592 Class &Current = OffsetBase < 64 ? Lo : Hi;
593 Current = Memory;
595 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
596 BuiltinType::Kind k = BT->getKind();
598 if (k == BuiltinType::Void) {
599 Current = NoClass;
600 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
601 Lo = Integer;
602 Hi = Integer;
603 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
604 Current = Integer;
605 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
606 Current = SSE;
607 } else if (k == BuiltinType::LongDouble) {
608 Lo = X87;
609 Hi = X87Up;
611 // FIXME: _Decimal32 and _Decimal64 are SSE.
612 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
613 } else if (const EnumType *ET = Ty->getAsEnumType()) {
614 // Classify the underlying integer type.
615 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
616 } else if (Ty->hasPointerRepresentation()) {
617 Current = Integer;
618 } else if (const VectorType *VT = Ty->getAsVectorType()) {
619 uint64_t Size = Context.getTypeSize(VT);
620 if (Size == 32) {
621 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
622 // float> as integer.
623 Current = Integer;
625 // If this type crosses an eightbyte boundary, it should be
626 // split.
627 uint64_t EB_Real = (OffsetBase) / 64;
628 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
629 if (EB_Real != EB_Imag)
630 Hi = Lo;
631 } else if (Size == 64) {
632 // gcc passes <1 x double> in memory. :(
633 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
634 return;
636 // gcc passes <1 x long long> as INTEGER.
637 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
638 Current = Integer;
639 else
640 Current = SSE;
642 // If this type crosses an eightbyte boundary, it should be
643 // split.
644 if (OffsetBase && OffsetBase != 64)
645 Hi = Lo;
646 } else if (Size == 128) {
647 Lo = SSE;
648 Hi = SSEUp;
650 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
651 QualType ET = Context.getCanonicalType(CT->getElementType());
653 uint64_t Size = Context.getTypeSize(Ty);
654 if (ET->isIntegralType()) {
655 if (Size <= 64)
656 Current = Integer;
657 else if (Size <= 128)
658 Lo = Hi = Integer;
659 } else if (ET == Context.FloatTy)
660 Current = SSE;
661 else if (ET == Context.DoubleTy)
662 Lo = Hi = SSE;
663 else if (ET == Context.LongDoubleTy)
664 Current = ComplexX87;
666 // If this complex type crosses an eightbyte boundary then it
667 // should be split.
668 uint64_t EB_Real = (OffsetBase) / 64;
669 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
670 if (Hi == NoClass && EB_Real != EB_Imag)
671 Hi = Lo;
672 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
673 // Arrays are treated like structures.
675 uint64_t Size = Context.getTypeSize(Ty);
677 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
678 // than two eightbytes, ..., it has class MEMORY.
679 if (Size > 128)
680 return;
682 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
683 // fields, it has class MEMORY.
685 // Only need to check alignment of array base.
686 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
687 return;
689 // Otherwise implement simplified merge. We could be smarter about
690 // this, but it isn't worth it and would be harder to verify.
691 Current = NoClass;
692 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
693 uint64_t ArraySize = AT->getSize().getZExtValue();
694 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
695 Class FieldLo, FieldHi;
696 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
697 Lo = merge(Lo, FieldLo);
698 Hi = merge(Hi, FieldHi);
699 if (Lo == Memory || Hi == Memory)
700 break;
703 // Do post merger cleanup (see below). Only case we worry about is Memory.
704 if (Hi == Memory)
705 Lo = Memory;
706 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
707 } else if (const RecordType *RT = Ty->getAsRecordType()) {
708 uint64_t Size = Context.getTypeSize(Ty);
710 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
711 // than two eightbytes, ..., it has class MEMORY.
712 if (Size > 128)
713 return;
715 const RecordDecl *RD = RT->getDecl();
717 // Assume variable sized types are passed in memory.
718 if (RD->hasFlexibleArrayMember())
719 return;
721 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
723 // Reset Lo class, this will be recomputed.
724 Current = NoClass;
725 unsigned idx = 0;
726 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
727 i != e; ++i, ++idx) {
728 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
729 bool BitField = i->isBitField();
731 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
732 // fields, it has class MEMORY.
734 // Note, skip this test for bit-fields, see below.
735 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
736 Lo = Memory;
737 return;
740 // Classify this field.
742 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
743 // exceeds a single eightbyte, each is classified
744 // separately. Each eightbyte gets initialized to class
745 // NO_CLASS.
746 Class FieldLo, FieldHi;
748 // Bit-fields require special handling, they do not force the
749 // structure to be passed in memory even if unaligned, and
750 // therefore they can straddle an eightbyte.
751 if (BitField) {
752 // Ignore padding bit-fields.
753 if (i->isUnnamedBitfield())
754 continue;
756 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
757 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
759 uint64_t EB_Lo = Offset / 64;
760 uint64_t EB_Hi = (Offset + Size - 1) / 64;
761 FieldLo = FieldHi = NoClass;
762 if (EB_Lo) {
763 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
764 FieldLo = NoClass;
765 FieldHi = Integer;
766 } else {
767 FieldLo = Integer;
768 FieldHi = EB_Hi ? Integer : NoClass;
770 } else
771 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
772 Lo = merge(Lo, FieldLo);
773 Hi = merge(Hi, FieldHi);
774 if (Lo == Memory || Hi == Memory)
775 break;
778 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
780 // (a) If one of the classes is MEMORY, the whole argument is
781 // passed in memory.
783 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
785 // The first of these conditions is guaranteed by how we implement
786 // the merge (just bail).
788 // The second condition occurs in the case of unions; for example
789 // union { _Complex double; unsigned; }.
790 if (Hi == Memory)
791 Lo = Memory;
792 if (Hi == SSEUp && Lo != SSE)
793 Hi = SSE;
797 ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
798 const llvm::Type *CoerceTo,
799 ASTContext &Context) const {
800 if (CoerceTo == llvm::Type::Int64Ty) {
801 // Integer and pointer types will end up in a general purpose
802 // register.
803 if (Ty->isIntegralType() || Ty->isPointerType())
804 return (Ty->isPromotableIntegerType() ?
805 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
806 } else if (CoerceTo == llvm::Type::DoubleTy) {
807 // FIXME: It would probably be better to make CGFunctionInfo only map using
808 // canonical types than to canonize here.
809 QualType CTy = Context.getCanonicalType(Ty);
811 // Float and double end up in a single SSE reg.
812 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
813 return ABIArgInfo::getDirect();
817 return ABIArgInfo::getCoerce(CoerceTo);
820 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
821 ASTContext &Context) const {
822 // If this is a scalar LLVM value then assume LLVM will pass it in the right
823 // place naturally.
824 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
825 return (Ty->isPromotableIntegerType() ?
826 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
828 // FIXME: Set alignment correctly.
829 return ABIArgInfo::getIndirect(0);
832 ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
833 ASTContext &Context,
834 llvm::LLVMContext &VMContext) const {
835 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
836 // classification algorithm.
837 X86_64ABIInfo::Class Lo, Hi;
838 classify(RetTy, Context, 0, Lo, Hi);
840 // Check some invariants.
841 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
842 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
843 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
845 const llvm::Type *ResType = 0;
846 switch (Lo) {
847 case NoClass:
848 return ABIArgInfo::getIgnore();
850 case SSEUp:
851 case X87Up:
852 assert(0 && "Invalid classification for lo word.");
854 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
855 // hidden argument.
856 case Memory:
857 return getIndirectResult(RetTy, Context);
859 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
860 // available register of the sequence %rax, %rdx is used.
861 case Integer:
862 ResType = llvm::Type::Int64Ty; break;
864 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
865 // available SSE register of the sequence %xmm0, %xmm1 is used.
866 case SSE:
867 ResType = llvm::Type::DoubleTy; break;
869 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
870 // returned on the X87 stack in %st0 as 80-bit x87 number.
871 case X87:
872 ResType = llvm::Type::X86_FP80Ty; break;
874 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
875 // part of the value is returned in %st0 and the imaginary part in
876 // %st1.
877 case ComplexX87:
878 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
879 ResType = VMContext.getStructType(llvm::Type::X86_FP80Ty,
880 llvm::Type::X86_FP80Ty,
881 NULL);
882 break;
885 switch (Hi) {
886 // Memory was handled previously and X87 should
887 // never occur as a hi class.
888 case Memory:
889 case X87:
890 assert(0 && "Invalid classification for hi word.");
892 case ComplexX87: // Previously handled.
893 case NoClass: break;
895 case Integer:
896 ResType = VMContext.getStructType(ResType, llvm::Type::Int64Ty, NULL);
897 break;
898 case SSE:
899 ResType = VMContext.getStructType(ResType, llvm::Type::DoubleTy, NULL);
900 break;
902 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
903 // is passed in the upper half of the last used SSE register.
905 // SSEUP should always be preceeded by SSE, just widen.
906 case SSEUp:
907 assert(Lo == SSE && "Unexpected SSEUp classification.");
908 ResType = VMContext.getVectorType(llvm::Type::DoubleTy, 2);
909 break;
911 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
912 // returned together with the previous X87 value in %st0.
913 case X87Up:
914 // If X87Up is preceeded by X87, we don't need to do
915 // anything. However, in some cases with unions it may not be
916 // preceeded by X87. In such situations we follow gcc and pass the
917 // extra bits in an SSE reg.
918 if (Lo != X87)
919 ResType = VMContext.getStructType(ResType, llvm::Type::DoubleTy, NULL);
920 break;
923 return getCoerceResult(RetTy, ResType, Context);
926 ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
927 llvm::LLVMContext &VMContext,
928 unsigned &neededInt,
929 unsigned &neededSSE) const {
930 X86_64ABIInfo::Class Lo, Hi;
931 classify(Ty, Context, 0, Lo, Hi);
933 // Check some invariants.
934 // FIXME: Enforce these by construction.
935 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
936 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
937 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
939 neededInt = 0;
940 neededSSE = 0;
941 const llvm::Type *ResType = 0;
942 switch (Lo) {
943 case NoClass:
944 return ABIArgInfo::getIgnore();
946 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
947 // on the stack.
948 case Memory:
950 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
951 // COMPLEX_X87, it is passed in memory.
952 case X87:
953 case ComplexX87:
954 return getIndirectResult(Ty, Context);
956 case SSEUp:
957 case X87Up:
958 assert(0 && "Invalid classification for lo word.");
960 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
961 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
962 // and %r9 is used.
963 case Integer:
964 ++neededInt;
965 ResType = llvm::Type::Int64Ty;
966 break;
968 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
969 // available SSE register is used, the registers are taken in the
970 // order from %xmm0 to %xmm7.
971 case SSE:
972 ++neededSSE;
973 ResType = llvm::Type::DoubleTy;
974 break;
977 switch (Hi) {
978 // Memory was handled previously, ComplexX87 and X87 should
979 // never occur as hi classes, and X87Up must be preceed by X87,
980 // which is passed in memory.
981 case Memory:
982 case X87:
983 case ComplexX87:
984 assert(0 && "Invalid classification for hi word.");
985 break;
987 case NoClass: break;
988 case Integer:
989 ResType = VMContext.getStructType(ResType, llvm::Type::Int64Ty, NULL);
990 ++neededInt;
991 break;
993 // X87Up generally doesn't occur here (long double is passed in
994 // memory), except in situations involving unions.
995 case X87Up:
996 case SSE:
997 ResType = VMContext.getStructType(ResType, llvm::Type::DoubleTy, NULL);
998 ++neededSSE;
999 break;
1001 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1002 // eightbyte is passed in the upper half of the last used SSE
1003 // register.
1004 case SSEUp:
1005 assert(Lo == SSE && "Unexpected SSEUp classification.");
1006 ResType = VMContext.getVectorType(llvm::Type::DoubleTy, 2);
1007 break;
1010 return getCoerceResult(Ty, ResType, Context);
1013 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1014 llvm::LLVMContext &VMContext) const {
1015 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1016 Context, VMContext);
1018 // Keep track of the number of assigned registers.
1019 unsigned freeIntRegs = 6, freeSSERegs = 8;
1021 // If the return value is indirect, then the hidden argument is consuming one
1022 // integer register.
1023 if (FI.getReturnInfo().isIndirect())
1024 --freeIntRegs;
1026 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1027 // get assigned (in left-to-right order) for passing as follows...
1028 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1029 it != ie; ++it) {
1030 unsigned neededInt, neededSSE;
1031 it->info = classifyArgumentType(it->type, Context, VMContext,
1032 neededInt, neededSSE);
1034 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1035 // eightbyte of an argument, the whole argument is passed on the
1036 // stack. If registers have already been assigned for some
1037 // eightbytes of such an argument, the assignments get reverted.
1038 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1039 freeIntRegs -= neededInt;
1040 freeSSERegs -= neededSSE;
1041 } else {
1042 it->info = getIndirectResult(it->type, Context);
1047 static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1048 QualType Ty,
1049 CodeGenFunction &CGF) {
1050 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1051 llvm::Value *overflow_arg_area_p =
1052 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1053 llvm::Value *overflow_arg_area =
1054 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1056 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1057 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1058 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1059 if (Align > 8) {
1060 // Note that we follow the ABI & gcc here, even though the type
1061 // could in theory have an alignment greater than 16. This case
1062 // shouldn't ever matter in practice.
1064 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1065 llvm::Value *Offset = VMContext.getConstantInt(llvm::Type::Int32Ty, 15);
1066 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1067 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1068 llvm::Type::Int64Ty);
1069 llvm::Value *Mask = VMContext.getConstantInt(llvm::Type::Int64Ty, ~15LL);
1070 overflow_arg_area =
1071 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1072 overflow_arg_area->getType(),
1073 "overflow_arg_area.align");
1076 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1077 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1078 llvm::Value *Res =
1079 CGF.Builder.CreateBitCast(overflow_arg_area,
1080 VMContext.getPointerTypeUnqual(LTy));
1082 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1083 // l->overflow_arg_area + sizeof(type).
1084 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1085 // an 8 byte boundary.
1087 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1088 llvm::Value *Offset = VMContext.getConstantInt(llvm::Type::Int32Ty,
1089 (SizeInBytes + 7) & ~7);
1090 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1091 "overflow_arg_area.next");
1092 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1094 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1095 return Res;
1098 llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1099 CodeGenFunction &CGF) const {
1100 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1102 // Assume that va_list type is correct; should be pointer to LLVM type:
1103 // struct {
1104 // i32 gp_offset;
1105 // i32 fp_offset;
1106 // i8* overflow_arg_area;
1107 // i8* reg_save_area;
1108 // };
1109 unsigned neededInt, neededSSE;
1110 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
1111 neededInt, neededSSE);
1113 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1114 // in the registers. If not go to step 7.
1115 if (!neededInt && !neededSSE)
1116 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1118 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1119 // general purpose registers needed to pass type and num_fp to hold
1120 // the number of floating point registers needed.
1122 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1123 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1124 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1126 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1127 // register save space).
1129 llvm::Value *InRegs = 0;
1130 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1131 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1132 if (neededInt) {
1133 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1134 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1135 InRegs =
1136 CGF.Builder.CreateICmpULE(gp_offset,
1137 VMContext.getConstantInt(llvm::Type::Int32Ty,
1138 48 - neededInt * 8),
1139 "fits_in_gp");
1142 if (neededSSE) {
1143 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1144 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1145 llvm::Value *FitsInFP =
1146 CGF.Builder.CreateICmpULE(fp_offset,
1147 VMContext.getConstantInt(llvm::Type::Int32Ty,
1148 176 - neededSSE * 16),
1149 "fits_in_fp");
1150 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1153 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1154 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1155 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1156 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1158 // Emit code to load the value if it was passed in registers.
1160 CGF.EmitBlock(InRegBlock);
1162 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1163 // an offset of l->gp_offset and/or l->fp_offset. This may require
1164 // copying to a temporary location in case the parameter is passed
1165 // in different register classes or requires an alignment greater
1166 // than 8 for general purpose registers and 16 for XMM registers.
1168 // FIXME: This really results in shameful code when we end up needing to
1169 // collect arguments from different places; often what should result in a
1170 // simple assembling of a structure from scattered addresses has many more
1171 // loads than necessary. Can we clean this up?
1172 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1173 llvm::Value *RegAddr =
1174 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1175 "reg_save_area");
1176 if (neededInt && neededSSE) {
1177 // FIXME: Cleanup.
1178 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1179 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1180 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1181 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1182 const llvm::Type *TyLo = ST->getElementType(0);
1183 const llvm::Type *TyHi = ST->getElementType(1);
1184 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1185 "Unexpected ABI info for mixed regs");
1186 const llvm::Type *PTyLo = VMContext.getPointerTypeUnqual(TyLo);
1187 const llvm::Type *PTyHi = VMContext.getPointerTypeUnqual(TyHi);
1188 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1189 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1190 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1191 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1192 llvm::Value *V =
1193 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1194 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1195 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1196 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1198 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1199 VMContext.getPointerTypeUnqual(LTy));
1200 } else if (neededInt) {
1201 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1202 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1203 VMContext.getPointerTypeUnqual(LTy));
1204 } else {
1205 if (neededSSE == 1) {
1206 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1207 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1208 VMContext.getPointerTypeUnqual(LTy));
1209 } else {
1210 assert(neededSSE == 2 && "Invalid number of needed registers!");
1211 // SSE registers are spaced 16 bytes apart in the register save
1212 // area, we need to collect the two eightbytes together.
1213 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1214 llvm::Value *RegAddrHi =
1215 CGF.Builder.CreateGEP(RegAddrLo,
1216 VMContext.getConstantInt(llvm::Type::Int32Ty, 16));
1217 const llvm::Type *DblPtrTy =
1218 VMContext.getPointerTypeUnqual(llvm::Type::DoubleTy);
1219 const llvm::StructType *ST = VMContext.getStructType(llvm::Type::DoubleTy,
1220 llvm::Type::DoubleTy,
1221 NULL);
1222 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1223 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1224 DblPtrTy));
1225 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1226 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1227 DblPtrTy));
1228 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1229 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1230 VMContext.getPointerTypeUnqual(LTy));
1234 // AMD64-ABI 3.5.7p5: Step 5. Set:
1235 // l->gp_offset = l->gp_offset + num_gp * 8
1236 // l->fp_offset = l->fp_offset + num_fp * 16.
1237 if (neededInt) {
1238 llvm::Value *Offset = VMContext.getConstantInt(llvm::Type::Int32Ty,
1239 neededInt * 8);
1240 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1241 gp_offset_p);
1243 if (neededSSE) {
1244 llvm::Value *Offset = VMContext.getConstantInt(llvm::Type::Int32Ty,
1245 neededSSE * 16);
1246 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1247 fp_offset_p);
1249 CGF.EmitBranch(ContBlock);
1251 // Emit code to load the value if it was passed in memory.
1253 CGF.EmitBlock(InMemBlock);
1254 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1256 // Return the appropriate result.
1258 CGF.EmitBlock(ContBlock);
1259 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1260 "vaarg.addr");
1261 ResAddr->reserveOperandSpace(2);
1262 ResAddr->addIncoming(RegAddr, InRegBlock);
1263 ResAddr->addIncoming(MemAddr, InMemBlock);
1265 return ResAddr;
1268 // ABI Info for PIC16
1269 class PIC16ABIInfo : public ABIInfo {
1270 ABIArgInfo classifyReturnType(QualType RetTy,
1271 ASTContext &Context,
1272 llvm::LLVMContext &VMContext) const;
1274 ABIArgInfo classifyArgumentType(QualType RetTy,
1275 ASTContext &Context,
1276 llvm::LLVMContext &VMContext) const;
1278 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1279 llvm::LLVMContext &VMContext) const {
1280 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1281 VMContext);
1282 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1283 it != ie; ++it)
1284 it->info = classifyArgumentType(it->type, Context, VMContext);
1287 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1288 CodeGenFunction &CGF) const;
1292 ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
1293 ASTContext &Context,
1294 llvm::LLVMContext &VMContext) const {
1295 if (RetTy->isVoidType()) {
1296 return ABIArgInfo::getIgnore();
1297 } else {
1298 return ABIArgInfo::getDirect();
1302 ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
1303 ASTContext &Context,
1304 llvm::LLVMContext &VMContext) const {
1305 return ABIArgInfo::getDirect();
1308 llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1309 CodeGenFunction &CGF) const {
1310 return 0;
1313 class ARMABIInfo : public ABIInfo {
1314 ABIArgInfo classifyReturnType(QualType RetTy,
1315 ASTContext &Context,
1316 llvm::LLVMContext &VMCOntext) const;
1318 ABIArgInfo classifyArgumentType(QualType RetTy,
1319 ASTContext &Context,
1320 llvm::LLVMContext &VMContext) const;
1322 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1323 llvm::LLVMContext &VMContext) const;
1325 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1326 CodeGenFunction &CGF) const;
1329 void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1330 llvm::LLVMContext &VMContext) const {
1331 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1332 VMContext);
1333 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1334 it != ie; ++it) {
1335 it->info = classifyArgumentType(it->type, Context, VMContext);
1339 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1340 ASTContext &Context,
1341 llvm::LLVMContext &VMContext) const {
1342 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1343 return (Ty->isPromotableIntegerType() ?
1344 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1346 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1347 // backend doesn't support byval.
1348 // FIXME: This doesn't handle alignment > 64 bits.
1349 const llvm::Type* ElemTy;
1350 unsigned SizeRegs;
1351 if (Context.getTypeAlign(Ty) > 32) {
1352 ElemTy = llvm::Type::Int64Ty;
1353 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1354 } else {
1355 ElemTy = llvm::Type::Int32Ty;
1356 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1358 std::vector<const llvm::Type*> LLVMFields;
1359 LLVMFields.push_back(VMContext.getArrayType(ElemTy, SizeRegs));
1360 const llvm::Type* STy = VMContext.getStructType(LLVMFields, true);
1361 return ABIArgInfo::getCoerce(STy);
1364 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1365 ASTContext &Context,
1366 llvm::LLVMContext &VMContext) const {
1367 if (RetTy->isVoidType()) {
1368 return ABIArgInfo::getIgnore();
1369 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1370 // Aggregates <= 4 bytes are returned in r0; other aggregates
1371 // are returned indirectly.
1372 uint64_t Size = Context.getTypeSize(RetTy);
1373 if (Size <= 32)
1374 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1375 return ABIArgInfo::getIndirect(0);
1376 } else {
1377 return (RetTy->isPromotableIntegerType() ?
1378 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1382 llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1383 CodeGenFunction &CGF) const {
1384 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
1386 // FIXME: Need to handle alignment
1387 const llvm::Type *BP = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
1388 const llvm::Type *BPP = VMContext.getPointerTypeUnqual(BP);
1390 CGBuilderTy &Builder = CGF.Builder;
1391 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1392 "ap");
1393 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1394 llvm::Type *PTy =
1395 VMContext.getPointerTypeUnqual(CGF.ConvertType(Ty));
1396 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1398 uint64_t Offset =
1399 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1400 llvm::Value *NextAddr =
1401 Builder.CreateGEP(Addr,
1402 VMContext.getConstantInt(llvm::Type::Int32Ty, Offset),
1403 "ap.next");
1404 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1406 return AddrTyped;
1409 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
1410 ASTContext &Context,
1411 llvm::LLVMContext &VMContext) const {
1412 if (RetTy->isVoidType()) {
1413 return ABIArgInfo::getIgnore();
1414 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1415 return ABIArgInfo::getIndirect(0);
1416 } else {
1417 return (RetTy->isPromotableIntegerType() ?
1418 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1422 namespace {
1423 class SystemZABIInfo : public ABIInfo {
1424 bool isPromotableIntegerType(QualType Ty) const;
1426 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1427 llvm::LLVMContext &VMContext) const;
1429 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1430 llvm::LLVMContext &VMContext) const;
1432 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1433 llvm::LLVMContext &VMContext) const {
1434 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1435 Context, VMContext);
1436 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1437 it != ie; ++it)
1438 it->info = classifyArgumentType(it->type, Context, VMContext);
1441 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1442 CodeGenFunction &CGF) const;
1446 bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1447 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
1448 if (const BuiltinType *BT = Ty->getAsBuiltinType())
1449 switch (BT->getKind()) {
1450 case BuiltinType::Bool:
1451 case BuiltinType::Char_S:
1452 case BuiltinType::Char_U:
1453 case BuiltinType::SChar:
1454 case BuiltinType::UChar:
1455 case BuiltinType::Short:
1456 case BuiltinType::UShort:
1457 case BuiltinType::Int:
1458 case BuiltinType::UInt:
1459 return true;
1460 default:
1461 return false;
1463 return false;
1466 llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1467 CodeGenFunction &CGF) const {
1468 // FIXME: Implement
1469 return 0;
1473 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1474 ASTContext &Context,
1475 llvm::LLVMContext &VMContext) const {
1476 if (RetTy->isVoidType()) {
1477 return ABIArgInfo::getIgnore();
1478 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1479 return ABIArgInfo::getIndirect(0);
1480 } else {
1481 return (isPromotableIntegerType(RetTy) ?
1482 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1486 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1487 ASTContext &Context,
1488 llvm::LLVMContext &VMContext) const {
1489 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1490 return ABIArgInfo::getIndirect(0);
1491 } else {
1492 return (isPromotableIntegerType(Ty) ?
1493 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1497 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
1498 ASTContext &Context,
1499 llvm::LLVMContext &VMContext) const {
1500 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1501 return ABIArgInfo::getIndirect(0);
1502 } else {
1503 return (Ty->isPromotableIntegerType() ?
1504 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1508 llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1509 CodeGenFunction &CGF) const {
1510 return 0;
1513 const ABIInfo &CodeGenTypes::getABIInfo() const {
1514 if (TheABIInfo)
1515 return *TheABIInfo;
1517 // For now we just cache this in the CodeGenTypes and don't bother
1518 // to free it.
1519 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1520 if (strcmp(TargetPrefix, "x86") == 0) {
1521 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
1522 switch (getContext().Target.getPointerWidth(0)) {
1523 case 32:
1524 return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
1525 case 64:
1526 return *(TheABIInfo = new X86_64ABIInfo());
1528 } else if (strcmp(TargetPrefix, "arm") == 0) {
1529 // FIXME: Support for OABI?
1530 return *(TheABIInfo = new ARMABIInfo());
1531 } else if (strcmp(TargetPrefix, "pic16") == 0) {
1532 return *(TheABIInfo = new PIC16ABIInfo());
1533 } else if (strcmp(TargetPrefix, "s390x") == 0) {
1534 return *(TheABIInfo = new SystemZABIInfo());
1537 return *(TheABIInfo = new DefaultABIInfo);