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); 776d694a38SEli Friedman void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false, 786d694a38SEli Friedman unsigned Alignment = 0); 79ca9fc09cSMike Stump 80a5efa738SJohn McCall void EmitMoveFromReturnSlot(const Expr *E, RValue Src); 81cc04e9f6SJohn McCall 828d6fc958SJohn McCall AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) { 8379a91418SDouglas Gregor if (CGF.getLangOptions().getGC() && TypeRequiresGCollection(T)) 848d6fc958SJohn McCall return AggValueSlot::NeedsGCBarriers; 858d6fc958SJohn McCall return AggValueSlot::DoesNotNeedGCBarriers; 868d6fc958SJohn McCall } 878d6fc958SJohn McCall 88cc04e9f6SJohn McCall bool TypeRequiresGCollection(QualType T); 89cc04e9f6SJohn McCall 907a51313dSChris Lattner //===--------------------------------------------------------------------===// 917a51313dSChris Lattner // Visitor Methods 927a51313dSChris Lattner //===--------------------------------------------------------------------===// 937a51313dSChris Lattner 947a51313dSChris Lattner void VisitStmt(Stmt *S) { 95a7c8cf62SDaniel Dunbar CGF.ErrorUnsupported(S, "aggregate expression"); 967a51313dSChris Lattner } 977a51313dSChris Lattner void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } 9891147596SPeter Collingbourne void VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 9991147596SPeter Collingbourne Visit(GE->getResultExpr()); 10091147596SPeter Collingbourne } 1013f66b84cSEli Friedman void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 1027c454bb8SJohn McCall void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) { 1037c454bb8SJohn McCall return Visit(E->getReplacement()); 1047c454bb8SJohn McCall } 1057a51313dSChris Lattner 1067a51313dSChris Lattner // l-values. 1077a51313dSChris Lattner void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } 1087a51313dSChris Lattner void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 1097a51313dSChris Lattner void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 110d443c0a0SDaniel Dunbar void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 1119b71f0cfSDouglas Gregor void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 1127a51313dSChris Lattner void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 1137a51313dSChris Lattner EmitAggLoadOfLValue(E); 1147a51313dSChris Lattner } 1152f343dd5SChris Lattner void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 1162f343dd5SChris Lattner EmitAggLoadOfLValue(E); 1172f343dd5SChris Lattner } 1182f343dd5SChris Lattner void VisitPredefinedExpr(const PredefinedExpr *E) { 1192f343dd5SChris Lattner EmitAggLoadOfLValue(E); 1202f343dd5SChris Lattner } 121bc7d67ceSMike Stump 1227a51313dSChris Lattner // Operators. 123ec143777SAnders Carlsson void VisitCastExpr(CastExpr *E); 1247a51313dSChris Lattner void VisitCallExpr(const CallExpr *E); 1257a51313dSChris Lattner void VisitStmtExpr(const StmtExpr *E); 1267a51313dSChris Lattner void VisitBinaryOperator(const BinaryOperator *BO); 127ffba662dSFariborz Jahanian void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); 1287a51313dSChris Lattner void VisitBinAssign(const BinaryOperator *E); 1294b0e2a30SEli Friedman void VisitBinComma(const BinaryOperator *E); 1307a51313dSChris Lattner 131b1d329daSChris Lattner void VisitObjCMessageExpr(ObjCMessageExpr *E); 132c8317a44SDaniel Dunbar void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 133c8317a44SDaniel Dunbar EmitAggLoadOfLValue(E); 134c8317a44SDaniel Dunbar } 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 151fe96e0b6SJohn McCall void VisitPseudoObjectExpr(PseudoObjectExpr *E) { 152fe96e0b6SJohn McCall if (E->isGLValue()) { 153fe96e0b6SJohn McCall LValue LV = CGF.EmitPseudoObjectLValue(E); 154fe96e0b6SJohn McCall return EmitFinalDestCopy(E, LV); 155fe96e0b6SJohn McCall } 156fe96e0b6SJohn McCall 157fe96e0b6SJohn McCall CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType())); 158fe96e0b6SJohn McCall } 159fe96e0b6SJohn McCall 16021911e89SEli Friedman void VisitVAArgExpr(VAArgExpr *E); 161579a05d7SChris Lattner 1621553b190SJohn McCall void EmitInitializationToLValue(Expr *E, LValue Address); 1631553b190SJohn McCall void EmitNullInitializationToLValue(LValue Address); 1647a51313dSChris Lattner // case Expr::ChooseExprClass: 165f16b8c30SMike Stump void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } 166df14b3a8SEli Friedman void VisitAtomicExpr(AtomicExpr *E) { 167df14b3a8SEli Friedman CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr()); 168df14b3a8SEli Friedman } 1697a51313dSChris Lattner }; 1707a51313dSChris Lattner } // end anonymous namespace. 1717a51313dSChris Lattner 1727a51313dSChris Lattner //===----------------------------------------------------------------------===// 1737a51313dSChris Lattner // Utilities 1747a51313dSChris Lattner //===----------------------------------------------------------------------===// 1757a51313dSChris Lattner 1767a51313dSChris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that 1777a51313dSChris Lattner /// represents a value lvalue, this method emits the address of the lvalue, 1787a51313dSChris Lattner /// then loads the result into DestPtr. 1797a51313dSChris Lattner void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 1807a51313dSChris Lattner LValue LV = CGF.EmitLValue(E); 181ca9fc09cSMike Stump EmitFinalDestCopy(E, LV); 182ca9fc09cSMike Stump } 183ca9fc09cSMike Stump 184cc04e9f6SJohn McCall /// \brief True if the given aggregate type requires special GC API calls. 185cc04e9f6SJohn McCall bool AggExprEmitter::TypeRequiresGCollection(QualType T) { 186cc04e9f6SJohn McCall // Only record types have members that might require garbage collection. 187cc04e9f6SJohn McCall const RecordType *RecordTy = T->getAs<RecordType>(); 188cc04e9f6SJohn McCall if (!RecordTy) return false; 189cc04e9f6SJohn McCall 190cc04e9f6SJohn McCall // Don't mess with non-trivial C++ types. 191cc04e9f6SJohn McCall RecordDecl *Record = RecordTy->getDecl(); 192cc04e9f6SJohn McCall if (isa<CXXRecordDecl>(Record) && 193cc04e9f6SJohn McCall (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() || 194cc04e9f6SJohn McCall !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) 195cc04e9f6SJohn McCall return false; 196cc04e9f6SJohn McCall 197cc04e9f6SJohn McCall // Check whether the type has an object member. 198cc04e9f6SJohn McCall return Record->hasObjectMember(); 199cc04e9f6SJohn McCall } 200cc04e9f6SJohn McCall 201a5efa738SJohn McCall /// \brief Perform the final move to DestPtr if for some reason 202a5efa738SJohn McCall /// getReturnValueSlot() didn't use it directly. 203cc04e9f6SJohn McCall /// 204cc04e9f6SJohn McCall /// The idea is that you do something like this: 205cc04e9f6SJohn McCall /// RValue Result = EmitSomething(..., getReturnValueSlot()); 206a5efa738SJohn McCall /// EmitMoveFromReturnSlot(E, Result); 207a5efa738SJohn McCall /// 208a5efa738SJohn McCall /// If nothing interferes, this will cause the result to be emitted 209a5efa738SJohn McCall /// directly into the return value slot. Otherwise, a final move 210a5efa738SJohn McCall /// will be performed. 211a5efa738SJohn McCall void AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue Src) { 212a5efa738SJohn McCall if (shouldUseDestForReturnSlot()) { 213a5efa738SJohn McCall // Logically, Dest.getAddr() should equal Src.getAggregateAddr(). 214a5efa738SJohn McCall // The possibility of undef rvalues complicates that a lot, 215a5efa738SJohn McCall // though, so we can't really assert. 216a5efa738SJohn McCall return; 217021510e9SFariborz Jahanian } 218a5efa738SJohn McCall 219a5efa738SJohn McCall // Otherwise, do a final copy, 220a5efa738SJohn McCall assert(Dest.getAddr() != Src.getAggregateAddr()); 221a5efa738SJohn McCall EmitFinalDestCopy(E, Src, /*Ignore*/ true); 222cc04e9f6SJohn McCall } 223cc04e9f6SJohn McCall 224ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 2256d694a38SEli Friedman void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore, 2266d694a38SEli Friedman unsigned Alignment) { 227ca9fc09cSMike Stump assert(Src.isAggregate() && "value must be aggregate value!"); 2287a51313dSChris Lattner 2297a626f63SJohn McCall // If Dest is ignored, then we're evaluating an aggregate expression 2308d752430SJohn McCall // in a context (like an expression statement) that doesn't care 2318d752430SJohn McCall // about the result. C says that an lvalue-to-rvalue conversion is 2328d752430SJohn McCall // performed in these cases; C++ says that it is not. In either 2338d752430SJohn McCall // case, we don't actually need to do anything unless the value is 2348d752430SJohn McCall // volatile. 2357a626f63SJohn McCall if (Dest.isIgnored()) { 2368d752430SJohn McCall if (!Src.isVolatileQualified() || 2378d752430SJohn McCall CGF.CGM.getLangOptions().CPlusPlus || 2388d752430SJohn McCall (IgnoreResult && Ignore)) 239ec3cbfe8SMike Stump return; 240c123623dSFariborz Jahanian 241332ec2ceSMike Stump // If the source is volatile, we must read from it; to do that, we need 242332ec2ceSMike Stump // some place to put it. 2437a626f63SJohn McCall Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp"); 244332ec2ceSMike Stump } 2457a51313dSChris Lattner 24658649dc6SJohn McCall if (Dest.requiresGCollection()) { 2473b4bd9a1SKen Dyck CharUnits size = CGF.getContext().getTypeSizeInChars(E->getType()); 2482192fe50SChris Lattner llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 2493b4bd9a1SKen Dyck llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 250879d7266SFariborz Jahanian CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 2517a626f63SJohn McCall Dest.getAddr(), 2527a626f63SJohn McCall Src.getAggregateAddr(), 253021510e9SFariborz Jahanian SizeVal); 254879d7266SFariborz Jahanian return; 255879d7266SFariborz Jahanian } 256ca9fc09cSMike Stump // If the result of the assignment is used, copy the LHS there also. 257ca9fc09cSMike Stump // FIXME: Pass VolatileDest as well. I think we also need to merge volatile 258ca9fc09cSMike Stump // from the source as well, as we can't eliminate it if either operand 259ca9fc09cSMike Stump // is volatile, unless copy has volatile for both source and destination.. 2607a626f63SJohn McCall CGF.EmitAggregateCopy(Dest.getAddr(), Src.getAggregateAddr(), E->getType(), 2616d694a38SEli Friedman Dest.isVolatile()|Src.isVolatileQualified(), 2626d694a38SEli Friedman Alignment); 263ca9fc09cSMike Stump } 264ca9fc09cSMike Stump 265ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 266ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { 267ca9fc09cSMike Stump assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); 268ca9fc09cSMike Stump 2696d694a38SEli Friedman CharUnits Alignment = std::min(Src.getAlignment(), Dest.getAlignment()); 2706d694a38SEli Friedman EmitFinalDestCopy(E, Src.asAggregateRValue(), Ignore, Alignment.getQuantity()); 2717a51313dSChris Lattner } 2727a51313dSChris Lattner 2737a51313dSChris Lattner //===----------------------------------------------------------------------===// 2747a51313dSChris Lattner // Visitor Methods 2757a51313dSChris Lattner //===----------------------------------------------------------------------===// 2767a51313dSChris Lattner 277fe31481fSDouglas Gregor void AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){ 278fe31481fSDouglas Gregor Visit(E->GetTemporaryExpr()); 279fe31481fSDouglas Gregor } 280fe31481fSDouglas Gregor 2811bf5846aSJohn McCall void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) { 282c07a0c7eSJohn McCall EmitFinalDestCopy(e, CGF.getOpaqueLValueMapping(e)); 2831bf5846aSJohn McCall } 2841bf5846aSJohn McCall 2859b71f0cfSDouglas Gregor void 2869b71f0cfSDouglas Gregor AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 2876c9d31ebSDouglas Gregor if (E->getType().isPODType(CGF.getContext())) { 2886c9d31ebSDouglas Gregor // For a POD type, just emit a load of the lvalue + a copy, because our 2896c9d31ebSDouglas Gregor // compound literal might alias the destination. 2906c9d31ebSDouglas Gregor // FIXME: This is a band-aid; the real problem appears to be in our handling 2916c9d31ebSDouglas Gregor // of assignments, where we store directly into the LHS without checking 2926c9d31ebSDouglas Gregor // whether anything in the RHS aliases. 2936c9d31ebSDouglas Gregor EmitAggLoadOfLValue(E); 2946c9d31ebSDouglas Gregor return; 2956c9d31ebSDouglas Gregor } 2966c9d31ebSDouglas Gregor 2979b71f0cfSDouglas Gregor AggValueSlot Slot = EnsureSlot(E->getType()); 2989b71f0cfSDouglas Gregor CGF.EmitAggExpr(E->getInitializer(), Slot); 2999b71f0cfSDouglas Gregor } 3009b71f0cfSDouglas Gregor 3019b71f0cfSDouglas Gregor 302ec143777SAnders Carlsson void AggExprEmitter::VisitCastExpr(CastExpr *E) { 3031fb7ae9eSAnders Carlsson switch (E->getCastKind()) { 3048a01a751SAnders Carlsson case CK_Dynamic: { 3051c073f47SDouglas Gregor assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?"); 3061c073f47SDouglas Gregor LValue LV = CGF.EmitCheckedLValue(E->getSubExpr()); 3071c073f47SDouglas Gregor // FIXME: Do we also need to handle property references here? 3081c073f47SDouglas Gregor if (LV.isSimple()) 3091c073f47SDouglas Gregor CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E)); 3101c073f47SDouglas Gregor else 3111c073f47SDouglas Gregor CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast"); 3121c073f47SDouglas Gregor 3137a626f63SJohn McCall if (!Dest.isIgnored()) 3141c073f47SDouglas Gregor CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination"); 3151c073f47SDouglas Gregor break; 3161c073f47SDouglas Gregor } 3171c073f47SDouglas Gregor 318e302792bSJohn McCall case CK_ToUnion: { 31958989b71SJohn McCall if (Dest.isIgnored()) break; 32058989b71SJohn McCall 3217ffcf93bSNuno Lopes // GCC union extension 3222e442a00SDaniel Dunbar QualType Ty = E->getSubExpr()->getType(); 3232e442a00SDaniel Dunbar QualType PtrTy = CGF.getContext().getPointerType(Ty); 3247a626f63SJohn McCall llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(), 325dd274848SEli Friedman CGF.ConvertType(PtrTy)); 3261553b190SJohn McCall EmitInitializationToLValue(E->getSubExpr(), 3271553b190SJohn McCall CGF.MakeAddrLValue(CastPtr, Ty)); 3281fb7ae9eSAnders Carlsson break; 3297ffcf93bSNuno Lopes } 3307ffcf93bSNuno Lopes 331e302792bSJohn McCall case CK_DerivedToBase: 332e302792bSJohn McCall case CK_BaseToDerived: 333e302792bSJohn McCall case CK_UncheckedDerivedToBase: { 33483d382b1SDavid Blaikie llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: " 335aae38d66SDouglas Gregor "should have been unpacked before we got here"); 336aae38d66SDouglas Gregor } 337aae38d66SDouglas Gregor 33834376a68SJohn McCall case CK_LValueToRValue: // hope for downstream optimization 339e302792bSJohn McCall case CK_NoOp: 340fa35df62SDavid Chisnall case CK_AtomicToNonAtomic: 341fa35df62SDavid Chisnall case CK_NonAtomicToAtomic: 342e302792bSJohn McCall case CK_UserDefinedConversion: 343e302792bSJohn McCall case CK_ConstructorConversion: 3442a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 3452a69547fSEli Friedman E->getType()) && 3460f398c44SChris Lattner "Implicit cast types must be compatible"); 3477a51313dSChris Lattner Visit(E->getSubExpr()); 3481fb7ae9eSAnders Carlsson break; 349b05a3e55SAnders Carlsson 350e302792bSJohn McCall case CK_LValueBitCast: 351f3735e01SJohn McCall llvm_unreachable("should not be emitting lvalue bitcast as rvalue"); 35231996343SJohn McCall 353f3735e01SJohn McCall case CK_Dependent: 354f3735e01SJohn McCall case CK_BitCast: 355f3735e01SJohn McCall case CK_ArrayToPointerDecay: 356f3735e01SJohn McCall case CK_FunctionToPointerDecay: 357f3735e01SJohn McCall case CK_NullToPointer: 358f3735e01SJohn McCall case CK_NullToMemberPointer: 359f3735e01SJohn McCall case CK_BaseToDerivedMemberPointer: 360f3735e01SJohn McCall case CK_DerivedToBaseMemberPointer: 361f3735e01SJohn McCall case CK_MemberPointerToBoolean: 362f3735e01SJohn McCall case CK_IntegralToPointer: 363f3735e01SJohn McCall case CK_PointerToIntegral: 364f3735e01SJohn McCall case CK_PointerToBoolean: 365f3735e01SJohn McCall case CK_ToVoid: 366f3735e01SJohn McCall case CK_VectorSplat: 367f3735e01SJohn McCall case CK_IntegralCast: 368f3735e01SJohn McCall case CK_IntegralToBoolean: 369f3735e01SJohn McCall case CK_IntegralToFloating: 370f3735e01SJohn McCall case CK_FloatingToIntegral: 371f3735e01SJohn McCall case CK_FloatingToBoolean: 372f3735e01SJohn McCall case CK_FloatingCast: 3739320b87cSJohn McCall case CK_CPointerToObjCPointerCast: 3749320b87cSJohn McCall case CK_BlockPointerToObjCPointerCast: 375f3735e01SJohn McCall case CK_AnyPointerToBlockPointerCast: 376f3735e01SJohn McCall case CK_ObjCObjectLValueCast: 377f3735e01SJohn McCall case CK_FloatingRealToComplex: 378f3735e01SJohn McCall case CK_FloatingComplexToReal: 379f3735e01SJohn McCall case CK_FloatingComplexToBoolean: 380f3735e01SJohn McCall case CK_FloatingComplexCast: 381f3735e01SJohn McCall case CK_FloatingComplexToIntegralComplex: 382f3735e01SJohn McCall case CK_IntegralRealToComplex: 383f3735e01SJohn McCall case CK_IntegralComplexToReal: 384f3735e01SJohn McCall case CK_IntegralComplexToBoolean: 385f3735e01SJohn McCall case CK_IntegralComplexCast: 386f3735e01SJohn McCall case CK_IntegralComplexToFloatingComplex: 3872d637d2eSJohn McCall case CK_ARCProduceObject: 3882d637d2eSJohn McCall case CK_ARCConsumeObject: 3892d637d2eSJohn McCall case CK_ARCReclaimReturnedObject: 3902d637d2eSJohn McCall case CK_ARCExtendBlockObject: 391f3735e01SJohn McCall llvm_unreachable("cast kind invalid for aggregate types"); 3921fb7ae9eSAnders Carlsson } 3937a51313dSChris Lattner } 3947a51313dSChris Lattner 3950f398c44SChris Lattner void AggExprEmitter::VisitCallExpr(const CallExpr *E) { 396ddcbfe7bSAnders Carlsson if (E->getCallReturnType()->isReferenceType()) { 397ddcbfe7bSAnders Carlsson EmitAggLoadOfLValue(E); 398ddcbfe7bSAnders Carlsson return; 399ddcbfe7bSAnders Carlsson } 400ddcbfe7bSAnders Carlsson 401cc04e9f6SJohn McCall RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); 402a5efa738SJohn McCall EmitMoveFromReturnSlot(E, RV); 4037a51313dSChris Lattner } 4040f398c44SChris Lattner 4050f398c44SChris Lattner void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 406cc04e9f6SJohn McCall RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); 407a5efa738SJohn McCall EmitMoveFromReturnSlot(E, RV); 408b1d329daSChris Lattner } 4097a51313dSChris Lattner 4100f398c44SChris Lattner void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 411a2342eb8SJohn McCall CGF.EmitIgnoredExpr(E->getLHS()); 4127a626f63SJohn McCall Visit(E->getRHS()); 4134b0e2a30SEli Friedman } 4144b0e2a30SEli Friedman 4157a51313dSChris Lattner void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 416ce1de617SJohn McCall CodeGenFunction::StmtExprEvaluation eval(CGF); 4177a626f63SJohn McCall CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest); 4187a51313dSChris Lattner } 4197a51313dSChris Lattner 4207a51313dSChris Lattner void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 421e302792bSJohn McCall if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI) 422ffba662dSFariborz Jahanian VisitPointerToDataMemberBinaryOperator(E); 423ffba662dSFariborz Jahanian else 424a7c8cf62SDaniel Dunbar CGF.ErrorUnsupported(E, "aggregate binary expression"); 4257a51313dSChris Lattner } 4267a51313dSChris Lattner 427ffba662dSFariborz Jahanian void AggExprEmitter::VisitPointerToDataMemberBinaryOperator( 428ffba662dSFariborz Jahanian const BinaryOperator *E) { 429ffba662dSFariborz Jahanian LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); 430ffba662dSFariborz Jahanian EmitFinalDestCopy(E, LV); 431ffba662dSFariborz Jahanian } 432ffba662dSFariborz Jahanian 4337a51313dSChris Lattner void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 4347a51313dSChris Lattner // For an assignment to work, the value on the right has 4357a51313dSChris Lattner // to be compatible with the value on the left. 4362a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 4372a69547fSEli Friedman E->getRHS()->getType()) 4387a51313dSChris Lattner && "Invalid assignment"); 439d0a30016SJohn McCall 44099514b91SFariborz Jahanian if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->getLHS())) 44152a8cca5SFariborz Jahanian if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) 44299514b91SFariborz Jahanian if (VD->hasAttr<BlocksAttr>() && 44399514b91SFariborz Jahanian E->getRHS()->HasSideEffects(CGF.getContext())) { 44499514b91SFariborz Jahanian // When __block variable on LHS, the RHS must be evaluated first 44599514b91SFariborz Jahanian // as it may change the 'forwarding' field via call to Block_copy. 44699514b91SFariborz Jahanian LValue RHS = CGF.EmitLValue(E->getRHS()); 44799514b91SFariborz Jahanian LValue LHS = CGF.EmitLValue(E->getLHS()); 4488d6fc958SJohn McCall Dest = AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 44946759f4fSJohn McCall needsGC(E->getLHS()->getType()), 45046759f4fSJohn McCall AggValueSlot::IsAliased); 45199514b91SFariborz Jahanian EmitFinalDestCopy(E, RHS, true); 45299514b91SFariborz Jahanian return; 45399514b91SFariborz Jahanian } 45499514b91SFariborz Jahanian 4557a51313dSChris Lattner LValue LHS = CGF.EmitLValue(E->getLHS()); 4567a51313dSChris Lattner 4577a51313dSChris Lattner // Codegen the RHS so that it stores directly into the LHS. 4588d6fc958SJohn McCall AggValueSlot LHSSlot = 4598d6fc958SJohn McCall AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 46046759f4fSJohn McCall needsGC(E->getLHS()->getType()), 46146759f4fSJohn McCall AggValueSlot::IsAliased); 462b60e70f9SFariborz Jahanian CGF.EmitAggExpr(E->getRHS(), LHSSlot, false); 463ec3cbfe8SMike Stump EmitFinalDestCopy(E, LHS, true); 4647a51313dSChris Lattner } 4657a51313dSChris Lattner 466c07a0c7eSJohn McCall void AggExprEmitter:: 467c07a0c7eSJohn McCall VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 468a612e79bSDaniel Dunbar llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 469a612e79bSDaniel Dunbar llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 470a612e79bSDaniel Dunbar llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 4717a51313dSChris Lattner 472c07a0c7eSJohn McCall // Bind the common expression if necessary. 47348fd89adSEli Friedman CodeGenFunction::OpaqueValueMapping binding(CGF, E); 474c07a0c7eSJohn McCall 475ce1de617SJohn McCall CodeGenFunction::ConditionalEvaluation eval(CGF); 476b8841af8SEli Friedman CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 4777a51313dSChris Lattner 4785b26f65bSJohn McCall // Save whether the destination's lifetime is externally managed. 479cac93853SJohn McCall bool isExternallyDestructed = Dest.isExternallyDestructed(); 4807a51313dSChris Lattner 481ce1de617SJohn McCall eval.begin(CGF); 482ce1de617SJohn McCall CGF.EmitBlock(LHSBlock); 483c07a0c7eSJohn McCall Visit(E->getTrueExpr()); 484ce1de617SJohn McCall eval.end(CGF); 4857a51313dSChris Lattner 486ce1de617SJohn McCall assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!"); 487ce1de617SJohn McCall CGF.Builder.CreateBr(ContBlock); 4887a51313dSChris Lattner 4895b26f65bSJohn McCall // If the result of an agg expression is unused, then the emission 4905b26f65bSJohn McCall // of the LHS might need to create a destination slot. That's fine 4915b26f65bSJohn McCall // with us, and we can safely emit the RHS into the same slot, but 492cac93853SJohn McCall // we shouldn't claim that it's already being destructed. 493cac93853SJohn McCall Dest.setExternallyDestructed(isExternallyDestructed); 4945b26f65bSJohn McCall 495ce1de617SJohn McCall eval.begin(CGF); 496ce1de617SJohn McCall CGF.EmitBlock(RHSBlock); 497c07a0c7eSJohn McCall Visit(E->getFalseExpr()); 498ce1de617SJohn McCall eval.end(CGF); 4997a51313dSChris Lattner 5007a51313dSChris Lattner CGF.EmitBlock(ContBlock); 5017a51313dSChris Lattner } 5027a51313dSChris Lattner 5035b2095ceSAnders Carlsson void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 5045b2095ceSAnders Carlsson Visit(CE->getChosenSubExpr(CGF.getContext())); 5055b2095ceSAnders Carlsson } 5065b2095ceSAnders Carlsson 50721911e89SEli Friedman void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 508e9fcadd2SDaniel Dunbar llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 50913abd7e9SAnders Carlsson llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 51013abd7e9SAnders Carlsson 511020cddcfSSebastian Redl if (!ArgPtr) { 51213abd7e9SAnders Carlsson CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 513020cddcfSSebastian Redl return; 514020cddcfSSebastian Redl } 51513abd7e9SAnders Carlsson 5162e442a00SDaniel Dunbar EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType())); 51721911e89SEli Friedman } 51821911e89SEli Friedman 5193be22e27SAnders Carlsson void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 5207a626f63SJohn McCall // Ensure that we have a slot, but if we already do, remember 521cac93853SJohn McCall // whether it was externally destructed. 522cac93853SJohn McCall bool wasExternallyDestructed = Dest.isExternallyDestructed(); 5237a626f63SJohn McCall Dest = EnsureSlot(E->getType()); 524cac93853SJohn McCall 525cac93853SJohn McCall // We're going to push a destructor if there isn't already one. 526cac93853SJohn McCall Dest.setExternallyDestructed(); 5273be22e27SAnders Carlsson 5283be22e27SAnders Carlsson Visit(E->getSubExpr()); 5293be22e27SAnders Carlsson 530cac93853SJohn McCall // Push that destructor we promised. 531cac93853SJohn McCall if (!wasExternallyDestructed) 532702b2841SPeter Collingbourne CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddr()); 5333be22e27SAnders Carlsson } 5343be22e27SAnders Carlsson 535b7f8f594SAnders Carlsson void 5361619a504SAnders Carlsson AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 5377a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getType()); 5387a626f63SJohn McCall CGF.EmitCXXConstructExpr(E, Slot); 539c82b86dfSAnders Carlsson } 540c82b86dfSAnders Carlsson 5415d413781SJohn McCall void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) { 54208ef4660SJohn McCall CGF.enterFullExpression(E); 54308ef4660SJohn McCall CodeGenFunction::RunCleanupsScope cleanups(CGF); 54408ef4660SJohn McCall Visit(E->getSubExpr()); 545b7f8f594SAnders Carlsson } 546b7f8f594SAnders Carlsson 547747eb784SDouglas Gregor void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 5487a626f63SJohn McCall QualType T = E->getType(); 5497a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 5501553b190SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 55118ada985SAnders Carlsson } 55218ada985SAnders Carlsson 55318ada985SAnders Carlsson void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 5547a626f63SJohn McCall QualType T = E->getType(); 5557a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 5561553b190SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 557ff3507b9SNuno Lopes } 558ff3507b9SNuno Lopes 55927a3631bSChris Lattner /// isSimpleZero - If emitting this value will obviously just cause a store of 56027a3631bSChris Lattner /// zero to memory, return true. This can return false if uncertain, so it just 56127a3631bSChris Lattner /// handles simple cases. 56227a3631bSChris Lattner static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { 56391147596SPeter Collingbourne E = E->IgnoreParens(); 56491147596SPeter Collingbourne 56527a3631bSChris Lattner // 0 56627a3631bSChris Lattner if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) 56727a3631bSChris Lattner return IL->getValue() == 0; 56827a3631bSChris Lattner // +0.0 56927a3631bSChris Lattner if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E)) 57027a3631bSChris Lattner return FL->getValue().isPosZero(); 57127a3631bSChris Lattner // int() 57227a3631bSChris Lattner if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) && 57327a3631bSChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 57427a3631bSChris Lattner return true; 57527a3631bSChris Lattner // (int*)0 - Null pointer expressions. 57627a3631bSChris Lattner if (const CastExpr *ICE = dyn_cast<CastExpr>(E)) 57727a3631bSChris Lattner return ICE->getCastKind() == CK_NullToPointer; 57827a3631bSChris Lattner // '\0' 57927a3631bSChris Lattner if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) 58027a3631bSChris Lattner return CL->getValue() == 0; 58127a3631bSChris Lattner 58227a3631bSChris Lattner // Otherwise, hard case: conservatively return false. 58327a3631bSChris Lattner return false; 58427a3631bSChris Lattner } 58527a3631bSChris Lattner 58627a3631bSChris Lattner 587b247350eSAnders Carlsson void 5881553b190SJohn McCall AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { 5891553b190SJohn McCall QualType type = LV.getType(); 590df0fe27bSMike Stump // FIXME: Ignore result? 591579a05d7SChris Lattner // FIXME: Are initializers affected by volatile? 59227a3631bSChris Lattner if (Dest.isZeroed() && isSimpleZero(E, CGF)) { 59327a3631bSChris Lattner // Storing "i32 0" to a zero'd memory location is a noop. 59427a3631bSChris Lattner } else if (isa<ImplicitValueInitExpr>(E)) { 5951553b190SJohn McCall EmitNullInitializationToLValue(LV); 5961553b190SJohn McCall } else if (type->isReferenceType()) { 59704775f84SAnders Carlsson RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); 59855e1fbc8SJohn McCall CGF.EmitStoreThroughLValue(RV, LV); 5991553b190SJohn McCall } else if (type->isAnyComplexType()) { 6000202cb40SDouglas Gregor CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); 6011553b190SJohn McCall } else if (CGF.hasAggregateLLVMType(type)) { 6028d6fc958SJohn McCall CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV, 6038d6fc958SJohn McCall AggValueSlot::IsDestructed, 6048d6fc958SJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 605a5efa738SJohn McCall AggValueSlot::IsNotAliased, 6061553b190SJohn McCall Dest.isZeroed())); 60731168b07SJohn McCall } else if (LV.isSimple()) { 6081553b190SJohn McCall CGF.EmitScalarInit(E, /*D=*/0, LV, /*Captured=*/false); 6096e313210SEli Friedman } else { 61055e1fbc8SJohn McCall CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV); 6117a51313dSChris Lattner } 612579a05d7SChris Lattner } 613579a05d7SChris Lattner 6141553b190SJohn McCall void AggExprEmitter::EmitNullInitializationToLValue(LValue lv) { 6151553b190SJohn McCall QualType type = lv.getType(); 6161553b190SJohn McCall 61727a3631bSChris Lattner // If the destination slot is already zeroed out before the aggregate is 61827a3631bSChris Lattner // copied into it, we don't have to emit any zeros here. 6191553b190SJohn McCall if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type)) 62027a3631bSChris Lattner return; 62127a3631bSChris Lattner 6221553b190SJohn McCall if (!CGF.hasAggregateLLVMType(type)) { 623579a05d7SChris Lattner // For non-aggregates, we can store zero 6241553b190SJohn McCall llvm::Value *null = llvm::Constant::getNullValue(CGF.ConvertType(type)); 62555e1fbc8SJohn McCall CGF.EmitStoreThroughLValue(RValue::get(null), lv); 626579a05d7SChris Lattner } else { 627579a05d7SChris Lattner // There's a potential optimization opportunity in combining 628579a05d7SChris Lattner // memsets; that would be easy for arrays, but relatively 629579a05d7SChris Lattner // difficult for structures with the current code. 6301553b190SJohn McCall CGF.EmitNullInitialization(lv.getAddress(), lv.getType()); 631579a05d7SChris Lattner } 632579a05d7SChris Lattner } 633579a05d7SChris Lattner 634579a05d7SChris Lattner void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 635f5d08c9eSEli Friedman #if 0 6366d11ec8cSEli Friedman // FIXME: Assess perf here? Figure out what cases are worth optimizing here 6376d11ec8cSEli Friedman // (Length of globals? Chunks of zeroed-out space?). 638f5d08c9eSEli Friedman // 63918bb9284SMike Stump // If we can, prefer a copy from a global; this is a lot less code for long 64018bb9284SMike Stump // globals, and it's easier for the current optimizers to analyze. 6416d11ec8cSEli Friedman if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { 642c59bb48eSEli Friedman llvm::GlobalVariable* GV = 6436d11ec8cSEli Friedman new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, 6446d11ec8cSEli Friedman llvm::GlobalValue::InternalLinkage, C, ""); 6452e442a00SDaniel Dunbar EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType())); 646c59bb48eSEli Friedman return; 647c59bb48eSEli Friedman } 648f5d08c9eSEli Friedman #endif 649f53c0968SChris Lattner if (E->hadArrayRangeDesignator()) 650bf7207a1SDouglas Gregor CGF.ErrorUnsupported(E, "GNU array range designator extension"); 651bf7207a1SDouglas Gregor 6527a626f63SJohn McCall llvm::Value *DestPtr = Dest.getAddr(); 6537a626f63SJohn McCall 654579a05d7SChris Lattner // Handle initialization of an array. 655579a05d7SChris Lattner if (E->getType()->isArrayType()) { 6562192fe50SChris Lattner llvm::PointerType *APType = 657579a05d7SChris Lattner cast<llvm::PointerType>(DestPtr->getType()); 6582192fe50SChris Lattner llvm::ArrayType *AType = 659579a05d7SChris Lattner cast<llvm::ArrayType>(APType->getElementType()); 660579a05d7SChris Lattner 661579a05d7SChris Lattner uint64_t NumInitElements = E->getNumInits(); 662f23b6fa4SEli Friedman 6630f398c44SChris Lattner if (E->getNumInits() > 0) { 6640f398c44SChris Lattner QualType T1 = E->getType(); 6650f398c44SChris Lattner QualType T2 = E->getInit(0)->getType(); 6662a69547fSEli Friedman if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { 667f23b6fa4SEli Friedman EmitAggLoadOfLValue(E->getInit(0)); 668f23b6fa4SEli Friedman return; 669f23b6fa4SEli Friedman } 6700f398c44SChris Lattner } 671f23b6fa4SEli Friedman 672579a05d7SChris Lattner uint64_t NumArrayElements = AType->getNumElements(); 67382fe67bbSJohn McCall assert(NumInitElements <= NumArrayElements); 674579a05d7SChris Lattner 67582fe67bbSJohn McCall QualType elementType = E->getType().getCanonicalType(); 67682fe67bbSJohn McCall elementType = CGF.getContext().getQualifiedType( 67782fe67bbSJohn McCall cast<ArrayType>(elementType)->getElementType(), 67882fe67bbSJohn McCall elementType.getQualifiers() + Dest.getQualifiers()); 67982fe67bbSJohn McCall 68082fe67bbSJohn McCall // DestPtr is an array*. Construct an elementType* by drilling 68182fe67bbSJohn McCall // down a level. 68282fe67bbSJohn McCall llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0); 68382fe67bbSJohn McCall llvm::Value *indices[] = { zero, zero }; 68482fe67bbSJohn McCall llvm::Value *begin = 685040dd82fSJay Foad Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin"); 68682fe67bbSJohn McCall 68782fe67bbSJohn McCall // Exception safety requires us to destroy all the 68882fe67bbSJohn McCall // already-constructed members if an initializer throws. 68982fe67bbSJohn McCall // For that, we'll need an EH cleanup. 69082fe67bbSJohn McCall QualType::DestructionKind dtorKind = elementType.isDestructedType(); 69182fe67bbSJohn McCall llvm::AllocaInst *endOfInit = 0; 69282fe67bbSJohn McCall EHScopeStack::stable_iterator cleanup; 693f4beacd0SJohn McCall llvm::Instruction *cleanupDominator = 0; 69482fe67bbSJohn McCall if (CGF.needsEHCleanup(dtorKind)) { 69582fe67bbSJohn McCall // In principle we could tell the cleanup where we are more 69682fe67bbSJohn McCall // directly, but the control flow can get so varied here that it 69782fe67bbSJohn McCall // would actually be quite complex. Therefore we go through an 69882fe67bbSJohn McCall // alloca. 69982fe67bbSJohn McCall endOfInit = CGF.CreateTempAlloca(begin->getType(), 70082fe67bbSJohn McCall "arrayinit.endOfInit"); 701f4beacd0SJohn McCall cleanupDominator = Builder.CreateStore(begin, endOfInit); 702178360e1SJohn McCall CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType, 703178360e1SJohn McCall CGF.getDestroyer(dtorKind)); 70482fe67bbSJohn McCall cleanup = CGF.EHStack.stable_begin(); 70582fe67bbSJohn McCall 70682fe67bbSJohn McCall // Otherwise, remember that we didn't need a cleanup. 70782fe67bbSJohn McCall } else { 70882fe67bbSJohn McCall dtorKind = QualType::DK_none; 709e07425a5SArgyrios Kyrtzidis } 710e07425a5SArgyrios Kyrtzidis 71182fe67bbSJohn McCall llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1); 71227a3631bSChris Lattner 71382fe67bbSJohn McCall // The 'current element to initialize'. The invariants on this 71482fe67bbSJohn McCall // variable are complicated. Essentially, after each iteration of 71582fe67bbSJohn McCall // the loop, it points to the last initialized element, except 71682fe67bbSJohn McCall // that it points to the beginning of the array before any 71782fe67bbSJohn McCall // elements have been initialized. 71882fe67bbSJohn McCall llvm::Value *element = begin; 71927a3631bSChris Lattner 72082fe67bbSJohn McCall // Emit the explicit initializers. 72182fe67bbSJohn McCall for (uint64_t i = 0; i != NumInitElements; ++i) { 72282fe67bbSJohn McCall // Advance to the next element. 723178360e1SJohn McCall if (i > 0) { 72482fe67bbSJohn McCall element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element"); 72582fe67bbSJohn McCall 726178360e1SJohn McCall // Tell the cleanup that it needs to destroy up to this 727178360e1SJohn McCall // element. TODO: some of these stores can be trivially 728178360e1SJohn McCall // observed to be unnecessary. 729178360e1SJohn McCall if (endOfInit) Builder.CreateStore(element, endOfInit); 730178360e1SJohn McCall } 731178360e1SJohn McCall 73282fe67bbSJohn McCall LValue elementLV = CGF.MakeAddrLValue(element, elementType); 73382fe67bbSJohn McCall EmitInitializationToLValue(E->getInit(i), elementLV); 73482fe67bbSJohn McCall } 73582fe67bbSJohn McCall 73682fe67bbSJohn McCall // Check whether there's a non-trivial array-fill expression. 73782fe67bbSJohn McCall // Note that this will be a CXXConstructExpr even if the element 73882fe67bbSJohn McCall // type is an array (or array of array, etc.) of class type. 73982fe67bbSJohn McCall Expr *filler = E->getArrayFiller(); 74082fe67bbSJohn McCall bool hasTrivialFiller = true; 74182fe67bbSJohn McCall if (CXXConstructExpr *cons = dyn_cast_or_null<CXXConstructExpr>(filler)) { 74282fe67bbSJohn McCall assert(cons->getConstructor()->isDefaultConstructor()); 74382fe67bbSJohn McCall hasTrivialFiller = cons->getConstructor()->isTrivial(); 74482fe67bbSJohn McCall } 74582fe67bbSJohn McCall 74682fe67bbSJohn McCall // Any remaining elements need to be zero-initialized, possibly 74782fe67bbSJohn McCall // using the filler expression. We can skip this if the we're 74882fe67bbSJohn McCall // emitting to zeroed memory. 74982fe67bbSJohn McCall if (NumInitElements != NumArrayElements && 75082fe67bbSJohn McCall !(Dest.isZeroed() && hasTrivialFiller && 75182fe67bbSJohn McCall CGF.getTypes().isZeroInitializable(elementType))) { 75282fe67bbSJohn McCall 75382fe67bbSJohn McCall // Use an actual loop. This is basically 75482fe67bbSJohn McCall // do { *array++ = filler; } while (array != end); 75582fe67bbSJohn McCall 75682fe67bbSJohn McCall // Advance to the start of the rest of the array. 757178360e1SJohn McCall if (NumInitElements) { 75882fe67bbSJohn McCall element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start"); 759178360e1SJohn McCall if (endOfInit) Builder.CreateStore(element, endOfInit); 760178360e1SJohn McCall } 76182fe67bbSJohn McCall 76282fe67bbSJohn McCall // Compute the end of the array. 76382fe67bbSJohn McCall llvm::Value *end = Builder.CreateInBoundsGEP(begin, 76482fe67bbSJohn McCall llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements), 76582fe67bbSJohn McCall "arrayinit.end"); 76682fe67bbSJohn McCall 76782fe67bbSJohn McCall llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); 76882fe67bbSJohn McCall llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body"); 76982fe67bbSJohn McCall 77082fe67bbSJohn McCall // Jump into the body. 77182fe67bbSJohn McCall CGF.EmitBlock(bodyBB); 77282fe67bbSJohn McCall llvm::PHINode *currentElement = 77382fe67bbSJohn McCall Builder.CreatePHI(element->getType(), 2, "arrayinit.cur"); 77482fe67bbSJohn McCall currentElement->addIncoming(element, entryBB); 77582fe67bbSJohn McCall 77682fe67bbSJohn McCall // Emit the actual filler expression. 77782fe67bbSJohn McCall LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType); 77882fe67bbSJohn McCall if (filler) 77982fe67bbSJohn McCall EmitInitializationToLValue(filler, elementLV); 780579a05d7SChris Lattner else 78182fe67bbSJohn McCall EmitNullInitializationToLValue(elementLV); 78227a3631bSChris Lattner 78382fe67bbSJohn McCall // Move on to the next element. 78482fe67bbSJohn McCall llvm::Value *nextElement = 78582fe67bbSJohn McCall Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next"); 78682fe67bbSJohn McCall 787178360e1SJohn McCall // Tell the EH cleanup that we finished with the last element. 788178360e1SJohn McCall if (endOfInit) Builder.CreateStore(nextElement, endOfInit); 789178360e1SJohn McCall 79082fe67bbSJohn McCall // Leave the loop if we're done. 79182fe67bbSJohn McCall llvm::Value *done = Builder.CreateICmpEQ(nextElement, end, 79282fe67bbSJohn McCall "arrayinit.done"); 79382fe67bbSJohn McCall llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end"); 79482fe67bbSJohn McCall Builder.CreateCondBr(done, endBB, bodyBB); 79582fe67bbSJohn McCall currentElement->addIncoming(nextElement, Builder.GetInsertBlock()); 79682fe67bbSJohn McCall 79782fe67bbSJohn McCall CGF.EmitBlock(endBB); 798579a05d7SChris Lattner } 79982fe67bbSJohn McCall 80082fe67bbSJohn McCall // Leave the partial-array cleanup if we entered one. 801f4beacd0SJohn McCall if (dtorKind) CGF.DeactivateCleanupBlock(cleanup, cleanupDominator); 80282fe67bbSJohn McCall 803579a05d7SChris Lattner return; 804579a05d7SChris Lattner } 805579a05d7SChris Lattner 806579a05d7SChris Lattner assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 807579a05d7SChris Lattner 808579a05d7SChris Lattner // Do struct initialization; this code just sets each individual member 809579a05d7SChris Lattner // to the approprate value. This makes bitfield support automatic; 810579a05d7SChris Lattner // the disadvantage is that the generated code is more difficult for 811579a05d7SChris Lattner // the optimizer, especially with bitfields. 812579a05d7SChris Lattner unsigned NumInitElements = E->getNumInits(); 8133b935d33SJohn McCall RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl(); 81452bcf963SChris Lattner 8153b935d33SJohn McCall if (record->isUnion()) { 8165169570eSDouglas Gregor // Only initialize one field of a union. The field itself is 8175169570eSDouglas Gregor // specified by the initializer list. 8185169570eSDouglas Gregor if (!E->getInitializedFieldInUnion()) { 8195169570eSDouglas Gregor // Empty union; we have nothing to do. 8205169570eSDouglas Gregor 8215169570eSDouglas Gregor #ifndef NDEBUG 8225169570eSDouglas Gregor // Make sure that it's really an empty and not a failure of 8235169570eSDouglas Gregor // semantic analysis. 8243b935d33SJohn McCall for (RecordDecl::field_iterator Field = record->field_begin(), 8253b935d33SJohn McCall FieldEnd = record->field_end(); 8265169570eSDouglas Gregor Field != FieldEnd; ++Field) 8275169570eSDouglas Gregor assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 8285169570eSDouglas Gregor #endif 8295169570eSDouglas Gregor return; 8305169570eSDouglas Gregor } 8315169570eSDouglas Gregor 8325169570eSDouglas Gregor // FIXME: volatility 8335169570eSDouglas Gregor FieldDecl *Field = E->getInitializedFieldInUnion(); 8345169570eSDouglas Gregor 83527a3631bSChris Lattner LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0); 8365169570eSDouglas Gregor if (NumInitElements) { 8375169570eSDouglas Gregor // Store the initializer into the field 8381553b190SJohn McCall EmitInitializationToLValue(E->getInit(0), FieldLoc); 8395169570eSDouglas Gregor } else { 84027a3631bSChris Lattner // Default-initialize to null. 8411553b190SJohn McCall EmitNullInitializationToLValue(FieldLoc); 8425169570eSDouglas Gregor } 8435169570eSDouglas Gregor 8445169570eSDouglas Gregor return; 8455169570eSDouglas Gregor } 846579a05d7SChris Lattner 8473b935d33SJohn McCall // We'll need to enter cleanup scopes in case any of the member 8483b935d33SJohn McCall // initializers throw an exception. 8490e62c1ccSChris Lattner SmallVector<EHScopeStack::stable_iterator, 16> cleanups; 850f4beacd0SJohn McCall llvm::Instruction *cleanupDominator = 0; 8513b935d33SJohn McCall 852579a05d7SChris Lattner // Here we iterate over the fields; this makes it simpler to both 853579a05d7SChris Lattner // default-initialize fields and skip over unnamed fields. 8543b935d33SJohn McCall unsigned curInitIndex = 0; 8553b935d33SJohn McCall for (RecordDecl::field_iterator field = record->field_begin(), 8563b935d33SJohn McCall fieldEnd = record->field_end(); 8573b935d33SJohn McCall field != fieldEnd; ++field) { 8583b935d33SJohn McCall // We're done once we hit the flexible array member. 8593b935d33SJohn McCall if (field->getType()->isIncompleteArrayType()) 86091f84216SDouglas Gregor break; 86191f84216SDouglas Gregor 8623b935d33SJohn McCall // Always skip anonymous bitfields. 8633b935d33SJohn McCall if (field->isUnnamedBitfield()) 864579a05d7SChris Lattner continue; 86517bd094aSDouglas Gregor 8663b935d33SJohn McCall // We're done if we reach the end of the explicit initializers, we 8673b935d33SJohn McCall // have a zeroed object, and the rest of the fields are 8683b935d33SJohn McCall // zero-initializable. 8693b935d33SJohn McCall if (curInitIndex == NumInitElements && Dest.isZeroed() && 87027a3631bSChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 87127a3631bSChris Lattner break; 87227a3631bSChris Lattner 873327944b3SEli Friedman // FIXME: volatility 8743b935d33SJohn McCall LValue LV = CGF.EmitLValueForFieldInitialization(DestPtr, *field, 0); 8757c1baf46SFariborz Jahanian // We never generate write-barries for initialized fields. 8763b935d33SJohn McCall LV.setNonGC(true); 87727a3631bSChris Lattner 8783b935d33SJohn McCall if (curInitIndex < NumInitElements) { 879e18aaf2cSChris Lattner // Store the initializer into the field. 8803b935d33SJohn McCall EmitInitializationToLValue(E->getInit(curInitIndex++), LV); 881579a05d7SChris Lattner } else { 882579a05d7SChris Lattner // We're out of initalizers; default-initialize to null 8833b935d33SJohn McCall EmitNullInitializationToLValue(LV); 8843b935d33SJohn McCall } 8853b935d33SJohn McCall 8863b935d33SJohn McCall // Push a destructor if necessary. 8873b935d33SJohn McCall // FIXME: if we have an array of structures, all explicitly 8883b935d33SJohn McCall // initialized, we can end up pushing a linear number of cleanups. 8893b935d33SJohn McCall bool pushedCleanup = false; 8903b935d33SJohn McCall if (QualType::DestructionKind dtorKind 8913b935d33SJohn McCall = field->getType().isDestructedType()) { 8923b935d33SJohn McCall assert(LV.isSimple()); 8933b935d33SJohn McCall if (CGF.needsEHCleanup(dtorKind)) { 894f4beacd0SJohn McCall if (!cleanupDominator) 895f4beacd0SJohn McCall cleanupDominator = CGF.Builder.CreateUnreachable(); // placeholder 896f4beacd0SJohn McCall 8973b935d33SJohn McCall CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(), 8983b935d33SJohn McCall CGF.getDestroyer(dtorKind), false); 8993b935d33SJohn McCall cleanups.push_back(CGF.EHStack.stable_begin()); 9003b935d33SJohn McCall pushedCleanup = true; 9013b935d33SJohn McCall } 902579a05d7SChris Lattner } 90327a3631bSChris Lattner 90427a3631bSChris Lattner // If the GEP didn't get used because of a dead zero init or something 90527a3631bSChris Lattner // else, clean it up for -O0 builds and general tidiness. 9063b935d33SJohn McCall if (!pushedCleanup && LV.isSimple()) 90727a3631bSChris Lattner if (llvm::GetElementPtrInst *GEP = 9083b935d33SJohn McCall dyn_cast<llvm::GetElementPtrInst>(LV.getAddress())) 90927a3631bSChris Lattner if (GEP->use_empty()) 91027a3631bSChris Lattner GEP->eraseFromParent(); 9117a51313dSChris Lattner } 9123b935d33SJohn McCall 9133b935d33SJohn McCall // Deactivate all the partial cleanups in reverse order, which 9143b935d33SJohn McCall // generally means popping them. 9153b935d33SJohn McCall for (unsigned i = cleanups.size(); i != 0; --i) 916f4beacd0SJohn McCall CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator); 917f4beacd0SJohn McCall 918f4beacd0SJohn McCall // Destroy the placeholder if we made one. 919f4beacd0SJohn McCall if (cleanupDominator) 920f4beacd0SJohn McCall cleanupDominator->eraseFromParent(); 9217a51313dSChris Lattner } 9227a51313dSChris Lattner 9237a51313dSChris Lattner //===----------------------------------------------------------------------===// 9247a51313dSChris Lattner // Entry Points into this File 9257a51313dSChris Lattner //===----------------------------------------------------------------------===// 9267a51313dSChris Lattner 92727a3631bSChris Lattner /// GetNumNonZeroBytesInInit - Get an approximate count of the number of 92827a3631bSChris Lattner /// non-zero bytes that will be stored when outputting the initializer for the 92927a3631bSChris Lattner /// specified initializer expression. 930df94cb7dSKen Dyck static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) { 93191147596SPeter Collingbourne E = E->IgnoreParens(); 93227a3631bSChris Lattner 93327a3631bSChris Lattner // 0 and 0.0 won't require any non-zero stores! 934df94cb7dSKen Dyck if (isSimpleZero(E, CGF)) return CharUnits::Zero(); 93527a3631bSChris Lattner 93627a3631bSChris Lattner // If this is an initlist expr, sum up the size of sizes of the (present) 93727a3631bSChris Lattner // elements. If this is something weird, assume the whole thing is non-zero. 93827a3631bSChris Lattner const InitListExpr *ILE = dyn_cast<InitListExpr>(E); 93927a3631bSChris Lattner if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType())) 940df94cb7dSKen Dyck return CGF.getContext().getTypeSizeInChars(E->getType()); 94127a3631bSChris Lattner 942c5cc2fb9SChris Lattner // InitListExprs for structs have to be handled carefully. If there are 943c5cc2fb9SChris Lattner // reference members, we need to consider the size of the reference, not the 944c5cc2fb9SChris Lattner // referencee. InitListExprs for unions and arrays can't have references. 9455cd84755SChris Lattner if (const RecordType *RT = E->getType()->getAs<RecordType>()) { 9465cd84755SChris Lattner if (!RT->isUnionType()) { 947c5cc2fb9SChris Lattner RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 948df94cb7dSKen Dyck CharUnits NumNonZeroBytes = CharUnits::Zero(); 949c5cc2fb9SChris Lattner 950c5cc2fb9SChris Lattner unsigned ILEElement = 0; 951c5cc2fb9SChris Lattner for (RecordDecl::field_iterator Field = SD->field_begin(), 952c5cc2fb9SChris Lattner FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) { 953c5cc2fb9SChris Lattner // We're done once we hit the flexible array member or run out of 954c5cc2fb9SChris Lattner // InitListExpr elements. 955c5cc2fb9SChris Lattner if (Field->getType()->isIncompleteArrayType() || 956c5cc2fb9SChris Lattner ILEElement == ILE->getNumInits()) 957c5cc2fb9SChris Lattner break; 958c5cc2fb9SChris Lattner if (Field->isUnnamedBitfield()) 959c5cc2fb9SChris Lattner continue; 960c5cc2fb9SChris Lattner 961c5cc2fb9SChris Lattner const Expr *E = ILE->getInit(ILEElement++); 962c5cc2fb9SChris Lattner 963c5cc2fb9SChris Lattner // Reference values are always non-null and have the width of a pointer. 9645cd84755SChris Lattner if (Field->getType()->isReferenceType()) 965df94cb7dSKen Dyck NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits( 966e8bbc121SDouglas Gregor CGF.getContext().getTargetInfo().getPointerWidth(0)); 9675cd84755SChris Lattner else 968c5cc2fb9SChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF); 969c5cc2fb9SChris Lattner } 970c5cc2fb9SChris Lattner 971c5cc2fb9SChris Lattner return NumNonZeroBytes; 972c5cc2fb9SChris Lattner } 9735cd84755SChris Lattner } 974c5cc2fb9SChris Lattner 975c5cc2fb9SChris Lattner 976df94cb7dSKen Dyck CharUnits NumNonZeroBytes = CharUnits::Zero(); 97727a3631bSChris Lattner for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) 97827a3631bSChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF); 97927a3631bSChris Lattner return NumNonZeroBytes; 98027a3631bSChris Lattner } 98127a3631bSChris Lattner 98227a3631bSChris Lattner /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of 98327a3631bSChris Lattner /// zeros in it, emit a memset and avoid storing the individual zeros. 98427a3631bSChris Lattner /// 98527a3631bSChris Lattner static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E, 98627a3631bSChris Lattner CodeGenFunction &CGF) { 98727a3631bSChris Lattner // If the slot is already known to be zeroed, nothing to do. Don't mess with 98827a3631bSChris Lattner // volatile stores. 98927a3631bSChris Lattner if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return; 99027a3631bSChris Lattner 99103535265SArgyrios Kyrtzidis // C++ objects with a user-declared constructor don't need zero'ing. 99203535265SArgyrios Kyrtzidis if (CGF.getContext().getLangOptions().CPlusPlus) 99303535265SArgyrios Kyrtzidis if (const RecordType *RT = CGF.getContext() 99403535265SArgyrios Kyrtzidis .getBaseElementType(E->getType())->getAs<RecordType>()) { 99503535265SArgyrios Kyrtzidis const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 99603535265SArgyrios Kyrtzidis if (RD->hasUserDeclaredConstructor()) 99703535265SArgyrios Kyrtzidis return; 99803535265SArgyrios Kyrtzidis } 99903535265SArgyrios Kyrtzidis 100027a3631bSChris Lattner // If the type is 16-bytes or smaller, prefer individual stores over memset. 1001239a3357SKen Dyck std::pair<CharUnits, CharUnits> TypeInfo = 1002239a3357SKen Dyck CGF.getContext().getTypeInfoInChars(E->getType()); 1003239a3357SKen Dyck if (TypeInfo.first <= CharUnits::fromQuantity(16)) 100427a3631bSChris Lattner return; 100527a3631bSChris Lattner 100627a3631bSChris Lattner // Check to see if over 3/4 of the initializer are known to be zero. If so, 100727a3631bSChris Lattner // we prefer to emit memset + individual stores for the rest. 1008239a3357SKen Dyck CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF); 1009239a3357SKen Dyck if (NumNonZeroBytes*4 > TypeInfo.first) 101027a3631bSChris Lattner return; 101127a3631bSChris Lattner 101227a3631bSChris Lattner // Okay, it seems like a good idea to use an initial memset, emit the call. 1013239a3357SKen Dyck llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity()); 1014239a3357SKen Dyck CharUnits Align = TypeInfo.second; 101527a3631bSChris Lattner 101627a3631bSChris Lattner llvm::Value *Loc = Slot.getAddr(); 101727a3631bSChris Lattner 1018*ece0409aSChris Lattner Loc = CGF.Builder.CreateBitCast(Loc, CGF.Int8PtrTy); 1019239a3357SKen Dyck CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, 1020239a3357SKen Dyck Align.getQuantity(), false); 102127a3631bSChris Lattner 102227a3631bSChris Lattner // Tell the AggExprEmitter that the slot is known zero. 102327a3631bSChris Lattner Slot.setZeroed(); 102427a3631bSChris Lattner } 102527a3631bSChris Lattner 102627a3631bSChris Lattner 102727a3631bSChris Lattner 102827a3631bSChris Lattner 102925306cacSMike Stump /// EmitAggExpr - Emit the computation of the specified expression of aggregate 103025306cacSMike Stump /// type. The result is computed into DestPtr. Note that if DestPtr is null, 103125306cacSMike Stump /// the value of the aggregate expression is not needed. If VolatileDest is 103225306cacSMike Stump /// true, DestPtr cannot be 0. 10337a626f63SJohn McCall /// 10347a626f63SJohn McCall /// \param IsInitializer - true if this evaluation is initializing an 10357a626f63SJohn McCall /// object whose lifetime is already being managed. 10367a626f63SJohn McCall void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot, 1037b60e70f9SFariborz Jahanian bool IgnoreResult) { 10387a51313dSChris Lattner assert(E && hasAggregateLLVMType(E->getType()) && 10397a51313dSChris Lattner "Invalid aggregate expression to emit"); 104027a3631bSChris Lattner assert((Slot.getAddr() != 0 || Slot.isIgnored()) && 104127a3631bSChris Lattner "slot has bits but no address"); 10427a51313dSChris Lattner 104327a3631bSChris Lattner // Optimize the slot if possible. 104427a3631bSChris Lattner CheckAggExprForMemSetUse(Slot, E, *this); 104527a3631bSChris Lattner 104627a3631bSChris Lattner AggExprEmitter(*this, Slot, IgnoreResult).Visit(const_cast<Expr*>(E)); 10477a51313dSChris Lattner } 10480bc8e86dSDaniel Dunbar 1049d0bc7b9dSDaniel Dunbar LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { 1050d0bc7b9dSDaniel Dunbar assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!"); 1051a7566f16SDaniel Dunbar llvm::Value *Temp = CreateMemTemp(E->getType()); 10522e442a00SDaniel Dunbar LValue LV = MakeAddrLValue(Temp, E->getType()); 10538d6fc958SJohn McCall EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed, 105446759f4fSJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 105546759f4fSJohn McCall AggValueSlot::IsNotAliased)); 10562e442a00SDaniel Dunbar return LV; 1057d0bc7b9dSDaniel Dunbar } 1058d0bc7b9dSDaniel Dunbar 10590bc8e86dSDaniel Dunbar void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 10605e9e61b8SMike Stump llvm::Value *SrcPtr, QualType Ty, 10616d694a38SEli Friedman bool isVolatile, unsigned Alignment) { 10620bc8e86dSDaniel Dunbar assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 10630bc8e86dSDaniel Dunbar 106416e94af6SAnders Carlsson if (getContext().getLangOptions().CPlusPlus) { 106516e94af6SAnders Carlsson if (const RecordType *RT = Ty->getAs<RecordType>()) { 1066f22101a0SDouglas Gregor CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 1067f22101a0SDouglas Gregor assert((Record->hasTrivialCopyConstructor() || 1068146b8e9aSDouglas Gregor Record->hasTrivialCopyAssignment() || 1069146b8e9aSDouglas Gregor Record->hasTrivialMoveConstructor() || 1070146b8e9aSDouglas Gregor Record->hasTrivialMoveAssignment()) && 1071f22101a0SDouglas Gregor "Trying to aggregate-copy a type without a trivial copy " 1072f22101a0SDouglas Gregor "constructor or assignment operator"); 1073265b8b8dSDouglas Gregor // Ignore empty classes in C++. 1074f22101a0SDouglas Gregor if (Record->isEmpty()) 107516e94af6SAnders Carlsson return; 107616e94af6SAnders Carlsson } 107716e94af6SAnders Carlsson } 107816e94af6SAnders Carlsson 1079ca05dfefSChris Lattner // Aggregate assignment turns into llvm.memcpy. This is almost valid per 10803ef668c2SChris Lattner // C99 6.5.16.1p3, which states "If the value being stored in an object is 10813ef668c2SChris Lattner // read from another object that overlaps in anyway the storage of the first 10823ef668c2SChris Lattner // object, then the overlap shall be exact and the two objects shall have 10833ef668c2SChris Lattner // qualified or unqualified versions of a compatible type." 10843ef668c2SChris Lattner // 1085ca05dfefSChris Lattner // memcpy is not defined if the source and destination pointers are exactly 10863ef668c2SChris Lattner // equal, but other compilers do this optimization, and almost every memcpy 10873ef668c2SChris Lattner // implementation handles this case safely. If there is a libc that does not 10883ef668c2SChris Lattner // safely handle this, we can add a target hook. 10890bc8e86dSDaniel Dunbar 10900bc8e86dSDaniel Dunbar // Get size and alignment info for this aggregate. 1091bb2c2400SKen Dyck std::pair<CharUnits, CharUnits> TypeInfo = 1092bb2c2400SKen Dyck getContext().getTypeInfoInChars(Ty); 10930bc8e86dSDaniel Dunbar 10946d694a38SEli Friedman if (!Alignment) 10956d694a38SEli Friedman Alignment = TypeInfo.second.getQuantity(); 10966d694a38SEli Friedman 10970bc8e86dSDaniel Dunbar // FIXME: Handle variable sized types. 10980bc8e86dSDaniel Dunbar 109986736572SMike Stump // FIXME: If we have a volatile struct, the optimizer can remove what might 110086736572SMike Stump // appear to be `extra' memory ops: 110186736572SMike Stump // 110286736572SMike Stump // volatile struct { int i; } a, b; 110386736572SMike Stump // 110486736572SMike Stump // int main() { 110586736572SMike Stump // a = b; 110686736572SMike Stump // a = b; 110786736572SMike Stump // } 110886736572SMike Stump // 1109cc2ab0cdSMon P Wang // we need to use a different call here. We use isVolatile to indicate when 1110ec3cbfe8SMike Stump // either the source or the destination is volatile. 1111cc2ab0cdSMon P Wang 11122192fe50SChris Lattner llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); 11132192fe50SChris Lattner llvm::Type *DBP = 1114ad7c5c16SJohn McCall llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace()); 111576399eb2SBenjamin Kramer DestPtr = Builder.CreateBitCast(DestPtr, DBP); 1116cc2ab0cdSMon P Wang 11172192fe50SChris Lattner llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); 11182192fe50SChris Lattner llvm::Type *SBP = 1119ad7c5c16SJohn McCall llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace()); 112076399eb2SBenjamin Kramer SrcPtr = Builder.CreateBitCast(SrcPtr, SBP); 1121cc2ab0cdSMon P Wang 112231168b07SJohn McCall // Don't do any of the memmove_collectable tests if GC isn't set. 112379a91418SDouglas Gregor if (CGM.getLangOptions().getGC() == LangOptions::NonGC) { 112431168b07SJohn McCall // fall through 112531168b07SJohn McCall } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { 1126021510e9SFariborz Jahanian RecordDecl *Record = RecordTy->getDecl(); 1127021510e9SFariborz Jahanian if (Record->hasObjectMember()) { 1128bb2c2400SKen Dyck CharUnits size = TypeInfo.first; 11292192fe50SChris Lattner llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1130bb2c2400SKen Dyck llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1131021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1132021510e9SFariborz Jahanian SizeVal); 1133021510e9SFariborz Jahanian return; 1134021510e9SFariborz Jahanian } 113531168b07SJohn McCall } else if (Ty->isArrayType()) { 1136021510e9SFariborz Jahanian QualType BaseType = getContext().getBaseElementType(Ty); 1137021510e9SFariborz Jahanian if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { 1138021510e9SFariborz Jahanian if (RecordTy->getDecl()->hasObjectMember()) { 1139bb2c2400SKen Dyck CharUnits size = TypeInfo.first; 11402192fe50SChris Lattner llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1141bb2c2400SKen Dyck llvm::Value *SizeVal = 1142bb2c2400SKen Dyck llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1143021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1144021510e9SFariborz Jahanian SizeVal); 1145021510e9SFariborz Jahanian return; 1146021510e9SFariborz Jahanian } 1147021510e9SFariborz Jahanian } 1148021510e9SFariborz Jahanian } 1149021510e9SFariborz Jahanian 1150acc6b4e2SBenjamin Kramer Builder.CreateMemCpy(DestPtr, SrcPtr, 1151bb2c2400SKen Dyck llvm::ConstantInt::get(IntPtrTy, 1152bb2c2400SKen Dyck TypeInfo.first.getQuantity()), 11536d694a38SEli Friedman Alignment, isVolatile); 11540bc8e86dSDaniel Dunbar } 1155