1566b6ce741c742cc3f8cb85e2376ec4a3490ff5fChris Lattner//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===// 2af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner// 3af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner// The LLVM Compiler Infrastructure 4af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 7af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner// 8af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner//===----------------------------------------------------------------------===// 9af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner// 10af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner// This contains code to emit Aggregate Expr nodes as LLVM code. 11af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner// 12af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner//===----------------------------------------------------------------------===// 13af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner 14af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattner#include "CodeGenFunction.h" 15082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian#include "CGObjCRuntime.h" 1655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CodeGenModule.h" 17de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h" 18b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson#include "clang/AST/DeclCXX.h" 1932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl#include "clang/AST/DeclTemplate.h" 20de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/StmtVisitor.h" 213b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Constants.h" 223b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Function.h" 233b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/GlobalVariable.h" 243b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h" 25af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattnerusing namespace clang; 26af6f528b2bd6c3ee517e02d346238addb74159ccChris Lattnerusing namespace CodeGen; 27883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner 289c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner//===----------------------------------------------------------------------===// 299c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner// Aggregate Expression Emitter 309c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner//===----------------------------------------------------------------------===// 319c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner 329c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattnernamespace { 3385b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramerclass AggExprEmitter : public StmtVisitor<AggExprEmitter> { 349c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner CodeGenFunction &CGF; 3545d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbar CGBuilderTy &Builder; 36558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall AggValueSlot Dest; 37ef072fd2f3347cfd857d6eb787b245b950771430John McCall 38410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall /// We want to use 'dest' as the return slot except under two 39410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall /// conditions: 40410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall /// - The destination slot requires garbage collection, so we 41410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall /// need to use the GC API. 42410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall /// - The destination slot is potentially aliased. 43410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall bool shouldUseDestForReturnSlot() const { 44410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall return !(Dest.requiresGCollection() || Dest.isPotentiallyAliased()); 45410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall } 46410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall 47ef072fd2f3347cfd857d6eb787b245b950771430John McCall ReturnValueSlot getReturnValueSlot() const { 48410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall if (!shouldUseDestForReturnSlot()) 49410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall return ReturnValueSlot(); 50fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall 51558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile()); 52558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall } 53558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall 54558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall AggValueSlot EnsureSlot(QualType T) { 55558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall if (!Dest.isIgnored()) return Dest; 56558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall return CGF.CreateAggTemp(T, "agg.tmp.ensured"); 57ef072fd2f3347cfd857d6eb787b245b950771430John McCall } 58e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall void EnsureDest(QualType T) { 59e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (!Dest.isIgnored()) return; 60e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall Dest = CGF.CreateAggTemp(T, "agg.tmp.ensured"); 61e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } 62fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall 639c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattnerpublic: 64e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest) 65e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall : CGF(cgf), Builder(CGF.Builder), Dest(Dest) { 669c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner } 679c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner 68ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner //===--------------------------------------------------------------------===// 69ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner // Utilities 70ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner //===--------------------------------------------------------------------===// 71ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner 729c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner /// EmitAggLoadOfLValue - Given an expression with aggregate type that 739c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner /// represents a value lvalue, this method emits the address of the lvalue, 749c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner /// then loads the result into DestPtr. 759c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner void EmitAggLoadOfLValue(const Expr *E); 76922696f03ec9637449e2cba260493808b4977cd3Eli Friedman 774ac20ddc7ab324a59862657f756bdd060076b137Mike Stump /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 78e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall void EmitFinalDestCopy(QualType type, const LValue &src); 79e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall void EmitFinalDestCopy(QualType type, RValue src, 80e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall CharUnits srcAlignment = CharUnits::Zero()); 81e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall void EmitCopy(QualType type, const AggValueSlot &dest, 82e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall const AggValueSlot &src); 834ac20ddc7ab324a59862657f756bdd060076b137Mike Stump 84410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall void EmitMoveFromReturnSlot(const Expr *E, RValue Src); 85fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall 8632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl void EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType, 8732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl QualType elementType, InitListExpr *E); 8832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 897c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) { 904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGF.getLangOpts().getGC() && TypeRequiresGCollection(T)) 917c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall return AggValueSlot::NeedsGCBarriers; 927c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall return AggValueSlot::DoesNotNeedGCBarriers; 937c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall } 947c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall 95fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall bool TypeRequiresGCollection(QualType T); 96fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall 97ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner //===--------------------------------------------------------------------===// 98ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner // Visitor Methods 99ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner //===--------------------------------------------------------------------===// 1001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1019c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner void VisitStmt(Stmt *S) { 102488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar CGF.ErrorUnsupported(S, "aggregate expression"); 1039c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner } 1049c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } 105f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne void VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 106f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne Visit(GE->getResultExpr()); 107f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne } 10812444a24419fe88b42a16b46106db3c11ac5cd35Eli Friedman void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } 10991a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) { 11091a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall return Visit(E->getReplacement()); 11191a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall } 1129c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner 1139c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner // l-values. 114f4b88a45902af1802a1cb42ba48b1c474474f228John McCall void VisitDeclRefExpr(DeclRefExpr *E) { 115dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall // For aggregates, we should always be able to emit the variable 116dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall // as an l-value unless it's a reference. This is due to the fact 117dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall // that we can't actually ever see a normal l2r conversion on an 118dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall // aggregate in C++, and in C there's no language standard 119dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall // actively preventing us from listing variables in the captures 120dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall // list of a block. 121f4b88a45902af1802a1cb42ba48b1c474474f228John McCall if (E->getDecl()->getType()->isReferenceType()) { 122dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall if (CodeGenFunction::ConstantEmission result 123f4b88a45902af1802a1cb42ba48b1c474474f228John McCall = CGF.tryEmitAsConstant(E)) { 124e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitFinalDestCopy(E->getType(), result.getReferenceLValue(CGF, E)); 125dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall return; 126dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall } 127dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall } 128dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall 129f4b88a45902af1802a1cb42ba48b1c474474f228John McCall EmitAggLoadOfLValue(E); 130dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall } 131dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall 1329b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } 1339b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } 1345be028f84243e0f6906c259e67cbdaf9bee431b2Daniel Dunbar void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } 135751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregor void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); 1369b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 1379b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon EmitAggLoadOfLValue(E); 1389b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon } 139f0a990c2aa0b596a7e3cdd8fa2a5909d591ffe66Chris Lattner void VisitPredefinedExpr(const PredefinedExpr *E) { 1401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump EmitAggLoadOfLValue(E); 141f0a990c2aa0b596a7e3cdd8fa2a5909d591ffe66Chris Lattner } 1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1439c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner // Operators. 1444d8673b645ad86e496b886a0f80b60763f67071dAnders Carlsson void VisitCastExpr(CastExpr *E); 145148fe6772733166c720e28b7bb5084af6e624b44Anders Carlsson void VisitCallExpr(const CallExpr *E); 146b2d963f527674275c9109252474948368b6e6161Chris Lattner void VisitStmtExpr(const StmtExpr *E); 1479c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner void VisitBinaryOperator(const BinaryOperator *BO); 1488bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); 14903d6fb99224c36935c9af9f4785cb33453c99b2bChris Lattner void VisitBinAssign(const BinaryOperator *E); 15007fa52ab33a75d7a5736ea5bd0d4e3134fb10c7eEli Friedman void VisitBinComma(const BinaryOperator *E); 1519c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner 1528fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner void VisitObjCMessageExpr(ObjCMessageExpr *E); 1530a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 1540a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar EmitAggLoadOfLValue(E); 1550a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar } 1561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); 158a294ca8c64fbb345f32e4af9d8fabdf2f64e4883Anders Carlsson void VisitChooseExpr(const ChooseExpr *CE); 159636c3d04673da8c8605d7e45640a2ff7aec648f1Devang Patel void VisitInitListExpr(InitListExpr *E); 16030311fa6b0735b9cb73b01e25bf9652a4b9b0c53Anders Carlsson void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); 16104421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 16204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner Visit(DAE->getExpr()); 16304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner } 164c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { 165c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith CodeGenFunction::CXXDefaultInitExprScope Scope(CGF); 166c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith Visit(DIE->getExpr()); 167c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith } 168b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlsson void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); 16931ccf377f4a676eb6c205b47eef435de616d5e2dAnders Carlsson void VisitCXXConstructExpr(const CXXConstructExpr *E); 1704c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman void VisitLambdaExpr(LambdaExpr *E); 1717c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith void VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E); 1724765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall void VisitExprWithCleanups(ExprWithCleanups *E); 173ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); 1742710c4159ff4761ba9867aca18f60a178b297686Mike Stump void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } 17503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); 176e996ffd240f20a1048179d7727a6ee3227261921John McCall void VisitOpaqueValueExpr(OpaqueValueExpr *E); 177e996ffd240f20a1048179d7727a6ee3227261921John McCall 1784b9c2d235fb9449e249d74f48ecfec601650de93John McCall void VisitPseudoObjectExpr(PseudoObjectExpr *E) { 1794b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (E->isGLValue()) { 1804b9c2d235fb9449e249d74f48ecfec601650de93John McCall LValue LV = CGF.EmitPseudoObjectLValue(E); 181e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return EmitFinalDestCopy(E->getType(), LV); 1824b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 1834b9c2d235fb9449e249d74f48ecfec601650de93John McCall 1844b9c2d235fb9449e249d74f48ecfec601650de93John McCall CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType())); 1854b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 1864b9c2d235fb9449e249d74f48ecfec601650de93John McCall 187b1851249d787f573b9e1312fff8ca4bbcf351f10Eli Friedman void VisitVAArgExpr(VAArgExpr *E); 188f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner 189649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier void EmitInitializationToLValue(Expr *E, LValue Address); 190a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall void EmitNullInitializationToLValue(LValue Address); 1919c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner // case Expr::ChooseExprClass: 19239406b1395f69341c045e863a6620310abdc55b6Mike Stump void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } 193276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman void VisitAtomicExpr(AtomicExpr *E) { 194276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr()); 195276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 1969c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner}; 1979c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner} // end anonymous namespace. 1989c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner 199ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===// 200ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner// Utilities 201ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===// 2029c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner 203883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner/// EmitAggLoadOfLValue - Given an expression with aggregate type that 204883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner/// represents a value lvalue, this method emits the address of the lvalue, 205883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner/// then loads the result into DestPtr. 2069c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattnervoid AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { 2079c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner LValue LV = CGF.EmitLValue(E); 2089eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 2099eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // If the type of the l-value is atomic, then do an atomic load. 2109eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (LV.getType()->isAtomicType()) { 2114ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky CGF.EmitAtomicLoad(LV, E->getExprLoc(), Dest); 2129eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return; 2139eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 2149eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 215e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitFinalDestCopy(E->getType(), LV); 2164ac20ddc7ab324a59862657f756bdd060076b137Mike Stump} 2174ac20ddc7ab324a59862657f756bdd060076b137Mike Stump 218fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall/// \brief True if the given aggregate type requires special GC API calls. 219fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCallbool AggExprEmitter::TypeRequiresGCollection(QualType T) { 220fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall // Only record types have members that might require garbage collection. 221fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall const RecordType *RecordTy = T->getAs<RecordType>(); 222fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall if (!RecordTy) return false; 223fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall 224fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall // Don't mess with non-trivial C++ types. 225fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall RecordDecl *Record = RecordTy->getDecl(); 226fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall if (isa<CXXRecordDecl>(Record) && 227426391cd51af86f9d59eceb0fb1c42153eccbb9aRichard Smith (cast<CXXRecordDecl>(Record)->hasNonTrivialCopyConstructor() || 228fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) 229fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall return false; 230fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall 231fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall // Check whether the type has an object member. 232fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall return Record->hasObjectMember(); 233fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall} 234fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall 235410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// \brief Perform the final move to DestPtr if for some reason 236410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// getReturnValueSlot() didn't use it directly. 237fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall/// 238fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall/// The idea is that you do something like this: 239fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall/// RValue Result = EmitSomething(..., getReturnValueSlot()); 240410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// EmitMoveFromReturnSlot(E, Result); 241410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// 242410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// If nothing interferes, this will cause the result to be emitted 243410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// directly into the return value slot. Otherwise, a final move 244410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// will be performed. 245e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue src) { 246410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall if (shouldUseDestForReturnSlot()) { 247410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall // Logically, Dest.getAddr() should equal Src.getAggregateAddr(). 248410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall // The possibility of undef rvalues complicates that a lot, 249410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall // though, so we can't really assert. 250410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall return; 25155bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian } 252410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall 253e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Otherwise, copy from there to the destination. 254e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall assert(Dest.getAddr() != src.getAggregateAddr()); 255e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall std::pair<CharUnits, CharUnits> typeInfo = 25626397edfea66a29ef433b62b4db6d621db438f75Chad Rosier CGF.getContext().getTypeInfoInChars(E->getType()); 257e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitFinalDestCopy(E->getType(), src, typeInfo.second); 258fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall} 259fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall 2604ac20ddc7ab324a59862657f756bdd060076b137Mike Stump/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 261e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src, 262e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall CharUnits srcAlign) { 263e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall assert(src.isAggregate() && "value must be aggregate value!"); 264e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddr(), type, srcAlign); 265e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitFinalDestCopy(type, srcLV); 266e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall} 2674ac20ddc7ab324a59862657f756bdd060076b137Mike Stump 268e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. 269e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src) { 270558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall // If Dest is ignored, then we're evaluating an aggregate expression 271e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // in a context that doesn't care about the result. Note that loads 272e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // from volatile l-values force the existence of a non-ignored 273e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // destination. 274e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (Dest.isIgnored()) 275e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return; 2768a97005f97a2a93fc2cd942c040668c5d4df7537Fariborz Jahanian 277e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall AggValueSlot srcAgg = 278e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall AggValueSlot::forLValue(src, AggValueSlot::IsDestructed, 279e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall needsGC(type), AggValueSlot::IsAliased); 280e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitCopy(type, Dest, srcAgg); 281e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall} 282883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner 283e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// Perform a copy from the source into the destination. 284e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// 285e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// \param type - the type of the aggregate being copied; qualifiers are 286e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// ignored 287e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid AggExprEmitter::EmitCopy(QualType type, const AggValueSlot &dest, 288e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall const AggValueSlot &src) { 289e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (dest.requiresGCollection()) { 290e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall CharUnits sz = CGF.getContext().getTypeSizeInChars(type); 291e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall llvm::Value *size = llvm::ConstantInt::get(CGF.SizeTy, sz.getQuantity()); 29208c321380fff07d476a19daab6d29522c046cd49Fariborz Jahanian CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, 293e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall dest.getAddr(), 294e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall src.getAddr(), 295e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall size); 29608c321380fff07d476a19daab6d29522c046cd49Fariborz Jahanian return; 29708c321380fff07d476a19daab6d29522c046cd49Fariborz Jahanian } 2984ac20ddc7ab324a59862657f756bdd060076b137Mike Stump 299e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // If the result of the assignment is used, copy the LHS there also. 300e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // It's volatile if either side is. Use the minimum alignment of 301e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // the two sides. 302e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall CGF.EmitAggregateCopy(dest.getAddr(), src.getAddr(), type, 303e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall dest.isVolatile() || src.isVolatile(), 304e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall std::min(dest.getAlignment(), src.getAlignment())); 305883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner} 306883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner 30732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl/// \brief Emit the initializer for a std::initializer_list initialized with a 30832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl/// real initializer list. 3097c3e615f01e8f9f587315800fdaf2305ed824568Richard Smithvoid 3107c3e615f01e8f9f587315800fdaf2305ed824568Richard SmithAggExprEmitter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) { 3117c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith // Emit an array containing the elements. The array is externally destructed 3127c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith // if the std::initializer_list object is. 3137c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith ASTContext &Ctx = CGF.getContext(); 3147c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith LValue Array = CGF.EmitLValue(E->getSubExpr()); 3157c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith assert(Array.isSimple() && "initializer_list array not a simple lvalue"); 3167c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith llvm::Value *ArrayPtr = Array.getAddress(); 3177c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith 3187c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith const ConstantArrayType *ArrayType = 3197c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); 3207c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith assert(ArrayType && "std::initializer_list constructed from non-array"); 3217c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith 3227c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith // FIXME: Perform the checks on the field types in SemaInit. 3237c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); 3247c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith RecordDecl::field_iterator Field = Record->field_begin(); 3257c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith if (Field == Record->field_end()) { 3267c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith CGF.ErrorUnsupported(E, "weird std::initializer_list"); 327babcf9d04f4ed9d7ac96812e42c9e8fc0f1ae2c2Sebastian Redl return; 32832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } 32932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 33032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Start pointer. 3317c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith if (!Field->getType()->isPointerType() || 3327c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith !Ctx.hasSameType(Field->getType()->getPointeeType(), 3337c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith ArrayType->getElementType())) { 3347c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith CGF.ErrorUnsupported(E, "weird std::initializer_list"); 335babcf9d04f4ed9d7ac96812e42c9e8fc0f1ae2c2Sebastian Redl return; 33632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } 3377c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith 3387c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith AggValueSlot Dest = EnsureSlot(E->getType()); 3397c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(), 3407c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith Dest.getAlignment()); 3417c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith LValue Start = CGF.EmitLValueForFieldInitialization(DestLV, *Field); 3427c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith llvm::Value *Zero = llvm::ConstantInt::get(CGF.PtrDiffTy, 0); 3437c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith llvm::Value *IdxStart[] = { Zero, Zero }; 3447c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith llvm::Value *ArrayStart = 3457c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith Builder.CreateInBoundsGEP(ArrayPtr, IdxStart, "arraystart"); 3467c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith CGF.EmitStoreThroughLValue(RValue::get(ArrayStart), Start); 3477c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith ++Field; 3487c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith 3497c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith if (Field == Record->field_end()) { 3507c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith CGF.ErrorUnsupported(E, "weird std::initializer_list"); 351babcf9d04f4ed9d7ac96812e42c9e8fc0f1ae2c2Sebastian Redl return; 35232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } 3537c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith 3547c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith llvm::Value *Size = Builder.getInt(ArrayType->getSize()); 3557c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith LValue EndOrLength = CGF.EmitLValueForFieldInitialization(DestLV, *Field); 3567c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith if (Field->getType()->isPointerType() && 3577c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith Ctx.hasSameType(Field->getType()->getPointeeType(), 3587c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith ArrayType->getElementType())) { 35932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // End pointer. 3607c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith llvm::Value *IdxEnd[] = { Zero, Size }; 3617c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith llvm::Value *ArrayEnd = 3627c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith Builder.CreateInBoundsGEP(ArrayPtr, IdxEnd, "arrayend"); 3637c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith CGF.EmitStoreThroughLValue(RValue::get(ArrayEnd), EndOrLength); 3647c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith } else if (Ctx.hasSameType(Field->getType(), Ctx.getSizeType())) { 36532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Length. 3667c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith CGF.EmitStoreThroughLValue(RValue::get(Size), EndOrLength); 36732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } else { 3687c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith CGF.ErrorUnsupported(E, "weird std::initializer_list"); 369babcf9d04f4ed9d7ac96812e42c9e8fc0f1ae2c2Sebastian Redl return; 37032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } 37132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl} 37232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 373ef8225444452a1486bd721f3285301fe84643b00Stephen Hines/// \brief Determine if E is a trivial array filler, that is, one that is 374ef8225444452a1486bd721f3285301fe84643b00Stephen Hines/// equivalent to zero-initialization. 375ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesstatic bool isTrivialFiller(Expr *E) { 376ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (!E) 377ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return true; 378ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 379ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (isa<ImplicitValueInitExpr>(E)) 380ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return true; 381ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 382ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (auto *ILE = dyn_cast<InitListExpr>(E)) { 383ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (ILE->getNumInits()) 384ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return false; 385ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return isTrivialFiller(ILE->getArrayFiller()); 386ef8225444452a1486bd721f3285301fe84643b00Stephen Hines } 387ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 388ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (auto *Cons = dyn_cast_or_null<CXXConstructExpr>(E)) 389ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return Cons->getConstructor()->isDefaultConstructor() && 390ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Cons->getConstructor()->isTrivial(); 391ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 392ef8225444452a1486bd721f3285301fe84643b00Stephen Hines // FIXME: Are there other cases where we can avoid emitting an initializer? 393ef8225444452a1486bd721f3285301fe84643b00Stephen Hines return false; 394ef8225444452a1486bd721f3285301fe84643b00Stephen Hines} 395ef8225444452a1486bd721f3285301fe84643b00Stephen Hines 39632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl/// \brief Emit initialization of an array from an initializer list. 39732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redlvoid AggExprEmitter::EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType, 39832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl QualType elementType, InitListExpr *E) { 39932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl uint64_t NumInitElements = E->getNumInits(); 40032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 40132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl uint64_t NumArrayElements = AType->getNumElements(); 40232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl assert(NumInitElements <= NumArrayElements); 40332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 40432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // DestPtr is an array*. Construct an elementType* by drilling 40532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // down a level. 40632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0); 40732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::Value *indices[] = { zero, zero }; 40832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::Value *begin = 40932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin"); 41032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 41132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Exception safety requires us to destroy all the 41232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // already-constructed members if an initializer throws. 41332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // For that, we'll need an EH cleanup. 41432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl QualType::DestructionKind dtorKind = elementType.isDestructedType(); 4156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines llvm::AllocaInst *endOfInit = nullptr; 41632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl EHScopeStack::stable_iterator cleanup; 4176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines llvm::Instruction *cleanupDominator = nullptr; 41832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl if (CGF.needsEHCleanup(dtorKind)) { 41932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // In principle we could tell the cleanup where we are more 42032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // directly, but the control flow can get so varied here that it 42132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // would actually be quite complex. Therefore we go through an 42232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // alloca. 42332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl endOfInit = CGF.CreateTempAlloca(begin->getType(), 42432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl "arrayinit.endOfInit"); 42532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl cleanupDominator = Builder.CreateStore(begin, endOfInit); 42632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType, 42732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl CGF.getDestroyer(dtorKind)); 42832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl cleanup = CGF.EHStack.stable_begin(); 42932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 43032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Otherwise, remember that we didn't need a cleanup. 43132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } else { 43232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl dtorKind = QualType::DK_none; 43332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } 43432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 43532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1); 43632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 43732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // The 'current element to initialize'. The invariants on this 43832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // variable are complicated. Essentially, after each iteration of 43932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // the loop, it points to the last initialized element, except 44032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // that it points to the beginning of the array before any 44132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // elements have been initialized. 44232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::Value *element = begin; 44332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 44432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Emit the explicit initializers. 44532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl for (uint64_t i = 0; i != NumInitElements; ++i) { 44632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Advance to the next element. 44732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl if (i > 0) { 44832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element"); 44932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 45032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Tell the cleanup that it needs to destroy up to this 45132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // element. TODO: some of these stores can be trivially 45232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // observed to be unnecessary. 45332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl if (endOfInit) Builder.CreateStore(element, endOfInit); 45432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } 45532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 4567c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith LValue elementLV = CGF.MakeAddrLValue(element, elementType); 4577c3e615f01e8f9f587315800fdaf2305ed824568Richard Smith EmitInitializationToLValue(E->getInit(i), elementLV); 45832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } 45932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 46032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Check whether there's a non-trivial array-fill expression. 46132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl Expr *filler = E->getArrayFiller(); 462ef8225444452a1486bd721f3285301fe84643b00Stephen Hines bool hasTrivialFiller = isTrivialFiller(filler); 46332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 46432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Any remaining elements need to be zero-initialized, possibly 46532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // using the filler expression. We can skip this if the we're 46632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // emitting to zeroed memory. 46732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl if (NumInitElements != NumArrayElements && 46832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl !(Dest.isZeroed() && hasTrivialFiller && 46932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl CGF.getTypes().isZeroInitializable(elementType))) { 47032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 47132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Use an actual loop. This is basically 47232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // do { *array++ = filler; } while (array != end); 47332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 47432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Advance to the start of the rest of the array. 47532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl if (NumInitElements) { 47632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start"); 47732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl if (endOfInit) Builder.CreateStore(element, endOfInit); 47832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } 47932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 48032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Compute the end of the array. 48132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::Value *end = Builder.CreateInBoundsGEP(begin, 48232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements), 48332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl "arrayinit.end"); 48432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 48532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); 48632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body"); 48732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 48832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Jump into the body. 48932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl CGF.EmitBlock(bodyBB); 49032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::PHINode *currentElement = 49132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl Builder.CreatePHI(element->getType(), 2, "arrayinit.cur"); 49232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl currentElement->addIncoming(element, entryBB); 49332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 49432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Emit the actual filler expression. 49532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType); 49632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl if (filler) 497649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitInitializationToLValue(filler, elementLV); 49832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl else 49932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl EmitNullInitializationToLValue(elementLV); 50032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 50132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Move on to the next element. 50232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::Value *nextElement = 50332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next"); 50432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 50532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Tell the EH cleanup that we finished with the last element. 50632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl if (endOfInit) Builder.CreateStore(nextElement, endOfInit); 50732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 50832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Leave the loop if we're done. 50932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::Value *done = Builder.CreateICmpEQ(nextElement, end, 51032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl "arrayinit.done"); 51132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end"); 51232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl Builder.CreateCondBr(done, endBB, bodyBB); 51332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl currentElement->addIncoming(nextElement, Builder.GetInsertBlock()); 51432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 51532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl CGF.EmitBlock(endBB); 51632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl } 51732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 51832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl // Leave the partial-array cleanup if we entered one. 51932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl if (dtorKind) CGF.DeactivateCleanupBlock(cleanup, cleanupDominator); 52032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl} 52132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl 522ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===// 523ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner// Visitor Methods 524ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===// 525ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner 52603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregorvoid AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){ 52703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor Visit(E->GetTemporaryExpr()); 52803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor} 52903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor 530e996ffd240f20a1048179d7727a6ee3227261921John McCallvoid AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) { 531e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitFinalDestCopy(e->getType(), CGF.getOpaqueLValueMapping(e)); 532e996ffd240f20a1048179d7727a6ee3227261921John McCall} 533e996ffd240f20a1048179d7727a6ee3227261921John McCall 534751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregorvoid 535751ec9be961888f14342fb63b39bf8727f0dee49Douglas GregorAggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 5361723f6398ea8aa8d602a478f47695bf3b0374d35John McCall if (Dest.isPotentiallyAliased() && 5371723f6398ea8aa8d602a478f47695bf3b0374d35John McCall E->getType().isPODType(CGF.getContext())) { 538673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor // For a POD type, just emit a load of the lvalue + a copy, because our 539673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor // compound literal might alias the destination. 540673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor EmitAggLoadOfLValue(E); 541673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor return; 542673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor } 543673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor 544751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregor AggValueSlot Slot = EnsureSlot(E->getType()); 545751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregor CGF.EmitAggExpr(E->getInitializer(), Slot); 546751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregor} 547751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregor 5489eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall/// Attempt to look through various unimportant expressions to find a 5499eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall/// cast of the given kind. 5509eda3abe7e183b05834947391c0cdc291f4ee0d8John McCallstatic Expr *findPeephole(Expr *op, CastKind kind) { 5519eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall while (true) { 5529eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall op = op->IgnoreParens(); 5539eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (CastExpr *castE = dyn_cast<CastExpr>(op)) { 5549eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (castE->getCastKind() == kind) 5559eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return castE->getSubExpr(); 5569eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (castE->getCastKind() == CK_NoOp) 5579eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall continue; 5589eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 5596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 5609eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 5619eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall} 562751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregor 5634d8673b645ad86e496b886a0f80b60763f67071dAnders Carlssonvoid AggExprEmitter::VisitCastExpr(CastExpr *E) { 5643016842613674ab80796567239c15d529aff1458Anders Carlsson switch (E->getCastKind()) { 565575b374fdbfc2c2224fd3047ac11ffc4b8db9ae5Anders Carlsson case CK_Dynamic: { 5662c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith // FIXME: Can this actually happen? We have no test coverage for it. 56769cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?"); 5682c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith LValue LV = CGF.EmitCheckedLValue(E->getSubExpr(), 5697ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith CodeGenFunction::TCK_Load); 57069cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor // FIXME: Do we also need to handle property references here? 57169cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor if (LV.isSimple()) 57269cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E)); 57369cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor else 57469cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast"); 57569cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor 576558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall if (!Dest.isIgnored()) 577558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination"); 57869cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor break; 57969cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor } 58069cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor 5812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_ToUnion: { 5826591271275f7a3db357f3cb7af37ef86e800e4baJohn McCall if (Dest.isIgnored()) break; 5836591271275f7a3db357f3cb7af37ef86e800e4baJohn McCall 5844d8673b645ad86e496b886a0f80b60763f67071dAnders Carlsson // GCC union extension 58579c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar QualType Ty = E->getSubExpr()->getType(); 58679c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar QualType PtrTy = CGF.getContext().getPointerType(Ty); 587558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(), 58834ebf4d1767e6748a1a59a5d1935c495cd8877e8Eli Friedman CGF.ConvertType(PtrTy)); 589a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall EmitInitializationToLValue(E->getSubExpr(), 590649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier CGF.MakeAddrLValue(CastPtr, Ty)); 5913016842613674ab80796567239c15d529aff1458Anders Carlsson break; 5927e91627301b05cd8f2324795e19d87a62f444c31Nuno Lopes } 5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_DerivedToBase: 5952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_BaseToDerived: 5962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_UncheckedDerivedToBase: { 597b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: " 5982d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor "should have been unpacked before we got here"); 5992d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor } 6002d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor 6019eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall case CK_NonAtomicToAtomic: 6029eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall case CK_AtomicToNonAtomic: { 6039eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall bool isToAtomic = (E->getCastKind() == CK_NonAtomicToAtomic); 6049eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6059eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // Determine the atomic and value types. 6069eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall QualType atomicType = E->getSubExpr()->getType(); 6079eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall QualType valueType = E->getType(); 6089eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (isToAtomic) std::swap(atomicType, valueType); 6099eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6109eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall assert(atomicType->isAtomicType()); 6119eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall assert(CGF.getContext().hasSameUnqualifiedType(valueType, 6129eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall atomicType->castAs<AtomicType>()->getValueType())); 6139eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6149eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // Just recurse normally if we're ignoring the result or the 6159eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // atomic type doesn't change representation. 6169eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (Dest.isIgnored() || !CGF.CGM.isPaddedAtomicType(atomicType)) { 6179eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return Visit(E->getSubExpr()); 6189eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 6199eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6209eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall CastKind peepholeTarget = 6219eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall (isToAtomic ? CK_AtomicToNonAtomic : CK_NonAtomicToAtomic); 6229eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6239eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // These two cases are reverses of each other; try to peephole them. 6249eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (Expr *op = findPeephole(E->getSubExpr(), peepholeTarget)) { 6259eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall assert(CGF.getContext().hasSameUnqualifiedType(op->getType(), 6269eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall E->getType()) && 6279eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall "peephole significantly changed types?"); 6289eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return Visit(op); 6299eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 6309eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6319eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // If we're converting an r-value of non-atomic type to an r-value 632336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman // of atomic type, just emit directly into the relevant sub-object. 6339eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (isToAtomic) { 634336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman AggValueSlot valueDest = Dest; 635336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman if (!valueDest.isIgnored() && CGF.CGM.isPaddedAtomicType(atomicType)) { 636336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman // Zero-initialize. (Strictly speaking, we only need to intialize 637336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman // the padding at the end, but this is simpler.) 638336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman if (!Dest.isZeroed()) 639eb1f276b10c8705e4282442aa61ab9033302b2beEli Friedman CGF.EmitNullInitialization(Dest.getAddr(), atomicType); 640336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman 641336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman // Build a GEP to refer to the subobject. 642336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman llvm::Value *valueAddr = 643336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman CGF.Builder.CreateStructGEP(valueDest.getAddr(), 0); 644336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman valueDest = AggValueSlot::forAddr(valueAddr, 645336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman valueDest.getAlignment(), 646336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman valueDest.getQualifiers(), 647336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman valueDest.isExternallyDestructed(), 648336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman valueDest.requiresGCollection(), 649336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman valueDest.isPotentiallyAliased(), 650336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman AggValueSlot::IsZeroed); 651336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman } 652336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman 653eb1f276b10c8705e4282442aa61ab9033302b2beEli Friedman CGF.EmitAggExpr(E->getSubExpr(), valueDest); 6549eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return; 6559eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 6569eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6579eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // Otherwise, we're converting an atomic type to a non-atomic type. 658336d9df5e628279425344d754dc68047fa5b00a7Eli Friedman // Make an atomic temporary, emit into that, and then copy the value out. 6599eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall AggValueSlot atomicSlot = 6609eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall CGF.CreateAggTemp(atomicType, "atomic-to-nonatomic.temp"); 6619eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall CGF.EmitAggExpr(E->getSubExpr(), atomicSlot); 6629eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6639eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall llvm::Value *valueAddr = 6649eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall Builder.CreateStructGEP(atomicSlot.getAddr(), 0); 6659eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall RValue rvalue = RValue::getAggregate(valueAddr, atomicSlot.isVolatile()); 6669eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return EmitFinalDestCopy(valueType, rvalue); 6679eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 6689eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 669e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall case CK_LValueToRValue: 670e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // If we're loading from a volatile type, force the destination 671e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // into existence. 672e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (E->getSubExpr()->getType().isVolatileQualified()) { 673e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EnsureDest(E->getType()); 674e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return Visit(E->getSubExpr()); 675e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } 6769eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 677e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // fallthrough 678e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 6792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_NoOp: 6802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_UserDefinedConversion: 6812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_ConstructorConversion: 6823016842613674ab80796567239c15d529aff1458Anders Carlsson assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), 6833016842613674ab80796567239c15d529aff1458Anders Carlsson E->getType()) && 6843016842613674ab80796567239c15d529aff1458Anders Carlsson "Implicit cast types must be compatible"); 6853016842613674ab80796567239c15d529aff1458Anders Carlsson Visit(E->getSubExpr()); 6863016842613674ab80796567239c15d529aff1458Anders Carlsson break; 6870ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall 6882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_LValueBitCast: 6890ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall llvm_unreachable("should not be emitting lvalue bitcast as rvalue"); 6901de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall 6910ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_Dependent: 6920ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_BitCast: 6930ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_ArrayToPointerDecay: 6940ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_FunctionToPointerDecay: 6950ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_NullToPointer: 6960ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_NullToMemberPointer: 6970ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_BaseToDerivedMemberPointer: 6980ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_DerivedToBaseMemberPointer: 6990ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_MemberPointerToBoolean: 7004d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case CK_ReinterpretMemberPointer: 7010ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_IntegralToPointer: 7020ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_PointerToIntegral: 7030ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_PointerToBoolean: 7040ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_ToVoid: 7050ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_VectorSplat: 7060ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_IntegralCast: 7070ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_IntegralToBoolean: 7080ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_IntegralToFloating: 7090ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_FloatingToIntegral: 7100ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_FloatingToBoolean: 7110ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_FloatingCast: 7121d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 7131d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 7140ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_AnyPointerToBlockPointerCast: 7150ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_ObjCObjectLValueCast: 7160ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_FloatingRealToComplex: 7170ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_FloatingComplexToReal: 7180ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_FloatingComplexToBoolean: 7190ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_FloatingComplexCast: 7200ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_FloatingComplexToIntegralComplex: 7210ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_IntegralRealToComplex: 7220ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_IntegralComplexToReal: 7230ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_IntegralComplexToBoolean: 7240ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_IntegralComplexCast: 7250ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall case CK_IntegralComplexToFloatingComplex: 72633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 72733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 72833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 72933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: 730ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor case CK_CopyAndAutoreleaseBlockObject: 731a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman case CK_BuiltinFnToFnPtr: 732e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei case CK_ZeroToOCLEvent: 733651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines case CK_AddressSpaceConversion: 7340ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall llvm_unreachable("cast kind invalid for aggregate types"); 7353016842613674ab80796567239c15d529aff1458Anders Carlsson } 736e4707ff0bb48add651c6a1ad9acfcb22609462d1Anders Carlsson} 737e4707ff0bb48add651c6a1ad9acfcb22609462d1Anders Carlsson 7389619662a1d42e2008b865d3459c0677e149dad1bChris Lattnervoid AggExprEmitter::VisitCallExpr(const CallExpr *E) { 739e70e8f7fef3efb3d526ee25b3a0e2a4bf67a04b6Anders Carlsson if (E->getCallReturnType()->isReferenceType()) { 740e70e8f7fef3efb3d526ee25b3a0e2a4bf67a04b6Anders Carlsson EmitAggLoadOfLValue(E); 741e70e8f7fef3efb3d526ee25b3a0e2a4bf67a04b6Anders Carlsson return; 742e70e8f7fef3efb3d526ee25b3a0e2a4bf67a04b6Anders Carlsson } 7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 744fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); 745410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall EmitMoveFromReturnSlot(E, RV); 746796ef3d4a32ce8d9e76df0d5bcab07db97883064Nate Begeman} 7479619662a1d42e2008b865d3459c0677e149dad1bChris Lattner 7489619662a1d42e2008b865d3459c0677e149dad1bChris Lattnervoid AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { 749fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); 750410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall EmitMoveFromReturnSlot(E, RV); 7518fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner} 752796ef3d4a32ce8d9e76df0d5bcab07db97883064Nate Begeman 7539619662a1d42e2008b865d3459c0677e149dad1bChris Lattnervoid AggExprEmitter::VisitBinComma(const BinaryOperator *E) { 7542a41637a995affa1563f4d82a8b026e326a2faa0John McCall CGF.EmitIgnoredExpr(E->getLHS()); 755558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall Visit(E->getRHS()); 75607fa52ab33a75d7a5736ea5bd0d4e3134fb10c7eEli Friedman} 75707fa52ab33a75d7a5736ea5bd0d4e3134fb10c7eEli Friedman 758b2d963f527674275c9109252474948368b6e6161Chris Lattnervoid AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { 759150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall CodeGenFunction::StmtExprEvaluation eval(CGF); 760558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest); 761b2d963f527674275c9109252474948368b6e6161Chris Lattner} 762b2d963f527674275c9109252474948368b6e6161Chris Lattner 7639c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattnervoid AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { 7642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI) 7658bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian VisitPointerToDataMemberBinaryOperator(E); 7668bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian else 7678bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian CGF.ErrorUnsupported(E, "aggregate binary expression"); 7688bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian} 7698bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian 7708bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanianvoid AggExprEmitter::VisitPointerToDataMemberBinaryOperator( 7718bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian const BinaryOperator *E) { 7728bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); 773e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitFinalDestCopy(E->getType(), LV); 774e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall} 775e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 776e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// Is the value of the given expression possibly a reference to or 777e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// into a __block variable? 778e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallstatic bool isBlockVarRef(const Expr *E) { 779e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Make sure we look through parens. 780e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall E = E->IgnoreParens(); 781e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 782e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Check for a direct reference to a __block variable. 783e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 784e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl()); 785e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return (var && var->hasAttr<BlocksAttr>()); 786e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } 787e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 788e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // More complicated stuff. 789e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 790e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Binary operators. 791e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) { 792e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // For an assignment or pointer-to-member operation, just care 793e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // about the LHS. 794e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (op->isAssignmentOp() || op->isPtrMemOp()) 795e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return isBlockVarRef(op->getLHS()); 796e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 797e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // For a comma, just care about the RHS. 798e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (op->getOpcode() == BO_Comma) 799e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return isBlockVarRef(op->getRHS()); 800e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 801e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // FIXME: pointer arithmetic? 802e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return false; 803e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 804e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Check both sides of a conditional operator. 805e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } else if (const AbstractConditionalOperator *op 806e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall = dyn_cast<AbstractConditionalOperator>(E)) { 807e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return isBlockVarRef(op->getTrueExpr()) 808e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall || isBlockVarRef(op->getFalseExpr()); 809e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 810e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // OVEs are required to support BinaryConditionalOperators. 811e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } else if (const OpaqueValueExpr *op 812e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall = dyn_cast<OpaqueValueExpr>(E)) { 813e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (const Expr *src = op->getSourceExpr()) 814e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return isBlockVarRef(src); 815e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 816e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Casts are necessary to get things like (*(int*)&var) = foo(). 817e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // We don't really care about the kind of cast here, except 818e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // we don't want to look through l2r casts, because it's okay 819e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // to get the *value* in a __block variable. 820e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) { 821e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (cast->getCastKind() == CK_LValueToRValue) 822e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return false; 823e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return isBlockVarRef(cast->getSubExpr()); 824e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 825e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Handle unary operators. Again, just aggressively look through 826e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // it, ignoring the operation. 827e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) { 828e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return isBlockVarRef(uop->getSubExpr()); 829e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 830e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Look into the base of a field access. 831e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) { 832e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return isBlockVarRef(mem->getBase()); 833e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 834e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Look into the base of a subscript. 835e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) { 836e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return isBlockVarRef(sub->getBase()); 837e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } 838e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 839e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return false; 840ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner} 841ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner 84203d6fb99224c36935c9af9f4785cb33453c99b2bChris Lattnervoid AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { 843ff6e2b7d31b0f5494f583419e5061c32ea4e6180Eli Friedman // For an assignment to work, the value on the right has 844ff6e2b7d31b0f5494f583419e5061c32ea4e6180Eli Friedman // to be compatible with the value on the left. 8452dce5f8a99b5c48f1287ff3941288ca6f7fde2deEli Friedman assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 8462dce5f8a99b5c48f1287ff3941288ca6f7fde2deEli Friedman E->getRHS()->getType()) 847ff6e2b7d31b0f5494f583419e5061c32ea4e6180Eli Friedman && "Invalid assignment"); 848cd940a1e13e588a43973cd7ae33b5c33a3062739John McCall 849e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // If the LHS might be a __block variable, and the RHS can 850e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // potentially cause a block copy, we need to evaluate the RHS first 851e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // so that the assignment goes the right place. 852e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // This is pretty semantically fragile. 853e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (isBlockVarRef(E->getLHS()) && 854e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall E->getRHS()->HasSideEffects(CGF.getContext())) { 855e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Ensure that we have a destination, and evaluate the RHS into that. 856e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EnsureDest(E->getRHS()->getType()); 857e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall Visit(E->getRHS()); 858e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 859e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Now emit the LHS and copy into it. 8604def70d3040e73707c738f7c366737a986135edfRichard Smith LValue LHS = CGF.EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store); 861e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 8629eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // That copy is an atomic copy if the LHS is atomic. 8639eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (LHS.getType()->isAtomicType()) { 8649eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false); 8659eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return; 8669eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 8679eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 868e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitCopy(E->getLHS()->getType(), 869e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 870e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall needsGC(E->getLHS()->getType()), 871e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall AggValueSlot::IsAliased), 872e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall Dest); 873e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall return; 874e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall } 875649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier 8769c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner LValue LHS = CGF.EmitLValue(E->getLHS()); 877883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner 8789eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // If we have an atomic type, evaluate into the destination and then 8799eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // do an atomic copy. 8809eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (LHS.getType()->isAtomicType()) { 8819eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall EnsureDest(E->getRHS()->getType()); 8829eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall Visit(E->getRHS()); 8839eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false); 8849eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return; 8859eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 8869eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 887db45806b991013280a03057025c9538de64d5dfbJohn McCall // Codegen the RHS so that it stores directly into the LHS. 888db45806b991013280a03057025c9538de64d5dfbJohn McCall AggValueSlot LHSSlot = 889db45806b991013280a03057025c9538de64d5dfbJohn McCall AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed, 890db45806b991013280a03057025c9538de64d5dfbJohn McCall needsGC(E->getLHS()->getType()), 891649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsAliased); 8923ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian // A non-volatile aggregate destination might have volatile member. 8933ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian if (!LHSSlot.isVolatile() && 8943ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian CGF.hasVolatileMember(E->getLHS()->getType())) 8953ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian LHSSlot.setVolatile(true); 8963ac83d69c61238cd0d38e90fcdd03390530ab2fbFariborz Jahanian 897e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall CGF.EmitAggExpr(E->getRHS(), LHSSlot); 898e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall 899e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall // Copy into the destination if the assignment isn't ignored. 900e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitFinalDestCopy(E->getType(), LHS); 901883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner} 902883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner 90356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallvoid AggExprEmitter:: 90456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallVisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 9059615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 9069615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 9079615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 9081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 90956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall // Bind the common expression if necessary. 910d97927d69b277120f8d403580c44acd84907d7b4Eli Friedman CodeGenFunction::OpaqueValueMapping binding(CGF, E); 91156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 912651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RegionCounter Cnt = CGF.getPGORegionCounter(E); 913150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall CodeGenFunction::ConditionalEvaluation eval(CGF); 914651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock, Cnt.getCount()); 9151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 91674fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall // Save whether the destination's lifetime is externally managed. 917fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall bool isExternallyDestructed = Dest.isExternallyDestructed(); 918883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner 919150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall eval.begin(CGF); 920150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall CGF.EmitBlock(LHSBlock); 921651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Cnt.beginRegion(Builder); 92256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall Visit(E->getTrueExpr()); 923150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall eval.end(CGF); 9241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 925150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!"); 926150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall CGF.Builder.CreateBr(ContBlock); 9271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 92874fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall // If the result of an agg expression is unused, then the emission 92974fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall // of the LHS might need to create a destination slot. That's fine 93074fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall // with us, and we can safely emit the RHS into the same slot, but 931fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall // we shouldn't claim that it's already being destructed. 932fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall Dest.setExternallyDestructed(isExternallyDestructed); 93374fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall 934150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall eval.begin(CGF); 935150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall CGF.EmitBlock(RHSBlock); 93656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall Visit(E->getFalseExpr()); 937150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall eval.end(CGF); 9381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9399c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner CGF.EmitBlock(ContBlock); 940883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner} 941ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner 942a294ca8c64fbb345f32e4af9d8fabdf2f64e4883Anders Carlssonvoid AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { 943a5e660188a3c654cf0c88ed1093b28207e870b2bEli Friedman Visit(CE->getChosenSubExpr()); 944a294ca8c64fbb345f32e4af9d8fabdf2f64e4883Anders Carlsson} 945a294ca8c64fbb345f32e4af9d8fabdf2f64e4883Anders Carlsson 946b1851249d787f573b9e1312fff8ca4bbcf351f10Eli Friedmanvoid AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 9470785570af3ef5f8c5a0377129e41efe6f3f8d770Daniel Dunbar llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 948ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 949ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson 9500262f02cbaa35cafb61b1b994e0adff7c422a235Sebastian Redl if (!ArgPtr) { 951651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // If EmitVAArg fails, we fall back to the LLVM instruction. 952651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines llvm::Value *Val = 953651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType())); 954651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!Dest.isIgnored()) 955651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Builder.CreateStore(Val, Dest.getAddr()); 9560262f02cbaa35cafb61b1b994e0adff7c422a235Sebastian Redl return; 9570262f02cbaa35cafb61b1b994e0adff7c422a235Sebastian Redl } 9580262f02cbaa35cafb61b1b994e0adff7c422a235Sebastian Redl 959e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType())); 960b1851249d787f573b9e1312fff8ca4bbcf351f10Eli Friedman} 961b1851249d787f573b9e1312fff8ca4bbcf351f10Eli Friedman 962b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlssonvoid AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 963558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall // Ensure that we have a slot, but if we already do, remember 964fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall // whether it was externally destructed. 965fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall bool wasExternallyDestructed = Dest.isExternallyDestructed(); 966e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EnsureDest(E->getType()); 967fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall 968fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall // We're going to push a destructor if there isn't already one. 969fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall Dest.setExternallyDestructed(); 970558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall 971558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall Visit(E->getSubExpr()); 972558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall 973fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall // Push that destructor we promised. 974fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall if (!wasExternallyDestructed) 97586811609d9353e3aed198045d56e790eb3b6118cPeter Collingbourne CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddr()); 976b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlsson} 977b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlsson 978b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlssonvoid 97931ccf377f4a676eb6c205b47eef435de616d5e2dAnders CarlssonAggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { 980558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall AggValueSlot Slot = EnsureSlot(E->getType()); 981558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall CGF.EmitCXXConstructExpr(E, Slot); 9827f6ad153565245026c7569314f65a4d4ff4ac41fAnders Carlsson} 9837f6ad153565245026c7569314f65a4d4ff4ac41fAnders Carlsson 9844c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedmanvoid 9854c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli FriedmanAggExprEmitter::VisitLambdaExpr(LambdaExpr *E) { 9864c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman AggValueSlot Slot = EnsureSlot(E->getType()); 9874c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman CGF.EmitLambdaExpr(E, Slot); 9884c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman} 9894c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman 9904765fa05b5652fcc4356371c2f481d0ea9a1b007John McCallvoid AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) { 9911a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall CGF.enterFullExpression(E); 9921a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall CodeGenFunction::RunCleanupsScope cleanups(CGF); 9931a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall Visit(E->getSubExpr()); 994b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson} 995b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson 996ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregorvoid AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 997558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall QualType T = E->getType(); 998558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall AggValueSlot Slot = EnsureSlot(T); 999a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 100030311fa6b0735b9cb73b01e25bf9652a4b9b0c53Anders Carlsson} 100130311fa6b0735b9cb73b01e25bf9652a4b9b0c53Anders Carlsson 100230311fa6b0735b9cb73b01e25bf9652a4b9b0c53Anders Carlssonvoid AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 1003558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall QualType T = E->getType(); 1004558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall AggValueSlot Slot = EnsureSlot(T); 1005a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T)); 1006329763b1e9ec8c216025e3a8379ed446d7372cbcNuno Lopes} 1007329763b1e9ec8c216025e3a8379ed446d7372cbcNuno Lopes 10081b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// isSimpleZero - If emitting this value will obviously just cause a store of 10091b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// zero to memory, return true. This can return false if uncertain, so it just 10101b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// handles simple cases. 10111b726771d00762fb5c4c2638e60d134c385493aeChris Lattnerstatic bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) { 1012f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne E = E->IgnoreParens(); 1013f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne 10141b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // 0 10151b726771d00762fb5c4c2638e60d134c385493aeChris Lattner if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) 10161b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return IL->getValue() == 0; 10171b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // +0.0 10181b726771d00762fb5c4c2638e60d134c385493aeChris Lattner if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E)) 10191b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return FL->getValue().isPosZero(); 10201b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // int() 10211b726771d00762fb5c4c2638e60d134c385493aeChris Lattner if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) && 10221b726771d00762fb5c4c2638e60d134c385493aeChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 10231b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return true; 10241b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // (int*)0 - Null pointer expressions. 10251b726771d00762fb5c4c2638e60d134c385493aeChris Lattner if (const CastExpr *ICE = dyn_cast<CastExpr>(E)) 10261b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return ICE->getCastKind() == CK_NullToPointer; 10271b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // '\0' 10281b726771d00762fb5c4c2638e60d134c385493aeChris Lattner if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) 10291b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return CL->getValue() == 0; 10301b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 10311b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // Otherwise, hard case: conservatively return false. 10321b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return false; 10331b726771d00762fb5c4c2638e60d134c385493aeChris Lattner} 10341b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 10351b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 103678e83f881e59d4b8648a7b85ec6f2d36ef5cc680Anders Carlssonvoid 10374ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick LewyckyAggExprEmitter::EmitInitializationToLValue(Expr *E, LValue LV) { 1038a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall QualType type = LV.getType(); 10397f79f9be5916c51c35da4f126b7c12596a101607Mike Stump // FIXME: Ignore result? 1040f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // FIXME: Are initializers affected by volatile? 10411b726771d00762fb5c4c2638e60d134c385493aeChris Lattner if (Dest.isZeroed() && isSimpleZero(E, CGF)) { 10421b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // Storing "i32 0" to a zero'd memory location is a noop. 10439d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 10440dbe2fb7758fe64568206b5bc0f1c5b106b9c806Richard Smith } else if (isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) { 10459d232c884ea9872d6555df0fd7359699819bc1f1John McCall return EmitNullInitializationToLValue(LV); 1046a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall } else if (type->isReferenceType()) { 1047d4ec562b3aaf50ea9015f82c96ebfd05a35fc7efRichard Smith RValue RV = CGF.EmitReferenceBindingToExpr(E); 10489d232c884ea9872d6555df0fd7359699819bc1f1John McCall return CGF.EmitStoreThroughLValue(RV, LV); 10499d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 10509d232c884ea9872d6555df0fd7359699819bc1f1John McCall 10519d232c884ea9872d6555df0fd7359699819bc1f1John McCall switch (CGF.getEvaluationKind(type)) { 10529d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Complex: 10539d232c884ea9872d6555df0fd7359699819bc1f1John McCall CGF.EmitComplexExprIntoLValue(E, LV, /*isInit*/ true); 10549d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 10559d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Aggregate: 10567c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV, 10577c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall AggValueSlot::IsDestructed, 10587c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 1059410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall AggValueSlot::IsNotAliased, 1060a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall Dest.isZeroed())); 10619d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 10629d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Scalar: 10639d232c884ea9872d6555df0fd7359699819bc1f1John McCall if (LV.isSimple()) { 10646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines CGF.EmitScalarInit(E, /*D=*/nullptr, LV, /*Captured=*/false); 10659d232c884ea9872d6555df0fd7359699819bc1f1John McCall } else { 10669d232c884ea9872d6555df0fd7359699819bc1f1John McCall CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV); 10679d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 10689d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 1069f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner } 10709d232c884ea9872d6555df0fd7359699819bc1f1John McCall llvm_unreachable("bad evaluation kind"); 1071f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner} 1072305762c08975cd6e0bebd684ca910fa208792483Lauro Ramos Venancio 1073a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCallvoid AggExprEmitter::EmitNullInitializationToLValue(LValue lv) { 1074a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall QualType type = lv.getType(); 1075a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall 10761b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // If the destination slot is already zeroed out before the aggregate is 10771b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // copied into it, we don't have to emit any zeros here. 1078a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type)) 10791b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return; 10801b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 10819d232c884ea9872d6555df0fd7359699819bc1f1John McCall if (CGF.hasScalarEvaluationKind(type)) { 10820dbe2fb7758fe64568206b5bc0f1c5b106b9c806Richard Smith // For non-aggregates, we can store the appropriate null constant. 10830dbe2fb7758fe64568206b5bc0f1c5b106b9c806Richard Smith llvm::Value *null = CGF.CGM.EmitNullConstant(type); 1084b1e3f324b0c4d17399609c246918dadcb886d739Eli Friedman // Note that the following is not equivalent to 1085b1e3f324b0c4d17399609c246918dadcb886d739Eli Friedman // EmitStoreThroughBitfieldLValue for ARC types. 10865a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman if (lv.isBitField()) { 1087b1e3f324b0c4d17399609c246918dadcb886d739Eli Friedman CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv); 10885a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman } else { 10895a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman assert(lv.isSimple()); 10905a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true); 10915a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman } 1092f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner } else { 1093f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // There's a potential optimization opportunity in combining 1094f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // memsets; that would be easy for arrays, but relatively 1095f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // difficult for structures with the current code. 1096a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall CGF.EmitNullInitialization(lv.getAddress(), lv.getType()); 1097f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner } 1098f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner} 1099305762c08975cd6e0bebd684ca910fa208792483Lauro Ramos Venancio 1100f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattnervoid AggExprEmitter::VisitInitListExpr(InitListExpr *E) { 1101a385b3c9c442831fc6a9ec6e8823b2067bd65710Eli Friedman#if 0 110213a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman // FIXME: Assess perf here? Figure out what cases are worth optimizing here 110313a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman // (Length of globals? Chunks of zeroed-out space?). 1104a385b3c9c442831fc6a9ec6e8823b2067bd65710Eli Friedman // 1105f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // If we can, prefer a copy from a global; this is a lot less code for long 1106f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // globals, and it's easier for the current optimizers to analyze. 110713a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { 1108994ffef4353056363ba5915eeecf0e1b0678f286Eli Friedman llvm::GlobalVariable* GV = 110913a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, 111013a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman llvm::GlobalValue::InternalLinkage, C, ""); 1111e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType())); 1112994ffef4353056363ba5915eeecf0e1b0678f286Eli Friedman return; 1113994ffef4353056363ba5915eeecf0e1b0678f286Eli Friedman } 1114a385b3c9c442831fc6a9ec6e8823b2067bd65710Eli Friedman#endif 1115d0db03a561671b8b466b07026cc8fbbb037bb639Chris Lattner if (E->hadArrayRangeDesignator()) 1116a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor CGF.ErrorUnsupported(E, "GNU array range designator extension"); 1117a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor 1118e69fb2043519d8f36314dd5dd78bb7638c6140f6Richard Smith AggValueSlot Dest = EnsureSlot(E->getType()); 1119e69fb2043519d8f36314dd5dd78bb7638c6140f6Richard Smith 1120377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(), 1121377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman Dest.getAlignment()); 1122558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall 1123f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // Handle initialization of an array. 1124f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner if (E->getType()->isArrayType()) { 1125fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith if (E->isStringLiteralInit()) 1126fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith return Visit(E->getInit(0)); 1127922696f03ec9637449e2cba260493808b4977cd3Eli Friedman 11285c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman QualType elementType = 11295c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman CGF.getContext().getAsArrayType(E->getType())->getElementType(); 1130bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 113132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::PointerType *APType = 1132377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman cast<llvm::PointerType>(Dest.getAddr()->getType()); 113332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl llvm::ArrayType *AType = 113432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl cast<llvm::ArrayType>(APType->getElementType()); 1135bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 1136377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman EmitArrayInit(Dest.getAddr(), AType, elementType, E); 1137305762c08975cd6e0bebd684ca910fa208792483Lauro Ramos Venancio return; 1138f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner } 11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1140f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner assert(E->getType()->isRecordType() && "Only support structs/unions here!"); 11411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1142f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // Do struct initialization; this code just sets each individual member 1143f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // to the approprate value. This makes bitfield support automatic; 1144f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // the disadvantage is that the generated code is more difficult for 1145f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // the optimizer, especially with bitfields. 1146f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner unsigned NumInitElements = E->getNumInits(); 11472b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl(); 1148c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith 1149c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith // Prepare a 'this' for CXXDefaultInitExprs. 1150c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith CodeGenFunction::FieldConstructionScope FCS(CGF, Dest.getAddr()); 1151c3bf52ced9652f555aa0767bb822ec4c64546212Richard Smith 11522b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall if (record->isUnion()) { 11530bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor // Only initialize one field of a union. The field itself is 11540bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor // specified by the initializer list. 11550bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor if (!E->getInitializedFieldInUnion()) { 11560bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor // Empty union; we have nothing to do. 11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11580bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor#ifndef NDEBUG 11590bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor // Make sure that it's really an empty and not a failure of 11600bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor // semantic analysis. 1161651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto *Field : record->fields()) 11620bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); 11630bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor#endif 11640bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor return; 11650bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor } 11660bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor 11670bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor // FIXME: volatility 11680bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor FieldDecl *Field = E->getInitializedFieldInUnion(); 11690bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor 1170377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field); 11710bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor if (NumInitElements) { 11720bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor // Store the initializer into the field 1173649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitInitializationToLValue(E->getInit(0), FieldLoc); 11740bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor } else { 11751b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // Default-initialize to null. 1176a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall EmitNullInitializationToLValue(FieldLoc); 11770bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor } 11780bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor 11790bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor return; 11800bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor } 11811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11822b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // We'll need to enter cleanup scopes in case any of the member 11832b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // initializers throw an exception. 11845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<EHScopeStack::stable_iterator, 16> cleanups; 11856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines llvm::Instruction *cleanupDominator = nullptr; 11862b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall 1187f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // Here we iterate over the fields; this makes it simpler to both 1188f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // default-initialize fields and skip over unnamed fields. 11892b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall unsigned curInitIndex = 0; 1190651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto *field : record->fields()) { 11912b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // We're done once we hit the flexible array member. 11922b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall if (field->getType()->isIncompleteArrayType()) 119344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor break; 119444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 11952b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // Always skip anonymous bitfields. 11962b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall if (field->isUnnamedBitfield()) 1197f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner continue; 119834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor 11992b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // We're done if we reach the end of the explicit initializers, we 12002b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // have a zeroed object, and the rest of the fields are 12012b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // zero-initializable. 12022b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall if (curInitIndex == NumInitElements && Dest.isZeroed() && 12031b726771d00762fb5c4c2638e60d134c385493aeChris Lattner CGF.getTypes().isZeroInitializable(E->getType())) 12041b726771d00762fb5c4c2638e60d134c385493aeChris Lattner break; 12051b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 1206377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman 1207651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, field); 120814674ffb81dccbc4e1bf78ab5b7987685819b445Fariborz Jahanian // We never generate write-barries for initialized fields. 12092b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall LV.setNonGC(true); 12101b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 12112b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall if (curInitIndex < NumInitElements) { 1212b35baae19b906245b5c2266b47ef411abcc6b25aChris Lattner // Store the initializer into the field. 1213649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitInitializationToLValue(E->getInit(curInitIndex++), LV); 1214f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner } else { 1215f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner // We're out of initalizers; default-initialize to null 12162b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall EmitNullInitializationToLValue(LV); 12172b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall } 12182b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall 12192b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // Push a destructor if necessary. 12202b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // FIXME: if we have an array of structures, all explicitly 12212b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // initialized, we can end up pushing a linear number of cleanups. 12222b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall bool pushedCleanup = false; 12232b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall if (QualType::DestructionKind dtorKind 12242b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall = field->getType().isDestructedType()) { 12252b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall assert(LV.isSimple()); 12262b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall if (CGF.needsEHCleanup(dtorKind)) { 12276f103ba42cb69d50005a977c5ea583984ab63fc4John McCall if (!cleanupDominator) 12286f103ba42cb69d50005a977c5ea583984ab63fc4John McCall cleanupDominator = CGF.Builder.CreateUnreachable(); // placeholder 12296f103ba42cb69d50005a977c5ea583984ab63fc4John McCall 12302b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(), 12312b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall CGF.getDestroyer(dtorKind), false); 12322b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall cleanups.push_back(CGF.EHStack.stable_begin()); 12332b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall pushedCleanup = true; 12342b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall } 1235f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner } 12361b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 12371b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // If the GEP didn't get used because of a dead zero init or something 12381b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // else, clean it up for -O0 builds and general tidiness. 12392b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall if (!pushedCleanup && LV.isSimple()) 12401b726771d00762fb5c4c2638e60d134c385493aeChris Lattner if (llvm::GetElementPtrInst *GEP = 12412b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall dyn_cast<llvm::GetElementPtrInst>(LV.getAddress())) 12421b726771d00762fb5c4c2638e60d134c385493aeChris Lattner if (GEP->use_empty()) 12431b726771d00762fb5c4c2638e60d134c385493aeChris Lattner GEP->eraseFromParent(); 1244145cd89f9233d375381aa13bd28b2d36f83e6181Lauro Ramos Venancio } 12452b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall 12462b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // Deactivate all the partial cleanups in reverse order, which 12472b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall // generally means popping them. 12482b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall for (unsigned i = cleanups.size(); i != 0; --i) 12496f103ba42cb69d50005a977c5ea583984ab63fc4John McCall CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator); 12506f103ba42cb69d50005a977c5ea583984ab63fc4John McCall 12516f103ba42cb69d50005a977c5ea583984ab63fc4John McCall // Destroy the placeholder if we made one. 12526f103ba42cb69d50005a977c5ea583984ab63fc4John McCall if (cleanupDominator) 12536f103ba42cb69d50005a977c5ea583984ab63fc4John McCall cleanupDominator->eraseFromParent(); 1254636c3d04673da8c8605d7e45640a2ff7aec648f1Devang Patel} 1255636c3d04673da8c8605d7e45640a2ff7aec648f1Devang Patel 1256ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===// 1257ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner// Entry Points into this File 1258ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===// 1259ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner 12601b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// GetNumNonZeroBytesInInit - Get an approximate count of the number of 12611b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// non-zero bytes that will be stored when outputting the initializer for the 12621b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// specified initializer expression. 126302c45333b8310bb792a15f85f219706025f9752cKen Dyckstatic CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) { 1264f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne E = E->IgnoreParens(); 12651b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 12661b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // 0 and 0.0 won't require any non-zero stores! 126702c45333b8310bb792a15f85f219706025f9752cKen Dyck if (isSimpleZero(E, CGF)) return CharUnits::Zero(); 12681b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 12691b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // If this is an initlist expr, sum up the size of sizes of the (present) 12701b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // elements. If this is something weird, assume the whole thing is non-zero. 12711b726771d00762fb5c4c2638e60d134c385493aeChris Lattner const InitListExpr *ILE = dyn_cast<InitListExpr>(E); 12726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!ILE || !CGF.getTypes().isZeroInitializable(ILE->getType())) 127302c45333b8310bb792a15f85f219706025f9752cKen Dyck return CGF.getContext().getTypeSizeInChars(E->getType()); 12741b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 1275d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner // InitListExprs for structs have to be handled carefully. If there are 1276d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner // reference members, we need to consider the size of the reference, not the 1277d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner // referencee. InitListExprs for unions and arrays can't have references. 12788c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner if (const RecordType *RT = E->getType()->getAs<RecordType>()) { 12798c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner if (!RT->isUnionType()) { 12808c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); 128102c45333b8310bb792a15f85f219706025f9752cKen Dyck CharUnits NumNonZeroBytes = CharUnits::Zero(); 1282d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner 12838c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner unsigned ILEElement = 0; 1284651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto *Field : SD->fields()) { 12858c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner // We're done once we hit the flexible array member or run out of 12868c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner // InitListExpr elements. 12878c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner if (Field->getType()->isIncompleteArrayType() || 12888c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner ILEElement == ILE->getNumInits()) 12898c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner break; 12908c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner if (Field->isUnnamedBitfield()) 12918c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner continue; 12928c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner 12938c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner const Expr *E = ILE->getInit(ILEElement++); 12948c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner 12958c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner // Reference values are always non-null and have the width of a pointer. 12968c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner if (Field->getType()->isReferenceType()) 129702c45333b8310bb792a15f85f219706025f9752cKen Dyck NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits( 129864aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall CGF.getTarget().getPointerWidth(0)); 12998c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner else 13008c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF); 13018c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner } 1302d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner 13038c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner return NumNonZeroBytes; 1304d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner } 1305d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner } 1306d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner 1307d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner 130802c45333b8310bb792a15f85f219706025f9752cKen Dyck CharUnits NumNonZeroBytes = CharUnits::Zero(); 13091b726771d00762fb5c4c2638e60d134c385493aeChris Lattner for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) 13101b726771d00762fb5c4c2638e60d134c385493aeChris Lattner NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF); 13111b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return NumNonZeroBytes; 13121b726771d00762fb5c4c2638e60d134c385493aeChris Lattner} 13131b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 13141b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// CheckAggExprForMemSetUse - If the initializer is large and has a lot of 13151b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// zeros in it, emit a memset and avoid storing the individual zeros. 13161b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// 13171b726771d00762fb5c4c2638e60d134c385493aeChris Lattnerstatic void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E, 13181b726771d00762fb5c4c2638e60d134c385493aeChris Lattner CodeGenFunction &CGF) { 13191b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // If the slot is already known to be zeroed, nothing to do. Don't mess with 13201b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // volatile stores. 13216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == nullptr) 13226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return; 1323657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis 1324657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis // C++ objects with a user-declared constructor don't need zero'ing. 13257edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith if (CGF.getLangOpts().CPlusPlus) 1326657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis if (const RecordType *RT = CGF.getContext() 1327657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis .getBaseElementType(E->getType())->getAs<RecordType>()) { 1328657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1329657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis if (RD->hasUserDeclaredConstructor()) 1330657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis return; 1331657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis } 1332657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis 13331b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // If the type is 16-bytes or smaller, prefer individual stores over memset. 13345ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck std::pair<CharUnits, CharUnits> TypeInfo = 13355ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck CGF.getContext().getTypeInfoInChars(E->getType()); 13365ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck if (TypeInfo.first <= CharUnits::fromQuantity(16)) 13371b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return; 13381b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 13391b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // Check to see if over 3/4 of the initializer are known to be zero. If so, 13401b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // we prefer to emit memset + individual stores for the rest. 13415ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF); 13425ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck if (NumNonZeroBytes*4 > TypeInfo.first) 13431b726771d00762fb5c4c2638e60d134c385493aeChris Lattner return; 13441b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 13451b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // Okay, it seems like a good idea to use an initial memset, emit the call. 13465ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity()); 13475ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck CharUnits Align = TypeInfo.second; 13481b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 13491b726771d00762fb5c4c2638e60d134c385493aeChris Lattner llvm::Value *Loc = Slot.getAddr(); 13501b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 13518b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner Loc = CGF.Builder.CreateBitCast(Loc, CGF.Int8PtrTy); 13525ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, 13535ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck Align.getQuantity(), false); 13541b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 13551b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // Tell the AggExprEmitter that the slot is known zero. 13561b726771d00762fb5c4c2638e60d134c385493aeChris Lattner Slot.setZeroed(); 13571b726771d00762fb5c4c2638e60d134c385493aeChris Lattner} 13581b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 13591b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 13601b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 13611b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 1362e1129a92b25f9b05f1b97fdd81d38ea451875414Mike Stump/// EmitAggExpr - Emit the computation of the specified expression of aggregate 1363e1129a92b25f9b05f1b97fdd81d38ea451875414Mike Stump/// type. The result is computed into DestPtr. Note that if DestPtr is null, 1364e1129a92b25f9b05f1b97fdd81d38ea451875414Mike Stump/// the value of the aggregate expression is not needed. If VolatileDest is 1365e1129a92b25f9b05f1b97fdd81d38ea451875414Mike Stump/// true, DestPtr cannot be 0. 1366e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) { 13679d232c884ea9872d6555df0fd7359699819bc1f1John McCall assert(E && hasAggregateEvaluationKind(E->getType()) && 1368ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner "Invalid aggregate expression to emit"); 13696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines assert((Slot.getAddr() != nullptr || Slot.isIgnored()) && 13701b726771d00762fb5c4c2638e60d134c385493aeChris Lattner "slot has bits but no address"); 13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13721b726771d00762fb5c4c2638e60d134c385493aeChris Lattner // Optimize the slot if possible. 13731b726771d00762fb5c4c2638e60d134c385493aeChris Lattner CheckAggExprForMemSetUse(Slot, E, *this); 13741b726771d00762fb5c4c2638e60d134c385493aeChris Lattner 1375e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall AggExprEmitter(*this, Slot).Visit(const_cast<Expr*>(E)); 1376ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner} 13777482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar 137818aba0dd518e486d8b50523e7dafb4b5657135d2Daniel DunbarLValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { 13799d232c884ea9872d6555df0fd7359699819bc1f1John McCall assert(hasAggregateEvaluationKind(E->getType()) && "Invalid argument!"); 1380195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar llvm::Value *Temp = CreateMemTemp(E->getType()); 138179c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar LValue LV = MakeAddrLValue(Temp, E->getType()); 13827c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed, 13834418439220a8f8e0b1deffdccce2354854c702f5John McCall AggValueSlot::DoesNotNeedGCBarriers, 1384649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 138579c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar return LV; 138618aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar} 138718aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar 1388649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosiervoid CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, 1389649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier llvm::Value *SrcPtr, QualType Ty, 1390e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall bool isVolatile, 13916cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer CharUnits alignment, 13926cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer bool isAssignment) { 1393649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); 13941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13957edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith if (getLangOpts().CPlusPlus) { 1396649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier if (const RecordType *RT = Ty->getAs<RecordType>()) { 1397649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 1398649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier assert((Record->hasTrivialCopyConstructor() || 1399649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier Record->hasTrivialCopyAssignment() || 1400649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier Record->hasTrivialMoveConstructor() || 1401649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier Record->hasTrivialMoveAssignment()) && 1402426391cd51af86f9d59eceb0fb1c42153eccbb9aRichard Smith "Trying to aggregate-copy a type without a trivial copy/move " 1403e9979484670ca2b528146d1b681e149fdc29f7ccDouglas Gregor "constructor or assignment operator"); 1404649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // Ignore empty classes in C++. 1405649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier if (Record->isEmpty()) 14060d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson return; 14070d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson } 14080d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson } 14090d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson 1410649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // Aggregate assignment turns into llvm.memcpy. This is almost valid per 1411649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // C99 6.5.16.1p3, which states "If the value being stored in an object is 1412649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // read from another object that overlaps in anyway the storage of the first 1413649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // object, then the overlap shall be exact and the two objects shall have 1414649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // qualified or unqualified versions of a compatible type." 1415649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // 1416649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // memcpy is not defined if the source and destination pointers are exactly 1417649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // equal, but other compilers do this optimization, and almost every memcpy 1418649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // implementation handles this case safely. If there is a libc that does not 1419649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // safely handle this, we can add a target hook. 1420649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier 14216cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // Get data size and alignment info for this aggregate. If this is an 14226cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer // assignment don't copy the tail padding. Otherwise copying it is fine. 14236cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer std::pair<CharUnits, CharUnits> TypeInfo; 14246cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer if (isAssignment) 14256cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer TypeInfo = getContext().getTypeInfoDataSizeInChars(Ty); 14266cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer else 14276cacae8bf9597b8124cd40aedc189c04484e1990Benjamin Kramer TypeInfo = getContext().getTypeInfoInChars(Ty); 1428649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier 1429e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall if (alignment.isZero()) 1430e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall alignment = TypeInfo.second; 1431649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier 1432649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // FIXME: Handle variable sized types. 1433649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier 1434649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // FIXME: If we have a volatile struct, the optimizer can remove what might 1435649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // appear to be `extra' memory ops: 1436649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // 1437649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // volatile struct { int i; } a, b; 1438649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // 1439649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // int main() { 1440649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // a = b; 1441649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // a = b; 1442649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // } 1443649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // 1444649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // we need to use a different call here. We use isVolatile to indicate when 1445649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier // either the source or the destination is volatile. 1446649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier 1447649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); 14482acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *DBP = 1449d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace()); 1450649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier DestPtr = Builder.CreateBitCast(DestPtr, DBP); 14513ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang 1452649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); 14532acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *SBP = 1454d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace()); 1455649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier SrcPtr = Builder.CreateBitCast(SrcPtr, SBP); 14563ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang 1457f85e193739c953358c865005855253af4f68a497John McCall // Don't do any of the memmove_collectable tests if GC isn't set. 14584e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { 1459f85e193739c953358c865005855253af4f68a497John McCall // fall through 1460649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { 1461649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier RecordDecl *Record = RecordTy->getDecl(); 1462649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier if (Record->hasObjectMember()) { 1463649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier CharUnits size = TypeInfo.first; 1464649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1465649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1466649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1467649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier SizeVal); 146855bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian return; 146955bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian } 1470649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier } else if (Ty->isArrayType()) { 1471649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier QualType BaseType = getContext().getBaseElementType(Ty); 1472649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { 1473649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier if (RecordTy->getDecl()->hasObjectMember()) { 1474649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier CharUnits size = TypeInfo.first; 1475649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 1476649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier llvm::Value *SizeVal = 1477649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1478649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, 1479649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier SizeVal); 148055bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian return; 148155bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian } 148255bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian } 148355bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian } 1484b22c7dc707cf3770ff3b5e5f11f11fd0aaa06d9bDan Gohman 1485b22c7dc707cf3770ff3b5e5f11f11fd0aaa06d9bDan Gohman // Determine the metadata to describe the position of any padding in this 1486b22c7dc707cf3770ff3b5e5f11f11fd0aaa06d9bDan Gohman // memcpy, as well as the TBAA tags for the members of the struct, in case 1487b22c7dc707cf3770ff3b5e5f11f11fd0aaa06d9bDan Gohman // the optimizer wishes to expand it in to scalar memory operations. 1488b22c7dc707cf3770ff3b5e5f11f11fd0aaa06d9bDan Gohman llvm::MDNode *TBAAStructTag = CGM.getTBAAStructInfo(Ty); 14896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 1490649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier Builder.CreateMemCpy(DestPtr, SrcPtr, 1491649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier llvm::ConstantInt::get(IntPtrTy, 1492649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier TypeInfo.first.getQuantity()), 1493b22c7dc707cf3770ff3b5e5f11f11fd0aaa06d9bDan Gohman alignment.getQuantity(), isVolatile, 14946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines /*TBAATag=*/nullptr, TBAAStructTag); 14957482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar} 1496