17a51313dSChris Lattner //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
27a51313dSChris Lattner //
37a51313dSChris Lattner //                     The LLVM Compiler Infrastructure
47a51313dSChris Lattner //
57a51313dSChris Lattner // This file is distributed under the University of Illinois Open Source
67a51313dSChris Lattner // License. See LICENSE.TXT for details.
77a51313dSChris Lattner //
87a51313dSChris Lattner //===----------------------------------------------------------------------===//
97a51313dSChris Lattner //
107a51313dSChris Lattner // This contains code to emit Aggregate Expr nodes as LLVM code.
117a51313dSChris Lattner //
127a51313dSChris Lattner //===----------------------------------------------------------------------===//
137a51313dSChris Lattner 
147a51313dSChris Lattner #include "CodeGenFunction.h"
157a51313dSChris Lattner #include "CodeGenModule.h"
165f21d2f6SFariborz Jahanian #include "CGObjCRuntime.h"
17ad319a73SDaniel Dunbar #include "clang/AST/ASTContext.h"
18b7f8f594SAnders Carlsson #include "clang/AST/DeclCXX.h"
19ad319a73SDaniel Dunbar #include "clang/AST/StmtVisitor.h"
207a51313dSChris Lattner #include "llvm/Constants.h"
217a51313dSChris Lattner #include "llvm/Function.h"
227a51313dSChris Lattner #include "llvm/GlobalVariable.h"
23579a05d7SChris Lattner #include "llvm/Intrinsics.h"
247a51313dSChris Lattner using namespace clang;
257a51313dSChris Lattner using namespace CodeGen;
267a51313dSChris Lattner 
277a51313dSChris Lattner //===----------------------------------------------------------------------===//
287a51313dSChris Lattner //                        Aggregate Expression Emitter
297a51313dSChris Lattner //===----------------------------------------------------------------------===//
307a51313dSChris Lattner 
317a51313dSChris Lattner namespace  {
32337e3a5fSBenjamin Kramer class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
337a51313dSChris Lattner   CodeGenFunction &CGF;
34cb463859SDaniel Dunbar   CGBuilderTy &Builder;
357a626f63SJohn McCall   AggValueSlot Dest;
36ec3cbfe8SMike Stump   bool IgnoreResult;
3778a15113SJohn McCall 
3878a15113SJohn McCall   ReturnValueSlot getReturnValueSlot() const {
39cc04e9f6SJohn McCall     // If the destination slot requires garbage collection, we can't
40cc04e9f6SJohn McCall     // use the real return value slot, because we have to use the GC
41cc04e9f6SJohn McCall     // API.
4258649dc6SJohn McCall     if (Dest.requiresGCollection()) return ReturnValueSlot();
43cc04e9f6SJohn McCall 
447a626f63SJohn McCall     return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile());
457a626f63SJohn McCall   }
467a626f63SJohn McCall 
477a626f63SJohn McCall   AggValueSlot EnsureSlot(QualType T) {
487a626f63SJohn McCall     if (!Dest.isIgnored()) return Dest;
497a626f63SJohn McCall     return CGF.CreateAggTemp(T, "agg.tmp.ensured");
5078a15113SJohn McCall   }
51cc04e9f6SJohn McCall 
527a51313dSChris Lattner public:
537a626f63SJohn McCall   AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest,
54b60e70f9SFariborz Jahanian                  bool ignore)
557a626f63SJohn McCall     : CGF(cgf), Builder(CGF.Builder), Dest(Dest),
56b60e70f9SFariborz Jahanian       IgnoreResult(ignore) {
577a51313dSChris Lattner   }
587a51313dSChris Lattner 
597a51313dSChris Lattner   //===--------------------------------------------------------------------===//
607a51313dSChris Lattner   //                               Utilities
617a51313dSChris Lattner   //===--------------------------------------------------------------------===//
627a51313dSChris Lattner 
637a51313dSChris Lattner   /// EmitAggLoadOfLValue - Given an expression with aggregate type that
647a51313dSChris Lattner   /// represents a value lvalue, this method emits the address of the lvalue,
657a51313dSChris Lattner   /// then loads the result into DestPtr.
667a51313dSChris Lattner   void EmitAggLoadOfLValue(const Expr *E);
677a51313dSChris Lattner 
68ca9fc09cSMike Stump   /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
69ec3cbfe8SMike Stump   void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
70ec3cbfe8SMike Stump   void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
71ca9fc09cSMike Stump 
72cc04e9f6SJohn McCall   void EmitGCMove(const Expr *E, RValue Src);
73cc04e9f6SJohn McCall 
74cc04e9f6SJohn McCall   bool TypeRequiresGCollection(QualType T);
75cc04e9f6SJohn McCall 
767a51313dSChris Lattner   //===--------------------------------------------------------------------===//
777a51313dSChris Lattner   //                            Visitor Methods
787a51313dSChris Lattner   //===--------------------------------------------------------------------===//
797a51313dSChris Lattner 
807a51313dSChris Lattner   void VisitStmt(Stmt *S) {
81a7c8cf62SDaniel Dunbar     CGF.ErrorUnsupported(S, "aggregate expression");
827a51313dSChris Lattner   }
837a51313dSChris Lattner   void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
8491147596SPeter Collingbourne   void VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
8591147596SPeter Collingbourne     Visit(GE->getResultExpr());
8691147596SPeter Collingbourne   }
873f66b84cSEli Friedman   void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
887a51313dSChris Lattner 
897a51313dSChris Lattner   // l-values.
907a51313dSChris Lattner   void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
917a51313dSChris Lattner   void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
927a51313dSChris Lattner   void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
93d443c0a0SDaniel Dunbar   void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
942f343dd5SChris Lattner   void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
952f343dd5SChris Lattner     EmitAggLoadOfLValue(E);
962f343dd5SChris Lattner   }
977a51313dSChris Lattner   void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
987a51313dSChris Lattner     EmitAggLoadOfLValue(E);
997a51313dSChris Lattner   }
1002f343dd5SChris Lattner   void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
1012f343dd5SChris Lattner     EmitAggLoadOfLValue(E);
1022f343dd5SChris Lattner   }
1032f343dd5SChris Lattner   void VisitPredefinedExpr(const PredefinedExpr *E) {
1042f343dd5SChris Lattner     EmitAggLoadOfLValue(E);
1052f343dd5SChris Lattner   }
106bc7d67ceSMike Stump 
1077a51313dSChris Lattner   // Operators.
108ec143777SAnders Carlsson   void VisitCastExpr(CastExpr *E);
1097a51313dSChris Lattner   void VisitCallExpr(const CallExpr *E);
1107a51313dSChris Lattner   void VisitStmtExpr(const StmtExpr *E);
1117a51313dSChris Lattner   void VisitBinaryOperator(const BinaryOperator *BO);
112ffba662dSFariborz Jahanian   void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
1137a51313dSChris Lattner   void VisitBinAssign(const BinaryOperator *E);
1144b0e2a30SEli Friedman   void VisitBinComma(const BinaryOperator *E);
1157a51313dSChris Lattner 
116b1d329daSChris Lattner   void VisitObjCMessageExpr(ObjCMessageExpr *E);
117c8317a44SDaniel Dunbar   void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
118c8317a44SDaniel Dunbar     EmitAggLoadOfLValue(E);
119c8317a44SDaniel Dunbar   }
12055310df7SDaniel Dunbar   void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
1217a51313dSChris Lattner 
122c07a0c7eSJohn McCall   void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
1235b2095ceSAnders Carlsson   void VisitChooseExpr(const ChooseExpr *CE);
1247a51313dSChris Lattner   void VisitInitListExpr(InitListExpr *E);
12518ada985SAnders Carlsson   void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
126aa9c7aedSChris Lattner   void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
127aa9c7aedSChris Lattner     Visit(DAE->getExpr());
128aa9c7aedSChris Lattner   }
1293be22e27SAnders Carlsson   void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
1301619a504SAnders Carlsson   void VisitCXXConstructExpr(const CXXConstructExpr *E);
1315d413781SJohn McCall   void VisitExprWithCleanups(ExprWithCleanups *E);
132747eb784SDouglas Gregor   void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
1335bbbb137SMike Stump   void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
134c82b86dfSAnders Carlsson 
1351bf5846aSJohn McCall   void VisitOpaqueValueExpr(OpaqueValueExpr *E);
1361bf5846aSJohn McCall 
13721911e89SEli Friedman   void VisitVAArgExpr(VAArgExpr *E);
138579a05d7SChris Lattner 
139b247350eSAnders Carlsson   void EmitInitializationToLValue(Expr *E, LValue Address, QualType T);
140579a05d7SChris Lattner   void EmitNullInitializationToLValue(LValue Address, QualType T);
1417a51313dSChris Lattner   //  case Expr::ChooseExprClass:
142f16b8c30SMike Stump   void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
1437a51313dSChris Lattner };
1447a51313dSChris Lattner }  // end anonymous namespace.
1457a51313dSChris Lattner 
1467a51313dSChris Lattner //===----------------------------------------------------------------------===//
1477a51313dSChris Lattner //                                Utilities
1487a51313dSChris Lattner //===----------------------------------------------------------------------===//
1497a51313dSChris Lattner 
1507a51313dSChris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that
1517a51313dSChris Lattner /// represents a value lvalue, this method emits the address of the lvalue,
1527a51313dSChris Lattner /// then loads the result into DestPtr.
1537a51313dSChris Lattner void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
1547a51313dSChris Lattner   LValue LV = CGF.EmitLValue(E);
155ca9fc09cSMike Stump   EmitFinalDestCopy(E, LV);
156ca9fc09cSMike Stump }
157ca9fc09cSMike Stump 
158cc04e9f6SJohn McCall /// \brief True if the given aggregate type requires special GC API calls.
159cc04e9f6SJohn McCall bool AggExprEmitter::TypeRequiresGCollection(QualType T) {
160cc04e9f6SJohn McCall   // Only record types have members that might require garbage collection.
161cc04e9f6SJohn McCall   const RecordType *RecordTy = T->getAs<RecordType>();
162cc04e9f6SJohn McCall   if (!RecordTy) return false;
163cc04e9f6SJohn McCall 
164cc04e9f6SJohn McCall   // Don't mess with non-trivial C++ types.
165cc04e9f6SJohn McCall   RecordDecl *Record = RecordTy->getDecl();
166cc04e9f6SJohn McCall   if (isa<CXXRecordDecl>(Record) &&
167cc04e9f6SJohn McCall       (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() ||
168cc04e9f6SJohn McCall        !cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
169cc04e9f6SJohn McCall     return false;
170cc04e9f6SJohn McCall 
171cc04e9f6SJohn McCall   // Check whether the type has an object member.
172cc04e9f6SJohn McCall   return Record->hasObjectMember();
173cc04e9f6SJohn McCall }
174cc04e9f6SJohn McCall 
175cc04e9f6SJohn McCall /// \brief Perform the final move to DestPtr if RequiresGCollection is set.
176cc04e9f6SJohn McCall ///
177cc04e9f6SJohn McCall /// The idea is that you do something like this:
178cc04e9f6SJohn McCall ///   RValue Result = EmitSomething(..., getReturnValueSlot());
179cc04e9f6SJohn McCall ///   EmitGCMove(E, Result);
180cc04e9f6SJohn McCall /// If GC doesn't interfere, this will cause the result to be emitted
181cc04e9f6SJohn McCall /// directly into the return value slot.  If GC does interfere, a final
182cc04e9f6SJohn McCall /// move will be performed.
183cc04e9f6SJohn McCall void AggExprEmitter::EmitGCMove(const Expr *E, RValue Src) {
18458649dc6SJohn McCall   if (Dest.requiresGCollection()) {
1853b4bd9a1SKen Dyck     CharUnits size = CGF.getContext().getTypeSizeInChars(E->getType());
186021510e9SFariborz Jahanian     const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
1873b4bd9a1SKen Dyck     llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity());
1887a626f63SJohn McCall     CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, Dest.getAddr(),
189cc04e9f6SJohn McCall                                                     Src.getAggregateAddr(),
190021510e9SFariborz Jahanian                                                     SizeVal);
191021510e9SFariborz Jahanian   }
192cc04e9f6SJohn McCall }
193cc04e9f6SJohn McCall 
194ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
195ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
196ca9fc09cSMike Stump   assert(Src.isAggregate() && "value must be aggregate value!");
1977a51313dSChris Lattner 
1987a626f63SJohn McCall   // If Dest is ignored, then we're evaluating an aggregate expression
1998d752430SJohn McCall   // in a context (like an expression statement) that doesn't care
2008d752430SJohn McCall   // about the result.  C says that an lvalue-to-rvalue conversion is
2018d752430SJohn McCall   // performed in these cases; C++ says that it is not.  In either
2028d752430SJohn McCall   // case, we don't actually need to do anything unless the value is
2038d752430SJohn McCall   // volatile.
2047a626f63SJohn McCall   if (Dest.isIgnored()) {
2058d752430SJohn McCall     if (!Src.isVolatileQualified() ||
2068d752430SJohn McCall         CGF.CGM.getLangOptions().CPlusPlus ||
2078d752430SJohn McCall         (IgnoreResult && Ignore))
208ec3cbfe8SMike Stump       return;
209c123623dSFariborz Jahanian 
210332ec2ceSMike Stump     // If the source is volatile, we must read from it; to do that, we need
211332ec2ceSMike Stump     // some place to put it.
2127a626f63SJohn McCall     Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp");
213332ec2ceSMike Stump   }
2147a51313dSChris Lattner 
21558649dc6SJohn McCall   if (Dest.requiresGCollection()) {
2163b4bd9a1SKen Dyck     CharUnits size = CGF.getContext().getTypeSizeInChars(E->getType());
217021510e9SFariborz Jahanian     const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
2183b4bd9a1SKen Dyck     llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity());
219879d7266SFariborz Jahanian     CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
2207a626f63SJohn McCall                                                       Dest.getAddr(),
2217a626f63SJohn McCall                                                       Src.getAggregateAddr(),
222021510e9SFariborz Jahanian                                                       SizeVal);
223879d7266SFariborz Jahanian     return;
224879d7266SFariborz Jahanian   }
225ca9fc09cSMike Stump   // If the result of the assignment is used, copy the LHS there also.
226ca9fc09cSMike Stump   // FIXME: Pass VolatileDest as well.  I think we also need to merge volatile
227ca9fc09cSMike Stump   // from the source as well, as we can't eliminate it if either operand
228ca9fc09cSMike Stump   // is volatile, unless copy has volatile for both source and destination..
2297a626f63SJohn McCall   CGF.EmitAggregateCopy(Dest.getAddr(), Src.getAggregateAddr(), E->getType(),
2307a626f63SJohn McCall                         Dest.isVolatile()|Src.isVolatileQualified());
231ca9fc09cSMike Stump }
232ca9fc09cSMike Stump 
233ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
234ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
235ca9fc09cSMike Stump   assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
236ca9fc09cSMike Stump 
237ca9fc09cSMike Stump   EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
238ec3cbfe8SMike Stump                                             Src.isVolatileQualified()),
239ec3cbfe8SMike Stump                     Ignore);
2407a51313dSChris Lattner }
2417a51313dSChris Lattner 
2427a51313dSChris Lattner //===----------------------------------------------------------------------===//
2437a51313dSChris Lattner //                            Visitor Methods
2447a51313dSChris Lattner //===----------------------------------------------------------------------===//
2457a51313dSChris Lattner 
2461bf5846aSJohn McCall void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) {
247c07a0c7eSJohn McCall   EmitFinalDestCopy(e, CGF.getOpaqueLValueMapping(e));
2481bf5846aSJohn McCall }
2491bf5846aSJohn McCall 
250ec143777SAnders Carlsson void AggExprEmitter::VisitCastExpr(CastExpr *E) {
2511fb7ae9eSAnders Carlsson   switch (E->getCastKind()) {
2528a01a751SAnders Carlsson   case CK_Dynamic: {
2531c073f47SDouglas Gregor     assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
2541c073f47SDouglas Gregor     LValue LV = CGF.EmitCheckedLValue(E->getSubExpr());
2551c073f47SDouglas Gregor     // FIXME: Do we also need to handle property references here?
2561c073f47SDouglas Gregor     if (LV.isSimple())
2571c073f47SDouglas Gregor       CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
2581c073f47SDouglas Gregor     else
2591c073f47SDouglas Gregor       CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
2601c073f47SDouglas Gregor 
2617a626f63SJohn McCall     if (!Dest.isIgnored())
2621c073f47SDouglas Gregor       CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
2631c073f47SDouglas Gregor     break;
2641c073f47SDouglas Gregor   }
2651c073f47SDouglas Gregor 
266e302792bSJohn McCall   case CK_ToUnion: {
26758989b71SJohn McCall     if (Dest.isIgnored()) break;
26858989b71SJohn McCall 
2697ffcf93bSNuno Lopes     // GCC union extension
2702e442a00SDaniel Dunbar     QualType Ty = E->getSubExpr()->getType();
2712e442a00SDaniel Dunbar     QualType PtrTy = CGF.getContext().getPointerType(Ty);
2727a626f63SJohn McCall     llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(),
273dd274848SEli Friedman                                                  CGF.ConvertType(PtrTy));
2742e442a00SDaniel Dunbar     EmitInitializationToLValue(E->getSubExpr(), CGF.MakeAddrLValue(CastPtr, Ty),
2752e442a00SDaniel Dunbar                                Ty);
2761fb7ae9eSAnders Carlsson     break;
2777ffcf93bSNuno Lopes   }
2787ffcf93bSNuno Lopes 
279e302792bSJohn McCall   case CK_DerivedToBase:
280e302792bSJohn McCall   case CK_BaseToDerived:
281e302792bSJohn McCall   case CK_UncheckedDerivedToBase: {
282aae38d66SDouglas Gregor     assert(0 && "cannot perform hierarchy conversion in EmitAggExpr: "
283aae38d66SDouglas Gregor                 "should have been unpacked before we got here");
284aae38d66SDouglas Gregor     break;
285aae38d66SDouglas Gregor   }
286aae38d66SDouglas Gregor 
28734376a68SJohn McCall   case CK_GetObjCProperty: {
28834376a68SJohn McCall     LValue LV = CGF.EmitLValue(E->getSubExpr());
28934376a68SJohn McCall     assert(LV.isPropertyRef());
29034376a68SJohn McCall     RValue RV = CGF.EmitLoadOfPropertyRefLValue(LV, getReturnValueSlot());
29134376a68SJohn McCall     EmitGCMove(E, RV);
29234376a68SJohn McCall     break;
29334376a68SJohn McCall   }
29434376a68SJohn McCall 
29534376a68SJohn McCall   case CK_LValueToRValue: // hope for downstream optimization
296e302792bSJohn McCall   case CK_NoOp:
297e302792bSJohn McCall   case CK_UserDefinedConversion:
298e302792bSJohn McCall   case CK_ConstructorConversion:
2992a69547fSEli Friedman     assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
3002a69547fSEli Friedman                                                    E->getType()) &&
3010f398c44SChris Lattner            "Implicit cast types must be compatible");
3027a51313dSChris Lattner     Visit(E->getSubExpr());
3031fb7ae9eSAnders Carlsson     break;
304b05a3e55SAnders Carlsson 
305e302792bSJohn McCall   case CK_LValueBitCast:
306f3735e01SJohn McCall     llvm_unreachable("should not be emitting lvalue bitcast as rvalue");
30751954276SDouglas Gregor     break;
30831996343SJohn McCall 
309f3735e01SJohn McCall   case CK_Dependent:
310f3735e01SJohn McCall   case CK_BitCast:
311f3735e01SJohn McCall   case CK_ArrayToPointerDecay:
312f3735e01SJohn McCall   case CK_FunctionToPointerDecay:
313f3735e01SJohn McCall   case CK_NullToPointer:
314f3735e01SJohn McCall   case CK_NullToMemberPointer:
315f3735e01SJohn McCall   case CK_BaseToDerivedMemberPointer:
316f3735e01SJohn McCall   case CK_DerivedToBaseMemberPointer:
317f3735e01SJohn McCall   case CK_MemberPointerToBoolean:
318f3735e01SJohn McCall   case CK_IntegralToPointer:
319f3735e01SJohn McCall   case CK_PointerToIntegral:
320f3735e01SJohn McCall   case CK_PointerToBoolean:
321f3735e01SJohn McCall   case CK_ToVoid:
322f3735e01SJohn McCall   case CK_VectorSplat:
323f3735e01SJohn McCall   case CK_IntegralCast:
324f3735e01SJohn McCall   case CK_IntegralToBoolean:
325f3735e01SJohn McCall   case CK_IntegralToFloating:
326f3735e01SJohn McCall   case CK_FloatingToIntegral:
327f3735e01SJohn McCall   case CK_FloatingToBoolean:
328f3735e01SJohn McCall   case CK_FloatingCast:
329f3735e01SJohn McCall   case CK_AnyPointerToObjCPointerCast:
330f3735e01SJohn McCall   case CK_AnyPointerToBlockPointerCast:
331f3735e01SJohn McCall   case CK_ObjCObjectLValueCast:
332f3735e01SJohn McCall   case CK_FloatingRealToComplex:
333f3735e01SJohn McCall   case CK_FloatingComplexToReal:
334f3735e01SJohn McCall   case CK_FloatingComplexToBoolean:
335f3735e01SJohn McCall   case CK_FloatingComplexCast:
336f3735e01SJohn McCall   case CK_FloatingComplexToIntegralComplex:
337f3735e01SJohn McCall   case CK_IntegralRealToComplex:
338f3735e01SJohn McCall   case CK_IntegralComplexToReal:
339f3735e01SJohn McCall   case CK_IntegralComplexToBoolean:
340f3735e01SJohn McCall   case CK_IntegralComplexCast:
341f3735e01SJohn McCall   case CK_IntegralComplexToFloatingComplex:
342f3735e01SJohn McCall     llvm_unreachable("cast kind invalid for aggregate types");
3431fb7ae9eSAnders Carlsson   }
3447a51313dSChris Lattner }
3457a51313dSChris Lattner 
3460f398c44SChris Lattner void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
347ddcbfe7bSAnders Carlsson   if (E->getCallReturnType()->isReferenceType()) {
348ddcbfe7bSAnders Carlsson     EmitAggLoadOfLValue(E);
349ddcbfe7bSAnders Carlsson     return;
350ddcbfe7bSAnders Carlsson   }
351ddcbfe7bSAnders Carlsson 
352cc04e9f6SJohn McCall   RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
353cc04e9f6SJohn McCall   EmitGCMove(E, RV);
3547a51313dSChris Lattner }
3550f398c44SChris Lattner 
3560f398c44SChris Lattner void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
357cc04e9f6SJohn McCall   RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
358cc04e9f6SJohn McCall   EmitGCMove(E, RV);
359b1d329daSChris Lattner }
3607a51313dSChris Lattner 
36155310df7SDaniel Dunbar void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
36234376a68SJohn McCall   llvm_unreachable("direct property access not surrounded by "
36334376a68SJohn McCall                    "lvalue-to-rvalue cast");
36455310df7SDaniel Dunbar }
36555310df7SDaniel Dunbar 
3660f398c44SChris Lattner void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
367a2342eb8SJohn McCall   CGF.EmitIgnoredExpr(E->getLHS());
3687a626f63SJohn McCall   Visit(E->getRHS());
3694b0e2a30SEli Friedman }
3704b0e2a30SEli Friedman 
3717a51313dSChris Lattner void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
372ce1de617SJohn McCall   CodeGenFunction::StmtExprEvaluation eval(CGF);
3737a626f63SJohn McCall   CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
3747a51313dSChris Lattner }
3757a51313dSChris Lattner 
3767a51313dSChris Lattner void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
377e302792bSJohn McCall   if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
378ffba662dSFariborz Jahanian     VisitPointerToDataMemberBinaryOperator(E);
379ffba662dSFariborz Jahanian   else
380a7c8cf62SDaniel Dunbar     CGF.ErrorUnsupported(E, "aggregate binary expression");
3817a51313dSChris Lattner }
3827a51313dSChris Lattner 
383ffba662dSFariborz Jahanian void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
384ffba662dSFariborz Jahanian                                                     const BinaryOperator *E) {
385ffba662dSFariborz Jahanian   LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
386ffba662dSFariborz Jahanian   EmitFinalDestCopy(E, LV);
387ffba662dSFariborz Jahanian }
388ffba662dSFariborz Jahanian 
3897a51313dSChris Lattner void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
3907a51313dSChris Lattner   // For an assignment to work, the value on the right has
3917a51313dSChris Lattner   // to be compatible with the value on the left.
3922a69547fSEli Friedman   assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
3932a69547fSEli Friedman                                                  E->getRHS()->getType())
3947a51313dSChris Lattner          && "Invalid assignment");
395d0a30016SJohn McCall 
396*99514b91SFariborz Jahanian   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->getLHS()))
397*99514b91SFariborz Jahanian     if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
398*99514b91SFariborz Jahanian       if (VD->hasAttr<BlocksAttr>() &&
399*99514b91SFariborz Jahanian           E->getRHS()->HasSideEffects(CGF.getContext())) {
400*99514b91SFariborz Jahanian         // When __block variable on LHS, the RHS must be evaluated first
401*99514b91SFariborz Jahanian         // as it may change the 'forwarding' field via call to Block_copy.
402*99514b91SFariborz Jahanian         LValue RHS = CGF.EmitLValue(E->getRHS());
403*99514b91SFariborz Jahanian         LValue LHS = CGF.EmitLValue(E->getLHS());
404*99514b91SFariborz Jahanian         bool GCollection = false;
405*99514b91SFariborz Jahanian         if (CGF.getContext().getLangOptions().getGCMode())
406*99514b91SFariborz Jahanian           GCollection = TypeRequiresGCollection(E->getLHS()->getType());
407*99514b91SFariborz Jahanian         // Codegen the RHS so that it stores directly into the LHS.
408*99514b91SFariborz Jahanian         Dest = AggValueSlot::forLValue(LHS, true, GCollection);
409*99514b91SFariborz Jahanian         EmitFinalDestCopy(E, RHS, true);
410*99514b91SFariborz Jahanian         return;
411*99514b91SFariborz Jahanian       }
412*99514b91SFariborz Jahanian     }
413*99514b91SFariborz Jahanian 
4147a51313dSChris Lattner   LValue LHS = CGF.EmitLValue(E->getLHS());
4157a51313dSChris Lattner 
4164b8c6db9SDaniel Dunbar   // We have to special case property setters, otherwise we must have
4174b8c6db9SDaniel Dunbar   // a simple lvalue (no aggregates inside vectors, bitfields).
4184b8c6db9SDaniel Dunbar   if (LHS.isPropertyRef()) {
4197a26ba4dSFariborz Jahanian     const ObjCPropertyRefExpr *RE = LHS.getPropertyRefExpr();
4207a26ba4dSFariborz Jahanian     QualType ArgType = RE->getSetterArgType();
4217a26ba4dSFariborz Jahanian     RValue Src;
4227a26ba4dSFariborz Jahanian     if (ArgType->isReferenceType())
4237a26ba4dSFariborz Jahanian       Src = CGF.EmitReferenceBindingToExpr(E->getRHS(), 0);
4247a26ba4dSFariborz Jahanian     else {
4257a626f63SJohn McCall       AggValueSlot Slot = EnsureSlot(E->getRHS()->getType());
4267a626f63SJohn McCall       CGF.EmitAggExpr(E->getRHS(), Slot);
4277a26ba4dSFariborz Jahanian       Src = Slot.asRValue();
4287a26ba4dSFariborz Jahanian     }
4297a26ba4dSFariborz Jahanian     CGF.EmitStoreThroughPropertyRefLValue(Src, LHS);
4304b8c6db9SDaniel Dunbar   } else {
431b60e70f9SFariborz Jahanian     bool GCollection = false;
432cc04e9f6SJohn McCall     if (CGF.getContext().getLangOptions().getGCMode())
433b60e70f9SFariborz Jahanian       GCollection = TypeRequiresGCollection(E->getLHS()->getType());
434cc04e9f6SJohn McCall 
4357a51313dSChris Lattner     // Codegen the RHS so that it stores directly into the LHS.
436b60e70f9SFariborz Jahanian     AggValueSlot LHSSlot = AggValueSlot::forLValue(LHS, true,
437b60e70f9SFariborz Jahanian                                                    GCollection);
438b60e70f9SFariborz Jahanian     CGF.EmitAggExpr(E->getRHS(), LHSSlot, false);
439ec3cbfe8SMike Stump     EmitFinalDestCopy(E, LHS, true);
4407a51313dSChris Lattner   }
4414b8c6db9SDaniel Dunbar }
4427a51313dSChris Lattner 
443c07a0c7eSJohn McCall void AggExprEmitter::
444c07a0c7eSJohn McCall VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
445a612e79bSDaniel Dunbar   llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
446a612e79bSDaniel Dunbar   llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
447a612e79bSDaniel Dunbar   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
4487a51313dSChris Lattner 
449c07a0c7eSJohn McCall   // Bind the common expression if necessary.
450c07a0c7eSJohn McCall   CodeGenFunction::OpaqueValueMapping binding(CGF, E);
451c07a0c7eSJohn McCall 
452ce1de617SJohn McCall   CodeGenFunction::ConditionalEvaluation eval(CGF);
453b8841af8SEli Friedman   CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
4547a51313dSChris Lattner 
4555b26f65bSJohn McCall   // Save whether the destination's lifetime is externally managed.
4565b26f65bSJohn McCall   bool DestLifetimeManaged = Dest.isLifetimeExternallyManaged();
4577a51313dSChris Lattner 
458ce1de617SJohn McCall   eval.begin(CGF);
459ce1de617SJohn McCall   CGF.EmitBlock(LHSBlock);
460c07a0c7eSJohn McCall   Visit(E->getTrueExpr());
461ce1de617SJohn McCall   eval.end(CGF);
4627a51313dSChris Lattner 
463ce1de617SJohn McCall   assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!");
464ce1de617SJohn McCall   CGF.Builder.CreateBr(ContBlock);
4657a51313dSChris Lattner 
4665b26f65bSJohn McCall   // If the result of an agg expression is unused, then the emission
4675b26f65bSJohn McCall   // of the LHS might need to create a destination slot.  That's fine
4685b26f65bSJohn McCall   // with us, and we can safely emit the RHS into the same slot, but
4695b26f65bSJohn McCall   // we shouldn't claim that its lifetime is externally managed.
4705b26f65bSJohn McCall   Dest.setLifetimeExternallyManaged(DestLifetimeManaged);
4715b26f65bSJohn McCall 
472ce1de617SJohn McCall   eval.begin(CGF);
473ce1de617SJohn McCall   CGF.EmitBlock(RHSBlock);
474c07a0c7eSJohn McCall   Visit(E->getFalseExpr());
475ce1de617SJohn McCall   eval.end(CGF);
4767a51313dSChris Lattner 
4777a51313dSChris Lattner   CGF.EmitBlock(ContBlock);
4787a51313dSChris Lattner }
4797a51313dSChris Lattner 
4805b2095ceSAnders Carlsson void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
4815b2095ceSAnders Carlsson   Visit(CE->getChosenSubExpr(CGF.getContext()));
4825b2095ceSAnders Carlsson }
4835b2095ceSAnders Carlsson 
48421911e89SEli Friedman void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
485e9fcadd2SDaniel Dunbar   llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
48613abd7e9SAnders Carlsson   llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
48713abd7e9SAnders Carlsson 
488020cddcfSSebastian Redl   if (!ArgPtr) {
48913abd7e9SAnders Carlsson     CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
490020cddcfSSebastian Redl     return;
491020cddcfSSebastian Redl   }
49213abd7e9SAnders Carlsson 
4932e442a00SDaniel Dunbar   EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType()));
49421911e89SEli Friedman }
49521911e89SEli Friedman 
4963be22e27SAnders Carlsson void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
4977a626f63SJohn McCall   // Ensure that we have a slot, but if we already do, remember
4987a626f63SJohn McCall   // whether its lifetime was externally managed.
4997a626f63SJohn McCall   bool WasManaged = Dest.isLifetimeExternallyManaged();
5007a626f63SJohn McCall   Dest = EnsureSlot(E->getType());
5017a626f63SJohn McCall   Dest.setLifetimeExternallyManaged();
5023be22e27SAnders Carlsson 
5033be22e27SAnders Carlsson   Visit(E->getSubExpr());
5043be22e27SAnders Carlsson 
5057a626f63SJohn McCall   // Set up the temporary's destructor if its lifetime wasn't already
5067a626f63SJohn McCall   // being managed.
5077a626f63SJohn McCall   if (!WasManaged)
5087a626f63SJohn McCall     CGF.EmitCXXTemporary(E->getTemporary(), Dest.getAddr());
5093be22e27SAnders Carlsson }
5103be22e27SAnders Carlsson 
511b7f8f594SAnders Carlsson void
5121619a504SAnders Carlsson AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
5137a626f63SJohn McCall   AggValueSlot Slot = EnsureSlot(E->getType());
5147a626f63SJohn McCall   CGF.EmitCXXConstructExpr(E, Slot);
515c82b86dfSAnders Carlsson }
516c82b86dfSAnders Carlsson 
5175d413781SJohn McCall void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {
5185d413781SJohn McCall   CGF.EmitExprWithCleanups(E, Dest);
519b7f8f594SAnders Carlsson }
520b7f8f594SAnders Carlsson 
521747eb784SDouglas Gregor void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5227a626f63SJohn McCall   QualType T = E->getType();
5237a626f63SJohn McCall   AggValueSlot Slot = EnsureSlot(T);
5247a626f63SJohn McCall   EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T);
52518ada985SAnders Carlsson }
52618ada985SAnders Carlsson 
52718ada985SAnders Carlsson void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5287a626f63SJohn McCall   QualType T = E->getType();
5297a626f63SJohn McCall   AggValueSlot Slot = EnsureSlot(T);
5307a626f63SJohn McCall   EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T);
531ff3507b9SNuno Lopes }
532ff3507b9SNuno Lopes 
53327a3631bSChris Lattner /// isSimpleZero - If emitting this value will obviously just cause a store of
53427a3631bSChris Lattner /// zero to memory, return true.  This can return false if uncertain, so it just
53527a3631bSChris Lattner /// handles simple cases.
53627a3631bSChris Lattner static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) {
53791147596SPeter Collingbourne   E = E->IgnoreParens();
53891147596SPeter Collingbourne 
53927a3631bSChris Lattner   // 0
54027a3631bSChris Lattner   if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E))
54127a3631bSChris Lattner     return IL->getValue() == 0;
54227a3631bSChris Lattner   // +0.0
54327a3631bSChris Lattner   if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E))
54427a3631bSChris Lattner     return FL->getValue().isPosZero();
54527a3631bSChris Lattner   // int()
54627a3631bSChris Lattner   if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) &&
54727a3631bSChris Lattner       CGF.getTypes().isZeroInitializable(E->getType()))
54827a3631bSChris Lattner     return true;
54927a3631bSChris Lattner   // (int*)0 - Null pointer expressions.
55027a3631bSChris Lattner   if (const CastExpr *ICE = dyn_cast<CastExpr>(E))
55127a3631bSChris Lattner     return ICE->getCastKind() == CK_NullToPointer;
55227a3631bSChris Lattner   // '\0'
55327a3631bSChris Lattner   if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E))
55427a3631bSChris Lattner     return CL->getValue() == 0;
55527a3631bSChris Lattner 
55627a3631bSChris Lattner   // Otherwise, hard case: conservatively return false.
55727a3631bSChris Lattner   return false;
55827a3631bSChris Lattner }
55927a3631bSChris Lattner 
56027a3631bSChris Lattner 
561b247350eSAnders Carlsson void
562b247350eSAnders Carlsson AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) {
563df0fe27bSMike Stump   // FIXME: Ignore result?
564579a05d7SChris Lattner   // FIXME: Are initializers affected by volatile?
56527a3631bSChris Lattner   if (Dest.isZeroed() && isSimpleZero(E, CGF)) {
56627a3631bSChris Lattner     // Storing "i32 0" to a zero'd memory location is a noop.
56727a3631bSChris Lattner   } else if (isa<ImplicitValueInitExpr>(E)) {
568b247350eSAnders Carlsson     EmitNullInitializationToLValue(LV, T);
56966498388SAnders Carlsson   } else if (T->isReferenceType()) {
57004775f84SAnders Carlsson     RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
57166498388SAnders Carlsson     CGF.EmitStoreThroughLValue(RV, LV, T);
572b247350eSAnders Carlsson   } else if (T->isAnyComplexType()) {
5730202cb40SDouglas Gregor     CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
574b247350eSAnders Carlsson   } else if (CGF.hasAggregateLLVMType(T)) {
57527a3631bSChris Lattner     CGF.EmitAggExpr(E, AggValueSlot::forAddr(LV.getAddress(), false, true,
57627a3631bSChris Lattner                                              false, Dest.isZeroed()));
5776e313210SEli Friedman   } else {
578a2342eb8SJohn McCall     CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV, T);
5797a51313dSChris Lattner   }
580579a05d7SChris Lattner }
581579a05d7SChris Lattner 
582579a05d7SChris Lattner void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
58327a3631bSChris Lattner   // If the destination slot is already zeroed out before the aggregate is
58427a3631bSChris Lattner   // copied into it, we don't have to emit any zeros here.
58527a3631bSChris Lattner   if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(T))
58627a3631bSChris Lattner     return;
58727a3631bSChris Lattner 
588579a05d7SChris Lattner   if (!CGF.hasAggregateLLVMType(T)) {
589579a05d7SChris Lattner     // For non-aggregates, we can store zero
5900b75f23bSOwen Anderson     llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
591e8bdce44SDaniel Dunbar     CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
592579a05d7SChris Lattner   } else {
593579a05d7SChris Lattner     // There's a potential optimization opportunity in combining
594579a05d7SChris Lattner     // memsets; that would be easy for arrays, but relatively
595579a05d7SChris Lattner     // difficult for structures with the current code.
596c0964b60SAnders Carlsson     CGF.EmitNullInitialization(LV.getAddress(), T);
597579a05d7SChris Lattner   }
598579a05d7SChris Lattner }
599579a05d7SChris Lattner 
600579a05d7SChris Lattner void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
601f5d08c9eSEli Friedman #if 0
6026d11ec8cSEli Friedman   // FIXME: Assess perf here?  Figure out what cases are worth optimizing here
6036d11ec8cSEli Friedman   // (Length of globals? Chunks of zeroed-out space?).
604f5d08c9eSEli Friedman   //
60518bb9284SMike Stump   // If we can, prefer a copy from a global; this is a lot less code for long
60618bb9284SMike Stump   // globals, and it's easier for the current optimizers to analyze.
6076d11ec8cSEli Friedman   if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
608c59bb48eSEli Friedman     llvm::GlobalVariable* GV =
6096d11ec8cSEli Friedman     new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
6106d11ec8cSEli Friedman                              llvm::GlobalValue::InternalLinkage, C, "");
6112e442a00SDaniel Dunbar     EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType()));
612c59bb48eSEli Friedman     return;
613c59bb48eSEli Friedman   }
614f5d08c9eSEli Friedman #endif
615f53c0968SChris Lattner   if (E->hadArrayRangeDesignator())
616bf7207a1SDouglas Gregor     CGF.ErrorUnsupported(E, "GNU array range designator extension");
617bf7207a1SDouglas Gregor 
6187a626f63SJohn McCall   llvm::Value *DestPtr = Dest.getAddr();
6197a626f63SJohn McCall 
620579a05d7SChris Lattner   // Handle initialization of an array.
621579a05d7SChris Lattner   if (E->getType()->isArrayType()) {
622579a05d7SChris Lattner     const llvm::PointerType *APType =
623579a05d7SChris Lattner       cast<llvm::PointerType>(DestPtr->getType());
624579a05d7SChris Lattner     const llvm::ArrayType *AType =
625579a05d7SChris Lattner       cast<llvm::ArrayType>(APType->getElementType());
626579a05d7SChris Lattner 
627579a05d7SChris Lattner     uint64_t NumInitElements = E->getNumInits();
628f23b6fa4SEli Friedman 
6290f398c44SChris Lattner     if (E->getNumInits() > 0) {
6300f398c44SChris Lattner       QualType T1 = E->getType();
6310f398c44SChris Lattner       QualType T2 = E->getInit(0)->getType();
6322a69547fSEli Friedman       if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
633f23b6fa4SEli Friedman         EmitAggLoadOfLValue(E->getInit(0));
634f23b6fa4SEli Friedman         return;
635f23b6fa4SEli Friedman       }
6360f398c44SChris Lattner     }
637f23b6fa4SEli Friedman 
638579a05d7SChris Lattner     uint64_t NumArrayElements = AType->getNumElements();
6397adf0760SChris Lattner     QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
6407adf0760SChris Lattner     ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
641579a05d7SChris Lattner 
642e07425a5SArgyrios Kyrtzidis     bool hasNonTrivialCXXConstructor = false;
643e07425a5SArgyrios Kyrtzidis     if (CGF.getContext().getLangOptions().CPlusPlus)
6449d3c5040SArgyrios Kyrtzidis       if (const RecordType *RT = CGF.getContext()
6459d3c5040SArgyrios Kyrtzidis                         .getBaseElementType(ElementType)->getAs<RecordType>()) {
646e07425a5SArgyrios Kyrtzidis         const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
647e07425a5SArgyrios Kyrtzidis         hasNonTrivialCXXConstructor = !RD->hasTrivialConstructor();
648e07425a5SArgyrios Kyrtzidis       }
649e07425a5SArgyrios Kyrtzidis 
6508ccfcb51SJohn McCall     // FIXME: were we intentionally ignoring address spaces and GC attributes?
651327944b3SEli Friedman 
652579a05d7SChris Lattner     for (uint64_t i = 0; i != NumArrayElements; ++i) {
65327a3631bSChris Lattner       // If we're done emitting initializers and the destination is known-zeroed
65427a3631bSChris Lattner       // then we're done.
65527a3631bSChris Lattner       if (i == NumInitElements &&
65627a3631bSChris Lattner           Dest.isZeroed() &&
657e07425a5SArgyrios Kyrtzidis           CGF.getTypes().isZeroInitializable(ElementType) &&
658e07425a5SArgyrios Kyrtzidis           !hasNonTrivialCXXConstructor)
65927a3631bSChris Lattner         break;
66027a3631bSChris Lattner 
661579a05d7SChris Lattner       llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
662f6fb7e2bSDaniel Dunbar       LValue LV = CGF.MakeAddrLValue(NextVal, ElementType);
66327a3631bSChris Lattner 
664579a05d7SChris Lattner       if (i < NumInitElements)
665f6fb7e2bSDaniel Dunbar         EmitInitializationToLValue(E->getInit(i), LV, ElementType);
666b2ed28eaSArgyrios Kyrtzidis       else if (Expr *filler = E->getArrayFiller())
667b2ed28eaSArgyrios Kyrtzidis         EmitInitializationToLValue(filler, LV, ElementType);
668579a05d7SChris Lattner       else
669f6fb7e2bSDaniel Dunbar         EmitNullInitializationToLValue(LV, ElementType);
67027a3631bSChris Lattner 
67127a3631bSChris Lattner       // If the GEP didn't get used because of a dead zero init or something
67227a3631bSChris Lattner       // else, clean it up for -O0 builds and general tidiness.
67327a3631bSChris Lattner       if (llvm::GetElementPtrInst *GEP =
67427a3631bSChris Lattner             dyn_cast<llvm::GetElementPtrInst>(NextVal))
67527a3631bSChris Lattner         if (GEP->use_empty())
67627a3631bSChris Lattner           GEP->eraseFromParent();
677579a05d7SChris Lattner     }
678579a05d7SChris Lattner     return;
679579a05d7SChris Lattner   }
680579a05d7SChris Lattner 
681579a05d7SChris Lattner   assert(E->getType()->isRecordType() && "Only support structs/unions here!");
682579a05d7SChris Lattner 
683579a05d7SChris Lattner   // Do struct initialization; this code just sets each individual member
684579a05d7SChris Lattner   // to the approprate value.  This makes bitfield support automatic;
685579a05d7SChris Lattner   // the disadvantage is that the generated code is more difficult for
686579a05d7SChris Lattner   // the optimizer, especially with bitfields.
687579a05d7SChris Lattner   unsigned NumInitElements = E->getNumInits();
688c23c7e6aSTed Kremenek   RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
68952bcf963SChris Lattner 
6905169570eSDouglas Gregor   if (E->getType()->isUnionType()) {
6915169570eSDouglas Gregor     // Only initialize one field of a union. The field itself is
6925169570eSDouglas Gregor     // specified by the initializer list.
6935169570eSDouglas Gregor     if (!E->getInitializedFieldInUnion()) {
6945169570eSDouglas Gregor       // Empty union; we have nothing to do.
6955169570eSDouglas Gregor 
6965169570eSDouglas Gregor #ifndef NDEBUG
6975169570eSDouglas Gregor       // Make sure that it's really an empty and not a failure of
6985169570eSDouglas Gregor       // semantic analysis.
699cfbfe78eSArgyrios Kyrtzidis       for (RecordDecl::field_iterator Field = SD->field_begin(),
700cfbfe78eSArgyrios Kyrtzidis                                    FieldEnd = SD->field_end();
7015169570eSDouglas Gregor            Field != FieldEnd; ++Field)
7025169570eSDouglas Gregor         assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
7035169570eSDouglas Gregor #endif
7045169570eSDouglas Gregor       return;
7055169570eSDouglas Gregor     }
7065169570eSDouglas Gregor 
7075169570eSDouglas Gregor     // FIXME: volatility
7085169570eSDouglas Gregor     FieldDecl *Field = E->getInitializedFieldInUnion();
7095169570eSDouglas Gregor 
71027a3631bSChris Lattner     LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0);
7115169570eSDouglas Gregor     if (NumInitElements) {
7125169570eSDouglas Gregor       // Store the initializer into the field
713b247350eSAnders Carlsson       EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType());
7145169570eSDouglas Gregor     } else {
71527a3631bSChris Lattner       // Default-initialize to null.
7165169570eSDouglas Gregor       EmitNullInitializationToLValue(FieldLoc, Field->getType());
7175169570eSDouglas Gregor     }
7185169570eSDouglas Gregor 
7195169570eSDouglas Gregor     return;
7205169570eSDouglas Gregor   }
721579a05d7SChris Lattner 
722579a05d7SChris Lattner   // Here we iterate over the fields; this makes it simpler to both
723579a05d7SChris Lattner   // default-initialize fields and skip over unnamed fields.
72452bcf963SChris Lattner   unsigned CurInitVal = 0;
725cfbfe78eSArgyrios Kyrtzidis   for (RecordDecl::field_iterator Field = SD->field_begin(),
726cfbfe78eSArgyrios Kyrtzidis                                FieldEnd = SD->field_end();
72791f84216SDouglas Gregor        Field != FieldEnd; ++Field) {
72891f84216SDouglas Gregor     // We're done once we hit the flexible array member
72991f84216SDouglas Gregor     if (Field->getType()->isIncompleteArrayType())
73091f84216SDouglas Gregor       break;
73191f84216SDouglas Gregor 
73217bd094aSDouglas Gregor     if (Field->isUnnamedBitfield())
733579a05d7SChris Lattner       continue;
73417bd094aSDouglas Gregor 
73527a3631bSChris Lattner     // Don't emit GEP before a noop store of zero.
73627a3631bSChris Lattner     if (CurInitVal == NumInitElements && Dest.isZeroed() &&
73727a3631bSChris Lattner         CGF.getTypes().isZeroInitializable(E->getType()))
73827a3631bSChris Lattner       break;
73927a3631bSChris Lattner 
740327944b3SEli Friedman     // FIXME: volatility
74166498388SAnders Carlsson     LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0);
7427c1baf46SFariborz Jahanian     // We never generate write-barries for initialized fields.
743e50dda95SDaniel Dunbar     FieldLoc.setNonGC(true);
74427a3631bSChris Lattner 
745579a05d7SChris Lattner     if (CurInitVal < NumInitElements) {
746e18aaf2cSChris Lattner       // Store the initializer into the field.
747b247350eSAnders Carlsson       EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc,
748b247350eSAnders Carlsson                                  Field->getType());
749579a05d7SChris Lattner     } else {
750579a05d7SChris Lattner       // We're out of initalizers; default-initialize to null
75191f84216SDouglas Gregor       EmitNullInitializationToLValue(FieldLoc, Field->getType());
752579a05d7SChris Lattner     }
75327a3631bSChris Lattner 
75427a3631bSChris Lattner     // If the GEP didn't get used because of a dead zero init or something
75527a3631bSChris Lattner     // else, clean it up for -O0 builds and general tidiness.
75627a3631bSChris Lattner     if (FieldLoc.isSimple())
75727a3631bSChris Lattner       if (llvm::GetElementPtrInst *GEP =
75827a3631bSChris Lattner             dyn_cast<llvm::GetElementPtrInst>(FieldLoc.getAddress()))
75927a3631bSChris Lattner         if (GEP->use_empty())
76027a3631bSChris Lattner           GEP->eraseFromParent();
7617a51313dSChris Lattner   }
7627a51313dSChris Lattner }
7637a51313dSChris Lattner 
7647a51313dSChris Lattner //===----------------------------------------------------------------------===//
7657a51313dSChris Lattner //                        Entry Points into this File
7667a51313dSChris Lattner //===----------------------------------------------------------------------===//
7677a51313dSChris Lattner 
76827a3631bSChris Lattner /// GetNumNonZeroBytesInInit - Get an approximate count of the number of
76927a3631bSChris Lattner /// non-zero bytes that will be stored when outputting the initializer for the
77027a3631bSChris Lattner /// specified initializer expression.
771df94cb7dSKen Dyck static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
77291147596SPeter Collingbourne   E = E->IgnoreParens();
77327a3631bSChris Lattner 
77427a3631bSChris Lattner   // 0 and 0.0 won't require any non-zero stores!
775df94cb7dSKen Dyck   if (isSimpleZero(E, CGF)) return CharUnits::Zero();
77627a3631bSChris Lattner 
77727a3631bSChris Lattner   // If this is an initlist expr, sum up the size of sizes of the (present)
77827a3631bSChris Lattner   // elements.  If this is something weird, assume the whole thing is non-zero.
77927a3631bSChris Lattner   const InitListExpr *ILE = dyn_cast<InitListExpr>(E);
78027a3631bSChris Lattner   if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType()))
781df94cb7dSKen Dyck     return CGF.getContext().getTypeSizeInChars(E->getType());
78227a3631bSChris Lattner 
783c5cc2fb9SChris Lattner   // InitListExprs for structs have to be handled carefully.  If there are
784c5cc2fb9SChris Lattner   // reference members, we need to consider the size of the reference, not the
785c5cc2fb9SChris Lattner   // referencee.  InitListExprs for unions and arrays can't have references.
7865cd84755SChris Lattner   if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
7875cd84755SChris Lattner     if (!RT->isUnionType()) {
788c5cc2fb9SChris Lattner       RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
789df94cb7dSKen Dyck       CharUnits NumNonZeroBytes = CharUnits::Zero();
790c5cc2fb9SChris Lattner 
791c5cc2fb9SChris Lattner       unsigned ILEElement = 0;
792c5cc2fb9SChris Lattner       for (RecordDecl::field_iterator Field = SD->field_begin(),
793c5cc2fb9SChris Lattner            FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) {
794c5cc2fb9SChris Lattner         // We're done once we hit the flexible array member or run out of
795c5cc2fb9SChris Lattner         // InitListExpr elements.
796c5cc2fb9SChris Lattner         if (Field->getType()->isIncompleteArrayType() ||
797c5cc2fb9SChris Lattner             ILEElement == ILE->getNumInits())
798c5cc2fb9SChris Lattner           break;
799c5cc2fb9SChris Lattner         if (Field->isUnnamedBitfield())
800c5cc2fb9SChris Lattner           continue;
801c5cc2fb9SChris Lattner 
802c5cc2fb9SChris Lattner         const Expr *E = ILE->getInit(ILEElement++);
803c5cc2fb9SChris Lattner 
804c5cc2fb9SChris Lattner         // Reference values are always non-null and have the width of a pointer.
8055cd84755SChris Lattner         if (Field->getType()->isReferenceType())
806df94cb7dSKen Dyck           NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits(
807df94cb7dSKen Dyck               CGF.getContext().Target.getPointerWidth(0));
8085cd84755SChris Lattner         else
809c5cc2fb9SChris Lattner           NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
810c5cc2fb9SChris Lattner       }
811c5cc2fb9SChris Lattner 
812c5cc2fb9SChris Lattner       return NumNonZeroBytes;
813c5cc2fb9SChris Lattner     }
8145cd84755SChris Lattner   }
815c5cc2fb9SChris Lattner 
816c5cc2fb9SChris Lattner 
817df94cb7dSKen Dyck   CharUnits NumNonZeroBytes = CharUnits::Zero();
81827a3631bSChris Lattner   for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
81927a3631bSChris Lattner     NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);
82027a3631bSChris Lattner   return NumNonZeroBytes;
82127a3631bSChris Lattner }
82227a3631bSChris Lattner 
82327a3631bSChris Lattner /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of
82427a3631bSChris Lattner /// zeros in it, emit a memset and avoid storing the individual zeros.
82527a3631bSChris Lattner ///
82627a3631bSChris Lattner static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
82727a3631bSChris Lattner                                      CodeGenFunction &CGF) {
82827a3631bSChris Lattner   // If the slot is already known to be zeroed, nothing to do.  Don't mess with
82927a3631bSChris Lattner   // volatile stores.
83027a3631bSChris Lattner   if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return;
83127a3631bSChris Lattner 
83203535265SArgyrios Kyrtzidis   // C++ objects with a user-declared constructor don't need zero'ing.
83303535265SArgyrios Kyrtzidis   if (CGF.getContext().getLangOptions().CPlusPlus)
83403535265SArgyrios Kyrtzidis     if (const RecordType *RT = CGF.getContext()
83503535265SArgyrios Kyrtzidis                        .getBaseElementType(E->getType())->getAs<RecordType>()) {
83603535265SArgyrios Kyrtzidis       const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
83703535265SArgyrios Kyrtzidis       if (RD->hasUserDeclaredConstructor())
83803535265SArgyrios Kyrtzidis         return;
83903535265SArgyrios Kyrtzidis     }
84003535265SArgyrios Kyrtzidis 
84127a3631bSChris Lattner   // If the type is 16-bytes or smaller, prefer individual stores over memset.
842239a3357SKen Dyck   std::pair<CharUnits, CharUnits> TypeInfo =
843239a3357SKen Dyck     CGF.getContext().getTypeInfoInChars(E->getType());
844239a3357SKen Dyck   if (TypeInfo.first <= CharUnits::fromQuantity(16))
84527a3631bSChris Lattner     return;
84627a3631bSChris Lattner 
84727a3631bSChris Lattner   // Check to see if over 3/4 of the initializer are known to be zero.  If so,
84827a3631bSChris Lattner   // we prefer to emit memset + individual stores for the rest.
849239a3357SKen Dyck   CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF);
850239a3357SKen Dyck   if (NumNonZeroBytes*4 > TypeInfo.first)
85127a3631bSChris Lattner     return;
85227a3631bSChris Lattner 
85327a3631bSChris Lattner   // Okay, it seems like a good idea to use an initial memset, emit the call.
854239a3357SKen Dyck   llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity());
855239a3357SKen Dyck   CharUnits Align = TypeInfo.second;
85627a3631bSChris Lattner 
85727a3631bSChris Lattner   llvm::Value *Loc = Slot.getAddr();
85827a3631bSChris Lattner   const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
85927a3631bSChris Lattner 
86027a3631bSChris Lattner   Loc = CGF.Builder.CreateBitCast(Loc, BP);
861239a3357SKen Dyck   CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal,
862239a3357SKen Dyck                            Align.getQuantity(), false);
86327a3631bSChris Lattner 
86427a3631bSChris Lattner   // Tell the AggExprEmitter that the slot is known zero.
86527a3631bSChris Lattner   Slot.setZeroed();
86627a3631bSChris Lattner }
86727a3631bSChris Lattner 
86827a3631bSChris Lattner 
86927a3631bSChris Lattner 
87027a3631bSChris Lattner 
87125306cacSMike Stump /// EmitAggExpr - Emit the computation of the specified expression of aggregate
87225306cacSMike Stump /// type.  The result is computed into DestPtr.  Note that if DestPtr is null,
87325306cacSMike Stump /// the value of the aggregate expression is not needed.  If VolatileDest is
87425306cacSMike Stump /// true, DestPtr cannot be 0.
8757a626f63SJohn McCall ///
8767a626f63SJohn McCall /// \param IsInitializer - true if this evaluation is initializing an
8777a626f63SJohn McCall /// object whose lifetime is already being managed.
878d0bc7b9dSDaniel Dunbar //
879d0bc7b9dSDaniel Dunbar // FIXME: Take Qualifiers object.
8807a626f63SJohn McCall void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot,
881b60e70f9SFariborz Jahanian                                   bool IgnoreResult) {
8827a51313dSChris Lattner   assert(E && hasAggregateLLVMType(E->getType()) &&
8837a51313dSChris Lattner          "Invalid aggregate expression to emit");
88427a3631bSChris Lattner   assert((Slot.getAddr() != 0 || Slot.isIgnored()) &&
88527a3631bSChris Lattner          "slot has bits but no address");
8867a51313dSChris Lattner 
88727a3631bSChris Lattner   // Optimize the slot if possible.
88827a3631bSChris Lattner   CheckAggExprForMemSetUse(Slot, E, *this);
88927a3631bSChris Lattner 
89027a3631bSChris Lattner   AggExprEmitter(*this, Slot, IgnoreResult).Visit(const_cast<Expr*>(E));
8917a51313dSChris Lattner }
8920bc8e86dSDaniel Dunbar 
893d0bc7b9dSDaniel Dunbar LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
894d0bc7b9dSDaniel Dunbar   assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!");
895a7566f16SDaniel Dunbar   llvm::Value *Temp = CreateMemTemp(E->getType());
8962e442a00SDaniel Dunbar   LValue LV = MakeAddrLValue(Temp, E->getType());
89727a3631bSChris Lattner   EmitAggExpr(E, AggValueSlot::forAddr(Temp, LV.isVolatileQualified(), false));
8982e442a00SDaniel Dunbar   return LV;
899d0bc7b9dSDaniel Dunbar }
900d0bc7b9dSDaniel Dunbar 
9010bc8e86dSDaniel Dunbar void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
9025e9e61b8SMike Stump                                         llvm::Value *SrcPtr, QualType Ty,
9035e9e61b8SMike Stump                                         bool isVolatile) {
9040bc8e86dSDaniel Dunbar   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
9050bc8e86dSDaniel Dunbar 
90616e94af6SAnders Carlsson   if (getContext().getLangOptions().CPlusPlus) {
90716e94af6SAnders Carlsson     if (const RecordType *RT = Ty->getAs<RecordType>()) {
908f22101a0SDouglas Gregor       CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
909f22101a0SDouglas Gregor       assert((Record->hasTrivialCopyConstructor() ||
9106855ba2cSFariborz Jahanian               Record->hasTrivialCopyAssignment()) &&
911f22101a0SDouglas Gregor              "Trying to aggregate-copy a type without a trivial copy "
912f22101a0SDouglas Gregor              "constructor or assignment operator");
913265b8b8dSDouglas Gregor       // Ignore empty classes in C++.
914f22101a0SDouglas Gregor       if (Record->isEmpty())
91516e94af6SAnders Carlsson         return;
91616e94af6SAnders Carlsson     }
91716e94af6SAnders Carlsson   }
91816e94af6SAnders Carlsson 
919ca05dfefSChris Lattner   // Aggregate assignment turns into llvm.memcpy.  This is almost valid per
9203ef668c2SChris Lattner   // C99 6.5.16.1p3, which states "If the value being stored in an object is
9213ef668c2SChris Lattner   // read from another object that overlaps in anyway the storage of the first
9223ef668c2SChris Lattner   // object, then the overlap shall be exact and the two objects shall have
9233ef668c2SChris Lattner   // qualified or unqualified versions of a compatible type."
9243ef668c2SChris Lattner   //
925ca05dfefSChris Lattner   // memcpy is not defined if the source and destination pointers are exactly
9263ef668c2SChris Lattner   // equal, but other compilers do this optimization, and almost every memcpy
9273ef668c2SChris Lattner   // implementation handles this case safely.  If there is a libc that does not
9283ef668c2SChris Lattner   // safely handle this, we can add a target hook.
9290bc8e86dSDaniel Dunbar 
9300bc8e86dSDaniel Dunbar   // Get size and alignment info for this aggregate.
931bb2c2400SKen Dyck   std::pair<CharUnits, CharUnits> TypeInfo =
932bb2c2400SKen Dyck     getContext().getTypeInfoInChars(Ty);
9330bc8e86dSDaniel Dunbar 
9340bc8e86dSDaniel Dunbar   // FIXME: Handle variable sized types.
9350bc8e86dSDaniel Dunbar 
93686736572SMike Stump   // FIXME: If we have a volatile struct, the optimizer can remove what might
93786736572SMike Stump   // appear to be `extra' memory ops:
93886736572SMike Stump   //
93986736572SMike Stump   // volatile struct { int i; } a, b;
94086736572SMike Stump   //
94186736572SMike Stump   // int main() {
94286736572SMike Stump   //   a = b;
94386736572SMike Stump   //   a = b;
94486736572SMike Stump   // }
94586736572SMike Stump   //
946cc2ab0cdSMon P Wang   // we need to use a different call here.  We use isVolatile to indicate when
947ec3cbfe8SMike Stump   // either the source or the destination is volatile.
948cc2ab0cdSMon P Wang 
949cc2ab0cdSMon P Wang   const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
950cb7696cfSChris Lattner   const llvm::Type *DBP =
951ad7c5c16SJohn McCall     llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace());
952cc2ab0cdSMon P Wang   DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp");
953cc2ab0cdSMon P Wang 
954cc2ab0cdSMon P Wang   const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
955cb7696cfSChris Lattner   const llvm::Type *SBP =
956ad7c5c16SJohn McCall     llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace());
957cc2ab0cdSMon P Wang   SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp");
958cc2ab0cdSMon P Wang 
959021510e9SFariborz Jahanian   if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
960021510e9SFariborz Jahanian     RecordDecl *Record = RecordTy->getDecl();
961021510e9SFariborz Jahanian     if (Record->hasObjectMember()) {
962bb2c2400SKen Dyck       CharUnits size = TypeInfo.first;
963021510e9SFariborz Jahanian       const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
964bb2c2400SKen Dyck       llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity());
965021510e9SFariborz Jahanian       CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
966021510e9SFariborz Jahanian                                                     SizeVal);
967021510e9SFariborz Jahanian       return;
968021510e9SFariborz Jahanian     }
969021510e9SFariborz Jahanian   } else if (getContext().getAsArrayType(Ty)) {
970021510e9SFariborz Jahanian     QualType BaseType = getContext().getBaseElementType(Ty);
971021510e9SFariborz Jahanian     if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
972021510e9SFariborz Jahanian       if (RecordTy->getDecl()->hasObjectMember()) {
973bb2c2400SKen Dyck         CharUnits size = TypeInfo.first;
974021510e9SFariborz Jahanian         const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
975bb2c2400SKen Dyck         llvm::Value *SizeVal =
976bb2c2400SKen Dyck           llvm::ConstantInt::get(SizeTy, size.getQuantity());
977021510e9SFariborz Jahanian         CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
978021510e9SFariborz Jahanian                                                       SizeVal);
979021510e9SFariborz Jahanian         return;
980021510e9SFariborz Jahanian       }
981021510e9SFariborz Jahanian     }
982021510e9SFariborz Jahanian   }
983021510e9SFariborz Jahanian 
984acc6b4e2SBenjamin Kramer   Builder.CreateMemCpy(DestPtr, SrcPtr,
985bb2c2400SKen Dyck                        llvm::ConstantInt::get(IntPtrTy,
986bb2c2400SKen Dyck                                               TypeInfo.first.getQuantity()),
987bb2c2400SKen Dyck                        TypeInfo.second.getQuantity(), isVolatile);
9880bc8e86dSDaniel Dunbar }
989