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()); } 843f66b84cSEli Friedman void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 857a51313dSChris Lattner 867a51313dSChris Lattner // l-values. 877a51313dSChris Lattner void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } 887a51313dSChris Lattner void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 897a51313dSChris Lattner void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 90d443c0a0SDaniel Dunbar void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 912f343dd5SChris Lattner void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 922f343dd5SChris Lattner EmitAggLoadOfLValue(E); 932f343dd5SChris Lattner } 947a51313dSChris Lattner void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 957a51313dSChris Lattner EmitAggLoadOfLValue(E); 967a51313dSChris Lattner } 972f343dd5SChris Lattner void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 982f343dd5SChris Lattner EmitAggLoadOfLValue(E); 992f343dd5SChris Lattner } 1002f343dd5SChris Lattner void VisitPredefinedExpr(const PredefinedExpr *E) { 1012f343dd5SChris Lattner EmitAggLoadOfLValue(E); 1022f343dd5SChris Lattner } 103bc7d67ceSMike Stump 1047a51313dSChris Lattner // Operators. 105ec143777SAnders Carlsson void VisitCastExpr(CastExpr *E); 1067a51313dSChris Lattner void VisitCallExpr(const CallExpr *E); 1077a51313dSChris Lattner void VisitStmtExpr(const StmtExpr *E); 1087a51313dSChris Lattner void VisitBinaryOperator(const BinaryOperator *BO); 109ffba662dSFariborz Jahanian void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); 1107a51313dSChris Lattner void VisitBinAssign(const BinaryOperator *E); 1114b0e2a30SEli Friedman void VisitBinComma(const BinaryOperator *E); 1127a51313dSChris Lattner 113b1d329daSChris Lattner void VisitObjCMessageExpr(ObjCMessageExpr *E); 114c8317a44SDaniel Dunbar void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 115c8317a44SDaniel Dunbar EmitAggLoadOfLValue(E); 116c8317a44SDaniel Dunbar } 11755310df7SDaniel Dunbar void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); 1187a51313dSChris Lattner 1197a51313dSChris Lattner void VisitConditionalOperator(const ConditionalOperator *CO); 1205b2095ceSAnders Carlsson void VisitChooseExpr(const ChooseExpr *CE); 1217a51313dSChris Lattner void VisitInitListExpr(InitListExpr *E); 12218ada985SAnders Carlsson void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); 123aa9c7aedSChris Lattner void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 124aa9c7aedSChris Lattner Visit(DAE->getExpr()); 125aa9c7aedSChris Lattner } 1263be22e27SAnders Carlsson void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 1271619a504SAnders Carlsson void VisitCXXConstructExpr(const CXXConstructExpr *E); 128c82b86dfSAnders Carlsson void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E); 129747eb784SDouglas Gregor void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 1305bbbb137SMike Stump void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } 131c82b86dfSAnders Carlsson 13221911e89SEli Friedman void VisitVAArgExpr(VAArgExpr *E); 133579a05d7SChris Lattner 134b247350eSAnders Carlsson void EmitInitializationToLValue(Expr *E, LValue Address, QualType T); 135579a05d7SChris Lattner void EmitNullInitializationToLValue(LValue Address, QualType T); 1367a51313dSChris Lattner // case Expr::ChooseExprClass: 137f16b8c30SMike Stump void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } 1387a51313dSChris Lattner }; 1397a51313dSChris Lattner } // end anonymous namespace. 1407a51313dSChris Lattner 1417a51313dSChris Lattner //===----------------------------------------------------------------------===// 1427a51313dSChris Lattner // Utilities 1437a51313dSChris Lattner //===----------------------------------------------------------------------===// 1447a51313dSChris Lattner 1457a51313dSChris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that 1467a51313dSChris Lattner /// represents a value lvalue, this method emits the address of the lvalue, 1477a51313dSChris Lattner /// then loads the result into DestPtr. 1487a51313dSChris Lattner void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 1497a51313dSChris Lattner LValue LV = CGF.EmitLValue(E); 150ca9fc09cSMike Stump EmitFinalDestCopy(E, LV); 151ca9fc09cSMike Stump } 152ca9fc09cSMike Stump 153cc04e9f6SJohn McCall /// \brief True if the given aggregate type requires special GC API calls. 154cc04e9f6SJohn McCall bool AggExprEmitter::TypeRequiresGCollection(QualType T) { 155cc04e9f6SJohn McCall // Only record types have members that might require garbage collection. 156cc04e9f6SJohn McCall const RecordType *RecordTy = T->getAs<RecordType>(); 157cc04e9f6SJohn McCall if (!RecordTy) return false; 158cc04e9f6SJohn McCall 159cc04e9f6SJohn McCall // Don't mess with non-trivial C++ types. 160cc04e9f6SJohn McCall RecordDecl *Record = RecordTy->getDecl(); 161cc04e9f6SJohn McCall if (isa<CXXRecordDecl>(Record) && 162cc04e9f6SJohn McCall (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() || 163cc04e9f6SJohn McCall !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) 164cc04e9f6SJohn McCall return false; 165cc04e9f6SJohn McCall 166cc04e9f6SJohn McCall // Check whether the type has an object member. 167cc04e9f6SJohn McCall return Record->hasObjectMember(); 168cc04e9f6SJohn McCall } 169cc04e9f6SJohn McCall 170cc04e9f6SJohn McCall /// \brief Perform the final move to DestPtr if RequiresGCollection is set. 171cc04e9f6SJohn McCall /// 172cc04e9f6SJohn McCall /// The idea is that you do something like this: 173cc04e9f6SJohn McCall /// RValue Result = EmitSomething(..., getReturnValueSlot()); 174cc04e9f6SJohn McCall /// EmitGCMove(E, Result); 175cc04e9f6SJohn McCall /// If GC doesn't interfere, this will cause the result to be emitted 176cc04e9f6SJohn McCall /// directly into the return value slot. If GC does interfere, a final 177cc04e9f6SJohn McCall /// move will be performed. 178cc04e9f6SJohn McCall void AggExprEmitter::EmitGCMove(const Expr *E, RValue Src) { 17958649dc6SJohn McCall if (Dest.requiresGCollection()) { 180021510e9SFariborz Jahanian std::pair<uint64_t, unsigned> TypeInfo = 181021510e9SFariborz Jahanian CGF.getContext().getTypeInfo(E->getType()); 182021510e9SFariborz Jahanian unsigned long size = TypeInfo.first/8; 183021510e9SFariborz Jahanian const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 184021510e9SFariborz Jahanian llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); 1857a626f63SJohn McCall CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, Dest.getAddr(), 186cc04e9f6SJohn McCall Src.getAggregateAddr(), 187021510e9SFariborz Jahanian SizeVal); 188021510e9SFariborz Jahanian } 189cc04e9f6SJohn McCall } 190cc04e9f6SJohn McCall 191ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 192ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) { 193ca9fc09cSMike Stump assert(Src.isAggregate() && "value must be aggregate value!"); 1947a51313dSChris Lattner 1957a626f63SJohn McCall // If Dest is ignored, then we're evaluating an aggregate expression 1968d752430SJohn McCall // in a context (like an expression statement) that doesn't care 1978d752430SJohn McCall // about the result. C says that an lvalue-to-rvalue conversion is 1988d752430SJohn McCall // performed in these cases; C++ says that it is not. In either 1998d752430SJohn McCall // case, we don't actually need to do anything unless the value is 2008d752430SJohn McCall // volatile. 2017a626f63SJohn McCall if (Dest.isIgnored()) { 2028d752430SJohn McCall if (!Src.isVolatileQualified() || 2038d752430SJohn McCall CGF.CGM.getLangOptions().CPlusPlus || 2048d752430SJohn McCall (IgnoreResult && Ignore)) 205ec3cbfe8SMike Stump return; 206c123623dSFariborz Jahanian 207332ec2ceSMike Stump // If the source is volatile, we must read from it; to do that, we need 208332ec2ceSMike Stump // some place to put it. 2097a626f63SJohn McCall Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp"); 210332ec2ceSMike Stump } 2117a51313dSChris Lattner 21258649dc6SJohn McCall if (Dest.requiresGCollection()) { 213021510e9SFariborz Jahanian std::pair<uint64_t, unsigned> TypeInfo = 214021510e9SFariborz Jahanian CGF.getContext().getTypeInfo(E->getType()); 215021510e9SFariborz Jahanian unsigned long size = TypeInfo.first/8; 216021510e9SFariborz Jahanian const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 217021510e9SFariborz Jahanian llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); 218879d7266SFariborz Jahanian CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 2197a626f63SJohn McCall Dest.getAddr(), 2207a626f63SJohn McCall Src.getAggregateAddr(), 221021510e9SFariborz Jahanian SizeVal); 222879d7266SFariborz Jahanian return; 223879d7266SFariborz Jahanian } 224ca9fc09cSMike Stump // If the result of the assignment is used, copy the LHS there also. 225ca9fc09cSMike Stump // FIXME: Pass VolatileDest as well. I think we also need to merge volatile 226ca9fc09cSMike Stump // from the source as well, as we can't eliminate it if either operand 227ca9fc09cSMike Stump // is volatile, unless copy has volatile for both source and destination.. 2287a626f63SJohn McCall CGF.EmitAggregateCopy(Dest.getAddr(), Src.getAggregateAddr(), E->getType(), 2297a626f63SJohn McCall Dest.isVolatile()|Src.isVolatileQualified()); 230ca9fc09cSMike Stump } 231ca9fc09cSMike Stump 232ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 233ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { 234ca9fc09cSMike Stump assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); 235ca9fc09cSMike Stump 236ca9fc09cSMike Stump EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(), 237ec3cbfe8SMike Stump Src.isVolatileQualified()), 238ec3cbfe8SMike Stump Ignore); 2397a51313dSChris Lattner } 2407a51313dSChris Lattner 2417a51313dSChris Lattner //===----------------------------------------------------------------------===// 2427a51313dSChris Lattner // Visitor Methods 2437a51313dSChris Lattner //===----------------------------------------------------------------------===// 2447a51313dSChris Lattner 245ec143777SAnders Carlsson void AggExprEmitter::VisitCastExpr(CastExpr *E) { 2467a626f63SJohn McCall if (Dest.isIgnored() && E->getCastKind() != CK_Dynamic) { 247c934bc84SDouglas Gregor Visit(E->getSubExpr()); 248c934bc84SDouglas Gregor return; 249c934bc84SDouglas Gregor } 250c934bc84SDouglas Gregor 2511fb7ae9eSAnders Carlsson switch (E->getCastKind()) { 252e302792bSJohn McCall 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: { 2677ffcf93bSNuno Lopes // GCC union extension 2682e442a00SDaniel Dunbar QualType Ty = E->getSubExpr()->getType(); 2692e442a00SDaniel Dunbar QualType PtrTy = CGF.getContext().getPointerType(Ty); 2707a626f63SJohn McCall llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(), 271dd274848SEli Friedman CGF.ConvertType(PtrTy)); 2722e442a00SDaniel Dunbar EmitInitializationToLValue(E->getSubExpr(), CGF.MakeAddrLValue(CastPtr, Ty), 2732e442a00SDaniel Dunbar Ty); 2741fb7ae9eSAnders Carlsson break; 2757ffcf93bSNuno Lopes } 2767ffcf93bSNuno Lopes 277e302792bSJohn McCall case CK_DerivedToBase: 278e302792bSJohn McCall case CK_BaseToDerived: 279e302792bSJohn McCall case CK_UncheckedDerivedToBase: { 280aae38d66SDouglas Gregor assert(0 && "cannot perform hierarchy conversion in EmitAggExpr: " 281aae38d66SDouglas Gregor "should have been unpacked before we got here"); 282aae38d66SDouglas Gregor break; 283aae38d66SDouglas Gregor } 284aae38d66SDouglas Gregor 285e302792bSJohn McCall case CK_NoOp: 286f3735e01SJohn McCall case CK_LValueToRValue: 287e302792bSJohn McCall case CK_UserDefinedConversion: 288e302792bSJohn McCall case CK_ConstructorConversion: 2892a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 2902a69547fSEli Friedman E->getType()) && 2910f398c44SChris Lattner "Implicit cast types must be compatible"); 2927a51313dSChris Lattner Visit(E->getSubExpr()); 2931fb7ae9eSAnders Carlsson break; 294b05a3e55SAnders Carlsson 295e302792bSJohn McCall case CK_LValueBitCast: 296f3735e01SJohn McCall llvm_unreachable("should not be emitting lvalue bitcast as rvalue"); 29751954276SDouglas Gregor break; 298f3735e01SJohn McCall 299f3735e01SJohn McCall case CK_Dependent: 300f3735e01SJohn McCall case CK_BitCast: 301f3735e01SJohn McCall case CK_ArrayToPointerDecay: 302f3735e01SJohn McCall case CK_FunctionToPointerDecay: 303f3735e01SJohn McCall case CK_NullToPointer: 304f3735e01SJohn McCall case CK_NullToMemberPointer: 305f3735e01SJohn McCall case CK_BaseToDerivedMemberPointer: 306f3735e01SJohn McCall case CK_DerivedToBaseMemberPointer: 307f3735e01SJohn McCall case CK_MemberPointerToBoolean: 308f3735e01SJohn McCall case CK_IntegralToPointer: 309f3735e01SJohn McCall case CK_PointerToIntegral: 310f3735e01SJohn McCall case CK_PointerToBoolean: 311f3735e01SJohn McCall case CK_ToVoid: 312f3735e01SJohn McCall case CK_VectorSplat: 313f3735e01SJohn McCall case CK_IntegralCast: 314f3735e01SJohn McCall case CK_IntegralToBoolean: 315f3735e01SJohn McCall case CK_IntegralToFloating: 316f3735e01SJohn McCall case CK_FloatingToIntegral: 317f3735e01SJohn McCall case CK_FloatingToBoolean: 318f3735e01SJohn McCall case CK_FloatingCast: 319f3735e01SJohn McCall case CK_AnyPointerToObjCPointerCast: 320f3735e01SJohn McCall case CK_AnyPointerToBlockPointerCast: 321f3735e01SJohn McCall case CK_ObjCObjectLValueCast: 322f3735e01SJohn McCall case CK_FloatingRealToComplex: 323f3735e01SJohn McCall case CK_FloatingComplexToReal: 324f3735e01SJohn McCall case CK_FloatingComplexToBoolean: 325f3735e01SJohn McCall case CK_FloatingComplexCast: 326f3735e01SJohn McCall case CK_FloatingComplexToIntegralComplex: 327f3735e01SJohn McCall case CK_IntegralRealToComplex: 328f3735e01SJohn McCall case CK_IntegralComplexToReal: 329f3735e01SJohn McCall case CK_IntegralComplexToBoolean: 330f3735e01SJohn McCall case CK_IntegralComplexCast: 331f3735e01SJohn McCall case CK_IntegralComplexToFloatingComplex: 332f3735e01SJohn McCall llvm_unreachable("cast kind invalid for aggregate types"); 3331fb7ae9eSAnders Carlsson } 3347a51313dSChris Lattner } 3357a51313dSChris Lattner 3360f398c44SChris Lattner void AggExprEmitter::VisitCallExpr(const CallExpr *E) { 337ddcbfe7bSAnders Carlsson if (E->getCallReturnType()->isReferenceType()) { 338ddcbfe7bSAnders Carlsson EmitAggLoadOfLValue(E); 339ddcbfe7bSAnders Carlsson return; 340ddcbfe7bSAnders Carlsson } 341ddcbfe7bSAnders Carlsson 342cc04e9f6SJohn McCall RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); 343cc04e9f6SJohn McCall EmitGCMove(E, RV); 3447a51313dSChris Lattner } 3450f398c44SChris Lattner 3460f398c44SChris Lattner void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 347cc04e9f6SJohn McCall RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); 348cc04e9f6SJohn McCall EmitGCMove(E, RV); 349b1d329daSChris Lattner } 3507a51313dSChris Lattner 35155310df7SDaniel Dunbar void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 352cc04e9f6SJohn McCall RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot()); 353cc04e9f6SJohn McCall EmitGCMove(E, RV); 35455310df7SDaniel Dunbar } 35555310df7SDaniel Dunbar 3560f398c44SChris Lattner void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 3577a626f63SJohn McCall CGF.EmitAnyExpr(E->getLHS(), AggValueSlot::ignored(), true); 3587a626f63SJohn McCall Visit(E->getRHS()); 3594b0e2a30SEli Friedman } 3604b0e2a30SEli Friedman 3617a51313dSChris Lattner void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 3627a626f63SJohn McCall CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest); 3637a51313dSChris Lattner } 3647a51313dSChris Lattner 3657a51313dSChris Lattner void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 366e302792bSJohn McCall if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI) 367ffba662dSFariborz Jahanian VisitPointerToDataMemberBinaryOperator(E); 368ffba662dSFariborz Jahanian else 369a7c8cf62SDaniel Dunbar CGF.ErrorUnsupported(E, "aggregate binary expression"); 3707a51313dSChris Lattner } 3717a51313dSChris Lattner 372ffba662dSFariborz Jahanian void AggExprEmitter::VisitPointerToDataMemberBinaryOperator( 373ffba662dSFariborz Jahanian const BinaryOperator *E) { 374ffba662dSFariborz Jahanian LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); 375ffba662dSFariborz Jahanian EmitFinalDestCopy(E, LV); 376ffba662dSFariborz Jahanian } 377ffba662dSFariborz Jahanian 3787a51313dSChris Lattner void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 3797a51313dSChris Lattner // For an assignment to work, the value on the right has 3807a51313dSChris Lattner // to be compatible with the value on the left. 3812a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 3822a69547fSEli Friedman E->getRHS()->getType()) 3837a51313dSChris Lattner && "Invalid assignment"); 3847a51313dSChris Lattner LValue LHS = CGF.EmitLValue(E->getLHS()); 3857a51313dSChris Lattner 3864b8c6db9SDaniel Dunbar // We have to special case property setters, otherwise we must have 3874b8c6db9SDaniel Dunbar // a simple lvalue (no aggregates inside vectors, bitfields). 3884b8c6db9SDaniel Dunbar if (LHS.isPropertyRef()) { 3897a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getRHS()->getType()); 3907a626f63SJohn McCall CGF.EmitAggExpr(E->getRHS(), Slot); 3917a626f63SJohn McCall CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), Slot.asRValue()); 392658fe02dSMike Stump } else if (LHS.isKVCRef()) { 3937a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getRHS()->getType()); 3947a626f63SJohn McCall CGF.EmitAggExpr(E->getRHS(), Slot); 3957a626f63SJohn McCall CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), Slot.asRValue()); 3964b8c6db9SDaniel Dunbar } else { 397b60e70f9SFariborz Jahanian bool GCollection = false; 398cc04e9f6SJohn McCall if (CGF.getContext().getLangOptions().getGCMode()) 399b60e70f9SFariborz Jahanian GCollection = TypeRequiresGCollection(E->getLHS()->getType()); 400cc04e9f6SJohn McCall 4017a51313dSChris Lattner // Codegen the RHS so that it stores directly into the LHS. 402b60e70f9SFariborz Jahanian AggValueSlot LHSSlot = AggValueSlot::forLValue(LHS, true, 403b60e70f9SFariborz Jahanian GCollection); 404b60e70f9SFariborz Jahanian CGF.EmitAggExpr(E->getRHS(), LHSSlot, false); 405ec3cbfe8SMike Stump EmitFinalDestCopy(E, LHS, true); 4067a51313dSChris Lattner } 4074b8c6db9SDaniel Dunbar } 4087a51313dSChris Lattner 4097a51313dSChris Lattner void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { 410b8841af8SEli Friedman if (!E->getLHS()) { 411b8841af8SEli Friedman CGF.ErrorUnsupported(E, "conditional operator with missing LHS"); 412b8841af8SEli Friedman return; 413b8841af8SEli Friedman } 414b8841af8SEli Friedman 415a612e79bSDaniel Dunbar llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 416a612e79bSDaniel Dunbar llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 417a612e79bSDaniel Dunbar llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 4187a51313dSChris Lattner 419b8841af8SEli Friedman CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 4207a51313dSChris Lattner 421ae612d22SAnders Carlsson CGF.BeginConditionalBranch(); 4227a51313dSChris Lattner CGF.EmitBlock(LHSBlock); 4237a51313dSChris Lattner 4245b26f65bSJohn McCall // Save whether the destination's lifetime is externally managed. 4255b26f65bSJohn McCall bool DestLifetimeManaged = Dest.isLifetimeExternallyManaged(); 4267a51313dSChris Lattner 4277a51313dSChris Lattner Visit(E->getLHS()); 428ae612d22SAnders Carlsson CGF.EndConditionalBranch(); 429c56e6764SDaniel Dunbar CGF.EmitBranch(ContBlock); 4307a51313dSChris Lattner 431ae612d22SAnders Carlsson CGF.BeginConditionalBranch(); 4327a51313dSChris Lattner CGF.EmitBlock(RHSBlock); 4337a51313dSChris Lattner 4345b26f65bSJohn McCall // If the result of an agg expression is unused, then the emission 4355b26f65bSJohn McCall // of the LHS might need to create a destination slot. That's fine 4365b26f65bSJohn McCall // with us, and we can safely emit the RHS into the same slot, but 4375b26f65bSJohn McCall // we shouldn't claim that its lifetime is externally managed. 4385b26f65bSJohn McCall Dest.setLifetimeExternallyManaged(DestLifetimeManaged); 4395b26f65bSJohn McCall 4407a51313dSChris Lattner Visit(E->getRHS()); 441ae612d22SAnders Carlsson CGF.EndConditionalBranch(); 442c56e6764SDaniel Dunbar CGF.EmitBranch(ContBlock); 4437a51313dSChris Lattner 4447a51313dSChris Lattner CGF.EmitBlock(ContBlock); 4457a51313dSChris Lattner } 4467a51313dSChris Lattner 4475b2095ceSAnders Carlsson void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 4485b2095ceSAnders Carlsson Visit(CE->getChosenSubExpr(CGF.getContext())); 4495b2095ceSAnders Carlsson } 4505b2095ceSAnders Carlsson 45121911e89SEli Friedman void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 452e9fcadd2SDaniel Dunbar llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 45313abd7e9SAnders Carlsson llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 45413abd7e9SAnders Carlsson 455020cddcfSSebastian Redl if (!ArgPtr) { 45613abd7e9SAnders Carlsson CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 457020cddcfSSebastian Redl return; 458020cddcfSSebastian Redl } 45913abd7e9SAnders Carlsson 4602e442a00SDaniel Dunbar EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType())); 46121911e89SEli Friedman } 46221911e89SEli Friedman 4633be22e27SAnders Carlsson void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 4647a626f63SJohn McCall // Ensure that we have a slot, but if we already do, remember 4657a626f63SJohn McCall // whether its lifetime was externally managed. 4667a626f63SJohn McCall bool WasManaged = Dest.isLifetimeExternallyManaged(); 4677a626f63SJohn McCall Dest = EnsureSlot(E->getType()); 4687a626f63SJohn McCall Dest.setLifetimeExternallyManaged(); 4693be22e27SAnders Carlsson 4703be22e27SAnders Carlsson Visit(E->getSubExpr()); 4713be22e27SAnders Carlsson 4727a626f63SJohn McCall // Set up the temporary's destructor if its lifetime wasn't already 4737a626f63SJohn McCall // being managed. 4747a626f63SJohn McCall if (!WasManaged) 4757a626f63SJohn McCall CGF.EmitCXXTemporary(E->getTemporary(), Dest.getAddr()); 4763be22e27SAnders Carlsson } 4773be22e27SAnders Carlsson 478b7f8f594SAnders Carlsson void 4791619a504SAnders Carlsson AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 4807a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getType()); 4817a626f63SJohn McCall CGF.EmitCXXConstructExpr(E, Slot); 482c82b86dfSAnders Carlsson } 483c82b86dfSAnders Carlsson 484c82b86dfSAnders Carlsson void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { 4857a626f63SJohn McCall CGF.EmitCXXExprWithTemporaries(E, Dest); 486b7f8f594SAnders Carlsson } 487b7f8f594SAnders Carlsson 488747eb784SDouglas Gregor void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 4897a626f63SJohn McCall QualType T = E->getType(); 4907a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 4917a626f63SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T); 49218ada985SAnders Carlsson } 49318ada985SAnders Carlsson 49418ada985SAnders Carlsson void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 4957a626f63SJohn McCall QualType T = E->getType(); 4967a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 4977a626f63SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T); 498ff3507b9SNuno Lopes } 499ff3507b9SNuno Lopes 50027a3631bSChris Lattner /// isSimpleZero - If emitting this value will obviously just cause a store of 50127a3631bSChris Lattner /// zero to memory, return true. This can return false if uncertain, so it just 50227a3631bSChris Lattner /// handles simple cases. 50327a3631bSChris Lattner static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { 50427a3631bSChris Lattner // (0) 50527a3631bSChris Lattner if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 50627a3631bSChris Lattner return isSimpleZero(PE->getSubExpr(), CGF); 50727a3631bSChris Lattner // 0 50827a3631bSChris Lattner if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) 50927a3631bSChris Lattner return IL->getValue() == 0; 51027a3631bSChris Lattner // +0.0 51127a3631bSChris Lattner if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E)) 51227a3631bSChris Lattner return FL->getValue().isPosZero(); 51327a3631bSChris Lattner // int() 51427a3631bSChris Lattner if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) && 51527a3631bSChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 51627a3631bSChris Lattner return true; 51727a3631bSChris Lattner // (int*)0 - Null pointer expressions. 51827a3631bSChris Lattner if (const CastExpr *ICE = dyn_cast<CastExpr>(E)) 51927a3631bSChris Lattner return ICE->getCastKind() == CK_NullToPointer; 52027a3631bSChris Lattner // '\0' 52127a3631bSChris Lattner if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) 52227a3631bSChris Lattner return CL->getValue() == 0; 52327a3631bSChris Lattner 52427a3631bSChris Lattner // Otherwise, hard case: conservatively return false. 52527a3631bSChris Lattner return false; 52627a3631bSChris Lattner } 52727a3631bSChris Lattner 52827a3631bSChris Lattner 529b247350eSAnders Carlsson void 530b247350eSAnders Carlsson AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) { 531df0fe27bSMike Stump // FIXME: Ignore result? 532579a05d7SChris Lattner // FIXME: Are initializers affected by volatile? 53327a3631bSChris Lattner if (Dest.isZeroed() && isSimpleZero(E, CGF)) { 53427a3631bSChris Lattner // Storing "i32 0" to a zero'd memory location is a noop. 53527a3631bSChris Lattner } else if (isa<ImplicitValueInitExpr>(E)) { 536b247350eSAnders Carlsson EmitNullInitializationToLValue(LV, T); 53766498388SAnders Carlsson } else if (T->isReferenceType()) { 53804775f84SAnders Carlsson RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); 53966498388SAnders Carlsson CGF.EmitStoreThroughLValue(RV, LV, T); 540b247350eSAnders Carlsson } else if (T->isAnyComplexType()) { 5410202cb40SDouglas Gregor CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); 542b247350eSAnders Carlsson } else if (CGF.hasAggregateLLVMType(T)) { 54327a3631bSChris Lattner CGF.EmitAggExpr(E, AggValueSlot::forAddr(LV.getAddress(), false, true, 54427a3631bSChris Lattner false, Dest.isZeroed())); 5456e313210SEli Friedman } else { 546b247350eSAnders Carlsson CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, T); 5477a51313dSChris Lattner } 548579a05d7SChris Lattner } 549579a05d7SChris Lattner 550579a05d7SChris Lattner void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { 55127a3631bSChris Lattner // If the destination slot is already zeroed out before the aggregate is 55227a3631bSChris Lattner // copied into it, we don't have to emit any zeros here. 55327a3631bSChris Lattner if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(T)) 55427a3631bSChris Lattner return; 55527a3631bSChris Lattner 556579a05d7SChris Lattner if (!CGF.hasAggregateLLVMType(T)) { 557579a05d7SChris Lattner // For non-aggregates, we can store zero 5580b75f23bSOwen Anderson llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T)); 559e8bdce44SDaniel Dunbar CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T); 560579a05d7SChris Lattner } else { 561579a05d7SChris Lattner // There's a potential optimization opportunity in combining 562579a05d7SChris Lattner // memsets; that would be easy for arrays, but relatively 563579a05d7SChris Lattner // difficult for structures with the current code. 564c0964b60SAnders Carlsson CGF.EmitNullInitialization(LV.getAddress(), T); 565579a05d7SChris Lattner } 566579a05d7SChris Lattner } 567579a05d7SChris Lattner 568579a05d7SChris Lattner void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 569f5d08c9eSEli Friedman #if 0 5706d11ec8cSEli Friedman // FIXME: Assess perf here? Figure out what cases are worth optimizing here 5716d11ec8cSEli Friedman // (Length of globals? Chunks of zeroed-out space?). 572f5d08c9eSEli Friedman // 57318bb9284SMike Stump // If we can, prefer a copy from a global; this is a lot less code for long 57418bb9284SMike Stump // globals, and it's easier for the current optimizers to analyze. 5756d11ec8cSEli Friedman if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { 576c59bb48eSEli Friedman llvm::GlobalVariable* GV = 5776d11ec8cSEli Friedman new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, 5786d11ec8cSEli Friedman llvm::GlobalValue::InternalLinkage, C, ""); 5792e442a00SDaniel Dunbar EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType())); 580c59bb48eSEli Friedman return; 581c59bb48eSEli Friedman } 582f5d08c9eSEli Friedman #endif 583f53c0968SChris Lattner if (E->hadArrayRangeDesignator()) 584bf7207a1SDouglas Gregor CGF.ErrorUnsupported(E, "GNU array range designator extension"); 585bf7207a1SDouglas Gregor 5867a626f63SJohn McCall llvm::Value *DestPtr = Dest.getAddr(); 5877a626f63SJohn McCall 588579a05d7SChris Lattner // Handle initialization of an array. 589579a05d7SChris Lattner if (E->getType()->isArrayType()) { 590579a05d7SChris Lattner const llvm::PointerType *APType = 591579a05d7SChris Lattner cast<llvm::PointerType>(DestPtr->getType()); 592579a05d7SChris Lattner const llvm::ArrayType *AType = 593579a05d7SChris Lattner cast<llvm::ArrayType>(APType->getElementType()); 594579a05d7SChris Lattner 595579a05d7SChris Lattner uint64_t NumInitElements = E->getNumInits(); 596f23b6fa4SEli Friedman 5970f398c44SChris Lattner if (E->getNumInits() > 0) { 5980f398c44SChris Lattner QualType T1 = E->getType(); 5990f398c44SChris Lattner QualType T2 = E->getInit(0)->getType(); 6002a69547fSEli Friedman if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { 601f23b6fa4SEli Friedman EmitAggLoadOfLValue(E->getInit(0)); 602f23b6fa4SEli Friedman return; 603f23b6fa4SEli Friedman } 6040f398c44SChris Lattner } 605f23b6fa4SEli Friedman 606579a05d7SChris Lattner uint64_t NumArrayElements = AType->getNumElements(); 6077adf0760SChris Lattner QualType ElementType = CGF.getContext().getCanonicalType(E->getType()); 6087adf0760SChris Lattner ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType(); 609579a05d7SChris Lattner 6108ccfcb51SJohn McCall // FIXME: were we intentionally ignoring address spaces and GC attributes? 611327944b3SEli Friedman 612579a05d7SChris Lattner for (uint64_t i = 0; i != NumArrayElements; ++i) { 61327a3631bSChris Lattner // If we're done emitting initializers and the destination is known-zeroed 61427a3631bSChris Lattner // then we're done. 61527a3631bSChris Lattner if (i == NumInitElements && 61627a3631bSChris Lattner Dest.isZeroed() && 61727a3631bSChris Lattner CGF.getTypes().isZeroInitializable(ElementType)) 61827a3631bSChris Lattner break; 61927a3631bSChris Lattner 620579a05d7SChris Lattner llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); 621f6fb7e2bSDaniel Dunbar LValue LV = CGF.MakeAddrLValue(NextVal, ElementType); 62227a3631bSChris Lattner 623579a05d7SChris Lattner if (i < NumInitElements) 624f6fb7e2bSDaniel Dunbar EmitInitializationToLValue(E->getInit(i), LV, ElementType); 625579a05d7SChris Lattner else 626f6fb7e2bSDaniel Dunbar EmitNullInitializationToLValue(LV, ElementType); 62727a3631bSChris Lattner 62827a3631bSChris Lattner // If the GEP didn't get used because of a dead zero init or something 62927a3631bSChris Lattner // else, clean it up for -O0 builds and general tidiness. 63027a3631bSChris Lattner if (llvm::GetElementPtrInst *GEP = 63127a3631bSChris Lattner dyn_cast<llvm::GetElementPtrInst>(NextVal)) 63227a3631bSChris Lattner if (GEP->use_empty()) 63327a3631bSChris Lattner GEP->eraseFromParent(); 634579a05d7SChris Lattner } 635579a05d7SChris Lattner return; 636579a05d7SChris Lattner } 637579a05d7SChris Lattner 638579a05d7SChris Lattner assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 639579a05d7SChris Lattner 640579a05d7SChris Lattner // Do struct initialization; this code just sets each individual member 641579a05d7SChris Lattner // to the approprate value. This makes bitfield support automatic; 642579a05d7SChris Lattner // the disadvantage is that the generated code is more difficult for 643579a05d7SChris Lattner // the optimizer, especially with bitfields. 644579a05d7SChris Lattner unsigned NumInitElements = E->getNumInits(); 645c23c7e6aSTed Kremenek RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 64652bcf963SChris Lattner 6475169570eSDouglas Gregor if (E->getType()->isUnionType()) { 6485169570eSDouglas Gregor // Only initialize one field of a union. The field itself is 6495169570eSDouglas Gregor // specified by the initializer list. 6505169570eSDouglas Gregor if (!E->getInitializedFieldInUnion()) { 6515169570eSDouglas Gregor // Empty union; we have nothing to do. 6525169570eSDouglas Gregor 6535169570eSDouglas Gregor #ifndef NDEBUG 6545169570eSDouglas Gregor // Make sure that it's really an empty and not a failure of 6555169570eSDouglas Gregor // semantic analysis. 656cfbfe78eSArgyrios Kyrtzidis for (RecordDecl::field_iterator Field = SD->field_begin(), 657cfbfe78eSArgyrios Kyrtzidis FieldEnd = SD->field_end(); 6585169570eSDouglas Gregor Field != FieldEnd; ++Field) 6595169570eSDouglas Gregor assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 6605169570eSDouglas Gregor #endif 6615169570eSDouglas Gregor return; 6625169570eSDouglas Gregor } 6635169570eSDouglas Gregor 6645169570eSDouglas Gregor // FIXME: volatility 6655169570eSDouglas Gregor FieldDecl *Field = E->getInitializedFieldInUnion(); 6665169570eSDouglas Gregor 66727a3631bSChris Lattner LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0); 6685169570eSDouglas Gregor if (NumInitElements) { 6695169570eSDouglas Gregor // Store the initializer into the field 670b247350eSAnders Carlsson EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType()); 6715169570eSDouglas Gregor } else { 67227a3631bSChris Lattner // Default-initialize to null. 6735169570eSDouglas Gregor EmitNullInitializationToLValue(FieldLoc, Field->getType()); 6745169570eSDouglas Gregor } 6755169570eSDouglas Gregor 6765169570eSDouglas Gregor return; 6775169570eSDouglas Gregor } 678579a05d7SChris Lattner 679579a05d7SChris Lattner // Here we iterate over the fields; this makes it simpler to both 680579a05d7SChris Lattner // default-initialize fields and skip over unnamed fields. 68152bcf963SChris Lattner unsigned CurInitVal = 0; 682cfbfe78eSArgyrios Kyrtzidis for (RecordDecl::field_iterator Field = SD->field_begin(), 683cfbfe78eSArgyrios Kyrtzidis FieldEnd = SD->field_end(); 68491f84216SDouglas Gregor Field != FieldEnd; ++Field) { 68591f84216SDouglas Gregor // We're done once we hit the flexible array member 68691f84216SDouglas Gregor if (Field->getType()->isIncompleteArrayType()) 68791f84216SDouglas Gregor break; 68891f84216SDouglas Gregor 68917bd094aSDouglas Gregor if (Field->isUnnamedBitfield()) 690579a05d7SChris Lattner continue; 69117bd094aSDouglas Gregor 69227a3631bSChris Lattner // Don't emit GEP before a noop store of zero. 69327a3631bSChris Lattner if (CurInitVal == NumInitElements && Dest.isZeroed() && 69427a3631bSChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 69527a3631bSChris Lattner break; 69627a3631bSChris Lattner 697327944b3SEli Friedman // FIXME: volatility 69866498388SAnders Carlsson LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0); 6997c1baf46SFariborz Jahanian // We never generate write-barries for initialized fields. 700e50dda95SDaniel Dunbar FieldLoc.setNonGC(true); 70127a3631bSChris Lattner 702579a05d7SChris Lattner if (CurInitVal < NumInitElements) { 703e18aaf2cSChris Lattner // Store the initializer into the field. 704b247350eSAnders Carlsson EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc, 705b247350eSAnders Carlsson Field->getType()); 706579a05d7SChris Lattner } else { 707579a05d7SChris Lattner // We're out of initalizers; default-initialize to null 70891f84216SDouglas Gregor EmitNullInitializationToLValue(FieldLoc, Field->getType()); 709579a05d7SChris Lattner } 71027a3631bSChris Lattner 71127a3631bSChris Lattner // If the GEP didn't get used because of a dead zero init or something 71227a3631bSChris Lattner // else, clean it up for -O0 builds and general tidiness. 71327a3631bSChris Lattner if (FieldLoc.isSimple()) 71427a3631bSChris Lattner if (llvm::GetElementPtrInst *GEP = 71527a3631bSChris Lattner dyn_cast<llvm::GetElementPtrInst>(FieldLoc.getAddress())) 71627a3631bSChris Lattner if (GEP->use_empty()) 71727a3631bSChris Lattner GEP->eraseFromParent(); 7187a51313dSChris Lattner } 7197a51313dSChris Lattner } 7207a51313dSChris Lattner 7217a51313dSChris Lattner //===----------------------------------------------------------------------===// 7227a51313dSChris Lattner // Entry Points into this File 7237a51313dSChris Lattner //===----------------------------------------------------------------------===// 7247a51313dSChris Lattner 72527a3631bSChris Lattner /// GetNumNonZeroBytesInInit - Get an approximate count of the number of 72627a3631bSChris Lattner /// non-zero bytes that will be stored when outputting the initializer for the 72727a3631bSChris Lattner /// specified initializer expression. 72827a3631bSChris Lattner static uint64_t GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) { 72927a3631bSChris Lattner if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 73027a3631bSChris Lattner return GetNumNonZeroBytesInInit(PE->getSubExpr(), CGF); 73127a3631bSChris Lattner 73227a3631bSChris Lattner // 0 and 0.0 won't require any non-zero stores! 73327a3631bSChris Lattner if (isSimpleZero(E, CGF)) return 0; 73427a3631bSChris Lattner 73527a3631bSChris Lattner // If this is an initlist expr, sum up the size of sizes of the (present) 73627a3631bSChris Lattner // elements. If this is something weird, assume the whole thing is non-zero. 73727a3631bSChris Lattner const InitListExpr *ILE = dyn_cast<InitListExpr>(E); 73827a3631bSChris Lattner if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType())) 73927a3631bSChris Lattner return CGF.getContext().getTypeSize(E->getType())/8; 74027a3631bSChris Lattner 741*c5cc2fb9SChris Lattner // InitListExprs for structs have to be handled carefully. If there are 742*c5cc2fb9SChris Lattner // reference members, we need to consider the size of the reference, not the 743*c5cc2fb9SChris Lattner // referencee. InitListExprs for unions and arrays can't have references. 744*c5cc2fb9SChris Lattner if (!E->getType()->isUnionType() && !E->getType()->isArrayType()) { 745*c5cc2fb9SChris Lattner RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 746*c5cc2fb9SChris Lattner uint64_t NumNonZeroBytes = 0; 747*c5cc2fb9SChris Lattner 748*c5cc2fb9SChris Lattner unsigned ILEElement = 0; 749*c5cc2fb9SChris Lattner for (RecordDecl::field_iterator Field = SD->field_begin(), 750*c5cc2fb9SChris Lattner FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) { 751*c5cc2fb9SChris Lattner // We're done once we hit the flexible array member or run out of 752*c5cc2fb9SChris Lattner // InitListExpr elements. 753*c5cc2fb9SChris Lattner if (Field->getType()->isIncompleteArrayType() || 754*c5cc2fb9SChris Lattner ILEElement == ILE->getNumInits()) 755*c5cc2fb9SChris Lattner break; 756*c5cc2fb9SChris Lattner if (Field->isUnnamedBitfield()) 757*c5cc2fb9SChris Lattner continue; 758*c5cc2fb9SChris Lattner 759*c5cc2fb9SChris Lattner const Expr *E = ILE->getInit(ILEElement++); 760*c5cc2fb9SChris Lattner 761*c5cc2fb9SChris Lattner // Reference values are always non-null and have the width of a pointer. 762*c5cc2fb9SChris Lattner if (Field->getType()->isReferenceType()) { 763*c5cc2fb9SChris Lattner NumNonZeroBytes += CGF.getContext().Target.getPointerWidth(0); 764*c5cc2fb9SChris Lattner continue; 765*c5cc2fb9SChris Lattner } 766*c5cc2fb9SChris Lattner 767*c5cc2fb9SChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF); 768*c5cc2fb9SChris Lattner } 769*c5cc2fb9SChris Lattner 770*c5cc2fb9SChris Lattner return NumNonZeroBytes; 771*c5cc2fb9SChris Lattner } 772*c5cc2fb9SChris Lattner 773*c5cc2fb9SChris Lattner 77427a3631bSChris Lattner uint64_t NumNonZeroBytes = 0; 77527a3631bSChris Lattner for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) 77627a3631bSChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF); 77727a3631bSChris Lattner return NumNonZeroBytes; 77827a3631bSChris Lattner } 77927a3631bSChris Lattner 78027a3631bSChris Lattner /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of 78127a3631bSChris Lattner /// zeros in it, emit a memset and avoid storing the individual zeros. 78227a3631bSChris Lattner /// 78327a3631bSChris Lattner static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E, 78427a3631bSChris Lattner CodeGenFunction &CGF) { 78527a3631bSChris Lattner // If the slot is already known to be zeroed, nothing to do. Don't mess with 78627a3631bSChris Lattner // volatile stores. 78727a3631bSChris Lattner if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return; 78827a3631bSChris Lattner 78927a3631bSChris Lattner // If the type is 16-bytes or smaller, prefer individual stores over memset. 79027a3631bSChris Lattner std::pair<uint64_t, unsigned> TypeInfo = 79127a3631bSChris Lattner CGF.getContext().getTypeInfo(E->getType()); 79227a3631bSChris Lattner if (TypeInfo.first/8 <= 16) 79327a3631bSChris Lattner return; 79427a3631bSChris Lattner 79527a3631bSChris Lattner // Check to see if over 3/4 of the initializer are known to be zero. If so, 79627a3631bSChris Lattner // we prefer to emit memset + individual stores for the rest. 79727a3631bSChris Lattner uint64_t NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF); 79827a3631bSChris Lattner if (NumNonZeroBytes*4 > TypeInfo.first/8) 79927a3631bSChris Lattner return; 80027a3631bSChris Lattner 80127a3631bSChris Lattner // Okay, it seems like a good idea to use an initial memset, emit the call. 80227a3631bSChris Lattner llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first/8); 80327a3631bSChris Lattner llvm::ConstantInt *AlignVal = CGF.Builder.getInt32(TypeInfo.second/8); 80427a3631bSChris Lattner 80527a3631bSChris Lattner llvm::Value *Loc = Slot.getAddr(); 80627a3631bSChris Lattner const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 80727a3631bSChris Lattner 80827a3631bSChris Lattner Loc = CGF.Builder.CreateBitCast(Loc, BP); 80927a3631bSChris Lattner CGF.Builder.CreateCall5(CGF.CGM.getMemSetFn(Loc->getType(), 81027a3631bSChris Lattner SizeVal->getType()), 81127a3631bSChris Lattner Loc, CGF.Builder.getInt8(0), SizeVal, AlignVal, 81227a3631bSChris Lattner CGF.Builder.getFalse()); 81327a3631bSChris Lattner 81427a3631bSChris Lattner // Tell the AggExprEmitter that the slot is known zero. 81527a3631bSChris Lattner Slot.setZeroed(); 81627a3631bSChris Lattner } 81727a3631bSChris Lattner 81827a3631bSChris Lattner 81927a3631bSChris Lattner 82027a3631bSChris Lattner 82125306cacSMike Stump /// EmitAggExpr - Emit the computation of the specified expression of aggregate 82225306cacSMike Stump /// type. The result is computed into DestPtr. Note that if DestPtr is null, 82325306cacSMike Stump /// the value of the aggregate expression is not needed. If VolatileDest is 82425306cacSMike Stump /// true, DestPtr cannot be 0. 8257a626f63SJohn McCall /// 8267a626f63SJohn McCall /// \param IsInitializer - true if this evaluation is initializing an 8277a626f63SJohn McCall /// object whose lifetime is already being managed. 828d0bc7b9dSDaniel Dunbar // 829d0bc7b9dSDaniel Dunbar // FIXME: Take Qualifiers object. 8307a626f63SJohn McCall void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot, 831b60e70f9SFariborz Jahanian bool IgnoreResult) { 8327a51313dSChris Lattner assert(E && hasAggregateLLVMType(E->getType()) && 8337a51313dSChris Lattner "Invalid aggregate expression to emit"); 83427a3631bSChris Lattner assert((Slot.getAddr() != 0 || Slot.isIgnored()) && 83527a3631bSChris Lattner "slot has bits but no address"); 8367a51313dSChris Lattner 83727a3631bSChris Lattner // Optimize the slot if possible. 83827a3631bSChris Lattner CheckAggExprForMemSetUse(Slot, E, *this); 83927a3631bSChris Lattner 84027a3631bSChris Lattner AggExprEmitter(*this, Slot, IgnoreResult).Visit(const_cast<Expr*>(E)); 8417a51313dSChris Lattner } 8420bc8e86dSDaniel Dunbar 843d0bc7b9dSDaniel Dunbar LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { 844d0bc7b9dSDaniel Dunbar assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!"); 845a7566f16SDaniel Dunbar llvm::Value *Temp = CreateMemTemp(E->getType()); 8462e442a00SDaniel Dunbar LValue LV = MakeAddrLValue(Temp, E->getType()); 84727a3631bSChris Lattner EmitAggExpr(E, AggValueSlot::forAddr(Temp, LV.isVolatileQualified(), false)); 8482e442a00SDaniel Dunbar return LV; 849d0bc7b9dSDaniel Dunbar } 850d0bc7b9dSDaniel Dunbar 8510bc8e86dSDaniel Dunbar void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 8525e9e61b8SMike Stump llvm::Value *SrcPtr, QualType Ty, 8535e9e61b8SMike Stump bool isVolatile) { 8540bc8e86dSDaniel Dunbar assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 8550bc8e86dSDaniel Dunbar 85616e94af6SAnders Carlsson if (getContext().getLangOptions().CPlusPlus) { 85716e94af6SAnders Carlsson if (const RecordType *RT = Ty->getAs<RecordType>()) { 858f22101a0SDouglas Gregor CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 859f22101a0SDouglas Gregor assert((Record->hasTrivialCopyConstructor() || 8606855ba2cSFariborz Jahanian Record->hasTrivialCopyAssignment()) && 861f22101a0SDouglas Gregor "Trying to aggregate-copy a type without a trivial copy " 862f22101a0SDouglas Gregor "constructor or assignment operator"); 863265b8b8dSDouglas Gregor // Ignore empty classes in C++. 864f22101a0SDouglas Gregor if (Record->isEmpty()) 86516e94af6SAnders Carlsson return; 86616e94af6SAnders Carlsson } 86716e94af6SAnders Carlsson } 86816e94af6SAnders Carlsson 869ca05dfefSChris Lattner // Aggregate assignment turns into llvm.memcpy. This is almost valid per 8703ef668c2SChris Lattner // C99 6.5.16.1p3, which states "If the value being stored in an object is 8713ef668c2SChris Lattner // read from another object that overlaps in anyway the storage of the first 8723ef668c2SChris Lattner // object, then the overlap shall be exact and the two objects shall have 8733ef668c2SChris Lattner // qualified or unqualified versions of a compatible type." 8743ef668c2SChris Lattner // 875ca05dfefSChris Lattner // memcpy is not defined if the source and destination pointers are exactly 8763ef668c2SChris Lattner // equal, but other compilers do this optimization, and almost every memcpy 8773ef668c2SChris Lattner // implementation handles this case safely. If there is a libc that does not 8783ef668c2SChris Lattner // safely handle this, we can add a target hook. 8790bc8e86dSDaniel Dunbar 8800bc8e86dSDaniel Dunbar // Get size and alignment info for this aggregate. 8810bc8e86dSDaniel Dunbar std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); 8820bc8e86dSDaniel Dunbar 8830bc8e86dSDaniel Dunbar // FIXME: Handle variable sized types. 8840bc8e86dSDaniel Dunbar 88586736572SMike Stump // FIXME: If we have a volatile struct, the optimizer can remove what might 88686736572SMike Stump // appear to be `extra' memory ops: 88786736572SMike Stump // 88886736572SMike Stump // volatile struct { int i; } a, b; 88986736572SMike Stump // 89086736572SMike Stump // int main() { 89186736572SMike Stump // a = b; 89286736572SMike Stump // a = b; 89386736572SMike Stump // } 89486736572SMike Stump // 895cc2ab0cdSMon P Wang // we need to use a different call here. We use isVolatile to indicate when 896ec3cbfe8SMike Stump // either the source or the destination is volatile. 897cc2ab0cdSMon P Wang 898cc2ab0cdSMon P Wang const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); 899cb7696cfSChris Lattner const llvm::Type *DBP = 900cb7696cfSChris Lattner llvm::Type::getInt8PtrTy(VMContext, DPT->getAddressSpace()); 901cc2ab0cdSMon P Wang DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp"); 902cc2ab0cdSMon P Wang 903cc2ab0cdSMon P Wang const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); 904cb7696cfSChris Lattner const llvm::Type *SBP = 905cb7696cfSChris Lattner llvm::Type::getInt8PtrTy(VMContext, SPT->getAddressSpace()); 906cc2ab0cdSMon P Wang SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp"); 907cc2ab0cdSMon P Wang 908021510e9SFariborz Jahanian if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { 909021510e9SFariborz Jahanian RecordDecl *Record = RecordTy->getDecl(); 910021510e9SFariborz Jahanian if (Record->hasObjectMember()) { 911021510e9SFariborz Jahanian unsigned long size = TypeInfo.first/8; 912021510e9SFariborz Jahanian const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 913021510e9SFariborz Jahanian llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); 914021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 915021510e9SFariborz Jahanian SizeVal); 916021510e9SFariborz Jahanian return; 917021510e9SFariborz Jahanian } 918021510e9SFariborz Jahanian } else if (getContext().getAsArrayType(Ty)) { 919021510e9SFariborz Jahanian QualType BaseType = getContext().getBaseElementType(Ty); 920021510e9SFariborz Jahanian if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { 921021510e9SFariborz Jahanian if (RecordTy->getDecl()->hasObjectMember()) { 922021510e9SFariborz Jahanian unsigned long size = TypeInfo.first/8; 923021510e9SFariborz Jahanian const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 924021510e9SFariborz Jahanian llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); 925021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 926021510e9SFariborz Jahanian SizeVal); 927021510e9SFariborz Jahanian return; 928021510e9SFariborz Jahanian } 929021510e9SFariborz Jahanian } 930021510e9SFariborz Jahanian } 931021510e9SFariborz Jahanian 932cc2ab0cdSMon P Wang Builder.CreateCall5(CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(), 9335e016ae9SChris Lattner IntPtrTy), 9340bc8e86dSDaniel Dunbar DestPtr, SrcPtr, 9350bc8e86dSDaniel Dunbar // TypeInfo.first describes size in bits. 9365e016ae9SChris Lattner llvm::ConstantInt::get(IntPtrTy, TypeInfo.first/8), 937cb7696cfSChris Lattner Builder.getInt32(TypeInfo.second/8), 938cb7696cfSChris Lattner Builder.getInt1(isVolatile)); 9390bc8e86dSDaniel Dunbar } 940