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"
15883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner#include "CodeGenModule.h"
16082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian#include "CGObjCRuntime.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"
21883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner#include "llvm/Constants.h"
22883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner#include "llvm/Function.h"
23636c3d04673da8c8605d7e45640a2ff7aec648f1Devang Patel#include "llvm/GlobalVariable.h"
24f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner#include "llvm/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
86af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  void EmitStdInitializerList(llvm::Value *DestPtr, InitListExpr *InitList);
8732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  void EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType,
8832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                     QualType elementType, InitListExpr *E);
8932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
907c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall  AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) {
914e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (CGF.getLangOpts().getGC() && TypeRequiresGCollection(T))
927c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall      return AggValueSlot::NeedsGCBarriers;
937c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall    return AggValueSlot::DoesNotNeedGCBarriers;
947c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall  }
957c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall
96fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  bool TypeRequiresGCollection(QualType T);
97fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall
98ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner  //===--------------------------------------------------------------------===//
99ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner  //                            Visitor Methods
100ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner  //===--------------------------------------------------------------------===//
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1029c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  void VisitStmt(Stmt *S) {
103488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar    CGF.ErrorUnsupported(S, "aggregate expression");
1049c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  }
1059c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
106f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  void VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
107f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    Visit(GE->getResultExpr());
108f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  }
10912444a24419fe88b42a16b46106db3c11ac5cd35Eli Friedman  void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
11091a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
11191a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall    return Visit(E->getReplacement());
11291a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall  }
1139c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner
1149c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  // l-values.
115f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  void VisitDeclRefExpr(DeclRefExpr *E) {
116dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    // For aggregates, we should always be able to emit the variable
117dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    // as an l-value unless it's a reference.  This is due to the fact
118dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    // that we can't actually ever see a normal l2r conversion on an
119dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    // aggregate in C++, and in C there's no language standard
120dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    // actively preventing us from listing variables in the captures
121dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    // list of a block.
122f4b88a45902af1802a1cb42ba48b1c474474f228John McCall    if (E->getDecl()->getType()->isReferenceType()) {
123dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall      if (CodeGenFunction::ConstantEmission result
124f4b88a45902af1802a1cb42ba48b1c474474f228John McCall            = CGF.tryEmitAsConstant(E)) {
125e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall        EmitFinalDestCopy(E->getType(), result.getReferenceLValue(CGF, E));
126dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall        return;
127dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall      }
128dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall    }
129dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall
130f4b88a45902af1802a1cb42ba48b1c474474f228John McCall    EmitAggLoadOfLValue(E);
131dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall  }
132dd2ecee313b558a9b621ec003b45e0fbc83508d7John McCall
1339b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon  void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
1349b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon  void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
1355be028f84243e0f6906c259e67cbdaf9bee431b2Daniel Dunbar  void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
136751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregor  void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
1379b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon  void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
1389b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon    EmitAggLoadOfLValue(E);
1399b73b39f6fbf987acbbe6570d557d13f07c7e0f7Seo Sanghyeon  }
140f0a990c2aa0b596a7e3cdd8fa2a5909d591ffe66Chris Lattner  void VisitPredefinedExpr(const PredefinedExpr *E) {
1411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EmitAggLoadOfLValue(E);
142f0a990c2aa0b596a7e3cdd8fa2a5909d591ffe66Chris Lattner  }
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1449c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  // Operators.
1454d8673b645ad86e496b886a0f80b60763f67071dAnders Carlsson  void VisitCastExpr(CastExpr *E);
146148fe6772733166c720e28b7bb5084af6e624b44Anders Carlsson  void VisitCallExpr(const CallExpr *E);
147b2d963f527674275c9109252474948368b6e6161Chris Lattner  void VisitStmtExpr(const StmtExpr *E);
1489c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  void VisitBinaryOperator(const BinaryOperator *BO);
1498bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian  void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
15003d6fb99224c36935c9af9f4785cb33453c99b2bChris Lattner  void VisitBinAssign(const BinaryOperator *E);
15107fa52ab33a75d7a5736ea5bd0d4e3134fb10c7eEli Friedman  void VisitBinComma(const BinaryOperator *E);
1529c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner
1538fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  void VisitObjCMessageExpr(ObjCMessageExpr *E);
1540a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1550a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar    EmitAggLoadOfLValue(E);
1560a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  }
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
159a294ca8c64fbb345f32e4af9d8fabdf2f64e4883Anders Carlsson  void VisitChooseExpr(const ChooseExpr *CE);
160636c3d04673da8c8605d7e45640a2ff7aec648f1Devang Patel  void VisitInitListExpr(InitListExpr *E);
16130311fa6b0735b9cb73b01e25bf9652a4b9b0c53Anders Carlsson  void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
16204421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
16304421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner    Visit(DAE->getExpr());
16404421087832a031c90bd58f128c7c0e741db8dd2Chris Lattner  }
165b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlsson  void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
16631ccf377f4a676eb6c205b47eef435de616d5e2dAnders Carlsson  void VisitCXXConstructExpr(const CXXConstructExpr *E);
1674c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman  void VisitLambdaExpr(LambdaExpr *E);
1684765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  void VisitExprWithCleanups(ExprWithCleanups *E);
169ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor  void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
1702710c4159ff4761ba9867aca18f60a178b297686Mike Stump  void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
17103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
172e996ffd240f20a1048179d7727a6ee3227261921John McCall  void VisitOpaqueValueExpr(OpaqueValueExpr *E);
173e996ffd240f20a1048179d7727a6ee3227261921John McCall
1744b9c2d235fb9449e249d74f48ecfec601650de93John McCall  void VisitPseudoObjectExpr(PseudoObjectExpr *E) {
1754b9c2d235fb9449e249d74f48ecfec601650de93John McCall    if (E->isGLValue()) {
1764b9c2d235fb9449e249d74f48ecfec601650de93John McCall      LValue LV = CGF.EmitPseudoObjectLValue(E);
177e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall      return EmitFinalDestCopy(E->getType(), LV);
1784b9c2d235fb9449e249d74f48ecfec601650de93John McCall    }
1794b9c2d235fb9449e249d74f48ecfec601650de93John McCall
1804b9c2d235fb9449e249d74f48ecfec601650de93John McCall    CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType()));
1814b9c2d235fb9449e249d74f48ecfec601650de93John McCall  }
1824b9c2d235fb9449e249d74f48ecfec601650de93John McCall
183b1851249d787f573b9e1312fff8ca4bbcf351f10Eli Friedman  void VisitVAArgExpr(VAArgExpr *E);
184f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner
185649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  void EmitInitializationToLValue(Expr *E, LValue Address);
186a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  void EmitNullInitializationToLValue(LValue Address);
1879c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  //  case Expr::ChooseExprClass:
18839406b1395f69341c045e863a6620310abdc55b6Mike Stump  void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
189276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  void VisitAtomicExpr(AtomicExpr *E) {
190276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman    CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr());
191276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman  }
1929c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner};
1939c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner}  // end anonymous namespace.
1949c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner
195ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===//
196ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//                                Utilities
197ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===//
1989c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner
199883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner/// EmitAggLoadOfLValue - Given an expression with aggregate type that
200883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner/// represents a value lvalue, this method emits the address of the lvalue,
201883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner/// then loads the result into DestPtr.
2029c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattnervoid AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
2039c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  LValue LV = CGF.EmitLValue(E);
204e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  EmitFinalDestCopy(E->getType(), LV);
2054ac20ddc7ab324a59862657f756bdd060076b137Mike Stump}
2064ac20ddc7ab324a59862657f756bdd060076b137Mike Stump
207fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall/// \brief True if the given aggregate type requires special GC API calls.
208fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCallbool AggExprEmitter::TypeRequiresGCollection(QualType T) {
209fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  // Only record types have members that might require garbage collection.
210fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  const RecordType *RecordTy = T->getAs<RecordType>();
211fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  if (!RecordTy) return false;
212fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall
213fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  // Don't mess with non-trivial C++ types.
214fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  RecordDecl *Record = RecordTy->getDecl();
215fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  if (isa<CXXRecordDecl>(Record) &&
216fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall      (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() ||
217fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall       !cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
218fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall    return false;
219fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall
220fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  // Check whether the type has an object member.
221fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  return Record->hasObjectMember();
222fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall}
223fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall
224410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// \brief Perform the final move to DestPtr if for some reason
225410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// getReturnValueSlot() didn't use it directly.
226fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall///
227fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall/// The idea is that you do something like this:
228fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall///   RValue Result = EmitSomething(..., getReturnValueSlot());
229410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall///   EmitMoveFromReturnSlot(E, Result);
230410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall///
231410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// If nothing interferes, this will cause the result to be emitted
232410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// directly into the return value slot.  Otherwise, a final move
233410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall/// will be performed.
234e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue src) {
235410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall  if (shouldUseDestForReturnSlot()) {
236410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall    // Logically, Dest.getAddr() should equal Src.getAggregateAddr().
237410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall    // The possibility of undef rvalues complicates that a lot,
238410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall    // though, so we can't really assert.
239410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall    return;
24055bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian  }
241410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall
242e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Otherwise, copy from there to the destination.
243e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  assert(Dest.getAddr() != src.getAggregateAddr());
244e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  std::pair<CharUnits, CharUnits> typeInfo =
24526397edfea66a29ef433b62b4db6d621db438f75Chad Rosier    CGF.getContext().getTypeInfoInChars(E->getType());
246e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  EmitFinalDestCopy(E->getType(), src, typeInfo.second);
247fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall}
248fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall
2494ac20ddc7ab324a59862657f756bdd060076b137Mike Stump/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
250e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src,
251e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                                       CharUnits srcAlign) {
252e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  assert(src.isAggregate() && "value must be aggregate value!");
253e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddr(), type, srcAlign);
254e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  EmitFinalDestCopy(type, srcLV);
255e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall}
2564ac20ddc7ab324a59862657f756bdd060076b137Mike Stump
257e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
258e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src) {
259558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  // If Dest is ignored, then we're evaluating an aggregate expression
260e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // in a context that doesn't care about the result.  Note that loads
261e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // from volatile l-values force the existence of a non-ignored
262e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // destination.
263e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  if (Dest.isIgnored())
264e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    return;
2658a97005f97a2a93fc2cd942c040668c5d4df7537Fariborz Jahanian
266e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  AggValueSlot srcAgg =
267e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    AggValueSlot::forLValue(src, AggValueSlot::IsDestructed,
268e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                            needsGC(type), AggValueSlot::IsAliased);
269e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  EmitCopy(type, Dest, srcAgg);
270e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall}
271883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner
272e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// Perform a copy from the source into the destination.
273e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall///
274e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// \param type - the type of the aggregate being copied; qualifiers are
275e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall///   ignored
276e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid AggExprEmitter::EmitCopy(QualType type, const AggValueSlot &dest,
277e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                              const AggValueSlot &src) {
278e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  if (dest.requiresGCollection()) {
279e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    CharUnits sz = CGF.getContext().getTypeSizeInChars(type);
280e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    llvm::Value *size = llvm::ConstantInt::get(CGF.SizeTy, sz.getQuantity());
28108c321380fff07d476a19daab6d29522c046cd49Fariborz Jahanian    CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
282e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                                                      dest.getAddr(),
283e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                                                      src.getAddr(),
284e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                                                      size);
28508c321380fff07d476a19daab6d29522c046cd49Fariborz Jahanian    return;
28608c321380fff07d476a19daab6d29522c046cd49Fariborz Jahanian  }
2874ac20ddc7ab324a59862657f756bdd060076b137Mike Stump
288e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // If the result of the assignment is used, copy the LHS there also.
289e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // It's volatile if either side is.  Use the minimum alignment of
290e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // the two sides.
291e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  CGF.EmitAggregateCopy(dest.getAddr(), src.getAddr(), type,
292e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                        dest.isVolatile() || src.isVolatile(),
293e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                        std::min(dest.getAlignment(), src.getAlignment()));
294883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner}
295883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner
29632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redlstatic QualType GetStdInitializerListElementType(QualType T) {
29732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // Just assume that this is really std::initializer_list.
29832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  ClassTemplateSpecializationDecl *specialization =
29932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      cast<ClassTemplateSpecializationDecl>(T->castAs<RecordType>()->getDecl());
30032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  return specialization->getTemplateArgs()[0].getAsType();
30132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl}
30232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
30332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl/// \brief Prepare cleanup for the temporary array.
30432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redlstatic void EmitStdInitializerListCleanup(CodeGenFunction &CGF,
30532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                                          QualType arrayType,
30632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                                          llvm::Value *addr,
30732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                                          const InitListExpr *initList) {
30832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  QualType::DestructionKind dtorKind = arrayType.isDestructedType();
30932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (!dtorKind)
31032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    return; // Type doesn't need destroying.
31132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (dtorKind != QualType::DK_cxx_destructor) {
31232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.ErrorUnsupported(initList, "ObjC ARC type in initializer_list");
31332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    return;
31432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
31532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
31632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  CodeGenFunction::Destroyer *destroyer = CGF.getDestroyer(dtorKind);
31732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  CGF.pushDestroy(NormalAndEHCleanup, addr, arrayType, destroyer,
31832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                  /*EHCleanup=*/true);
31932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl}
32032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
32132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl/// \brief Emit the initializer for a std::initializer_list initialized with a
32232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl/// real initializer list.
323af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redlvoid AggExprEmitter::EmitStdInitializerList(llvm::Value *destPtr,
324af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl                                            InitListExpr *initList) {
32532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // We emit an array containing the elements, then have the init list point
32632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // at the array.
32732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  ASTContext &ctx = CGF.getContext();
32832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  unsigned numInits = initList->getNumInits();
32932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  QualType element = GetStdInitializerListElementType(initList->getType());
33032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::APInt size(ctx.getTypeSize(ctx.getSizeType()), numInits);
33132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  QualType array = ctx.getConstantArrayType(element, size, ArrayType::Normal,0);
33232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::Type *LTy = CGF.ConvertTypeForMem(array);
33332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::AllocaInst *alloc = CGF.CreateTempAlloca(LTy);
33432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  alloc->setAlignment(ctx.getTypeAlignInChars(array).getQuantity());
33532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  alloc->setName(".initlist.");
33632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
33732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  EmitArrayInit(alloc, cast<llvm::ArrayType>(LTy), element, initList);
33832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
33932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // FIXME: The diagnostics are somewhat out of place here.
34032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  RecordDecl *record = initList->getType()->castAs<RecordType>()->getDecl();
34132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  RecordDecl::field_iterator field = record->field_begin();
34232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (field == record->field_end()) {
34332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.ErrorUnsupported(initList, "weird std::initializer_list");
344babcf9d04f4ed9d7ac96812e42c9e8fc0f1ae2c2Sebastian Redl    return;
34532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
34632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
34732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  QualType elementPtr = ctx.getPointerType(element.withConst());
34832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
34932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // Start pointer.
35032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (!ctx.hasSameType(field->getType(), elementPtr)) {
35132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.ErrorUnsupported(initList, "weird std::initializer_list");
352babcf9d04f4ed9d7ac96812e42c9e8fc0f1ae2c2Sebastian Redl    return;
35332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
354377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman  LValue DestLV = CGF.MakeNaturalAlignAddrLValue(destPtr, initList->getType());
355581deb3da481053c4993c7600f97acf7768caac5David Blaikie  LValue start = CGF.EmitLValueForFieldInitialization(DestLV, *field);
35632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::Value *arrayStart = Builder.CreateStructGEP(alloc, 0, "arraystart");
35732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  CGF.EmitStoreThroughLValue(RValue::get(arrayStart), start);
35832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  ++field;
35932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
36032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (field == record->field_end()) {
36132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.ErrorUnsupported(initList, "weird std::initializer_list");
362babcf9d04f4ed9d7ac96812e42c9e8fc0f1ae2c2Sebastian Redl    return;
36332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
364581deb3da481053c4993c7600f97acf7768caac5David Blaikie  LValue endOrLength = CGF.EmitLValueForFieldInitialization(DestLV, *field);
36532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (ctx.hasSameType(field->getType(), elementPtr)) {
36632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // End pointer.
36732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::Value *arrayEnd = Builder.CreateStructGEP(alloc,numInits, "arrayend");
36832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.EmitStoreThroughLValue(RValue::get(arrayEnd), endOrLength);
36932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  } else if(ctx.hasSameType(field->getType(), ctx.getSizeType())) {
37032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Length.
37132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.EmitStoreThroughLValue(RValue::get(Builder.getInt(size)), endOrLength);
37232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  } else {
37332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.ErrorUnsupported(initList, "weird std::initializer_list");
374babcf9d04f4ed9d7ac96812e42c9e8fc0f1ae2c2Sebastian Redl    return;
37532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
37632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
37732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (!Dest.isExternallyDestructed())
37832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    EmitStdInitializerListCleanup(CGF, array, alloc, initList);
37932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl}
38032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
38132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl/// \brief Emit initialization of an array from an initializer list.
38232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redlvoid AggExprEmitter::EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType,
38332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                                   QualType elementType, InitListExpr *E) {
38432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  uint64_t NumInitElements = E->getNumInits();
38532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
38632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  uint64_t NumArrayElements = AType->getNumElements();
38732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  assert(NumInitElements <= NumArrayElements);
38832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
38932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // DestPtr is an array*.  Construct an elementType* by drilling
39032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // down a level.
39132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
39232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::Value *indices[] = { zero, zero };
39332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::Value *begin =
39432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin");
39532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
39632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // Exception safety requires us to destroy all the
39732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // already-constructed members if an initializer throws.
39832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // For that, we'll need an EH cleanup.
39932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  QualType::DestructionKind dtorKind = elementType.isDestructedType();
40032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::AllocaInst *endOfInit = 0;
40132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  EHScopeStack::stable_iterator cleanup;
40232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::Instruction *cleanupDominator = 0;
40332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (CGF.needsEHCleanup(dtorKind)) {
40432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // In principle we could tell the cleanup where we are more
40532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // directly, but the control flow can get so varied here that it
40632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // would actually be quite complex.  Therefore we go through an
40732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // alloca.
40832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    endOfInit = CGF.CreateTempAlloca(begin->getType(),
40932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                                     "arrayinit.endOfInit");
41032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    cleanupDominator = Builder.CreateStore(begin, endOfInit);
41132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType,
41232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                                         CGF.getDestroyer(dtorKind));
41332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    cleanup = CGF.EHStack.stable_begin();
41432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
41532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // Otherwise, remember that we didn't need a cleanup.
41632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  } else {
41732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    dtorKind = QualType::DK_none;
41832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
41932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
42032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1);
42132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
42232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // The 'current element to initialize'.  The invariants on this
42332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // variable are complicated.  Essentially, after each iteration of
42432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // the loop, it points to the last initialized element, except
42532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // that it points to the beginning of the array before any
42632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // elements have been initialized.
42732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::Value *element = begin;
42832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
42932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // Emit the explicit initializers.
43032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  for (uint64_t i = 0; i != NumInitElements; ++i) {
43132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Advance to the next element.
43232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    if (i > 0) {
43332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element");
43432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
43532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      // Tell the cleanup that it needs to destroy up to this
43632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      // element.  TODO: some of these stores can be trivially
43732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      // observed to be unnecessary.
43832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      if (endOfInit) Builder.CreateStore(element, endOfInit);
43932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    }
44032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
441af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    // If these are nested std::initializer_list inits, do them directly,
442af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    // because they are conceptually the same "location".
443af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    InitListExpr *initList = dyn_cast<InitListExpr>(E->getInit(i));
444af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    if (initList && initList->initializesStdInitializerList()) {
445af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl      EmitStdInitializerList(element, initList);
446af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    } else {
447af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl      LValue elementLV = CGF.MakeAddrLValue(element, elementType);
448649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      EmitInitializationToLValue(E->getInit(i), elementLV);
449af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    }
45032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
45132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
45232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // Check whether there's a non-trivial array-fill expression.
45332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // Note that this will be a CXXConstructExpr even if the element
45432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // type is an array (or array of array, etc.) of class type.
45532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  Expr *filler = E->getArrayFiller();
45632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  bool hasTrivialFiller = true;
45732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (CXXConstructExpr *cons = dyn_cast_or_null<CXXConstructExpr>(filler)) {
45832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    assert(cons->getConstructor()->isDefaultConstructor());
45932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    hasTrivialFiller = cons->getConstructor()->isTrivial();
46032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
46132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
46232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // Any remaining elements need to be zero-initialized, possibly
46332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // using the filler expression.  We can skip this if the we're
46432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // emitting to zeroed memory.
46532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (NumInitElements != NumArrayElements &&
46632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      !(Dest.isZeroed() && hasTrivialFiller &&
46732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl        CGF.getTypes().isZeroInitializable(elementType))) {
46832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
46932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Use an actual loop.  This is basically
47032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    //   do { *array++ = filler; } while (array != end);
47132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
47232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Advance to the start of the rest of the array.
47332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    if (NumInitElements) {
47432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start");
47532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      if (endOfInit) Builder.CreateStore(element, endOfInit);
47632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    }
47732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
47832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Compute the end of the array.
47932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::Value *end = Builder.CreateInBoundsGEP(begin,
48032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                      llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements),
48132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                                                 "arrayinit.end");
48232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
48332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
48432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body");
48532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
48632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Jump into the body.
48732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.EmitBlock(bodyBB);
48832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::PHINode *currentElement =
48932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      Builder.CreatePHI(element->getType(), 2, "arrayinit.cur");
49032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    currentElement->addIncoming(element, entryBB);
49132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
49232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Emit the actual filler expression.
49332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType);
49432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    if (filler)
495649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      EmitInitializationToLValue(filler, elementLV);
49632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    else
49732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      EmitNullInitializationToLValue(elementLV);
49832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
49932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Move on to the next element.
50032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::Value *nextElement =
50132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next");
50232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
50332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Tell the EH cleanup that we finished with the last element.
50432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    if (endOfInit) Builder.CreateStore(nextElement, endOfInit);
50532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
50632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // Leave the loop if we're done.
50732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::Value *done = Builder.CreateICmpEQ(nextElement, end,
50832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                                             "arrayinit.done");
50932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end");
51032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    Builder.CreateCondBr(done, endBB, bodyBB);
51132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    currentElement->addIncoming(nextElement, Builder.GetInsertBlock());
51232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
51332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    CGF.EmitBlock(endBB);
51432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
51532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
51632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // Leave the partial-array cleanup if we entered one.
51732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (dtorKind) CGF.DeactivateCleanupBlock(cleanup, cleanupDominator);
51832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl}
51932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
520ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===//
521ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//                            Visitor Methods
522ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===//
523ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner
52403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregorvoid AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){
52503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  Visit(E->GetTemporaryExpr());
52603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor}
52703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
528e996ffd240f20a1048179d7727a6ee3227261921John McCallvoid AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) {
529e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  EmitFinalDestCopy(e->getType(), CGF.getOpaqueLValueMapping(e));
530e996ffd240f20a1048179d7727a6ee3227261921John McCall}
531e996ffd240f20a1048179d7727a6ee3227261921John McCall
532751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregorvoid
533751ec9be961888f14342fb63b39bf8727f0dee49Douglas GregorAggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
534673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor  if (E->getType().isPODType(CGF.getContext())) {
535673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor    // For a POD type, just emit a load of the lvalue + a copy, because our
536673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor    // compound literal might alias the destination.
537673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor    // FIXME: This is a band-aid; the real problem appears to be in our handling
538673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor    // of assignments, where we store directly into the LHS without checking
539673e98b4814642eb040a661fcc0f7953edd4c150Douglas Gregor    // whether anything in the RHS aliases.
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
548751ec9be961888f14342fb63b39bf8727f0dee49Douglas Gregor
5494d8673b645ad86e496b886a0f80b60763f67071dAnders Carlssonvoid AggExprEmitter::VisitCastExpr(CastExpr *E) {
5503016842613674ab80796567239c15d529aff1458Anders Carlsson  switch (E->getCastKind()) {
551575b374fdbfc2c2224fd3047ac11ffc4b8db9ae5Anders Carlsson  case CK_Dynamic: {
5522c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    // FIXME: Can this actually happen? We have no test coverage for it.
55369cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor    assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
5542c9f87ca5cccbfdaad82762368af5b2323320653Richard Smith    LValue LV = CGF.EmitCheckedLValue(E->getSubExpr(),
5557ac9ef1c82c779a5348ed11b3d10e01c58803b35Richard Smith                                      CodeGenFunction::TCK_Load);
55669cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor    // FIXME: Do we also need to handle property references here?
55769cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor    if (LV.isSimple())
55869cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor      CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
55969cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor    else
56069cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor      CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
56169cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor
562558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    if (!Dest.isIgnored())
563558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall      CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
56469cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor    break;
56569cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor  }
56669cfeb1036ad22c911b7243dca0eecee72e452d3Douglas Gregor
5672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ToUnion: {
5686591271275f7a3db357f3cb7af37ef86e800e4baJohn McCall    if (Dest.isIgnored()) break;
5696591271275f7a3db357f3cb7af37ef86e800e4baJohn McCall
5704d8673b645ad86e496b886a0f80b60763f67071dAnders Carlsson    // GCC union extension
57179c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar    QualType Ty = E->getSubExpr()->getType();
57279c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar    QualType PtrTy = CGF.getContext().getPointerType(Ty);
573558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall    llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(),
57434ebf4d1767e6748a1a59a5d1935c495cd8877e8Eli Friedman                                                 CGF.ConvertType(PtrTy));
575a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall    EmitInitializationToLValue(E->getSubExpr(),
576649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                               CGF.MakeAddrLValue(CastPtr, Ty));
5773016842613674ab80796567239c15d529aff1458Anders Carlsson    break;
5787e91627301b05cd8f2324795e19d87a62f444c31Nuno Lopes  }
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_DerivedToBase:
5812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_BaseToDerived:
5822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UncheckedDerivedToBase: {
583b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: "
5842d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor                "should have been unpacked before we got here");
5852d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor  }
5862d6b0e94db30c0e2754d270753c6f75478e451bfDouglas Gregor
587e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  case CK_LValueToRValue:
588e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    // If we're loading from a volatile type, force the destination
589e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    // into existence.
590e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    if (E->getSubExpr()->getType().isVolatileQualified()) {
591e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall      EnsureDest(E->getType());
592e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall      return Visit(E->getSubExpr());
593e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    }
594e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    // fallthrough
595e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
5962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_NoOp:
5977a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_AtomicToNonAtomic:
5987a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall  case CK_NonAtomicToAtomic:
5992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_UserDefinedConversion:
6002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_ConstructorConversion:
6013016842613674ab80796567239c15d529aff1458Anders Carlsson    assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
6023016842613674ab80796567239c15d529aff1458Anders Carlsson                                                   E->getType()) &&
6033016842613674ab80796567239c15d529aff1458Anders Carlsson           "Implicit cast types must be compatible");
6043016842613674ab80796567239c15d529aff1458Anders Carlsson    Visit(E->getSubExpr());
6053016842613674ab80796567239c15d529aff1458Anders Carlsson    break;
6060ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall
6072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  case CK_LValueBitCast:
6080ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall    llvm_unreachable("should not be emitting lvalue bitcast as rvalue");
6091de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall
6100ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_Dependent:
6110ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_BitCast:
6120ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_ArrayToPointerDecay:
6130ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_FunctionToPointerDecay:
6140ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_NullToPointer:
6150ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_NullToMemberPointer:
6160ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_BaseToDerivedMemberPointer:
6170ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_DerivedToBaseMemberPointer:
6180ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_MemberPointerToBoolean:
6194d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  case CK_ReinterpretMemberPointer:
6200ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_IntegralToPointer:
6210ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_PointerToIntegral:
6220ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_PointerToBoolean:
6230ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_ToVoid:
6240ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_VectorSplat:
6250ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_IntegralCast:
6260ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_IntegralToBoolean:
6270ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_IntegralToFloating:
6280ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_FloatingToIntegral:
6290ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_FloatingToBoolean:
6300ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_FloatingCast:
6311d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_CPointerToObjCPointerCast:
6321d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  case CK_BlockPointerToObjCPointerCast:
6330ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_AnyPointerToBlockPointerCast:
6340ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_ObjCObjectLValueCast:
6350ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_FloatingRealToComplex:
6360ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_FloatingComplexToReal:
6370ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_FloatingComplexToBoolean:
6380ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_FloatingComplexCast:
6390ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_FloatingComplexToIntegralComplex:
6400ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_IntegralRealToComplex:
6410ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_IntegralComplexToReal:
6420ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_IntegralComplexToBoolean:
6430ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_IntegralComplexCast:
6440ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall  case CK_IntegralComplexToFloatingComplex:
64533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCProduceObject:
64633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCConsumeObject:
64733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCReclaimReturnedObject:
64833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall  case CK_ARCExtendBlockObject:
649ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor  case CK_CopyAndAutoreleaseBlockObject:
650a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  case CK_BuiltinFnToFnPtr:
6510ae287a498b8cec2086fe6b7e753cbb3df63e74aJohn McCall    llvm_unreachable("cast kind invalid for aggregate types");
6523016842613674ab80796567239c15d529aff1458Anders Carlsson  }
653e4707ff0bb48add651c6a1ad9acfcb22609462d1Anders Carlsson}
654e4707ff0bb48add651c6a1ad9acfcb22609462d1Anders Carlsson
6559619662a1d42e2008b865d3459c0677e149dad1bChris Lattnervoid AggExprEmitter::VisitCallExpr(const CallExpr *E) {
656e70e8f7fef3efb3d526ee25b3a0e2a4bf67a04b6Anders Carlsson  if (E->getCallReturnType()->isReferenceType()) {
657e70e8f7fef3efb3d526ee25b3a0e2a4bf67a04b6Anders Carlsson    EmitAggLoadOfLValue(E);
658e70e8f7fef3efb3d526ee25b3a0e2a4bf67a04b6Anders Carlsson    return;
659e70e8f7fef3efb3d526ee25b3a0e2a4bf67a04b6Anders Carlsson  }
6601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
661fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
662410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall  EmitMoveFromReturnSlot(E, RV);
663796ef3d4a32ce8d9e76df0d5bcab07db97883064Nate Begeman}
6649619662a1d42e2008b865d3459c0677e149dad1bChris Lattner
6659619662a1d42e2008b865d3459c0677e149dad1bChris Lattnervoid AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
666fa037bd3f79d3c70197a3224bb1b29c6c4af0098John McCall  RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
667410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall  EmitMoveFromReturnSlot(E, RV);
6688fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner}
669796ef3d4a32ce8d9e76df0d5bcab07db97883064Nate Begeman
6709619662a1d42e2008b865d3459c0677e149dad1bChris Lattnervoid AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
6712a41637a995affa1563f4d82a8b026e326a2faa0John McCall  CGF.EmitIgnoredExpr(E->getLHS());
672558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  Visit(E->getRHS());
67307fa52ab33a75d7a5736ea5bd0d4e3134fb10c7eEli Friedman}
67407fa52ab33a75d7a5736ea5bd0d4e3134fb10c7eEli Friedman
675b2d963f527674275c9109252474948368b6e6161Chris Lattnervoid AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
676150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  CodeGenFunction::StmtExprEvaluation eval(CGF);
677558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
678b2d963f527674275c9109252474948368b6e6161Chris Lattner}
679b2d963f527674275c9109252474948368b6e6161Chris Lattner
6809c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattnervoid AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
6812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
6828bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian    VisitPointerToDataMemberBinaryOperator(E);
6838bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian  else
6848bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian    CGF.ErrorUnsupported(E, "aggregate binary expression");
6858bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian}
6868bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian
6878bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanianvoid AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
6888bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian                                                    const BinaryOperator *E) {
6898bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian  LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
690e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  EmitFinalDestCopy(E->getType(), LV);
691e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall}
692e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
693e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// Is the value of the given expression possibly a reference to or
694e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall/// into a __block variable?
695e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallstatic bool isBlockVarRef(const Expr *E) {
696e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Make sure we look through parens.
697e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  E = E->IgnoreParens();
698e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
699e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Check for a direct reference to a __block variable.
700e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
701e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
702e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    return (var && var->hasAttr<BlocksAttr>());
703e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  }
704e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
705e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // More complicated stuff.
706e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
707e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Binary operators.
708e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) {
709e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    // For an assignment or pointer-to-member operation, just care
710e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    // about the LHS.
711e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    if (op->isAssignmentOp() || op->isPtrMemOp())
712e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall      return isBlockVarRef(op->getLHS());
713e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
714e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    // For a comma, just care about the RHS.
715e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    if (op->getOpcode() == BO_Comma)
716e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall      return isBlockVarRef(op->getRHS());
717e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
718e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    // FIXME: pointer arithmetic?
719e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    return false;
720e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
721e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Check both sides of a conditional operator.
722e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  } else if (const AbstractConditionalOperator *op
723e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall               = dyn_cast<AbstractConditionalOperator>(E)) {
724e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    return isBlockVarRef(op->getTrueExpr())
725e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall        || isBlockVarRef(op->getFalseExpr());
726e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
727e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // OVEs are required to support BinaryConditionalOperators.
728e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  } else if (const OpaqueValueExpr *op
729e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall               = dyn_cast<OpaqueValueExpr>(E)) {
730e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    if (const Expr *src = op->getSourceExpr())
731e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall      return isBlockVarRef(src);
732e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
733e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Casts are necessary to get things like (*(int*)&var) = foo().
734e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // We don't really care about the kind of cast here, except
735e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // we don't want to look through l2r casts, because it's okay
736e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // to get the *value* in a __block variable.
737e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) {
738e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    if (cast->getCastKind() == CK_LValueToRValue)
739e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall      return false;
740e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    return isBlockVarRef(cast->getSubExpr());
741e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
742e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Handle unary operators.  Again, just aggressively look through
743e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // it, ignoring the operation.
744e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) {
745e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    return isBlockVarRef(uop->getSubExpr());
746e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
747e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Look into the base of a field access.
748e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) {
749e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    return isBlockVarRef(mem->getBase());
750e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
751e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Look into the base of a subscript.
752e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) {
753e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    return isBlockVarRef(sub->getBase());
754e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  }
755e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
756e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  return false;
757ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner}
758ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner
75903d6fb99224c36935c9af9f4785cb33453c99b2bChris Lattnervoid AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
760ff6e2b7d31b0f5494f583419e5061c32ea4e6180Eli Friedman  // For an assignment to work, the value on the right has
761ff6e2b7d31b0f5494f583419e5061c32ea4e6180Eli Friedman  // to be compatible with the value on the left.
7622dce5f8a99b5c48f1287ff3941288ca6f7fde2deEli Friedman  assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
7632dce5f8a99b5c48f1287ff3941288ca6f7fde2deEli Friedman                                                 E->getRHS()->getType())
764ff6e2b7d31b0f5494f583419e5061c32ea4e6180Eli Friedman         && "Invalid assignment");
765cd940a1e13e588a43973cd7ae33b5c33a3062739John McCall
766e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // If the LHS might be a __block variable, and the RHS can
767e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // potentially cause a block copy, we need to evaluate the RHS first
768e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // so that the assignment goes the right place.
769e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // This is pretty semantically fragile.
770e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  if (isBlockVarRef(E->getLHS()) &&
771e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall      E->getRHS()->HasSideEffects(CGF.getContext())) {
772e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    // Ensure that we have a destination, and evaluate the RHS into that.
773e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    EnsureDest(E->getRHS()->getType());
774e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    Visit(E->getRHS());
775e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
776e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    // Now emit the LHS and copy into it.
777e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    LValue LHS = CGF.EmitLValue(E->getLHS());
778e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
779e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    EmitCopy(E->getLHS()->getType(),
780e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall             AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
781e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                                     needsGC(E->getLHS()->getType()),
782e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                                     AggValueSlot::IsAliased),
783e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall             Dest);
784e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    return;
785e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  }
786649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier
7879c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  LValue LHS = CGF.EmitLValue(E->getLHS());
788883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner
789db45806b991013280a03057025c9538de64d5dfbJohn McCall  // Codegen the RHS so that it stores directly into the LHS.
790db45806b991013280a03057025c9538de64d5dfbJohn McCall  AggValueSlot LHSSlot =
791db45806b991013280a03057025c9538de64d5dfbJohn McCall    AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
792db45806b991013280a03057025c9538de64d5dfbJohn McCall                            needsGC(E->getLHS()->getType()),
793649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                            AggValueSlot::IsAliased);
794e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  CGF.EmitAggExpr(E->getRHS(), LHSSlot);
795e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall
796e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  // Copy into the destination if the assignment isn't ignored.
797e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  EmitFinalDestCopy(E->getType(), LHS);
798883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner}
799883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner
80056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallvoid AggExprEmitter::
80156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallVisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
8029615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar  llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
8039615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
8049615ecb44f549ae9fa2b4db6ff46bc78befbf62cDaniel Dunbar  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
8051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
80656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  // Bind the common expression if necessary.
807d97927d69b277120f8d403580c44acd84907d7b4Eli Friedman  CodeGenFunction::OpaqueValueMapping binding(CGF, E);
80856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
809150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  CodeGenFunction::ConditionalEvaluation eval(CGF);
8108e274bd14bcca8466542477844b88e90e90cde1aEli Friedman  CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81274fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall  // Save whether the destination's lifetime is externally managed.
813fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall  bool isExternallyDestructed = Dest.isExternallyDestructed();
814883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner
815150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  eval.begin(CGF);
816150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  CGF.EmitBlock(LHSBlock);
81756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  Visit(E->getTrueExpr());
818150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  eval.end(CGF);
8191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
820150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!");
821150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  CGF.Builder.CreateBr(ContBlock);
8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82374fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall  // If the result of an agg expression is unused, then the emission
82474fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall  // of the LHS might need to create a destination slot.  That's fine
82574fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall  // with us, and we can safely emit the RHS into the same slot, but
826fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall  // we shouldn't claim that it's already being destructed.
827fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall  Dest.setExternallyDestructed(isExternallyDestructed);
82874fb0edb44b7ed52af9b8053032ccaab29b5c0ccJohn McCall
829150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  eval.begin(CGF);
830150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  CGF.EmitBlock(RHSBlock);
83156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  Visit(E->getFalseExpr());
832150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  eval.end(CGF);
8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8349c03356918aee078e925c35f9854dfdf2492dfc3Chris Lattner  CGF.EmitBlock(ContBlock);
835883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner}
836ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner
837a294ca8c64fbb345f32e4af9d8fabdf2f64e4883Anders Carlssonvoid AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
838a294ca8c64fbb345f32e4af9d8fabdf2f64e4883Anders Carlsson  Visit(CE->getChosenSubExpr(CGF.getContext()));
839a294ca8c64fbb345f32e4af9d8fabdf2f64e4883Anders Carlsson}
840a294ca8c64fbb345f32e4af9d8fabdf2f64e4883Anders Carlsson
841b1851249d787f573b9e1312fff8ca4bbcf351f10Eli Friedmanvoid AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
8420785570af3ef5f8c5a0377129e41efe6f3f8d770Daniel Dunbar  llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
843ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
844ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
8450262f02cbaa35cafb61b1b994e0adff7c422a235Sebastian Redl  if (!ArgPtr) {
846ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson    CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
8470262f02cbaa35cafb61b1b994e0adff7c422a235Sebastian Redl    return;
8480262f02cbaa35cafb61b1b994e0adff7c422a235Sebastian Redl  }
8490262f02cbaa35cafb61b1b994e0adff7c422a235Sebastian Redl
850e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType()));
851b1851249d787f573b9e1312fff8ca4bbcf351f10Eli Friedman}
852b1851249d787f573b9e1312fff8ca4bbcf351f10Eli Friedman
853b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlssonvoid AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
854558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  // Ensure that we have a slot, but if we already do, remember
855fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall  // whether it was externally destructed.
856fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall  bool wasExternallyDestructed = Dest.isExternallyDestructed();
857e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  EnsureDest(E->getType());
858fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall
859fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall  // We're going to push a destructor if there isn't already one.
860fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall  Dest.setExternallyDestructed();
861558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall
862558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  Visit(E->getSubExpr());
863558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall
864fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall  // Push that destructor we promised.
865fd71fb81c5f9382caf0131946e890b133e12ceb5John McCall  if (!wasExternallyDestructed)
86686811609d9353e3aed198045d56e790eb3b6118cPeter Collingbourne    CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddr());
867b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlsson}
868b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlsson
869b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlssonvoid
87031ccf377f4a676eb6c205b47eef435de616d5e2dAnders CarlssonAggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
871558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  AggValueSlot Slot = EnsureSlot(E->getType());
872558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  CGF.EmitCXXConstructExpr(E, Slot);
8737f6ad153565245026c7569314f65a4d4ff4ac41fAnders Carlsson}
8747f6ad153565245026c7569314f65a4d4ff4ac41fAnders Carlsson
8754c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedmanvoid
8764c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli FriedmanAggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {
8774c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman  AggValueSlot Slot = EnsureSlot(E->getType());
8784c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman  CGF.EmitLambdaExpr(E, Slot);
8794c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman}
8804c5d8afd100189b6cce4fd89bfb8aec5700acb50Eli Friedman
8814765fa05b5652fcc4356371c2f481d0ea9a1b007John McCallvoid AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {
8821a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  CGF.enterFullExpression(E);
8831a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  CodeGenFunction::RunCleanupsScope cleanups(CGF);
8841a343ebbf413e8eae6b2737b2b2d79cbf5765571John McCall  Visit(E->getSubExpr());
885b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson}
886b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson
887ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregorvoid AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
888558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  QualType T = E->getType();
889558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  AggValueSlot Slot = EnsureSlot(T);
890a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
89130311fa6b0735b9cb73b01e25bf9652a4b9b0c53Anders Carlsson}
89230311fa6b0735b9cb73b01e25bf9652a4b9b0c53Anders Carlsson
89330311fa6b0735b9cb73b01e25bf9652a4b9b0c53Anders Carlssonvoid AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
894558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  QualType T = E->getType();
895558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  AggValueSlot Slot = EnsureSlot(T);
896a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
897329763b1e9ec8c216025e3a8379ed446d7372cbcNuno Lopes}
898329763b1e9ec8c216025e3a8379ed446d7372cbcNuno Lopes
8991b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// isSimpleZero - If emitting this value will obviously just cause a store of
9001b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// zero to memory, return true.  This can return false if uncertain, so it just
9011b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// handles simple cases.
9021b726771d00762fb5c4c2638e60d134c385493aeChris Lattnerstatic bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) {
903f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  E = E->IgnoreParens();
904f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
9051b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // 0
9061b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E))
9071b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    return IL->getValue() == 0;
9081b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // +0.0
9091b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E))
9101b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    return FL->getValue().isPosZero();
9111b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // int()
9121b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) &&
9131b726771d00762fb5c4c2638e60d134c385493aeChris Lattner      CGF.getTypes().isZeroInitializable(E->getType()))
9141b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    return true;
9151b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // (int*)0 - Null pointer expressions.
9161b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  if (const CastExpr *ICE = dyn_cast<CastExpr>(E))
9171b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    return ICE->getCastKind() == CK_NullToPointer;
9181b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // '\0'
9191b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E))
9201b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    return CL->getValue() == 0;
9211b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
9221b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // Otherwise, hard case: conservatively return false.
9231b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  return false;
9241b726771d00762fb5c4c2638e60d134c385493aeChris Lattner}
9251b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
9261b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
92778e83f881e59d4b8648a7b85ec6f2d36ef5cc680Anders Carlssonvoid
928649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad RosierAggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
929a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  QualType type = LV.getType();
9307f79f9be5916c51c35da4f126b7c12596a101607Mike Stump  // FIXME: Ignore result?
931f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  // FIXME: Are initializers affected by volatile?
9321b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  if (Dest.isZeroed() && isSimpleZero(E, CGF)) {
9331b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    // Storing "i32 0" to a zero'd memory location is a noop.
9341b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  } else if (isa<ImplicitValueInitExpr>(E)) {
935a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall    EmitNullInitializationToLValue(LV);
936a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  } else if (type->isReferenceType()) {
93732f36baa6c8d491c374af622b4e3ac28d597453cAnders Carlsson    RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
938545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    CGF.EmitStoreThroughLValue(RV, LV);
939a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  } else if (type->isAnyComplexType()) {
9403498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
941a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  } else if (CGF.hasAggregateLLVMType(type)) {
9427c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall    CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV,
9437c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall                                               AggValueSlot::IsDestructed,
9447c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall                                      AggValueSlot::DoesNotNeedGCBarriers,
945410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall                                               AggValueSlot::IsNotAliased,
946a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall                                               Dest.isZeroed()));
947f85e193739c953358c865005855253af4f68a497John McCall  } else if (LV.isSimple()) {
948a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall    CGF.EmitScalarInit(E, /*D=*/0, LV, /*Captured=*/false);
949c8ba9614ca5469c3ae259e3ec09792f4b8969397Eli Friedman  } else {
950545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV);
951f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  }
952f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner}
953305762c08975cd6e0bebd684ca910fa208792483Lauro Ramos Venancio
954a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCallvoid AggExprEmitter::EmitNullInitializationToLValue(LValue lv) {
955a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  QualType type = lv.getType();
956a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall
9571b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // If the destination slot is already zeroed out before the aggregate is
9581b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // copied into it, we don't have to emit any zeros here.
959a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type))
9601b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    return;
9611b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
962a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  if (!CGF.hasAggregateLLVMType(type)) {
963b1e3f324b0c4d17399609c246918dadcb886d739Eli Friedman    // For non-aggregates, we can store zero.
964a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall    llvm::Value *null = llvm::Constant::getNullValue(CGF.ConvertType(type));
965b1e3f324b0c4d17399609c246918dadcb886d739Eli Friedman    // Note that the following is not equivalent to
966b1e3f324b0c4d17399609c246918dadcb886d739Eli Friedman    // EmitStoreThroughBitfieldLValue for ARC types.
9675a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman    if (lv.isBitField()) {
968b1e3f324b0c4d17399609c246918dadcb886d739Eli Friedman      CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv);
9695a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman    } else {
9705a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman      assert(lv.isSimple());
9715a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman      CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true);
9725a13d4d2c1e45ab611ca857d923caacfaeb3cd07Eli Friedman    }
973f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  } else {
974f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner    // There's a potential optimization opportunity in combining
975f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner    // memsets; that would be easy for arrays, but relatively
976f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner    // difficult for structures with the current code.
977a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall    CGF.EmitNullInitialization(lv.getAddress(), lv.getType());
978f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  }
979f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner}
980305762c08975cd6e0bebd684ca910fa208792483Lauro Ramos Venancio
981f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattnervoid AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
982a385b3c9c442831fc6a9ec6e8823b2067bd65710Eli Friedman#if 0
98313a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman  // FIXME: Assess perf here?  Figure out what cases are worth optimizing here
98413a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman  // (Length of globals? Chunks of zeroed-out space?).
985a385b3c9c442831fc6a9ec6e8823b2067bd65710Eli Friedman  //
986f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // If we can, prefer a copy from a global; this is a lot less code for long
987f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // globals, and it's easier for the current optimizers to analyze.
98813a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman  if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
989994ffef4353056363ba5915eeecf0e1b0678f286Eli Friedman    llvm::GlobalVariable* GV =
99013a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman    new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
99113a5be10b198a5dc7e3e72c54481cd8b70f68495Eli Friedman                             llvm::GlobalValue::InternalLinkage, C, "");
992e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType()));
993994ffef4353056363ba5915eeecf0e1b0678f286Eli Friedman    return;
994994ffef4353056363ba5915eeecf0e1b0678f286Eli Friedman  }
995a385b3c9c442831fc6a9ec6e8823b2067bd65710Eli Friedman#endif
996d0db03a561671b8b466b07026cc8fbbb037bb639Chris Lattner  if (E->hadArrayRangeDesignator())
997a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor    CGF.ErrorUnsupported(E, "GNU array range designator extension");
998a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor
99932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (E->initializesStdInitializerList()) {
1000af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    EmitStdInitializerList(Dest.getAddr(), E);
100132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    return;
100232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
100332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
1004377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman  AggValueSlot Dest = EnsureSlot(E->getType());
1005377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman  LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
1006377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman                                     Dest.getAlignment());
1007558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall
1008f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  // Handle initialization of an array.
1009f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  if (E->getType()->isArrayType()) {
1010fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith    if (E->isStringLiteralInit())
1011fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith      return Visit(E->getInit(0));
1012922696f03ec9637449e2cba260493808b4977cd3Eli Friedman
10135c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman    QualType elementType =
10145c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman        CGF.getContext().getAsArrayType(E->getType())->getElementType();
1015bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
101632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::PointerType *APType =
1017377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman      cast<llvm::PointerType>(Dest.getAddr()->getType());
101832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    llvm::ArrayType *AType =
101932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      cast<llvm::ArrayType>(APType->getElementType());
1020bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1021377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman    EmitArrayInit(Dest.getAddr(), AType, elementType, E);
1022305762c08975cd6e0bebd684ca910fa208792483Lauro Ramos Venancio    return;
1023f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  }
10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1025f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  assert(E->getType()->isRecordType() && "Only support structs/unions here!");
10261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1027f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  // Do struct initialization; this code just sets each individual member
1028f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  // to the approprate value.  This makes bitfield support automatic;
1029f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  // the disadvantage is that the generated code is more difficult for
1030f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  // the optimizer, especially with bitfields.
1031f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  unsigned NumInitElements = E->getNumInits();
10322b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall  RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl();
1033bd7de38eae1fc20ee88db63f469c54241bc240f8Chris Lattner
10342b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall  if (record->isUnion()) {
10350bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    // Only initialize one field of a union. The field itself is
10360bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    // specified by the initializer list.
10370bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (!E->getInitializedFieldInUnion()) {
10380bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      // Empty union; we have nothing to do.
10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10400bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor#ifndef NDEBUG
10410bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      // Make sure that it's really an empty and not a failure of
10420bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      // semantic analysis.
10432b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall      for (RecordDecl::field_iterator Field = record->field_begin(),
10442b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall                                   FieldEnd = record->field_end();
10450bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor           Field != FieldEnd; ++Field)
10460bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor        assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
10470bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor#endif
10480bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      return;
10490bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
10500bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor
10510bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    // FIXME: volatility
10520bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    FieldDecl *Field = E->getInitializedFieldInUnion();
10530bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor
1054377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman    LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field);
10550bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (NumInitElements) {
10560bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      // Store the initializer into the field
1057649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      EmitInitializationToLValue(E->getInit(0), FieldLoc);
10580bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    } else {
10591b726771d00762fb5c4c2638e60d134c385493aeChris Lattner      // Default-initialize to null.
1060a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall      EmitNullInitializationToLValue(FieldLoc);
10610bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
10620bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor
10630bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    return;
10640bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor  }
10651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10662b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall  // We'll need to enter cleanup scopes in case any of the member
10672b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall  // initializers throw an exception.
10685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<EHScopeStack::stable_iterator, 16> cleanups;
10696f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  llvm::Instruction *cleanupDominator = 0;
10702b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall
1071f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  // Here we iterate over the fields; this makes it simpler to both
1072f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner  // default-initialize fields and skip over unnamed fields.
10732b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall  unsigned curInitIndex = 0;
10742b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall  for (RecordDecl::field_iterator field = record->field_begin(),
10752b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall                               fieldEnd = record->field_end();
10762b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall       field != fieldEnd; ++field) {
10772b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    // We're done once we hit the flexible array member.
10782b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    if (field->getType()->isIncompleteArrayType())
107944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      break;
108044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
10812b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    // Always skip anonymous bitfields.
10822b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    if (field->isUnnamedBitfield())
1083f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner      continue;
108434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
10852b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    // We're done if we reach the end of the explicit initializers, we
10862b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    // have a zeroed object, and the rest of the fields are
10872b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    // zero-initializable.
10882b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    if (curInitIndex == NumInitElements && Dest.isZeroed() &&
10891b726771d00762fb5c4c2638e60d134c385493aeChris Lattner        CGF.getTypes().isZeroInitializable(E->getType()))
10901b726771d00762fb5c4c2638e60d134c385493aeChris Lattner      break;
10911b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
1092377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman
1093581deb3da481053c4993c7600f97acf7768caac5David Blaikie    LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, *field);
109414674ffb81dccbc4e1bf78ab5b7987685819b445Fariborz Jahanian    // We never generate write-barries for initialized fields.
10952b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    LV.setNonGC(true);
10961b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
10972b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    if (curInitIndex < NumInitElements) {
1098b35baae19b906245b5c2266b47ef411abcc6b25aChris Lattner      // Store the initializer into the field.
1099649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      EmitInitializationToLValue(E->getInit(curInitIndex++), LV);
1100f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner    } else {
1101f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner      // We're out of initalizers; default-initialize to null
11022b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall      EmitNullInitializationToLValue(LV);
11032b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    }
11042b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall
11052b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    // Push a destructor if necessary.
11062b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    // FIXME: if we have an array of structures, all explicitly
11072b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    // initialized, we can end up pushing a linear number of cleanups.
11082b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    bool pushedCleanup = false;
11092b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    if (QualType::DestructionKind dtorKind
11102b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall          = field->getType().isDestructedType()) {
11112b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall      assert(LV.isSimple());
11122b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall      if (CGF.needsEHCleanup(dtorKind)) {
11136f103ba42cb69d50005a977c5ea583984ab63fc4John McCall        if (!cleanupDominator)
11146f103ba42cb69d50005a977c5ea583984ab63fc4John McCall          cleanupDominator = CGF.Builder.CreateUnreachable(); // placeholder
11156f103ba42cb69d50005a977c5ea583984ab63fc4John McCall
11162b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall        CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(),
11172b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall                        CGF.getDestroyer(dtorKind), false);
11182b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall        cleanups.push_back(CGF.EHStack.stable_begin());
11192b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall        pushedCleanup = true;
11202b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall      }
1121f81557cb719dd0d1ce3713f050fb76b0a0cb729aChris Lattner    }
11221b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
11231b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    // If the GEP didn't get used because of a dead zero init or something
11241b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    // else, clean it up for -O0 builds and general tidiness.
11252b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall    if (!pushedCleanup && LV.isSimple())
11261b726771d00762fb5c4c2638e60d134c385493aeChris Lattner      if (llvm::GetElementPtrInst *GEP =
11272b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall            dyn_cast<llvm::GetElementPtrInst>(LV.getAddress()))
11281b726771d00762fb5c4c2638e60d134c385493aeChris Lattner        if (GEP->use_empty())
11291b726771d00762fb5c4c2638e60d134c385493aeChris Lattner          GEP->eraseFromParent();
1130145cd89f9233d375381aa13bd28b2d36f83e6181Lauro Ramos Venancio  }
11312b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall
11322b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall  // Deactivate all the partial cleanups in reverse order, which
11332b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall  // generally means popping them.
11342b30dcfb769e4015f8d41c5c9a4e723ff3d522a0John McCall  for (unsigned i = cleanups.size(); i != 0; --i)
11356f103ba42cb69d50005a977c5ea583984ab63fc4John McCall    CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator);
11366f103ba42cb69d50005a977c5ea583984ab63fc4John McCall
11376f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  // Destroy the placeholder if we made one.
11386f103ba42cb69d50005a977c5ea583984ab63fc4John McCall  if (cleanupDominator)
11396f103ba42cb69d50005a977c5ea583984ab63fc4John McCall    cleanupDominator->eraseFromParent();
1140636c3d04673da8c8605d7e45640a2ff7aec648f1Devang Patel}
1141636c3d04673da8c8605d7e45640a2ff7aec648f1Devang Patel
1142ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===//
1143ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//                        Entry Points into this File
1144ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner//===----------------------------------------------------------------------===//
1145ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner
11461b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// GetNumNonZeroBytesInInit - Get an approximate count of the number of
11471b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// non-zero bytes that will be stored when outputting the initializer for the
11481b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// specified initializer expression.
114902c45333b8310bb792a15f85f219706025f9752cKen Dyckstatic CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
1150f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  E = E->IgnoreParens();
11511b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
11521b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // 0 and 0.0 won't require any non-zero stores!
115302c45333b8310bb792a15f85f219706025f9752cKen Dyck  if (isSimpleZero(E, CGF)) return CharUnits::Zero();
11541b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
11551b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // If this is an initlist expr, sum up the size of sizes of the (present)
11561b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // elements.  If this is something weird, assume the whole thing is non-zero.
11571b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  const InitListExpr *ILE = dyn_cast<InitListExpr>(E);
11581b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType()))
115902c45333b8310bb792a15f85f219706025f9752cKen Dyck    return CGF.getContext().getTypeSizeInChars(E->getType());
11601b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
1161d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner  // InitListExprs for structs have to be handled carefully.  If there are
1162d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner  // reference members, we need to consider the size of the reference, not the
1163d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner  // referencee.  InitListExprs for unions and arrays can't have references.
11648c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner  if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
11658c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner    if (!RT->isUnionType()) {
11668c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner      RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
116702c45333b8310bb792a15f85f219706025f9752cKen Dyck      CharUnits NumNonZeroBytes = CharUnits::Zero();
1168d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner
11698c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner      unsigned ILEElement = 0;
11708c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner      for (RecordDecl::field_iterator Field = SD->field_begin(),
11718c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner           FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) {
11728c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner        // We're done once we hit the flexible array member or run out of
11738c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner        // InitListExpr elements.
11748c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner        if (Field->getType()->isIncompleteArrayType() ||
11758c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner            ILEElement == ILE->getNumInits())
11768c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner          break;
11778c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner        if (Field->isUnnamedBitfield())
11788c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner          continue;
11798c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner
11808c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner        const Expr *E = ILE->getInit(ILEElement++);
11818c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner
11828c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner        // Reference values are always non-null and have the width of a pointer.
11838c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner        if (Field->getType()->isReferenceType())
118402c45333b8310bb792a15f85f219706025f9752cKen Dyck          NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits(
1185bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor              CGF.getContext().getTargetInfo().getPointerWidth(0));
11868c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner        else
11878c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner          NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
11888c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner      }
1189d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner
11908c00ad1e3897e8a00f41bbd52135be8390d5c15cChris Lattner      return NumNonZeroBytes;
1191d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner    }
1192d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner  }
1193d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner
1194d1d56df188e25c633f9bc65d229897b42442b0f7Chris Lattner
119502c45333b8310bb792a15f85f219706025f9752cKen Dyck  CharUnits NumNonZeroBytes = CharUnits::Zero();
11961b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
11971b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);
11981b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  return NumNonZeroBytes;
11991b726771d00762fb5c4c2638e60d134c385493aeChris Lattner}
12001b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
12011b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// CheckAggExprForMemSetUse - If the initializer is large and has a lot of
12021b726771d00762fb5c4c2638e60d134c385493aeChris Lattner/// zeros in it, emit a memset and avoid storing the individual zeros.
12031b726771d00762fb5c4c2638e60d134c385493aeChris Lattner///
12041b726771d00762fb5c4c2638e60d134c385493aeChris Lattnerstatic void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
12051b726771d00762fb5c4c2638e60d134c385493aeChris Lattner                                     CodeGenFunction &CGF) {
12061b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // If the slot is already known to be zeroed, nothing to do.  Don't mess with
12071b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // volatile stores.
12081b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return;
1209657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis
1210657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis  // C++ objects with a user-declared constructor don't need zero'ing.
12114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGF.getContext().getLangOpts().CPlusPlus)
1212657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis    if (const RecordType *RT = CGF.getContext()
1213657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis                       .getBaseElementType(E->getType())->getAs<RecordType>()) {
1214657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis      const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1215657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis      if (RD->hasUserDeclaredConstructor())
1216657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis        return;
1217657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis    }
1218657baf19ca8a48a926bd3bc148b6ad1b17e53199Argyrios Kyrtzidis
12191b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // If the type is 16-bytes or smaller, prefer individual stores over memset.
12205ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck  std::pair<CharUnits, CharUnits> TypeInfo =
12215ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck    CGF.getContext().getTypeInfoInChars(E->getType());
12225ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck  if (TypeInfo.first <= CharUnits::fromQuantity(16))
12231b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    return;
12241b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
12251b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // Check to see if over 3/4 of the initializer are known to be zero.  If so,
12261b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // we prefer to emit memset + individual stores for the rest.
12275ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck  CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF);
12285ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck  if (NumNonZeroBytes*4 > TypeInfo.first)
12291b726771d00762fb5c4c2638e60d134c385493aeChris Lattner    return;
12301b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
12311b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // Okay, it seems like a good idea to use an initial memset, emit the call.
12325ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck  llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity());
12335ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck  CharUnits Align = TypeInfo.second;
12341b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
12351b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  llvm::Value *Loc = Slot.getAddr();
12361b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
12378b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  Loc = CGF.Builder.CreateBitCast(Loc, CGF.Int8PtrTy);
12385ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck  CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal,
12395ff1a3508b39cfe3c9d108679a6532d85586b5ceKen Dyck                           Align.getQuantity(), false);
12401b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
12411b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // Tell the AggExprEmitter that the slot is known zero.
12421b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  Slot.setZeroed();
12431b726771d00762fb5c4c2638e60d134c385493aeChris Lattner}
12441b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
12451b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
12461b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
12471b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
1248e1129a92b25f9b05f1b97fdd81d38ea451875414Mike Stump/// EmitAggExpr - Emit the computation of the specified expression of aggregate
1249e1129a92b25f9b05f1b97fdd81d38ea451875414Mike Stump/// type.  The result is computed into DestPtr.  Note that if DestPtr is null,
1250e1129a92b25f9b05f1b97fdd81d38ea451875414Mike Stump/// the value of the aggregate expression is not needed.  If VolatileDest is
1251e1129a92b25f9b05f1b97fdd81d38ea451875414Mike Stump/// true, DestPtr cannot be 0.
1252e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCallvoid CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) {
1253ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner  assert(E && hasAggregateLLVMType(E->getType()) &&
1254ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner         "Invalid aggregate expression to emit");
12551b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  assert((Slot.getAddr() != 0 || Slot.isIgnored()) &&
12561b726771d00762fb5c4c2638e60d134c385493aeChris Lattner         "slot has bits but no address");
12571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12581b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  // Optimize the slot if possible.
12591b726771d00762fb5c4c2638e60d134c385493aeChris Lattner  CheckAggExprForMemSetUse(Slot, E, *this);
12601b726771d00762fb5c4c2638e60d134c385493aeChris Lattner
1261e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  AggExprEmitter(*this, Slot).Visit(const_cast<Expr*>(E));
1262ee755f9118c4061b21e0a787d4a20484df36f603Chris Lattner}
12637482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar
126418aba0dd518e486d8b50523e7dafb4b5657135d2Daniel DunbarLValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
126518aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!");
1266195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  llvm::Value *Temp = CreateMemTemp(E->getType());
126779c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar  LValue LV = MakeAddrLValue(Temp, E->getType());
12687c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall  EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed,
12694418439220a8f8e0b1deffdccce2354854c702f5John McCall                                         AggValueSlot::DoesNotNeedGCBarriers,
1270649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                         AggValueSlot::IsNotAliased));
127179c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar  return LV;
127218aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar}
127318aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar
1274649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosiervoid CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
1275649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                        llvm::Value *SrcPtr, QualType Ty,
1276e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                                        bool isVolatile,
1277e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                                        CharUnits alignment) {
1278649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
12791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getContext().getLangOpts().CPlusPlus) {
1281649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier    if (const RecordType *RT = Ty->getAs<RecordType>()) {
1282649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
1283649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      assert((Record->hasTrivialCopyConstructor() ||
1284649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier              Record->hasTrivialCopyAssignment() ||
1285649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier              Record->hasTrivialMoveConstructor() ||
1286649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier              Record->hasTrivialMoveAssignment()) &&
1287e9979484670ca2b528146d1b681e149fdc29f7ccDouglas Gregor             "Trying to aggregate-copy a type without a trivial copy "
1288e9979484670ca2b528146d1b681e149fdc29f7ccDouglas Gregor             "constructor or assignment operator");
1289649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      // Ignore empty classes in C++.
1290649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      if (Record->isEmpty())
12910d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson        return;
12920d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson    }
12930d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson  }
12940d7c583a4b4d0f57c6b69c66fd73babec4ef3799Anders Carlsson
1295649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // Aggregate assignment turns into llvm.memcpy.  This is almost valid per
1296649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // C99 6.5.16.1p3, which states "If the value being stored in an object is
1297649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // read from another object that overlaps in anyway the storage of the first
1298649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // object, then the overlap shall be exact and the two objects shall have
1299649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // qualified or unqualified versions of a compatible type."
1300649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  //
1301649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // memcpy is not defined if the source and destination pointers are exactly
1302649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // equal, but other compilers do this optimization, and almost every memcpy
1303649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // implementation handles this case safely.  If there is a libc that does not
1304649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // safely handle this, we can add a target hook.
1305649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier
1306929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  // Get data size and alignment info for this aggregate.
1307649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  std::pair<CharUnits, CharUnits> TypeInfo =
1308929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall    getContext().getTypeInfoDataSizeInChars(Ty);
1309649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier
1310e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall  if (alignment.isZero())
1311e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall    alignment = TypeInfo.second;
1312649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier
1313649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // FIXME: Handle variable sized types.
1314649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier
1315649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // FIXME: If we have a volatile struct, the optimizer can remove what might
1316649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // appear to be `extra' memory ops:
1317649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  //
1318649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // volatile struct { int i; } a, b;
1319649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  //
1320649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // int main() {
1321649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  //   a = b;
1322649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  //   a = b;
1323649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // }
1324649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  //
1325649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // we need to use a different call here.  We use isVolatile to indicate when
1326649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  // either the source or the destination is volatile.
1327649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier
1328649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
13292acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *DBP =
1330d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace());
1331649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  DestPtr = Builder.CreateBitCast(DestPtr, DBP);
13323ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang
1333649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
13342acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *SBP =
1335d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace());
1336649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  SrcPtr = Builder.CreateBitCast(SrcPtr, SBP);
13373ecd785aff34381f3704d9cb28fe3ef85af759deMon P Wang
1338f85e193739c953358c865005855253af4f68a497John McCall  // Don't do any of the memmove_collectable tests if GC isn't set.
13394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
1340f85e193739c953358c865005855253af4f68a497John McCall    // fall through
1341649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1342649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier    RecordDecl *Record = RecordTy->getDecl();
1343649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier    if (Record->hasObjectMember()) {
1344649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      CharUnits size = TypeInfo.first;
1345649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
1346649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity());
1347649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
1348649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                                    SizeVal);
134955bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian      return;
135055bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian    }
1351649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  } else if (Ty->isArrayType()) {
1352649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier    QualType BaseType = getContext().getBaseElementType(Ty);
1353649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier    if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
1354649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier      if (RecordTy->getDecl()->hasObjectMember()) {
1355649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier        CharUnits size = TypeInfo.first;
1356649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier        llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
1357649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier        llvm::Value *SizeVal =
1358649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier          llvm::ConstantInt::get(SizeTy, size.getQuantity());
1359649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier        CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
1360649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                                      SizeVal);
136155bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian        return;
136255bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian      }
136355bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian    }
136455bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian  }
136555bcace250e1ff366e4482714b344b8cbc8be5f3Fariborz Jahanian
1366649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier  Builder.CreateMemCpy(DestPtr, SrcPtr,
1367649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                       llvm::ConstantInt::get(IntPtrTy,
1368649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier                                              TypeInfo.first.getQuantity()),
1369e0c1168ec7910a1a7ed08df4d4f0c58c2fa2ecd1John McCall                       alignment.getQuantity(), isVolatile);
13707482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar}
137132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
1372972edf0534d8a50f87fac1d0ff34eb22f593df11Sebastian Redlvoid CodeGenFunction::MaybeEmitStdInitializerListCleanup(llvm::Value *loc,
1373972edf0534d8a50f87fac1d0ff34eb22f593df11Sebastian Redl                                                         const Expr *init) {
137432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(init);
1375972edf0534d8a50f87fac1d0ff34eb22f593df11Sebastian Redl  if (cleanups)
1376972edf0534d8a50f87fac1d0ff34eb22f593df11Sebastian Redl    init = cleanups->getSubExpr();
137732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
137832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  if (isa<InitListExpr>(init) &&
137932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      cast<InitListExpr>(init)->initializesStdInitializerList()) {
138032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // We initialized this std::initializer_list with an initializer list.
138132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl    // A backing array was created. Push a cleanup for it.
1382972edf0534d8a50f87fac1d0ff34eb22f593df11Sebastian Redl    EmitStdInitializerListCleanup(loc, cast<InitListExpr>(init));
1383af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  }
1384af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl}
1385af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl
1386af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redlstatic void EmitRecursiveStdInitializerListCleanup(CodeGenFunction &CGF,
1387af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl                                                   llvm::Value *arrayStart,
1388af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl                                                   const InitListExpr *init) {
1389af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  // Check if there are any recursive cleanups to do, i.e. if we have
1390af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  //   std::initializer_list<std::initializer_list<obj>> list = {{obj()}};
1391af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  // then we need to destroy the inner array as well.
1392af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  for (unsigned i = 0, e = init->getNumInits(); i != e; ++i) {
1393af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    const InitListExpr *subInit = dyn_cast<InitListExpr>(init->getInit(i));
1394af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    if (!subInit || !subInit->initializesStdInitializerList())
1395af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl      continue;
1396af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl
1397af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    // This one needs to be destroyed. Get the address of the std::init_list.
1398af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    llvm::Value *offset = llvm::ConstantInt::get(CGF.SizeTy, i);
1399af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    llvm::Value *loc = CGF.Builder.CreateInBoundsGEP(arrayStart, offset,
1400af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl                                                 "std.initlist");
1401af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl    CGF.EmitStdInitializerListCleanup(loc, subInit);
140232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  }
140332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl}
140432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
1405af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redlvoid CodeGenFunction::EmitStdInitializerListCleanup(llvm::Value *loc,
140632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl                                                    const InitListExpr *init) {
140732cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  ASTContext &ctx = getContext();
140832cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  QualType element = GetStdInitializerListElementType(init->getType());
140932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  unsigned numInits = init->getNumInits();
141032cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::APInt size(ctx.getTypeSize(ctx.getSizeType()), numInits);
141132cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  QualType array =ctx.getConstantArrayType(element, size, ArrayType::Normal, 0);
141232cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  QualType arrayPtr = ctx.getPointerType(array);
141332cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  llvm::Type *arrayPtrType = ConvertType(arrayPtr);
141432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
141532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // lvalue is the location of a std::initializer_list, which as its first
141632cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  // element has a pointer to the array we want to destroy.
1417af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  llvm::Value *startPointer = Builder.CreateStructGEP(loc, 0, "startPointer");
1418af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  llvm::Value *startAddress = Builder.CreateLoad(startPointer, "startAddress");
141932cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl
1420af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  ::EmitRecursiveStdInitializerListCleanup(*this, startAddress, init);
1421af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl
1422af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl  llvm::Value *arrayAddress =
1423af130fd78267ee9e2395b758a7d827b07ce317a0Sebastian Redl      Builder.CreateBitCast(startAddress, arrayPtrType, "arrayAddress");
142432cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl  ::EmitStdInitializerListCleanup(*this, array, arrayAddress, init);
142532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl}
1426