1f22ef01cSRoman Divacky //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This contains code to emit Aggregate Expr nodes as LLVM code.
11f22ef01cSRoman Divacky //
12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
13f22ef01cSRoman Divacky 
14f22ef01cSRoman Divacky #include "CodeGenFunction.h"
15f22ef01cSRoman Divacky #include "CGObjCRuntime.h"
16139f7f9bSDimitry Andric #include "CodeGenModule.h"
17f22ef01cSRoman Divacky #include "clang/AST/ASTContext.h"
18f22ef01cSRoman Divacky #include "clang/AST/DeclCXX.h"
19dff0c46cSDimitry Andric #include "clang/AST/DeclTemplate.h"
20f22ef01cSRoman Divacky #include "clang/AST/StmtVisitor.h"
21139f7f9bSDimitry Andric #include "llvm/IR/Constants.h"
22139f7f9bSDimitry Andric #include "llvm/IR/Function.h"
23139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h"
24139f7f9bSDimitry Andric #include "llvm/IR/Intrinsics.h"
25f22ef01cSRoman Divacky using namespace clang;
26f22ef01cSRoman Divacky using namespace CodeGen;
27f22ef01cSRoman Divacky 
28f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
29f22ef01cSRoman Divacky //                        Aggregate Expression Emitter
30f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
31f22ef01cSRoman Divacky 
32f22ef01cSRoman Divacky namespace  {
33f22ef01cSRoman Divacky class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
34f22ef01cSRoman Divacky   CodeGenFunction &CGF;
35f22ef01cSRoman Divacky   CGBuilderTy &Builder;
362754fe60SDimitry Andric   AggValueSlot Dest;
3797bc6c73SDimitry Andric   bool IsResultUnused;
38f22ef01cSRoman Divacky 
396122f3e6SDimitry Andric   /// We want to use 'dest' as the return slot except under two
406122f3e6SDimitry Andric   /// conditions:
416122f3e6SDimitry Andric   ///   - The destination slot requires garbage collection, so we
426122f3e6SDimitry Andric   ///     need to use the GC API.
436122f3e6SDimitry Andric   ///   - The destination slot is potentially aliased.
446122f3e6SDimitry Andric   bool shouldUseDestForReturnSlot() const {
456122f3e6SDimitry Andric     return !(Dest.requiresGCollection() || Dest.isPotentiallyAliased());
466122f3e6SDimitry Andric   }
476122f3e6SDimitry Andric 
48f22ef01cSRoman Divacky   ReturnValueSlot getReturnValueSlot() const {
496122f3e6SDimitry Andric     if (!shouldUseDestForReturnSlot())
506122f3e6SDimitry Andric       return ReturnValueSlot();
51f22ef01cSRoman Divacky 
520623d748SDimitry Andric     return ReturnValueSlot(Dest.getAddress(), Dest.isVolatile(),
530623d748SDimitry Andric                            IsResultUnused);
542754fe60SDimitry Andric   }
552754fe60SDimitry Andric 
562754fe60SDimitry Andric   AggValueSlot EnsureSlot(QualType T) {
572754fe60SDimitry Andric     if (!Dest.isIgnored()) return Dest;
582754fe60SDimitry Andric     return CGF.CreateAggTemp(T, "agg.tmp.ensured");
59f22ef01cSRoman Divacky   }
607ae0e2c9SDimitry Andric   void EnsureDest(QualType T) {
617ae0e2c9SDimitry Andric     if (!Dest.isIgnored()) return;
627ae0e2c9SDimitry Andric     Dest = CGF.CreateAggTemp(T, "agg.tmp.ensured");
637ae0e2c9SDimitry Andric   }
64f22ef01cSRoman Divacky 
65f22ef01cSRoman Divacky public:
6697bc6c73SDimitry Andric   AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest, bool IsResultUnused)
6797bc6c73SDimitry Andric     : CGF(cgf), Builder(CGF.Builder), Dest(Dest),
6897bc6c73SDimitry Andric     IsResultUnused(IsResultUnused) { }
69f22ef01cSRoman Divacky 
70f22ef01cSRoman Divacky   //===--------------------------------------------------------------------===//
71f22ef01cSRoman Divacky   //                               Utilities
72f22ef01cSRoman Divacky   //===--------------------------------------------------------------------===//
73f22ef01cSRoman Divacky 
74f22ef01cSRoman Divacky   /// EmitAggLoadOfLValue - Given an expression with aggregate type that
75f22ef01cSRoman Divacky   /// represents a value lvalue, this method emits the address of the lvalue,
76f22ef01cSRoman Divacky   /// then loads the result into DestPtr.
77f22ef01cSRoman Divacky   void EmitAggLoadOfLValue(const Expr *E);
78f22ef01cSRoman Divacky 
79f22ef01cSRoman Divacky   /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
807ae0e2c9SDimitry Andric   void EmitFinalDestCopy(QualType type, const LValue &src);
810623d748SDimitry Andric   void EmitFinalDestCopy(QualType type, RValue src);
827ae0e2c9SDimitry Andric   void EmitCopy(QualType type, const AggValueSlot &dest,
837ae0e2c9SDimitry Andric                 const AggValueSlot &src);
84f22ef01cSRoman Divacky 
856122f3e6SDimitry Andric   void EmitMoveFromReturnSlot(const Expr *E, RValue Src);
866122f3e6SDimitry Andric 
870623d748SDimitry Andric   void EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
88dff0c46cSDimitry Andric                      QualType elementType, InitListExpr *E);
89dff0c46cSDimitry Andric 
906122f3e6SDimitry Andric   AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) {
91dff0c46cSDimitry Andric     if (CGF.getLangOpts().getGC() && TypeRequiresGCollection(T))
926122f3e6SDimitry Andric       return AggValueSlot::NeedsGCBarriers;
936122f3e6SDimitry Andric     return AggValueSlot::DoesNotNeedGCBarriers;
946122f3e6SDimitry Andric   }
95f22ef01cSRoman Divacky 
96f22ef01cSRoman Divacky   bool TypeRequiresGCollection(QualType T);
97f22ef01cSRoman Divacky 
98f22ef01cSRoman Divacky   //===--------------------------------------------------------------------===//
99f22ef01cSRoman Divacky   //                            Visitor Methods
100f22ef01cSRoman Divacky   //===--------------------------------------------------------------------===//
101f22ef01cSRoman Divacky 
10233956c43SDimitry Andric   void Visit(Expr *E) {
10333956c43SDimitry Andric     ApplyDebugLocation DL(CGF, E);
10433956c43SDimitry Andric     StmtVisitor<AggExprEmitter>::Visit(E);
10533956c43SDimitry Andric   }
10633956c43SDimitry Andric 
107f22ef01cSRoman Divacky   void VisitStmt(Stmt *S) {
108f22ef01cSRoman Divacky     CGF.ErrorUnsupported(S, "aggregate expression");
109f22ef01cSRoman Divacky   }
110f22ef01cSRoman Divacky   void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
1113b0f4066SDimitry Andric   void VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
1123b0f4066SDimitry Andric     Visit(GE->getResultExpr());
1133b0f4066SDimitry Andric   }
114f22ef01cSRoman Divacky   void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
11517a519f9SDimitry Andric   void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
11617a519f9SDimitry Andric     return Visit(E->getReplacement());
11717a519f9SDimitry Andric   }
118f22ef01cSRoman Divacky 
119f22ef01cSRoman Divacky   // l-values.
120dff0c46cSDimitry Andric   void VisitDeclRefExpr(DeclRefExpr *E) {
121dff0c46cSDimitry Andric     // For aggregates, we should always be able to emit the variable
122dff0c46cSDimitry Andric     // as an l-value unless it's a reference.  This is due to the fact
123dff0c46cSDimitry Andric     // that we can't actually ever see a normal l2r conversion on an
124dff0c46cSDimitry Andric     // aggregate in C++, and in C there's no language standard
125dff0c46cSDimitry Andric     // actively preventing us from listing variables in the captures
126dff0c46cSDimitry Andric     // list of a block.
127dff0c46cSDimitry Andric     if (E->getDecl()->getType()->isReferenceType()) {
128dff0c46cSDimitry Andric       if (CodeGenFunction::ConstantEmission result
129dff0c46cSDimitry Andric             = CGF.tryEmitAsConstant(E)) {
1307ae0e2c9SDimitry Andric         EmitFinalDestCopy(E->getType(), result.getReferenceLValue(CGF, E));
131dff0c46cSDimitry Andric         return;
132dff0c46cSDimitry Andric       }
133dff0c46cSDimitry Andric     }
134dff0c46cSDimitry Andric 
135dff0c46cSDimitry Andric     EmitAggLoadOfLValue(E);
136dff0c46cSDimitry Andric   }
137dff0c46cSDimitry Andric 
138f22ef01cSRoman Divacky   void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
139f22ef01cSRoman Divacky   void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
140f22ef01cSRoman Divacky   void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
14117a519f9SDimitry Andric   void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
142f22ef01cSRoman Divacky   void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
143f22ef01cSRoman Divacky     EmitAggLoadOfLValue(E);
144f22ef01cSRoman Divacky   }
145f22ef01cSRoman Divacky   void VisitPredefinedExpr(const PredefinedExpr *E) {
146f22ef01cSRoman Divacky     EmitAggLoadOfLValue(E);
147f22ef01cSRoman Divacky   }
148f22ef01cSRoman Divacky 
149f22ef01cSRoman Divacky   // Operators.
150f22ef01cSRoman Divacky   void VisitCastExpr(CastExpr *E);
151f22ef01cSRoman Divacky   void VisitCallExpr(const CallExpr *E);
152f22ef01cSRoman Divacky   void VisitStmtExpr(const StmtExpr *E);
153f22ef01cSRoman Divacky   void VisitBinaryOperator(const BinaryOperator *BO);
154f22ef01cSRoman Divacky   void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
155f22ef01cSRoman Divacky   void VisitBinAssign(const BinaryOperator *E);
156f22ef01cSRoman Divacky   void VisitBinComma(const BinaryOperator *E);
157f22ef01cSRoman Divacky 
158f22ef01cSRoman Divacky   void VisitObjCMessageExpr(ObjCMessageExpr *E);
159f22ef01cSRoman Divacky   void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
160f22ef01cSRoman Divacky     EmitAggLoadOfLValue(E);
161f22ef01cSRoman Divacky   }
162f22ef01cSRoman Divacky 
1638f0fd8f6SDimitry Andric   void VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E);
1642754fe60SDimitry Andric   void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
165f22ef01cSRoman Divacky   void VisitChooseExpr(const ChooseExpr *CE);
166f22ef01cSRoman Divacky   void VisitInitListExpr(InitListExpr *E);
167f22ef01cSRoman Divacky   void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
1688f0fd8f6SDimitry Andric   void VisitNoInitExpr(NoInitExpr *E) { } // Do nothing.
169f22ef01cSRoman Divacky   void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
170f22ef01cSRoman Divacky     Visit(DAE->getExpr());
171f22ef01cSRoman Divacky   }
172284c1978SDimitry Andric   void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
173284c1978SDimitry Andric     CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
174284c1978SDimitry Andric     Visit(DIE->getExpr());
175284c1978SDimitry Andric   }
176f22ef01cSRoman Divacky   void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
177f22ef01cSRoman Divacky   void VisitCXXConstructExpr(const CXXConstructExpr *E);
178e7145dcbSDimitry Andric   void VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);
179dff0c46cSDimitry Andric   void VisitLambdaExpr(LambdaExpr *E);
180f785676fSDimitry Andric   void VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
1812754fe60SDimitry Andric   void VisitExprWithCleanups(ExprWithCleanups *E);
182ffd1746dSEd Schouten   void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
183f22ef01cSRoman Divacky   void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
18417a519f9SDimitry Andric   void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
1852754fe60SDimitry Andric   void VisitOpaqueValueExpr(OpaqueValueExpr *E);
1862754fe60SDimitry Andric 
187dff0c46cSDimitry Andric   void VisitPseudoObjectExpr(PseudoObjectExpr *E) {
188dff0c46cSDimitry Andric     if (E->isGLValue()) {
189dff0c46cSDimitry Andric       LValue LV = CGF.EmitPseudoObjectLValue(E);
1907ae0e2c9SDimitry Andric       return EmitFinalDestCopy(E->getType(), LV);
191dff0c46cSDimitry Andric     }
192dff0c46cSDimitry Andric 
193dff0c46cSDimitry Andric     CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType()));
194dff0c46cSDimitry Andric   }
195dff0c46cSDimitry Andric 
196f22ef01cSRoman Divacky   void VisitVAArgExpr(VAArgExpr *E);
197f22ef01cSRoman Divacky 
19817a519f9SDimitry Andric   void EmitInitializationToLValue(Expr *E, LValue Address);
19917a519f9SDimitry Andric   void EmitNullInitializationToLValue(LValue Address);
200f22ef01cSRoman Divacky   //  case Expr::ChooseExprClass:
201f22ef01cSRoman Divacky   void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
2026122f3e6SDimitry Andric   void VisitAtomicExpr(AtomicExpr *E) {
2030623d748SDimitry Andric     RValue Res = CGF.EmitAtomicExpr(E);
2040623d748SDimitry Andric     EmitFinalDestCopy(E->getType(), Res);
2056122f3e6SDimitry Andric   }
206f22ef01cSRoman Divacky };
207f22ef01cSRoman Divacky }  // end anonymous namespace.
208f22ef01cSRoman Divacky 
209f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
210f22ef01cSRoman Divacky //                                Utilities
211f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
212f22ef01cSRoman Divacky 
213f22ef01cSRoman Divacky /// EmitAggLoadOfLValue - Given an expression with aggregate type that
214f22ef01cSRoman Divacky /// represents a value lvalue, this method emits the address of the lvalue,
215f22ef01cSRoman Divacky /// then loads the result into DestPtr.
216f22ef01cSRoman Divacky void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
217f22ef01cSRoman Divacky   LValue LV = CGF.EmitLValue(E);
218139f7f9bSDimitry Andric 
219139f7f9bSDimitry Andric   // If the type of the l-value is atomic, then do an atomic load.
22033956c43SDimitry Andric   if (LV.getType()->isAtomicType() || CGF.LValueIsSuitableForInlineAtomic(LV)) {
221f785676fSDimitry Andric     CGF.EmitAtomicLoad(LV, E->getExprLoc(), Dest);
222139f7f9bSDimitry Andric     return;
223139f7f9bSDimitry Andric   }
224139f7f9bSDimitry Andric 
2257ae0e2c9SDimitry Andric   EmitFinalDestCopy(E->getType(), LV);
226f22ef01cSRoman Divacky }
227f22ef01cSRoman Divacky 
228f22ef01cSRoman Divacky /// \brief True if the given aggregate type requires special GC API calls.
229f22ef01cSRoman Divacky bool AggExprEmitter::TypeRequiresGCollection(QualType T) {
230f22ef01cSRoman Divacky   // Only record types have members that might require garbage collection.
231f22ef01cSRoman Divacky   const RecordType *RecordTy = T->getAs<RecordType>();
232f22ef01cSRoman Divacky   if (!RecordTy) return false;
233f22ef01cSRoman Divacky 
234f22ef01cSRoman Divacky   // Don't mess with non-trivial C++ types.
235f22ef01cSRoman Divacky   RecordDecl *Record = RecordTy->getDecl();
236f22ef01cSRoman Divacky   if (isa<CXXRecordDecl>(Record) &&
237139f7f9bSDimitry Andric       (cast<CXXRecordDecl>(Record)->hasNonTrivialCopyConstructor() ||
238f22ef01cSRoman Divacky        !cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
239f22ef01cSRoman Divacky     return false;
240f22ef01cSRoman Divacky 
241f22ef01cSRoman Divacky   // Check whether the type has an object member.
242f22ef01cSRoman Divacky   return Record->hasObjectMember();
243f22ef01cSRoman Divacky }
244f22ef01cSRoman Divacky 
2456122f3e6SDimitry Andric /// \brief Perform the final move to DestPtr if for some reason
2466122f3e6SDimitry Andric /// getReturnValueSlot() didn't use it directly.
247f22ef01cSRoman Divacky ///
248f22ef01cSRoman Divacky /// The idea is that you do something like this:
249f22ef01cSRoman Divacky ///   RValue Result = EmitSomething(..., getReturnValueSlot());
2506122f3e6SDimitry Andric ///   EmitMoveFromReturnSlot(E, Result);
2516122f3e6SDimitry Andric ///
2526122f3e6SDimitry Andric /// If nothing interferes, this will cause the result to be emitted
2536122f3e6SDimitry Andric /// directly into the return value slot.  Otherwise, a final move
2546122f3e6SDimitry Andric /// will be performed.
2557ae0e2c9SDimitry Andric void AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue src) {
2566122f3e6SDimitry Andric   if (shouldUseDestForReturnSlot()) {
2576122f3e6SDimitry Andric     // Logically, Dest.getAddr() should equal Src.getAggregateAddr().
2586122f3e6SDimitry Andric     // The possibility of undef rvalues complicates that a lot,
2596122f3e6SDimitry Andric     // though, so we can't really assert.
2606122f3e6SDimitry Andric     return;
261ffd1746dSEd Schouten   }
2626122f3e6SDimitry Andric 
2637ae0e2c9SDimitry Andric   // Otherwise, copy from there to the destination.
2640623d748SDimitry Andric   assert(Dest.getPointer() != src.getAggregatePointer());
2650623d748SDimitry Andric   EmitFinalDestCopy(E->getType(), src);
266f22ef01cSRoman Divacky }
267f22ef01cSRoman Divacky 
268f22ef01cSRoman Divacky /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
2690623d748SDimitry Andric void AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src) {
2707ae0e2c9SDimitry Andric   assert(src.isAggregate() && "value must be aggregate value!");
2710623d748SDimitry Andric   LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddress(), type);
2727ae0e2c9SDimitry Andric   EmitFinalDestCopy(type, srcLV);
2737ae0e2c9SDimitry Andric }
274f22ef01cSRoman Divacky 
2757ae0e2c9SDimitry Andric /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
2767ae0e2c9SDimitry Andric void AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src) {
2772754fe60SDimitry Andric   // If Dest is ignored, then we're evaluating an aggregate expression
2787ae0e2c9SDimitry Andric   // in a context that doesn't care about the result.  Note that loads
2797ae0e2c9SDimitry Andric   // from volatile l-values force the existence of a non-ignored
2807ae0e2c9SDimitry Andric   // destination.
2817ae0e2c9SDimitry Andric   if (Dest.isIgnored())
282f22ef01cSRoman Divacky     return;
283e580952dSDimitry Andric 
2847ae0e2c9SDimitry Andric   AggValueSlot srcAgg =
2857ae0e2c9SDimitry Andric     AggValueSlot::forLValue(src, AggValueSlot::IsDestructed,
2867ae0e2c9SDimitry Andric                             needsGC(type), AggValueSlot::IsAliased);
2877ae0e2c9SDimitry Andric   EmitCopy(type, Dest, srcAgg);
288f22ef01cSRoman Divacky }
289f22ef01cSRoman Divacky 
2907ae0e2c9SDimitry Andric /// Perform a copy from the source into the destination.
2917ae0e2c9SDimitry Andric ///
2927ae0e2c9SDimitry Andric /// \param type - the type of the aggregate being copied; qualifiers are
2937ae0e2c9SDimitry Andric ///   ignored
2947ae0e2c9SDimitry Andric void AggExprEmitter::EmitCopy(QualType type, const AggValueSlot &dest,
2957ae0e2c9SDimitry Andric                               const AggValueSlot &src) {
2967ae0e2c9SDimitry Andric   if (dest.requiresGCollection()) {
2977ae0e2c9SDimitry Andric     CharUnits sz = CGF.getContext().getTypeSizeInChars(type);
2987ae0e2c9SDimitry Andric     llvm::Value *size = llvm::ConstantInt::get(CGF.SizeTy, sz.getQuantity());
299f22ef01cSRoman Divacky     CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
3000623d748SDimitry Andric                                                       dest.getAddress(),
3010623d748SDimitry Andric                                                       src.getAddress(),
3027ae0e2c9SDimitry Andric                                                       size);
303f22ef01cSRoman Divacky     return;
304f22ef01cSRoman Divacky   }
3057ae0e2c9SDimitry Andric 
306f22ef01cSRoman Divacky   // If the result of the assignment is used, copy the LHS there also.
3077ae0e2c9SDimitry Andric   // It's volatile if either side is.  Use the minimum alignment of
3087ae0e2c9SDimitry Andric   // the two sides.
3090623d748SDimitry Andric   CGF.EmitAggregateCopy(dest.getAddress(), src.getAddress(), type,
3100623d748SDimitry Andric                         dest.isVolatile() || src.isVolatile());
311dff0c46cSDimitry Andric }
312dff0c46cSDimitry Andric 
313dff0c46cSDimitry Andric /// \brief Emit the initializer for a std::initializer_list initialized with a
314dff0c46cSDimitry Andric /// real initializer list.
315f785676fSDimitry Andric void
316f785676fSDimitry Andric AggExprEmitter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
317f785676fSDimitry Andric   // Emit an array containing the elements.  The array is externally destructed
318f785676fSDimitry Andric   // if the std::initializer_list object is.
319f785676fSDimitry Andric   ASTContext &Ctx = CGF.getContext();
320f785676fSDimitry Andric   LValue Array = CGF.EmitLValue(E->getSubExpr());
321f785676fSDimitry Andric   assert(Array.isSimple() && "initializer_list array not a simple lvalue");
3220623d748SDimitry Andric   Address ArrayPtr = Array.getAddress();
323dff0c46cSDimitry Andric 
324f785676fSDimitry Andric   const ConstantArrayType *ArrayType =
325f785676fSDimitry Andric       Ctx.getAsConstantArrayType(E->getSubExpr()->getType());
326f785676fSDimitry Andric   assert(ArrayType && "std::initializer_list constructed from non-array");
327dff0c46cSDimitry Andric 
328f785676fSDimitry Andric   // FIXME: Perform the checks on the field types in SemaInit.
329f785676fSDimitry Andric   RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl();
330f785676fSDimitry Andric   RecordDecl::field_iterator Field = Record->field_begin();
331f785676fSDimitry Andric   if (Field == Record->field_end()) {
332f785676fSDimitry Andric     CGF.ErrorUnsupported(E, "weird std::initializer_list");
333dff0c46cSDimitry Andric     return;
334dff0c46cSDimitry Andric   }
335dff0c46cSDimitry Andric 
336dff0c46cSDimitry Andric   // Start pointer.
337f785676fSDimitry Andric   if (!Field->getType()->isPointerType() ||
338f785676fSDimitry Andric       !Ctx.hasSameType(Field->getType()->getPointeeType(),
339f785676fSDimitry Andric                        ArrayType->getElementType())) {
340f785676fSDimitry Andric     CGF.ErrorUnsupported(E, "weird std::initializer_list");
341dff0c46cSDimitry Andric     return;
342dff0c46cSDimitry Andric   }
343dff0c46cSDimitry Andric 
344f785676fSDimitry Andric   AggValueSlot Dest = EnsureSlot(E->getType());
3450623d748SDimitry Andric   LValue DestLV = CGF.MakeAddrLValue(Dest.getAddress(), E->getType());
346f785676fSDimitry Andric   LValue Start = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
347f785676fSDimitry Andric   llvm::Value *Zero = llvm::ConstantInt::get(CGF.PtrDiffTy, 0);
348f785676fSDimitry Andric   llvm::Value *IdxStart[] = { Zero, Zero };
349f785676fSDimitry Andric   llvm::Value *ArrayStart =
3500623d748SDimitry Andric       Builder.CreateInBoundsGEP(ArrayPtr.getPointer(), IdxStart, "arraystart");
351f785676fSDimitry Andric   CGF.EmitStoreThroughLValue(RValue::get(ArrayStart), Start);
352f785676fSDimitry Andric   ++Field;
353f785676fSDimitry Andric 
354f785676fSDimitry Andric   if (Field == Record->field_end()) {
355f785676fSDimitry Andric     CGF.ErrorUnsupported(E, "weird std::initializer_list");
356dff0c46cSDimitry Andric     return;
357dff0c46cSDimitry Andric   }
358f785676fSDimitry Andric 
359f785676fSDimitry Andric   llvm::Value *Size = Builder.getInt(ArrayType->getSize());
360f785676fSDimitry Andric   LValue EndOrLength = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
361f785676fSDimitry Andric   if (Field->getType()->isPointerType() &&
362f785676fSDimitry Andric       Ctx.hasSameType(Field->getType()->getPointeeType(),
363f785676fSDimitry Andric                       ArrayType->getElementType())) {
364dff0c46cSDimitry Andric     // End pointer.
365f785676fSDimitry Andric     llvm::Value *IdxEnd[] = { Zero, Size };
366f785676fSDimitry Andric     llvm::Value *ArrayEnd =
3670623d748SDimitry Andric         Builder.CreateInBoundsGEP(ArrayPtr.getPointer(), IdxEnd, "arrayend");
368f785676fSDimitry Andric     CGF.EmitStoreThroughLValue(RValue::get(ArrayEnd), EndOrLength);
369f785676fSDimitry Andric   } else if (Ctx.hasSameType(Field->getType(), Ctx.getSizeType())) {
370dff0c46cSDimitry Andric     // Length.
371f785676fSDimitry Andric     CGF.EmitStoreThroughLValue(RValue::get(Size), EndOrLength);
372dff0c46cSDimitry Andric   } else {
373f785676fSDimitry Andric     CGF.ErrorUnsupported(E, "weird std::initializer_list");
374dff0c46cSDimitry Andric     return;
375dff0c46cSDimitry Andric   }
376dff0c46cSDimitry Andric }
377dff0c46cSDimitry Andric 
37859d1ed5bSDimitry Andric /// \brief Determine if E is a trivial array filler, that is, one that is
37959d1ed5bSDimitry Andric /// equivalent to zero-initialization.
38059d1ed5bSDimitry Andric static bool isTrivialFiller(Expr *E) {
38159d1ed5bSDimitry Andric   if (!E)
38259d1ed5bSDimitry Andric     return true;
38359d1ed5bSDimitry Andric 
38459d1ed5bSDimitry Andric   if (isa<ImplicitValueInitExpr>(E))
38559d1ed5bSDimitry Andric     return true;
38659d1ed5bSDimitry Andric 
38759d1ed5bSDimitry Andric   if (auto *ILE = dyn_cast<InitListExpr>(E)) {
38859d1ed5bSDimitry Andric     if (ILE->getNumInits())
38959d1ed5bSDimitry Andric       return false;
39059d1ed5bSDimitry Andric     return isTrivialFiller(ILE->getArrayFiller());
39159d1ed5bSDimitry Andric   }
39259d1ed5bSDimitry Andric 
39359d1ed5bSDimitry Andric   if (auto *Cons = dyn_cast_or_null<CXXConstructExpr>(E))
39459d1ed5bSDimitry Andric     return Cons->getConstructor()->isDefaultConstructor() &&
39559d1ed5bSDimitry Andric            Cons->getConstructor()->isTrivial();
39659d1ed5bSDimitry Andric 
39759d1ed5bSDimitry Andric   // FIXME: Are there other cases where we can avoid emitting an initializer?
39859d1ed5bSDimitry Andric   return false;
39959d1ed5bSDimitry Andric }
40059d1ed5bSDimitry Andric 
401dff0c46cSDimitry Andric /// \brief Emit initialization of an array from an initializer list.
4020623d748SDimitry Andric void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
403dff0c46cSDimitry Andric                                    QualType elementType, InitListExpr *E) {
404dff0c46cSDimitry Andric   uint64_t NumInitElements = E->getNumInits();
405dff0c46cSDimitry Andric 
406dff0c46cSDimitry Andric   uint64_t NumArrayElements = AType->getNumElements();
407dff0c46cSDimitry Andric   assert(NumInitElements <= NumArrayElements);
408dff0c46cSDimitry Andric 
409dff0c46cSDimitry Andric   // DestPtr is an array*.  Construct an elementType* by drilling
410dff0c46cSDimitry Andric   // down a level.
411dff0c46cSDimitry Andric   llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
412dff0c46cSDimitry Andric   llvm::Value *indices[] = { zero, zero };
413dff0c46cSDimitry Andric   llvm::Value *begin =
4140623d748SDimitry Andric     Builder.CreateInBoundsGEP(DestPtr.getPointer(), indices, "arrayinit.begin");
4150623d748SDimitry Andric 
4160623d748SDimitry Andric   CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType);
4170623d748SDimitry Andric   CharUnits elementAlign =
4180623d748SDimitry Andric     DestPtr.getAlignment().alignmentOfArrayElement(elementSize);
419dff0c46cSDimitry Andric 
420dff0c46cSDimitry Andric   // Exception safety requires us to destroy all the
421dff0c46cSDimitry Andric   // already-constructed members if an initializer throws.
422dff0c46cSDimitry Andric   // For that, we'll need an EH cleanup.
423dff0c46cSDimitry Andric   QualType::DestructionKind dtorKind = elementType.isDestructedType();
4240623d748SDimitry Andric   Address endOfInit = Address::invalid();
425dff0c46cSDimitry Andric   EHScopeStack::stable_iterator cleanup;
42659d1ed5bSDimitry Andric   llvm::Instruction *cleanupDominator = nullptr;
427dff0c46cSDimitry Andric   if (CGF.needsEHCleanup(dtorKind)) {
428dff0c46cSDimitry Andric     // In principle we could tell the cleanup where we are more
429dff0c46cSDimitry Andric     // directly, but the control flow can get so varied here that it
430dff0c46cSDimitry Andric     // would actually be quite complex.  Therefore we go through an
431dff0c46cSDimitry Andric     // alloca.
4320623d748SDimitry Andric     endOfInit = CGF.CreateTempAlloca(begin->getType(), CGF.getPointerAlign(),
433dff0c46cSDimitry Andric                                      "arrayinit.endOfInit");
434dff0c46cSDimitry Andric     cleanupDominator = Builder.CreateStore(begin, endOfInit);
435dff0c46cSDimitry Andric     CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType,
4360623d748SDimitry Andric                                          elementAlign,
437dff0c46cSDimitry Andric                                          CGF.getDestroyer(dtorKind));
438dff0c46cSDimitry Andric     cleanup = CGF.EHStack.stable_begin();
439dff0c46cSDimitry Andric 
440dff0c46cSDimitry Andric   // Otherwise, remember that we didn't need a cleanup.
441dff0c46cSDimitry Andric   } else {
442dff0c46cSDimitry Andric     dtorKind = QualType::DK_none;
443dff0c46cSDimitry Andric   }
444dff0c46cSDimitry Andric 
445dff0c46cSDimitry Andric   llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1);
446dff0c46cSDimitry Andric 
447dff0c46cSDimitry Andric   // The 'current element to initialize'.  The invariants on this
448dff0c46cSDimitry Andric   // variable are complicated.  Essentially, after each iteration of
449dff0c46cSDimitry Andric   // the loop, it points to the last initialized element, except
450dff0c46cSDimitry Andric   // that it points to the beginning of the array before any
451dff0c46cSDimitry Andric   // elements have been initialized.
452dff0c46cSDimitry Andric   llvm::Value *element = begin;
453dff0c46cSDimitry Andric 
454dff0c46cSDimitry Andric   // Emit the explicit initializers.
455dff0c46cSDimitry Andric   for (uint64_t i = 0; i != NumInitElements; ++i) {
456dff0c46cSDimitry Andric     // Advance to the next element.
457dff0c46cSDimitry Andric     if (i > 0) {
458dff0c46cSDimitry Andric       element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element");
459dff0c46cSDimitry Andric 
460dff0c46cSDimitry Andric       // Tell the cleanup that it needs to destroy up to this
461dff0c46cSDimitry Andric       // element.  TODO: some of these stores can be trivially
462dff0c46cSDimitry Andric       // observed to be unnecessary.
4630623d748SDimitry Andric       if (endOfInit.isValid()) Builder.CreateStore(element, endOfInit);
464dff0c46cSDimitry Andric     }
465dff0c46cSDimitry Andric 
4660623d748SDimitry Andric     LValue elementLV =
4670623d748SDimitry Andric       CGF.MakeAddrLValue(Address(element, elementAlign), elementType);
468dff0c46cSDimitry Andric     EmitInitializationToLValue(E->getInit(i), elementLV);
469dff0c46cSDimitry Andric   }
470dff0c46cSDimitry Andric 
471dff0c46cSDimitry Andric   // Check whether there's a non-trivial array-fill expression.
472dff0c46cSDimitry Andric   Expr *filler = E->getArrayFiller();
47359d1ed5bSDimitry Andric   bool hasTrivialFiller = isTrivialFiller(filler);
474dff0c46cSDimitry Andric 
475dff0c46cSDimitry Andric   // Any remaining elements need to be zero-initialized, possibly
476dff0c46cSDimitry Andric   // using the filler expression.  We can skip this if the we're
477dff0c46cSDimitry Andric   // emitting to zeroed memory.
478dff0c46cSDimitry Andric   if (NumInitElements != NumArrayElements &&
479dff0c46cSDimitry Andric       !(Dest.isZeroed() && hasTrivialFiller &&
480dff0c46cSDimitry Andric         CGF.getTypes().isZeroInitializable(elementType))) {
481dff0c46cSDimitry Andric 
482dff0c46cSDimitry Andric     // Use an actual loop.  This is basically
483dff0c46cSDimitry Andric     //   do { *array++ = filler; } while (array != end);
484dff0c46cSDimitry Andric 
485dff0c46cSDimitry Andric     // Advance to the start of the rest of the array.
486dff0c46cSDimitry Andric     if (NumInitElements) {
487dff0c46cSDimitry Andric       element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start");
4880623d748SDimitry Andric       if (endOfInit.isValid()) Builder.CreateStore(element, endOfInit);
489dff0c46cSDimitry Andric     }
490dff0c46cSDimitry Andric 
491dff0c46cSDimitry Andric     // Compute the end of the array.
492dff0c46cSDimitry Andric     llvm::Value *end = Builder.CreateInBoundsGEP(begin,
493dff0c46cSDimitry Andric                       llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements),
494dff0c46cSDimitry Andric                                                  "arrayinit.end");
495dff0c46cSDimitry Andric 
496dff0c46cSDimitry Andric     llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
497dff0c46cSDimitry Andric     llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body");
498dff0c46cSDimitry Andric 
499dff0c46cSDimitry Andric     // Jump into the body.
500dff0c46cSDimitry Andric     CGF.EmitBlock(bodyBB);
501dff0c46cSDimitry Andric     llvm::PHINode *currentElement =
502dff0c46cSDimitry Andric       Builder.CreatePHI(element->getType(), 2, "arrayinit.cur");
503dff0c46cSDimitry Andric     currentElement->addIncoming(element, entryBB);
504dff0c46cSDimitry Andric 
505dff0c46cSDimitry Andric     // Emit the actual filler expression.
5060623d748SDimitry Andric     LValue elementLV =
5070623d748SDimitry Andric       CGF.MakeAddrLValue(Address(currentElement, elementAlign), elementType);
508dff0c46cSDimitry Andric     if (filler)
509dff0c46cSDimitry Andric       EmitInitializationToLValue(filler, elementLV);
510dff0c46cSDimitry Andric     else
511dff0c46cSDimitry Andric       EmitNullInitializationToLValue(elementLV);
512dff0c46cSDimitry Andric 
513dff0c46cSDimitry Andric     // Move on to the next element.
514dff0c46cSDimitry Andric     llvm::Value *nextElement =
515dff0c46cSDimitry Andric       Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next");
516dff0c46cSDimitry Andric 
517dff0c46cSDimitry Andric     // Tell the EH cleanup that we finished with the last element.
5180623d748SDimitry Andric     if (endOfInit.isValid()) Builder.CreateStore(nextElement, endOfInit);
519dff0c46cSDimitry Andric 
520dff0c46cSDimitry Andric     // Leave the loop if we're done.
521dff0c46cSDimitry Andric     llvm::Value *done = Builder.CreateICmpEQ(nextElement, end,
522dff0c46cSDimitry Andric                                              "arrayinit.done");
523dff0c46cSDimitry Andric     llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end");
524dff0c46cSDimitry Andric     Builder.CreateCondBr(done, endBB, bodyBB);
525dff0c46cSDimitry Andric     currentElement->addIncoming(nextElement, Builder.GetInsertBlock());
526dff0c46cSDimitry Andric 
527dff0c46cSDimitry Andric     CGF.EmitBlock(endBB);
528dff0c46cSDimitry Andric   }
529dff0c46cSDimitry Andric 
530dff0c46cSDimitry Andric   // Leave the partial-array cleanup if we entered one.
531dff0c46cSDimitry Andric   if (dtorKind) CGF.DeactivateCleanupBlock(cleanup, cleanupDominator);
532f22ef01cSRoman Divacky }
533f22ef01cSRoman Divacky 
534f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
535f22ef01cSRoman Divacky //                            Visitor Methods
536f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
537f22ef01cSRoman Divacky 
53817a519f9SDimitry Andric void AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){
53917a519f9SDimitry Andric   Visit(E->GetTemporaryExpr());
54017a519f9SDimitry Andric }
54117a519f9SDimitry Andric 
5422754fe60SDimitry Andric void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) {
5437ae0e2c9SDimitry Andric   EmitFinalDestCopy(e->getType(), CGF.getOpaqueLValueMapping(e));
5442754fe60SDimitry Andric }
5452754fe60SDimitry Andric 
54617a519f9SDimitry Andric void
54717a519f9SDimitry Andric AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
548139f7f9bSDimitry Andric   if (Dest.isPotentiallyAliased() &&
549139f7f9bSDimitry Andric       E->getType().isPODType(CGF.getContext())) {
55017a519f9SDimitry Andric     // For a POD type, just emit a load of the lvalue + a copy, because our
55117a519f9SDimitry Andric     // compound literal might alias the destination.
55217a519f9SDimitry Andric     EmitAggLoadOfLValue(E);
55317a519f9SDimitry Andric     return;
55417a519f9SDimitry Andric   }
55517a519f9SDimitry Andric 
55617a519f9SDimitry Andric   AggValueSlot Slot = EnsureSlot(E->getType());
55717a519f9SDimitry Andric   CGF.EmitAggExpr(E->getInitializer(), Slot);
55817a519f9SDimitry Andric }
55917a519f9SDimitry Andric 
560139f7f9bSDimitry Andric /// Attempt to look through various unimportant expressions to find a
561139f7f9bSDimitry Andric /// cast of the given kind.
562139f7f9bSDimitry Andric static Expr *findPeephole(Expr *op, CastKind kind) {
563139f7f9bSDimitry Andric   while (true) {
564139f7f9bSDimitry Andric     op = op->IgnoreParens();
565139f7f9bSDimitry Andric     if (CastExpr *castE = dyn_cast<CastExpr>(op)) {
566139f7f9bSDimitry Andric       if (castE->getCastKind() == kind)
567139f7f9bSDimitry Andric         return castE->getSubExpr();
568139f7f9bSDimitry Andric       if (castE->getCastKind() == CK_NoOp)
569139f7f9bSDimitry Andric         continue;
570139f7f9bSDimitry Andric     }
57159d1ed5bSDimitry Andric     return nullptr;
572139f7f9bSDimitry Andric   }
573139f7f9bSDimitry Andric }
57417a519f9SDimitry Andric 
575f22ef01cSRoman Divacky void AggExprEmitter::VisitCastExpr(CastExpr *E) {
5760623d748SDimitry Andric   if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
5770623d748SDimitry Andric     CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
578f22ef01cSRoman Divacky   switch (E->getCastKind()) {
579e580952dSDimitry Andric   case CK_Dynamic: {
5803861d79fSDimitry Andric     // FIXME: Can this actually happen? We have no test coverage for it.
581f22ef01cSRoman Divacky     assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
5823861d79fSDimitry Andric     LValue LV = CGF.EmitCheckedLValue(E->getSubExpr(),
5833861d79fSDimitry Andric                                       CodeGenFunction::TCK_Load);
584f22ef01cSRoman Divacky     // FIXME: Do we also need to handle property references here?
585f22ef01cSRoman Divacky     if (LV.isSimple())
586f22ef01cSRoman Divacky       CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
587f22ef01cSRoman Divacky     else
588f22ef01cSRoman Divacky       CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
589f22ef01cSRoman Divacky 
5902754fe60SDimitry Andric     if (!Dest.isIgnored())
591f22ef01cSRoman Divacky       CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
592f22ef01cSRoman Divacky     break;
593f22ef01cSRoman Divacky   }
594f22ef01cSRoman Divacky 
595e580952dSDimitry Andric   case CK_ToUnion: {
59633956c43SDimitry Andric     // Evaluate even if the destination is ignored.
59733956c43SDimitry Andric     if (Dest.isIgnored()) {
59833956c43SDimitry Andric       CGF.EmitAnyExpr(E->getSubExpr(), AggValueSlot::ignored(),
59933956c43SDimitry Andric                       /*ignoreResult=*/true);
60033956c43SDimitry Andric       break;
60133956c43SDimitry Andric     }
6023b0f4066SDimitry Andric 
603f22ef01cSRoman Divacky     // GCC union extension
604e580952dSDimitry Andric     QualType Ty = E->getSubExpr()->getType();
6050623d748SDimitry Andric     Address CastPtr =
6060623d748SDimitry Andric       Builder.CreateElementBitCast(Dest.getAddress(), CGF.ConvertType(Ty));
60717a519f9SDimitry Andric     EmitInitializationToLValue(E->getSubExpr(),
60817a519f9SDimitry Andric                                CGF.MakeAddrLValue(CastPtr, Ty));
609f22ef01cSRoman Divacky     break;
610f22ef01cSRoman Divacky   }
611f22ef01cSRoman Divacky 
612e580952dSDimitry Andric   case CK_DerivedToBase:
613e580952dSDimitry Andric   case CK_BaseToDerived:
614e580952dSDimitry Andric   case CK_UncheckedDerivedToBase: {
6156122f3e6SDimitry Andric     llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: "
616f22ef01cSRoman Divacky                 "should have been unpacked before we got here");
617f22ef01cSRoman Divacky   }
618f22ef01cSRoman Divacky 
619139f7f9bSDimitry Andric   case CK_NonAtomicToAtomic:
620139f7f9bSDimitry Andric   case CK_AtomicToNonAtomic: {
621139f7f9bSDimitry Andric     bool isToAtomic = (E->getCastKind() == CK_NonAtomicToAtomic);
622139f7f9bSDimitry Andric 
623139f7f9bSDimitry Andric     // Determine the atomic and value types.
624139f7f9bSDimitry Andric     QualType atomicType = E->getSubExpr()->getType();
625139f7f9bSDimitry Andric     QualType valueType = E->getType();
626139f7f9bSDimitry Andric     if (isToAtomic) std::swap(atomicType, valueType);
627139f7f9bSDimitry Andric 
628139f7f9bSDimitry Andric     assert(atomicType->isAtomicType());
629139f7f9bSDimitry Andric     assert(CGF.getContext().hasSameUnqualifiedType(valueType,
630139f7f9bSDimitry Andric                           atomicType->castAs<AtomicType>()->getValueType()));
631139f7f9bSDimitry Andric 
632139f7f9bSDimitry Andric     // Just recurse normally if we're ignoring the result or the
633139f7f9bSDimitry Andric     // atomic type doesn't change representation.
634139f7f9bSDimitry Andric     if (Dest.isIgnored() || !CGF.CGM.isPaddedAtomicType(atomicType)) {
635139f7f9bSDimitry Andric       return Visit(E->getSubExpr());
636139f7f9bSDimitry Andric     }
637139f7f9bSDimitry Andric 
638139f7f9bSDimitry Andric     CastKind peepholeTarget =
639139f7f9bSDimitry Andric       (isToAtomic ? CK_AtomicToNonAtomic : CK_NonAtomicToAtomic);
640139f7f9bSDimitry Andric 
641139f7f9bSDimitry Andric     // These two cases are reverses of each other; try to peephole them.
642139f7f9bSDimitry Andric     if (Expr *op = findPeephole(E->getSubExpr(), peepholeTarget)) {
643139f7f9bSDimitry Andric       assert(CGF.getContext().hasSameUnqualifiedType(op->getType(),
644139f7f9bSDimitry Andric                                                      E->getType()) &&
645139f7f9bSDimitry Andric            "peephole significantly changed types?");
646139f7f9bSDimitry Andric       return Visit(op);
647139f7f9bSDimitry Andric     }
648139f7f9bSDimitry Andric 
649139f7f9bSDimitry Andric     // If we're converting an r-value of non-atomic type to an r-value
650f785676fSDimitry Andric     // of atomic type, just emit directly into the relevant sub-object.
651139f7f9bSDimitry Andric     if (isToAtomic) {
652f785676fSDimitry Andric       AggValueSlot valueDest = Dest;
653f785676fSDimitry Andric       if (!valueDest.isIgnored() && CGF.CGM.isPaddedAtomicType(atomicType)) {
654f785676fSDimitry Andric         // Zero-initialize.  (Strictly speaking, we only need to intialize
655f785676fSDimitry Andric         // the padding at the end, but this is simpler.)
656f785676fSDimitry Andric         if (!Dest.isZeroed())
6570623d748SDimitry Andric           CGF.EmitNullInitialization(Dest.getAddress(), atomicType);
658f785676fSDimitry Andric 
659f785676fSDimitry Andric         // Build a GEP to refer to the subobject.
6600623d748SDimitry Andric         Address valueAddr =
6610623d748SDimitry Andric             CGF.Builder.CreateStructGEP(valueDest.getAddress(), 0,
6620623d748SDimitry Andric                                         CharUnits());
663f785676fSDimitry Andric         valueDest = AggValueSlot::forAddr(valueAddr,
664f785676fSDimitry Andric                                           valueDest.getQualifiers(),
665f785676fSDimitry Andric                                           valueDest.isExternallyDestructed(),
666f785676fSDimitry Andric                                           valueDest.requiresGCollection(),
667f785676fSDimitry Andric                                           valueDest.isPotentiallyAliased(),
668f785676fSDimitry Andric                                           AggValueSlot::IsZeroed);
669f785676fSDimitry Andric       }
670f785676fSDimitry Andric 
671f785676fSDimitry Andric       CGF.EmitAggExpr(E->getSubExpr(), valueDest);
672139f7f9bSDimitry Andric       return;
673139f7f9bSDimitry Andric     }
674139f7f9bSDimitry Andric 
675139f7f9bSDimitry Andric     // Otherwise, we're converting an atomic type to a non-atomic type.
676f785676fSDimitry Andric     // Make an atomic temporary, emit into that, and then copy the value out.
677139f7f9bSDimitry Andric     AggValueSlot atomicSlot =
678139f7f9bSDimitry Andric       CGF.CreateAggTemp(atomicType, "atomic-to-nonatomic.temp");
679139f7f9bSDimitry Andric     CGF.EmitAggExpr(E->getSubExpr(), atomicSlot);
680139f7f9bSDimitry Andric 
6810623d748SDimitry Andric     Address valueAddr =
6820623d748SDimitry Andric       Builder.CreateStructGEP(atomicSlot.getAddress(), 0, CharUnits());
683139f7f9bSDimitry Andric     RValue rvalue = RValue::getAggregate(valueAddr, atomicSlot.isVolatile());
684139f7f9bSDimitry Andric     return EmitFinalDestCopy(valueType, rvalue);
685139f7f9bSDimitry Andric   }
686139f7f9bSDimitry Andric 
6877ae0e2c9SDimitry Andric   case CK_LValueToRValue:
6887ae0e2c9SDimitry Andric     // If we're loading from a volatile type, force the destination
6897ae0e2c9SDimitry Andric     // into existence.
6907ae0e2c9SDimitry Andric     if (E->getSubExpr()->getType().isVolatileQualified()) {
6917ae0e2c9SDimitry Andric       EnsureDest(E->getType());
6927ae0e2c9SDimitry Andric       return Visit(E->getSubExpr());
6937ae0e2c9SDimitry Andric     }
694139f7f9bSDimitry Andric 
6957ae0e2c9SDimitry Andric     // fallthrough
6967ae0e2c9SDimitry Andric 
697e580952dSDimitry Andric   case CK_NoOp:
698e580952dSDimitry Andric   case CK_UserDefinedConversion:
699e580952dSDimitry Andric   case CK_ConstructorConversion:
700f22ef01cSRoman Divacky     assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
701f22ef01cSRoman Divacky                                                    E->getType()) &&
702f22ef01cSRoman Divacky            "Implicit cast types must be compatible");
703f22ef01cSRoman Divacky     Visit(E->getSubExpr());
704f22ef01cSRoman Divacky     break;
705f22ef01cSRoman Divacky 
706e580952dSDimitry Andric   case CK_LValueBitCast:
7072754fe60SDimitry Andric     llvm_unreachable("should not be emitting lvalue bitcast as rvalue");
7082754fe60SDimitry Andric 
7092754fe60SDimitry Andric   case CK_Dependent:
7102754fe60SDimitry Andric   case CK_BitCast:
7112754fe60SDimitry Andric   case CK_ArrayToPointerDecay:
7122754fe60SDimitry Andric   case CK_FunctionToPointerDecay:
7132754fe60SDimitry Andric   case CK_NullToPointer:
7142754fe60SDimitry Andric   case CK_NullToMemberPointer:
7152754fe60SDimitry Andric   case CK_BaseToDerivedMemberPointer:
7162754fe60SDimitry Andric   case CK_DerivedToBaseMemberPointer:
7172754fe60SDimitry Andric   case CK_MemberPointerToBoolean:
718dff0c46cSDimitry Andric   case CK_ReinterpretMemberPointer:
7192754fe60SDimitry Andric   case CK_IntegralToPointer:
7202754fe60SDimitry Andric   case CK_PointerToIntegral:
7212754fe60SDimitry Andric   case CK_PointerToBoolean:
7222754fe60SDimitry Andric   case CK_ToVoid:
7232754fe60SDimitry Andric   case CK_VectorSplat:
7242754fe60SDimitry Andric   case CK_IntegralCast:
725444ed5c5SDimitry Andric   case CK_BooleanToSignedIntegral:
7262754fe60SDimitry Andric   case CK_IntegralToBoolean:
7272754fe60SDimitry Andric   case CK_IntegralToFloating:
7282754fe60SDimitry Andric   case CK_FloatingToIntegral:
7292754fe60SDimitry Andric   case CK_FloatingToBoolean:
7302754fe60SDimitry Andric   case CK_FloatingCast:
7316122f3e6SDimitry Andric   case CK_CPointerToObjCPointerCast:
7326122f3e6SDimitry Andric   case CK_BlockPointerToObjCPointerCast:
7332754fe60SDimitry Andric   case CK_AnyPointerToBlockPointerCast:
7342754fe60SDimitry Andric   case CK_ObjCObjectLValueCast:
7352754fe60SDimitry Andric   case CK_FloatingRealToComplex:
7362754fe60SDimitry Andric   case CK_FloatingComplexToReal:
7372754fe60SDimitry Andric   case CK_FloatingComplexToBoolean:
7382754fe60SDimitry Andric   case CK_FloatingComplexCast:
7392754fe60SDimitry Andric   case CK_FloatingComplexToIntegralComplex:
7402754fe60SDimitry Andric   case CK_IntegralRealToComplex:
7412754fe60SDimitry Andric   case CK_IntegralComplexToReal:
7422754fe60SDimitry Andric   case CK_IntegralComplexToBoolean:
7432754fe60SDimitry Andric   case CK_IntegralComplexCast:
7442754fe60SDimitry Andric   case CK_IntegralComplexToFloatingComplex:
7456122f3e6SDimitry Andric   case CK_ARCProduceObject:
7466122f3e6SDimitry Andric   case CK_ARCConsumeObject:
7476122f3e6SDimitry Andric   case CK_ARCReclaimReturnedObject:
7486122f3e6SDimitry Andric   case CK_ARCExtendBlockObject:
749dff0c46cSDimitry Andric   case CK_CopyAndAutoreleaseBlockObject:
7503861d79fSDimitry Andric   case CK_BuiltinFnToFnPtr:
751139f7f9bSDimitry Andric   case CK_ZeroToOCLEvent:
75259d1ed5bSDimitry Andric   case CK_AddressSpaceConversion:
7532754fe60SDimitry Andric     llvm_unreachable("cast kind invalid for aggregate types");
754f22ef01cSRoman Divacky   }
755f22ef01cSRoman Divacky }
756f22ef01cSRoman Divacky 
757f22ef01cSRoman Divacky void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
75833956c43SDimitry Andric   if (E->getCallReturnType(CGF.getContext())->isReferenceType()) {
759f22ef01cSRoman Divacky     EmitAggLoadOfLValue(E);
760f22ef01cSRoman Divacky     return;
761f22ef01cSRoman Divacky   }
762f22ef01cSRoman Divacky 
763f22ef01cSRoman Divacky   RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
7646122f3e6SDimitry Andric   EmitMoveFromReturnSlot(E, RV);
765f22ef01cSRoman Divacky }
766f22ef01cSRoman Divacky 
767f22ef01cSRoman Divacky void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
768f22ef01cSRoman Divacky   RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
7696122f3e6SDimitry Andric   EmitMoveFromReturnSlot(E, RV);
770f22ef01cSRoman Divacky }
771f22ef01cSRoman Divacky 
772f22ef01cSRoman Divacky void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
7732754fe60SDimitry Andric   CGF.EmitIgnoredExpr(E->getLHS());
7742754fe60SDimitry Andric   Visit(E->getRHS());
775f22ef01cSRoman Divacky }
776f22ef01cSRoman Divacky 
777f22ef01cSRoman Divacky void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
7782754fe60SDimitry Andric   CodeGenFunction::StmtExprEvaluation eval(CGF);
7792754fe60SDimitry Andric   CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
780f22ef01cSRoman Divacky }
781f22ef01cSRoman Divacky 
782f22ef01cSRoman Divacky void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
783e580952dSDimitry Andric   if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
784f22ef01cSRoman Divacky     VisitPointerToDataMemberBinaryOperator(E);
785f22ef01cSRoman Divacky   else
786f22ef01cSRoman Divacky     CGF.ErrorUnsupported(E, "aggregate binary expression");
787f22ef01cSRoman Divacky }
788f22ef01cSRoman Divacky 
789f22ef01cSRoman Divacky void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
790f22ef01cSRoman Divacky                                                     const BinaryOperator *E) {
791f22ef01cSRoman Divacky   LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
7927ae0e2c9SDimitry Andric   EmitFinalDestCopy(E->getType(), LV);
7937ae0e2c9SDimitry Andric }
7947ae0e2c9SDimitry Andric 
7957ae0e2c9SDimitry Andric /// Is the value of the given expression possibly a reference to or
7967ae0e2c9SDimitry Andric /// into a __block variable?
7977ae0e2c9SDimitry Andric static bool isBlockVarRef(const Expr *E) {
7987ae0e2c9SDimitry Andric   // Make sure we look through parens.
7997ae0e2c9SDimitry Andric   E = E->IgnoreParens();
8007ae0e2c9SDimitry Andric 
8017ae0e2c9SDimitry Andric   // Check for a direct reference to a __block variable.
8027ae0e2c9SDimitry Andric   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
8037ae0e2c9SDimitry Andric     const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
8047ae0e2c9SDimitry Andric     return (var && var->hasAttr<BlocksAttr>());
8057ae0e2c9SDimitry Andric   }
8067ae0e2c9SDimitry Andric 
8077ae0e2c9SDimitry Andric   // More complicated stuff.
8087ae0e2c9SDimitry Andric 
8097ae0e2c9SDimitry Andric   // Binary operators.
8107ae0e2c9SDimitry Andric   if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) {
8117ae0e2c9SDimitry Andric     // For an assignment or pointer-to-member operation, just care
8127ae0e2c9SDimitry Andric     // about the LHS.
8137ae0e2c9SDimitry Andric     if (op->isAssignmentOp() || op->isPtrMemOp())
8147ae0e2c9SDimitry Andric       return isBlockVarRef(op->getLHS());
8157ae0e2c9SDimitry Andric 
8167ae0e2c9SDimitry Andric     // For a comma, just care about the RHS.
8177ae0e2c9SDimitry Andric     if (op->getOpcode() == BO_Comma)
8187ae0e2c9SDimitry Andric       return isBlockVarRef(op->getRHS());
8197ae0e2c9SDimitry Andric 
8207ae0e2c9SDimitry Andric     // FIXME: pointer arithmetic?
8217ae0e2c9SDimitry Andric     return false;
8227ae0e2c9SDimitry Andric 
8237ae0e2c9SDimitry Andric   // Check both sides of a conditional operator.
8247ae0e2c9SDimitry Andric   } else if (const AbstractConditionalOperator *op
8257ae0e2c9SDimitry Andric                = dyn_cast<AbstractConditionalOperator>(E)) {
8267ae0e2c9SDimitry Andric     return isBlockVarRef(op->getTrueExpr())
8277ae0e2c9SDimitry Andric         || isBlockVarRef(op->getFalseExpr());
8287ae0e2c9SDimitry Andric 
8297ae0e2c9SDimitry Andric   // OVEs are required to support BinaryConditionalOperators.
8307ae0e2c9SDimitry Andric   } else if (const OpaqueValueExpr *op
8317ae0e2c9SDimitry Andric                = dyn_cast<OpaqueValueExpr>(E)) {
8327ae0e2c9SDimitry Andric     if (const Expr *src = op->getSourceExpr())
8337ae0e2c9SDimitry Andric       return isBlockVarRef(src);
8347ae0e2c9SDimitry Andric 
8357ae0e2c9SDimitry Andric   // Casts are necessary to get things like (*(int*)&var) = foo().
8367ae0e2c9SDimitry Andric   // We don't really care about the kind of cast here, except
8377ae0e2c9SDimitry Andric   // we don't want to look through l2r casts, because it's okay
8387ae0e2c9SDimitry Andric   // to get the *value* in a __block variable.
8397ae0e2c9SDimitry Andric   } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) {
8407ae0e2c9SDimitry Andric     if (cast->getCastKind() == CK_LValueToRValue)
8417ae0e2c9SDimitry Andric       return false;
8427ae0e2c9SDimitry Andric     return isBlockVarRef(cast->getSubExpr());
8437ae0e2c9SDimitry Andric 
8447ae0e2c9SDimitry Andric   // Handle unary operators.  Again, just aggressively look through
8457ae0e2c9SDimitry Andric   // it, ignoring the operation.
8467ae0e2c9SDimitry Andric   } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) {
8477ae0e2c9SDimitry Andric     return isBlockVarRef(uop->getSubExpr());
8487ae0e2c9SDimitry Andric 
8497ae0e2c9SDimitry Andric   // Look into the base of a field access.
8507ae0e2c9SDimitry Andric   } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) {
8517ae0e2c9SDimitry Andric     return isBlockVarRef(mem->getBase());
8527ae0e2c9SDimitry Andric 
8537ae0e2c9SDimitry Andric   // Look into the base of a subscript.
8547ae0e2c9SDimitry Andric   } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) {
8557ae0e2c9SDimitry Andric     return isBlockVarRef(sub->getBase());
8567ae0e2c9SDimitry Andric   }
8577ae0e2c9SDimitry Andric 
8587ae0e2c9SDimitry Andric   return false;
859f22ef01cSRoman Divacky }
860f22ef01cSRoman Divacky 
861f22ef01cSRoman Divacky void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
862f22ef01cSRoman Divacky   // For an assignment to work, the value on the right has
863f22ef01cSRoman Divacky   // to be compatible with the value on the left.
864f22ef01cSRoman Divacky   assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
865f22ef01cSRoman Divacky                                                  E->getRHS()->getType())
866f22ef01cSRoman Divacky          && "Invalid assignment");
8672754fe60SDimitry Andric 
8687ae0e2c9SDimitry Andric   // If the LHS might be a __block variable, and the RHS can
8697ae0e2c9SDimitry Andric   // potentially cause a block copy, we need to evaluate the RHS first
8707ae0e2c9SDimitry Andric   // so that the assignment goes the right place.
8717ae0e2c9SDimitry Andric   // This is pretty semantically fragile.
8727ae0e2c9SDimitry Andric   if (isBlockVarRef(E->getLHS()) &&
8733b0f4066SDimitry Andric       E->getRHS()->HasSideEffects(CGF.getContext())) {
8747ae0e2c9SDimitry Andric     // Ensure that we have a destination, and evaluate the RHS into that.
8757ae0e2c9SDimitry Andric     EnsureDest(E->getRHS()->getType());
8767ae0e2c9SDimitry Andric     Visit(E->getRHS());
8777ae0e2c9SDimitry Andric 
8787ae0e2c9SDimitry Andric     // Now emit the LHS and copy into it.
8793861d79fSDimitry Andric     LValue LHS = CGF.EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
8807ae0e2c9SDimitry Andric 
881139f7f9bSDimitry Andric     // That copy is an atomic copy if the LHS is atomic.
88233956c43SDimitry Andric     if (LHS.getType()->isAtomicType() ||
88333956c43SDimitry Andric         CGF.LValueIsSuitableForInlineAtomic(LHS)) {
884139f7f9bSDimitry Andric       CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
885139f7f9bSDimitry Andric       return;
886139f7f9bSDimitry Andric     }
887139f7f9bSDimitry Andric 
8887ae0e2c9SDimitry Andric     EmitCopy(E->getLHS()->getType(),
8897ae0e2c9SDimitry Andric              AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
8906122f3e6SDimitry Andric                                      needsGC(E->getLHS()->getType()),
8917ae0e2c9SDimitry Andric                                      AggValueSlot::IsAliased),
8927ae0e2c9SDimitry Andric              Dest);
8933b0f4066SDimitry Andric     return;
8943b0f4066SDimitry Andric   }
8953b0f4066SDimitry Andric 
896f22ef01cSRoman Divacky   LValue LHS = CGF.EmitLValue(E->getLHS());
897f22ef01cSRoman Divacky 
898139f7f9bSDimitry Andric   // If we have an atomic type, evaluate into the destination and then
899139f7f9bSDimitry Andric   // do an atomic copy.
90033956c43SDimitry Andric   if (LHS.getType()->isAtomicType() ||
90133956c43SDimitry Andric       CGF.LValueIsSuitableForInlineAtomic(LHS)) {
902139f7f9bSDimitry Andric     EnsureDest(E->getRHS()->getType());
903139f7f9bSDimitry Andric     Visit(E->getRHS());
904139f7f9bSDimitry Andric     CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
905139f7f9bSDimitry Andric     return;
906139f7f9bSDimitry Andric   }
907139f7f9bSDimitry Andric 
908f22ef01cSRoman Divacky   // Codegen the RHS so that it stores directly into the LHS.
9096122f3e6SDimitry Andric   AggValueSlot LHSSlot =
9106122f3e6SDimitry Andric     AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
9116122f3e6SDimitry Andric                             needsGC(E->getLHS()->getType()),
9126122f3e6SDimitry Andric                             AggValueSlot::IsAliased);
913139f7f9bSDimitry Andric   // A non-volatile aggregate destination might have volatile member.
914139f7f9bSDimitry Andric   if (!LHSSlot.isVolatile() &&
915139f7f9bSDimitry Andric       CGF.hasVolatileMember(E->getLHS()->getType()))
916139f7f9bSDimitry Andric     LHSSlot.setVolatile(true);
917139f7f9bSDimitry Andric 
9187ae0e2c9SDimitry Andric   CGF.EmitAggExpr(E->getRHS(), LHSSlot);
9197ae0e2c9SDimitry Andric 
9207ae0e2c9SDimitry Andric   // Copy into the destination if the assignment isn't ignored.
9217ae0e2c9SDimitry Andric   EmitFinalDestCopy(E->getType(), LHS);
922f22ef01cSRoman Divacky }
923f22ef01cSRoman Divacky 
9242754fe60SDimitry Andric void AggExprEmitter::
9252754fe60SDimitry Andric VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
926f22ef01cSRoman Divacky   llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
927f22ef01cSRoman Divacky   llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
928f22ef01cSRoman Divacky   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
929f22ef01cSRoman Divacky 
9302754fe60SDimitry Andric   // Bind the common expression if necessary.
9312754fe60SDimitry Andric   CodeGenFunction::OpaqueValueMapping binding(CGF, E);
9322754fe60SDimitry Andric 
9332754fe60SDimitry Andric   CodeGenFunction::ConditionalEvaluation eval(CGF);
93433956c43SDimitry Andric   CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
93533956c43SDimitry Andric                            CGF.getProfileCount(E));
936f22ef01cSRoman Divacky 
9372754fe60SDimitry Andric   // Save whether the destination's lifetime is externally managed.
9386122f3e6SDimitry Andric   bool isExternallyDestructed = Dest.isExternallyDestructed();
9392754fe60SDimitry Andric 
9402754fe60SDimitry Andric   eval.begin(CGF);
941f22ef01cSRoman Divacky   CGF.EmitBlock(LHSBlock);
94233956c43SDimitry Andric   CGF.incrementProfileCounter(E);
9432754fe60SDimitry Andric   Visit(E->getTrueExpr());
9442754fe60SDimitry Andric   eval.end(CGF);
945f22ef01cSRoman Divacky 
9462754fe60SDimitry Andric   assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!");
9472754fe60SDimitry Andric   CGF.Builder.CreateBr(ContBlock);
948f22ef01cSRoman Divacky 
9492754fe60SDimitry Andric   // If the result of an agg expression is unused, then the emission
9502754fe60SDimitry Andric   // of the LHS might need to create a destination slot.  That's fine
9512754fe60SDimitry Andric   // with us, and we can safely emit the RHS into the same slot, but
9526122f3e6SDimitry Andric   // we shouldn't claim that it's already being destructed.
9536122f3e6SDimitry Andric   Dest.setExternallyDestructed(isExternallyDestructed);
954f22ef01cSRoman Divacky 
9552754fe60SDimitry Andric   eval.begin(CGF);
956f22ef01cSRoman Divacky   CGF.EmitBlock(RHSBlock);
9572754fe60SDimitry Andric   Visit(E->getFalseExpr());
9582754fe60SDimitry Andric   eval.end(CGF);
959f22ef01cSRoman Divacky 
960f22ef01cSRoman Divacky   CGF.EmitBlock(ContBlock);
961f22ef01cSRoman Divacky }
962f22ef01cSRoman Divacky 
963f22ef01cSRoman Divacky void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
964f785676fSDimitry Andric   Visit(CE->getChosenSubExpr());
965f22ef01cSRoman Divacky }
966f22ef01cSRoman Divacky 
967f22ef01cSRoman Divacky void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
9680623d748SDimitry Andric   Address ArgValue = Address::invalid();
9690623d748SDimitry Andric   Address ArgPtr = CGF.EmitVAArg(VE, ArgValue);
970f22ef01cSRoman Divacky 
971e7145dcbSDimitry Andric   // If EmitVAArg fails, emit an error.
9720623d748SDimitry Andric   if (!ArgPtr.isValid()) {
973e7145dcbSDimitry Andric     CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
974f22ef01cSRoman Divacky     return;
975f22ef01cSRoman Divacky   }
976f22ef01cSRoman Divacky 
9777ae0e2c9SDimitry Andric   EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType()));
978f22ef01cSRoman Divacky }
979f22ef01cSRoman Divacky 
980f22ef01cSRoman Divacky void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
9812754fe60SDimitry Andric   // Ensure that we have a slot, but if we already do, remember
9826122f3e6SDimitry Andric   // whether it was externally destructed.
9836122f3e6SDimitry Andric   bool wasExternallyDestructed = Dest.isExternallyDestructed();
9847ae0e2c9SDimitry Andric   EnsureDest(E->getType());
9856122f3e6SDimitry Andric 
9866122f3e6SDimitry Andric   // We're going to push a destructor if there isn't already one.
9876122f3e6SDimitry Andric   Dest.setExternallyDestructed();
988f22ef01cSRoman Divacky 
989f22ef01cSRoman Divacky   Visit(E->getSubExpr());
990f22ef01cSRoman Divacky 
9916122f3e6SDimitry Andric   // Push that destructor we promised.
9926122f3e6SDimitry Andric   if (!wasExternallyDestructed)
9930623d748SDimitry Andric     CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddress());
994f22ef01cSRoman Divacky }
995f22ef01cSRoman Divacky 
996f22ef01cSRoman Divacky void
997f22ef01cSRoman Divacky AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
9982754fe60SDimitry Andric   AggValueSlot Slot = EnsureSlot(E->getType());
9992754fe60SDimitry Andric   CGF.EmitCXXConstructExpr(E, Slot);
1000f22ef01cSRoman Divacky }
1001f22ef01cSRoman Divacky 
1002e7145dcbSDimitry Andric void AggExprEmitter::VisitCXXInheritedCtorInitExpr(
1003e7145dcbSDimitry Andric     const CXXInheritedCtorInitExpr *E) {
1004e7145dcbSDimitry Andric   AggValueSlot Slot = EnsureSlot(E->getType());
1005e7145dcbSDimitry Andric   CGF.EmitInheritedCXXConstructorCall(
1006e7145dcbSDimitry Andric       E->getConstructor(), E->constructsVBase(), Slot.getAddress(),
1007e7145dcbSDimitry Andric       E->inheritedFromVBase(), E);
1008e7145dcbSDimitry Andric }
1009e7145dcbSDimitry Andric 
1010dff0c46cSDimitry Andric void
1011dff0c46cSDimitry Andric AggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {
1012dff0c46cSDimitry Andric   AggValueSlot Slot = EnsureSlot(E->getType());
1013dff0c46cSDimitry Andric   CGF.EmitLambdaExpr(E, Slot);
1014dff0c46cSDimitry Andric }
1015dff0c46cSDimitry Andric 
10162754fe60SDimitry Andric void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {
1017dff0c46cSDimitry Andric   CGF.enterFullExpression(E);
1018dff0c46cSDimitry Andric   CodeGenFunction::RunCleanupsScope cleanups(CGF);
1019dff0c46cSDimitry Andric   Visit(E->getSubExpr());
1020f22ef01cSRoman Divacky }
1021f22ef01cSRoman Divacky 
1022ffd1746dSEd Schouten void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
10232754fe60SDimitry Andric   QualType T = E->getType();
10242754fe60SDimitry Andric   AggValueSlot Slot = EnsureSlot(T);
10250623d748SDimitry Andric   EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddress(), T));
1026f22ef01cSRoman Divacky }
1027f22ef01cSRoman Divacky 
1028f22ef01cSRoman Divacky void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
10292754fe60SDimitry Andric   QualType T = E->getType();
10302754fe60SDimitry Andric   AggValueSlot Slot = EnsureSlot(T);
10310623d748SDimitry Andric   EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddress(), T));
10322754fe60SDimitry Andric }
1033f22ef01cSRoman Divacky 
10342754fe60SDimitry Andric /// isSimpleZero - If emitting this value will obviously just cause a store of
10352754fe60SDimitry Andric /// zero to memory, return true.  This can return false if uncertain, so it just
10362754fe60SDimitry Andric /// handles simple cases.
10372754fe60SDimitry Andric static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) {
10383b0f4066SDimitry Andric   E = E->IgnoreParens();
10393b0f4066SDimitry Andric 
10402754fe60SDimitry Andric   // 0
10412754fe60SDimitry Andric   if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E))
10422754fe60SDimitry Andric     return IL->getValue() == 0;
10432754fe60SDimitry Andric   // +0.0
10442754fe60SDimitry Andric   if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E))
10452754fe60SDimitry Andric     return FL->getValue().isPosZero();
10462754fe60SDimitry Andric   // int()
10472754fe60SDimitry Andric   if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) &&
10482754fe60SDimitry Andric       CGF.getTypes().isZeroInitializable(E->getType()))
10492754fe60SDimitry Andric     return true;
10502754fe60SDimitry Andric   // (int*)0 - Null pointer expressions.
10512754fe60SDimitry Andric   if (const CastExpr *ICE = dyn_cast<CastExpr>(E))
10522754fe60SDimitry Andric     return ICE->getCastKind() == CK_NullToPointer;
10532754fe60SDimitry Andric   // '\0'
10542754fe60SDimitry Andric   if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E))
10552754fe60SDimitry Andric     return CL->getValue() == 0;
10562754fe60SDimitry Andric 
10572754fe60SDimitry Andric   // Otherwise, hard case: conservatively return false.
10582754fe60SDimitry Andric   return false;
1059f22ef01cSRoman Divacky }
10602754fe60SDimitry Andric 
1061f22ef01cSRoman Divacky 
1062f22ef01cSRoman Divacky void
106317a519f9SDimitry Andric AggExprEmitter::EmitInitializationToLValue(Expr *E, LValue LV) {
106417a519f9SDimitry Andric   QualType type = LV.getType();
1065f22ef01cSRoman Divacky   // FIXME: Ignore result?
1066f22ef01cSRoman Divacky   // FIXME: Are initializers affected by volatile?
10672754fe60SDimitry Andric   if (Dest.isZeroed() && isSimpleZero(E, CGF)) {
10682754fe60SDimitry Andric     // Storing "i32 0" to a zero'd memory location is a noop.
1069139f7f9bSDimitry Andric     return;
1070139f7f9bSDimitry Andric   } else if (isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) {
1071139f7f9bSDimitry Andric     return EmitNullInitializationToLValue(LV);
10728f0fd8f6SDimitry Andric   } else if (isa<NoInitExpr>(E)) {
10738f0fd8f6SDimitry Andric     // Do nothing.
10748f0fd8f6SDimitry Andric     return;
107517a519f9SDimitry Andric   } else if (type->isReferenceType()) {
1076f785676fSDimitry Andric     RValue RV = CGF.EmitReferenceBindingToExpr(E);
1077139f7f9bSDimitry Andric     return CGF.EmitStoreThroughLValue(RV, LV);
1078139f7f9bSDimitry Andric   }
1079139f7f9bSDimitry Andric 
1080139f7f9bSDimitry Andric   switch (CGF.getEvaluationKind(type)) {
1081139f7f9bSDimitry Andric   case TEK_Complex:
1082139f7f9bSDimitry Andric     CGF.EmitComplexExprIntoLValue(E, LV, /*isInit*/ true);
1083139f7f9bSDimitry Andric     return;
1084139f7f9bSDimitry Andric   case TEK_Aggregate:
10856122f3e6SDimitry Andric     CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV,
10866122f3e6SDimitry Andric                                                AggValueSlot::IsDestructed,
10876122f3e6SDimitry Andric                                       AggValueSlot::DoesNotNeedGCBarriers,
10886122f3e6SDimitry Andric                                                AggValueSlot::IsNotAliased,
108917a519f9SDimitry Andric                                                Dest.isZeroed()));
1090139f7f9bSDimitry Andric     return;
1091139f7f9bSDimitry Andric   case TEK_Scalar:
1092139f7f9bSDimitry Andric     if (LV.isSimple()) {
109359d1ed5bSDimitry Andric       CGF.EmitScalarInit(E, /*D=*/nullptr, LV, /*Captured=*/false);
1094f22ef01cSRoman Divacky     } else {
109517a519f9SDimitry Andric       CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV);
1096f22ef01cSRoman Divacky     }
1097139f7f9bSDimitry Andric     return;
1098139f7f9bSDimitry Andric   }
1099139f7f9bSDimitry Andric   llvm_unreachable("bad evaluation kind");
1100f22ef01cSRoman Divacky }
1101f22ef01cSRoman Divacky 
110217a519f9SDimitry Andric void AggExprEmitter::EmitNullInitializationToLValue(LValue lv) {
110317a519f9SDimitry Andric   QualType type = lv.getType();
110417a519f9SDimitry Andric 
11052754fe60SDimitry Andric   // If the destination slot is already zeroed out before the aggregate is
11062754fe60SDimitry Andric   // copied into it, we don't have to emit any zeros here.
110717a519f9SDimitry Andric   if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type))
11082754fe60SDimitry Andric     return;
11092754fe60SDimitry Andric 
1110139f7f9bSDimitry Andric   if (CGF.hasScalarEvaluationKind(type)) {
1111139f7f9bSDimitry Andric     // For non-aggregates, we can store the appropriate null constant.
1112139f7f9bSDimitry Andric     llvm::Value *null = CGF.CGM.EmitNullConstant(type);
1113dff0c46cSDimitry Andric     // Note that the following is not equivalent to
1114dff0c46cSDimitry Andric     // EmitStoreThroughBitfieldLValue for ARC types.
1115dff0c46cSDimitry Andric     if (lv.isBitField()) {
1116dff0c46cSDimitry Andric       CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv);
1117dff0c46cSDimitry Andric     } else {
1118dff0c46cSDimitry Andric       assert(lv.isSimple());
1119dff0c46cSDimitry Andric       CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true);
1120dff0c46cSDimitry Andric     }
1121f22ef01cSRoman Divacky   } else {
1122f22ef01cSRoman Divacky     // There's a potential optimization opportunity in combining
1123f22ef01cSRoman Divacky     // memsets; that would be easy for arrays, but relatively
1124f22ef01cSRoman Divacky     // difficult for structures with the current code.
112517a519f9SDimitry Andric     CGF.EmitNullInitialization(lv.getAddress(), lv.getType());
1126f22ef01cSRoman Divacky   }
1127f22ef01cSRoman Divacky }
1128f22ef01cSRoman Divacky 
1129f22ef01cSRoman Divacky void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
1130f22ef01cSRoman Divacky #if 0
1131f22ef01cSRoman Divacky   // FIXME: Assess perf here?  Figure out what cases are worth optimizing here
1132f22ef01cSRoman Divacky   // (Length of globals? Chunks of zeroed-out space?).
1133f22ef01cSRoman Divacky   //
1134f22ef01cSRoman Divacky   // If we can, prefer a copy from a global; this is a lot less code for long
1135f22ef01cSRoman Divacky   // globals, and it's easier for the current optimizers to analyze.
1136f22ef01cSRoman Divacky   if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
1137f22ef01cSRoman Divacky     llvm::GlobalVariable* GV =
1138f22ef01cSRoman Divacky     new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
1139f22ef01cSRoman Divacky                              llvm::GlobalValue::InternalLinkage, C, "");
11407ae0e2c9SDimitry Andric     EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType()));
1141f22ef01cSRoman Divacky     return;
1142f22ef01cSRoman Divacky   }
1143f22ef01cSRoman Divacky #endif
11442754fe60SDimitry Andric   if (E->hadArrayRangeDesignator())
1145f22ef01cSRoman Divacky     CGF.ErrorUnsupported(E, "GNU array range designator extension");
11462754fe60SDimitry Andric 
1147cb4dff85SDimitry Andric   AggValueSlot Dest = EnsureSlot(E->getType());
1148f785676fSDimitry Andric 
11490623d748SDimitry Andric   LValue DestLV = CGF.MakeAddrLValue(Dest.getAddress(), E->getType());
1150f22ef01cSRoman Divacky 
1151f22ef01cSRoman Divacky   // Handle initialization of an array.
1152f22ef01cSRoman Divacky   if (E->getType()->isArrayType()) {
1153cb4dff85SDimitry Andric     if (E->isStringLiteralInit())
1154cb4dff85SDimitry Andric       return Visit(E->getInit(0));
1155f22ef01cSRoman Divacky 
1156dff0c46cSDimitry Andric     QualType elementType =
1157dff0c46cSDimitry Andric         CGF.getContext().getAsArrayType(E->getType())->getElementType();
1158f22ef01cSRoman Divacky 
11590623d748SDimitry Andric     auto AType = cast<llvm::ArrayType>(Dest.getAddress().getElementType());
11600623d748SDimitry Andric     EmitArrayInit(Dest.getAddress(), AType, elementType, E);
1161f22ef01cSRoman Divacky     return;
1162f22ef01cSRoman Divacky   }
1163f22ef01cSRoman Divacky 
116459d1ed5bSDimitry Andric   if (E->getType()->isAtomicType()) {
116559d1ed5bSDimitry Andric     // An _Atomic(T) object can be list-initialized from an expression
116659d1ed5bSDimitry Andric     // of the same type.
116759d1ed5bSDimitry Andric     assert(E->getNumInits() == 1 &&
116859d1ed5bSDimitry Andric            CGF.getContext().hasSameUnqualifiedType(E->getInit(0)->getType(),
116959d1ed5bSDimitry Andric                                                    E->getType()) &&
117059d1ed5bSDimitry Andric            "unexpected list initialization for atomic object");
117159d1ed5bSDimitry Andric     return Visit(E->getInit(0));
117259d1ed5bSDimitry Andric   }
117359d1ed5bSDimitry Andric 
1174f22ef01cSRoman Divacky   assert(E->getType()->isRecordType() && "Only support structs/unions here!");
1175f22ef01cSRoman Divacky 
1176f22ef01cSRoman Divacky   // Do struct initialization; this code just sets each individual member
1177f22ef01cSRoman Divacky   // to the approprate value.  This makes bitfield support automatic;
1178f22ef01cSRoman Divacky   // the disadvantage is that the generated code is more difficult for
1179f22ef01cSRoman Divacky   // the optimizer, especially with bitfields.
1180f22ef01cSRoman Divacky   unsigned NumInitElements = E->getNumInits();
118117a519f9SDimitry Andric   RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl();
1182e580952dSDimitry Andric 
1183e7145dcbSDimitry Andric   // We'll need to enter cleanup scopes in case any of the element
1184e7145dcbSDimitry Andric   // initializers throws an exception.
1185e7145dcbSDimitry Andric   SmallVector<EHScopeStack::stable_iterator, 16> cleanups;
1186e7145dcbSDimitry Andric   llvm::Instruction *cleanupDominator = nullptr;
1187e7145dcbSDimitry Andric 
1188e7145dcbSDimitry Andric   unsigned curInitIndex = 0;
1189e7145dcbSDimitry Andric 
1190e7145dcbSDimitry Andric   // Emit initialization of base classes.
1191e7145dcbSDimitry Andric   if (auto *CXXRD = dyn_cast<CXXRecordDecl>(record)) {
1192e7145dcbSDimitry Andric     assert(E->getNumInits() >= CXXRD->getNumBases() &&
1193e7145dcbSDimitry Andric            "missing initializer for base class");
1194e7145dcbSDimitry Andric     for (auto &Base : CXXRD->bases()) {
1195e7145dcbSDimitry Andric       assert(!Base.isVirtual() && "should not see vbases here");
1196e7145dcbSDimitry Andric       auto *BaseRD = Base.getType()->getAsCXXRecordDecl();
1197e7145dcbSDimitry Andric       Address V = CGF.GetAddressOfDirectBaseInCompleteClass(
1198e7145dcbSDimitry Andric           Dest.getAddress(), CXXRD, BaseRD,
1199e7145dcbSDimitry Andric           /*isBaseVirtual*/ false);
1200e7145dcbSDimitry Andric       AggValueSlot AggSlot =
1201e7145dcbSDimitry Andric         AggValueSlot::forAddr(V, Qualifiers(),
1202e7145dcbSDimitry Andric                               AggValueSlot::IsDestructed,
1203e7145dcbSDimitry Andric                               AggValueSlot::DoesNotNeedGCBarriers,
1204e7145dcbSDimitry Andric                               AggValueSlot::IsNotAliased);
1205e7145dcbSDimitry Andric       CGF.EmitAggExpr(E->getInit(curInitIndex++), AggSlot);
1206e7145dcbSDimitry Andric 
1207e7145dcbSDimitry Andric       if (QualType::DestructionKind dtorKind =
1208e7145dcbSDimitry Andric               Base.getType().isDestructedType()) {
1209e7145dcbSDimitry Andric         CGF.pushDestroy(dtorKind, V, Base.getType());
1210e7145dcbSDimitry Andric         cleanups.push_back(CGF.EHStack.stable_begin());
1211e7145dcbSDimitry Andric       }
1212e7145dcbSDimitry Andric     }
1213e7145dcbSDimitry Andric   }
1214e7145dcbSDimitry Andric 
1215284c1978SDimitry Andric   // Prepare a 'this' for CXXDefaultInitExprs.
12160623d748SDimitry Andric   CodeGenFunction::FieldConstructionScope FCS(CGF, Dest.getAddress());
1217284c1978SDimitry Andric 
121817a519f9SDimitry Andric   if (record->isUnion()) {
1219f22ef01cSRoman Divacky     // Only initialize one field of a union. The field itself is
1220f22ef01cSRoman Divacky     // specified by the initializer list.
1221f22ef01cSRoman Divacky     if (!E->getInitializedFieldInUnion()) {
1222f22ef01cSRoman Divacky       // Empty union; we have nothing to do.
1223f22ef01cSRoman Divacky 
1224f22ef01cSRoman Divacky #ifndef NDEBUG
1225f22ef01cSRoman Divacky       // Make sure that it's really an empty and not a failure of
1226f22ef01cSRoman Divacky       // semantic analysis.
122759d1ed5bSDimitry Andric       for (const auto *Field : record->fields())
1228f22ef01cSRoman Divacky         assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
1229f22ef01cSRoman Divacky #endif
1230f22ef01cSRoman Divacky       return;
1231f22ef01cSRoman Divacky     }
1232f22ef01cSRoman Divacky 
1233f22ef01cSRoman Divacky     // FIXME: volatility
1234f22ef01cSRoman Divacky     FieldDecl *Field = E->getInitializedFieldInUnion();
1235f22ef01cSRoman Divacky 
1236cb4dff85SDimitry Andric     LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field);
1237f22ef01cSRoman Divacky     if (NumInitElements) {
1238f22ef01cSRoman Divacky       // Store the initializer into the field
123917a519f9SDimitry Andric       EmitInitializationToLValue(E->getInit(0), FieldLoc);
1240f22ef01cSRoman Divacky     } else {
12412754fe60SDimitry Andric       // Default-initialize to null.
124217a519f9SDimitry Andric       EmitNullInitializationToLValue(FieldLoc);
1243f22ef01cSRoman Divacky     }
1244f22ef01cSRoman Divacky 
1245f22ef01cSRoman Divacky     return;
1246f22ef01cSRoman Divacky   }
1247f22ef01cSRoman Divacky 
1248f22ef01cSRoman Divacky   // Here we iterate over the fields; this makes it simpler to both
1249f22ef01cSRoman Divacky   // default-initialize fields and skip over unnamed fields.
125059d1ed5bSDimitry Andric   for (const auto *field : record->fields()) {
125117a519f9SDimitry Andric     // We're done once we hit the flexible array member.
125217a519f9SDimitry Andric     if (field->getType()->isIncompleteArrayType())
1253f22ef01cSRoman Divacky       break;
1254f22ef01cSRoman Divacky 
125517a519f9SDimitry Andric     // Always skip anonymous bitfields.
125617a519f9SDimitry Andric     if (field->isUnnamedBitfield())
1257f22ef01cSRoman Divacky       continue;
1258f22ef01cSRoman Divacky 
125917a519f9SDimitry Andric     // We're done if we reach the end of the explicit initializers, we
126017a519f9SDimitry Andric     // have a zeroed object, and the rest of the fields are
126117a519f9SDimitry Andric     // zero-initializable.
126217a519f9SDimitry Andric     if (curInitIndex == NumInitElements && Dest.isZeroed() &&
12632754fe60SDimitry Andric         CGF.getTypes().isZeroInitializable(E->getType()))
12642754fe60SDimitry Andric       break;
12652754fe60SDimitry Andric 
1266cb4dff85SDimitry Andric 
126759d1ed5bSDimitry Andric     LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, field);
1268f22ef01cSRoman Divacky     // We never generate write-barries for initialized fields.
126917a519f9SDimitry Andric     LV.setNonGC(true);
12702754fe60SDimitry Andric 
127117a519f9SDimitry Andric     if (curInitIndex < NumInitElements) {
1272f22ef01cSRoman Divacky       // Store the initializer into the field.
127317a519f9SDimitry Andric       EmitInitializationToLValue(E->getInit(curInitIndex++), LV);
1274f22ef01cSRoman Divacky     } else {
1275f22ef01cSRoman Divacky       // We're out of initalizers; default-initialize to null
127617a519f9SDimitry Andric       EmitNullInitializationToLValue(LV);
127717a519f9SDimitry Andric     }
127817a519f9SDimitry Andric 
127917a519f9SDimitry Andric     // Push a destructor if necessary.
128017a519f9SDimitry Andric     // FIXME: if we have an array of structures, all explicitly
128117a519f9SDimitry Andric     // initialized, we can end up pushing a linear number of cleanups.
128217a519f9SDimitry Andric     bool pushedCleanup = false;
128317a519f9SDimitry Andric     if (QualType::DestructionKind dtorKind
128417a519f9SDimitry Andric           = field->getType().isDestructedType()) {
128517a519f9SDimitry Andric       assert(LV.isSimple());
128617a519f9SDimitry Andric       if (CGF.needsEHCleanup(dtorKind)) {
1287dff0c46cSDimitry Andric         if (!cleanupDominator)
12880623d748SDimitry Andric           cleanupDominator = CGF.Builder.CreateAlignedLoad(
12890623d748SDimitry Andric               CGF.Int8Ty,
12900623d748SDimitry Andric               llvm::Constant::getNullValue(CGF.Int8PtrTy),
12910623d748SDimitry Andric               CharUnits::One()); // placeholder
1292dff0c46cSDimitry Andric 
129317a519f9SDimitry Andric         CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(),
129417a519f9SDimitry Andric                         CGF.getDestroyer(dtorKind), false);
129517a519f9SDimitry Andric         cleanups.push_back(CGF.EHStack.stable_begin());
129617a519f9SDimitry Andric         pushedCleanup = true;
129717a519f9SDimitry Andric       }
1298f22ef01cSRoman Divacky     }
12992754fe60SDimitry Andric 
13002754fe60SDimitry Andric     // If the GEP didn't get used because of a dead zero init or something
13012754fe60SDimitry Andric     // else, clean it up for -O0 builds and general tidiness.
130217a519f9SDimitry Andric     if (!pushedCleanup && LV.isSimple())
13032754fe60SDimitry Andric       if (llvm::GetElementPtrInst *GEP =
13040623d748SDimitry Andric             dyn_cast<llvm::GetElementPtrInst>(LV.getPointer()))
13052754fe60SDimitry Andric         if (GEP->use_empty())
13062754fe60SDimitry Andric           GEP->eraseFromParent();
1307f22ef01cSRoman Divacky   }
130817a519f9SDimitry Andric 
130917a519f9SDimitry Andric   // Deactivate all the partial cleanups in reverse order, which
131017a519f9SDimitry Andric   // generally means popping them.
131117a519f9SDimitry Andric   for (unsigned i = cleanups.size(); i != 0; --i)
1312dff0c46cSDimitry Andric     CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator);
1313dff0c46cSDimitry Andric 
1314dff0c46cSDimitry Andric   // Destroy the placeholder if we made one.
1315dff0c46cSDimitry Andric   if (cleanupDominator)
1316dff0c46cSDimitry Andric     cleanupDominator->eraseFromParent();
1317f22ef01cSRoman Divacky }
1318f22ef01cSRoman Divacky 
13198f0fd8f6SDimitry Andric void AggExprEmitter::VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E) {
13208f0fd8f6SDimitry Andric   AggValueSlot Dest = EnsureSlot(E->getType());
13218f0fd8f6SDimitry Andric 
13220623d748SDimitry Andric   LValue DestLV = CGF.MakeAddrLValue(Dest.getAddress(), E->getType());
13238f0fd8f6SDimitry Andric   EmitInitializationToLValue(E->getBase(), DestLV);
13248f0fd8f6SDimitry Andric   VisitInitListExpr(E->getUpdater());
13258f0fd8f6SDimitry Andric }
13268f0fd8f6SDimitry Andric 
1327f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
1328f22ef01cSRoman Divacky //                        Entry Points into this File
1329f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
1330f22ef01cSRoman Divacky 
13312754fe60SDimitry Andric /// GetNumNonZeroBytesInInit - Get an approximate count of the number of
13322754fe60SDimitry Andric /// non-zero bytes that will be stored when outputting the initializer for the
13332754fe60SDimitry Andric /// specified initializer expression.
13343b0f4066SDimitry Andric static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
13353b0f4066SDimitry Andric   E = E->IgnoreParens();
13362754fe60SDimitry Andric 
13372754fe60SDimitry Andric   // 0 and 0.0 won't require any non-zero stores!
13383b0f4066SDimitry Andric   if (isSimpleZero(E, CGF)) return CharUnits::Zero();
13392754fe60SDimitry Andric 
13402754fe60SDimitry Andric   // If this is an initlist expr, sum up the size of sizes of the (present)
13412754fe60SDimitry Andric   // elements.  If this is something weird, assume the whole thing is non-zero.
13422754fe60SDimitry Andric   const InitListExpr *ILE = dyn_cast<InitListExpr>(E);
134359d1ed5bSDimitry Andric   if (!ILE || !CGF.getTypes().isZeroInitializable(ILE->getType()))
13443b0f4066SDimitry Andric     return CGF.getContext().getTypeSizeInChars(E->getType());
13452754fe60SDimitry Andric 
13462754fe60SDimitry Andric   // InitListExprs for structs have to be handled carefully.  If there are
13472754fe60SDimitry Andric   // reference members, we need to consider the size of the reference, not the
13482754fe60SDimitry Andric   // referencee.  InitListExprs for unions and arrays can't have references.
13492754fe60SDimitry Andric   if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
13502754fe60SDimitry Andric     if (!RT->isUnionType()) {
13512754fe60SDimitry Andric       RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
13523b0f4066SDimitry Andric       CharUnits NumNonZeroBytes = CharUnits::Zero();
13532754fe60SDimitry Andric 
13542754fe60SDimitry Andric       unsigned ILEElement = 0;
1355e7145dcbSDimitry Andric       if (auto *CXXRD = dyn_cast<CXXRecordDecl>(SD))
1356e7145dcbSDimitry Andric         while (ILEElement != CXXRD->getNumBases())
1357e7145dcbSDimitry Andric           NumNonZeroBytes +=
1358e7145dcbSDimitry Andric               GetNumNonZeroBytesInInit(ILE->getInit(ILEElement++), CGF);
135959d1ed5bSDimitry Andric       for (const auto *Field : SD->fields()) {
13602754fe60SDimitry Andric         // We're done once we hit the flexible array member or run out of
13612754fe60SDimitry Andric         // InitListExpr elements.
13622754fe60SDimitry Andric         if (Field->getType()->isIncompleteArrayType() ||
13632754fe60SDimitry Andric             ILEElement == ILE->getNumInits())
13642754fe60SDimitry Andric           break;
13652754fe60SDimitry Andric         if (Field->isUnnamedBitfield())
13662754fe60SDimitry Andric           continue;
13672754fe60SDimitry Andric 
13682754fe60SDimitry Andric         const Expr *E = ILE->getInit(ILEElement++);
13692754fe60SDimitry Andric 
13702754fe60SDimitry Andric         // Reference values are always non-null and have the width of a pointer.
13712754fe60SDimitry Andric         if (Field->getType()->isReferenceType())
13723b0f4066SDimitry Andric           NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits(
1373284c1978SDimitry Andric               CGF.getTarget().getPointerWidth(0));
13742754fe60SDimitry Andric         else
13752754fe60SDimitry Andric           NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
13762754fe60SDimitry Andric       }
13772754fe60SDimitry Andric 
13782754fe60SDimitry Andric       return NumNonZeroBytes;
13792754fe60SDimitry Andric     }
13802754fe60SDimitry Andric   }
13812754fe60SDimitry Andric 
13822754fe60SDimitry Andric 
13833b0f4066SDimitry Andric   CharUnits NumNonZeroBytes = CharUnits::Zero();
13842754fe60SDimitry Andric   for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
13852754fe60SDimitry Andric     NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);
13862754fe60SDimitry Andric   return NumNonZeroBytes;
13872754fe60SDimitry Andric }
13882754fe60SDimitry Andric 
13892754fe60SDimitry Andric /// CheckAggExprForMemSetUse - If the initializer is large and has a lot of
13902754fe60SDimitry Andric /// zeros in it, emit a memset and avoid storing the individual zeros.
13912754fe60SDimitry Andric ///
13922754fe60SDimitry Andric static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
13932754fe60SDimitry Andric                                      CodeGenFunction &CGF) {
13942754fe60SDimitry Andric   // If the slot is already known to be zeroed, nothing to do.  Don't mess with
13952754fe60SDimitry Andric   // volatile stores.
13960623d748SDimitry Andric   if (Slot.isZeroed() || Slot.isVolatile() || !Slot.getAddress().isValid())
139759d1ed5bSDimitry Andric     return;
13982754fe60SDimitry Andric 
13993b0f4066SDimitry Andric   // C++ objects with a user-declared constructor don't need zero'ing.
14003861d79fSDimitry Andric   if (CGF.getLangOpts().CPlusPlus)
14013b0f4066SDimitry Andric     if (const RecordType *RT = CGF.getContext()
14023b0f4066SDimitry Andric                        .getBaseElementType(E->getType())->getAs<RecordType>()) {
14033b0f4066SDimitry Andric       const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
14043b0f4066SDimitry Andric       if (RD->hasUserDeclaredConstructor())
14053b0f4066SDimitry Andric         return;
14063b0f4066SDimitry Andric     }
14073b0f4066SDimitry Andric 
14082754fe60SDimitry Andric   // If the type is 16-bytes or smaller, prefer individual stores over memset.
14090623d748SDimitry Andric   CharUnits Size = CGF.getContext().getTypeSizeInChars(E->getType());
14100623d748SDimitry Andric   if (Size <= CharUnits::fromQuantity(16))
14112754fe60SDimitry Andric     return;
14122754fe60SDimitry Andric 
14132754fe60SDimitry Andric   // Check to see if over 3/4 of the initializer are known to be zero.  If so,
14142754fe60SDimitry Andric   // we prefer to emit memset + individual stores for the rest.
14153b0f4066SDimitry Andric   CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF);
14160623d748SDimitry Andric   if (NumNonZeroBytes*4 > Size)
14172754fe60SDimitry Andric     return;
14182754fe60SDimitry Andric 
14192754fe60SDimitry Andric   // Okay, it seems like a good idea to use an initial memset, emit the call.
14200623d748SDimitry Andric   llvm::Constant *SizeVal = CGF.Builder.getInt64(Size.getQuantity());
14212754fe60SDimitry Andric 
14220623d748SDimitry Andric   Address Loc = Slot.getAddress();
14230623d748SDimitry Andric   Loc = CGF.Builder.CreateElementBitCast(Loc, CGF.Int8Ty);
14240623d748SDimitry Andric   CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, false);
14252754fe60SDimitry Andric 
14262754fe60SDimitry Andric   // Tell the AggExprEmitter that the slot is known zero.
14272754fe60SDimitry Andric   Slot.setZeroed();
14282754fe60SDimitry Andric }
14292754fe60SDimitry Andric 
14302754fe60SDimitry Andric 
14312754fe60SDimitry Andric 
14322754fe60SDimitry Andric 
1433f22ef01cSRoman Divacky /// EmitAggExpr - Emit the computation of the specified expression of aggregate
1434f22ef01cSRoman Divacky /// type.  The result is computed into DestPtr.  Note that if DestPtr is null,
1435f22ef01cSRoman Divacky /// the value of the aggregate expression is not needed.  If VolatileDest is
1436f22ef01cSRoman Divacky /// true, DestPtr cannot be 0.
14377ae0e2c9SDimitry Andric void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) {
1438139f7f9bSDimitry Andric   assert(E && hasAggregateEvaluationKind(E->getType()) &&
1439f22ef01cSRoman Divacky          "Invalid aggregate expression to emit");
14400623d748SDimitry Andric   assert((Slot.getAddress().isValid() || Slot.isIgnored()) &&
14412754fe60SDimitry Andric          "slot has bits but no address");
1442f22ef01cSRoman Divacky 
14432754fe60SDimitry Andric   // Optimize the slot if possible.
14442754fe60SDimitry Andric   CheckAggExprForMemSetUse(Slot, E, *this);
14452754fe60SDimitry Andric 
144697bc6c73SDimitry Andric   AggExprEmitter(*this, Slot, Slot.isIgnored()).Visit(const_cast<Expr*>(E));
1447f22ef01cSRoman Divacky }
1448f22ef01cSRoman Divacky 
1449f22ef01cSRoman Divacky LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
1450139f7f9bSDimitry Andric   assert(hasAggregateEvaluationKind(E->getType()) && "Invalid argument!");
14510623d748SDimitry Andric   Address Temp = CreateMemTemp(E->getType());
1452e580952dSDimitry Andric   LValue LV = MakeAddrLValue(Temp, E->getType());
14536122f3e6SDimitry Andric   EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed,
14546122f3e6SDimitry Andric                                          AggValueSlot::DoesNotNeedGCBarriers,
14556122f3e6SDimitry Andric                                          AggValueSlot::IsNotAliased));
1456e580952dSDimitry Andric   return LV;
1457f22ef01cSRoman Divacky }
1458f22ef01cSRoman Divacky 
14590623d748SDimitry Andric void CodeGenFunction::EmitAggregateCopy(Address DestPtr,
14600623d748SDimitry Andric                                         Address SrcPtr, QualType Ty,
14617ae0e2c9SDimitry Andric                                         bool isVolatile,
14623861d79fSDimitry Andric                                         bool isAssignment) {
1463f22ef01cSRoman Divacky   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
1464f22ef01cSRoman Divacky 
14653861d79fSDimitry Andric   if (getLangOpts().CPlusPlus) {
1466f22ef01cSRoman Divacky     if (const RecordType *RT = Ty->getAs<RecordType>()) {
1467f22ef01cSRoman Divacky       CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
1468f22ef01cSRoman Divacky       assert((Record->hasTrivialCopyConstructor() ||
14696122f3e6SDimitry Andric               Record->hasTrivialCopyAssignment() ||
14706122f3e6SDimitry Andric               Record->hasTrivialMoveConstructor() ||
147133956c43SDimitry Andric               Record->hasTrivialMoveAssignment() ||
147233956c43SDimitry Andric               Record->isUnion()) &&
1473139f7f9bSDimitry Andric              "Trying to aggregate-copy a type without a trivial copy/move "
1474f22ef01cSRoman Divacky              "constructor or assignment operator");
1475f22ef01cSRoman Divacky       // Ignore empty classes in C++.
1476f22ef01cSRoman Divacky       if (Record->isEmpty())
1477f22ef01cSRoman Divacky         return;
1478f22ef01cSRoman Divacky     }
1479f22ef01cSRoman Divacky   }
1480f22ef01cSRoman Divacky 
1481f22ef01cSRoman Divacky   // Aggregate assignment turns into llvm.memcpy.  This is almost valid per
1482f22ef01cSRoman Divacky   // C99 6.5.16.1p3, which states "If the value being stored in an object is
1483f22ef01cSRoman Divacky   // read from another object that overlaps in anyway the storage of the first
1484f22ef01cSRoman Divacky   // object, then the overlap shall be exact and the two objects shall have
1485f22ef01cSRoman Divacky   // qualified or unqualified versions of a compatible type."
1486f22ef01cSRoman Divacky   //
1487f22ef01cSRoman Divacky   // memcpy is not defined if the source and destination pointers are exactly
1488f22ef01cSRoman Divacky   // equal, but other compilers do this optimization, and almost every memcpy
1489f22ef01cSRoman Divacky   // implementation handles this case safely.  If there is a libc that does not
1490f22ef01cSRoman Divacky   // safely handle this, we can add a target hook.
1491f22ef01cSRoman Divacky 
14920623d748SDimitry Andric   // Get data size info for this aggregate. If this is an assignment,
14930623d748SDimitry Andric   // don't copy the tail padding, because we might be assigning into a
14940623d748SDimitry Andric   // base subobject where the tail padding is claimed.  Otherwise,
14950623d748SDimitry Andric   // copying it is fine.
14963861d79fSDimitry Andric   std::pair<CharUnits, CharUnits> TypeInfo;
14973861d79fSDimitry Andric   if (isAssignment)
14983861d79fSDimitry Andric     TypeInfo = getContext().getTypeInfoDataSizeInChars(Ty);
14993861d79fSDimitry Andric   else
15003861d79fSDimitry Andric     TypeInfo = getContext().getTypeInfoInChars(Ty);
1501f22ef01cSRoman Divacky 
150233956c43SDimitry Andric   llvm::Value *SizeVal = nullptr;
150333956c43SDimitry Andric   if (TypeInfo.first.isZero()) {
150433956c43SDimitry Andric     // But note that getTypeInfo returns 0 for a VLA.
150533956c43SDimitry Andric     if (auto *VAT = dyn_cast_or_null<VariableArrayType>(
150633956c43SDimitry Andric             getContext().getAsArrayType(Ty))) {
150733956c43SDimitry Andric       QualType BaseEltTy;
150833956c43SDimitry Andric       SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
150933956c43SDimitry Andric       TypeInfo = getContext().getTypeInfoDataSizeInChars(BaseEltTy);
151033956c43SDimitry Andric       std::pair<CharUnits, CharUnits> LastElementTypeInfo;
151133956c43SDimitry Andric       if (!isAssignment)
151233956c43SDimitry Andric         LastElementTypeInfo = getContext().getTypeInfoInChars(BaseEltTy);
151333956c43SDimitry Andric       assert(!TypeInfo.first.isZero());
151433956c43SDimitry Andric       SizeVal = Builder.CreateNUWMul(
151533956c43SDimitry Andric           SizeVal,
151633956c43SDimitry Andric           llvm::ConstantInt::get(SizeTy, TypeInfo.first.getQuantity()));
151733956c43SDimitry Andric       if (!isAssignment) {
151833956c43SDimitry Andric         SizeVal = Builder.CreateNUWSub(
151933956c43SDimitry Andric             SizeVal,
152033956c43SDimitry Andric             llvm::ConstantInt::get(SizeTy, TypeInfo.first.getQuantity()));
152133956c43SDimitry Andric         SizeVal = Builder.CreateNUWAdd(
152233956c43SDimitry Andric             SizeVal, llvm::ConstantInt::get(
152333956c43SDimitry Andric                          SizeTy, LastElementTypeInfo.first.getQuantity()));
152433956c43SDimitry Andric       }
152533956c43SDimitry Andric     }
152633956c43SDimitry Andric   }
152733956c43SDimitry Andric   if (!SizeVal) {
152833956c43SDimitry Andric     SizeVal = llvm::ConstantInt::get(SizeTy, TypeInfo.first.getQuantity());
152933956c43SDimitry Andric   }
1530f22ef01cSRoman Divacky 
1531f22ef01cSRoman Divacky   // FIXME: If we have a volatile struct, the optimizer can remove what might
1532f22ef01cSRoman Divacky   // appear to be `extra' memory ops:
1533f22ef01cSRoman Divacky   //
1534f22ef01cSRoman Divacky   // volatile struct { int i; } a, b;
1535f22ef01cSRoman Divacky   //
1536f22ef01cSRoman Divacky   // int main() {
1537f22ef01cSRoman Divacky   //   a = b;
1538f22ef01cSRoman Divacky   //   a = b;
1539f22ef01cSRoman Divacky   // }
1540f22ef01cSRoman Divacky   //
1541f22ef01cSRoman Divacky   // we need to use a different call here.  We use isVolatile to indicate when
1542f22ef01cSRoman Divacky   // either the source or the destination is volatile.
1543f22ef01cSRoman Divacky 
15440623d748SDimitry Andric   DestPtr = Builder.CreateElementBitCast(DestPtr, Int8Ty);
15450623d748SDimitry Andric   SrcPtr = Builder.CreateElementBitCast(SrcPtr, Int8Ty);
1546f22ef01cSRoman Divacky 
154717a519f9SDimitry Andric   // Don't do any of the memmove_collectable tests if GC isn't set.
1548dff0c46cSDimitry Andric   if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
154917a519f9SDimitry Andric     // fall through
155017a519f9SDimitry Andric   } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1551ffd1746dSEd Schouten     RecordDecl *Record = RecordTy->getDecl();
1552ffd1746dSEd Schouten     if (Record->hasObjectMember()) {
1553ffd1746dSEd Schouten       CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
1554ffd1746dSEd Schouten                                                     SizeVal);
1555ffd1746dSEd Schouten       return;
1556ffd1746dSEd Schouten     }
155717a519f9SDimitry Andric   } else if (Ty->isArrayType()) {
1558ffd1746dSEd Schouten     QualType BaseType = getContext().getBaseElementType(Ty);
1559ffd1746dSEd Schouten     if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
1560ffd1746dSEd Schouten       if (RecordTy->getDecl()->hasObjectMember()) {
1561ffd1746dSEd Schouten         CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
1562ffd1746dSEd Schouten                                                       SizeVal);
1563ffd1746dSEd Schouten         return;
1564ffd1746dSEd Schouten       }
1565ffd1746dSEd Schouten     }
1566ffd1746dSEd Schouten   }
1567ffd1746dSEd Schouten 
15680623d748SDimitry Andric   auto Inst = Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, isVolatile);
15690623d748SDimitry Andric 
15703861d79fSDimitry Andric   // Determine the metadata to describe the position of any padding in this
15713861d79fSDimitry Andric   // memcpy, as well as the TBAA tags for the members of the struct, in case
15723861d79fSDimitry Andric   // the optimizer wishes to expand it in to scalar memory operations.
15730623d748SDimitry Andric   if (llvm::MDNode *TBAAStructTag = CGM.getTBAAStructInfo(Ty))
15740623d748SDimitry Andric     Inst->setMetadata(llvm::LLVMContext::MD_tbaa_struct, TBAAStructTag);
1575dff0c46cSDimitry Andric }
1576