17a51313dSChris Lattner //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===// 27a51313dSChris Lattner // 37a51313dSChris Lattner // The LLVM Compiler Infrastructure 47a51313dSChris Lattner // 57a51313dSChris Lattner // This file is distributed under the University of Illinois Open Source 67a51313dSChris Lattner // License. See LICENSE.TXT for details. 77a51313dSChris Lattner // 87a51313dSChris Lattner //===----------------------------------------------------------------------===// 97a51313dSChris Lattner // 107a51313dSChris Lattner // This contains code to emit Aggregate Expr nodes as LLVM code. 117a51313dSChris Lattner // 127a51313dSChris Lattner //===----------------------------------------------------------------------===// 137a51313dSChris Lattner 147a51313dSChris Lattner #include "CodeGenFunction.h" 157a51313dSChris Lattner #include "CodeGenModule.h" 165f21d2f6SFariborz Jahanian #include "CGObjCRuntime.h" 17ad319a73SDaniel Dunbar #include "clang/AST/ASTContext.h" 18b7f8f594SAnders Carlsson #include "clang/AST/DeclCXX.h" 19ad319a73SDaniel Dunbar #include "clang/AST/StmtVisitor.h" 207a51313dSChris Lattner #include "llvm/Constants.h" 217a51313dSChris Lattner #include "llvm/Function.h" 227a51313dSChris Lattner #include "llvm/GlobalVariable.h" 23579a05d7SChris Lattner #include "llvm/Intrinsics.h" 247a51313dSChris Lattner using namespace clang; 257a51313dSChris Lattner using namespace CodeGen; 267a51313dSChris Lattner 277a51313dSChris Lattner //===----------------------------------------------------------------------===// 287a51313dSChris Lattner // Aggregate Expression Emitter 297a51313dSChris Lattner //===----------------------------------------------------------------------===// 307a51313dSChris Lattner 317a51313dSChris Lattner namespace { 32337e3a5fSBenjamin Kramer class AggExprEmitter : public StmtVisitor<AggExprEmitter> { 337a51313dSChris Lattner CodeGenFunction &CGF; 34cb463859SDaniel Dunbar CGBuilderTy &Builder; 357a626f63SJohn McCall AggValueSlot Dest; 36ec3cbfe8SMike Stump bool IgnoreResult; 3778a15113SJohn McCall 3878a15113SJohn McCall ReturnValueSlot getReturnValueSlot() const { 39cc04e9f6SJohn McCall // If the destination slot requires garbage collection, we can't 40cc04e9f6SJohn McCall // use the real return value slot, because we have to use the GC 41cc04e9f6SJohn McCall // API. 4258649dc6SJohn McCall if (Dest.requiresGCollection()) return ReturnValueSlot(); 43cc04e9f6SJohn McCall 447a626f63SJohn McCall return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile()); 457a626f63SJohn McCall } 467a626f63SJohn McCall 477a626f63SJohn McCall AggValueSlot EnsureSlot(QualType T) { 487a626f63SJohn McCall if (!Dest.isIgnored()) return Dest; 497a626f63SJohn McCall return CGF.CreateAggTemp(T, "agg.tmp.ensured"); 5078a15113SJohn McCall } 51cc04e9f6SJohn McCall 527a51313dSChris Lattner public: 537a626f63SJohn McCall AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest, 54b60e70f9SFariborz Jahanian bool ignore) 557a626f63SJohn McCall : CGF(cgf), Builder(CGF.Builder), Dest(Dest), 56b60e70f9SFariborz Jahanian IgnoreResult(ignore) { 577a51313dSChris Lattner } 587a51313dSChris Lattner 597a51313dSChris Lattner //===--------------------------------------------------------------------===// 607a51313dSChris Lattner // Utilities 617a51313dSChris Lattner //===--------------------------------------------------------------------===// 627a51313dSChris Lattner 637a51313dSChris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that 647a51313dSChris Lattner /// represents a value lvalue, this method emits the address of the lvalue, 657a51313dSChris Lattner /// then loads the result into DestPtr. 667a51313dSChris Lattner void EmitAggLoadOfLValue(const Expr *E); 677a51313dSChris Lattner 68ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 69ec3cbfe8SMike Stump void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false); 70ec3cbfe8SMike Stump void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false); 71ca9fc09cSMike Stump 72cc04e9f6SJohn McCall void EmitGCMove(const Expr *E, RValue Src); 73cc04e9f6SJohn McCall 74cc04e9f6SJohn McCall bool TypeRequiresGCollection(QualType T); 75cc04e9f6SJohn McCall 767a51313dSChris Lattner //===--------------------------------------------------------------------===// 777a51313dSChris Lattner // Visitor Methods 787a51313dSChris Lattner //===--------------------------------------------------------------------===// 797a51313dSChris Lattner 807a51313dSChris Lattner void VisitStmt(Stmt *S) { 81a7c8cf62SDaniel Dunbar CGF.ErrorUnsupported(S, "aggregate expression"); 827a51313dSChris Lattner } 837a51313dSChris Lattner void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } 8491147596SPeter Collingbourne void VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 8591147596SPeter Collingbourne Visit(GE->getResultExpr()); 8691147596SPeter Collingbourne } 873f66b84cSEli Friedman void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 887a51313dSChris Lattner 897a51313dSChris Lattner // l-values. 907a51313dSChris Lattner void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } 917a51313dSChris Lattner void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 927a51313dSChris Lattner void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 93d443c0a0SDaniel Dunbar void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 942f343dd5SChris Lattner void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 952f343dd5SChris Lattner EmitAggLoadOfLValue(E); 962f343dd5SChris Lattner } 977a51313dSChris Lattner void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 987a51313dSChris Lattner EmitAggLoadOfLValue(E); 997a51313dSChris Lattner } 1002f343dd5SChris Lattner void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 1012f343dd5SChris Lattner EmitAggLoadOfLValue(E); 1022f343dd5SChris Lattner } 1032f343dd5SChris Lattner void VisitPredefinedExpr(const PredefinedExpr *E) { 1042f343dd5SChris Lattner EmitAggLoadOfLValue(E); 1052f343dd5SChris Lattner } 106bc7d67ceSMike Stump 1077a51313dSChris Lattner // Operators. 108ec143777SAnders Carlsson void VisitCastExpr(CastExpr *E); 1097a51313dSChris Lattner void VisitCallExpr(const CallExpr *E); 1107a51313dSChris Lattner void VisitStmtExpr(const StmtExpr *E); 1117a51313dSChris Lattner void VisitBinaryOperator(const BinaryOperator *BO); 112ffba662dSFariborz Jahanian void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); 1137a51313dSChris Lattner void VisitBinAssign(const BinaryOperator *E); 1144b0e2a30SEli Friedman void VisitBinComma(const BinaryOperator *E); 1157a51313dSChris Lattner 116b1d329daSChris Lattner void VisitObjCMessageExpr(ObjCMessageExpr *E); 117c8317a44SDaniel Dunbar void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 118c8317a44SDaniel Dunbar EmitAggLoadOfLValue(E); 119c8317a44SDaniel Dunbar } 12055310df7SDaniel Dunbar void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); 1217a51313dSChris Lattner 122c07a0c7eSJohn McCall void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); 1235b2095ceSAnders Carlsson void VisitChooseExpr(const ChooseExpr *CE); 1247a51313dSChris Lattner void VisitInitListExpr(InitListExpr *E); 12518ada985SAnders Carlsson void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); 126aa9c7aedSChris Lattner void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 127aa9c7aedSChris Lattner Visit(DAE->getExpr()); 128aa9c7aedSChris Lattner } 1293be22e27SAnders Carlsson void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 1301619a504SAnders Carlsson void VisitCXXConstructExpr(const CXXConstructExpr *E); 1315d413781SJohn McCall void VisitExprWithCleanups(ExprWithCleanups *E); 132747eb784SDouglas Gregor void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 1335bbbb137SMike Stump void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } 134c82b86dfSAnders Carlsson 1351bf5846aSJohn McCall void VisitOpaqueValueExpr(OpaqueValueExpr *E); 1361bf5846aSJohn McCall 13721911e89SEli Friedman void VisitVAArgExpr(VAArgExpr *E); 138579a05d7SChris Lattner 139b247350eSAnders Carlsson void EmitInitializationToLValue(Expr *E, LValue Address, QualType T); 140579a05d7SChris Lattner void EmitNullInitializationToLValue(LValue Address, QualType T); 1417a51313dSChris Lattner // case Expr::ChooseExprClass: 142f16b8c30SMike Stump void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } 1437a51313dSChris Lattner }; 1447a51313dSChris Lattner } // end anonymous namespace. 1457a51313dSChris Lattner 1467a51313dSChris Lattner //===----------------------------------------------------------------------===// 1477a51313dSChris Lattner // Utilities 1487a51313dSChris Lattner //===----------------------------------------------------------------------===// 1497a51313dSChris Lattner 1507a51313dSChris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that 1517a51313dSChris Lattner /// represents a value lvalue, this method emits the address of the lvalue, 1527a51313dSChris Lattner /// then loads the result into DestPtr. 1537a51313dSChris Lattner void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 1547a51313dSChris Lattner LValue LV = CGF.EmitLValue(E); 155ca9fc09cSMike Stump EmitFinalDestCopy(E, LV); 156ca9fc09cSMike Stump } 157ca9fc09cSMike Stump 158cc04e9f6SJohn McCall /// \brief True if the given aggregate type requires special GC API calls. 159cc04e9f6SJohn McCall bool AggExprEmitter::TypeRequiresGCollection(QualType T) { 160cc04e9f6SJohn McCall // Only record types have members that might require garbage collection. 161cc04e9f6SJohn McCall const RecordType *RecordTy = T->getAs<RecordType>(); 162cc04e9f6SJohn McCall if (!RecordTy) return false; 163cc04e9f6SJohn McCall 164cc04e9f6SJohn McCall // Don't mess with non-trivial C++ types. 165cc04e9f6SJohn McCall RecordDecl *Record = RecordTy->getDecl(); 166cc04e9f6SJohn McCall if (isa<CXXRecordDecl>(Record) && 167cc04e9f6SJohn McCall (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() || 168cc04e9f6SJohn McCall !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) 169cc04e9f6SJohn McCall return false; 170cc04e9f6SJohn McCall 171cc04e9f6SJohn McCall // Check whether the type has an object member. 172cc04e9f6SJohn McCall return Record->hasObjectMember(); 173cc04e9f6SJohn McCall } 174cc04e9f6SJohn McCall 175cc04e9f6SJohn McCall /// \brief Perform the final move to DestPtr if RequiresGCollection is set. 176cc04e9f6SJohn McCall /// 177cc04e9f6SJohn McCall /// The idea is that you do something like this: 178cc04e9f6SJohn McCall /// RValue Result = EmitSomething(..., getReturnValueSlot()); 179cc04e9f6SJohn McCall /// EmitGCMove(E, Result); 180cc04e9f6SJohn McCall /// If GC doesn't interfere, this will cause the result to be emitted 181cc04e9f6SJohn McCall /// directly into the return value slot. If GC does interfere, a final 182cc04e9f6SJohn McCall /// move will be performed. 183cc04e9f6SJohn McCall void AggExprEmitter::EmitGCMove(const Expr *E, RValue Src) { 18458649dc6SJohn McCall if (Dest.requiresGCollection()) { 1853b4bd9a1SKen Dyck CharUnits size = CGF.getContext().getTypeSizeInChars(E->getType()); 186021510e9SFariborz Jahanian const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 1873b4bd9a1SKen Dyck llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1887a626f63SJohn McCall CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, Dest.getAddr(), 189cc04e9f6SJohn McCall Src.getAggregateAddr(), 190021510e9SFariborz Jahanian SizeVal); 191021510e9SFariborz Jahanian } 192cc04e9f6SJohn McCall } 193cc04e9f6SJohn McCall 194ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 195ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) { 196ca9fc09cSMike Stump assert(Src.isAggregate() && "value must be aggregate value!"); 1977a51313dSChris Lattner 1987a626f63SJohn McCall // If Dest is ignored, then we're evaluating an aggregate expression 1998d752430SJohn McCall // in a context (like an expression statement) that doesn't care 2008d752430SJohn McCall // about the result. C says that an lvalue-to-rvalue conversion is 2018d752430SJohn McCall // performed in these cases; C++ says that it is not. In either 2028d752430SJohn McCall // case, we don't actually need to do anything unless the value is 2038d752430SJohn McCall // volatile. 2047a626f63SJohn McCall if (Dest.isIgnored()) { 2058d752430SJohn McCall if (!Src.isVolatileQualified() || 2068d752430SJohn McCall CGF.CGM.getLangOptions().CPlusPlus || 2078d752430SJohn McCall (IgnoreResult && Ignore)) 208ec3cbfe8SMike Stump return; 209c123623dSFariborz Jahanian 210332ec2ceSMike Stump // If the source is volatile, we must read from it; to do that, we need 211332ec2ceSMike Stump // some place to put it. 2127a626f63SJohn McCall Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp"); 213332ec2ceSMike Stump } 2147a51313dSChris Lattner 21558649dc6SJohn McCall if (Dest.requiresGCollection()) { 2163b4bd9a1SKen Dyck CharUnits size = CGF.getContext().getTypeSizeInChars(E->getType()); 217021510e9SFariborz Jahanian const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); 2183b4bd9a1SKen Dyck llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 219879d7266SFariborz Jahanian CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 2207a626f63SJohn McCall Dest.getAddr(), 2217a626f63SJohn McCall Src.getAggregateAddr(), 222021510e9SFariborz Jahanian SizeVal); 223879d7266SFariborz Jahanian return; 224879d7266SFariborz Jahanian } 225ca9fc09cSMike Stump // If the result of the assignment is used, copy the LHS there also. 226ca9fc09cSMike Stump // FIXME: Pass VolatileDest as well. I think we also need to merge volatile 227ca9fc09cSMike Stump // from the source as well, as we can't eliminate it if either operand 228ca9fc09cSMike Stump // is volatile, unless copy has volatile for both source and destination.. 2297a626f63SJohn McCall CGF.EmitAggregateCopy(Dest.getAddr(), Src.getAggregateAddr(), E->getType(), 2307a626f63SJohn McCall Dest.isVolatile()|Src.isVolatileQualified()); 231ca9fc09cSMike Stump } 232ca9fc09cSMike Stump 233ca9fc09cSMike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 234ec3cbfe8SMike Stump void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { 235ca9fc09cSMike Stump assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); 236ca9fc09cSMike Stump 237ca9fc09cSMike Stump EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(), 238ec3cbfe8SMike Stump Src.isVolatileQualified()), 239ec3cbfe8SMike Stump Ignore); 2407a51313dSChris Lattner } 2417a51313dSChris Lattner 2427a51313dSChris Lattner //===----------------------------------------------------------------------===// 2437a51313dSChris Lattner // Visitor Methods 2447a51313dSChris Lattner //===----------------------------------------------------------------------===// 2457a51313dSChris Lattner 2461bf5846aSJohn McCall void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) { 247c07a0c7eSJohn McCall EmitFinalDestCopy(e, CGF.getOpaqueLValueMapping(e)); 2481bf5846aSJohn McCall } 2491bf5846aSJohn McCall 250ec143777SAnders Carlsson void AggExprEmitter::VisitCastExpr(CastExpr *E) { 2511fb7ae9eSAnders Carlsson switch (E->getCastKind()) { 2528a01a751SAnders Carlsson case CK_Dynamic: { 2531c073f47SDouglas Gregor assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?"); 2541c073f47SDouglas Gregor LValue LV = CGF.EmitCheckedLValue(E->getSubExpr()); 2551c073f47SDouglas Gregor // FIXME: Do we also need to handle property references here? 2561c073f47SDouglas Gregor if (LV.isSimple()) 2571c073f47SDouglas Gregor CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E)); 2581c073f47SDouglas Gregor else 2591c073f47SDouglas Gregor CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast"); 2601c073f47SDouglas Gregor 2617a626f63SJohn McCall if (!Dest.isIgnored()) 2621c073f47SDouglas Gregor CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination"); 2631c073f47SDouglas Gregor break; 2641c073f47SDouglas Gregor } 2651c073f47SDouglas Gregor 266e302792bSJohn McCall case CK_ToUnion: { 26758989b71SJohn McCall if (Dest.isIgnored()) break; 26858989b71SJohn McCall 2697ffcf93bSNuno Lopes // GCC union extension 2702e442a00SDaniel Dunbar QualType Ty = E->getSubExpr()->getType(); 2712e442a00SDaniel Dunbar QualType PtrTy = CGF.getContext().getPointerType(Ty); 2727a626f63SJohn McCall llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(), 273dd274848SEli Friedman CGF.ConvertType(PtrTy)); 2742e442a00SDaniel Dunbar EmitInitializationToLValue(E->getSubExpr(), CGF.MakeAddrLValue(CastPtr, Ty), 2752e442a00SDaniel Dunbar Ty); 2761fb7ae9eSAnders Carlsson break; 2777ffcf93bSNuno Lopes } 2787ffcf93bSNuno Lopes 279e302792bSJohn McCall case CK_DerivedToBase: 280e302792bSJohn McCall case CK_BaseToDerived: 281e302792bSJohn McCall case CK_UncheckedDerivedToBase: { 282aae38d66SDouglas Gregor assert(0 && "cannot perform hierarchy conversion in EmitAggExpr: " 283aae38d66SDouglas Gregor "should have been unpacked before we got here"); 284aae38d66SDouglas Gregor break; 285aae38d66SDouglas Gregor } 286aae38d66SDouglas Gregor 28734376a68SJohn McCall case CK_GetObjCProperty: { 28834376a68SJohn McCall LValue LV = CGF.EmitLValue(E->getSubExpr()); 28934376a68SJohn McCall assert(LV.isPropertyRef()); 29034376a68SJohn McCall RValue RV = CGF.EmitLoadOfPropertyRefLValue(LV, getReturnValueSlot()); 29134376a68SJohn McCall EmitGCMove(E, RV); 29234376a68SJohn McCall break; 29334376a68SJohn McCall } 29434376a68SJohn McCall 29534376a68SJohn McCall case CK_LValueToRValue: // hope for downstream optimization 296e302792bSJohn McCall case CK_NoOp: 297e302792bSJohn McCall case CK_UserDefinedConversion: 298e302792bSJohn McCall case CK_ConstructorConversion: 2992a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 3002a69547fSEli Friedman E->getType()) && 3010f398c44SChris Lattner "Implicit cast types must be compatible"); 3027a51313dSChris Lattner Visit(E->getSubExpr()); 3031fb7ae9eSAnders Carlsson break; 304b05a3e55SAnders Carlsson 305e302792bSJohn McCall case CK_LValueBitCast: 306f3735e01SJohn McCall llvm_unreachable("should not be emitting lvalue bitcast as rvalue"); 30751954276SDouglas Gregor break; 30831996343SJohn McCall 309f3735e01SJohn McCall case CK_Dependent: 310f3735e01SJohn McCall case CK_BitCast: 311f3735e01SJohn McCall case CK_ArrayToPointerDecay: 312f3735e01SJohn McCall case CK_FunctionToPointerDecay: 313f3735e01SJohn McCall case CK_NullToPointer: 314f3735e01SJohn McCall case CK_NullToMemberPointer: 315f3735e01SJohn McCall case CK_BaseToDerivedMemberPointer: 316f3735e01SJohn McCall case CK_DerivedToBaseMemberPointer: 317f3735e01SJohn McCall case CK_MemberPointerToBoolean: 318f3735e01SJohn McCall case CK_IntegralToPointer: 319f3735e01SJohn McCall case CK_PointerToIntegral: 320f3735e01SJohn McCall case CK_PointerToBoolean: 321f3735e01SJohn McCall case CK_ToVoid: 322f3735e01SJohn McCall case CK_VectorSplat: 323f3735e01SJohn McCall case CK_IntegralCast: 324f3735e01SJohn McCall case CK_IntegralToBoolean: 325f3735e01SJohn McCall case CK_IntegralToFloating: 326f3735e01SJohn McCall case CK_FloatingToIntegral: 327f3735e01SJohn McCall case CK_FloatingToBoolean: 328f3735e01SJohn McCall case CK_FloatingCast: 329f3735e01SJohn McCall case CK_AnyPointerToObjCPointerCast: 330f3735e01SJohn McCall case CK_AnyPointerToBlockPointerCast: 331f3735e01SJohn McCall case CK_ObjCObjectLValueCast: 332f3735e01SJohn McCall case CK_FloatingRealToComplex: 333f3735e01SJohn McCall case CK_FloatingComplexToReal: 334f3735e01SJohn McCall case CK_FloatingComplexToBoolean: 335f3735e01SJohn McCall case CK_FloatingComplexCast: 336f3735e01SJohn McCall case CK_FloatingComplexToIntegralComplex: 337f3735e01SJohn McCall case CK_IntegralRealToComplex: 338f3735e01SJohn McCall case CK_IntegralComplexToReal: 339f3735e01SJohn McCall case CK_IntegralComplexToBoolean: 340f3735e01SJohn McCall case CK_IntegralComplexCast: 341f3735e01SJohn McCall case CK_IntegralComplexToFloatingComplex: 342f3735e01SJohn McCall llvm_unreachable("cast kind invalid for aggregate types"); 3431fb7ae9eSAnders Carlsson } 3447a51313dSChris Lattner } 3457a51313dSChris Lattner 3460f398c44SChris Lattner void AggExprEmitter::VisitCallExpr(const CallExpr *E) { 347ddcbfe7bSAnders Carlsson if (E->getCallReturnType()->isReferenceType()) { 348ddcbfe7bSAnders Carlsson EmitAggLoadOfLValue(E); 349ddcbfe7bSAnders Carlsson return; 350ddcbfe7bSAnders Carlsson } 351ddcbfe7bSAnders Carlsson 352cc04e9f6SJohn McCall RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); 353cc04e9f6SJohn McCall EmitGCMove(E, RV); 3547a51313dSChris Lattner } 3550f398c44SChris Lattner 3560f398c44SChris Lattner void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 357cc04e9f6SJohn McCall RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); 358cc04e9f6SJohn McCall EmitGCMove(E, RV); 359b1d329daSChris Lattner } 3607a51313dSChris Lattner 36155310df7SDaniel Dunbar void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 36234376a68SJohn McCall llvm_unreachable("direct property access not surrounded by " 36334376a68SJohn McCall "lvalue-to-rvalue cast"); 36455310df7SDaniel Dunbar } 36555310df7SDaniel Dunbar 3660f398c44SChris Lattner void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 367a2342eb8SJohn McCall CGF.EmitIgnoredExpr(E->getLHS()); 3687a626f63SJohn McCall Visit(E->getRHS()); 3694b0e2a30SEli Friedman } 3704b0e2a30SEli Friedman 3717a51313dSChris Lattner void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 372ce1de617SJohn McCall CodeGenFunction::StmtExprEvaluation eval(CGF); 3737a626f63SJohn McCall CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest); 3747a51313dSChris Lattner } 3757a51313dSChris Lattner 3767a51313dSChris Lattner void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 377e302792bSJohn McCall if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI) 378ffba662dSFariborz Jahanian VisitPointerToDataMemberBinaryOperator(E); 379ffba662dSFariborz Jahanian else 380a7c8cf62SDaniel Dunbar CGF.ErrorUnsupported(E, "aggregate binary expression"); 3817a51313dSChris Lattner } 3827a51313dSChris Lattner 383ffba662dSFariborz Jahanian void AggExprEmitter::VisitPointerToDataMemberBinaryOperator( 384ffba662dSFariborz Jahanian const BinaryOperator *E) { 385ffba662dSFariborz Jahanian LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); 386ffba662dSFariborz Jahanian EmitFinalDestCopy(E, LV); 387ffba662dSFariborz Jahanian } 388ffba662dSFariborz Jahanian 3897a51313dSChris Lattner void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 3907a51313dSChris Lattner // For an assignment to work, the value on the right has 3917a51313dSChris Lattner // to be compatible with the value on the left. 3922a69547fSEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 3932a69547fSEli Friedman E->getRHS()->getType()) 3947a51313dSChris Lattner && "Invalid assignment"); 395d0a30016SJohn McCall 396d0a30016SJohn McCall // FIXME: __block variables need the RHS evaluated first! 3977a51313dSChris Lattner LValue LHS = CGF.EmitLValue(E->getLHS()); 3987a51313dSChris Lattner 3994b8c6db9SDaniel Dunbar // We have to special case property setters, otherwise we must have 4004b8c6db9SDaniel Dunbar // a simple lvalue (no aggregates inside vectors, bitfields). 4014b8c6db9SDaniel Dunbar if (LHS.isPropertyRef()) { 4027a26ba4dSFariborz Jahanian const ObjCPropertyRefExpr *RE = LHS.getPropertyRefExpr(); 4037a26ba4dSFariborz Jahanian QualType ArgType = RE->getSetterArgType(); 4047a26ba4dSFariborz Jahanian RValue Src; 4057a26ba4dSFariborz Jahanian if (ArgType->isReferenceType()) 4067a26ba4dSFariborz Jahanian Src = CGF.EmitReferenceBindingToExpr(E->getRHS(), 0); 4077a26ba4dSFariborz Jahanian else { 4087a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getRHS()->getType()); 4097a626f63SJohn McCall CGF.EmitAggExpr(E->getRHS(), Slot); 4107a26ba4dSFariborz Jahanian Src = Slot.asRValue(); 4117a26ba4dSFariborz Jahanian } 4127a26ba4dSFariborz Jahanian CGF.EmitStoreThroughPropertyRefLValue(Src, LHS); 4134b8c6db9SDaniel Dunbar } else { 414b60e70f9SFariborz Jahanian bool GCollection = false; 415cc04e9f6SJohn McCall if (CGF.getContext().getLangOptions().getGCMode()) 416b60e70f9SFariborz Jahanian GCollection = TypeRequiresGCollection(E->getLHS()->getType()); 417cc04e9f6SJohn McCall 4187a51313dSChris Lattner // Codegen the RHS so that it stores directly into the LHS. 419b60e70f9SFariborz Jahanian AggValueSlot LHSSlot = AggValueSlot::forLValue(LHS, true, 420b60e70f9SFariborz Jahanian GCollection); 421b60e70f9SFariborz Jahanian CGF.EmitAggExpr(E->getRHS(), LHSSlot, false); 422ec3cbfe8SMike Stump EmitFinalDestCopy(E, LHS, true); 4237a51313dSChris Lattner } 4244b8c6db9SDaniel Dunbar } 4257a51313dSChris Lattner 426c07a0c7eSJohn McCall void AggExprEmitter:: 427c07a0c7eSJohn McCall VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 428a612e79bSDaniel Dunbar llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 429a612e79bSDaniel Dunbar llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 430a612e79bSDaniel Dunbar llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 4317a51313dSChris Lattner 432c07a0c7eSJohn McCall // Bind the common expression if necessary. 433c07a0c7eSJohn McCall CodeGenFunction::OpaqueValueMapping binding(CGF, E); 434c07a0c7eSJohn McCall 435ce1de617SJohn McCall CodeGenFunction::ConditionalEvaluation eval(CGF); 436b8841af8SEli Friedman CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 4377a51313dSChris Lattner 4385b26f65bSJohn McCall // Save whether the destination's lifetime is externally managed. 4395b26f65bSJohn McCall bool DestLifetimeManaged = Dest.isLifetimeExternallyManaged(); 4407a51313dSChris Lattner 441ce1de617SJohn McCall eval.begin(CGF); 442ce1de617SJohn McCall CGF.EmitBlock(LHSBlock); 443c07a0c7eSJohn McCall Visit(E->getTrueExpr()); 444ce1de617SJohn McCall eval.end(CGF); 4457a51313dSChris Lattner 446ce1de617SJohn McCall assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!"); 447ce1de617SJohn McCall CGF.Builder.CreateBr(ContBlock); 4487a51313dSChris Lattner 4495b26f65bSJohn McCall // If the result of an agg expression is unused, then the emission 4505b26f65bSJohn McCall // of the LHS might need to create a destination slot. That's fine 4515b26f65bSJohn McCall // with us, and we can safely emit the RHS into the same slot, but 4525b26f65bSJohn McCall // we shouldn't claim that its lifetime is externally managed. 4535b26f65bSJohn McCall Dest.setLifetimeExternallyManaged(DestLifetimeManaged); 4545b26f65bSJohn McCall 455ce1de617SJohn McCall eval.begin(CGF); 456ce1de617SJohn McCall CGF.EmitBlock(RHSBlock); 457c07a0c7eSJohn McCall Visit(E->getFalseExpr()); 458ce1de617SJohn McCall eval.end(CGF); 4597a51313dSChris Lattner 4607a51313dSChris Lattner CGF.EmitBlock(ContBlock); 4617a51313dSChris Lattner } 4627a51313dSChris Lattner 4635b2095ceSAnders Carlsson void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 4645b2095ceSAnders Carlsson Visit(CE->getChosenSubExpr(CGF.getContext())); 4655b2095ceSAnders Carlsson } 4665b2095ceSAnders Carlsson 46721911e89SEli Friedman void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 468e9fcadd2SDaniel Dunbar llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 46913abd7e9SAnders Carlsson llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 47013abd7e9SAnders Carlsson 471020cddcfSSebastian Redl if (!ArgPtr) { 47213abd7e9SAnders Carlsson CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); 473020cddcfSSebastian Redl return; 474020cddcfSSebastian Redl } 47513abd7e9SAnders Carlsson 4762e442a00SDaniel Dunbar EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType())); 47721911e89SEli Friedman } 47821911e89SEli Friedman 4793be22e27SAnders Carlsson void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 4807a626f63SJohn McCall // Ensure that we have a slot, but if we already do, remember 4817a626f63SJohn McCall // whether its lifetime was externally managed. 4827a626f63SJohn McCall bool WasManaged = Dest.isLifetimeExternallyManaged(); 4837a626f63SJohn McCall Dest = EnsureSlot(E->getType()); 4847a626f63SJohn McCall Dest.setLifetimeExternallyManaged(); 4853be22e27SAnders Carlsson 4863be22e27SAnders Carlsson Visit(E->getSubExpr()); 4873be22e27SAnders Carlsson 4887a626f63SJohn McCall // Set up the temporary's destructor if its lifetime wasn't already 4897a626f63SJohn McCall // being managed. 4907a626f63SJohn McCall if (!WasManaged) 4917a626f63SJohn McCall CGF.EmitCXXTemporary(E->getTemporary(), Dest.getAddr()); 4923be22e27SAnders Carlsson } 4933be22e27SAnders Carlsson 494b7f8f594SAnders Carlsson void 4951619a504SAnders Carlsson AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 4967a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(E->getType()); 4977a626f63SJohn McCall CGF.EmitCXXConstructExpr(E, Slot); 498c82b86dfSAnders Carlsson } 499c82b86dfSAnders Carlsson 5005d413781SJohn McCall void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) { 5015d413781SJohn McCall CGF.EmitExprWithCleanups(E, Dest); 502b7f8f594SAnders Carlsson } 503b7f8f594SAnders Carlsson 504747eb784SDouglas Gregor void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 5057a626f63SJohn McCall QualType T = E->getType(); 5067a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 5077a626f63SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T); 50818ada985SAnders Carlsson } 50918ada985SAnders Carlsson 51018ada985SAnders Carlsson void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 5117a626f63SJohn McCall QualType T = E->getType(); 5127a626f63SJohn McCall AggValueSlot Slot = EnsureSlot(T); 5137a626f63SJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T); 514ff3507b9SNuno Lopes } 515ff3507b9SNuno Lopes 51627a3631bSChris Lattner /// isSimpleZero - If emitting this value will obviously just cause a store of 51727a3631bSChris Lattner /// zero to memory, return true. This can return false if uncertain, so it just 51827a3631bSChris Lattner /// handles simple cases. 51927a3631bSChris Lattner static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { 52091147596SPeter Collingbourne E = E->IgnoreParens(); 52191147596SPeter Collingbourne 52227a3631bSChris Lattner // 0 52327a3631bSChris Lattner if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) 52427a3631bSChris Lattner return IL->getValue() == 0; 52527a3631bSChris Lattner // +0.0 52627a3631bSChris Lattner if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E)) 52727a3631bSChris Lattner return FL->getValue().isPosZero(); 52827a3631bSChris Lattner // int() 52927a3631bSChris Lattner if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) && 53027a3631bSChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 53127a3631bSChris Lattner return true; 53227a3631bSChris Lattner // (int*)0 - Null pointer expressions. 53327a3631bSChris Lattner if (const CastExpr *ICE = dyn_cast<CastExpr>(E)) 53427a3631bSChris Lattner return ICE->getCastKind() == CK_NullToPointer; 53527a3631bSChris Lattner // '\0' 53627a3631bSChris Lattner if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) 53727a3631bSChris Lattner return CL->getValue() == 0; 53827a3631bSChris Lattner 53927a3631bSChris Lattner // Otherwise, hard case: conservatively return false. 54027a3631bSChris Lattner return false; 54127a3631bSChris Lattner } 54227a3631bSChris Lattner 54327a3631bSChris Lattner 544b247350eSAnders Carlsson void 545b247350eSAnders Carlsson AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) { 546df0fe27bSMike Stump // FIXME: Ignore result? 547579a05d7SChris Lattner // FIXME: Are initializers affected by volatile? 54827a3631bSChris Lattner if (Dest.isZeroed() && isSimpleZero(E, CGF)) { 54927a3631bSChris Lattner // Storing "i32 0" to a zero'd memory location is a noop. 55027a3631bSChris Lattner } else if (isa<ImplicitValueInitExpr>(E)) { 551b247350eSAnders Carlsson EmitNullInitializationToLValue(LV, T); 55266498388SAnders Carlsson } else if (T->isReferenceType()) { 55304775f84SAnders Carlsson RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); 55466498388SAnders Carlsson CGF.EmitStoreThroughLValue(RV, LV, T); 555b247350eSAnders Carlsson } else if (T->isAnyComplexType()) { 5560202cb40SDouglas Gregor CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); 557b247350eSAnders Carlsson } else if (CGF.hasAggregateLLVMType(T)) { 55827a3631bSChris Lattner CGF.EmitAggExpr(E, AggValueSlot::forAddr(LV.getAddress(), false, true, 55927a3631bSChris Lattner false, Dest.isZeroed())); 5606e313210SEli Friedman } else { 561a2342eb8SJohn McCall CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV, T); 5627a51313dSChris Lattner } 563579a05d7SChris Lattner } 564579a05d7SChris Lattner 565579a05d7SChris Lattner void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { 56627a3631bSChris Lattner // If the destination slot is already zeroed out before the aggregate is 56727a3631bSChris Lattner // copied into it, we don't have to emit any zeros here. 56827a3631bSChris Lattner if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(T)) 56927a3631bSChris Lattner return; 57027a3631bSChris Lattner 571579a05d7SChris Lattner if (!CGF.hasAggregateLLVMType(T)) { 572579a05d7SChris Lattner // For non-aggregates, we can store zero 5730b75f23bSOwen Anderson llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T)); 574e8bdce44SDaniel Dunbar CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T); 575579a05d7SChris Lattner } else { 576579a05d7SChris Lattner // There's a potential optimization opportunity in combining 577579a05d7SChris Lattner // memsets; that would be easy for arrays, but relatively 578579a05d7SChris Lattner // difficult for structures with the current code. 579c0964b60SAnders Carlsson CGF.EmitNullInitialization(LV.getAddress(), T); 580579a05d7SChris Lattner } 581579a05d7SChris Lattner } 582579a05d7SChris Lattner 583579a05d7SChris Lattner void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 584f5d08c9eSEli Friedman #if 0 5856d11ec8cSEli Friedman // FIXME: Assess perf here? Figure out what cases are worth optimizing here 5866d11ec8cSEli Friedman // (Length of globals? Chunks of zeroed-out space?). 587f5d08c9eSEli Friedman // 58818bb9284SMike Stump // If we can, prefer a copy from a global; this is a lot less code for long 58918bb9284SMike Stump // globals, and it's easier for the current optimizers to analyze. 5906d11ec8cSEli Friedman if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { 591c59bb48eSEli Friedman llvm::GlobalVariable* GV = 5926d11ec8cSEli Friedman new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, 5936d11ec8cSEli Friedman llvm::GlobalValue::InternalLinkage, C, ""); 5942e442a00SDaniel Dunbar EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType())); 595c59bb48eSEli Friedman return; 596c59bb48eSEli Friedman } 597f5d08c9eSEli Friedman #endif 598f53c0968SChris Lattner if (E->hadArrayRangeDesignator()) 599bf7207a1SDouglas Gregor CGF.ErrorUnsupported(E, "GNU array range designator extension"); 600bf7207a1SDouglas Gregor 6017a626f63SJohn McCall llvm::Value *DestPtr = Dest.getAddr(); 6027a626f63SJohn McCall 603579a05d7SChris Lattner // Handle initialization of an array. 604579a05d7SChris Lattner if (E->getType()->isArrayType()) { 605579a05d7SChris Lattner const llvm::PointerType *APType = 606579a05d7SChris Lattner cast<llvm::PointerType>(DestPtr->getType()); 607579a05d7SChris Lattner const llvm::ArrayType *AType = 608579a05d7SChris Lattner cast<llvm::ArrayType>(APType->getElementType()); 609579a05d7SChris Lattner 610579a05d7SChris Lattner uint64_t NumInitElements = E->getNumInits(); 611f23b6fa4SEli Friedman 6120f398c44SChris Lattner if (E->getNumInits() > 0) { 6130f398c44SChris Lattner QualType T1 = E->getType(); 6140f398c44SChris Lattner QualType T2 = E->getInit(0)->getType(); 6152a69547fSEli Friedman if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { 616f23b6fa4SEli Friedman EmitAggLoadOfLValue(E->getInit(0)); 617f23b6fa4SEli Friedman return; 618f23b6fa4SEli Friedman } 6190f398c44SChris Lattner } 620f23b6fa4SEli Friedman 621579a05d7SChris Lattner uint64_t NumArrayElements = AType->getNumElements(); 6227adf0760SChris Lattner QualType ElementType = CGF.getContext().getCanonicalType(E->getType()); 6237adf0760SChris Lattner ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType(); 624579a05d7SChris Lattner 6258ccfcb51SJohn McCall // FIXME: were we intentionally ignoring address spaces and GC attributes? 626327944b3SEli Friedman 627579a05d7SChris Lattner for (uint64_t i = 0; i != NumArrayElements; ++i) { 62827a3631bSChris Lattner // If we're done emitting initializers and the destination is known-zeroed 62927a3631bSChris Lattner // then we're done. 63027a3631bSChris Lattner if (i == NumInitElements && 63127a3631bSChris Lattner Dest.isZeroed() && 63227a3631bSChris Lattner CGF.getTypes().isZeroInitializable(ElementType)) 63327a3631bSChris Lattner break; 63427a3631bSChris Lattner 635579a05d7SChris Lattner llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); 636f6fb7e2bSDaniel Dunbar LValue LV = CGF.MakeAddrLValue(NextVal, ElementType); 63727a3631bSChris Lattner 638579a05d7SChris Lattner if (i < NumInitElements) 639f6fb7e2bSDaniel Dunbar EmitInitializationToLValue(E->getInit(i), LV, ElementType); 640b2ed28eaSArgyrios Kyrtzidis else if (Expr *filler = E->getArrayFiller()) 641b2ed28eaSArgyrios Kyrtzidis EmitInitializationToLValue(filler, LV, ElementType); 642579a05d7SChris Lattner else 643f6fb7e2bSDaniel Dunbar EmitNullInitializationToLValue(LV, ElementType); 64427a3631bSChris Lattner 64527a3631bSChris Lattner // If the GEP didn't get used because of a dead zero init or something 64627a3631bSChris Lattner // else, clean it up for -O0 builds and general tidiness. 64727a3631bSChris Lattner if (llvm::GetElementPtrInst *GEP = 64827a3631bSChris Lattner dyn_cast<llvm::GetElementPtrInst>(NextVal)) 64927a3631bSChris Lattner if (GEP->use_empty()) 65027a3631bSChris Lattner GEP->eraseFromParent(); 651579a05d7SChris Lattner } 652579a05d7SChris Lattner return; 653579a05d7SChris Lattner } 654579a05d7SChris Lattner 655579a05d7SChris Lattner assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 656579a05d7SChris Lattner 657579a05d7SChris Lattner // Do struct initialization; this code just sets each individual member 658579a05d7SChris Lattner // to the approprate value. This makes bitfield support automatic; 659579a05d7SChris Lattner // the disadvantage is that the generated code is more difficult for 660579a05d7SChris Lattner // the optimizer, especially with bitfields. 661579a05d7SChris Lattner unsigned NumInitElements = E->getNumInits(); 662c23c7e6aSTed Kremenek RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 66352bcf963SChris Lattner 6645169570eSDouglas Gregor if (E->getType()->isUnionType()) { 6655169570eSDouglas Gregor // Only initialize one field of a union. The field itself is 6665169570eSDouglas Gregor // specified by the initializer list. 6675169570eSDouglas Gregor if (!E->getInitializedFieldInUnion()) { 6685169570eSDouglas Gregor // Empty union; we have nothing to do. 6695169570eSDouglas Gregor 6705169570eSDouglas Gregor #ifndef NDEBUG 6715169570eSDouglas Gregor // Make sure that it's really an empty and not a failure of 6725169570eSDouglas Gregor // semantic analysis. 673cfbfe78eSArgyrios Kyrtzidis for (RecordDecl::field_iterator Field = SD->field_begin(), 674cfbfe78eSArgyrios Kyrtzidis FieldEnd = SD->field_end(); 6755169570eSDouglas Gregor Field != FieldEnd; ++Field) 6765169570eSDouglas Gregor assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 6775169570eSDouglas Gregor #endif 6785169570eSDouglas Gregor return; 6795169570eSDouglas Gregor } 6805169570eSDouglas Gregor 6815169570eSDouglas Gregor // FIXME: volatility 6825169570eSDouglas Gregor FieldDecl *Field = E->getInitializedFieldInUnion(); 6835169570eSDouglas Gregor 68427a3631bSChris Lattner LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0); 6855169570eSDouglas Gregor if (NumInitElements) { 6865169570eSDouglas Gregor // Store the initializer into the field 687b247350eSAnders Carlsson EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType()); 6885169570eSDouglas Gregor } else { 68927a3631bSChris Lattner // Default-initialize to null. 6905169570eSDouglas Gregor EmitNullInitializationToLValue(FieldLoc, Field->getType()); 6915169570eSDouglas Gregor } 6925169570eSDouglas Gregor 6935169570eSDouglas Gregor return; 6945169570eSDouglas Gregor } 695579a05d7SChris Lattner 696579a05d7SChris Lattner // Here we iterate over the fields; this makes it simpler to both 697579a05d7SChris Lattner // default-initialize fields and skip over unnamed fields. 69852bcf963SChris Lattner unsigned CurInitVal = 0; 699cfbfe78eSArgyrios Kyrtzidis for (RecordDecl::field_iterator Field = SD->field_begin(), 700cfbfe78eSArgyrios Kyrtzidis FieldEnd = SD->field_end(); 70191f84216SDouglas Gregor Field != FieldEnd; ++Field) { 70291f84216SDouglas Gregor // We're done once we hit the flexible array member 70391f84216SDouglas Gregor if (Field->getType()->isIncompleteArrayType()) 70491f84216SDouglas Gregor break; 70591f84216SDouglas Gregor 70617bd094aSDouglas Gregor if (Field->isUnnamedBitfield()) 707579a05d7SChris Lattner continue; 70817bd094aSDouglas Gregor 70927a3631bSChris Lattner // Don't emit GEP before a noop store of zero. 71027a3631bSChris Lattner if (CurInitVal == NumInitElements && Dest.isZeroed() && 71127a3631bSChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 71227a3631bSChris Lattner break; 71327a3631bSChris Lattner 714327944b3SEli Friedman // FIXME: volatility 71566498388SAnders Carlsson LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0); 7167c1baf46SFariborz Jahanian // We never generate write-barries for initialized fields. 717e50dda95SDaniel Dunbar FieldLoc.setNonGC(true); 71827a3631bSChris Lattner 719579a05d7SChris Lattner if (CurInitVal < NumInitElements) { 720e18aaf2cSChris Lattner // Store the initializer into the field. 721b247350eSAnders Carlsson EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc, 722b247350eSAnders Carlsson Field->getType()); 723579a05d7SChris Lattner } else { 724579a05d7SChris Lattner // We're out of initalizers; default-initialize to null 72591f84216SDouglas Gregor EmitNullInitializationToLValue(FieldLoc, Field->getType()); 726579a05d7SChris Lattner } 72727a3631bSChris Lattner 72827a3631bSChris Lattner // If the GEP didn't get used because of a dead zero init or something 72927a3631bSChris Lattner // else, clean it up for -O0 builds and general tidiness. 73027a3631bSChris Lattner if (FieldLoc.isSimple()) 73127a3631bSChris Lattner if (llvm::GetElementPtrInst *GEP = 73227a3631bSChris Lattner dyn_cast<llvm::GetElementPtrInst>(FieldLoc.getAddress())) 73327a3631bSChris Lattner if (GEP->use_empty()) 73427a3631bSChris Lattner GEP->eraseFromParent(); 7357a51313dSChris Lattner } 7367a51313dSChris Lattner } 7377a51313dSChris Lattner 7387a51313dSChris Lattner //===----------------------------------------------------------------------===// 7397a51313dSChris Lattner // Entry Points into this File 7407a51313dSChris Lattner //===----------------------------------------------------------------------===// 7417a51313dSChris Lattner 74227a3631bSChris Lattner /// GetNumNonZeroBytesInInit - Get an approximate count of the number of 74327a3631bSChris Lattner /// non-zero bytes that will be stored when outputting the initializer for the 74427a3631bSChris Lattner /// specified initializer expression. 745df94cb7dSKen Dyck static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) { 74691147596SPeter Collingbourne E = E->IgnoreParens(); 74727a3631bSChris Lattner 74827a3631bSChris Lattner // 0 and 0.0 won't require any non-zero stores! 749df94cb7dSKen Dyck if (isSimpleZero(E, CGF)) return CharUnits::Zero(); 75027a3631bSChris Lattner 75127a3631bSChris Lattner // If this is an initlist expr, sum up the size of sizes of the (present) 75227a3631bSChris Lattner // elements. If this is something weird, assume the whole thing is non-zero. 75327a3631bSChris Lattner const InitListExpr *ILE = dyn_cast<InitListExpr>(E); 75427a3631bSChris Lattner if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType())) 755df94cb7dSKen Dyck return CGF.getContext().getTypeSizeInChars(E->getType()); 75627a3631bSChris Lattner 757c5cc2fb9SChris Lattner // InitListExprs for structs have to be handled carefully. If there are 758c5cc2fb9SChris Lattner // reference members, we need to consider the size of the reference, not the 759c5cc2fb9SChris Lattner // referencee. InitListExprs for unions and arrays can't have references. 7605cd84755SChris Lattner if (const RecordType *RT = E->getType()->getAs<RecordType>()) { 7615cd84755SChris Lattner if (!RT->isUnionType()) { 762c5cc2fb9SChris Lattner RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 763df94cb7dSKen Dyck CharUnits NumNonZeroBytes = CharUnits::Zero(); 764c5cc2fb9SChris Lattner 765c5cc2fb9SChris Lattner unsigned ILEElement = 0; 766c5cc2fb9SChris Lattner for (RecordDecl::field_iterator Field = SD->field_begin(), 767c5cc2fb9SChris Lattner FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) { 768c5cc2fb9SChris Lattner // We're done once we hit the flexible array member or run out of 769c5cc2fb9SChris Lattner // InitListExpr elements. 770c5cc2fb9SChris Lattner if (Field->getType()->isIncompleteArrayType() || 771c5cc2fb9SChris Lattner ILEElement == ILE->getNumInits()) 772c5cc2fb9SChris Lattner break; 773c5cc2fb9SChris Lattner if (Field->isUnnamedBitfield()) 774c5cc2fb9SChris Lattner continue; 775c5cc2fb9SChris Lattner 776c5cc2fb9SChris Lattner const Expr *E = ILE->getInit(ILEElement++); 777c5cc2fb9SChris Lattner 778c5cc2fb9SChris Lattner // Reference values are always non-null and have the width of a pointer. 7795cd84755SChris Lattner if (Field->getType()->isReferenceType()) 780df94cb7dSKen Dyck NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits( 781df94cb7dSKen Dyck CGF.getContext().Target.getPointerWidth(0)); 7825cd84755SChris Lattner else 783c5cc2fb9SChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF); 784c5cc2fb9SChris Lattner } 785c5cc2fb9SChris Lattner 786c5cc2fb9SChris Lattner return NumNonZeroBytes; 787c5cc2fb9SChris Lattner } 7885cd84755SChris Lattner } 789c5cc2fb9SChris Lattner 790c5cc2fb9SChris Lattner 791df94cb7dSKen Dyck CharUnits NumNonZeroBytes = CharUnits::Zero(); 79227a3631bSChris Lattner for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) 79327a3631bSChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF); 79427a3631bSChris Lattner return NumNonZeroBytes; 79527a3631bSChris Lattner } 79627a3631bSChris Lattner 79727a3631bSChris Lattner /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of 79827a3631bSChris Lattner /// zeros in it, emit a memset and avoid storing the individual zeros. 79927a3631bSChris Lattner /// 80027a3631bSChris Lattner static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E, 80127a3631bSChris Lattner CodeGenFunction &CGF) { 80227a3631bSChris Lattner // If the slot is already known to be zeroed, nothing to do. Don't mess with 80327a3631bSChris Lattner // volatile stores. 80427a3631bSChris Lattner if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return; 80527a3631bSChris Lattner 80627a3631bSChris Lattner // If the type is 16-bytes or smaller, prefer individual stores over memset. 807239a3357SKen Dyck std::pair<CharUnits, CharUnits> TypeInfo = 808239a3357SKen Dyck CGF.getContext().getTypeInfoInChars(E->getType()); 809239a3357SKen Dyck if (TypeInfo.first <= CharUnits::fromQuantity(16)) 81027a3631bSChris Lattner return; 81127a3631bSChris Lattner 81227a3631bSChris Lattner // Check to see if over 3/4 of the initializer are known to be zero. If so, 81327a3631bSChris Lattner // we prefer to emit memset + individual stores for the rest. 814239a3357SKen Dyck CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF); 815239a3357SKen Dyck if (NumNonZeroBytes*4 > TypeInfo.first) 81627a3631bSChris Lattner return; 81727a3631bSChris Lattner 81827a3631bSChris Lattner // Okay, it seems like a good idea to use an initial memset, emit the call. 819239a3357SKen Dyck llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity()); 820239a3357SKen Dyck CharUnits Align = TypeInfo.second; 82127a3631bSChris Lattner 82227a3631bSChris Lattner llvm::Value *Loc = Slot.getAddr(); 82327a3631bSChris Lattner const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 82427a3631bSChris Lattner 82527a3631bSChris Lattner Loc = CGF.Builder.CreateBitCast(Loc, BP); 826239a3357SKen Dyck CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, 827239a3357SKen Dyck Align.getQuantity(), false); 82827a3631bSChris Lattner 82927a3631bSChris Lattner // Tell the AggExprEmitter that the slot is known zero. 83027a3631bSChris Lattner Slot.setZeroed(); 83127a3631bSChris Lattner } 83227a3631bSChris Lattner 83327a3631bSChris Lattner 83427a3631bSChris Lattner 83527a3631bSChris Lattner 83625306cacSMike Stump /// EmitAggExpr - Emit the computation of the specified expression of aggregate 83725306cacSMike Stump /// type. The result is computed into DestPtr. Note that if DestPtr is null, 83825306cacSMike Stump /// the value of the aggregate expression is not needed. If VolatileDest is 83925306cacSMike Stump /// true, DestPtr cannot be 0. 8407a626f63SJohn McCall /// 8417a626f63SJohn McCall /// \param IsInitializer - true if this evaluation is initializing an 8427a626f63SJohn McCall /// object whose lifetime is already being managed. 843d0bc7b9dSDaniel Dunbar // 844d0bc7b9dSDaniel Dunbar // FIXME: Take Qualifiers object. 8457a626f63SJohn McCall void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot, 846b60e70f9SFariborz Jahanian bool IgnoreResult) { 8477a51313dSChris Lattner assert(E && hasAggregateLLVMType(E->getType()) && 8487a51313dSChris Lattner "Invalid aggregate expression to emit"); 84927a3631bSChris Lattner assert((Slot.getAddr() != 0 || Slot.isIgnored()) && 85027a3631bSChris Lattner "slot has bits but no address"); 8517a51313dSChris Lattner 85227a3631bSChris Lattner // Optimize the slot if possible. 85327a3631bSChris Lattner CheckAggExprForMemSetUse(Slot, E, *this); 85427a3631bSChris Lattner 85527a3631bSChris Lattner AggExprEmitter(*this, Slot, IgnoreResult).Visit(const_cast<Expr*>(E)); 8567a51313dSChris Lattner } 8570bc8e86dSDaniel Dunbar 858d0bc7b9dSDaniel Dunbar LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { 859d0bc7b9dSDaniel Dunbar assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!"); 860a7566f16SDaniel Dunbar llvm::Value *Temp = CreateMemTemp(E->getType()); 8612e442a00SDaniel Dunbar LValue LV = MakeAddrLValue(Temp, E->getType()); 86227a3631bSChris Lattner EmitAggExpr(E, AggValueSlot::forAddr(Temp, LV.isVolatileQualified(), false)); 8632e442a00SDaniel Dunbar return LV; 864d0bc7b9dSDaniel Dunbar } 865d0bc7b9dSDaniel Dunbar 8660bc8e86dSDaniel Dunbar void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 8675e9e61b8SMike Stump llvm::Value *SrcPtr, QualType Ty, 8685e9e61b8SMike Stump bool isVolatile) { 8690bc8e86dSDaniel Dunbar assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 8700bc8e86dSDaniel Dunbar 87116e94af6SAnders Carlsson if (getContext().getLangOptions().CPlusPlus) { 87216e94af6SAnders Carlsson if (const RecordType *RT = Ty->getAs<RecordType>()) { 873f22101a0SDouglas Gregor CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 874f22101a0SDouglas Gregor assert((Record->hasTrivialCopyConstructor() || 8756855ba2cSFariborz Jahanian Record->hasTrivialCopyAssignment()) && 876f22101a0SDouglas Gregor "Trying to aggregate-copy a type without a trivial copy " 877f22101a0SDouglas Gregor "constructor or assignment operator"); 878265b8b8dSDouglas Gregor // Ignore empty classes in C++. 879f22101a0SDouglas Gregor if (Record->isEmpty()) 88016e94af6SAnders Carlsson return; 88116e94af6SAnders Carlsson } 88216e94af6SAnders Carlsson } 88316e94af6SAnders Carlsson 884ca05dfefSChris Lattner // Aggregate assignment turns into llvm.memcpy. This is almost valid per 8853ef668c2SChris Lattner // C99 6.5.16.1p3, which states "If the value being stored in an object is 8863ef668c2SChris Lattner // read from another object that overlaps in anyway the storage of the first 8873ef668c2SChris Lattner // object, then the overlap shall be exact and the two objects shall have 8883ef668c2SChris Lattner // qualified or unqualified versions of a compatible type." 8893ef668c2SChris Lattner // 890ca05dfefSChris Lattner // memcpy is not defined if the source and destination pointers are exactly 8913ef668c2SChris Lattner // equal, but other compilers do this optimization, and almost every memcpy 8923ef668c2SChris Lattner // implementation handles this case safely. If there is a libc that does not 8933ef668c2SChris Lattner // safely handle this, we can add a target hook. 8940bc8e86dSDaniel Dunbar 8950bc8e86dSDaniel Dunbar // Get size and alignment info for this aggregate. 896*bb2c2400SKen Dyck std::pair<CharUnits, CharUnits> TypeInfo = 897*bb2c2400SKen Dyck getContext().getTypeInfoInChars(Ty); 8980bc8e86dSDaniel Dunbar 8990bc8e86dSDaniel Dunbar // FIXME: Handle variable sized types. 9000bc8e86dSDaniel Dunbar 90186736572SMike Stump // FIXME: If we have a volatile struct, the optimizer can remove what might 90286736572SMike Stump // appear to be `extra' memory ops: 90386736572SMike Stump // 90486736572SMike Stump // volatile struct { int i; } a, b; 90586736572SMike Stump // 90686736572SMike Stump // int main() { 90786736572SMike Stump // a = b; 90886736572SMike Stump // a = b; 90986736572SMike Stump // } 91086736572SMike Stump // 911cc2ab0cdSMon P Wang // we need to use a different call here. We use isVolatile to indicate when 912ec3cbfe8SMike Stump // either the source or the destination is volatile. 913cc2ab0cdSMon P Wang 914cc2ab0cdSMon P Wang const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); 915cb7696cfSChris Lattner const llvm::Type *DBP = 916ad7c5c16SJohn McCall llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace()); 917cc2ab0cdSMon P Wang DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp"); 918cc2ab0cdSMon P Wang 919cc2ab0cdSMon P Wang const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); 920cb7696cfSChris Lattner const llvm::Type *SBP = 921ad7c5c16SJohn McCall llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace()); 922cc2ab0cdSMon P Wang SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp"); 923cc2ab0cdSMon P Wang 924021510e9SFariborz Jahanian if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { 925021510e9SFariborz Jahanian RecordDecl *Record = RecordTy->getDecl(); 926021510e9SFariborz Jahanian if (Record->hasObjectMember()) { 927*bb2c2400SKen Dyck CharUnits size = TypeInfo.first; 928021510e9SFariborz Jahanian const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 929*bb2c2400SKen Dyck llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 930021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 931021510e9SFariborz Jahanian SizeVal); 932021510e9SFariborz Jahanian return; 933021510e9SFariborz Jahanian } 934021510e9SFariborz Jahanian } else if (getContext().getAsArrayType(Ty)) { 935021510e9SFariborz Jahanian QualType BaseType = getContext().getBaseElementType(Ty); 936021510e9SFariborz Jahanian if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { 937021510e9SFariborz Jahanian if (RecordTy->getDecl()->hasObjectMember()) { 938*bb2c2400SKen Dyck CharUnits size = TypeInfo.first; 939021510e9SFariborz Jahanian const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 940*bb2c2400SKen Dyck llvm::Value *SizeVal = 941*bb2c2400SKen Dyck llvm::ConstantInt::get(SizeTy, size.getQuantity()); 942021510e9SFariborz Jahanian CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 943021510e9SFariborz Jahanian SizeVal); 944021510e9SFariborz Jahanian return; 945021510e9SFariborz Jahanian } 946021510e9SFariborz Jahanian } 947021510e9SFariborz Jahanian } 948021510e9SFariborz Jahanian 949acc6b4e2SBenjamin Kramer Builder.CreateMemCpy(DestPtr, SrcPtr, 950*bb2c2400SKen Dyck llvm::ConstantInt::get(IntPtrTy, 951*bb2c2400SKen Dyck TypeInfo.first.getQuantity()), 952*bb2c2400SKen Dyck TypeInfo.second.getQuantity(), isVolatile); 9530bc8e86dSDaniel Dunbar } 954