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; 35*7a626f63SJohn McCall AggValueSlot Dest; 36ec3cbfe8SMike Stump bool IgnoreResult; 37879d7266SFariborz Jahanian bool RequiresGCollection; 3878a15113SJohn McCall 3978a15113SJohn McCall ReturnValueSlot getReturnValueSlot() const { 40cc04e9f6SJohn McCall // If the destination slot requires garbage collection, we can't 41cc04e9f6SJohn McCall // use the real return value slot, because we have to use the GC 42cc04e9f6SJohn McCall // API. 43cc04e9f6SJohn McCall if (RequiresGCollection) return ReturnValueSlot(); 44cc04e9f6SJohn McCall 45*7a626f63SJohn McCall return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile()); 46*7a626f63SJohn McCall } 47*7a626f63SJohn McCall 48*7a626f63SJohn McCall AggValueSlot EnsureSlot(QualType T) { 49*7a626f63SJohn McCall if (!Dest.isIgnored()) return Dest; 50*7a626f63SJohn McCall return CGF.CreateAggTemp(T, "agg.tmp.ensured"); 5178a15113SJohn McCall } 52cc04e9f6SJohn McCall 537a51313dSChris Lattner public: 54*7a626f63SJohn McCall AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest, 55*7a626f63SJohn McCall bool ignore, bool requiresGCollection) 56*7a626f63SJohn McCall : CGF(cgf), Builder(CGF.Builder), Dest(Dest), 57*7a626f63SJohn McCall IgnoreResult(ignore), RequiresGCollection(requiresGCollection) { 587a51313dSChris Lattner } 597a51313dSChris Lattner 607a51313dSChris Lattner //===--------------------------------------------------------------------===// 617a51313dSChris Lattner // Utilities 627a51313dSChris Lattner //===--------------------------------------------------------------------===// 637a51313dSChris Lattner 647a51313dSChris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that 657a51313dSChris Lattner /// represents a value lvalue, this method emits the address of the lvalue, 667a51313dSChris Lattner /// then loads the result into DestPtr. 677a51313dSChris Lattner void EmitAggLoadOfLValue(const Expr *E); 687a51313dSChris Lattner 69ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 70ec3cbfe8SMike Stump void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false); 71ec3cbfe8SMike Stump void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false); 72ca9fc09cSMike Stump 73cc04e9f6SJohn McCall void EmitGCMove(const Expr *E, RValue Src); 74cc04e9f6SJohn McCall 75cc04e9f6SJohn McCall bool TypeRequiresGCollection(QualType T); 76cc04e9f6SJohn McCall 777a51313dSChris Lattner //===--------------------------------------------------------------------===// 787a51313dSChris Lattner // Visitor Methods 797a51313dSChris Lattner //===--------------------------------------------------------------------===// 807a51313dSChris Lattner 817a51313dSChris Lattner void VisitStmt(Stmt *S) { 82a7c8cf62SDaniel Dunbar CGF.ErrorUnsupported(S, "aggregate expression"); 837a51313dSChris Lattner } 847a51313dSChris Lattner void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } 853f66b84cSEli Friedman void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 867a51313dSChris Lattner 877a51313dSChris Lattner // l-values. 887a51313dSChris Lattner void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } 897a51313dSChris Lattner void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 907a51313dSChris Lattner void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 91d443c0a0SDaniel Dunbar void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 922f343dd5SChris Lattner void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 932f343dd5SChris Lattner EmitAggLoadOfLValue(E); 942f343dd5SChris Lattner } 957a51313dSChris Lattner void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 967a51313dSChris Lattner EmitAggLoadOfLValue(E); 977a51313dSChris Lattner } 982f343dd5SChris Lattner void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 992f343dd5SChris Lattner EmitAggLoadOfLValue(E); 1002f343dd5SChris Lattner } 1012f343dd5SChris Lattner void VisitPredefinedExpr(const PredefinedExpr *E) { 1022f343dd5SChris Lattner EmitAggLoadOfLValue(E); 1032f343dd5SChris Lattner } 104bc7d67ceSMike Stump 1057a51313dSChris Lattner // Operators. 106ec143777SAnders Carlsson void VisitCastExpr(CastExpr *E); 1077a51313dSChris Lattner void VisitCallExpr(const CallExpr *E); 1087a51313dSChris Lattner void VisitStmtExpr(const StmtExpr *E); 1097a51313dSChris Lattner void VisitBinaryOperator(const BinaryOperator *BO); 110ffba662dSFariborz Jahanian void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); 1117a51313dSChris Lattner void VisitBinAssign(const BinaryOperator *E); 1124b0e2a30SEli Friedman void VisitBinComma(const BinaryOperator *E); 1137a51313dSChris Lattner 114b1d329daSChris Lattner void VisitObjCMessageExpr(ObjCMessageExpr *E); 115c8317a44SDaniel Dunbar void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 116c8317a44SDaniel Dunbar EmitAggLoadOfLValue(E); 117c8317a44SDaniel Dunbar } 11855310df7SDaniel Dunbar void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); 1199a846659SFariborz Jahanian void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E); 1207a51313dSChris Lattner 1217a51313dSChris Lattner void VisitConditionalOperator(const ConditionalOperator *CO); 1225b2095ceSAnders Carlsson void VisitChooseExpr(const ChooseExpr *CE); 1237a51313dSChris Lattner void VisitInitListExpr(InitListExpr *E); 12418ada985SAnders Carlsson void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); 125aa9c7aedSChris Lattner void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 126aa9c7aedSChris Lattner Visit(DAE->getExpr()); 127aa9c7aedSChris Lattner } 1283be22e27SAnders Carlsson void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 1291619a504SAnders Carlsson void VisitCXXConstructExpr(const CXXConstructExpr *E); 130c82b86dfSAnders Carlsson void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E); 131747eb784SDouglas Gregor void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 1325bbbb137SMike Stump void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } 133c82b86dfSAnders Carlsson 13421911e89SEli Friedman void VisitVAArgExpr(VAArgExpr *E); 135579a05d7SChris Lattner 136b247350eSAnders Carlsson void EmitInitializationToLValue(Expr *E, LValue Address, QualType T); 137579a05d7SChris Lattner void EmitNullInitializationToLValue(LValue Address, QualType T); 1387a51313dSChris Lattner // case Expr::ChooseExprClass: 139f16b8c30SMike Stump void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } 1407a51313dSChris Lattner }; 1417a51313dSChris Lattner } // end anonymous namespace. 1427a51313dSChris Lattner 1437a51313dSChris Lattner //===----------------------------------------------------------------------===// 1447a51313dSChris Lattner // Utilities 1457a51313dSChris Lattner //===----------------------------------------------------------------------===// 1467a51313dSChris Lattner 1477a51313dSChris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that 1487a51313dSChris Lattner /// represents a value lvalue, this method emits the address of the lvalue, 1497a51313dSChris Lattner /// then loads the result into DestPtr. 1507a51313dSChris Lattner void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 1517a51313dSChris Lattner LValue LV = CGF.EmitLValue(E); 152ca9fc09cSMike Stump EmitFinalDestCopy(E, LV); 153ca9fc09cSMike Stump } 154ca9fc09cSMike Stump 155cc04e9f6SJohn McCall /// \brief True if the given aggregate type requires special GC API calls. 156cc04e9f6SJohn McCall bool AggExprEmitter::TypeRequiresGCollection(QualType T) { 157cc04e9f6SJohn McCall // Only record types have members that might require garbage collection. 158cc04e9f6SJohn McCall const RecordType *RecordTy = T->getAs<RecordType>(); 159cc04e9f6SJohn McCall if (!RecordTy) return false; 160cc04e9f6SJohn McCall 161cc04e9f6SJohn McCall // Don't mess with non-trivial C++ types. 162cc04e9f6SJohn McCall RecordDecl *Record = RecordTy->getDecl(); 163cc04e9f6SJohn McCall if (isa<CXXRecordDecl>(Record) && 164cc04e9f6SJohn McCall (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() || 165cc04e9f6SJohn McCall !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) 166cc04e9f6SJohn McCall return false; 167cc04e9f6SJohn McCall 168cc04e9f6SJohn McCall // Check whether the type has an object member. 169cc04e9f6SJohn McCall return Record->hasObjectMember(); 170cc04e9f6SJohn McCall } 171cc04e9f6SJohn McCall 172cc04e9f6SJohn McCall /// \brief Perform the final move to DestPtr if RequiresGCollection is set. 173cc04e9f6SJohn McCall /// 174cc04e9f6SJohn McCall /// The idea is that you do something like this: 175cc04e9f6SJohn McCall /// RValue Result = EmitSomething(..., getReturnValueSlot()); 176cc04e9f6SJohn McCall /// EmitGCMove(E, Result); 177cc04e9f6SJohn McCall /// If GC doesn't interfere, this will cause the result to be emitted 178cc04e9f6SJohn McCall /// directly into the return value slot. If GC does interfere, a final 179cc04e9f6SJohn McCall /// move will be performed. 180cc04e9f6SJohn McCall void AggExprEmitter::EmitGCMove(const Expr *E, RValue Src) { 181021510e9SFariborz Jahanian if (RequiresGCollection) { 182021510e9SFariborz Jahanian std::pair<uint64_t, unsigned> TypeInfo = 183021510e9SFariborz Jahanian CGF.getContext().getTypeInfo(E->getType()); 184021510e9SFariborz Jahanian unsigned long size = TypeInfo.first/8; 185021510e9SFariborz Jahanian const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 186021510e9SFariborz Jahanian llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); 187*7a626f63SJohn McCall CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, Dest.getAddr(), 188cc04e9f6SJohn McCall Src.getAggregateAddr(), 189021510e9SFariborz Jahanian SizeVal); 190021510e9SFariborz Jahanian } 191cc04e9f6SJohn McCall } 192cc04e9f6SJohn McCall 193ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 194ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) { 195ca9fc09cSMike Stump assert(Src.isAggregate() && "value must be aggregate value!"); 1967a51313dSChris Lattner 197*7a626f63SJohn McCall // If Dest is ignored, then we're evaluating an aggregate expression 1988d752430SJohn McCall // in a context (like an expression statement) that doesn't care 1998d752430SJohn McCall // about the result. C says that an lvalue-to-rvalue conversion is 2008d752430SJohn McCall // performed in these cases; C++ says that it is not. In either 2018d752430SJohn McCall // case, we don't actually need to do anything unless the value is 2028d752430SJohn McCall // volatile. 203*7a626f63SJohn McCall if (Dest.isIgnored()) { 2048d752430SJohn McCall if (!Src.isVolatileQualified() || 2058d752430SJohn McCall CGF.CGM.getLangOptions().CPlusPlus || 2068d752430SJohn McCall (IgnoreResult && Ignore)) 207ec3cbfe8SMike Stump return; 2088d752430SJohn McCall 209332ec2ceSMike Stump // If the source is volatile, we must read from it; to do that, we need 210332ec2ceSMike Stump // some place to put it. 211*7a626f63SJohn McCall Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp"); 212332ec2ceSMike Stump } 2137a51313dSChris Lattner 214879d7266SFariborz Jahanian if (RequiresGCollection) { 215021510e9SFariborz Jahanian std::pair<uint64_t, unsigned> TypeInfo = 216021510e9SFariborz Jahanian CGF.getContext().getTypeInfo(E->getType()); 217021510e9SFariborz Jahanian unsigned long size = TypeInfo.first/8; 218021510e9SFariborz Jahanian const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 219021510e9SFariborz Jahanian llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); 220879d7266SFariborz Jahanian CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 221*7a626f63SJohn McCall Dest.getAddr(), 222*7a626f63SJohn McCall Src.getAggregateAddr(), 223021510e9SFariborz Jahanian SizeVal); 224879d7266SFariborz Jahanian return; 225879d7266SFariborz Jahanian } 226ca9fc09cSMike Stump // If the result of the assignment is used, copy the LHS there also. 227ca9fc09cSMike Stump // FIXME: Pass VolatileDest as well. I think we also need to merge volatile 228ca9fc09cSMike Stump // from the source as well, as we can't eliminate it if either operand 229ca9fc09cSMike Stump // is volatile, unless copy has volatile for both source and destination.. 230*7a626f63SJohn McCall CGF.EmitAggregateCopy(Dest.getAddr(), Src.getAggregateAddr(), E->getType(), 231*7a626f63SJohn McCall Dest.isVolatile()|Src.isVolatileQualified()); 232ca9fc09cSMike Stump } 233ca9fc09cSMike Stump 234ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 235ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { 236ca9fc09cSMike Stump assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); 237ca9fc09cSMike Stump 238ca9fc09cSMike Stump EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(), 239ec3cbfe8SMike Stump Src.isVolatileQualified()), 240ec3cbfe8SMike Stump Ignore); 2417a51313dSChris Lattner } 2427a51313dSChris Lattner 2437a51313dSChris Lattner //===----------------------------------------------------------------------===// 2447a51313dSChris Lattner // Visitor Methods 2457a51313dSChris Lattner //===----------------------------------------------------------------------===// 2467a51313dSChris Lattner 247ec143777SAnders Carlsson void AggExprEmitter::VisitCastExpr(CastExpr *E) { 248*7a626f63SJohn McCall if (Dest.isIgnored() && E->getCastKind() != CK_Dynamic) { 249c934bc84SDouglas Gregor Visit(E->getSubExpr()); 250c934bc84SDouglas Gregor return; 251c934bc84SDouglas Gregor } 252c934bc84SDouglas Gregor 2531fb7ae9eSAnders Carlsson switch (E->getCastKind()) { 2541fb7ae9eSAnders Carlsson default: assert(0 && "Unhandled cast kind!"); 2551fb7ae9eSAnders Carlsson 256e302792bSJohn McCall case CK_Dynamic: { 2571c073f47SDouglas Gregor assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?"); 2581c073f47SDouglas Gregor LValue LV = CGF.EmitCheckedLValue(E->getSubExpr()); 2591c073f47SDouglas Gregor // FIXME: Do we also need to handle property references here? 2601c073f47SDouglas Gregor if (LV.isSimple()) 2611c073f47SDouglas Gregor CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E)); 2621c073f47SDouglas Gregor else 2631c073f47SDouglas Gregor CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast"); 2641c073f47SDouglas Gregor 265*7a626f63SJohn McCall if (!Dest.isIgnored()) 2661c073f47SDouglas Gregor CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination"); 2671c073f47SDouglas Gregor break; 2681c073f47SDouglas Gregor } 2691c073f47SDouglas Gregor 270e302792bSJohn McCall case CK_ToUnion: { 2717ffcf93bSNuno Lopes // GCC union extension 2722e442a00SDaniel Dunbar QualType Ty = E->getSubExpr()->getType(); 2732e442a00SDaniel Dunbar QualType PtrTy = CGF.getContext().getPointerType(Ty); 274*7a626f63SJohn McCall llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(), 275dd274848SEli Friedman CGF.ConvertType(PtrTy)); 2762e442a00SDaniel Dunbar EmitInitializationToLValue(E->getSubExpr(), CGF.MakeAddrLValue(CastPtr, Ty), 2772e442a00SDaniel Dunbar Ty); 2781fb7ae9eSAnders Carlsson break; 2797ffcf93bSNuno Lopes } 2807ffcf93bSNuno Lopes 281e302792bSJohn McCall case CK_DerivedToBase: 282e302792bSJohn McCall case CK_BaseToDerived: 283e302792bSJohn McCall case CK_UncheckedDerivedToBase: { 284aae38d66SDouglas Gregor assert(0 && "cannot perform hierarchy conversion in EmitAggExpr: " 285aae38d66SDouglas Gregor "should have been unpacked before we got here"); 286aae38d66SDouglas Gregor break; 287aae38d66SDouglas Gregor } 288aae38d66SDouglas Gregor 289ec143777SAnders Carlsson // FIXME: Remove the CK_Unknown check here. 290e302792bSJohn McCall case CK_Unknown: 291e302792bSJohn McCall case CK_NoOp: 292e302792bSJohn McCall case CK_UserDefinedConversion: 293e302792bSJohn McCall case CK_ConstructorConversion: 2942a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 2952a69547fSEli Friedman E->getType()) && 2960f398c44SChris Lattner "Implicit cast types must be compatible"); 2977a51313dSChris Lattner Visit(E->getSubExpr()); 2981fb7ae9eSAnders Carlsson break; 299b05a3e55SAnders Carlsson 300e302792bSJohn McCall case CK_LValueBitCast: 30151954276SDouglas Gregor llvm_unreachable("there are no lvalue bit-casts on aggregates"); 30251954276SDouglas Gregor break; 3031fb7ae9eSAnders Carlsson } 3047a51313dSChris Lattner } 3057a51313dSChris Lattner 3060f398c44SChris Lattner void AggExprEmitter::VisitCallExpr(const CallExpr *E) { 307ddcbfe7bSAnders Carlsson if (E->getCallReturnType()->isReferenceType()) { 308ddcbfe7bSAnders Carlsson EmitAggLoadOfLValue(E); 309ddcbfe7bSAnders Carlsson return; 310ddcbfe7bSAnders Carlsson } 311ddcbfe7bSAnders Carlsson 312cc04e9f6SJohn McCall RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); 313cc04e9f6SJohn McCall EmitGCMove(E, RV); 3147a51313dSChris Lattner } 3150f398c44SChris Lattner 3160f398c44SChris Lattner void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 317cc04e9f6SJohn McCall RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); 318cc04e9f6SJohn McCall EmitGCMove(E, RV); 319b1d329daSChris Lattner } 3207a51313dSChris Lattner 32155310df7SDaniel Dunbar void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 322cc04e9f6SJohn McCall RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot()); 323cc04e9f6SJohn McCall EmitGCMove(E, RV); 32455310df7SDaniel Dunbar } 32555310df7SDaniel Dunbar 3269a846659SFariborz Jahanian void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr( 3279a846659SFariborz Jahanian ObjCImplicitSetterGetterRefExpr *E) { 328cc04e9f6SJohn McCall RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot()); 329cc04e9f6SJohn McCall EmitGCMove(E, RV); 3308a1810f0SFariborz Jahanian } 3318a1810f0SFariborz Jahanian 3320f398c44SChris Lattner void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 333*7a626f63SJohn McCall CGF.EmitAnyExpr(E->getLHS(), AggValueSlot::ignored(), true); 334*7a626f63SJohn McCall Visit(E->getRHS()); 3354b0e2a30SEli Friedman } 3364b0e2a30SEli Friedman 3377a51313dSChris Lattner void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 338*7a626f63SJohn McCall CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest); 3397a51313dSChris Lattner } 3407a51313dSChris Lattner 3417a51313dSChris Lattner void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 342e302792bSJohn McCall if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI) 343ffba662dSFariborz Jahanian VisitPointerToDataMemberBinaryOperator(E); 344ffba662dSFariborz Jahanian else 345a7c8cf62SDaniel Dunbar CGF.ErrorUnsupported(E, "aggregate binary expression"); 3467a51313dSChris Lattner } 3477a51313dSChris Lattner 348ffba662dSFariborz Jahanian void AggExprEmitter::VisitPointerToDataMemberBinaryOperator( 349ffba662dSFariborz Jahanian const BinaryOperator *E) { 350ffba662dSFariborz Jahanian LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); 351ffba662dSFariborz Jahanian EmitFinalDestCopy(E, LV); 352ffba662dSFariborz Jahanian } 353ffba662dSFariborz Jahanian 3547a51313dSChris Lattner void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 3557a51313dSChris Lattner // For an assignment to work, the value on the right has 3567a51313dSChris Lattner // to be compatible with the value on the left. 3572a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 3582a69547fSEli Friedman E->getRHS()->getType()) 3597a51313dSChris Lattner && "Invalid assignment"); 3607a51313dSChris Lattner LValue LHS = CGF.EmitLValue(E->getLHS()); 3617a51313dSChris Lattner 3624b8c6db9SDaniel Dunbar // We have to special case property setters, otherwise we must have 3634b8c6db9SDaniel Dunbar // a simple lvalue (no aggregates inside vectors, bitfields). 3644b8c6db9SDaniel Dunbar if (LHS.isPropertyRef()) { 365*7a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getRHS()->getType()); 366*7a626f63SJohn McCall CGF.EmitAggExpr(E->getRHS(), Slot); 367*7a626f63SJohn McCall CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), Slot.asRValue()); 368658fe02dSMike Stump } else if (LHS.isKVCRef()) { 369*7a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getRHS()->getType()); 370*7a626f63SJohn McCall CGF.EmitAggExpr(E->getRHS(), Slot); 371*7a626f63SJohn McCall CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), Slot.asRValue()); 3724b8c6db9SDaniel Dunbar } else { 373879d7266SFariborz Jahanian bool RequiresGCollection = false; 374cc04e9f6SJohn McCall if (CGF.getContext().getLangOptions().getGCMode()) 375cc04e9f6SJohn McCall RequiresGCollection = TypeRequiresGCollection(E->getLHS()->getType()); 376cc04e9f6SJohn McCall 3777a51313dSChris Lattner // Codegen the RHS so that it stores directly into the LHS. 378*7a626f63SJohn McCall AggValueSlot LHSSlot = AggValueSlot::forLValue(LHS, true); 379*7a626f63SJohn McCall CGF.EmitAggExpr(E->getRHS(), LHSSlot, false, RequiresGCollection); 380ec3cbfe8SMike Stump EmitFinalDestCopy(E, LHS, true); 3817a51313dSChris Lattner } 3824b8c6db9SDaniel Dunbar } 3837a51313dSChris Lattner 3847a51313dSChris Lattner void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { 385b8841af8SEli Friedman if (!E->getLHS()) { 386b8841af8SEli Friedman CGF.ErrorUnsupported(E, "conditional operator with missing LHS"); 387b8841af8SEli Friedman return; 388b8841af8SEli Friedman } 389b8841af8SEli Friedman 390a612e79bSDaniel Dunbar llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 391a612e79bSDaniel Dunbar llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 392a612e79bSDaniel Dunbar llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 3937a51313dSChris Lattner 394b8841af8SEli Friedman CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 3957a51313dSChris Lattner 396ae612d22SAnders Carlsson CGF.BeginConditionalBranch(); 3977a51313dSChris Lattner CGF.EmitBlock(LHSBlock); 3987a51313dSChris Lattner 3997a51313dSChris Lattner // Handle the GNU extension for missing LHS. 4007a51313dSChris Lattner assert(E->getLHS() && "Must have LHS for aggregate value"); 4017a51313dSChris Lattner 4027a51313dSChris Lattner Visit(E->getLHS()); 403ae612d22SAnders Carlsson CGF.EndConditionalBranch(); 404c56e6764SDaniel Dunbar CGF.EmitBranch(ContBlock); 4057a51313dSChris Lattner 406ae612d22SAnders Carlsson CGF.BeginConditionalBranch(); 4077a51313dSChris Lattner CGF.EmitBlock(RHSBlock); 4087a51313dSChris Lattner 4097a51313dSChris Lattner Visit(E->getRHS()); 410ae612d22SAnders Carlsson CGF.EndConditionalBranch(); 411c56e6764SDaniel Dunbar CGF.EmitBranch(ContBlock); 4127a51313dSChris Lattner 4137a51313dSChris Lattner CGF.EmitBlock(ContBlock); 4147a51313dSChris Lattner } 4157a51313dSChris Lattner 4165b2095ceSAnders Carlsson void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 4175b2095ceSAnders Carlsson Visit(CE->getChosenSubExpr(CGF.getContext())); 4185b2095ceSAnders Carlsson } 4195b2095ceSAnders Carlsson 42021911e89SEli Friedman void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 421e9fcadd2SDaniel Dunbar llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 42213abd7e9SAnders Carlsson llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 42313abd7e9SAnders Carlsson 424020cddcfSSebastian Redl if (!ArgPtr) { 42513abd7e9SAnders Carlsson CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 426020cddcfSSebastian Redl return; 427020cddcfSSebastian Redl } 42813abd7e9SAnders Carlsson 4292e442a00SDaniel Dunbar EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType())); 43021911e89SEli Friedman } 43121911e89SEli Friedman 4323be22e27SAnders Carlsson void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 433*7a626f63SJohn McCall // Ensure that we have a slot, but if we already do, remember 434*7a626f63SJohn McCall // whether its lifetime was externally managed. 435*7a626f63SJohn McCall bool WasManaged = Dest.isLifetimeExternallyManaged(); 436*7a626f63SJohn McCall Dest = EnsureSlot(E->getType()); 437*7a626f63SJohn McCall Dest.setLifetimeExternallyManaged(); 4383be22e27SAnders Carlsson 4393be22e27SAnders Carlsson Visit(E->getSubExpr()); 4403be22e27SAnders Carlsson 441*7a626f63SJohn McCall // Set up the temporary's destructor if its lifetime wasn't already 442*7a626f63SJohn McCall // being managed. 443*7a626f63SJohn McCall if (!WasManaged) 444*7a626f63SJohn McCall CGF.EmitCXXTemporary(E->getTemporary(), Dest.getAddr()); 4453be22e27SAnders Carlsson } 4463be22e27SAnders Carlsson 447b7f8f594SAnders Carlsson void 4481619a504SAnders Carlsson AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 449*7a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getType()); 450*7a626f63SJohn McCall CGF.EmitCXXConstructExpr(E, Slot); 451c82b86dfSAnders Carlsson } 452c82b86dfSAnders Carlsson 453c82b86dfSAnders Carlsson void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { 454*7a626f63SJohn McCall CGF.EmitCXXExprWithTemporaries(E, Dest); 455b7f8f594SAnders Carlsson } 456b7f8f594SAnders Carlsson 457747eb784SDouglas Gregor void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 458*7a626f63SJohn McCall QualType T = E->getType(); 459*7a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 460*7a626f63SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T); 46118ada985SAnders Carlsson } 46218ada985SAnders Carlsson 46318ada985SAnders Carlsson void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 464*7a626f63SJohn McCall QualType T = E->getType(); 465*7a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 466*7a626f63SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T); 467ff3507b9SNuno Lopes } 468ff3507b9SNuno Lopes 469b247350eSAnders Carlsson void 470b247350eSAnders Carlsson AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) { 471df0fe27bSMike Stump // FIXME: Ignore result? 472579a05d7SChris Lattner // FIXME: Are initializers affected by volatile? 4730202cb40SDouglas Gregor if (isa<ImplicitValueInitExpr>(E)) { 474b247350eSAnders Carlsson EmitNullInitializationToLValue(LV, T); 47566498388SAnders Carlsson } else if (T->isReferenceType()) { 47604775f84SAnders Carlsson RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); 47766498388SAnders Carlsson CGF.EmitStoreThroughLValue(RV, LV, T); 478b247350eSAnders Carlsson } else if (T->isAnyComplexType()) { 4790202cb40SDouglas Gregor CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); 480b247350eSAnders Carlsson } else if (CGF.hasAggregateLLVMType(T)) { 481*7a626f63SJohn McCall CGF.EmitAggExpr(E, AggValueSlot::forAddr(LV.getAddress(), false, true)); 4826e313210SEli Friedman } else { 483b247350eSAnders Carlsson CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, T); 4847a51313dSChris Lattner } 485579a05d7SChris Lattner } 486579a05d7SChris Lattner 487579a05d7SChris Lattner void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { 488579a05d7SChris Lattner if (!CGF.hasAggregateLLVMType(T)) { 489579a05d7SChris Lattner // For non-aggregates, we can store zero 4900b75f23bSOwen Anderson llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T)); 491e8bdce44SDaniel Dunbar CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T); 492579a05d7SChris Lattner } else { 493579a05d7SChris Lattner // There's a potential optimization opportunity in combining 494579a05d7SChris Lattner // memsets; that would be easy for arrays, but relatively 495579a05d7SChris Lattner // difficult for structures with the current code. 496c0964b60SAnders Carlsson CGF.EmitNullInitialization(LV.getAddress(), T); 497579a05d7SChris Lattner } 498579a05d7SChris Lattner } 499579a05d7SChris Lattner 500579a05d7SChris Lattner void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 501f5d08c9eSEli Friedman #if 0 5026d11ec8cSEli Friedman // FIXME: Assess perf here? Figure out what cases are worth optimizing here 5036d11ec8cSEli Friedman // (Length of globals? Chunks of zeroed-out space?). 504f5d08c9eSEli Friedman // 50518bb9284SMike Stump // If we can, prefer a copy from a global; this is a lot less code for long 50618bb9284SMike Stump // globals, and it's easier for the current optimizers to analyze. 5076d11ec8cSEli Friedman if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { 508c59bb48eSEli Friedman llvm::GlobalVariable* GV = 5096d11ec8cSEli Friedman new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, 5106d11ec8cSEli Friedman llvm::GlobalValue::InternalLinkage, C, ""); 5112e442a00SDaniel Dunbar EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType())); 512c59bb48eSEli Friedman return; 513c59bb48eSEli Friedman } 514f5d08c9eSEli Friedman #endif 515f53c0968SChris Lattner if (E->hadArrayRangeDesignator()) 516bf7207a1SDouglas Gregor CGF.ErrorUnsupported(E, "GNU array range designator extension"); 517bf7207a1SDouglas Gregor 518*7a626f63SJohn McCall llvm::Value *DestPtr = Dest.getAddr(); 519*7a626f63SJohn McCall 520579a05d7SChris Lattner // Handle initialization of an array. 521579a05d7SChris Lattner if (E->getType()->isArrayType()) { 522579a05d7SChris Lattner const llvm::PointerType *APType = 523579a05d7SChris Lattner cast<llvm::PointerType>(DestPtr->getType()); 524579a05d7SChris Lattner const llvm::ArrayType *AType = 525579a05d7SChris Lattner cast<llvm::ArrayType>(APType->getElementType()); 526579a05d7SChris Lattner 527579a05d7SChris Lattner uint64_t NumInitElements = E->getNumInits(); 528f23b6fa4SEli Friedman 5290f398c44SChris Lattner if (E->getNumInits() > 0) { 5300f398c44SChris Lattner QualType T1 = E->getType(); 5310f398c44SChris Lattner QualType T2 = E->getInit(0)->getType(); 5322a69547fSEli Friedman if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { 533f23b6fa4SEli Friedman EmitAggLoadOfLValue(E->getInit(0)); 534f23b6fa4SEli Friedman return; 535f23b6fa4SEli Friedman } 5360f398c44SChris Lattner } 537f23b6fa4SEli Friedman 538579a05d7SChris Lattner uint64_t NumArrayElements = AType->getNumElements(); 5397adf0760SChris Lattner QualType ElementType = CGF.getContext().getCanonicalType(E->getType()); 5407adf0760SChris Lattner ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType(); 541579a05d7SChris Lattner 5428ccfcb51SJohn McCall // FIXME: were we intentionally ignoring address spaces and GC attributes? 543327944b3SEli Friedman 544579a05d7SChris Lattner for (uint64_t i = 0; i != NumArrayElements; ++i) { 545579a05d7SChris Lattner llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); 546f6fb7e2bSDaniel Dunbar LValue LV = CGF.MakeAddrLValue(NextVal, ElementType); 547579a05d7SChris Lattner if (i < NumInitElements) 548f6fb7e2bSDaniel Dunbar EmitInitializationToLValue(E->getInit(i), LV, ElementType); 549f6fb7e2bSDaniel Dunbar 550579a05d7SChris Lattner else 551f6fb7e2bSDaniel Dunbar EmitNullInitializationToLValue(LV, ElementType); 552579a05d7SChris Lattner } 553579a05d7SChris Lattner return; 554579a05d7SChris Lattner } 555579a05d7SChris Lattner 556579a05d7SChris Lattner assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 557579a05d7SChris Lattner 558579a05d7SChris Lattner // Do struct initialization; this code just sets each individual member 559579a05d7SChris Lattner // to the approprate value. This makes bitfield support automatic; 560579a05d7SChris Lattner // the disadvantage is that the generated code is more difficult for 561579a05d7SChris Lattner // the optimizer, especially with bitfields. 562579a05d7SChris Lattner unsigned NumInitElements = E->getNumInits(); 563c23c7e6aSTed Kremenek RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 56452bcf963SChris Lattner 56552bcf963SChris Lattner // If we're initializing the whole aggregate, just do it in place. 56652bcf963SChris Lattner // FIXME: This is a hack around an AST bug (PR6537). 56752bcf963SChris Lattner if (NumInitElements == 1 && E->getType() == E->getInit(0)->getType()) { 56852bcf963SChris Lattner EmitInitializationToLValue(E->getInit(0), 56952bcf963SChris Lattner CGF.MakeAddrLValue(DestPtr, E->getType()), 57052bcf963SChris Lattner E->getType()); 57152bcf963SChris Lattner return; 57252bcf963SChris Lattner } 57352bcf963SChris Lattner 5745169570eSDouglas Gregor 5755169570eSDouglas Gregor if (E->getType()->isUnionType()) { 5765169570eSDouglas Gregor // Only initialize one field of a union. The field itself is 5775169570eSDouglas Gregor // specified by the initializer list. 5785169570eSDouglas Gregor if (!E->getInitializedFieldInUnion()) { 5795169570eSDouglas Gregor // Empty union; we have nothing to do. 5805169570eSDouglas Gregor 5815169570eSDouglas Gregor #ifndef NDEBUG 5825169570eSDouglas Gregor // Make sure that it's really an empty and not a failure of 5835169570eSDouglas Gregor // semantic analysis. 584cfbfe78eSArgyrios Kyrtzidis for (RecordDecl::field_iterator Field = SD->field_begin(), 585cfbfe78eSArgyrios Kyrtzidis FieldEnd = SD->field_end(); 5865169570eSDouglas Gregor Field != FieldEnd; ++Field) 5875169570eSDouglas Gregor assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 5885169570eSDouglas Gregor #endif 5895169570eSDouglas Gregor return; 5905169570eSDouglas Gregor } 5915169570eSDouglas Gregor 5925169570eSDouglas Gregor // FIXME: volatility 5935169570eSDouglas Gregor FieldDecl *Field = E->getInitializedFieldInUnion(); 59466498388SAnders Carlsson LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0); 5955169570eSDouglas Gregor 5965169570eSDouglas Gregor if (NumInitElements) { 5975169570eSDouglas Gregor // Store the initializer into the field 598b247350eSAnders Carlsson EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType()); 5995169570eSDouglas Gregor } else { 6005169570eSDouglas Gregor // Default-initialize to null 6015169570eSDouglas Gregor EmitNullInitializationToLValue(FieldLoc, Field->getType()); 6025169570eSDouglas Gregor } 6035169570eSDouglas Gregor 6045169570eSDouglas Gregor return; 6055169570eSDouglas Gregor } 606579a05d7SChris Lattner 607579a05d7SChris Lattner // Here we iterate over the fields; this makes it simpler to both 608579a05d7SChris Lattner // default-initialize fields and skip over unnamed fields. 60952bcf963SChris Lattner unsigned CurInitVal = 0; 610cfbfe78eSArgyrios Kyrtzidis for (RecordDecl::field_iterator Field = SD->field_begin(), 611cfbfe78eSArgyrios Kyrtzidis FieldEnd = SD->field_end(); 61291f84216SDouglas Gregor Field != FieldEnd; ++Field) { 61391f84216SDouglas Gregor // We're done once we hit the flexible array member 61491f84216SDouglas Gregor if (Field->getType()->isIncompleteArrayType()) 61591f84216SDouglas Gregor break; 61691f84216SDouglas Gregor 61717bd094aSDouglas Gregor if (Field->isUnnamedBitfield()) 618579a05d7SChris Lattner continue; 61917bd094aSDouglas Gregor 620327944b3SEli Friedman // FIXME: volatility 62166498388SAnders Carlsson LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0); 6227c1baf46SFariborz Jahanian // We never generate write-barries for initialized fields. 623e50dda95SDaniel Dunbar FieldLoc.setNonGC(true); 624579a05d7SChris Lattner if (CurInitVal < NumInitElements) { 625e18aaf2cSChris Lattner // Store the initializer into the field. 626b247350eSAnders Carlsson EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc, 627b247350eSAnders Carlsson Field->getType()); 628579a05d7SChris Lattner } else { 629579a05d7SChris Lattner // We're out of initalizers; default-initialize to null 63091f84216SDouglas Gregor EmitNullInitializationToLValue(FieldLoc, Field->getType()); 631579a05d7SChris Lattner } 6327a51313dSChris Lattner } 6337a51313dSChris Lattner } 6347a51313dSChris Lattner 6357a51313dSChris Lattner //===----------------------------------------------------------------------===// 6367a51313dSChris Lattner // Entry Points into this File 6377a51313dSChris Lattner //===----------------------------------------------------------------------===// 6387a51313dSChris Lattner 63925306cacSMike Stump /// EmitAggExpr - Emit the computation of the specified expression of aggregate 64025306cacSMike Stump /// type. The result is computed into DestPtr. Note that if DestPtr is null, 64125306cacSMike Stump /// the value of the aggregate expression is not needed. If VolatileDest is 64225306cacSMike Stump /// true, DestPtr cannot be 0. 643*7a626f63SJohn McCall /// 644*7a626f63SJohn McCall /// \param IsInitializer - true if this evaluation is initializing an 645*7a626f63SJohn McCall /// object whose lifetime is already being managed. 646d0bc7b9dSDaniel Dunbar // 647d0bc7b9dSDaniel Dunbar // FIXME: Take Qualifiers object. 648*7a626f63SJohn McCall void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot, 649*7a626f63SJohn McCall bool IgnoreResult, 650879d7266SFariborz Jahanian bool RequiresGCollection) { 6517a51313dSChris Lattner assert(E && hasAggregateLLVMType(E->getType()) && 6527a51313dSChris Lattner "Invalid aggregate expression to emit"); 653*7a626f63SJohn McCall assert((Slot.getAddr() != 0 || Slot.isIgnored()) 654*7a626f63SJohn McCall && "slot has bits but no address"); 6557a51313dSChris Lattner 656*7a626f63SJohn McCall AggExprEmitter(*this, Slot, IgnoreResult, RequiresGCollection) 657ec3cbfe8SMike Stump .Visit(const_cast<Expr*>(E)); 6587a51313dSChris Lattner } 6590bc8e86dSDaniel Dunbar 660d0bc7b9dSDaniel Dunbar LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { 661d0bc7b9dSDaniel Dunbar assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!"); 662a7566f16SDaniel Dunbar llvm::Value *Temp = CreateMemTemp(E->getType()); 6632e442a00SDaniel Dunbar LValue LV = MakeAddrLValue(Temp, E->getType()); 664*7a626f63SJohn McCall AggValueSlot Slot 665*7a626f63SJohn McCall = AggValueSlot::forAddr(Temp, LV.isVolatileQualified(), false); 666*7a626f63SJohn McCall EmitAggExpr(E, Slot); 6672e442a00SDaniel Dunbar return LV; 668d0bc7b9dSDaniel Dunbar } 669d0bc7b9dSDaniel Dunbar 6700bc8e86dSDaniel Dunbar void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 6715e9e61b8SMike Stump llvm::Value *SrcPtr, QualType Ty, 6725e9e61b8SMike Stump bool isVolatile) { 6730bc8e86dSDaniel Dunbar assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 6740bc8e86dSDaniel Dunbar 67516e94af6SAnders Carlsson if (getContext().getLangOptions().CPlusPlus) { 67616e94af6SAnders Carlsson if (const RecordType *RT = Ty->getAs<RecordType>()) { 677f22101a0SDouglas Gregor CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 678f22101a0SDouglas Gregor assert((Record->hasTrivialCopyConstructor() || 6796855ba2cSFariborz Jahanian Record->hasTrivialCopyAssignment()) && 680f22101a0SDouglas Gregor "Trying to aggregate-copy a type without a trivial copy " 681f22101a0SDouglas Gregor "constructor or assignment operator"); 682265b8b8dSDouglas Gregor // Ignore empty classes in C++. 683f22101a0SDouglas Gregor if (Record->isEmpty()) 68416e94af6SAnders Carlsson return; 68516e94af6SAnders Carlsson } 68616e94af6SAnders Carlsson } 68716e94af6SAnders Carlsson 688ca05dfefSChris Lattner // Aggregate assignment turns into llvm.memcpy. This is almost valid per 6893ef668c2SChris Lattner // C99 6.5.16.1p3, which states "If the value being stored in an object is 6903ef668c2SChris Lattner // read from another object that overlaps in anyway the storage of the first 6913ef668c2SChris Lattner // object, then the overlap shall be exact and the two objects shall have 6923ef668c2SChris Lattner // qualified or unqualified versions of a compatible type." 6933ef668c2SChris Lattner // 694ca05dfefSChris Lattner // memcpy is not defined if the source and destination pointers are exactly 6953ef668c2SChris Lattner // equal, but other compilers do this optimization, and almost every memcpy 6963ef668c2SChris Lattner // implementation handles this case safely. If there is a libc that does not 6973ef668c2SChris Lattner // safely handle this, we can add a target hook. 6980bc8e86dSDaniel Dunbar 6990bc8e86dSDaniel Dunbar // Get size and alignment info for this aggregate. 7000bc8e86dSDaniel Dunbar std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 7010bc8e86dSDaniel Dunbar 7020bc8e86dSDaniel Dunbar // FIXME: Handle variable sized types. 7030bc8e86dSDaniel Dunbar 70486736572SMike Stump // FIXME: If we have a volatile struct, the optimizer can remove what might 70586736572SMike Stump // appear to be `extra' memory ops: 70686736572SMike Stump // 70786736572SMike Stump // volatile struct { int i; } a, b; 70886736572SMike Stump // 70986736572SMike Stump // int main() { 71086736572SMike Stump // a = b; 71186736572SMike Stump // a = b; 71286736572SMike Stump // } 71386736572SMike Stump // 714cc2ab0cdSMon P Wang // we need to use a different call here. We use isVolatile to indicate when 715ec3cbfe8SMike Stump // either the source or the destination is volatile. 716cc2ab0cdSMon P Wang 717cc2ab0cdSMon P Wang const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); 718cb7696cfSChris Lattner const llvm::Type *DBP = 719cb7696cfSChris Lattner llvm::Type::getInt8PtrTy(VMContext, DPT->getAddressSpace()); 720cc2ab0cdSMon P Wang DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp"); 721cc2ab0cdSMon P Wang 722cc2ab0cdSMon P Wang const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); 723cb7696cfSChris Lattner const llvm::Type *SBP = 724cb7696cfSChris Lattner llvm::Type::getInt8PtrTy(VMContext, SPT->getAddressSpace()); 725cc2ab0cdSMon P Wang SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp"); 726cc2ab0cdSMon P Wang 727021510e9SFariborz Jahanian if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { 728021510e9SFariborz Jahanian RecordDecl *Record = RecordTy->getDecl(); 729021510e9SFariborz Jahanian if (Record->hasObjectMember()) { 730021510e9SFariborz Jahanian unsigned long size = TypeInfo.first/8; 731021510e9SFariborz Jahanian const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 732021510e9SFariborz Jahanian llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); 733021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 734021510e9SFariborz Jahanian SizeVal); 735021510e9SFariborz Jahanian return; 736021510e9SFariborz Jahanian } 737021510e9SFariborz Jahanian } else if (getContext().getAsArrayType(Ty)) { 738021510e9SFariborz Jahanian QualType BaseType = getContext().getBaseElementType(Ty); 739021510e9SFariborz Jahanian if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { 740021510e9SFariborz Jahanian if (RecordTy->getDecl()->hasObjectMember()) { 741021510e9SFariborz Jahanian unsigned long size = TypeInfo.first/8; 742021510e9SFariborz Jahanian const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 743021510e9SFariborz Jahanian llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); 744021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 745021510e9SFariborz Jahanian SizeVal); 746021510e9SFariborz Jahanian return; 747021510e9SFariborz Jahanian } 748021510e9SFariborz Jahanian } 749021510e9SFariborz Jahanian } 750021510e9SFariborz Jahanian 751cc2ab0cdSMon P Wang Builder.CreateCall5(CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(), 7525e016ae9SChris Lattner IntPtrTy), 7530bc8e86dSDaniel Dunbar DestPtr, SrcPtr, 7540bc8e86dSDaniel Dunbar // TypeInfo.first describes size in bits. 7555e016ae9SChris Lattner llvm::ConstantInt::get(IntPtrTy, TypeInfo.first/8), 756cb7696cfSChris Lattner Builder.getInt32(TypeInfo.second/8), 757cb7696cfSChris Lattner Builder.getInt1(isVolatile)); 7580bc8e86dSDaniel Dunbar } 759