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 38a5efa738SJohn McCall /// We want to use 'dest' as the return slot except under two 39a5efa738SJohn McCall /// conditions: 40a5efa738SJohn McCall /// - The destination slot requires garbage collection, so we 41a5efa738SJohn McCall /// need to use the GC API. 42a5efa738SJohn McCall /// - The destination slot is potentially aliased. 43a5efa738SJohn McCall bool shouldUseDestForReturnSlot() const { 44a5efa738SJohn McCall return !(Dest.requiresGCollection() || Dest.isPotentiallyAliased()); 45a5efa738SJohn McCall } 46a5efa738SJohn McCall 4778a15113SJohn McCall ReturnValueSlot getReturnValueSlot() const { 48a5efa738SJohn McCall if (!shouldUseDestForReturnSlot()) 49a5efa738SJohn McCall return ReturnValueSlot(); 50cc04e9f6SJohn McCall 517a626f63SJohn McCall return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile()); 527a626f63SJohn McCall } 537a626f63SJohn McCall 547a626f63SJohn McCall AggValueSlot EnsureSlot(QualType T) { 557a626f63SJohn McCall if (!Dest.isIgnored()) return Dest; 567a626f63SJohn McCall return CGF.CreateAggTemp(T, "agg.tmp.ensured"); 5778a15113SJohn McCall } 58cc04e9f6SJohn McCall 597a51313dSChris Lattner public: 607a626f63SJohn McCall AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest, 61b60e70f9SFariborz Jahanian bool ignore) 627a626f63SJohn McCall : CGF(cgf), Builder(CGF.Builder), Dest(Dest), 63b60e70f9SFariborz Jahanian IgnoreResult(ignore) { 647a51313dSChris Lattner } 657a51313dSChris Lattner 667a51313dSChris Lattner //===--------------------------------------------------------------------===// 677a51313dSChris Lattner // Utilities 687a51313dSChris Lattner //===--------------------------------------------------------------------===// 697a51313dSChris Lattner 707a51313dSChris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that 717a51313dSChris Lattner /// represents a value lvalue, this method emits the address of the lvalue, 727a51313dSChris Lattner /// then loads the result into DestPtr. 737a51313dSChris Lattner void EmitAggLoadOfLValue(const Expr *E); 747a51313dSChris Lattner 75ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 76ec3cbfe8SMike Stump void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false); 77ec3cbfe8SMike Stump void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false); 78ca9fc09cSMike Stump 79a5efa738SJohn McCall void EmitMoveFromReturnSlot(const Expr *E, RValue Src); 80cc04e9f6SJohn McCall 818d6fc958SJohn McCall AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) { 8279a91418SDouglas Gregor if (CGF.getLangOptions().getGC() && TypeRequiresGCollection(T)) 838d6fc958SJohn McCall return AggValueSlot::NeedsGCBarriers; 848d6fc958SJohn McCall return AggValueSlot::DoesNotNeedGCBarriers; 858d6fc958SJohn McCall } 868d6fc958SJohn McCall 87cc04e9f6SJohn McCall bool TypeRequiresGCollection(QualType T); 88cc04e9f6SJohn McCall 897a51313dSChris Lattner //===--------------------------------------------------------------------===// 907a51313dSChris Lattner // Visitor Methods 917a51313dSChris Lattner //===--------------------------------------------------------------------===// 927a51313dSChris Lattner 937a51313dSChris Lattner void VisitStmt(Stmt *S) { 94a7c8cf62SDaniel Dunbar CGF.ErrorUnsupported(S, "aggregate expression"); 957a51313dSChris Lattner } 967a51313dSChris Lattner void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } 9791147596SPeter Collingbourne void VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 9891147596SPeter Collingbourne Visit(GE->getResultExpr()); 9991147596SPeter Collingbourne } 1003f66b84cSEli Friedman void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 1017c454bb8SJohn McCall void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) { 1027c454bb8SJohn McCall return Visit(E->getReplacement()); 1037c454bb8SJohn McCall } 1047a51313dSChris Lattner 1057a51313dSChris Lattner // l-values. 1067a51313dSChris Lattner void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } 1077a51313dSChris Lattner void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 1087a51313dSChris Lattner void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 109d443c0a0SDaniel Dunbar void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 1109b71f0cfSDouglas Gregor void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 1117a51313dSChris Lattner void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 1127a51313dSChris Lattner EmitAggLoadOfLValue(E); 1137a51313dSChris Lattner } 1142f343dd5SChris Lattner void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 1152f343dd5SChris Lattner EmitAggLoadOfLValue(E); 1162f343dd5SChris Lattner } 1172f343dd5SChris Lattner void VisitPredefinedExpr(const PredefinedExpr *E) { 1182f343dd5SChris Lattner EmitAggLoadOfLValue(E); 1192f343dd5SChris Lattner } 120bc7d67ceSMike Stump 1217a51313dSChris Lattner // Operators. 122ec143777SAnders Carlsson void VisitCastExpr(CastExpr *E); 1237a51313dSChris Lattner void VisitCallExpr(const CallExpr *E); 1247a51313dSChris Lattner void VisitStmtExpr(const StmtExpr *E); 1257a51313dSChris Lattner void VisitBinaryOperator(const BinaryOperator *BO); 126ffba662dSFariborz Jahanian void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); 1277a51313dSChris Lattner void VisitBinAssign(const BinaryOperator *E); 1284b0e2a30SEli Friedman void VisitBinComma(const BinaryOperator *E); 1297a51313dSChris Lattner 130b1d329daSChris Lattner void VisitObjCMessageExpr(ObjCMessageExpr *E); 131c8317a44SDaniel Dunbar void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 132c8317a44SDaniel Dunbar EmitAggLoadOfLValue(E); 133c8317a44SDaniel Dunbar } 13455310df7SDaniel Dunbar void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); 1357a51313dSChris Lattner 136c07a0c7eSJohn McCall void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); 1375b2095ceSAnders Carlsson void VisitChooseExpr(const ChooseExpr *CE); 1387a51313dSChris Lattner void VisitInitListExpr(InitListExpr *E); 13918ada985SAnders Carlsson void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); 140aa9c7aedSChris Lattner void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 141aa9c7aedSChris Lattner Visit(DAE->getExpr()); 142aa9c7aedSChris Lattner } 1433be22e27SAnders Carlsson void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 1441619a504SAnders Carlsson void VisitCXXConstructExpr(const CXXConstructExpr *E); 1455d413781SJohn McCall void VisitExprWithCleanups(ExprWithCleanups *E); 146747eb784SDouglas Gregor void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 1475bbbb137SMike Stump void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } 148fe31481fSDouglas Gregor void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); 1491bf5846aSJohn McCall void VisitOpaqueValueExpr(OpaqueValueExpr *E); 1501bf5846aSJohn McCall 15121911e89SEli Friedman void VisitVAArgExpr(VAArgExpr *E); 152579a05d7SChris Lattner 1531553b190SJohn McCall void EmitInitializationToLValue(Expr *E, LValue Address); 1541553b190SJohn McCall void EmitNullInitializationToLValue(LValue Address); 1557a51313dSChris Lattner // case Expr::ChooseExprClass: 156f16b8c30SMike Stump void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } 157df14b3a8SEli Friedman void VisitAtomicExpr(AtomicExpr *E) { 158df14b3a8SEli Friedman CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr()); 159df14b3a8SEli Friedman } 1607a51313dSChris Lattner }; 1617a51313dSChris Lattner } // end anonymous namespace. 1627a51313dSChris Lattner 1637a51313dSChris Lattner //===----------------------------------------------------------------------===// 1647a51313dSChris Lattner // Utilities 1657a51313dSChris Lattner //===----------------------------------------------------------------------===// 1667a51313dSChris Lattner 1677a51313dSChris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that 1687a51313dSChris Lattner /// represents a value lvalue, this method emits the address of the lvalue, 1697a51313dSChris Lattner /// then loads the result into DestPtr. 1707a51313dSChris Lattner void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 1717a51313dSChris Lattner LValue LV = CGF.EmitLValue(E); 172ca9fc09cSMike Stump EmitFinalDestCopy(E, LV); 173ca9fc09cSMike Stump } 174ca9fc09cSMike Stump 175cc04e9f6SJohn McCall /// \brief True if the given aggregate type requires special GC API calls. 176cc04e9f6SJohn McCall bool AggExprEmitter::TypeRequiresGCollection(QualType T) { 177cc04e9f6SJohn McCall // Only record types have members that might require garbage collection. 178cc04e9f6SJohn McCall const RecordType *RecordTy = T->getAs<RecordType>(); 179cc04e9f6SJohn McCall if (!RecordTy) return false; 180cc04e9f6SJohn McCall 181cc04e9f6SJohn McCall // Don't mess with non-trivial C++ types. 182cc04e9f6SJohn McCall RecordDecl *Record = RecordTy->getDecl(); 183cc04e9f6SJohn McCall if (isa<CXXRecordDecl>(Record) && 184cc04e9f6SJohn McCall (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() || 185cc04e9f6SJohn McCall !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) 186cc04e9f6SJohn McCall return false; 187cc04e9f6SJohn McCall 188cc04e9f6SJohn McCall // Check whether the type has an object member. 189cc04e9f6SJohn McCall return Record->hasObjectMember(); 190cc04e9f6SJohn McCall } 191cc04e9f6SJohn McCall 192a5efa738SJohn McCall /// \brief Perform the final move to DestPtr if for some reason 193a5efa738SJohn McCall /// getReturnValueSlot() didn't use it directly. 194cc04e9f6SJohn McCall /// 195cc04e9f6SJohn McCall /// The idea is that you do something like this: 196cc04e9f6SJohn McCall /// RValue Result = EmitSomething(..., getReturnValueSlot()); 197a5efa738SJohn McCall /// EmitMoveFromReturnSlot(E, Result); 198a5efa738SJohn McCall /// 199a5efa738SJohn McCall /// If nothing interferes, this will cause the result to be emitted 200a5efa738SJohn McCall /// directly into the return value slot. Otherwise, a final move 201a5efa738SJohn McCall /// will be performed. 202a5efa738SJohn McCall void AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue Src) { 203a5efa738SJohn McCall if (shouldUseDestForReturnSlot()) { 204a5efa738SJohn McCall // Logically, Dest.getAddr() should equal Src.getAggregateAddr(). 205a5efa738SJohn McCall // The possibility of undef rvalues complicates that a lot, 206a5efa738SJohn McCall // though, so we can't really assert. 207a5efa738SJohn McCall return; 208021510e9SFariborz Jahanian } 209a5efa738SJohn McCall 210a5efa738SJohn McCall // Otherwise, do a final copy, 211a5efa738SJohn McCall assert(Dest.getAddr() != Src.getAggregateAddr()); 212a5efa738SJohn McCall EmitFinalDestCopy(E, Src, /*Ignore*/ true); 213cc04e9f6SJohn McCall } 214cc04e9f6SJohn McCall 215ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 216ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) { 217ca9fc09cSMike Stump assert(Src.isAggregate() && "value must be aggregate value!"); 2187a51313dSChris Lattner 2197a626f63SJohn McCall // If Dest is ignored, then we're evaluating an aggregate expression 2208d752430SJohn McCall // in a context (like an expression statement) that doesn't care 2218d752430SJohn McCall // about the result. C says that an lvalue-to-rvalue conversion is 2228d752430SJohn McCall // performed in these cases; C++ says that it is not. In either 2238d752430SJohn McCall // case, we don't actually need to do anything unless the value is 2248d752430SJohn McCall // volatile. 2257a626f63SJohn McCall if (Dest.isIgnored()) { 2268d752430SJohn McCall if (!Src.isVolatileQualified() || 2278d752430SJohn McCall CGF.CGM.getLangOptions().CPlusPlus || 2288d752430SJohn McCall (IgnoreResult && Ignore)) 229ec3cbfe8SMike Stump return; 230c123623dSFariborz Jahanian 231332ec2ceSMike Stump // If the source is volatile, we must read from it; to do that, we need 232332ec2ceSMike Stump // some place to put it. 2337a626f63SJohn McCall Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp"); 234332ec2ceSMike Stump } 2357a51313dSChris Lattner 23658649dc6SJohn McCall if (Dest.requiresGCollection()) { 2373b4bd9a1SKen Dyck CharUnits size = CGF.getContext().getTypeSizeInChars(E->getType()); 2382192fe50SChris Lattner llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 2393b4bd9a1SKen Dyck llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 240879d7266SFariborz Jahanian CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 2417a626f63SJohn McCall Dest.getAddr(), 2427a626f63SJohn McCall Src.getAggregateAddr(), 243021510e9SFariborz Jahanian SizeVal); 244879d7266SFariborz Jahanian return; 245879d7266SFariborz Jahanian } 246ca9fc09cSMike Stump // If the result of the assignment is used, copy the LHS there also. 247ca9fc09cSMike Stump // FIXME: Pass VolatileDest as well. I think we also need to merge volatile 248ca9fc09cSMike Stump // from the source as well, as we can't eliminate it if either operand 249ca9fc09cSMike Stump // is volatile, unless copy has volatile for both source and destination.. 2507a626f63SJohn McCall CGF.EmitAggregateCopy(Dest.getAddr(), Src.getAggregateAddr(), E->getType(), 2517a626f63SJohn McCall Dest.isVolatile()|Src.isVolatileQualified()); 252ca9fc09cSMike Stump } 253ca9fc09cSMike Stump 254ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 255ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { 256ca9fc09cSMike Stump assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); 257ca9fc09cSMike Stump 258ca9fc09cSMike Stump EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(), 259ec3cbfe8SMike Stump Src.isVolatileQualified()), 260ec3cbfe8SMike Stump Ignore); 2617a51313dSChris Lattner } 2627a51313dSChris Lattner 2637a51313dSChris Lattner //===----------------------------------------------------------------------===// 2647a51313dSChris Lattner // Visitor Methods 2657a51313dSChris Lattner //===----------------------------------------------------------------------===// 2667a51313dSChris Lattner 267fe31481fSDouglas Gregor void AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){ 268fe31481fSDouglas Gregor Visit(E->GetTemporaryExpr()); 269fe31481fSDouglas Gregor } 270fe31481fSDouglas Gregor 2711bf5846aSJohn McCall void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) { 272c07a0c7eSJohn McCall EmitFinalDestCopy(e, CGF.getOpaqueLValueMapping(e)); 2731bf5846aSJohn McCall } 2741bf5846aSJohn McCall 2759b71f0cfSDouglas Gregor void 2769b71f0cfSDouglas Gregor AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 2776c9d31ebSDouglas Gregor if (E->getType().isPODType(CGF.getContext())) { 2786c9d31ebSDouglas Gregor // For a POD type, just emit a load of the lvalue + a copy, because our 2796c9d31ebSDouglas Gregor // compound literal might alias the destination. 2806c9d31ebSDouglas Gregor // FIXME: This is a band-aid; the real problem appears to be in our handling 2816c9d31ebSDouglas Gregor // of assignments, where we store directly into the LHS without checking 2826c9d31ebSDouglas Gregor // whether anything in the RHS aliases. 2836c9d31ebSDouglas Gregor EmitAggLoadOfLValue(E); 2846c9d31ebSDouglas Gregor return; 2856c9d31ebSDouglas Gregor } 2866c9d31ebSDouglas Gregor 2879b71f0cfSDouglas Gregor AggValueSlot Slot = EnsureSlot(E->getType()); 2889b71f0cfSDouglas Gregor CGF.EmitAggExpr(E->getInitializer(), Slot); 2899b71f0cfSDouglas Gregor } 2909b71f0cfSDouglas Gregor 2919b71f0cfSDouglas Gregor 292ec143777SAnders Carlsson void AggExprEmitter::VisitCastExpr(CastExpr *E) { 2931fb7ae9eSAnders Carlsson switch (E->getCastKind()) { 2948a01a751SAnders Carlsson case CK_Dynamic: { 2951c073f47SDouglas Gregor assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?"); 2961c073f47SDouglas Gregor LValue LV = CGF.EmitCheckedLValue(E->getSubExpr()); 2971c073f47SDouglas Gregor // FIXME: Do we also need to handle property references here? 2981c073f47SDouglas Gregor if (LV.isSimple()) 2991c073f47SDouglas Gregor CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E)); 3001c073f47SDouglas Gregor else 3011c073f47SDouglas Gregor CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast"); 3021c073f47SDouglas Gregor 3037a626f63SJohn McCall if (!Dest.isIgnored()) 3041c073f47SDouglas Gregor CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination"); 3051c073f47SDouglas Gregor break; 3061c073f47SDouglas Gregor } 3071c073f47SDouglas Gregor 308e302792bSJohn McCall case CK_ToUnion: { 30958989b71SJohn McCall if (Dest.isIgnored()) break; 31058989b71SJohn McCall 3117ffcf93bSNuno Lopes // GCC union extension 3122e442a00SDaniel Dunbar QualType Ty = E->getSubExpr()->getType(); 3132e442a00SDaniel Dunbar QualType PtrTy = CGF.getContext().getPointerType(Ty); 3147a626f63SJohn McCall llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(), 315dd274848SEli Friedman CGF.ConvertType(PtrTy)); 3161553b190SJohn McCall EmitInitializationToLValue(E->getSubExpr(), 3171553b190SJohn McCall CGF.MakeAddrLValue(CastPtr, Ty)); 3181fb7ae9eSAnders Carlsson break; 3197ffcf93bSNuno Lopes } 3207ffcf93bSNuno Lopes 321e302792bSJohn McCall case CK_DerivedToBase: 322e302792bSJohn McCall case CK_BaseToDerived: 323e302792bSJohn McCall case CK_UncheckedDerivedToBase: { 32483d382b1SDavid Blaikie llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: " 325aae38d66SDouglas Gregor "should have been unpacked before we got here"); 326aae38d66SDouglas Gregor } 327aae38d66SDouglas Gregor 32834376a68SJohn McCall case CK_GetObjCProperty: { 329*526ab47aSJohn McCall LValue LV = 330*526ab47aSJohn McCall CGF.EmitObjCPropertyRefLValue(E->getSubExpr()->getObjCProperty()); 33134376a68SJohn McCall assert(LV.isPropertyRef()); 33234376a68SJohn McCall RValue RV = CGF.EmitLoadOfPropertyRefLValue(LV, getReturnValueSlot()); 333a5efa738SJohn McCall EmitMoveFromReturnSlot(E, RV); 33434376a68SJohn McCall break; 33534376a68SJohn McCall } 33634376a68SJohn McCall 33734376a68SJohn McCall case CK_LValueToRValue: // hope for downstream optimization 338e302792bSJohn McCall case CK_NoOp: 339e302792bSJohn McCall case CK_UserDefinedConversion: 340e302792bSJohn McCall case CK_ConstructorConversion: 3412a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 3422a69547fSEli Friedman E->getType()) && 3430f398c44SChris Lattner "Implicit cast types must be compatible"); 3447a51313dSChris Lattner Visit(E->getSubExpr()); 3451fb7ae9eSAnders Carlsson break; 346b05a3e55SAnders Carlsson 347e302792bSJohn McCall case CK_LValueBitCast: 348f3735e01SJohn McCall llvm_unreachable("should not be emitting lvalue bitcast as rvalue"); 34951954276SDouglas Gregor break; 35031996343SJohn McCall 351f3735e01SJohn McCall case CK_Dependent: 352f3735e01SJohn McCall case CK_BitCast: 353f3735e01SJohn McCall case CK_ArrayToPointerDecay: 354f3735e01SJohn McCall case CK_FunctionToPointerDecay: 355f3735e01SJohn McCall case CK_NullToPointer: 356f3735e01SJohn McCall case CK_NullToMemberPointer: 357f3735e01SJohn McCall case CK_BaseToDerivedMemberPointer: 358f3735e01SJohn McCall case CK_DerivedToBaseMemberPointer: 359f3735e01SJohn McCall case CK_MemberPointerToBoolean: 360f3735e01SJohn McCall case CK_IntegralToPointer: 361f3735e01SJohn McCall case CK_PointerToIntegral: 362f3735e01SJohn McCall case CK_PointerToBoolean: 363f3735e01SJohn McCall case CK_ToVoid: 364f3735e01SJohn McCall case CK_VectorSplat: 365f3735e01SJohn McCall case CK_IntegralCast: 366f3735e01SJohn McCall case CK_IntegralToBoolean: 367f3735e01SJohn McCall case CK_IntegralToFloating: 368f3735e01SJohn McCall case CK_FloatingToIntegral: 369f3735e01SJohn McCall case CK_FloatingToBoolean: 370f3735e01SJohn McCall case CK_FloatingCast: 3719320b87cSJohn McCall case CK_CPointerToObjCPointerCast: 3729320b87cSJohn McCall case CK_BlockPointerToObjCPointerCast: 373f3735e01SJohn McCall case CK_AnyPointerToBlockPointerCast: 374f3735e01SJohn McCall case CK_ObjCObjectLValueCast: 375f3735e01SJohn McCall case CK_FloatingRealToComplex: 376f3735e01SJohn McCall case CK_FloatingComplexToReal: 377f3735e01SJohn McCall case CK_FloatingComplexToBoolean: 378f3735e01SJohn McCall case CK_FloatingComplexCast: 379f3735e01SJohn McCall case CK_FloatingComplexToIntegralComplex: 380f3735e01SJohn McCall case CK_IntegralRealToComplex: 381f3735e01SJohn McCall case CK_IntegralComplexToReal: 382f3735e01SJohn McCall case CK_IntegralComplexToBoolean: 383f3735e01SJohn McCall case CK_IntegralComplexCast: 384f3735e01SJohn McCall case CK_IntegralComplexToFloatingComplex: 3852d637d2eSJohn McCall case CK_ARCProduceObject: 3862d637d2eSJohn McCall case CK_ARCConsumeObject: 3872d637d2eSJohn McCall case CK_ARCReclaimReturnedObject: 3882d637d2eSJohn McCall case CK_ARCExtendBlockObject: 389f3735e01SJohn McCall llvm_unreachable("cast kind invalid for aggregate types"); 3901fb7ae9eSAnders Carlsson } 3917a51313dSChris Lattner } 3927a51313dSChris Lattner 3930f398c44SChris Lattner void AggExprEmitter::VisitCallExpr(const CallExpr *E) { 394ddcbfe7bSAnders Carlsson if (E->getCallReturnType()->isReferenceType()) { 395ddcbfe7bSAnders Carlsson EmitAggLoadOfLValue(E); 396ddcbfe7bSAnders Carlsson return; 397ddcbfe7bSAnders Carlsson } 398ddcbfe7bSAnders Carlsson 399cc04e9f6SJohn McCall RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); 400a5efa738SJohn McCall EmitMoveFromReturnSlot(E, RV); 4017a51313dSChris Lattner } 4020f398c44SChris Lattner 4030f398c44SChris Lattner void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 404cc04e9f6SJohn McCall RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); 405a5efa738SJohn McCall EmitMoveFromReturnSlot(E, RV); 406b1d329daSChris Lattner } 4077a51313dSChris Lattner 40855310df7SDaniel Dunbar void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 40934376a68SJohn McCall llvm_unreachable("direct property access not surrounded by " 41034376a68SJohn McCall "lvalue-to-rvalue cast"); 41155310df7SDaniel Dunbar } 41255310df7SDaniel Dunbar 4130f398c44SChris Lattner void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 414a2342eb8SJohn McCall CGF.EmitIgnoredExpr(E->getLHS()); 4157a626f63SJohn McCall Visit(E->getRHS()); 4164b0e2a30SEli Friedman } 4174b0e2a30SEli Friedman 4187a51313dSChris Lattner void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 419ce1de617SJohn McCall CodeGenFunction::StmtExprEvaluation eval(CGF); 4207a626f63SJohn McCall CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest); 4217a51313dSChris Lattner } 4227a51313dSChris Lattner 4237a51313dSChris Lattner void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 424e302792bSJohn McCall if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI) 425ffba662dSFariborz Jahanian VisitPointerToDataMemberBinaryOperator(E); 426ffba662dSFariborz Jahanian else 427a7c8cf62SDaniel Dunbar CGF.ErrorUnsupported(E, "aggregate binary expression"); 4287a51313dSChris Lattner } 4297a51313dSChris Lattner 430ffba662dSFariborz Jahanian void AggExprEmitter::VisitPointerToDataMemberBinaryOperator( 431ffba662dSFariborz Jahanian const BinaryOperator *E) { 432ffba662dSFariborz Jahanian LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); 433ffba662dSFariborz Jahanian EmitFinalDestCopy(E, LV); 434ffba662dSFariborz Jahanian } 435ffba662dSFariborz Jahanian 4367a51313dSChris Lattner void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 4377a51313dSChris Lattner // For an assignment to work, the value on the right has 4387a51313dSChris Lattner // to be compatible with the value on the left. 4392a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 4402a69547fSEli Friedman E->getRHS()->getType()) 4417a51313dSChris Lattner && "Invalid assignment"); 442d0a30016SJohn McCall 44399514b91SFariborz Jahanian if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->getLHS())) 44452a8cca5SFariborz Jahanian if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) 44599514b91SFariborz Jahanian if (VD->hasAttr<BlocksAttr>() && 44699514b91SFariborz Jahanian E->getRHS()->HasSideEffects(CGF.getContext())) { 44799514b91SFariborz Jahanian // When __block variable on LHS, the RHS must be evaluated first 44899514b91SFariborz Jahanian // as it may change the 'forwarding' field via call to Block_copy. 44999514b91SFariborz Jahanian LValue RHS = CGF.EmitLValue(E->getRHS()); 45099514b91SFariborz Jahanian LValue LHS = CGF.EmitLValue(E->getLHS()); 4518d6fc958SJohn McCall Dest = AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 45246759f4fSJohn McCall needsGC(E->getLHS()->getType()), 45346759f4fSJohn McCall AggValueSlot::IsAliased); 45499514b91SFariborz Jahanian EmitFinalDestCopy(E, RHS, true); 45599514b91SFariborz Jahanian return; 45699514b91SFariborz Jahanian } 45799514b91SFariborz Jahanian 4587a51313dSChris Lattner LValue LHS = CGF.EmitLValue(E->getLHS()); 4597a51313dSChris Lattner 4604b8c6db9SDaniel Dunbar // We have to special case property setters, otherwise we must have 4614b8c6db9SDaniel Dunbar // a simple lvalue (no aggregates inside vectors, bitfields). 4624b8c6db9SDaniel Dunbar if (LHS.isPropertyRef()) { 4637a26ba4dSFariborz Jahanian const ObjCPropertyRefExpr *RE = LHS.getPropertyRefExpr(); 4647a26ba4dSFariborz Jahanian QualType ArgType = RE->getSetterArgType(); 4657a26ba4dSFariborz Jahanian RValue Src; 4667a26ba4dSFariborz Jahanian if (ArgType->isReferenceType()) 4677a26ba4dSFariborz Jahanian Src = CGF.EmitReferenceBindingToExpr(E->getRHS(), 0); 4687a26ba4dSFariborz Jahanian else { 4697a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getRHS()->getType()); 4707a626f63SJohn McCall CGF.EmitAggExpr(E->getRHS(), Slot); 4717a26ba4dSFariborz Jahanian Src = Slot.asRValue(); 4727a26ba4dSFariborz Jahanian } 4737a26ba4dSFariborz Jahanian CGF.EmitStoreThroughPropertyRefLValue(Src, LHS); 4744b8c6db9SDaniel Dunbar } else { 4757a51313dSChris Lattner // Codegen the RHS so that it stores directly into the LHS. 4768d6fc958SJohn McCall AggValueSlot LHSSlot = 4778d6fc958SJohn McCall AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 47846759f4fSJohn McCall needsGC(E->getLHS()->getType()), 47946759f4fSJohn McCall AggValueSlot::IsAliased); 480b60e70f9SFariborz Jahanian CGF.EmitAggExpr(E->getRHS(), LHSSlot, false); 481ec3cbfe8SMike Stump EmitFinalDestCopy(E, LHS, true); 4827a51313dSChris Lattner } 4834b8c6db9SDaniel Dunbar } 4847a51313dSChris Lattner 485c07a0c7eSJohn McCall void AggExprEmitter:: 486c07a0c7eSJohn McCall VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 487a612e79bSDaniel Dunbar llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 488a612e79bSDaniel Dunbar llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 489a612e79bSDaniel Dunbar llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 4907a51313dSChris Lattner 491c07a0c7eSJohn McCall // Bind the common expression if necessary. 492c07a0c7eSJohn McCall CodeGenFunction::OpaqueValueMapping binding(CGF, E); 493c07a0c7eSJohn McCall 494ce1de617SJohn McCall CodeGenFunction::ConditionalEvaluation eval(CGF); 495b8841af8SEli Friedman CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 4967a51313dSChris Lattner 4975b26f65bSJohn McCall // Save whether the destination's lifetime is externally managed. 498cac93853SJohn McCall bool isExternallyDestructed = Dest.isExternallyDestructed(); 4997a51313dSChris Lattner 500ce1de617SJohn McCall eval.begin(CGF); 501ce1de617SJohn McCall CGF.EmitBlock(LHSBlock); 502c07a0c7eSJohn McCall Visit(E->getTrueExpr()); 503ce1de617SJohn McCall eval.end(CGF); 5047a51313dSChris Lattner 505ce1de617SJohn McCall assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!"); 506ce1de617SJohn McCall CGF.Builder.CreateBr(ContBlock); 5077a51313dSChris Lattner 5085b26f65bSJohn McCall // If the result of an agg expression is unused, then the emission 5095b26f65bSJohn McCall // of the LHS might need to create a destination slot. That's fine 5105b26f65bSJohn McCall // with us, and we can safely emit the RHS into the same slot, but 511cac93853SJohn McCall // we shouldn't claim that it's already being destructed. 512cac93853SJohn McCall Dest.setExternallyDestructed(isExternallyDestructed); 5135b26f65bSJohn McCall 514ce1de617SJohn McCall eval.begin(CGF); 515ce1de617SJohn McCall CGF.EmitBlock(RHSBlock); 516c07a0c7eSJohn McCall Visit(E->getFalseExpr()); 517ce1de617SJohn McCall eval.end(CGF); 5187a51313dSChris Lattner 5197a51313dSChris Lattner CGF.EmitBlock(ContBlock); 5207a51313dSChris Lattner } 5217a51313dSChris Lattner 5225b2095ceSAnders Carlsson void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 5235b2095ceSAnders Carlsson Visit(CE->getChosenSubExpr(CGF.getContext())); 5245b2095ceSAnders Carlsson } 5255b2095ceSAnders Carlsson 52621911e89SEli Friedman void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 527e9fcadd2SDaniel Dunbar llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 52813abd7e9SAnders Carlsson llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 52913abd7e9SAnders Carlsson 530020cddcfSSebastian Redl if (!ArgPtr) { 53113abd7e9SAnders Carlsson CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 532020cddcfSSebastian Redl return; 533020cddcfSSebastian Redl } 53413abd7e9SAnders Carlsson 5352e442a00SDaniel Dunbar EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType())); 53621911e89SEli Friedman } 53721911e89SEli Friedman 5383be22e27SAnders Carlsson void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 5397a626f63SJohn McCall // Ensure that we have a slot, but if we already do, remember 540cac93853SJohn McCall // whether it was externally destructed. 541cac93853SJohn McCall bool wasExternallyDestructed = Dest.isExternallyDestructed(); 5427a626f63SJohn McCall Dest = EnsureSlot(E->getType()); 543cac93853SJohn McCall 544cac93853SJohn McCall // We're going to push a destructor if there isn't already one. 545cac93853SJohn McCall Dest.setExternallyDestructed(); 5463be22e27SAnders Carlsson 5473be22e27SAnders Carlsson Visit(E->getSubExpr()); 5483be22e27SAnders Carlsson 549cac93853SJohn McCall // Push that destructor we promised. 550cac93853SJohn McCall if (!wasExternallyDestructed) 5517a626f63SJohn McCall CGF.EmitCXXTemporary(E->getTemporary(), Dest.getAddr()); 5523be22e27SAnders Carlsson } 5533be22e27SAnders Carlsson 554b7f8f594SAnders Carlsson void 5551619a504SAnders Carlsson AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 5567a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getType()); 5577a626f63SJohn McCall CGF.EmitCXXConstructExpr(E, Slot); 558c82b86dfSAnders Carlsson } 559c82b86dfSAnders Carlsson 5605d413781SJohn McCall void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) { 5615d413781SJohn McCall CGF.EmitExprWithCleanups(E, Dest); 562b7f8f594SAnders Carlsson } 563b7f8f594SAnders Carlsson 564747eb784SDouglas Gregor void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 5657a626f63SJohn McCall QualType T = E->getType(); 5667a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 5671553b190SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 56818ada985SAnders Carlsson } 56918ada985SAnders Carlsson 57018ada985SAnders Carlsson void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 5717a626f63SJohn McCall QualType T = E->getType(); 5727a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 5731553b190SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 574ff3507b9SNuno Lopes } 575ff3507b9SNuno Lopes 57627a3631bSChris Lattner /// isSimpleZero - If emitting this value will obviously just cause a store of 57727a3631bSChris Lattner /// zero to memory, return true. This can return false if uncertain, so it just 57827a3631bSChris Lattner /// handles simple cases. 57927a3631bSChris Lattner static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { 58091147596SPeter Collingbourne E = E->IgnoreParens(); 58191147596SPeter Collingbourne 58227a3631bSChris Lattner // 0 58327a3631bSChris Lattner if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) 58427a3631bSChris Lattner return IL->getValue() == 0; 58527a3631bSChris Lattner // +0.0 58627a3631bSChris Lattner if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E)) 58727a3631bSChris Lattner return FL->getValue().isPosZero(); 58827a3631bSChris Lattner // int() 58927a3631bSChris Lattner if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) && 59027a3631bSChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 59127a3631bSChris Lattner return true; 59227a3631bSChris Lattner // (int*)0 - Null pointer expressions. 59327a3631bSChris Lattner if (const CastExpr *ICE = dyn_cast<CastExpr>(E)) 59427a3631bSChris Lattner return ICE->getCastKind() == CK_NullToPointer; 59527a3631bSChris Lattner // '\0' 59627a3631bSChris Lattner if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) 59727a3631bSChris Lattner return CL->getValue() == 0; 59827a3631bSChris Lattner 59927a3631bSChris Lattner // Otherwise, hard case: conservatively return false. 60027a3631bSChris Lattner return false; 60127a3631bSChris Lattner } 60227a3631bSChris Lattner 60327a3631bSChris Lattner 604b247350eSAnders Carlsson void 6051553b190SJohn McCall AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { 6061553b190SJohn McCall QualType type = LV.getType(); 607df0fe27bSMike Stump // FIXME: Ignore result? 608579a05d7SChris Lattner // FIXME: Are initializers affected by volatile? 60927a3631bSChris Lattner if (Dest.isZeroed() && isSimpleZero(E, CGF)) { 61027a3631bSChris Lattner // Storing "i32 0" to a zero'd memory location is a noop. 61127a3631bSChris Lattner } else if (isa<ImplicitValueInitExpr>(E)) { 6121553b190SJohn McCall EmitNullInitializationToLValue(LV); 6131553b190SJohn McCall } else if (type->isReferenceType()) { 61404775f84SAnders Carlsson RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); 61555e1fbc8SJohn McCall CGF.EmitStoreThroughLValue(RV, LV); 6161553b190SJohn McCall } else if (type->isAnyComplexType()) { 6170202cb40SDouglas Gregor CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); 6181553b190SJohn McCall } else if (CGF.hasAggregateLLVMType(type)) { 6198d6fc958SJohn McCall CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV, 6208d6fc958SJohn McCall AggValueSlot::IsDestructed, 6218d6fc958SJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 622a5efa738SJohn McCall AggValueSlot::IsNotAliased, 6231553b190SJohn McCall Dest.isZeroed())); 62431168b07SJohn McCall } else if (LV.isSimple()) { 6251553b190SJohn McCall CGF.EmitScalarInit(E, /*D=*/0, LV, /*Captured=*/false); 6266e313210SEli Friedman } else { 62755e1fbc8SJohn McCall CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV); 6287a51313dSChris Lattner } 629579a05d7SChris Lattner } 630579a05d7SChris Lattner 6311553b190SJohn McCall void AggExprEmitter::EmitNullInitializationToLValue(LValue lv) { 6321553b190SJohn McCall QualType type = lv.getType(); 6331553b190SJohn McCall 63427a3631bSChris Lattner // If the destination slot is already zeroed out before the aggregate is 63527a3631bSChris Lattner // copied into it, we don't have to emit any zeros here. 6361553b190SJohn McCall if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type)) 63727a3631bSChris Lattner return; 63827a3631bSChris Lattner 6391553b190SJohn McCall if (!CGF.hasAggregateLLVMType(type)) { 640579a05d7SChris Lattner // For non-aggregates, we can store zero 6411553b190SJohn McCall llvm::Value *null = llvm::Constant::getNullValue(CGF.ConvertType(type)); 64255e1fbc8SJohn McCall CGF.EmitStoreThroughLValue(RValue::get(null), lv); 643579a05d7SChris Lattner } else { 644579a05d7SChris Lattner // There's a potential optimization opportunity in combining 645579a05d7SChris Lattner // memsets; that would be easy for arrays, but relatively 646579a05d7SChris Lattner // difficult for structures with the current code. 6471553b190SJohn McCall CGF.EmitNullInitialization(lv.getAddress(), lv.getType()); 648579a05d7SChris Lattner } 649579a05d7SChris Lattner } 650579a05d7SChris Lattner 651579a05d7SChris Lattner void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 652f5d08c9eSEli Friedman #if 0 6536d11ec8cSEli Friedman // FIXME: Assess perf here? Figure out what cases are worth optimizing here 6546d11ec8cSEli Friedman // (Length of globals? Chunks of zeroed-out space?). 655f5d08c9eSEli Friedman // 65618bb9284SMike Stump // If we can, prefer a copy from a global; this is a lot less code for long 65718bb9284SMike Stump // globals, and it's easier for the current optimizers to analyze. 6586d11ec8cSEli Friedman if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { 659c59bb48eSEli Friedman llvm::GlobalVariable* GV = 6606d11ec8cSEli Friedman new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, 6616d11ec8cSEli Friedman llvm::GlobalValue::InternalLinkage, C, ""); 6622e442a00SDaniel Dunbar EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType())); 663c59bb48eSEli Friedman return; 664c59bb48eSEli Friedman } 665f5d08c9eSEli Friedman #endif 666f53c0968SChris Lattner if (E->hadArrayRangeDesignator()) 667bf7207a1SDouglas Gregor CGF.ErrorUnsupported(E, "GNU array range designator extension"); 668bf7207a1SDouglas Gregor 6697a626f63SJohn McCall llvm::Value *DestPtr = Dest.getAddr(); 6707a626f63SJohn McCall 671579a05d7SChris Lattner // Handle initialization of an array. 672579a05d7SChris Lattner if (E->getType()->isArrayType()) { 6732192fe50SChris Lattner llvm::PointerType *APType = 674579a05d7SChris Lattner cast<llvm::PointerType>(DestPtr->getType()); 6752192fe50SChris Lattner llvm::ArrayType *AType = 676579a05d7SChris Lattner cast<llvm::ArrayType>(APType->getElementType()); 677579a05d7SChris Lattner 678579a05d7SChris Lattner uint64_t NumInitElements = E->getNumInits(); 679f23b6fa4SEli Friedman 6800f398c44SChris Lattner if (E->getNumInits() > 0) { 6810f398c44SChris Lattner QualType T1 = E->getType(); 6820f398c44SChris Lattner QualType T2 = E->getInit(0)->getType(); 6832a69547fSEli Friedman if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { 684f23b6fa4SEli Friedman EmitAggLoadOfLValue(E->getInit(0)); 685f23b6fa4SEli Friedman return; 686f23b6fa4SEli Friedman } 6870f398c44SChris Lattner } 688f23b6fa4SEli Friedman 689579a05d7SChris Lattner uint64_t NumArrayElements = AType->getNumElements(); 69082fe67bbSJohn McCall assert(NumInitElements <= NumArrayElements); 691579a05d7SChris Lattner 69282fe67bbSJohn McCall QualType elementType = E->getType().getCanonicalType(); 69382fe67bbSJohn McCall elementType = CGF.getContext().getQualifiedType( 69482fe67bbSJohn McCall cast<ArrayType>(elementType)->getElementType(), 69582fe67bbSJohn McCall elementType.getQualifiers() + Dest.getQualifiers()); 69682fe67bbSJohn McCall 69782fe67bbSJohn McCall // DestPtr is an array*. Construct an elementType* by drilling 69882fe67bbSJohn McCall // down a level. 69982fe67bbSJohn McCall llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0); 70082fe67bbSJohn McCall llvm::Value *indices[] = { zero, zero }; 70182fe67bbSJohn McCall llvm::Value *begin = 702040dd82fSJay Foad Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin"); 70382fe67bbSJohn McCall 70482fe67bbSJohn McCall // Exception safety requires us to destroy all the 70582fe67bbSJohn McCall // already-constructed members if an initializer throws. 70682fe67bbSJohn McCall // For that, we'll need an EH cleanup. 70782fe67bbSJohn McCall QualType::DestructionKind dtorKind = elementType.isDestructedType(); 70882fe67bbSJohn McCall llvm::AllocaInst *endOfInit = 0; 70982fe67bbSJohn McCall EHScopeStack::stable_iterator cleanup; 71082fe67bbSJohn McCall if (CGF.needsEHCleanup(dtorKind)) { 71182fe67bbSJohn McCall // In principle we could tell the cleanup where we are more 71282fe67bbSJohn McCall // directly, but the control flow can get so varied here that it 71382fe67bbSJohn McCall // would actually be quite complex. Therefore we go through an 71482fe67bbSJohn McCall // alloca. 71582fe67bbSJohn McCall endOfInit = CGF.CreateTempAlloca(begin->getType(), 71682fe67bbSJohn McCall "arrayinit.endOfInit"); 71782fe67bbSJohn McCall Builder.CreateStore(begin, endOfInit); 718178360e1SJohn McCall CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType, 719178360e1SJohn McCall CGF.getDestroyer(dtorKind)); 72082fe67bbSJohn McCall cleanup = CGF.EHStack.stable_begin(); 72182fe67bbSJohn McCall 72282fe67bbSJohn McCall // Otherwise, remember that we didn't need a cleanup. 72382fe67bbSJohn McCall } else { 72482fe67bbSJohn McCall dtorKind = QualType::DK_none; 725e07425a5SArgyrios Kyrtzidis } 726e07425a5SArgyrios Kyrtzidis 72782fe67bbSJohn McCall llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1); 72827a3631bSChris Lattner 72982fe67bbSJohn McCall // The 'current element to initialize'. The invariants on this 73082fe67bbSJohn McCall // variable are complicated. Essentially, after each iteration of 73182fe67bbSJohn McCall // the loop, it points to the last initialized element, except 73282fe67bbSJohn McCall // that it points to the beginning of the array before any 73382fe67bbSJohn McCall // elements have been initialized. 73482fe67bbSJohn McCall llvm::Value *element = begin; 73527a3631bSChris Lattner 73682fe67bbSJohn McCall // Emit the explicit initializers. 73782fe67bbSJohn McCall for (uint64_t i = 0; i != NumInitElements; ++i) { 73882fe67bbSJohn McCall // Advance to the next element. 739178360e1SJohn McCall if (i > 0) { 74082fe67bbSJohn McCall element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element"); 74182fe67bbSJohn McCall 742178360e1SJohn McCall // Tell the cleanup that it needs to destroy up to this 743178360e1SJohn McCall // element. TODO: some of these stores can be trivially 744178360e1SJohn McCall // observed to be unnecessary. 745178360e1SJohn McCall if (endOfInit) Builder.CreateStore(element, endOfInit); 746178360e1SJohn McCall } 747178360e1SJohn McCall 74882fe67bbSJohn McCall LValue elementLV = CGF.MakeAddrLValue(element, elementType); 74982fe67bbSJohn McCall EmitInitializationToLValue(E->getInit(i), elementLV); 75082fe67bbSJohn McCall } 75182fe67bbSJohn McCall 75282fe67bbSJohn McCall // Check whether there's a non-trivial array-fill expression. 75382fe67bbSJohn McCall // Note that this will be a CXXConstructExpr even if the element 75482fe67bbSJohn McCall // type is an array (or array of array, etc.) of class type. 75582fe67bbSJohn McCall Expr *filler = E->getArrayFiller(); 75682fe67bbSJohn McCall bool hasTrivialFiller = true; 75782fe67bbSJohn McCall if (CXXConstructExpr *cons = dyn_cast_or_null<CXXConstructExpr>(filler)) { 75882fe67bbSJohn McCall assert(cons->getConstructor()->isDefaultConstructor()); 75982fe67bbSJohn McCall hasTrivialFiller = cons->getConstructor()->isTrivial(); 76082fe67bbSJohn McCall } 76182fe67bbSJohn McCall 76282fe67bbSJohn McCall // Any remaining elements need to be zero-initialized, possibly 76382fe67bbSJohn McCall // using the filler expression. We can skip this if the we're 76482fe67bbSJohn McCall // emitting to zeroed memory. 76582fe67bbSJohn McCall if (NumInitElements != NumArrayElements && 76682fe67bbSJohn McCall !(Dest.isZeroed() && hasTrivialFiller && 76782fe67bbSJohn McCall CGF.getTypes().isZeroInitializable(elementType))) { 76882fe67bbSJohn McCall 76982fe67bbSJohn McCall // Use an actual loop. This is basically 77082fe67bbSJohn McCall // do { *array++ = filler; } while (array != end); 77182fe67bbSJohn McCall 77282fe67bbSJohn McCall // Advance to the start of the rest of the array. 773178360e1SJohn McCall if (NumInitElements) { 77482fe67bbSJohn McCall element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start"); 775178360e1SJohn McCall if (endOfInit) Builder.CreateStore(element, endOfInit); 776178360e1SJohn McCall } 77782fe67bbSJohn McCall 77882fe67bbSJohn McCall // Compute the end of the array. 77982fe67bbSJohn McCall llvm::Value *end = Builder.CreateInBoundsGEP(begin, 78082fe67bbSJohn McCall llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements), 78182fe67bbSJohn McCall "arrayinit.end"); 78282fe67bbSJohn McCall 78382fe67bbSJohn McCall llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); 78482fe67bbSJohn McCall llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body"); 78582fe67bbSJohn McCall 78682fe67bbSJohn McCall // Jump into the body. 78782fe67bbSJohn McCall CGF.EmitBlock(bodyBB); 78882fe67bbSJohn McCall llvm::PHINode *currentElement = 78982fe67bbSJohn McCall Builder.CreatePHI(element->getType(), 2, "arrayinit.cur"); 79082fe67bbSJohn McCall currentElement->addIncoming(element, entryBB); 79182fe67bbSJohn McCall 79282fe67bbSJohn McCall // Emit the actual filler expression. 79382fe67bbSJohn McCall LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType); 79482fe67bbSJohn McCall if (filler) 79582fe67bbSJohn McCall EmitInitializationToLValue(filler, elementLV); 796579a05d7SChris Lattner else 79782fe67bbSJohn McCall EmitNullInitializationToLValue(elementLV); 79827a3631bSChris Lattner 79982fe67bbSJohn McCall // Move on to the next element. 80082fe67bbSJohn McCall llvm::Value *nextElement = 80182fe67bbSJohn McCall Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next"); 80282fe67bbSJohn McCall 803178360e1SJohn McCall // Tell the EH cleanup that we finished with the last element. 804178360e1SJohn McCall if (endOfInit) Builder.CreateStore(nextElement, endOfInit); 805178360e1SJohn McCall 80682fe67bbSJohn McCall // Leave the loop if we're done. 80782fe67bbSJohn McCall llvm::Value *done = Builder.CreateICmpEQ(nextElement, end, 80882fe67bbSJohn McCall "arrayinit.done"); 80982fe67bbSJohn McCall llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end"); 81082fe67bbSJohn McCall Builder.CreateCondBr(done, endBB, bodyBB); 81182fe67bbSJohn McCall currentElement->addIncoming(nextElement, Builder.GetInsertBlock()); 81282fe67bbSJohn McCall 81382fe67bbSJohn McCall CGF.EmitBlock(endBB); 814579a05d7SChris Lattner } 81582fe67bbSJohn McCall 81682fe67bbSJohn McCall // Leave the partial-array cleanup if we entered one. 81782fe67bbSJohn McCall if (dtorKind) CGF.DeactivateCleanupBlock(cleanup); 81882fe67bbSJohn McCall 819579a05d7SChris Lattner return; 820579a05d7SChris Lattner } 821579a05d7SChris Lattner 822579a05d7SChris Lattner assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 823579a05d7SChris Lattner 824579a05d7SChris Lattner // Do struct initialization; this code just sets each individual member 825579a05d7SChris Lattner // to the approprate value. This makes bitfield support automatic; 826579a05d7SChris Lattner // the disadvantage is that the generated code is more difficult for 827579a05d7SChris Lattner // the optimizer, especially with bitfields. 828579a05d7SChris Lattner unsigned NumInitElements = E->getNumInits(); 8293b935d33SJohn McCall RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl(); 83052bcf963SChris Lattner 8313b935d33SJohn McCall if (record->isUnion()) { 8325169570eSDouglas Gregor // Only initialize one field of a union. The field itself is 8335169570eSDouglas Gregor // specified by the initializer list. 8345169570eSDouglas Gregor if (!E->getInitializedFieldInUnion()) { 8355169570eSDouglas Gregor // Empty union; we have nothing to do. 8365169570eSDouglas Gregor 8375169570eSDouglas Gregor #ifndef NDEBUG 8385169570eSDouglas Gregor // Make sure that it's really an empty and not a failure of 8395169570eSDouglas Gregor // semantic analysis. 8403b935d33SJohn McCall for (RecordDecl::field_iterator Field = record->field_begin(), 8413b935d33SJohn McCall FieldEnd = record->field_end(); 8425169570eSDouglas Gregor Field != FieldEnd; ++Field) 8435169570eSDouglas Gregor assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 8445169570eSDouglas Gregor #endif 8455169570eSDouglas Gregor return; 8465169570eSDouglas Gregor } 8475169570eSDouglas Gregor 8485169570eSDouglas Gregor // FIXME: volatility 8495169570eSDouglas Gregor FieldDecl *Field = E->getInitializedFieldInUnion(); 8505169570eSDouglas Gregor 85127a3631bSChris Lattner LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0); 8525169570eSDouglas Gregor if (NumInitElements) { 8535169570eSDouglas Gregor // Store the initializer into the field 8541553b190SJohn McCall EmitInitializationToLValue(E->getInit(0), FieldLoc); 8555169570eSDouglas Gregor } else { 85627a3631bSChris Lattner // Default-initialize to null. 8571553b190SJohn McCall EmitNullInitializationToLValue(FieldLoc); 8585169570eSDouglas Gregor } 8595169570eSDouglas Gregor 8605169570eSDouglas Gregor return; 8615169570eSDouglas Gregor } 862579a05d7SChris Lattner 8633b935d33SJohn McCall // We'll need to enter cleanup scopes in case any of the member 8643b935d33SJohn McCall // initializers throw an exception. 8650e62c1ccSChris Lattner SmallVector<EHScopeStack::stable_iterator, 16> cleanups; 8663b935d33SJohn McCall 867579a05d7SChris Lattner // Here we iterate over the fields; this makes it simpler to both 868579a05d7SChris Lattner // default-initialize fields and skip over unnamed fields. 8693b935d33SJohn McCall unsigned curInitIndex = 0; 8703b935d33SJohn McCall for (RecordDecl::field_iterator field = record->field_begin(), 8713b935d33SJohn McCall fieldEnd = record->field_end(); 8723b935d33SJohn McCall field != fieldEnd; ++field) { 8733b935d33SJohn McCall // We're done once we hit the flexible array member. 8743b935d33SJohn McCall if (field->getType()->isIncompleteArrayType()) 87591f84216SDouglas Gregor break; 87691f84216SDouglas Gregor 8773b935d33SJohn McCall // Always skip anonymous bitfields. 8783b935d33SJohn McCall if (field->isUnnamedBitfield()) 879579a05d7SChris Lattner continue; 88017bd094aSDouglas Gregor 8813b935d33SJohn McCall // We're done if we reach the end of the explicit initializers, we 8823b935d33SJohn McCall // have a zeroed object, and the rest of the fields are 8833b935d33SJohn McCall // zero-initializable. 8843b935d33SJohn McCall if (curInitIndex == NumInitElements && Dest.isZeroed() && 88527a3631bSChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 88627a3631bSChris Lattner break; 88727a3631bSChris Lattner 888327944b3SEli Friedman // FIXME: volatility 8893b935d33SJohn McCall LValue LV = CGF.EmitLValueForFieldInitialization(DestPtr, *field, 0); 8907c1baf46SFariborz Jahanian // We never generate write-barries for initialized fields. 8913b935d33SJohn McCall LV.setNonGC(true); 89227a3631bSChris Lattner 8933b935d33SJohn McCall if (curInitIndex < NumInitElements) { 894e18aaf2cSChris Lattner // Store the initializer into the field. 8953b935d33SJohn McCall EmitInitializationToLValue(E->getInit(curInitIndex++), LV); 896579a05d7SChris Lattner } else { 897579a05d7SChris Lattner // We're out of initalizers; default-initialize to null 8983b935d33SJohn McCall EmitNullInitializationToLValue(LV); 8993b935d33SJohn McCall } 9003b935d33SJohn McCall 9013b935d33SJohn McCall // Push a destructor if necessary. 9023b935d33SJohn McCall // FIXME: if we have an array of structures, all explicitly 9033b935d33SJohn McCall // initialized, we can end up pushing a linear number of cleanups. 9043b935d33SJohn McCall bool pushedCleanup = false; 9053b935d33SJohn McCall if (QualType::DestructionKind dtorKind 9063b935d33SJohn McCall = field->getType().isDestructedType()) { 9073b935d33SJohn McCall assert(LV.isSimple()); 9083b935d33SJohn McCall if (CGF.needsEHCleanup(dtorKind)) { 9093b935d33SJohn McCall CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(), 9103b935d33SJohn McCall CGF.getDestroyer(dtorKind), false); 9113b935d33SJohn McCall cleanups.push_back(CGF.EHStack.stable_begin()); 9123b935d33SJohn McCall pushedCleanup = true; 9133b935d33SJohn McCall } 914579a05d7SChris Lattner } 91527a3631bSChris Lattner 91627a3631bSChris Lattner // If the GEP didn't get used because of a dead zero init or something 91727a3631bSChris Lattner // else, clean it up for -O0 builds and general tidiness. 9183b935d33SJohn McCall if (!pushedCleanup && LV.isSimple()) 91927a3631bSChris Lattner if (llvm::GetElementPtrInst *GEP = 9203b935d33SJohn McCall dyn_cast<llvm::GetElementPtrInst>(LV.getAddress())) 92127a3631bSChris Lattner if (GEP->use_empty()) 92227a3631bSChris Lattner GEP->eraseFromParent(); 9237a51313dSChris Lattner } 9243b935d33SJohn McCall 9253b935d33SJohn McCall // Deactivate all the partial cleanups in reverse order, which 9263b935d33SJohn McCall // generally means popping them. 9273b935d33SJohn McCall for (unsigned i = cleanups.size(); i != 0; --i) 9283b935d33SJohn McCall CGF.DeactivateCleanupBlock(cleanups[i-1]); 9297a51313dSChris Lattner } 9307a51313dSChris Lattner 9317a51313dSChris Lattner //===----------------------------------------------------------------------===// 9327a51313dSChris Lattner // Entry Points into this File 9337a51313dSChris Lattner //===----------------------------------------------------------------------===// 9347a51313dSChris Lattner 93527a3631bSChris Lattner /// GetNumNonZeroBytesInInit - Get an approximate count of the number of 93627a3631bSChris Lattner /// non-zero bytes that will be stored when outputting the initializer for the 93727a3631bSChris Lattner /// specified initializer expression. 938df94cb7dSKen Dyck static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) { 93991147596SPeter Collingbourne E = E->IgnoreParens(); 94027a3631bSChris Lattner 94127a3631bSChris Lattner // 0 and 0.0 won't require any non-zero stores! 942df94cb7dSKen Dyck if (isSimpleZero(E, CGF)) return CharUnits::Zero(); 94327a3631bSChris Lattner 94427a3631bSChris Lattner // If this is an initlist expr, sum up the size of sizes of the (present) 94527a3631bSChris Lattner // elements. If this is something weird, assume the whole thing is non-zero. 94627a3631bSChris Lattner const InitListExpr *ILE = dyn_cast<InitListExpr>(E); 94727a3631bSChris Lattner if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType())) 948df94cb7dSKen Dyck return CGF.getContext().getTypeSizeInChars(E->getType()); 94927a3631bSChris Lattner 950c5cc2fb9SChris Lattner // InitListExprs for structs have to be handled carefully. If there are 951c5cc2fb9SChris Lattner // reference members, we need to consider the size of the reference, not the 952c5cc2fb9SChris Lattner // referencee. InitListExprs for unions and arrays can't have references. 9535cd84755SChris Lattner if (const RecordType *RT = E->getType()->getAs<RecordType>()) { 9545cd84755SChris Lattner if (!RT->isUnionType()) { 955c5cc2fb9SChris Lattner RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 956df94cb7dSKen Dyck CharUnits NumNonZeroBytes = CharUnits::Zero(); 957c5cc2fb9SChris Lattner 958c5cc2fb9SChris Lattner unsigned ILEElement = 0; 959c5cc2fb9SChris Lattner for (RecordDecl::field_iterator Field = SD->field_begin(), 960c5cc2fb9SChris Lattner FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) { 961c5cc2fb9SChris Lattner // We're done once we hit the flexible array member or run out of 962c5cc2fb9SChris Lattner // InitListExpr elements. 963c5cc2fb9SChris Lattner if (Field->getType()->isIncompleteArrayType() || 964c5cc2fb9SChris Lattner ILEElement == ILE->getNumInits()) 965c5cc2fb9SChris Lattner break; 966c5cc2fb9SChris Lattner if (Field->isUnnamedBitfield()) 967c5cc2fb9SChris Lattner continue; 968c5cc2fb9SChris Lattner 969c5cc2fb9SChris Lattner const Expr *E = ILE->getInit(ILEElement++); 970c5cc2fb9SChris Lattner 971c5cc2fb9SChris Lattner // Reference values are always non-null and have the width of a pointer. 9725cd84755SChris Lattner if (Field->getType()->isReferenceType()) 973df94cb7dSKen Dyck NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits( 974e8bbc121SDouglas Gregor CGF.getContext().getTargetInfo().getPointerWidth(0)); 9755cd84755SChris Lattner else 976c5cc2fb9SChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF); 977c5cc2fb9SChris Lattner } 978c5cc2fb9SChris Lattner 979c5cc2fb9SChris Lattner return NumNonZeroBytes; 980c5cc2fb9SChris Lattner } 9815cd84755SChris Lattner } 982c5cc2fb9SChris Lattner 983c5cc2fb9SChris Lattner 984df94cb7dSKen Dyck CharUnits NumNonZeroBytes = CharUnits::Zero(); 98527a3631bSChris Lattner for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) 98627a3631bSChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF); 98727a3631bSChris Lattner return NumNonZeroBytes; 98827a3631bSChris Lattner } 98927a3631bSChris Lattner 99027a3631bSChris Lattner /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of 99127a3631bSChris Lattner /// zeros in it, emit a memset and avoid storing the individual zeros. 99227a3631bSChris Lattner /// 99327a3631bSChris Lattner static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E, 99427a3631bSChris Lattner CodeGenFunction &CGF) { 99527a3631bSChris Lattner // If the slot is already known to be zeroed, nothing to do. Don't mess with 99627a3631bSChris Lattner // volatile stores. 99727a3631bSChris Lattner if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return; 99827a3631bSChris Lattner 99903535265SArgyrios Kyrtzidis // C++ objects with a user-declared constructor don't need zero'ing. 100003535265SArgyrios Kyrtzidis if (CGF.getContext().getLangOptions().CPlusPlus) 100103535265SArgyrios Kyrtzidis if (const RecordType *RT = CGF.getContext() 100203535265SArgyrios Kyrtzidis .getBaseElementType(E->getType())->getAs<RecordType>()) { 100303535265SArgyrios Kyrtzidis const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 100403535265SArgyrios Kyrtzidis if (RD->hasUserDeclaredConstructor()) 100503535265SArgyrios Kyrtzidis return; 100603535265SArgyrios Kyrtzidis } 100703535265SArgyrios Kyrtzidis 100827a3631bSChris Lattner // If the type is 16-bytes or smaller, prefer individual stores over memset. 1009239a3357SKen Dyck std::pair<CharUnits, CharUnits> TypeInfo = 1010239a3357SKen Dyck CGF.getContext().getTypeInfoInChars(E->getType()); 1011239a3357SKen Dyck if (TypeInfo.first <= CharUnits::fromQuantity(16)) 101227a3631bSChris Lattner return; 101327a3631bSChris Lattner 101427a3631bSChris Lattner // Check to see if over 3/4 of the initializer are known to be zero. If so, 101527a3631bSChris Lattner // we prefer to emit memset + individual stores for the rest. 1016239a3357SKen Dyck CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF); 1017239a3357SKen Dyck if (NumNonZeroBytes*4 > TypeInfo.first) 101827a3631bSChris Lattner return; 101927a3631bSChris Lattner 102027a3631bSChris Lattner // Okay, it seems like a good idea to use an initial memset, emit the call. 1021239a3357SKen Dyck llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity()); 1022239a3357SKen Dyck CharUnits Align = TypeInfo.second; 102327a3631bSChris Lattner 102427a3631bSChris Lattner llvm::Value *Loc = Slot.getAddr(); 10252192fe50SChris Lattner llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 102627a3631bSChris Lattner 102727a3631bSChris Lattner Loc = CGF.Builder.CreateBitCast(Loc, BP); 1028239a3357SKen Dyck CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, 1029239a3357SKen Dyck Align.getQuantity(), false); 103027a3631bSChris Lattner 103127a3631bSChris Lattner // Tell the AggExprEmitter that the slot is known zero. 103227a3631bSChris Lattner Slot.setZeroed(); 103327a3631bSChris Lattner } 103427a3631bSChris Lattner 103527a3631bSChris Lattner 103627a3631bSChris Lattner 103727a3631bSChris Lattner 103825306cacSMike Stump /// EmitAggExpr - Emit the computation of the specified expression of aggregate 103925306cacSMike Stump /// type. The result is computed into DestPtr. Note that if DestPtr is null, 104025306cacSMike Stump /// the value of the aggregate expression is not needed. If VolatileDest is 104125306cacSMike Stump /// true, DestPtr cannot be 0. 10427a626f63SJohn McCall /// 10437a626f63SJohn McCall /// \param IsInitializer - true if this evaluation is initializing an 10447a626f63SJohn McCall /// object whose lifetime is already being managed. 10457a626f63SJohn McCall void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot, 1046b60e70f9SFariborz Jahanian bool IgnoreResult) { 10477a51313dSChris Lattner assert(E && hasAggregateLLVMType(E->getType()) && 10487a51313dSChris Lattner "Invalid aggregate expression to emit"); 104927a3631bSChris Lattner assert((Slot.getAddr() != 0 || Slot.isIgnored()) && 105027a3631bSChris Lattner "slot has bits but no address"); 10517a51313dSChris Lattner 105227a3631bSChris Lattner // Optimize the slot if possible. 105327a3631bSChris Lattner CheckAggExprForMemSetUse(Slot, E, *this); 105427a3631bSChris Lattner 105527a3631bSChris Lattner AggExprEmitter(*this, Slot, IgnoreResult).Visit(const_cast<Expr*>(E)); 10567a51313dSChris Lattner } 10570bc8e86dSDaniel Dunbar 1058d0bc7b9dSDaniel Dunbar LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { 1059d0bc7b9dSDaniel Dunbar assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!"); 1060a7566f16SDaniel Dunbar llvm::Value *Temp = CreateMemTemp(E->getType()); 10612e442a00SDaniel Dunbar LValue LV = MakeAddrLValue(Temp, E->getType()); 10628d6fc958SJohn McCall EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed, 106346759f4fSJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 106446759f4fSJohn McCall AggValueSlot::IsNotAliased)); 10652e442a00SDaniel Dunbar return LV; 1066d0bc7b9dSDaniel Dunbar } 1067d0bc7b9dSDaniel Dunbar 10680bc8e86dSDaniel Dunbar void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 10695e9e61b8SMike Stump llvm::Value *SrcPtr, QualType Ty, 10705e9e61b8SMike Stump bool isVolatile) { 10710bc8e86dSDaniel Dunbar assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 10720bc8e86dSDaniel Dunbar 107316e94af6SAnders Carlsson if (getContext().getLangOptions().CPlusPlus) { 107416e94af6SAnders Carlsson if (const RecordType *RT = Ty->getAs<RecordType>()) { 1075f22101a0SDouglas Gregor CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 1076f22101a0SDouglas Gregor assert((Record->hasTrivialCopyConstructor() || 1077146b8e9aSDouglas Gregor Record->hasTrivialCopyAssignment() || 1078146b8e9aSDouglas Gregor Record->hasTrivialMoveConstructor() || 1079146b8e9aSDouglas Gregor Record->hasTrivialMoveAssignment()) && 1080f22101a0SDouglas Gregor "Trying to aggregate-copy a type without a trivial copy " 1081f22101a0SDouglas Gregor "constructor or assignment operator"); 1082265b8b8dSDouglas Gregor // Ignore empty classes in C++. 1083f22101a0SDouglas Gregor if (Record->isEmpty()) 108416e94af6SAnders Carlsson return; 108516e94af6SAnders Carlsson } 108616e94af6SAnders Carlsson } 108716e94af6SAnders Carlsson 1088ca05dfefSChris Lattner // Aggregate assignment turns into llvm.memcpy. This is almost valid per 10893ef668c2SChris Lattner // C99 6.5.16.1p3, which states "If the value being stored in an object is 10903ef668c2SChris Lattner // read from another object that overlaps in anyway the storage of the first 10913ef668c2SChris Lattner // object, then the overlap shall be exact and the two objects shall have 10923ef668c2SChris Lattner // qualified or unqualified versions of a compatible type." 10933ef668c2SChris Lattner // 1094ca05dfefSChris Lattner // memcpy is not defined if the source and destination pointers are exactly 10953ef668c2SChris Lattner // equal, but other compilers do this optimization, and almost every memcpy 10963ef668c2SChris Lattner // implementation handles this case safely. If there is a libc that does not 10973ef668c2SChris Lattner // safely handle this, we can add a target hook. 10980bc8e86dSDaniel Dunbar 10990bc8e86dSDaniel Dunbar // Get size and alignment info for this aggregate. 1100bb2c2400SKen Dyck std::pair<CharUnits, CharUnits> TypeInfo = 1101bb2c2400SKen Dyck getContext().getTypeInfoInChars(Ty); 11020bc8e86dSDaniel Dunbar 11030bc8e86dSDaniel Dunbar // FIXME: Handle variable sized types. 11040bc8e86dSDaniel Dunbar 110586736572SMike Stump // FIXME: If we have a volatile struct, the optimizer can remove what might 110686736572SMike Stump // appear to be `extra' memory ops: 110786736572SMike Stump // 110886736572SMike Stump // volatile struct { int i; } a, b; 110986736572SMike Stump // 111086736572SMike Stump // int main() { 111186736572SMike Stump // a = b; 111286736572SMike Stump // a = b; 111386736572SMike Stump // } 111486736572SMike Stump // 1115cc2ab0cdSMon P Wang // we need to use a different call here. We use isVolatile to indicate when 1116ec3cbfe8SMike Stump // either the source or the destination is volatile. 1117cc2ab0cdSMon P Wang 11182192fe50SChris Lattner llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); 11192192fe50SChris Lattner llvm::Type *DBP = 1120ad7c5c16SJohn McCall llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace()); 112176399eb2SBenjamin Kramer DestPtr = Builder.CreateBitCast(DestPtr, DBP); 1122cc2ab0cdSMon P Wang 11232192fe50SChris Lattner llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); 11242192fe50SChris Lattner llvm::Type *SBP = 1125ad7c5c16SJohn McCall llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace()); 112676399eb2SBenjamin Kramer SrcPtr = Builder.CreateBitCast(SrcPtr, SBP); 1127cc2ab0cdSMon P Wang 112831168b07SJohn McCall // Don't do any of the memmove_collectable tests if GC isn't set. 112979a91418SDouglas Gregor if (CGM.getLangOptions().getGC() == LangOptions::NonGC) { 113031168b07SJohn McCall // fall through 113131168b07SJohn McCall } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { 1132021510e9SFariborz Jahanian RecordDecl *Record = RecordTy->getDecl(); 1133021510e9SFariborz Jahanian if (Record->hasObjectMember()) { 1134bb2c2400SKen Dyck CharUnits size = TypeInfo.first; 11352192fe50SChris Lattner llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1136bb2c2400SKen Dyck llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1137021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1138021510e9SFariborz Jahanian SizeVal); 1139021510e9SFariborz Jahanian return; 1140021510e9SFariborz Jahanian } 114131168b07SJohn McCall } else if (Ty->isArrayType()) { 1142021510e9SFariborz Jahanian QualType BaseType = getContext().getBaseElementType(Ty); 1143021510e9SFariborz Jahanian if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { 1144021510e9SFariborz Jahanian if (RecordTy->getDecl()->hasObjectMember()) { 1145bb2c2400SKen Dyck CharUnits size = TypeInfo.first; 11462192fe50SChris Lattner llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1147bb2c2400SKen Dyck llvm::Value *SizeVal = 1148bb2c2400SKen Dyck llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1149021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1150021510e9SFariborz Jahanian SizeVal); 1151021510e9SFariborz Jahanian return; 1152021510e9SFariborz Jahanian } 1153021510e9SFariborz Jahanian } 1154021510e9SFariborz Jahanian } 1155021510e9SFariborz Jahanian 1156acc6b4e2SBenjamin Kramer Builder.CreateMemCpy(DestPtr, SrcPtr, 1157bb2c2400SKen Dyck llvm::ConstantInt::get(IntPtrTy, 1158bb2c2400SKen Dyck TypeInfo.first.getQuantity()), 1159bb2c2400SKen Dyck TypeInfo.second.getQuantity(), isVolatile); 11600bc8e86dSDaniel Dunbar } 1161