CGExprAgg.cpp revision 261991
1193326Sed//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This contains code to emit Aggregate Expr nodes as LLVM code.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#include "CodeGenFunction.h"
15249423Sdim#include "CGObjCRuntime.h"
16193326Sed#include "CodeGenModule.h"
17193326Sed#include "clang/AST/ASTContext.h"
18193326Sed#include "clang/AST/DeclCXX.h"
19234353Sdim#include "clang/AST/DeclTemplate.h"
20193326Sed#include "clang/AST/StmtVisitor.h"
21249423Sdim#include "llvm/IR/Constants.h"
22249423Sdim#include "llvm/IR/Function.h"
23249423Sdim#include "llvm/IR/GlobalVariable.h"
24249423Sdim#include "llvm/IR/Intrinsics.h"
25193326Sedusing namespace clang;
26193326Sedusing namespace CodeGen;
27193326Sed
28193326Sed//===----------------------------------------------------------------------===//
29193326Sed//                        Aggregate Expression Emitter
30193326Sed//===----------------------------------------------------------------------===//
31193326Sed
32193326Sednamespace  {
33199990Srdivackyclass AggExprEmitter : public StmtVisitor<AggExprEmitter> {
34193326Sed  CodeGenFunction &CGF;
35193326Sed  CGBuilderTy &Builder;
36218893Sdim  AggValueSlot Dest;
37208600Srdivacky
38226633Sdim  /// We want to use 'dest' as the return slot except under two
39226633Sdim  /// conditions:
40226633Sdim  ///   - The destination slot requires garbage collection, so we
41226633Sdim  ///     need to use the GC API.
42226633Sdim  ///   - The destination slot is potentially aliased.
43226633Sdim  bool shouldUseDestForReturnSlot() const {
44226633Sdim    return !(Dest.requiresGCollection() || Dest.isPotentiallyAliased());
45226633Sdim  }
46226633Sdim
47208600Srdivacky  ReturnValueSlot getReturnValueSlot() const {
48226633Sdim    if (!shouldUseDestForReturnSlot())
49226633Sdim      return ReturnValueSlot();
50208600Srdivacky
51218893Sdim    return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile());
52208600Srdivacky  }
53208600Srdivacky
54218893Sdim  AggValueSlot EnsureSlot(QualType T) {
55218893Sdim    if (!Dest.isIgnored()) return Dest;
56218893Sdim    return CGF.CreateAggTemp(T, "agg.tmp.ensured");
57218893Sdim  }
58239462Sdim  void EnsureDest(QualType T) {
59239462Sdim    if (!Dest.isIgnored()) return;
60239462Sdim    Dest = CGF.CreateAggTemp(T, "agg.tmp.ensured");
61239462Sdim  }
62218893Sdim
63193326Sedpublic:
64239462Sdim  AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest)
65239462Sdim    : CGF(cgf), Builder(CGF.Builder), Dest(Dest) {
66193326Sed  }
67193326Sed
68193326Sed  //===--------------------------------------------------------------------===//
69193326Sed  //                               Utilities
70193326Sed  //===--------------------------------------------------------------------===//
71193326Sed
72193326Sed  /// EmitAggLoadOfLValue - Given an expression with aggregate type that
73193326Sed  /// represents a value lvalue, this method emits the address of the lvalue,
74193326Sed  /// then loads the result into DestPtr.
75193326Sed  void EmitAggLoadOfLValue(const Expr *E);
76193326Sed
77193326Sed  /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
78239462Sdim  void EmitFinalDestCopy(QualType type, const LValue &src);
79239462Sdim  void EmitFinalDestCopy(QualType type, RValue src,
80239462Sdim                         CharUnits srcAlignment = CharUnits::Zero());
81239462Sdim  void EmitCopy(QualType type, const AggValueSlot &dest,
82239462Sdim                const AggValueSlot &src);
83193326Sed
84226633Sdim  void EmitMoveFromReturnSlot(const Expr *E, RValue Src);
85208600Srdivacky
86234353Sdim  void EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType,
87234353Sdim                     QualType elementType, InitListExpr *E);
88234353Sdim
89226633Sdim  AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) {
90234353Sdim    if (CGF.getLangOpts().getGC() && TypeRequiresGCollection(T))
91226633Sdim      return AggValueSlot::NeedsGCBarriers;
92226633Sdim    return AggValueSlot::DoesNotNeedGCBarriers;
93226633Sdim  }
94226633Sdim
95208600Srdivacky  bool TypeRequiresGCollection(QualType T);
96208600Srdivacky
97193326Sed  //===--------------------------------------------------------------------===//
98193326Sed  //                            Visitor Methods
99193326Sed  //===--------------------------------------------------------------------===//
100198092Srdivacky
101193326Sed  void VisitStmt(Stmt *S) {
102193326Sed    CGF.ErrorUnsupported(S, "aggregate expression");
103193326Sed  }
104193326Sed  void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
105221345Sdim  void VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
106221345Sdim    Visit(GE->getResultExpr());
107221345Sdim  }
108193326Sed  void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
109224145Sdim  void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
110224145Sdim    return Visit(E->getReplacement());
111224145Sdim  }
112193326Sed
113193326Sed  // l-values.
114234353Sdim  void VisitDeclRefExpr(DeclRefExpr *E) {
115234353Sdim    // For aggregates, we should always be able to emit the variable
116234353Sdim    // as an l-value unless it's a reference.  This is due to the fact
117234353Sdim    // that we can't actually ever see a normal l2r conversion on an
118234353Sdim    // aggregate in C++, and in C there's no language standard
119234353Sdim    // actively preventing us from listing variables in the captures
120234353Sdim    // list of a block.
121234353Sdim    if (E->getDecl()->getType()->isReferenceType()) {
122234353Sdim      if (CodeGenFunction::ConstantEmission result
123234353Sdim            = CGF.tryEmitAsConstant(E)) {
124239462Sdim        EmitFinalDestCopy(E->getType(), result.getReferenceLValue(CGF, E));
125234353Sdim        return;
126234353Sdim      }
127234353Sdim    }
128234353Sdim
129234353Sdim    EmitAggLoadOfLValue(E);
130234353Sdim  }
131234353Sdim
132193326Sed  void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
133193326Sed  void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
134193326Sed  void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
135224145Sdim  void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
136193326Sed  void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
137193326Sed    EmitAggLoadOfLValue(E);
138193326Sed  }
139193326Sed  void VisitPredefinedExpr(const PredefinedExpr *E) {
140198092Srdivacky    EmitAggLoadOfLValue(E);
141193326Sed  }
142198092Srdivacky
143193326Sed  // Operators.
144198092Srdivacky  void VisitCastExpr(CastExpr *E);
145193326Sed  void VisitCallExpr(const CallExpr *E);
146193326Sed  void VisitStmtExpr(const StmtExpr *E);
147193326Sed  void VisitBinaryOperator(const BinaryOperator *BO);
148198398Srdivacky  void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
149193326Sed  void VisitBinAssign(const BinaryOperator *E);
150193326Sed  void VisitBinComma(const BinaryOperator *E);
151193326Sed
152193326Sed  void VisitObjCMessageExpr(ObjCMessageExpr *E);
153193326Sed  void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
154193326Sed    EmitAggLoadOfLValue(E);
155193326Sed  }
156198092Srdivacky
157218893Sdim  void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
158198092Srdivacky  void VisitChooseExpr(const ChooseExpr *CE);
159193326Sed  void VisitInitListExpr(InitListExpr *E);
160201361Srdivacky  void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
161193326Sed  void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
162193326Sed    Visit(DAE->getExpr());
163193326Sed  }
164251662Sdim  void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
165251662Sdim    CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
166251662Sdim    Visit(DIE->getExpr());
167251662Sdim  }
168193326Sed  void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
169193326Sed  void VisitCXXConstructExpr(const CXXConstructExpr *E);
170234353Sdim  void VisitLambdaExpr(LambdaExpr *E);
171261991Sdim  void VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
172218893Sdim  void VisitExprWithCleanups(ExprWithCleanups *E);
173210299Sed  void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
174199482Srdivacky  void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
175224145Sdim  void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
176218893Sdim  void VisitOpaqueValueExpr(OpaqueValueExpr *E);
177218893Sdim
178234353Sdim  void VisitPseudoObjectExpr(PseudoObjectExpr *E) {
179234353Sdim    if (E->isGLValue()) {
180234353Sdim      LValue LV = CGF.EmitPseudoObjectLValue(E);
181239462Sdim      return EmitFinalDestCopy(E->getType(), LV);
182234353Sdim    }
183234353Sdim
184234353Sdim    CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType()));
185234353Sdim  }
186234353Sdim
187193326Sed  void VisitVAArgExpr(VAArgExpr *E);
188193326Sed
189224145Sdim  void EmitInitializationToLValue(Expr *E, LValue Address);
190224145Sdim  void EmitNullInitializationToLValue(LValue Address);
191193326Sed  //  case Expr::ChooseExprClass:
192200583Srdivacky  void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
193226633Sdim  void VisitAtomicExpr(AtomicExpr *E) {
194226633Sdim    CGF.EmitAtomicExpr(E, EnsureSlot(E->getType()).getAddr());
195226633Sdim  }
196193326Sed};
197193326Sed}  // end anonymous namespace.
198193326Sed
199193326Sed//===----------------------------------------------------------------------===//
200193326Sed//                                Utilities
201193326Sed//===----------------------------------------------------------------------===//
202193326Sed
203193326Sed/// EmitAggLoadOfLValue - Given an expression with aggregate type that
204193326Sed/// represents a value lvalue, this method emits the address of the lvalue,
205193326Sed/// then loads the result into DestPtr.
206193326Sedvoid AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
207193326Sed  LValue LV = CGF.EmitLValue(E);
208249423Sdim
209249423Sdim  // If the type of the l-value is atomic, then do an atomic load.
210249423Sdim  if (LV.getType()->isAtomicType()) {
211261991Sdim    CGF.EmitAtomicLoad(LV, E->getExprLoc(), Dest);
212249423Sdim    return;
213249423Sdim  }
214249423Sdim
215239462Sdim  EmitFinalDestCopy(E->getType(), LV);
216193326Sed}
217193326Sed
218208600Srdivacky/// \brief True if the given aggregate type requires special GC API calls.
219208600Srdivackybool AggExprEmitter::TypeRequiresGCollection(QualType T) {
220208600Srdivacky  // Only record types have members that might require garbage collection.
221208600Srdivacky  const RecordType *RecordTy = T->getAs<RecordType>();
222208600Srdivacky  if (!RecordTy) return false;
223208600Srdivacky
224208600Srdivacky  // Don't mess with non-trivial C++ types.
225208600Srdivacky  RecordDecl *Record = RecordTy->getDecl();
226208600Srdivacky  if (isa<CXXRecordDecl>(Record) &&
227249423Sdim      (cast<CXXRecordDecl>(Record)->hasNonTrivialCopyConstructor() ||
228208600Srdivacky       !cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
229208600Srdivacky    return false;
230208600Srdivacky
231208600Srdivacky  // Check whether the type has an object member.
232208600Srdivacky  return Record->hasObjectMember();
233208600Srdivacky}
234208600Srdivacky
235226633Sdim/// \brief Perform the final move to DestPtr if for some reason
236226633Sdim/// getReturnValueSlot() didn't use it directly.
237208600Srdivacky///
238208600Srdivacky/// The idea is that you do something like this:
239208600Srdivacky///   RValue Result = EmitSomething(..., getReturnValueSlot());
240226633Sdim///   EmitMoveFromReturnSlot(E, Result);
241226633Sdim///
242226633Sdim/// If nothing interferes, this will cause the result to be emitted
243226633Sdim/// directly into the return value slot.  Otherwise, a final move
244226633Sdim/// will be performed.
245239462Sdimvoid AggExprEmitter::EmitMoveFromReturnSlot(const Expr *E, RValue src) {
246226633Sdim  if (shouldUseDestForReturnSlot()) {
247226633Sdim    // Logically, Dest.getAddr() should equal Src.getAggregateAddr().
248226633Sdim    // The possibility of undef rvalues complicates that a lot,
249226633Sdim    // though, so we can't really assert.
250226633Sdim    return;
251210299Sed  }
252226633Sdim
253239462Sdim  // Otherwise, copy from there to the destination.
254239462Sdim  assert(Dest.getAddr() != src.getAggregateAddr());
255239462Sdim  std::pair<CharUnits, CharUnits> typeInfo =
256234982Sdim    CGF.getContext().getTypeInfoInChars(E->getType());
257239462Sdim  EmitFinalDestCopy(E->getType(), src, typeInfo.second);
258208600Srdivacky}
259208600Srdivacky
260193326Sed/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
261239462Sdimvoid AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src,
262239462Sdim                                       CharUnits srcAlign) {
263239462Sdim  assert(src.isAggregate() && "value must be aggregate value!");
264239462Sdim  LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddr(), type, srcAlign);
265239462Sdim  EmitFinalDestCopy(type, srcLV);
266239462Sdim}
267193326Sed
268239462Sdim/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
269239462Sdimvoid AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src) {
270218893Sdim  // If Dest is ignored, then we're evaluating an aggregate expression
271239462Sdim  // in a context that doesn't care about the result.  Note that loads
272239462Sdim  // from volatile l-values force the existence of a non-ignored
273239462Sdim  // destination.
274239462Sdim  if (Dest.isIgnored())
275239462Sdim    return;
276212904Sdim
277239462Sdim  AggValueSlot srcAgg =
278239462Sdim    AggValueSlot::forLValue(src, AggValueSlot::IsDestructed,
279239462Sdim                            needsGC(type), AggValueSlot::IsAliased);
280239462Sdim  EmitCopy(type, Dest, srcAgg);
281239462Sdim}
282193326Sed
283239462Sdim/// Perform a copy from the source into the destination.
284239462Sdim///
285239462Sdim/// \param type - the type of the aggregate being copied; qualifiers are
286239462Sdim///   ignored
287239462Sdimvoid AggExprEmitter::EmitCopy(QualType type, const AggValueSlot &dest,
288239462Sdim                              const AggValueSlot &src) {
289239462Sdim  if (dest.requiresGCollection()) {
290239462Sdim    CharUnits sz = CGF.getContext().getTypeSizeInChars(type);
291239462Sdim    llvm::Value *size = llvm::ConstantInt::get(CGF.SizeTy, sz.getQuantity());
292198092Srdivacky    CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
293239462Sdim                                                      dest.getAddr(),
294239462Sdim                                                      src.getAddr(),
295239462Sdim                                                      size);
296198092Srdivacky    return;
297198092Srdivacky  }
298239462Sdim
299193326Sed  // If the result of the assignment is used, copy the LHS there also.
300239462Sdim  // It's volatile if either side is.  Use the minimum alignment of
301239462Sdim  // the two sides.
302239462Sdim  CGF.EmitAggregateCopy(dest.getAddr(), src.getAddr(), type,
303239462Sdim                        dest.isVolatile() || src.isVolatile(),
304239462Sdim                        std::min(dest.getAlignment(), src.getAlignment()));
305193326Sed}
306193326Sed
307234353Sdim/// \brief Emit the initializer for a std::initializer_list initialized with a
308234353Sdim/// real initializer list.
309261991Sdimvoid
310261991SdimAggExprEmitter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
311261991Sdim  // Emit an array containing the elements.  The array is externally destructed
312261991Sdim  // if the std::initializer_list object is.
313261991Sdim  ASTContext &Ctx = CGF.getContext();
314261991Sdim  LValue Array = CGF.EmitLValue(E->getSubExpr());
315261991Sdim  assert(Array.isSimple() && "initializer_list array not a simple lvalue");
316261991Sdim  llvm::Value *ArrayPtr = Array.getAddress();
317234353Sdim
318261991Sdim  const ConstantArrayType *ArrayType =
319261991Sdim      Ctx.getAsConstantArrayType(E->getSubExpr()->getType());
320261991Sdim  assert(ArrayType && "std::initializer_list constructed from non-array");
321234353Sdim
322261991Sdim  // FIXME: Perform the checks on the field types in SemaInit.
323261991Sdim  RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl();
324261991Sdim  RecordDecl::field_iterator Field = Record->field_begin();
325261991Sdim  if (Field == Record->field_end()) {
326261991Sdim    CGF.ErrorUnsupported(E, "weird std::initializer_list");
327234353Sdim    return;
328234353Sdim  }
329234353Sdim
330234353Sdim  // Start pointer.
331261991Sdim  if (!Field->getType()->isPointerType() ||
332261991Sdim      !Ctx.hasSameType(Field->getType()->getPointeeType(),
333261991Sdim                       ArrayType->getElementType())) {
334261991Sdim    CGF.ErrorUnsupported(E, "weird std::initializer_list");
335234353Sdim    return;
336234353Sdim  }
337234353Sdim
338261991Sdim  AggValueSlot Dest = EnsureSlot(E->getType());
339261991Sdim  LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
340261991Sdim                                     Dest.getAlignment());
341261991Sdim  LValue Start = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
342261991Sdim  llvm::Value *Zero = llvm::ConstantInt::get(CGF.PtrDiffTy, 0);
343261991Sdim  llvm::Value *IdxStart[] = { Zero, Zero };
344261991Sdim  llvm::Value *ArrayStart =
345261991Sdim      Builder.CreateInBoundsGEP(ArrayPtr, IdxStart, "arraystart");
346261991Sdim  CGF.EmitStoreThroughLValue(RValue::get(ArrayStart), Start);
347261991Sdim  ++Field;
348261991Sdim
349261991Sdim  if (Field == Record->field_end()) {
350261991Sdim    CGF.ErrorUnsupported(E, "weird std::initializer_list");
351234353Sdim    return;
352234353Sdim  }
353261991Sdim
354261991Sdim  llvm::Value *Size = Builder.getInt(ArrayType->getSize());
355261991Sdim  LValue EndOrLength = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
356261991Sdim  if (Field->getType()->isPointerType() &&
357261991Sdim      Ctx.hasSameType(Field->getType()->getPointeeType(),
358261991Sdim                      ArrayType->getElementType())) {
359234353Sdim    // End pointer.
360261991Sdim    llvm::Value *IdxEnd[] = { Zero, Size };
361261991Sdim    llvm::Value *ArrayEnd =
362261991Sdim        Builder.CreateInBoundsGEP(ArrayPtr, IdxEnd, "arrayend");
363261991Sdim    CGF.EmitStoreThroughLValue(RValue::get(ArrayEnd), EndOrLength);
364261991Sdim  } else if (Ctx.hasSameType(Field->getType(), Ctx.getSizeType())) {
365234353Sdim    // Length.
366261991Sdim    CGF.EmitStoreThroughLValue(RValue::get(Size), EndOrLength);
367234353Sdim  } else {
368261991Sdim    CGF.ErrorUnsupported(E, "weird std::initializer_list");
369234353Sdim    return;
370234353Sdim  }
371234353Sdim}
372234353Sdim
373234353Sdim/// \brief Emit initialization of an array from an initializer list.
374234353Sdimvoid AggExprEmitter::EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType,
375234353Sdim                                   QualType elementType, InitListExpr *E) {
376234353Sdim  uint64_t NumInitElements = E->getNumInits();
377234353Sdim
378234353Sdim  uint64_t NumArrayElements = AType->getNumElements();
379234353Sdim  assert(NumInitElements <= NumArrayElements);
380234353Sdim
381234353Sdim  // DestPtr is an array*.  Construct an elementType* by drilling
382234353Sdim  // down a level.
383234353Sdim  llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
384234353Sdim  llvm::Value *indices[] = { zero, zero };
385234353Sdim  llvm::Value *begin =
386234353Sdim    Builder.CreateInBoundsGEP(DestPtr, indices, "arrayinit.begin");
387234353Sdim
388234353Sdim  // Exception safety requires us to destroy all the
389234353Sdim  // already-constructed members if an initializer throws.
390234353Sdim  // For that, we'll need an EH cleanup.
391234353Sdim  QualType::DestructionKind dtorKind = elementType.isDestructedType();
392234353Sdim  llvm::AllocaInst *endOfInit = 0;
393234353Sdim  EHScopeStack::stable_iterator cleanup;
394234353Sdim  llvm::Instruction *cleanupDominator = 0;
395234353Sdim  if (CGF.needsEHCleanup(dtorKind)) {
396234353Sdim    // In principle we could tell the cleanup where we are more
397234353Sdim    // directly, but the control flow can get so varied here that it
398234353Sdim    // would actually be quite complex.  Therefore we go through an
399234353Sdim    // alloca.
400234353Sdim    endOfInit = CGF.CreateTempAlloca(begin->getType(),
401234353Sdim                                     "arrayinit.endOfInit");
402234353Sdim    cleanupDominator = Builder.CreateStore(begin, endOfInit);
403234353Sdim    CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType,
404234353Sdim                                         CGF.getDestroyer(dtorKind));
405234353Sdim    cleanup = CGF.EHStack.stable_begin();
406234353Sdim
407234353Sdim  // Otherwise, remember that we didn't need a cleanup.
408234353Sdim  } else {
409234353Sdim    dtorKind = QualType::DK_none;
410234353Sdim  }
411234353Sdim
412234353Sdim  llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1);
413234353Sdim
414234353Sdim  // The 'current element to initialize'.  The invariants on this
415234353Sdim  // variable are complicated.  Essentially, after each iteration of
416234353Sdim  // the loop, it points to the last initialized element, except
417234353Sdim  // that it points to the beginning of the array before any
418234353Sdim  // elements have been initialized.
419234353Sdim  llvm::Value *element = begin;
420234353Sdim
421234353Sdim  // Emit the explicit initializers.
422234353Sdim  for (uint64_t i = 0; i != NumInitElements; ++i) {
423234353Sdim    // Advance to the next element.
424234353Sdim    if (i > 0) {
425234353Sdim      element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element");
426234353Sdim
427234353Sdim      // Tell the cleanup that it needs to destroy up to this
428234353Sdim      // element.  TODO: some of these stores can be trivially
429234353Sdim      // observed to be unnecessary.
430234353Sdim      if (endOfInit) Builder.CreateStore(element, endOfInit);
431234353Sdim    }
432234353Sdim
433261991Sdim    LValue elementLV = CGF.MakeAddrLValue(element, elementType);
434261991Sdim    EmitInitializationToLValue(E->getInit(i), elementLV);
435234353Sdim  }
436234353Sdim
437234353Sdim  // Check whether there's a non-trivial array-fill expression.
438234353Sdim  // Note that this will be a CXXConstructExpr even if the element
439234353Sdim  // type is an array (or array of array, etc.) of class type.
440234353Sdim  Expr *filler = E->getArrayFiller();
441234353Sdim  bool hasTrivialFiller = true;
442234353Sdim  if (CXXConstructExpr *cons = dyn_cast_or_null<CXXConstructExpr>(filler)) {
443234353Sdim    assert(cons->getConstructor()->isDefaultConstructor());
444234353Sdim    hasTrivialFiller = cons->getConstructor()->isTrivial();
445234353Sdim  }
446234353Sdim
447234353Sdim  // Any remaining elements need to be zero-initialized, possibly
448234353Sdim  // using the filler expression.  We can skip this if the we're
449234353Sdim  // emitting to zeroed memory.
450234353Sdim  if (NumInitElements != NumArrayElements &&
451234353Sdim      !(Dest.isZeroed() && hasTrivialFiller &&
452234353Sdim        CGF.getTypes().isZeroInitializable(elementType))) {
453234353Sdim
454234353Sdim    // Use an actual loop.  This is basically
455234353Sdim    //   do { *array++ = filler; } while (array != end);
456234353Sdim
457234353Sdim    // Advance to the start of the rest of the array.
458234353Sdim    if (NumInitElements) {
459234353Sdim      element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start");
460234353Sdim      if (endOfInit) Builder.CreateStore(element, endOfInit);
461234353Sdim    }
462234353Sdim
463234353Sdim    // Compute the end of the array.
464234353Sdim    llvm::Value *end = Builder.CreateInBoundsGEP(begin,
465234353Sdim                      llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements),
466234353Sdim                                                 "arrayinit.end");
467234353Sdim
468234353Sdim    llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
469234353Sdim    llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body");
470234353Sdim
471234353Sdim    // Jump into the body.
472234353Sdim    CGF.EmitBlock(bodyBB);
473234353Sdim    llvm::PHINode *currentElement =
474234353Sdim      Builder.CreatePHI(element->getType(), 2, "arrayinit.cur");
475234353Sdim    currentElement->addIncoming(element, entryBB);
476234353Sdim
477234353Sdim    // Emit the actual filler expression.
478234353Sdim    LValue elementLV = CGF.MakeAddrLValue(currentElement, elementType);
479234353Sdim    if (filler)
480234353Sdim      EmitInitializationToLValue(filler, elementLV);
481234353Sdim    else
482234353Sdim      EmitNullInitializationToLValue(elementLV);
483234353Sdim
484234353Sdim    // Move on to the next element.
485234353Sdim    llvm::Value *nextElement =
486234353Sdim      Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next");
487234353Sdim
488234353Sdim    // Tell the EH cleanup that we finished with the last element.
489234353Sdim    if (endOfInit) Builder.CreateStore(nextElement, endOfInit);
490234353Sdim
491234353Sdim    // Leave the loop if we're done.
492234353Sdim    llvm::Value *done = Builder.CreateICmpEQ(nextElement, end,
493234353Sdim                                             "arrayinit.done");
494234353Sdim    llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end");
495234353Sdim    Builder.CreateCondBr(done, endBB, bodyBB);
496234353Sdim    currentElement->addIncoming(nextElement, Builder.GetInsertBlock());
497234353Sdim
498234353Sdim    CGF.EmitBlock(endBB);
499234353Sdim  }
500234353Sdim
501234353Sdim  // Leave the partial-array cleanup if we entered one.
502234353Sdim  if (dtorKind) CGF.DeactivateCleanupBlock(cleanup, cleanupDominator);
503234353Sdim}
504234353Sdim
505193326Sed//===----------------------------------------------------------------------===//
506193326Sed//                            Visitor Methods
507193326Sed//===----------------------------------------------------------------------===//
508193326Sed
509224145Sdimvoid AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){
510224145Sdim  Visit(E->GetTemporaryExpr());
511224145Sdim}
512224145Sdim
513218893Sdimvoid AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) {
514239462Sdim  EmitFinalDestCopy(e->getType(), CGF.getOpaqueLValueMapping(e));
515218893Sdim}
516218893Sdim
517224145Sdimvoid
518224145SdimAggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
519249423Sdim  if (Dest.isPotentiallyAliased() &&
520249423Sdim      E->getType().isPODType(CGF.getContext())) {
521224145Sdim    // For a POD type, just emit a load of the lvalue + a copy, because our
522224145Sdim    // compound literal might alias the destination.
523224145Sdim    EmitAggLoadOfLValue(E);
524224145Sdim    return;
525224145Sdim  }
526224145Sdim
527224145Sdim  AggValueSlot Slot = EnsureSlot(E->getType());
528224145Sdim  CGF.EmitAggExpr(E->getInitializer(), Slot);
529224145Sdim}
530224145Sdim
531249423Sdim/// Attempt to look through various unimportant expressions to find a
532249423Sdim/// cast of the given kind.
533249423Sdimstatic Expr *findPeephole(Expr *op, CastKind kind) {
534249423Sdim  while (true) {
535249423Sdim    op = op->IgnoreParens();
536249423Sdim    if (CastExpr *castE = dyn_cast<CastExpr>(op)) {
537249423Sdim      if (castE->getCastKind() == kind)
538249423Sdim        return castE->getSubExpr();
539249423Sdim      if (castE->getCastKind() == CK_NoOp)
540249423Sdim        continue;
541249423Sdim    }
542249423Sdim    return 0;
543249423Sdim  }
544249423Sdim}
545224145Sdim
546198092Srdivackyvoid AggExprEmitter::VisitCastExpr(CastExpr *E) {
547198092Srdivacky  switch (E->getCastKind()) {
548212904Sdim  case CK_Dynamic: {
549243830Sdim    // FIXME: Can this actually happen? We have no test coverage for it.
550208600Srdivacky    assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
551243830Sdim    LValue LV = CGF.EmitCheckedLValue(E->getSubExpr(),
552243830Sdim                                      CodeGenFunction::TCK_Load);
553208600Srdivacky    // FIXME: Do we also need to handle property references here?
554208600Srdivacky    if (LV.isSimple())
555208600Srdivacky      CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
556208600Srdivacky    else
557208600Srdivacky      CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
558208600Srdivacky
559218893Sdim    if (!Dest.isIgnored())
560218893Sdim      CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
561208600Srdivacky    break;
562208600Srdivacky  }
563208600Srdivacky
564212904Sdim  case CK_ToUnion: {
565221345Sdim    if (Dest.isIgnored()) break;
566221345Sdim
567198092Srdivacky    // GCC union extension
568212904Sdim    QualType Ty = E->getSubExpr()->getType();
569212904Sdim    QualType PtrTy = CGF.getContext().getPointerType(Ty);
570218893Sdim    llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(),
571193401Sed                                                 CGF.ConvertType(PtrTy));
572224145Sdim    EmitInitializationToLValue(E->getSubExpr(),
573224145Sdim                               CGF.MakeAddrLValue(CastPtr, Ty));
574198092Srdivacky    break;
575193326Sed  }
576193326Sed
577212904Sdim  case CK_DerivedToBase:
578212904Sdim  case CK_BaseToDerived:
579212904Sdim  case CK_UncheckedDerivedToBase: {
580226633Sdim    llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: "
581208600Srdivacky                "should have been unpacked before we got here");
582208600Srdivacky  }
583208600Srdivacky
584249423Sdim  case CK_NonAtomicToAtomic:
585249423Sdim  case CK_AtomicToNonAtomic: {
586249423Sdim    bool isToAtomic = (E->getCastKind() == CK_NonAtomicToAtomic);
587249423Sdim
588249423Sdim    // Determine the atomic and value types.
589249423Sdim    QualType atomicType = E->getSubExpr()->getType();
590249423Sdim    QualType valueType = E->getType();
591249423Sdim    if (isToAtomic) std::swap(atomicType, valueType);
592249423Sdim
593249423Sdim    assert(atomicType->isAtomicType());
594249423Sdim    assert(CGF.getContext().hasSameUnqualifiedType(valueType,
595249423Sdim                          atomicType->castAs<AtomicType>()->getValueType()));
596249423Sdim
597249423Sdim    // Just recurse normally if we're ignoring the result or the
598249423Sdim    // atomic type doesn't change representation.
599249423Sdim    if (Dest.isIgnored() || !CGF.CGM.isPaddedAtomicType(atomicType)) {
600249423Sdim      return Visit(E->getSubExpr());
601249423Sdim    }
602249423Sdim
603249423Sdim    CastKind peepholeTarget =
604249423Sdim      (isToAtomic ? CK_AtomicToNonAtomic : CK_NonAtomicToAtomic);
605249423Sdim
606249423Sdim    // These two cases are reverses of each other; try to peephole them.
607249423Sdim    if (Expr *op = findPeephole(E->getSubExpr(), peepholeTarget)) {
608249423Sdim      assert(CGF.getContext().hasSameUnqualifiedType(op->getType(),
609249423Sdim                                                     E->getType()) &&
610249423Sdim           "peephole significantly changed types?");
611249423Sdim      return Visit(op);
612249423Sdim    }
613249423Sdim
614249423Sdim    // If we're converting an r-value of non-atomic type to an r-value
615261991Sdim    // of atomic type, just emit directly into the relevant sub-object.
616249423Sdim    if (isToAtomic) {
617261991Sdim      AggValueSlot valueDest = Dest;
618261991Sdim      if (!valueDest.isIgnored() && CGF.CGM.isPaddedAtomicType(atomicType)) {
619261991Sdim        // Zero-initialize.  (Strictly speaking, we only need to intialize
620261991Sdim        // the padding at the end, but this is simpler.)
621261991Sdim        if (!Dest.isZeroed())
622261991Sdim          CGF.EmitNullInitialization(Dest.getAddr(), atomicType);
623261991Sdim
624261991Sdim        // Build a GEP to refer to the subobject.
625261991Sdim        llvm::Value *valueAddr =
626261991Sdim            CGF.Builder.CreateStructGEP(valueDest.getAddr(), 0);
627261991Sdim        valueDest = AggValueSlot::forAddr(valueAddr,
628261991Sdim                                          valueDest.getAlignment(),
629261991Sdim                                          valueDest.getQualifiers(),
630261991Sdim                                          valueDest.isExternallyDestructed(),
631261991Sdim                                          valueDest.requiresGCollection(),
632261991Sdim                                          valueDest.isPotentiallyAliased(),
633261991Sdim                                          AggValueSlot::IsZeroed);
634261991Sdim      }
635261991Sdim
636261991Sdim      CGF.EmitAggExpr(E->getSubExpr(), valueDest);
637249423Sdim      return;
638249423Sdim    }
639249423Sdim
640249423Sdim    // Otherwise, we're converting an atomic type to a non-atomic type.
641261991Sdim    // Make an atomic temporary, emit into that, and then copy the value out.
642249423Sdim    AggValueSlot atomicSlot =
643249423Sdim      CGF.CreateAggTemp(atomicType, "atomic-to-nonatomic.temp");
644249423Sdim    CGF.EmitAggExpr(E->getSubExpr(), atomicSlot);
645249423Sdim
646249423Sdim    llvm::Value *valueAddr =
647249423Sdim      Builder.CreateStructGEP(atomicSlot.getAddr(), 0);
648249423Sdim    RValue rvalue = RValue::getAggregate(valueAddr, atomicSlot.isVolatile());
649249423Sdim    return EmitFinalDestCopy(valueType, rvalue);
650249423Sdim  }
651249423Sdim
652239462Sdim  case CK_LValueToRValue:
653239462Sdim    // If we're loading from a volatile type, force the destination
654239462Sdim    // into existence.
655239462Sdim    if (E->getSubExpr()->getType().isVolatileQualified()) {
656239462Sdim      EnsureDest(E->getType());
657239462Sdim      return Visit(E->getSubExpr());
658239462Sdim    }
659249423Sdim
660239462Sdim    // fallthrough
661239462Sdim
662212904Sdim  case CK_NoOp:
663212904Sdim  case CK_UserDefinedConversion:
664212904Sdim  case CK_ConstructorConversion:
665198092Srdivacky    assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
666198092Srdivacky                                                   E->getType()) &&
667198092Srdivacky           "Implicit cast types must be compatible");
668198092Srdivacky    Visit(E->getSubExpr());
669198092Srdivacky    break;
670218893Sdim
671212904Sdim  case CK_LValueBitCast:
672218893Sdim    llvm_unreachable("should not be emitting lvalue bitcast as rvalue");
673221345Sdim
674218893Sdim  case CK_Dependent:
675218893Sdim  case CK_BitCast:
676218893Sdim  case CK_ArrayToPointerDecay:
677218893Sdim  case CK_FunctionToPointerDecay:
678218893Sdim  case CK_NullToPointer:
679218893Sdim  case CK_NullToMemberPointer:
680218893Sdim  case CK_BaseToDerivedMemberPointer:
681218893Sdim  case CK_DerivedToBaseMemberPointer:
682218893Sdim  case CK_MemberPointerToBoolean:
683234353Sdim  case CK_ReinterpretMemberPointer:
684218893Sdim  case CK_IntegralToPointer:
685218893Sdim  case CK_PointerToIntegral:
686218893Sdim  case CK_PointerToBoolean:
687218893Sdim  case CK_ToVoid:
688218893Sdim  case CK_VectorSplat:
689218893Sdim  case CK_IntegralCast:
690218893Sdim  case CK_IntegralToBoolean:
691218893Sdim  case CK_IntegralToFloating:
692218893Sdim  case CK_FloatingToIntegral:
693218893Sdim  case CK_FloatingToBoolean:
694218893Sdim  case CK_FloatingCast:
695226633Sdim  case CK_CPointerToObjCPointerCast:
696226633Sdim  case CK_BlockPointerToObjCPointerCast:
697218893Sdim  case CK_AnyPointerToBlockPointerCast:
698218893Sdim  case CK_ObjCObjectLValueCast:
699218893Sdim  case CK_FloatingRealToComplex:
700218893Sdim  case CK_FloatingComplexToReal:
701218893Sdim  case CK_FloatingComplexToBoolean:
702218893Sdim  case CK_FloatingComplexCast:
703218893Sdim  case CK_FloatingComplexToIntegralComplex:
704218893Sdim  case CK_IntegralRealToComplex:
705218893Sdim  case CK_IntegralComplexToReal:
706218893Sdim  case CK_IntegralComplexToBoolean:
707218893Sdim  case CK_IntegralComplexCast:
708218893Sdim  case CK_IntegralComplexToFloatingComplex:
709226633Sdim  case CK_ARCProduceObject:
710226633Sdim  case CK_ARCConsumeObject:
711226633Sdim  case CK_ARCReclaimReturnedObject:
712226633Sdim  case CK_ARCExtendBlockObject:
713234353Sdim  case CK_CopyAndAutoreleaseBlockObject:
714243830Sdim  case CK_BuiltinFnToFnPtr:
715249423Sdim  case CK_ZeroToOCLEvent:
716218893Sdim    llvm_unreachable("cast kind invalid for aggregate types");
717198398Srdivacky  }
718193326Sed}
719193326Sed
720193326Sedvoid AggExprEmitter::VisitCallExpr(const CallExpr *E) {
721193326Sed  if (E->getCallReturnType()->isReferenceType()) {
722193326Sed    EmitAggLoadOfLValue(E);
723193326Sed    return;
724193326Sed  }
725198092Srdivacky
726208600Srdivacky  RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
727226633Sdim  EmitMoveFromReturnSlot(E, RV);
728193326Sed}
729193326Sed
730193326Sedvoid AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
731208600Srdivacky  RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
732226633Sdim  EmitMoveFromReturnSlot(E, RV);
733193326Sed}
734193326Sed
735193326Sedvoid AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
736218893Sdim  CGF.EmitIgnoredExpr(E->getLHS());
737218893Sdim  Visit(E->getRHS());
738193326Sed}
739193326Sed
740193326Sedvoid AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
741218893Sdim  CodeGenFunction::StmtExprEvaluation eval(CGF);
742218893Sdim  CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
743193326Sed}
744193326Sed
745193326Sedvoid AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
746212904Sdim  if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
747198398Srdivacky    VisitPointerToDataMemberBinaryOperator(E);
748198398Srdivacky  else
749198398Srdivacky    CGF.ErrorUnsupported(E, "aggregate binary expression");
750193326Sed}
751193326Sed
752198398Srdivackyvoid AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
753198398Srdivacky                                                    const BinaryOperator *E) {
754198398Srdivacky  LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
755239462Sdim  EmitFinalDestCopy(E->getType(), LV);
756198398Srdivacky}
757198398Srdivacky
758239462Sdim/// Is the value of the given expression possibly a reference to or
759239462Sdim/// into a __block variable?
760239462Sdimstatic bool isBlockVarRef(const Expr *E) {
761239462Sdim  // Make sure we look through parens.
762239462Sdim  E = E->IgnoreParens();
763239462Sdim
764239462Sdim  // Check for a direct reference to a __block variable.
765239462Sdim  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
766239462Sdim    const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
767239462Sdim    return (var && var->hasAttr<BlocksAttr>());
768239462Sdim  }
769239462Sdim
770239462Sdim  // More complicated stuff.
771239462Sdim
772239462Sdim  // Binary operators.
773239462Sdim  if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) {
774239462Sdim    // For an assignment or pointer-to-member operation, just care
775239462Sdim    // about the LHS.
776239462Sdim    if (op->isAssignmentOp() || op->isPtrMemOp())
777239462Sdim      return isBlockVarRef(op->getLHS());
778239462Sdim
779239462Sdim    // For a comma, just care about the RHS.
780239462Sdim    if (op->getOpcode() == BO_Comma)
781239462Sdim      return isBlockVarRef(op->getRHS());
782239462Sdim
783239462Sdim    // FIXME: pointer arithmetic?
784239462Sdim    return false;
785239462Sdim
786239462Sdim  // Check both sides of a conditional operator.
787239462Sdim  } else if (const AbstractConditionalOperator *op
788239462Sdim               = dyn_cast<AbstractConditionalOperator>(E)) {
789239462Sdim    return isBlockVarRef(op->getTrueExpr())
790239462Sdim        || isBlockVarRef(op->getFalseExpr());
791239462Sdim
792239462Sdim  // OVEs are required to support BinaryConditionalOperators.
793239462Sdim  } else if (const OpaqueValueExpr *op
794239462Sdim               = dyn_cast<OpaqueValueExpr>(E)) {
795239462Sdim    if (const Expr *src = op->getSourceExpr())
796239462Sdim      return isBlockVarRef(src);
797239462Sdim
798239462Sdim  // Casts are necessary to get things like (*(int*)&var) = foo().
799239462Sdim  // We don't really care about the kind of cast here, except
800239462Sdim  // we don't want to look through l2r casts, because it's okay
801239462Sdim  // to get the *value* in a __block variable.
802239462Sdim  } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) {
803239462Sdim    if (cast->getCastKind() == CK_LValueToRValue)
804239462Sdim      return false;
805239462Sdim    return isBlockVarRef(cast->getSubExpr());
806239462Sdim
807239462Sdim  // Handle unary operators.  Again, just aggressively look through
808239462Sdim  // it, ignoring the operation.
809239462Sdim  } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) {
810239462Sdim    return isBlockVarRef(uop->getSubExpr());
811239462Sdim
812239462Sdim  // Look into the base of a field access.
813239462Sdim  } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) {
814239462Sdim    return isBlockVarRef(mem->getBase());
815239462Sdim
816239462Sdim  // Look into the base of a subscript.
817239462Sdim  } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) {
818239462Sdim    return isBlockVarRef(sub->getBase());
819239462Sdim  }
820239462Sdim
821239462Sdim  return false;
822239462Sdim}
823239462Sdim
824193326Sedvoid AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
825193326Sed  // For an assignment to work, the value on the right has
826193326Sed  // to be compatible with the value on the left.
827193326Sed  assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
828193326Sed                                                 E->getRHS()->getType())
829193326Sed         && "Invalid assignment");
830218893Sdim
831239462Sdim  // If the LHS might be a __block variable, and the RHS can
832239462Sdim  // potentially cause a block copy, we need to evaluate the RHS first
833239462Sdim  // so that the assignment goes the right place.
834239462Sdim  // This is pretty semantically fragile.
835239462Sdim  if (isBlockVarRef(E->getLHS()) &&
836239462Sdim      E->getRHS()->HasSideEffects(CGF.getContext())) {
837239462Sdim    // Ensure that we have a destination, and evaluate the RHS into that.
838239462Sdim    EnsureDest(E->getRHS()->getType());
839239462Sdim    Visit(E->getRHS());
840239462Sdim
841239462Sdim    // Now emit the LHS and copy into it.
842243830Sdim    LValue LHS = CGF.EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
843239462Sdim
844249423Sdim    // That copy is an atomic copy if the LHS is atomic.
845249423Sdim    if (LHS.getType()->isAtomicType()) {
846249423Sdim      CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
847249423Sdim      return;
848249423Sdim    }
849249423Sdim
850239462Sdim    EmitCopy(E->getLHS()->getType(),
851239462Sdim             AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
852239462Sdim                                     needsGC(E->getLHS()->getType()),
853239462Sdim                                     AggValueSlot::IsAliased),
854239462Sdim             Dest);
855239462Sdim    return;
856239462Sdim  }
857221345Sdim
858193326Sed  LValue LHS = CGF.EmitLValue(E->getLHS());
859193326Sed
860249423Sdim  // If we have an atomic type, evaluate into the destination and then
861249423Sdim  // do an atomic copy.
862249423Sdim  if (LHS.getType()->isAtomicType()) {
863249423Sdim    EnsureDest(E->getRHS()->getType());
864249423Sdim    Visit(E->getRHS());
865249423Sdim    CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
866249423Sdim    return;
867249423Sdim  }
868249423Sdim
869234353Sdim  // Codegen the RHS so that it stores directly into the LHS.
870234353Sdim  AggValueSlot LHSSlot =
871234353Sdim    AggValueSlot::forLValue(LHS, AggValueSlot::IsDestructed,
872234353Sdim                            needsGC(E->getLHS()->getType()),
873234353Sdim                            AggValueSlot::IsAliased);
874249423Sdim  // A non-volatile aggregate destination might have volatile member.
875249423Sdim  if (!LHSSlot.isVolatile() &&
876249423Sdim      CGF.hasVolatileMember(E->getLHS()->getType()))
877249423Sdim    LHSSlot.setVolatile(true);
878249423Sdim
879239462Sdim  CGF.EmitAggExpr(E->getRHS(), LHSSlot);
880239462Sdim
881239462Sdim  // Copy into the destination if the assignment isn't ignored.
882239462Sdim  EmitFinalDestCopy(E->getType(), LHS);
883193326Sed}
884193326Sed
885218893Sdimvoid AggExprEmitter::
886218893SdimVisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
887193326Sed  llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
888193326Sed  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
889193326Sed  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
890198092Srdivacky
891218893Sdim  // Bind the common expression if necessary.
892218893Sdim  CodeGenFunction::OpaqueValueMapping binding(CGF, E);
893218893Sdim
894218893Sdim  CodeGenFunction::ConditionalEvaluation eval(CGF);
895201361Srdivacky  CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
896198092Srdivacky
897218893Sdim  // Save whether the destination's lifetime is externally managed.
898226633Sdim  bool isExternallyDestructed = Dest.isExternallyDestructed();
899218893Sdim
900218893Sdim  eval.begin(CGF);
901193326Sed  CGF.EmitBlock(LHSBlock);
902218893Sdim  Visit(E->getTrueExpr());
903218893Sdim  eval.end(CGF);
904198092Srdivacky
905218893Sdim  assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!");
906218893Sdim  CGF.Builder.CreateBr(ContBlock);
907193326Sed
908218893Sdim  // If the result of an agg expression is unused, then the emission
909218893Sdim  // of the LHS might need to create a destination slot.  That's fine
910218893Sdim  // with us, and we can safely emit the RHS into the same slot, but
911226633Sdim  // we shouldn't claim that it's already being destructed.
912226633Sdim  Dest.setExternallyDestructed(isExternallyDestructed);
913198092Srdivacky
914218893Sdim  eval.begin(CGF);
915193326Sed  CGF.EmitBlock(RHSBlock);
916218893Sdim  Visit(E->getFalseExpr());
917218893Sdim  eval.end(CGF);
918198092Srdivacky
919193326Sed  CGF.EmitBlock(ContBlock);
920193326Sed}
921193326Sed
922198092Srdivackyvoid AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
923261991Sdim  Visit(CE->getChosenSubExpr());
924198092Srdivacky}
925198092Srdivacky
926193326Sedvoid AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
927193326Sed  llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
928193326Sed  llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
929193326Sed
930193326Sed  if (!ArgPtr) {
931193326Sed    CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
932193326Sed    return;
933193326Sed  }
934193326Sed
935239462Sdim  EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType()));
936193326Sed}
937193326Sed
938193326Sedvoid AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
939218893Sdim  // Ensure that we have a slot, but if we already do, remember
940226633Sdim  // whether it was externally destructed.
941226633Sdim  bool wasExternallyDestructed = Dest.isExternallyDestructed();
942239462Sdim  EnsureDest(E->getType());
943198092Srdivacky
944226633Sdim  // We're going to push a destructor if there isn't already one.
945226633Sdim  Dest.setExternallyDestructed();
946226633Sdim
947218893Sdim  Visit(E->getSubExpr());
948193326Sed
949226633Sdim  // Push that destructor we promised.
950226633Sdim  if (!wasExternallyDestructed)
951234353Sdim    CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddr());
952193326Sed}
953193326Sed
954193326Sedvoid
955193326SedAggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
956218893Sdim  AggValueSlot Slot = EnsureSlot(E->getType());
957218893Sdim  CGF.EmitCXXConstructExpr(E, Slot);
958193326Sed}
959193326Sed
960234353Sdimvoid
961234353SdimAggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {
962234353Sdim  AggValueSlot Slot = EnsureSlot(E->getType());
963234353Sdim  CGF.EmitLambdaExpr(E, Slot);
964234353Sdim}
965234353Sdim
966218893Sdimvoid AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {
967234353Sdim  CGF.enterFullExpression(E);
968234353Sdim  CodeGenFunction::RunCleanupsScope cleanups(CGF);
969234353Sdim  Visit(E->getSubExpr());
970193326Sed}
971193326Sed
972210299Sedvoid AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
973218893Sdim  QualType T = E->getType();
974218893Sdim  AggValueSlot Slot = EnsureSlot(T);
975224145Sdim  EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
976198398Srdivacky}
977198398Srdivacky
978201361Srdivackyvoid AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
979218893Sdim  QualType T = E->getType();
980218893Sdim  AggValueSlot Slot = EnsureSlot(T);
981224145Sdim  EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T));
982218893Sdim}
983201361Srdivacky
984218893Sdim/// isSimpleZero - If emitting this value will obviously just cause a store of
985218893Sdim/// zero to memory, return true.  This can return false if uncertain, so it just
986218893Sdim/// handles simple cases.
987218893Sdimstatic bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) {
988221345Sdim  E = E->IgnoreParens();
989221345Sdim
990218893Sdim  // 0
991218893Sdim  if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E))
992218893Sdim    return IL->getValue() == 0;
993218893Sdim  // +0.0
994218893Sdim  if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E))
995218893Sdim    return FL->getValue().isPosZero();
996218893Sdim  // int()
997218893Sdim  if ((isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) &&
998218893Sdim      CGF.getTypes().isZeroInitializable(E->getType()))
999218893Sdim    return true;
1000218893Sdim  // (int*)0 - Null pointer expressions.
1001218893Sdim  if (const CastExpr *ICE = dyn_cast<CastExpr>(E))
1002218893Sdim    return ICE->getCastKind() == CK_NullToPointer;
1003218893Sdim  // '\0'
1004218893Sdim  if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E))
1005218893Sdim    return CL->getValue() == 0;
1006218893Sdim
1007218893Sdim  // Otherwise, hard case: conservatively return false.
1008218893Sdim  return false;
1009201361Srdivacky}
1010201361Srdivacky
1011218893Sdim
1012203955Srdivackyvoid
1013261991SdimAggExprEmitter::EmitInitializationToLValue(Expr *E, LValue LV) {
1014224145Sdim  QualType type = LV.getType();
1015193326Sed  // FIXME: Ignore result?
1016193326Sed  // FIXME: Are initializers affected by volatile?
1017218893Sdim  if (Dest.isZeroed() && isSimpleZero(E, CGF)) {
1018218893Sdim    // Storing "i32 0" to a zero'd memory location is a noop.
1019249423Sdim    return;
1020249423Sdim  } else if (isa<ImplicitValueInitExpr>(E) || isa<CXXScalarValueInitExpr>(E)) {
1021249423Sdim    return EmitNullInitializationToLValue(LV);
1022224145Sdim  } else if (type->isReferenceType()) {
1023261991Sdim    RValue RV = CGF.EmitReferenceBindingToExpr(E);
1024249423Sdim    return CGF.EmitStoreThroughLValue(RV, LV);
1025249423Sdim  }
1026249423Sdim
1027249423Sdim  switch (CGF.getEvaluationKind(type)) {
1028249423Sdim  case TEK_Complex:
1029249423Sdim    CGF.EmitComplexExprIntoLValue(E, LV, /*isInit*/ true);
1030249423Sdim    return;
1031249423Sdim  case TEK_Aggregate:
1032226633Sdim    CGF.EmitAggExpr(E, AggValueSlot::forLValue(LV,
1033226633Sdim                                               AggValueSlot::IsDestructed,
1034226633Sdim                                      AggValueSlot::DoesNotNeedGCBarriers,
1035226633Sdim                                               AggValueSlot::IsNotAliased,
1036224145Sdim                                               Dest.isZeroed()));
1037249423Sdim    return;
1038249423Sdim  case TEK_Scalar:
1039249423Sdim    if (LV.isSimple()) {
1040249423Sdim      CGF.EmitScalarInit(E, /*D=*/0, LV, /*Captured=*/false);
1041249423Sdim    } else {
1042249423Sdim      CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV);
1043249423Sdim    }
1044249423Sdim    return;
1045193326Sed  }
1046249423Sdim  llvm_unreachable("bad evaluation kind");
1047193326Sed}
1048193326Sed
1049224145Sdimvoid AggExprEmitter::EmitNullInitializationToLValue(LValue lv) {
1050224145Sdim  QualType type = lv.getType();
1051224145Sdim
1052218893Sdim  // If the destination slot is already zeroed out before the aggregate is
1053218893Sdim  // copied into it, we don't have to emit any zeros here.
1054224145Sdim  if (Dest.isZeroed() && CGF.getTypes().isZeroInitializable(type))
1055218893Sdim    return;
1056218893Sdim
1057249423Sdim  if (CGF.hasScalarEvaluationKind(type)) {
1058249423Sdim    // For non-aggregates, we can store the appropriate null constant.
1059249423Sdim    llvm::Value *null = CGF.CGM.EmitNullConstant(type);
1060234353Sdim    // Note that the following is not equivalent to
1061234353Sdim    // EmitStoreThroughBitfieldLValue for ARC types.
1062234353Sdim    if (lv.isBitField()) {
1063234353Sdim      CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv);
1064234353Sdim    } else {
1065234353Sdim      assert(lv.isSimple());
1066234353Sdim      CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true);
1067234353Sdim    }
1068193326Sed  } else {
1069193326Sed    // There's a potential optimization opportunity in combining
1070193326Sed    // memsets; that would be easy for arrays, but relatively
1071193326Sed    // difficult for structures with the current code.
1072224145Sdim    CGF.EmitNullInitialization(lv.getAddress(), lv.getType());
1073193326Sed  }
1074193326Sed}
1075193326Sed
1076193326Sedvoid AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
1077193326Sed#if 0
1078200583Srdivacky  // FIXME: Assess perf here?  Figure out what cases are worth optimizing here
1079200583Srdivacky  // (Length of globals? Chunks of zeroed-out space?).
1080193326Sed  //
1081193326Sed  // If we can, prefer a copy from a global; this is a lot less code for long
1082193326Sed  // globals, and it's easier for the current optimizers to analyze.
1083200583Srdivacky  if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
1084193326Sed    llvm::GlobalVariable* GV =
1085200583Srdivacky    new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
1086200583Srdivacky                             llvm::GlobalValue::InternalLinkage, C, "");
1087239462Sdim    EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType()));
1088193326Sed    return;
1089193326Sed  }
1090193326Sed#endif
1091218893Sdim  if (E->hadArrayRangeDesignator())
1092193326Sed    CGF.ErrorUnsupported(E, "GNU array range designator extension");
1093193326Sed
1094261991Sdim  AggValueSlot Dest = EnsureSlot(E->getType());
1095218893Sdim
1096234982Sdim  LValue DestLV = CGF.MakeAddrLValue(Dest.getAddr(), E->getType(),
1097234982Sdim                                     Dest.getAlignment());
1098234353Sdim
1099193326Sed  // Handle initialization of an array.
1100193326Sed  if (E->getType()->isArrayType()) {
1101234982Sdim    if (E->isStringLiteralInit())
1102234982Sdim      return Visit(E->getInit(0));
1103193326Sed
1104234353Sdim    QualType elementType =
1105234353Sdim        CGF.getContext().getAsArrayType(E->getType())->getElementType();
1106193326Sed
1107234353Sdim    llvm::PointerType *APType =
1108234982Sdim      cast<llvm::PointerType>(Dest.getAddr()->getType());
1109234353Sdim    llvm::ArrayType *AType =
1110234353Sdim      cast<llvm::ArrayType>(APType->getElementType());
1111224145Sdim
1112234982Sdim    EmitArrayInit(Dest.getAddr(), AType, elementType, E);
1113193326Sed    return;
1114193326Sed  }
1115198092Srdivacky
1116193326Sed  assert(E->getType()->isRecordType() && "Only support structs/unions here!");
1117198092Srdivacky
1118193326Sed  // Do struct initialization; this code just sets each individual member
1119193326Sed  // to the approprate value.  This makes bitfield support automatic;
1120193326Sed  // the disadvantage is that the generated code is more difficult for
1121193326Sed  // the optimizer, especially with bitfields.
1122193326Sed  unsigned NumInitElements = E->getNumInits();
1123224145Sdim  RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl();
1124251662Sdim
1125251662Sdim  // Prepare a 'this' for CXXDefaultInitExprs.
1126251662Sdim  CodeGenFunction::FieldConstructionScope FCS(CGF, Dest.getAddr());
1127251662Sdim
1128224145Sdim  if (record->isUnion()) {
1129193326Sed    // Only initialize one field of a union. The field itself is
1130193326Sed    // specified by the initializer list.
1131193326Sed    if (!E->getInitializedFieldInUnion()) {
1132193326Sed      // Empty union; we have nothing to do.
1133198092Srdivacky
1134193326Sed#ifndef NDEBUG
1135193326Sed      // Make sure that it's really an empty and not a failure of
1136193326Sed      // semantic analysis.
1137224145Sdim      for (RecordDecl::field_iterator Field = record->field_begin(),
1138224145Sdim                                   FieldEnd = record->field_end();
1139193326Sed           Field != FieldEnd; ++Field)
1140193326Sed        assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
1141193326Sed#endif
1142193326Sed      return;
1143193326Sed    }
1144193326Sed
1145193326Sed    // FIXME: volatility
1146193326Sed    FieldDecl *Field = E->getInitializedFieldInUnion();
1147218893Sdim
1148234982Sdim    LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field);
1149193326Sed    if (NumInitElements) {
1150193326Sed      // Store the initializer into the field
1151224145Sdim      EmitInitializationToLValue(E->getInit(0), FieldLoc);
1152193326Sed    } else {
1153218893Sdim      // Default-initialize to null.
1154224145Sdim      EmitNullInitializationToLValue(FieldLoc);
1155193326Sed    }
1156193326Sed
1157193326Sed    return;
1158193326Sed  }
1159198092Srdivacky
1160224145Sdim  // We'll need to enter cleanup scopes in case any of the member
1161224145Sdim  // initializers throw an exception.
1162226633Sdim  SmallVector<EHScopeStack::stable_iterator, 16> cleanups;
1163234353Sdim  llvm::Instruction *cleanupDominator = 0;
1164224145Sdim
1165193326Sed  // Here we iterate over the fields; this makes it simpler to both
1166193326Sed  // default-initialize fields and skip over unnamed fields.
1167224145Sdim  unsigned curInitIndex = 0;
1168224145Sdim  for (RecordDecl::field_iterator field = record->field_begin(),
1169224145Sdim                               fieldEnd = record->field_end();
1170224145Sdim       field != fieldEnd; ++field) {
1171224145Sdim    // We're done once we hit the flexible array member.
1172224145Sdim    if (field->getType()->isIncompleteArrayType())
1173193326Sed      break;
1174193326Sed
1175224145Sdim    // Always skip anonymous bitfields.
1176224145Sdim    if (field->isUnnamedBitfield())
1177193326Sed      continue;
1178193326Sed
1179224145Sdim    // We're done if we reach the end of the explicit initializers, we
1180224145Sdim    // have a zeroed object, and the rest of the fields are
1181224145Sdim    // zero-initializable.
1182224145Sdim    if (curInitIndex == NumInitElements && Dest.isZeroed() &&
1183218893Sdim        CGF.getTypes().isZeroInitializable(E->getType()))
1184218893Sdim      break;
1185218893Sdim
1186234982Sdim
1187234982Sdim    LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, *field);
1188193326Sed    // We never generate write-barries for initialized fields.
1189224145Sdim    LV.setNonGC(true);
1190218893Sdim
1191224145Sdim    if (curInitIndex < NumInitElements) {
1192204962Srdivacky      // Store the initializer into the field.
1193224145Sdim      EmitInitializationToLValue(E->getInit(curInitIndex++), LV);
1194193326Sed    } else {
1195193326Sed      // We're out of initalizers; default-initialize to null
1196224145Sdim      EmitNullInitializationToLValue(LV);
1197193326Sed    }
1198224145Sdim
1199224145Sdim    // Push a destructor if necessary.
1200224145Sdim    // FIXME: if we have an array of structures, all explicitly
1201224145Sdim    // initialized, we can end up pushing a linear number of cleanups.
1202224145Sdim    bool pushedCleanup = false;
1203224145Sdim    if (QualType::DestructionKind dtorKind
1204224145Sdim          = field->getType().isDestructedType()) {
1205224145Sdim      assert(LV.isSimple());
1206224145Sdim      if (CGF.needsEHCleanup(dtorKind)) {
1207234353Sdim        if (!cleanupDominator)
1208234353Sdim          cleanupDominator = CGF.Builder.CreateUnreachable(); // placeholder
1209234353Sdim
1210224145Sdim        CGF.pushDestroy(EHCleanup, LV.getAddress(), field->getType(),
1211224145Sdim                        CGF.getDestroyer(dtorKind), false);
1212224145Sdim        cleanups.push_back(CGF.EHStack.stable_begin());
1213224145Sdim        pushedCleanup = true;
1214224145Sdim      }
1215224145Sdim    }
1216218893Sdim
1217218893Sdim    // If the GEP didn't get used because of a dead zero init or something
1218218893Sdim    // else, clean it up for -O0 builds and general tidiness.
1219224145Sdim    if (!pushedCleanup && LV.isSimple())
1220218893Sdim      if (llvm::GetElementPtrInst *GEP =
1221224145Sdim            dyn_cast<llvm::GetElementPtrInst>(LV.getAddress()))
1222218893Sdim        if (GEP->use_empty())
1223218893Sdim          GEP->eraseFromParent();
1224193326Sed  }
1225224145Sdim
1226224145Sdim  // Deactivate all the partial cleanups in reverse order, which
1227224145Sdim  // generally means popping them.
1228224145Sdim  for (unsigned i = cleanups.size(); i != 0; --i)
1229234353Sdim    CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator);
1230234353Sdim
1231234353Sdim  // Destroy the placeholder if we made one.
1232234353Sdim  if (cleanupDominator)
1233234353Sdim    cleanupDominator->eraseFromParent();
1234193326Sed}
1235193326Sed
1236193326Sed//===----------------------------------------------------------------------===//
1237193326Sed//                        Entry Points into this File
1238193326Sed//===----------------------------------------------------------------------===//
1239193326Sed
1240218893Sdim/// GetNumNonZeroBytesInInit - Get an approximate count of the number of
1241218893Sdim/// non-zero bytes that will be stored when outputting the initializer for the
1242218893Sdim/// specified initializer expression.
1243221345Sdimstatic CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
1244221345Sdim  E = E->IgnoreParens();
1245218893Sdim
1246218893Sdim  // 0 and 0.0 won't require any non-zero stores!
1247221345Sdim  if (isSimpleZero(E, CGF)) return CharUnits::Zero();
1248218893Sdim
1249218893Sdim  // If this is an initlist expr, sum up the size of sizes of the (present)
1250218893Sdim  // elements.  If this is something weird, assume the whole thing is non-zero.
1251218893Sdim  const InitListExpr *ILE = dyn_cast<InitListExpr>(E);
1252218893Sdim  if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType()))
1253221345Sdim    return CGF.getContext().getTypeSizeInChars(E->getType());
1254218893Sdim
1255218893Sdim  // InitListExprs for structs have to be handled carefully.  If there are
1256218893Sdim  // reference members, we need to consider the size of the reference, not the
1257218893Sdim  // referencee.  InitListExprs for unions and arrays can't have references.
1258218893Sdim  if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
1259218893Sdim    if (!RT->isUnionType()) {
1260218893Sdim      RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
1261221345Sdim      CharUnits NumNonZeroBytes = CharUnits::Zero();
1262218893Sdim
1263218893Sdim      unsigned ILEElement = 0;
1264218893Sdim      for (RecordDecl::field_iterator Field = SD->field_begin(),
1265218893Sdim           FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) {
1266218893Sdim        // We're done once we hit the flexible array member or run out of
1267218893Sdim        // InitListExpr elements.
1268218893Sdim        if (Field->getType()->isIncompleteArrayType() ||
1269218893Sdim            ILEElement == ILE->getNumInits())
1270218893Sdim          break;
1271218893Sdim        if (Field->isUnnamedBitfield())
1272218893Sdim          continue;
1273218893Sdim
1274218893Sdim        const Expr *E = ILE->getInit(ILEElement++);
1275218893Sdim
1276218893Sdim        // Reference values are always non-null and have the width of a pointer.
1277218893Sdim        if (Field->getType()->isReferenceType())
1278221345Sdim          NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits(
1279251662Sdim              CGF.getTarget().getPointerWidth(0));
1280218893Sdim        else
1281218893Sdim          NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
1282218893Sdim      }
1283218893Sdim
1284218893Sdim      return NumNonZeroBytes;
1285218893Sdim    }
1286218893Sdim  }
1287218893Sdim
1288218893Sdim
1289221345Sdim  CharUnits NumNonZeroBytes = CharUnits::Zero();
1290218893Sdim  for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
1291218893Sdim    NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);
1292218893Sdim  return NumNonZeroBytes;
1293218893Sdim}
1294218893Sdim
1295218893Sdim/// CheckAggExprForMemSetUse - If the initializer is large and has a lot of
1296218893Sdim/// zeros in it, emit a memset and avoid storing the individual zeros.
1297218893Sdim///
1298218893Sdimstatic void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
1299218893Sdim                                     CodeGenFunction &CGF) {
1300218893Sdim  // If the slot is already known to be zeroed, nothing to do.  Don't mess with
1301218893Sdim  // volatile stores.
1302218893Sdim  if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return;
1303221345Sdim
1304221345Sdim  // C++ objects with a user-declared constructor don't need zero'ing.
1305243830Sdim  if (CGF.getLangOpts().CPlusPlus)
1306221345Sdim    if (const RecordType *RT = CGF.getContext()
1307221345Sdim                       .getBaseElementType(E->getType())->getAs<RecordType>()) {
1308221345Sdim      const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1309221345Sdim      if (RD->hasUserDeclaredConstructor())
1310221345Sdim        return;
1311221345Sdim    }
1312221345Sdim
1313218893Sdim  // If the type is 16-bytes or smaller, prefer individual stores over memset.
1314221345Sdim  std::pair<CharUnits, CharUnits> TypeInfo =
1315221345Sdim    CGF.getContext().getTypeInfoInChars(E->getType());
1316221345Sdim  if (TypeInfo.first <= CharUnits::fromQuantity(16))
1317218893Sdim    return;
1318218893Sdim
1319218893Sdim  // Check to see if over 3/4 of the initializer are known to be zero.  If so,
1320218893Sdim  // we prefer to emit memset + individual stores for the rest.
1321221345Sdim  CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF);
1322221345Sdim  if (NumNonZeroBytes*4 > TypeInfo.first)
1323218893Sdim    return;
1324218893Sdim
1325218893Sdim  // Okay, it seems like a good idea to use an initial memset, emit the call.
1326221345Sdim  llvm::Constant *SizeVal = CGF.Builder.getInt64(TypeInfo.first.getQuantity());
1327221345Sdim  CharUnits Align = TypeInfo.second;
1328218893Sdim
1329218893Sdim  llvm::Value *Loc = Slot.getAddr();
1330218893Sdim
1331234353Sdim  Loc = CGF.Builder.CreateBitCast(Loc, CGF.Int8PtrTy);
1332221345Sdim  CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal,
1333221345Sdim                           Align.getQuantity(), false);
1334218893Sdim
1335218893Sdim  // Tell the AggExprEmitter that the slot is known zero.
1336218893Sdim  Slot.setZeroed();
1337218893Sdim}
1338218893Sdim
1339218893Sdim
1340218893Sdim
1341218893Sdim
1342193326Sed/// EmitAggExpr - Emit the computation of the specified expression of aggregate
1343193326Sed/// type.  The result is computed into DestPtr.  Note that if DestPtr is null,
1344193326Sed/// the value of the aggregate expression is not needed.  If VolatileDest is
1345193326Sed/// true, DestPtr cannot be 0.
1346239462Sdimvoid CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) {
1347249423Sdim  assert(E && hasAggregateEvaluationKind(E->getType()) &&
1348193326Sed         "Invalid aggregate expression to emit");
1349218893Sdim  assert((Slot.getAddr() != 0 || Slot.isIgnored()) &&
1350218893Sdim         "slot has bits but no address");
1351198092Srdivacky
1352218893Sdim  // Optimize the slot if possible.
1353218893Sdim  CheckAggExprForMemSetUse(Slot, E, *this);
1354218893Sdim
1355239462Sdim  AggExprEmitter(*this, Slot).Visit(const_cast<Expr*>(E));
1356193326Sed}
1357193326Sed
1358203955SrdivackyLValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
1359249423Sdim  assert(hasAggregateEvaluationKind(E->getType()) && "Invalid argument!");
1360203955Srdivacky  llvm::Value *Temp = CreateMemTemp(E->getType());
1361212904Sdim  LValue LV = MakeAddrLValue(Temp, E->getType());
1362226633Sdim  EmitAggExpr(E, AggValueSlot::forLValue(LV, AggValueSlot::IsNotDestructed,
1363226633Sdim                                         AggValueSlot::DoesNotNeedGCBarriers,
1364226633Sdim                                         AggValueSlot::IsNotAliased));
1365212904Sdim  return LV;
1366203955Srdivacky}
1367203955Srdivacky
1368193326Sedvoid CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
1369193326Sed                                        llvm::Value *SrcPtr, QualType Ty,
1370239462Sdim                                        bool isVolatile,
1371243830Sdim                                        CharUnits alignment,
1372243830Sdim                                        bool isAssignment) {
1373193326Sed  assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
1374198092Srdivacky
1375243830Sdim  if (getLangOpts().CPlusPlus) {
1376207619Srdivacky    if (const RecordType *RT = Ty->getAs<RecordType>()) {
1377208600Srdivacky      CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
1378208600Srdivacky      assert((Record->hasTrivialCopyConstructor() ||
1379226633Sdim              Record->hasTrivialCopyAssignment() ||
1380226633Sdim              Record->hasTrivialMoveConstructor() ||
1381226633Sdim              Record->hasTrivialMoveAssignment()) &&
1382249423Sdim             "Trying to aggregate-copy a type without a trivial copy/move "
1383208600Srdivacky             "constructor or assignment operator");
1384208600Srdivacky      // Ignore empty classes in C++.
1385208600Srdivacky      if (Record->isEmpty())
1386207619Srdivacky        return;
1387207619Srdivacky    }
1388207619Srdivacky  }
1389207619Srdivacky
1390193326Sed  // Aggregate assignment turns into llvm.memcpy.  This is almost valid per
1391193326Sed  // C99 6.5.16.1p3, which states "If the value being stored in an object is
1392193326Sed  // read from another object that overlaps in anyway the storage of the first
1393193326Sed  // object, then the overlap shall be exact and the two objects shall have
1394193326Sed  // qualified or unqualified versions of a compatible type."
1395193326Sed  //
1396193326Sed  // memcpy is not defined if the source and destination pointers are exactly
1397193326Sed  // equal, but other compilers do this optimization, and almost every memcpy
1398193326Sed  // implementation handles this case safely.  If there is a libc that does not
1399193326Sed  // safely handle this, we can add a target hook.
1400198092Srdivacky
1401243830Sdim  // Get data size and alignment info for this aggregate. If this is an
1402243830Sdim  // assignment don't copy the tail padding. Otherwise copying it is fine.
1403243830Sdim  std::pair<CharUnits, CharUnits> TypeInfo;
1404243830Sdim  if (isAssignment)
1405243830Sdim    TypeInfo = getContext().getTypeInfoDataSizeInChars(Ty);
1406243830Sdim  else
1407243830Sdim    TypeInfo = getContext().getTypeInfoInChars(Ty);
1408198092Srdivacky
1409239462Sdim  if (alignment.isZero())
1410239462Sdim    alignment = TypeInfo.second;
1411234353Sdim
1412193326Sed  // FIXME: Handle variable sized types.
1413198092Srdivacky
1414193326Sed  // FIXME: If we have a volatile struct, the optimizer can remove what might
1415193326Sed  // appear to be `extra' memory ops:
1416193326Sed  //
1417193326Sed  // volatile struct { int i; } a, b;
1418193326Sed  //
1419193326Sed  // int main() {
1420193326Sed  //   a = b;
1421193326Sed  //   a = b;
1422193326Sed  // }
1423193326Sed  //
1424206275Srdivacky  // we need to use a different call here.  We use isVolatile to indicate when
1425193326Sed  // either the source or the destination is volatile.
1426206275Srdivacky
1427226633Sdim  llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
1428226633Sdim  llvm::Type *DBP =
1429218893Sdim    llvm::Type::getInt8PtrTy(getLLVMContext(), DPT->getAddressSpace());
1430226633Sdim  DestPtr = Builder.CreateBitCast(DestPtr, DBP);
1431206275Srdivacky
1432226633Sdim  llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
1433226633Sdim  llvm::Type *SBP =
1434218893Sdim    llvm::Type::getInt8PtrTy(getLLVMContext(), SPT->getAddressSpace());
1435226633Sdim  SrcPtr = Builder.CreateBitCast(SrcPtr, SBP);
1436206275Srdivacky
1437224145Sdim  // Don't do any of the memmove_collectable tests if GC isn't set.
1438234353Sdim  if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
1439224145Sdim    // fall through
1440224145Sdim  } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1441210299Sed    RecordDecl *Record = RecordTy->getDecl();
1442210299Sed    if (Record->hasObjectMember()) {
1443221345Sdim      CharUnits size = TypeInfo.first;
1444226633Sdim      llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
1445221345Sdim      llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size.getQuantity());
1446210299Sed      CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
1447210299Sed                                                    SizeVal);
1448210299Sed      return;
1449210299Sed    }
1450224145Sdim  } else if (Ty->isArrayType()) {
1451210299Sed    QualType BaseType = getContext().getBaseElementType(Ty);
1452210299Sed    if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
1453210299Sed      if (RecordTy->getDecl()->hasObjectMember()) {
1454221345Sdim        CharUnits size = TypeInfo.first;
1455226633Sdim        llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
1456221345Sdim        llvm::Value *SizeVal =
1457221345Sdim          llvm::ConstantInt::get(SizeTy, size.getQuantity());
1458210299Sed        CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
1459210299Sed                                                      SizeVal);
1460210299Sed        return;
1461210299Sed      }
1462210299Sed    }
1463210299Sed  }
1464243830Sdim
1465243830Sdim  // Determine the metadata to describe the position of any padding in this
1466243830Sdim  // memcpy, as well as the TBAA tags for the members of the struct, in case
1467243830Sdim  // the optimizer wishes to expand it in to scalar memory operations.
1468243830Sdim  llvm::MDNode *TBAAStructTag = CGM.getTBAAStructInfo(Ty);
1469210299Sed
1470218893Sdim  Builder.CreateMemCpy(DestPtr, SrcPtr,
1471221345Sdim                       llvm::ConstantInt::get(IntPtrTy,
1472221345Sdim                                              TypeInfo.first.getQuantity()),
1473243830Sdim                       alignment.getQuantity(), isVolatile,
1474243830Sdim                       /*TBAATag=*/0, TBAAStructTag);
1475193326Sed}
1476