Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGExprAgg.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This contains code to emit Aggregate Expr nodes as LLVM code.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CGCXXABI.h"
14
#include "CGObjCRuntime.h"
15
#include "CodeGenFunction.h"
16
#include "CodeGenModule.h"
17
#include "ConstantEmitter.h"
18
#include "TargetInfo.h"
19
#include "clang/AST/ASTContext.h"
20
#include "clang/AST/Attr.h"
21
#include "clang/AST/DeclCXX.h"
22
#include "clang/AST/DeclTemplate.h"
23
#include "clang/AST/StmtVisitor.h"
24
#include "llvm/IR/Constants.h"
25
#include "llvm/IR/Function.h"
26
#include "llvm/IR/GlobalVariable.h"
27
#include "llvm/IR/IntrinsicInst.h"
28
#include "llvm/IR/Intrinsics.h"
29
using namespace clang;
30
using namespace CodeGen;
31
32
//===----------------------------------------------------------------------===//
33
//                        Aggregate Expression Emitter
34
//===----------------------------------------------------------------------===//
35
36
namespace  {
37
class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
38
  CodeGenFunction &CGF;
39
  CGBuilderTy &Builder;
40
  AggValueSlot Dest;
41
  bool IsResultUnused;
42
43
65.9k
  AggValueSlot EnsureSlot(QualType T) {
44
65.9k
    if (!Dest.isIgnored()) 
return Dest65.8k
;
45
69
    return CGF.CreateAggTemp(T, "agg.tmp.ensured");
46
69
  }
47
6.18k
  void EnsureDest(QualType T) {
48
6.18k
    if (!Dest.isIgnored()) 
return6.01k
;
49
161
    Dest = CGF.CreateAggTemp(T, "agg.tmp.ensured");
50
161
  }
51
52
  // Calls `Fn` with a valid return value slot, potentially creating a temporary
53
  // to do so. If a temporary is created, an appropriate copy into `Dest` will
54
  // be emitted, as will lifetime markers.
55
  //
56
  // The given function should take a ReturnValueSlot, and return an RValue that
57
  // points to said slot.
58
  void withReturnValueSlot(const Expr *E,
59
                           llvm::function_ref<RValue(ReturnValueSlot)> Fn);
60
61
public:
62
  AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest, bool IsResultUnused)
63
    : CGF(cgf), Builder(CGF.Builder), Dest(Dest),
64
76.7k
    IsResultUnused(IsResultUnused) { }
65
66
  //===--------------------------------------------------------------------===//
67
  //                               Utilities
68
  //===--------------------------------------------------------------------===//
69
70
  /// EmitAggLoadOfLValue - Given an expression with aggregate type that
71
  /// represents a value lvalue, this method emits the address of the lvalue,
72
  /// then loads the result into DestPtr.
73
  void EmitAggLoadOfLValue(const Expr *E);
74
75
  enum ExprValueKind {
76
    EVK_RValue,
77
    EVK_NonRValue
78
  };
79
80
  /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
81
  /// SrcIsRValue is true if source comes from an RValue.
82
  void EmitFinalDestCopy(QualType type, const LValue &src,
83
                         ExprValueKind SrcValueKind = EVK_NonRValue);
84
  void EmitFinalDestCopy(QualType type, RValue src);
85
  void EmitCopy(QualType type, const AggValueSlot &dest,
86
                const AggValueSlot &src);
87
88
  void EmitMoveFromReturnSlot(const Expr *E, RValue Src);
89
90
  void EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
91
                     QualType ArrayQTy, InitListExpr *E);
92
93
3.51k
  AggValueSlot::NeedsGCBarriers_t needsGC(QualType T) {
94
3.51k
    if (CGF.getLangOpts().getGC() && 
TypeRequiresGCollection(T)24
)
95
20
      return AggValueSlot::NeedsGCBarriers;
96
3.49k
    return AggValueSlot::DoesNotNeedGCBarriers;
97
3.49k
  }
98
99
  bool TypeRequiresGCollection(QualType T);
100
101
  //===--------------------------------------------------------------------===//
102
  //                            Visitor Methods
103
  //===--------------------------------------------------------------------===//
104
105
116k
  void Visit(Expr *E) {
106
116k
    ApplyDebugLocation DL(CGF, E);
107
116k
    StmtVisitor<AggExprEmitter>::Visit(E);
108
116k
  }
109
110
0
  void VisitStmt(Stmt *S) {
111
0
    CGF.ErrorUnsupported(S, "aggregate expression");
112
0
  }
113
90
  void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
114
0
  void VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
115
0
    Visit(GE->getResultExpr());
116
0
  }
117
5
  void VisitCoawaitExpr(CoawaitExpr *E) {
118
5
    CGF.EmitCoawaitExpr(*E, Dest, IsResultUnused);
119
5
  }
120
0
  void VisitCoyieldExpr(CoyieldExpr *E) {
121
0
    CGF.EmitCoyieldExpr(*E, Dest, IsResultUnused);
122
0
  }
123
0
  void VisitUnaryCoawait(UnaryOperator *E) { Visit(E->getSubExpr()); }
124
639
  void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
125
0
  void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
126
0
    return Visit(E->getReplacement());
127
0
  }
128
129
9
  void VisitConstantExpr(ConstantExpr *E) {
130
9
    if (llvm::Value *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E)) {
131
8
      CGF.EmitAggregateStore(Result, Dest.getAddress(),
132
8
                             E->getType().isVolatileQualified());
133
8
      return;
134
8
    }
135
1
    return Visit(E->getSubExpr());
136
1
  }
137
138
  // l-values.
139
2.40k
  void VisitDeclRefExpr(DeclRefExpr *E) { EmitAggLoadOfLValue(E); }
140
7
  void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
141
97
  void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
142
79
  void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
143
  void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
144
6
  void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
145
6
    EmitAggLoadOfLValue(E);
146
6
  }
147
0
  void VisitPredefinedExpr(const PredefinedExpr *E) {
148
0
    EmitAggLoadOfLValue(E);
149
0
  }
150
151
  // Operators.
152
  void VisitCastExpr(CastExpr *E);
153
  void VisitCallExpr(const CallExpr *E);
154
  void VisitStmtExpr(const StmtExpr *E);
155
  void VisitBinaryOperator(const BinaryOperator *BO);
156
  void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
157
  void VisitBinAssign(const BinaryOperator *E);
158
  void VisitBinComma(const BinaryOperator *E);
159
  void VisitBinCmp(const BinaryOperator *E);
160
0
  void VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) {
161
0
    Visit(E->getSemanticForm());
162
0
  }
163
164
  void VisitObjCMessageExpr(ObjCMessageExpr *E);
165
5
  void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
166
5
    EmitAggLoadOfLValue(E);
167
5
  }
168
169
  void VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E);
170
  void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
171
  void VisitChooseExpr(const ChooseExpr *CE);
172
  void VisitInitListExpr(InitListExpr *E);
173
  void VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E,
174
                              llvm::Value *outerBegin = nullptr);
175
  void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
176
0
  void VisitNoInitExpr(NoInitExpr *E) { } // Do nothing.
177
391
  void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
178
391
    CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE);
179
391
    Visit(DAE->getExpr());
180
391
  }
181
41
  void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
182
41
    CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE);
183
41
    Visit(DIE->getExpr());
184
41
  }
185
  void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
186
  void VisitCXXConstructExpr(const CXXConstructExpr *E);
187
  void VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);
188
  void VisitLambdaExpr(LambdaExpr *E);
189
  void VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
190
  void VisitExprWithCleanups(ExprWithCleanups *E);
191
  void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
192
0
  void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
193
  void VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
194
  void VisitOpaqueValueExpr(OpaqueValueExpr *E);
195
196
28
  void VisitPseudoObjectExpr(PseudoObjectExpr *E) {
197
28
    if (E->isGLValue()) {
198
0
      LValue LV = CGF.EmitPseudoObjectLValue(E);
199
0
      return EmitFinalDestCopy(E->getType(), LV);
200
0
    }
201
202
28
    CGF.EmitPseudoObjectRValue(E, EnsureSlot(E->getType()));
203
28
  }
204
205
  void VisitVAArgExpr(VAArgExpr *E);
206
207
  void EmitInitializationToLValue(Expr *E, LValue Address);
208
  void EmitNullInitializationToLValue(LValue Address);
209
  //  case Expr::ChooseExprClass:
210
1
  void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
211
15
  void VisitAtomicExpr(AtomicExpr *E) {
212
15
    RValue Res = CGF.EmitAtomicExpr(E);
213
15
    EmitFinalDestCopy(E->getType(), Res);
214
15
  }
215
};
216
}  // end anonymous namespace.
217
218
//===----------------------------------------------------------------------===//
219
//                                Utilities
220
//===----------------------------------------------------------------------===//
221
222
/// EmitAggLoadOfLValue - Given an expression with aggregate type that
223
/// represents a value lvalue, this method emits the address of the lvalue,
224
/// then loads the result into DestPtr.
225
2.61k
void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
226
2.61k
  LValue LV = CGF.EmitLValue(E);
227
228
  // If the type of the l-value is atomic, then do an atomic load.
229
2.61k
  if (LV.getType()->isAtomicType() || 
CGF.LValueIsSuitableForInlineAtomic(LV)2.61k
) {
230
11
    CGF.EmitAtomicLoad(LV, E->getExprLoc(), Dest);
231
11
    return;
232
11
  }
233
234
2.60k
  EmitFinalDestCopy(E->getType(), LV);
235
2.60k
}
236
237
/// True if the given aggregate type requires special GC API calls.
238
24
bool AggExprEmitter::TypeRequiresGCollection(QualType T) {
239
  // Only record types have members that might require garbage collection.
240
24
  const RecordType *RecordTy = T->getAs<RecordType>();
241
24
  if (!RecordTy) 
return false0
;
242
243
  // Don't mess with non-trivial C++ types.
244
24
  RecordDecl *Record = RecordTy->getDecl();
245
24
  if (isa<CXXRecordDecl>(Record) &&
246
5
      (cast<CXXRecordDecl>(Record)->hasNonTrivialCopyConstructor() ||
247
5
       !cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
248
0
    return false;
249
250
  // Check whether the type has an object member.
251
24
  return Record->hasObjectMember();
252
24
}
253
254
void AggExprEmitter::withReturnValueSlot(
255
6.80k
    const Expr *E, llvm::function_ref<RValue(ReturnValueSlot)> EmitCall) {
256
6.80k
  QualType RetTy = E->getType();
257
6.80k
  bool RequiresDestruction =
258
6.80k
      !Dest.isExternallyDestructed() &&
259
2.12k
      RetTy.isDestructedType() == QualType::DK_nontrivial_c_struct;
260
261
  // If it makes no observable difference, save a memcpy + temporary.
262
  //
263
  // We need to always provide our own temporary if destruction is required.
264
  // Otherwise, EmitCall will emit its own, notice that it's "unused", and end
265
  // its lifetime before we have the chance to emit a proper destructor call.
266
6.80k
  bool UseTemp = Dest.isPotentiallyAliased() || 
Dest.requiresGCollection()6.61k
||
267
6.61k
                 (RequiresDestruction && 
!Dest.getAddress().isValid()12
);
268
269
6.80k
  Address RetAddr = Address::invalid();
270
6.80k
  Address RetAllocaAddr = Address::invalid();
271
272
6.80k
  EHScopeStack::stable_iterator LifetimeEndBlock;
273
6.80k
  llvm::Value *LifetimeSizePtr = nullptr;
274
6.80k
  llvm::IntrinsicInst *LifetimeStartInst = nullptr;
275
6.80k
  if (!UseTemp) {
276
6.60k
    RetAddr = Dest.getAddress();
277
196
  } else {
278
196
    RetAddr = CGF.CreateMemTemp(RetTy, "tmp", &RetAllocaAddr);
279
196
    uint64_t Size =
280
196
        CGF.CGM.getDataLayout().getTypeAllocSize(CGF.ConvertTypeForMem(RetTy));
281
196
    LifetimeSizePtr = CGF.EmitLifetimeStart(Size, RetAllocaAddr.getPointer());
282
196
    if (LifetimeSizePtr) {
283
12
      LifetimeStartInst =
284
12
          cast<llvm::IntrinsicInst>(std::prev(Builder.GetInsertPoint()));
285
12
      assert(LifetimeStartInst->getIntrinsicID() ==
286
12
                 llvm::Intrinsic::lifetime_start &&
287
12
             "Last insertion wasn't a lifetime.start?");
288
289
12
      CGF.pushFullExprCleanup<CodeGenFunction::CallLifetimeEnd>(
290
12
          NormalEHLifetimeMarker, RetAllocaAddr, LifetimeSizePtr);
291
12
      LifetimeEndBlock = CGF.EHStack.stable_begin();
292
12
    }
293
196
  }
294
295
6.80k
  RValue Src =
296
6.80k
      EmitCall(ReturnValueSlot(RetAddr, Dest.isVolatile(), IsResultUnused,
297
6.80k
                               Dest.isExternallyDestructed()));
298
299
6.80k
  if (!UseTemp)
300
6.60k
    return;
301
302
196
  assert(Dest.getPointer() != Src.getAggregatePointer());
303
196
  EmitFinalDestCopy(E->getType(), Src);
304
305
196
  if (!RequiresDestruction && 
LifetimeStartInst184
) {
306
    // If there's no dtor to run, the copy was the last use of our temporary.
307
    // Since we're not guaranteed to be in an ExprWithCleanups, clean up
308
    // eagerly.
309
10
    CGF.DeactivateCleanupBlock(LifetimeEndBlock, LifetimeStartInst);
310
10
    CGF.EmitLifetimeEnd(LifetimeSizePtr, RetAllocaAddr.getPointer());
311
10
  }
312
196
}
313
314
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
315
215
void AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src) {
316
215
  assert(src.isAggregate() && "value must be aggregate value!");
317
215
  LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddress(), type);
318
215
  EmitFinalDestCopy(type, srcLV, EVK_RValue);
319
215
}
320
321
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
322
void AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src,
323
3.62k
                                       ExprValueKind SrcValueKind) {
324
  // If Dest is ignored, then we're evaluating an aggregate expression
325
  // in a context that doesn't care about the result.  Note that loads
326
  // from volatile l-values force the existence of a non-ignored
327
  // destination.
328
3.62k
  if (Dest.isIgnored())
329
485
    return;
330
331
  // Copy non-trivial C structs here.
332
3.14k
  LValue DstLV = CGF.MakeAddrLValue(
333
3.12k
      Dest.getAddress(), Dest.isVolatile() ? 
type.withVolatile()23
: type);
334
335
3.14k
  if (SrcValueKind == EVK_RValue) {
336
203
    if (type.isNonTrivialToPrimitiveDestructiveMove() == QualType::PCK_Struct) {
337
8
      if (Dest.isPotentiallyAliased())
338
8
        CGF.callCStructMoveAssignmentOperator(DstLV, src);
339
0
      else
340
0
        CGF.callCStructMoveConstructor(DstLV, src);
341
8
      return;
342
8
    }
343
2.94k
  } else {
344
2.94k
    if (type.isNonTrivialToPrimitiveCopy() == QualType::PCK_Struct) {
345
77
      if (Dest.isPotentiallyAliased())
346
20
        CGF.callCStructCopyAssignmentOperator(DstLV, src);
347
57
      else
348
57
        CGF.callCStructCopyConstructor(DstLV, src);
349
77
      return;
350
77
    }
351
3.05k
  }
352
353
3.05k
  AggValueSlot srcAgg = AggValueSlot::forLValue(
354
3.05k
      src, CGF, AggValueSlot::IsDestructed, needsGC(type),
355
3.05k
      AggValueSlot::IsAliased, AggValueSlot::MayOverlap);
356
3.05k
  EmitCopy(type, Dest, srcAgg);
357
3.05k
}
358
359
/// Perform a copy from the source into the destination.
360
///
361
/// \param type - the type of the aggregate being copied; qualifiers are
362
///   ignored
363
void AggExprEmitter::EmitCopy(QualType type, const AggValueSlot &dest,
364
3.06k
                              const AggValueSlot &src) {
365
3.06k
  if (dest.requiresGCollection()) {
366
6
    CharUnits sz = dest.getPreferredSize(CGF.getContext(), type);
367
6
    llvm::Value *size = llvm::ConstantInt::get(CGF.SizeTy, sz.getQuantity());
368
6
    CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
369
6
                                                      dest.getAddress(),
370
6
                                                      src.getAddress(),
371
6
                                                      size);
372
6
    return;
373
6
  }
374
375
  // If the result of the assignment is used, copy the LHS there also.
376
  // It's volatile if either side is.  Use the minimum alignment of
377
  // the two sides.
378
3.05k
  LValue DestLV = CGF.MakeAddrLValue(dest.getAddress(), type);
379
3.05k
  LValue SrcLV = CGF.MakeAddrLValue(src.getAddress(), type);
380
3.05k
  CGF.EmitAggregateCopy(DestLV, SrcLV, type, dest.mayOverlap(),
381
3.05k
                        dest.isVolatile() || 
src.isVolatile()3.03k
);
382
3.05k
}
383
384
/// Emit the initializer for a std::initializer_list initialized with a
385
/// real initializer list.
386
void
387
177
AggExprEmitter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
388
  // Emit an array containing the elements.  The array is externally destructed
389
  // if the std::initializer_list object is.
390
177
  ASTContext &Ctx = CGF.getContext();
391
177
  LValue Array = CGF.EmitLValue(E->getSubExpr());
392
177
  assert(Array.isSimple() && "initializer_list array not a simple lvalue");
393
177
  Address ArrayPtr = Array.getAddress(CGF);
394
395
177
  const ConstantArrayType *ArrayType =
396
177
      Ctx.getAsConstantArrayType(E->getSubExpr()->getType());
397
177
  assert(ArrayType && "std::initializer_list constructed from non-array");
398
399
  // FIXME: Perform the checks on the field types in SemaInit.
400
177
  RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl();
401
177
  RecordDecl::field_iterator Field = Record->field_begin();
402
177
  if (Field == Record->field_end()) {
403
1
    CGF.ErrorUnsupported(E, "weird std::initializer_list");
404
1
    return;
405
1
  }
406
407
  // Start pointer.
408
176
  if (!Field->getType()->isPointerType() ||
409
176
      !Ctx.hasSameType(Field->getType()->getPointeeType(),
410
0
                       ArrayType->getElementType())) {
411
0
    CGF.ErrorUnsupported(E, "weird std::initializer_list");
412
0
    return;
413
0
  }
414
415
176
  AggValueSlot Dest = EnsureSlot(E->getType());
416
176
  LValue DestLV = CGF.MakeAddrLValue(Dest.getAddress(), E->getType());
417
176
  LValue Start = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
418
176
  llvm::Value *Zero = llvm::ConstantInt::get(CGF.PtrDiffTy, 0);
419
176
  llvm::Value *IdxStart[] = { Zero, Zero };
420
176
  llvm::Value *ArrayStart =
421
176
      Builder.CreateInBoundsGEP(ArrayPtr.getPointer(), IdxStart, "arraystart");
422
176
  CGF.EmitStoreThroughLValue(RValue::get(ArrayStart), Start);
423
176
  ++Field;
424
425
176
  if (Field == Record->field_end()) {
426
0
    CGF.ErrorUnsupported(E, "weird std::initializer_list");
427
0
    return;
428
0
  }
429
430
176
  llvm::Value *Size = Builder.getInt(ArrayType->getSize());
431
176
  LValue EndOrLength = CGF.EmitLValueForFieldInitialization(DestLV, *Field);
432
176
  if (Field->getType()->isPointerType() &&
433
7
      Ctx.hasSameType(Field->getType()->getPointeeType(),
434
7
                      ArrayType->getElementType())) {
435
    // End pointer.
436
7
    llvm::Value *IdxEnd[] = { Zero, Size };
437
7
    llvm::Value *ArrayEnd =
438
7
        Builder.CreateInBoundsGEP(ArrayPtr.getPointer(), IdxEnd, "arrayend");
439
7
    CGF.EmitStoreThroughLValue(RValue::get(ArrayEnd), EndOrLength);
440
169
  } else if (Ctx.hasSameType(Field->getType(), Ctx.getSizeType())) {
441
    // Length.
442
169
    CGF.EmitStoreThroughLValue(RValue::get(Size), EndOrLength);
443
0
  } else {
444
0
    CGF.ErrorUnsupported(E, "weird std::initializer_list");
445
0
    return;
446
0
  }
447
176
}
448
449
/// Determine if E is a trivial array filler, that is, one that is
450
/// equivalent to zero-initialization.
451
1.63k
static bool isTrivialFiller(Expr *E) {
452
1.63k
  if (!E)
453
1.56k
    return true;
454
455
67
  if (isa<ImplicitValueInitExpr>(E))
456
33
    return true;
457
458
34
  if (auto *ILE = dyn_cast<InitListExpr>(E)) {
459
6
    if (ILE->getNumInits())
460
5
      return false;
461
1
    return isTrivialFiller(ILE->getArrayFiller());
462
1
  }
463
464
28
  if (auto *Cons = dyn_cast_or_null<CXXConstructExpr>(E))
465
25
    return Cons->getConstructor()->isDefaultConstructor() &&
466
23
           Cons->getConstructor()->isTrivial();
467
468
  // FIXME: Are there other cases where we can avoid emitting an initializer?
469
3
  return false;
470
3
}
471
472
/// Emit initialization of an array from an initializer list.
473
void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
474
1.66k
                                   QualType ArrayQTy, InitListExpr *E) {
475
1.66k
  uint64_t NumInitElements = E->getNumInits();
476
477
1.66k
  uint64_t NumArrayElements = AType->getNumElements();
478
1.66k
  assert(NumInitElements <= NumArrayElements);
479
480
1.66k
  QualType elementType =
481
1.66k
      CGF.getContext().getAsArrayType(ArrayQTy)->getElementType();
482
483
  // DestPtr is an array*.  Construct an elementType* by drilling
484
  // down a level.
485
1.66k
  llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
486
1.66k
  llvm::Value *indices[] = { zero, zero };
487
1.66k
  llvm::Value *begin =
488
1.66k
    Builder.CreateInBoundsGEP(DestPtr.getPointer(), indices, "arrayinit.begin");
489
490
1.66k
  CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType);
491
1.66k
  CharUnits elementAlign =
492
1.66k
    DestPtr.getAlignment().alignmentOfArrayElement(elementSize);
493
494
  // Consider initializing the array by copying from a global. For this to be
495
  // more efficient than per-element initialization, the size of the elements
496
  // with explicit initializers should be large enough.
497
1.66k
  if (NumInitElements * elementSize.getQuantity() > 16 &&
498
131
      elementType.isTriviallyCopyableType(CGF.getContext())) {
499
88
    CodeGen::CodeGenModule &CGM = CGF.CGM;
500
88
    ConstantEmitter Emitter(CGF);
501
88
    LangAS AS = ArrayQTy.getAddressSpace();
502
88
    if (llvm::Constant *C = Emitter.tryEmitForInitializer(E, AS, ArrayQTy)) {
503
32
      auto GV = new llvm::GlobalVariable(
504
32
          CGM.getModule(), C->getType(),
505
32
          CGM.isTypeConstant(ArrayQTy, /* ExcludeCtorDtor= */ true),
506
32
          llvm::GlobalValue::PrivateLinkage, C, "constinit",
507
32
          /* InsertBefore= */ nullptr, llvm::GlobalVariable::NotThreadLocal,
508
32
          CGM.getContext().getTargetAddressSpace(AS));
509
32
      Emitter.finalize(GV);
510
32
      CharUnits Align = CGM.getContext().getTypeAlignInChars(ArrayQTy);
511
32
      GV->setAlignment(Align.getAsAlign());
512
32
      EmitFinalDestCopy(ArrayQTy, CGF.MakeAddrLValue(GV, ArrayQTy, Align));
513
32
      return;
514
32
    }
515
1.63k
  }
516
517
  // Exception safety requires us to destroy all the
518
  // already-constructed members if an initializer throws.
519
  // For that, we'll need an EH cleanup.
520
1.63k
  QualType::DestructionKind dtorKind = elementType.isDestructedType();
521
1.63k
  Address endOfInit = Address::invalid();
522
1.63k
  EHScopeStack::stable_iterator cleanup;
523
1.63k
  llvm::Instruction *cleanupDominator = nullptr;
524
1.63k
  if (CGF.needsEHCleanup(dtorKind)) {
525
    // In principle we could tell the cleanup where we are more
526
    // directly, but the control flow can get so varied here that it
527
    // would actually be quite complex.  Therefore we go through an
528
    // alloca.
529
70
    endOfInit = CGF.CreateTempAlloca(begin->getType(), CGF.getPointerAlign(),
530
70
                                     "arrayinit.endOfInit");
531
70
    cleanupDominator = Builder.CreateStore(begin, endOfInit);
532
70
    CGF.pushIrregularPartialArrayCleanup(begin, endOfInit, elementType,
533
70
                                         elementAlign,
534
70
                                         CGF.getDestroyer(dtorKind));
535
70
    cleanup = CGF.EHStack.stable_begin();
536
537
  // Otherwise, remember that we didn't need a cleanup.
538
1.56k
  } else {
539
1.56k
    dtorKind = QualType::DK_none;
540
1.56k
  }
541
542
1.63k
  llvm::Value *one = llvm::ConstantInt::get(CGF.SizeTy, 1);
543
544
  // The 'current element to initialize'.  The invariants on this
545
  // variable are complicated.  Essentially, after each iteration of
546
  // the loop, it points to the last initialized element, except
547
  // that it points to the beginning of the array before any
548
  // elements have been initialized.
549
1.63k
  llvm::Value *element = begin;
550
551
  // Emit the explicit initializers.
552
4.98k
  for (uint64_t i = 0; i != NumInitElements; 
++i3.34k
) {
553
    // Advance to the next element.
554
3.34k
    if (i > 0) {
555
1.74k
      element = Builder.CreateInBoundsGEP(element, one, "arrayinit.element");
556
557
      // Tell the cleanup that it needs to destroy up to this
558
      // element.  TODO: some of these stores can be trivially
559
      // observed to be unnecessary.
560
1.74k
      if (endOfInit.isValid()) 
Builder.CreateStore(element, endOfInit)110
;
561
1.74k
    }
562
563
3.34k
    LValue elementLV =
564
3.34k
      CGF.MakeAddrLValue(Address(element, elementAlign), elementType);
565
3.34k
    EmitInitializationToLValue(E->getInit(i), elementLV);
566
3.34k
  }
567
568
  // Check whether there's a non-trivial array-fill expression.
569
1.63k
  Expr *filler = E->getArrayFiller();
570
1.63k
  bool hasTrivialFiller = isTrivialFiller(filler);
571
572
  // Any remaining elements need to be zero-initialized, possibly
573
  // using the filler expression.  We can skip this if the we're
574
  // emitting to zeroed memory.
575
1.63k
  if (NumInitElements != NumArrayElements &&
576
61
      !(Dest.isZeroed() && 
hasTrivialFiller29
&&
577
41
        
CGF.getTypes().isZeroInitializable(elementType)20
)) {
578
579
    // Use an actual loop.  This is basically
580
    //   do { *array++ = filler; } while (array != end);
581
582
    // Advance to the start of the rest of the array.
583
41
    if (NumInitElements) {
584
19
      element = Builder.CreateInBoundsGEP(element, one, "arrayinit.start");
585
19
      if (endOfInit.isValid()) 
Builder.CreateStore(element, endOfInit)2
;
586
19
    }
587
588
    // Compute the end of the array.
589
41
    llvm::Value *end = Builder.CreateInBoundsGEP(begin,
590
41
                      llvm::ConstantInt::get(CGF.SizeTy, NumArrayElements),
591
41
                                                 "arrayinit.end");
592
593
41
    llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
594
41
    llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body");
595
596
    // Jump into the body.
597
41
    CGF.EmitBlock(bodyBB);
598
41
    llvm::PHINode *currentElement =
599
41
      Builder.CreatePHI(element->getType(), 2, "arrayinit.cur");
600
41
    currentElement->addIncoming(element, entryBB);
601
602
    // Emit the actual filler expression.
603
41
    {
604
      // C++1z [class.temporary]p5:
605
      //   when a default constructor is called to initialize an element of
606
      //   an array with no corresponding initializer [...] the destruction of
607
      //   every temporary created in a default argument is sequenced before
608
      //   the construction of the next array element, if any
609
41
      CodeGenFunction::RunCleanupsScope CleanupsScope(CGF);
610
41
      LValue elementLV =
611
41
        CGF.MakeAddrLValue(Address(currentElement, elementAlign), elementType);
612
41
      if (filler)
613
41
        EmitInitializationToLValue(filler, elementLV);
614
0
      else
615
0
        EmitNullInitializationToLValue(elementLV);
616
41
    }
617
618
    // Move on to the next element.
619
41
    llvm::Value *nextElement =
620
41
      Builder.CreateInBoundsGEP(currentElement, one, "arrayinit.next");
621
622
    // Tell the EH cleanup that we finished with the last element.
623
41
    if (endOfInit.isValid()) 
Builder.CreateStore(nextElement, endOfInit)2
;
624
625
    // Leave the loop if we're done.
626
41
    llvm::Value *done = Builder.CreateICmpEQ(nextElement, end,
627
41
                                             "arrayinit.done");
628
41
    llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end");
629
41
    Builder.CreateCondBr(done, endBB, bodyBB);
630
41
    currentElement->addIncoming(nextElement, Builder.GetInsertBlock());
631
632
41
    CGF.EmitBlock(endBB);
633
41
  }
634
635
  // Leave the partial-array cleanup if we entered one.
636
1.63k
  if (dtorKind) 
CGF.DeactivateCleanupBlock(cleanup, cleanupDominator)70
;
637
1.63k
}
638
639
//===----------------------------------------------------------------------===//
640
//                            Visitor Methods
641
//===----------------------------------------------------------------------===//
642
643
12.3k
void AggExprEmitter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E){
644
12.3k
  Visit(E->getSubExpr());
645
12.3k
}
646
647
20
void AggExprEmitter::VisitOpaqueValueExpr(OpaqueValueExpr *e) {
648
  // If this is a unique OVE, just visit its source expression.
649
20
  if (e->isUnique())
650
7
    Visit(e->getSourceExpr());
651
13
  else
652
13
    EmitFinalDestCopy(e->getType(), CGF.getOrCreateOpaqueLValueMapping(e));
653
20
}
654
655
void
656
683
AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
657
683
  if (Dest.isPotentiallyAliased() &&
658
20
      E->getType().isPODType(CGF.getContext())) {
659
    // For a POD type, just emit a load of the lvalue + a copy, because our
660
    // compound literal might alias the destination.
661
20
    EmitAggLoadOfLValue(E);
662
20
    return;
663
20
  }
664
665
663
  AggValueSlot Slot = EnsureSlot(E->getType());
666
667
  // Block-scope compound literals are destroyed at the end of the enclosing
668
  // scope in C.
669
663
  bool Destruct =
670
663
      !CGF.getLangOpts().CPlusPlus && 
!Slot.isExternallyDestructed()361
;
671
663
  if (Destruct)
672
20
    Slot.setExternallyDestructed();
673
674
663
  CGF.EmitAggExpr(E->getInitializer(), Slot);
675
676
663
  if (Destruct)
677
20
    if (QualType::DestructionKind DtorKind = E->getType().isDestructedType())
678
0
      CGF.pushLifetimeExtendedDestroy(
679
0
          CGF.getCleanupKind(DtorKind), Slot.getAddress(), E->getType(),
680
0
          CGF.getDestroyer(DtorKind), DtorKind & EHCleanup);
681
663
}
682
683
/// Attempt to look through various unimportant expressions to find a
684
/// cast of the given kind.
685
11
static Expr *findPeephole(Expr *op, CastKind kind, const ASTContext &ctx) {
686
11
  op = op->IgnoreParenNoopCasts(ctx);
687
11
  if (auto castE = dyn_cast<CastExpr>(op)) {
688
0
    if (castE->getCastKind() == kind)
689
0
      return castE->getSubExpr();
690
11
  }
691
11
  return nullptr;
692
11
}
693
694
13.0k
void AggExprEmitter::VisitCastExpr(CastExpr *E) {
695
13.0k
  if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
696
3.49k
    CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
697
13.0k
  switch (E->getCastKind()) {
698
0
  case CK_Dynamic: {
699
    // FIXME: Can this actually happen? We have no test coverage for it.
700
0
    assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
701
0
    LValue LV = CGF.EmitCheckedLValue(E->getSubExpr(),
702
0
                                      CodeGenFunction::TCK_Load);
703
    // FIXME: Do we also need to handle property references here?
704
0
    if (LV.isSimple())
705
0
      CGF.EmitDynamicCast(LV.getAddress(CGF), cast<CXXDynamicCastExpr>(E));
706
0
    else
707
0
      CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
708
709
0
    if (!Dest.isIgnored())
710
0
      CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
711
0
    break;
712
0
  }
713
714
7
  case CK_ToUnion: {
715
    // Evaluate even if the destination is ignored.
716
7
    if (Dest.isIgnored()) {
717
1
      CGF.EmitAnyExpr(E->getSubExpr(), AggValueSlot::ignored(),
718
1
                      /*ignoreResult=*/true);
719
1
      break;
720
1
    }
721
722
    // GCC union extension
723
6
    QualType Ty = E->getSubExpr()->getType();
724
6
    Address CastPtr =
725
6
      Builder.CreateElementBitCast(Dest.getAddress(), CGF.ConvertType(Ty));
726
6
    EmitInitializationToLValue(E->getSubExpr(),
727
6
                               CGF.MakeAddrLValue(CastPtr, Ty));
728
6
    break;
729
6
  }
730
731
4
  case CK_LValueToRValueBitCast: {
732
4
    if (Dest.isIgnored()) {
733
0
      CGF.EmitAnyExpr(E->getSubExpr(), AggValueSlot::ignored(),
734
0
                      /*ignoreResult=*/true);
735
0
      break;
736
0
    }
737
738
4
    LValue SourceLV = CGF.EmitLValue(E->getSubExpr());
739
4
    Address SourceAddress =
740
4
        Builder.CreateElementBitCast(SourceLV.getAddress(CGF), CGF.Int8Ty);
741
4
    Address DestAddress =
742
4
        Builder.CreateElementBitCast(Dest.getAddress(), CGF.Int8Ty);
743
4
    llvm::Value *SizeVal = llvm::ConstantInt::get(
744
4
        CGF.SizeTy,
745
4
        CGF.getContext().getTypeSizeInChars(E->getType()).getQuantity());
746
4
    Builder.CreateMemCpy(DestAddress, SourceAddress, SizeVal);
747
4
    break;
748
4
  }
749
750
0
  case CK_DerivedToBase:
751
0
  case CK_BaseToDerived:
752
0
  case CK_UncheckedDerivedToBase: {
753
0
    llvm_unreachable("cannot perform hierarchy conversion in EmitAggExpr: "
754
0
                "should have been unpacked before we got here");
755
0
  }
756
757
17
  case CK_NonAtomicToAtomic:
758
25
  case CK_AtomicToNonAtomic: {
759
25
    bool isToAtomic = (E->getCastKind() == CK_NonAtomicToAtomic);
760
761
    // Determine the atomic and value types.
762
25
    QualType atomicType = E->getSubExpr()->getType();
763
25
    QualType valueType = E->getType();
764
25
    if (isToAtomic) 
std::swap(atomicType, valueType)17
;
765
766
25
    assert(atomicType->isAtomicType());
767
25
    assert(CGF.getContext().hasSameUnqualifiedType(valueType,
768
25
                          atomicType->castAs<AtomicType>()->getValueType()));
769
770
    // Just recurse normally if we're ignoring the result or the
771
    // atomic type doesn't change representation.
772
25
    if (Dest.isIgnored() || !CGF.CGM.isPaddedAtomicType(atomicType)) {
773
14
      return Visit(E->getSubExpr());
774
14
    }
775
776
11
    CastKind peepholeTarget =
777
7
      (isToAtomic ? CK_AtomicToNonAtomic : 
CK_NonAtomicToAtomic4
);
778
779
    // These two cases are reverses of each other; try to peephole them.
780
11
    if (Expr *op =
781
0
            findPeephole(E->getSubExpr(), peepholeTarget, CGF.getContext())) {
782
0
      assert(CGF.getContext().hasSameUnqualifiedType(op->getType(),
783
0
                                                     E->getType()) &&
784
0
           "peephole significantly changed types?");
785
0
      return Visit(op);
786
0
    }
787
788
    // If we're converting an r-value of non-atomic type to an r-value
789
    // of atomic type, just emit directly into the relevant sub-object.
790
11
    if (isToAtomic) {
791
7
      AggValueSlot valueDest = Dest;
792
7
      if (!valueDest.isIgnored() && CGF.CGM.isPaddedAtomicType(atomicType)) {
793
        // Zero-initialize.  (Strictly speaking, we only need to initialize
794
        // the padding at the end, but this is simpler.)
795
7
        if (!Dest.isZeroed())
796
7
          CGF.EmitNullInitialization(Dest.getAddress(), atomicType);
797
798
        // Build a GEP to refer to the subobject.
799
7
        Address valueAddr =
800
7
            CGF.Builder.CreateStructGEP(valueDest.getAddress(), 0);
801
7
        valueDest = AggValueSlot::forAddr(valueAddr,
802
7
                                          valueDest.getQualifiers(),
803
7
                                          valueDest.isExternallyDestructed(),
804
7
                                          valueDest.requiresGCollection(),
805
7
                                          valueDest.isPotentiallyAliased(),
806
7
                                          AggValueSlot::DoesNotOverlap,
807
7
                                          AggValueSlot::IsZeroed);
808
7
      }
809
810
7
      CGF.EmitAggExpr(E->getSubExpr(), valueDest);
811
7
      return;
812
7
    }
813
814
    // Otherwise, we're converting an atomic type to a non-atomic type.
815
    // Make an atomic temporary, emit into that, and then copy the value out.
816
4
    AggValueSlot atomicSlot =
817
4
      CGF.CreateAggTemp(atomicType, "atomic-to-nonatomic.temp");
818
4
    CGF.EmitAggExpr(E->getSubExpr(), atomicSlot);
819
820
4
    Address valueAddr = Builder.CreateStructGEP(atomicSlot.getAddress(), 0);
821
4
    RValue rvalue = RValue::getAggregate(valueAddr, atomicSlot.isVolatile());
822
4
    return EmitFinalDestCopy(valueType, rvalue);
823
4
  }
824
0
  case CK_AddressSpaceConversion:
825
0
     return Visit(E->getSubExpr());
826
827
2.83k
  case CK_LValueToRValue:
828
    // If we're loading from a volatile type, force the destination
829
    // into existence.
830
2.83k
    if (E->getSubExpr()->getType().isVolatileQualified()) {
831
24
      bool Destruct =
832
24
          !Dest.isExternallyDestructed() &&
833
12
          E->getType().isDestructedType() == QualType::DK_nontrivial_c_struct;
834
24
      if (Destruct)
835
2
        Dest.setExternallyDestructed();
836
24
      EnsureDest(E->getType());
837
24
      Visit(E->getSubExpr());
838
839
24
      if (Destruct)
840
2
        CGF.pushDestroy(QualType::DK_nontrivial_c_struct, Dest.getAddress(),
841
2
                        E->getType());
842
843
24
      return;
844
24
    }
845
846
2.80k
    LLVM_FALLTHROUGH;
847
848
849
6.72k
  case CK_NoOp:
850
6.78k
  case CK_UserDefinedConversion:
851
13.0k
  case CK_ConstructorConversion:
852
13.0k
    assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
853
13.0k
                                                   E->getType()) &&
854
13.0k
           "Implicit cast types must be compatible");
855
13.0k
    Visit(E->getSubExpr());
856
13.0k
    break;
857
858
0
  case CK_LValueBitCast:
859
0
    llvm_unreachable("should not be emitting lvalue bitcast as rvalue");
860
861
0
  case CK_Dependent:
862
0
  case CK_BitCast:
863
0
  case CK_ArrayToPointerDecay:
864
0
  case CK_FunctionToPointerDecay:
865
0
  case CK_NullToPointer:
866
0
  case CK_NullToMemberPointer:
867
0
  case CK_BaseToDerivedMemberPointer:
868
0
  case CK_DerivedToBaseMemberPointer:
869
0
  case CK_MemberPointerToBoolean:
870
0
  case CK_ReinterpretMemberPointer:
871
0
  case CK_IntegralToPointer:
872
0
  case CK_PointerToIntegral:
873
0
  case CK_PointerToBoolean:
874
0
  case CK_ToVoid:
875
0
  case CK_VectorSplat:
876
0
  case CK_IntegralCast:
877
0
  case CK_BooleanToSignedIntegral:
878
0
  case CK_IntegralToBoolean:
879
0
  case CK_IntegralToFloating:
880
0
  case CK_FloatingToIntegral:
881
0
  case CK_FloatingToBoolean:
882
0
  case CK_FloatingCast:
883
0
  case CK_CPointerToObjCPointerCast:
884
0
  case CK_BlockPointerToObjCPointerCast:
885
0
  case CK_AnyPointerToBlockPointerCast:
886
0
  case CK_ObjCObjectLValueCast:
887
0
  case CK_FloatingRealToComplex:
888
0
  case CK_FloatingComplexToReal:
889
0
  case CK_FloatingComplexToBoolean:
890
0
  case CK_FloatingComplexCast:
891
0
  case CK_FloatingComplexToIntegralComplex:
892
0
  case CK_IntegralRealToComplex:
893
0
  case CK_IntegralComplexToReal:
894
0
  case CK_IntegralComplexToBoolean:
895
0
  case CK_IntegralComplexCast:
896
0
  case CK_IntegralComplexToFloatingComplex:
897
0
  case CK_ARCProduceObject:
898
0
  case CK_ARCConsumeObject:
899
0
  case CK_ARCReclaimReturnedObject:
900
0
  case CK_ARCExtendBlockObject:
901
0
  case CK_CopyAndAutoreleaseBlockObject:
902
0
  case CK_BuiltinFnToFnPtr:
903
0
  case CK_ZeroToOCLOpaqueType:
904
905
0
  case CK_IntToOCLSampler:
906
0
  case CK_FloatingToFixedPoint:
907
0
  case CK_FixedPointToFloating:
908
0
  case CK_FixedPointCast:
909
0
  case CK_FixedPointToBoolean:
910
0
  case CK_FixedPointToIntegral:
911
0
  case CK_IntegralToFixedPoint:
912
0
    llvm_unreachable("cast kind invalid for aggregate types");
913
13.0k
  }
914
13.0k
}
915
916
6.73k
void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
917
6.73k
  if (E->getCallReturnType(CGF.getContext())->isReferenceType()) {
918
0
    EmitAggLoadOfLValue(E);
919
0
    return;
920
0
  }
921
922
6.73k
  withReturnValueSlot(E, [&](ReturnValueSlot Slot) {
923
6.73k
    return CGF.EmitCallExpr(E, Slot);
924
6.73k
  });
925
6.73k
}
926
927
69
void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
928
69
  withReturnValueSlot(E, [&](ReturnValueSlot Slot) {
929
69
    return CGF.EmitObjCMessageExpr(E, Slot);
930
69
  });
931
69
}
932
933
44
void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
934
44
  CGF.EmitIgnoredExpr(E->getLHS());
935
44
  Visit(E->getRHS());
936
44
}
937
938
645
void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
939
645
  CodeGenFunction::StmtExprEvaluation eval(CGF);
940
645
  CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
941
645
}
942
943
enum CompareKind {
944
  CK_Less,
945
  CK_Greater,
946
  CK_Equal,
947
};
948
949
static llvm::Value *EmitCompare(CGBuilderTy &Builder, CodeGenFunction &CGF,
950
                                const BinaryOperator *E, llvm::Value *LHS,
951
                                llvm::Value *RHS, CompareKind Kind,
952
27
                                const char *NameSuffix = "") {
953
27
  QualType ArgTy = E->getLHS()->getType();
954
27
  if (const ComplexType *CT = ArgTy->getAs<ComplexType>())
955
0
    ArgTy = CT->getElementType();
956
957
27
  if (const auto *MPT = ArgTy->getAs<MemberPointerType>()) {
958
0
    assert(Kind == CK_Equal &&
959
0
           "member pointers may only be compared for equality");
960
0
    return CGF.CGM.getCXXABI().EmitMemberPointerComparison(
961
0
        CGF, LHS, RHS, MPT, /*IsInequality*/ false);
962
0
  }
963
964
  // Compute the comparison instructions for the specified comparison kind.
965
27
  struct CmpInstInfo {
966
27
    const char *Name;
967
27
    llvm::CmpInst::Predicate FCmp;
968
27
    llvm::CmpInst::Predicate SCmp;
969
27
    llvm::CmpInst::Predicate UCmp;
970
27
  };
971
27
  CmpInstInfo InstInfo = [&]() -> CmpInstInfo {
972
27
    using FI = llvm::FCmpInst;
973
27
    using II = llvm::ICmpInst;
974
27
    switch (Kind) {
975
13
    case CK_Less:
976
13
      return {"cmp.lt", FI::FCMP_OLT, II::ICMP_SLT, II::ICMP_ULT};
977
1
    case CK_Greater:
978
1
      return {"cmp.gt", FI::FCMP_OGT, II::ICMP_SGT, II::ICMP_UGT};
979
13
    case CK_Equal:
980
13
      return {"cmp.eq", FI::FCMP_OEQ, II::ICMP_EQ, II::ICMP_EQ};
981
0
    }
982
0
    llvm_unreachable("Unrecognised CompareKind enum");
983
0
  }();
984
985
27
  if (ArgTy->hasFloatingRepresentation())
986
3
    return Builder.CreateFCmp(InstInfo.FCmp, LHS, RHS,
987
3
                              llvm::Twine(InstInfo.Name) + NameSuffix);
988
24
  if (ArgTy->isIntegralOrEnumerationType() || 
ArgTy->isPointerType()2
) {
989
24
    auto Inst =
990
14
        ArgTy->hasSignedIntegerRepresentation() ? InstInfo.SCmp : 
InstInfo.UCmp10
;
991
24
    return Builder.CreateICmp(Inst, LHS, RHS,
992
24
                              llvm::Twine(InstInfo.Name) + NameSuffix);
993
24
  }
994
995
0
  llvm_unreachable("unsupported aggregate binary expression should have "
996
0
                   "already been handled");
997
0
}
998
999
13
void AggExprEmitter::VisitBinCmp(const BinaryOperator *E) {
1000
13
  using llvm::BasicBlock;
1001
13
  using llvm::PHINode;
1002
13
  using llvm::Value;
1003
13
  assert(CGF.getContext().hasSameType(E->getLHS()->getType(),
1004
13
                                      E->getRHS()->getType()));
1005
13
  const ComparisonCategoryInfo &CmpInfo =
1006
13
      CGF.getContext().CompCategories.getInfoForType(E->getType());
1007
13
  assert(CmpInfo.Record->isTriviallyCopyable() &&
1008
13
         "cannot copy non-trivially copyable aggregate");
1009
1010
13
  QualType ArgTy = E->getLHS()->getType();
1011
1012
13
  if (!ArgTy->isIntegralOrEnumerationType() && 
!ArgTy->isRealFloatingType()2
&&
1013
1
      !ArgTy->isNullPtrType() && !ArgTy->isPointerType() &&
1014
0
      !ArgTy->isMemberPointerType() && !ArgTy->isAnyComplexType()) {
1015
0
    return CGF.ErrorUnsupported(E, "aggregate three-way comparison");
1016
0
  }
1017
13
  bool IsComplex = ArgTy->isAnyComplexType();
1018
1019
  // Evaluate the operands to the expression and extract their values.
1020
26
  auto EmitOperand = [&](Expr *E) -> std::pair<Value *, Value *> {
1021
26
    RValue RV = CGF.EmitAnyExpr(E);
1022
26
    if (RV.isScalar())
1023
26
      return {RV.getScalarVal(), nullptr};
1024
0
    if (RV.isAggregate())
1025
0
      return {RV.getAggregatePointer(), nullptr};
1026
0
    assert(RV.isComplex());
1027
0
    return RV.getComplexVal();
1028
0
  };
1029
13
  auto LHSValues = EmitOperand(E->getLHS()),
1030
13
       RHSValues = EmitOperand(E->getRHS());
1031
1032
27
  auto EmitCmp = [&](CompareKind K) {
1033
27
    Value *Cmp = EmitCompare(Builder, CGF, E, LHSValues.first, RHSValues.first,
1034
27
                             K, IsComplex ? 
".r"0
: "");
1035
27
    if (!IsComplex)
1036
27
      return Cmp;
1037
0
    assert(K == CompareKind::CK_Equal);
1038
0
    Value *CmpImag = EmitCompare(Builder, CGF, E, LHSValues.second,
1039
0
                                 RHSValues.second, K, ".i");
1040
0
    return Builder.CreateAnd(Cmp, CmpImag, "and.eq");
1041
0
  };
1042
40
  auto EmitCmpRes = [&](const ComparisonCategoryInfo::ValueInfo *VInfo) {
1043
40
    return Builder.getInt(VInfo->getIntValue());
1044
40
  };
1045
1046
13
  Value *Select;
1047
13
  if (ArgTy->isNullPtrType()) {
1048
0
    Select = EmitCmpRes(CmpInfo.getEqualOrEquiv());
1049
13
  } else if (!CmpInfo.isPartial()) {
1050
12
    Value *SelectOne =
1051
12
        Builder.CreateSelect(EmitCmp(CK_Less), EmitCmpRes(CmpInfo.getLess()),
1052
12
                             EmitCmpRes(CmpInfo.getGreater()), "sel.lt");
1053
12
    Select = Builder.CreateSelect(EmitCmp(CK_Equal),
1054
12
                                  EmitCmpRes(CmpInfo.getEqualOrEquiv()),
1055
12
                                  SelectOne, "sel.eq");
1056
1
  } else {
1057
1
    Value *SelectEq = Builder.CreateSelect(
1058
1
        EmitCmp(CK_Equal), EmitCmpRes(CmpInfo.getEqualOrEquiv()),
1059
1
        EmitCmpRes(CmpInfo.getUnordered()), "sel.eq");
1060
1
    Value *SelectGT = Builder.CreateSelect(EmitCmp(CK_Greater),
1061
1
                                           EmitCmpRes(CmpInfo.getGreater()),
1062
1
                                           SelectEq, "sel.gt");
1063
1
    Select = Builder.CreateSelect(
1064
1
        EmitCmp(CK_Less), EmitCmpRes(CmpInfo.getLess()), SelectGT, "sel.lt");
1065
1
  }
1066
  // Create the return value in the destination slot.
1067
13
  EnsureDest(E->getType());
1068
13
  LValue DestLV = CGF.MakeAddrLValue(Dest.getAddress(), E->getType());
1069
1070
  // Emit the address of the first (and only) field in the comparison category
1071
  // type, and initialize it from the constant integer value selected above.
1072
13
  LValue FieldLV = CGF.EmitLValueForFieldInitialization(
1073
13
      DestLV, *CmpInfo.Record->field_begin());
1074
13
  CGF.EmitStoreThroughLValue(RValue::get(Select), FieldLV, /*IsInit*/ true);
1075
1076
  // All done! The result is in the Dest slot.
1077
13
}
1078
1079
0
void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
1080
0
  if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
1081
0
    VisitPointerToDataMemberBinaryOperator(E);
1082
0
  else
1083
0
    CGF.ErrorUnsupported(E, "aggregate binary expression");
1084
0
}
1085
1086
void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
1087
0
                                                    const BinaryOperator *E) {
1088
0
  LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
1089
0
  EmitFinalDestCopy(E->getType(), LV);
1090
0
}
1091
1092
/// Is the value of the given expression possibly a reference to or
1093
/// into a __block variable?
1094
701
static bool isBlockVarRef(const Expr *E) {
1095
  // Make sure we look through parens.
1096
701
  E = E->IgnoreParens();
1097
1098
  // Check for a direct reference to a __block variable.
1099
701
  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
1100
249
    const VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl());
1101
249
    return (var && var->hasAttr<BlocksAttr>());
1102
249
  }
1103
1104
  // More complicated stuff.
1105
1106
  // Binary operators.
1107
452
  if (const BinaryOperator *op = dyn_cast<BinaryOperator>(E)) {
1108
    // For an assignment or pointer-to-member operation, just care
1109
    // about the LHS.
1110
0
    if (op->isAssignmentOp() || op->isPtrMemOp())
1111
0
      return isBlockVarRef(op->getLHS());
1112
1113
    // For a comma, just care about the RHS.
1114
0
    if (op->getOpcode() == BO_Comma)
1115
0
      return isBlockVarRef(op->getRHS());
1116
1117
    // FIXME: pointer arithmetic?
1118
0
    return false;
1119
1120
  // Check both sides of a conditional operator.
1121
452
  } else if (const AbstractConditionalOperator *op
1122
0
               = dyn_cast<AbstractConditionalOperator>(E)) {
1123
0
    return isBlockVarRef(op->getTrueExpr())
1124
0
        || isBlockVarRef(op->getFalseExpr());
1125
1126
  // OVEs are required to support BinaryConditionalOperators.
1127
452
  } else if (const OpaqueValueExpr *op
1128
0
               = dyn_cast<OpaqueValueExpr>(E)) {
1129
0
    if (const Expr *src = op->getSourceExpr())
1130
0
      return isBlockVarRef(src);
1131
1132
  // Casts are necessary to get things like (*(int*)&var) = foo().
1133
  // We don't really care about the kind of cast here, except
1134
  // we don't want to look through l2r casts, because it's okay
1135
  // to get the *value* in a __block variable.
1136
452
  } else if (const CastExpr *cast = dyn_cast<CastExpr>(E)) {
1137
227
    if (cast->getCastKind() == CK_LValueToRValue)
1138
213
      return false;
1139
14
    return isBlockVarRef(cast->getSubExpr());
1140
1141
  // Handle unary operators.  Again, just aggressively look through
1142
  // it, ignoring the operation.
1143
225
  } else if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E)) {
1144
200
    return isBlockVarRef(uop->getSubExpr());
1145
1146
  // Look into the base of a field access.
1147
25
  } else if (const MemberExpr *mem = dyn_cast<MemberExpr>(E)) {
1148
5
    return isBlockVarRef(mem->getBase());
1149
1150
  // Look into the base of a subscript.
1151
20
  } else if (const ArraySubscriptExpr *sub = dyn_cast<ArraySubscriptExpr>(E)) {
1152
13
    return isBlockVarRef(sub->getBase());
1153
13
  }
1154
1155
7
  return false;
1156
7
}
1157
1158
469
void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1159
  // For an assignment to work, the value on the right has
1160
  // to be compatible with the value on the left.
1161
469
  assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
1162
469
                                                 E->getRHS()->getType())
1163
469
         && "Invalid assignment");
1164
1165
  // If the LHS might be a __block variable, and the RHS can
1166
  // potentially cause a block copy, we need to evaluate the RHS first
1167
  // so that the assignment goes the right place.
1168
  // This is pretty semantically fragile.
1169
469
  if (isBlockVarRef(E->getLHS()) &&
1170
3
      E->getRHS()->HasSideEffects(CGF.getContext())) {
1171
    // Ensure that we have a destination, and evaluate the RHS into that.
1172
3
    EnsureDest(E->getRHS()->getType());
1173
3
    Visit(E->getRHS());
1174
1175
    // Now emit the LHS and copy into it.
1176
3
    LValue LHS = CGF.EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
1177
1178
    // That copy is an atomic copy if the LHS is atomic.
1179
3
    if (LHS.getType()->isAtomicType() ||
1180
3
        CGF.LValueIsSuitableForInlineAtomic(LHS)) {
1181
0
      CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
1182
0
      return;
1183
0
    }
1184
1185
3
    EmitCopy(E->getLHS()->getType(),
1186
3
             AggValueSlot::forLValue(LHS, CGF, AggValueSlot::IsDestructed,
1187
3
                                     needsGC(E->getLHS()->getType()),
1188
3
                                     AggValueSlot::IsAliased,
1189
3
                                     AggValueSlot::MayOverlap),
1190
3
             Dest);
1191
3
    return;
1192
3
  }
1193
1194
466
  LValue LHS = CGF.EmitLValue(E->getLHS());
1195
1196
  // If we have an atomic type, evaluate into the destination and then
1197
  // do an atomic copy.
1198
466
  if (LHS.getType()->isAtomicType() ||
1199
457
      CGF.LValueIsSuitableForInlineAtomic(LHS)) {
1200
12
    EnsureDest(E->getRHS()->getType());
1201
12
    Visit(E->getRHS());
1202
12
    CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
1203
12
    return;
1204
12
  }
1205
1206
  // Codegen the RHS so that it stores directly into the LHS.
1207
454
  AggValueSlot LHSSlot = AggValueSlot::forLValue(
1208
454
      LHS, CGF, AggValueSlot::IsDestructed, needsGC(E->getLHS()->getType()),
1209
454
      AggValueSlot::IsAliased, AggValueSlot::MayOverlap);
1210
  // A non-volatile aggregate destination might have volatile member.
1211
454
  if (!LHSSlot.isVolatile() &&
1212
438
      CGF.hasVolatileMember(E->getLHS()->getType()))
1213
5
    LHSSlot.setVolatile(true);
1214
1215
454
  CGF.EmitAggExpr(E->getRHS(), LHSSlot);
1216
1217
  // Copy into the destination if the assignment isn't ignored.
1218
454
  EmitFinalDestCopy(E->getType(), LHS);
1219
1220
454
  if (!Dest.isIgnored() && 
!Dest.isExternallyDestructed()9
&&
1221
4
      E->getType().isDestructedType() == QualType::DK_nontrivial_c_struct)
1222
2
    CGF.pushDestroy(QualType::DK_nontrivial_c_struct, Dest.getAddress(),
1223
2
                    E->getType());
1224
454
}
1225
1226
void AggExprEmitter::
1227
60
VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
1228
60
  llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1229
60
  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
1230
60
  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
1231
1232
  // Bind the common expression if necessary.
1233
60
  CodeGenFunction::OpaqueValueMapping binding(CGF, E);
1234
1235
60
  CodeGenFunction::ConditionalEvaluation eval(CGF);
1236
60
  CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
1237
60
                           CGF.getProfileCount(E));
1238
1239
  // Save whether the destination's lifetime is externally managed.
1240
60
  bool isExternallyDestructed = Dest.isExternallyDestructed();
1241
60
  bool destructNonTrivialCStruct =
1242
60
      !isExternallyDestructed &&
1243
12
      E->getType().isDestructedType() == QualType::DK_nontrivial_c_struct;
1244
60
  isExternallyDestructed |= destructNonTrivialCStruct;
1245
60
  Dest.setExternallyDestructed(isExternallyDestructed);
1246
1247
60
  eval.begin(CGF);
1248
60
  CGF.EmitBlock(LHSBlock);
1249
60
  CGF.incrementProfileCounter(E);
1250
60
  Visit(E->getTrueExpr());
1251
60
  eval.end(CGF);
1252
1253
60
  assert(CGF.HaveInsertPoint() && "expression evaluation ended with no IP!");
1254
60
  CGF.Builder.CreateBr(ContBlock);
1255
1256
  // If the result of an agg expression is unused, then the emission
1257
  // of the LHS might need to create a destination slot.  That's fine
1258
  // with us, and we can safely emit the RHS into the same slot, but
1259
  // we shouldn't claim that it's already being destructed.
1260
60
  Dest.setExternallyDestructed(isExternallyDestructed);
1261
1262
60
  eval.begin(CGF);
1263
60
  CGF.EmitBlock(RHSBlock);
1264
60
  Visit(E->getFalseExpr());
1265
60
  eval.end(CGF);
1266
1267
60
  if (destructNonTrivialCStruct)
1268
2
    CGF.pushDestroy(QualType::DK_nontrivial_c_struct, Dest.getAddress(),
1269
2
                    E->getType());
1270
1271
60
  CGF.EmitBlock(ContBlock);
1272
60
}
1273
1274
0
void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
1275
0
  Visit(CE->getChosenSubExpr());
1276
0
}
1277
1278
308
void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
1279
308
  Address ArgValue = Address::invalid();
1280
308
  Address ArgPtr = CGF.EmitVAArg(VE, ArgValue);
1281
1282
  // If EmitVAArg fails, emit an error.
1283
308
  if (!ArgPtr.isValid()) {
1284
0
    CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
1285
0
    return;
1286
0
  }
1287
1288
308
  EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType()));
1289
308
}
1290
1291
6.12k
void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
1292
  // Ensure that we have a slot, but if we already do, remember
1293
  // whether it was externally destructed.
1294
6.12k
  bool wasExternallyDestructed = Dest.isExternallyDestructed();
1295
6.12k
  EnsureDest(E->getType());
1296
1297
  // We're going to push a destructor if there isn't already one.
1298
6.12k
  Dest.setExternallyDestructed();
1299
1300
6.12k
  Visit(E->getSubExpr());
1301
1302
  // Push that destructor we promised.
1303
6.12k
  if (!wasExternallyDestructed)
1304
778
    CGF.EmitCXXTemporary(E->getTemporary(), E->getType(), Dest.getAddress());
1305
6.12k
}
1306
1307
void
1308
60.1k
AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
1309
60.1k
  AggValueSlot Slot = EnsureSlot(E->getType());
1310
60.1k
  CGF.EmitCXXConstructExpr(E, Slot);
1311
60.1k
}
1312
1313
void AggExprEmitter::VisitCXXInheritedCtorInitExpr(
1314
200
    const CXXInheritedCtorInitExpr *E) {
1315
200
  AggValueSlot Slot = EnsureSlot(E->getType());
1316
200
  CGF.EmitInheritedCXXConstructorCall(
1317
200
      E->getConstructor(), E->constructsVBase(), Slot.getAddress(),
1318
200
      E->inheritedFromVBase(), E);
1319
200
}
1320
1321
void
1322
1.52k
AggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {
1323
1.52k
  AggValueSlot Slot = EnsureSlot(E->getType());
1324
1.52k
  LValue SlotLV = CGF.MakeAddrLValue(Slot.getAddress(), E->getType());
1325
1326
  // We'll need to enter cleanup scopes in case any of the element
1327
  // initializers throws an exception.
1328
1.52k
  SmallVector<EHScopeStack::stable_iterator, 16> Cleanups;
1329
1.52k
  llvm::Instruction *CleanupDominator = nullptr;
1330
1331
1.52k
  CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin();
1332
1.52k
  for (LambdaExpr::const_capture_init_iterator i = E->capture_init_begin(),
1333
1.52k
                                               e = E->capture_init_end();
1334
4.06k
       i != e; 
++i, ++CurField2.53k
) {
1335
    // Emit initialization
1336
2.53k
    LValue LV = CGF.EmitLValueForFieldInitialization(SlotLV, *CurField);
1337
2.53k
    if (CurField->hasCapturedVLAType()) {
1338
24
      CGF.EmitLambdaVLACapture(CurField->getCapturedVLAType(), LV);
1339
24
      continue;
1340
24
    }
1341
1342
2.51k
    EmitInitializationToLValue(*i, LV);
1343
1344
    // Push a destructor if necessary.
1345
2.51k
    if (QualType::DestructionKind DtorKind =
1346
26
            CurField->getType().isDestructedType()) {
1347
26
      assert(LV.isSimple());
1348
26
      if (CGF.needsEHCleanup(DtorKind)) {
1349
19
        if (!CleanupDominator)
1350
18
          CleanupDominator = CGF.Builder.CreateAlignedLoad(
1351
18
              CGF.Int8Ty,
1352
18
              llvm::Constant::getNullValue(CGF.Int8PtrTy),
1353
18
              CharUnits::One()); // placeholder
1354
1355
19
        CGF.pushDestroy(EHCleanup, LV.getAddress(CGF), CurField->getType(),
1356
19
                        CGF.getDestroyer(DtorKind), false);
1357
19
        Cleanups.push_back(CGF.EHStack.stable_begin());
1358
19
      }
1359
26
    }
1360
2.51k
  }
1361
1362
  // Deactivate all the partial cleanups in reverse order, which
1363
  // generally means popping them.
1364
1.54k
  for (unsigned i = Cleanups.size(); i != 0; 
--i19
)
1365
19
    CGF.DeactivateCleanupBlock(Cleanups[i-1], CleanupDominator);
1366
1367
  // Destroy the placeholder if we made one.
1368
1.52k
  if (CleanupDominator)
1369
18
    CleanupDominator->eraseFromParent();
1370
1.52k
}
1371
1372
6.58k
void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {
1373
6.58k
  CodeGenFunction::RunCleanupsScope cleanups(CGF);
1374
6.58k
  Visit(E->getSubExpr());
1375
6.58k
}
1376
1377
0
void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1378
0
  QualType T = E->getType();
1379
0
  AggValueSlot Slot = EnsureSlot(T);
1380
0
  EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddress(), T));
1381
0
}
1382
1383
18
void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1384
18
  QualType T = E->getType();
1385
18
  AggValueSlot Slot = EnsureSlot(T);
1386
18
  EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddress(), T));
1387
18
}
1388
1389
/// Determine whether the given cast kind is known to always convert values
1390
/// with all zero bits in their value representation to values with all zero
1391
/// bits in their value representation.
1392
544
static bool castPreservesZero(const CastExpr *CE) {
1393
544
  switch (CE->getCastKind()) {
1394
    // No-ops.
1395
15
  case CK_NoOp:
1396
15
  case CK_UserDefinedConversion:
1397
15
  case CK_ConstructorConversion:
1398
140
  case CK_BitCast:
1399
140
  case CK_ToUnion:
1400
140
  case CK_ToVoid:
1401
    // Conversions between (possibly-complex) integral, (possibly-complex)
1402
    // floating-point, and bool.
1403
140
  case CK_BooleanToSignedIntegral:
1404
141
  case CK_FloatingCast:
1405
141
  case CK_FloatingComplexCast:
1406
141
  case CK_FloatingComplexToBoolean:
1407
141
  case CK_FloatingComplexToIntegralComplex:
1408
141
  case CK_FloatingComplexToReal:
1409
145
  case CK_FloatingRealToComplex:
1410
145
  case CK_FloatingToBoolean:
1411
152
  case CK_FloatingToIntegral:
1412
337
  case CK_IntegralCast:
1413
337
  case CK_IntegralComplexCast:
1414
337
  case CK_IntegralComplexToBoolean:
1415
337
  case CK_IntegralComplexToFloatingComplex:
1416
341
  case CK_IntegralComplexToReal:
1417
345
  case CK_IntegralRealToComplex:
1418
345
  case CK_IntegralToBoolean:
1419
353
  case CK_IntegralToFloating:
1420
    // Reinterpreting integers as pointers and vice versa.
1421
357
  case CK_IntegralToPointer:
1422
360
  case CK_PointerToIntegral:
1423
    // Language extensions.
1424
364
  case CK_VectorSplat:
1425
364
  case CK_NonAtomicToAtomic:
1426
364
  case CK_AtomicToNonAtomic:
1427
364
    return true;
1428
1429
0
  case CK_BaseToDerivedMemberPointer:
1430
0
  case CK_DerivedToBaseMemberPointer:
1431
0
  case CK_MemberPointerToBoolean:
1432
0
  case CK_NullToMemberPointer:
1433
0
  case CK_ReinterpretMemberPointer:
1434
    // FIXME: ABI-dependent.
1435
0
    return false;
1436
1437
0
  case CK_AnyPointerToBlockPointerCast:
1438
0
  case CK_BlockPointerToObjCPointerCast:
1439
0
  case CK_CPointerToObjCPointerCast:
1440
0
  case CK_ObjCObjectLValueCast:
1441
0
  case CK_IntToOCLSampler:
1442
0
  case CK_ZeroToOCLOpaqueType:
1443
    // FIXME: Check these.
1444
0
    return false;
1445
1446
0
  case CK_FixedPointCast:
1447
0
  case CK_FixedPointToBoolean:
1448
0
  case CK_FixedPointToFloating:
1449
0
  case CK_FixedPointToIntegral:
1450
0
  case CK_FloatingToFixedPoint:
1451
0
  case CK_IntegralToFixedPoint:
1452
    // FIXME: Do all fixed-point types represent zero as all 0 bits?
1453
0
    return false;
1454
1455
0
  case CK_AddressSpaceConversion:
1456
0
  case CK_BaseToDerived:
1457
0
  case CK_DerivedToBase:
1458
0
  case CK_Dynamic:
1459
18
  case CK_NullToPointer:
1460
18
  case CK_PointerToBoolean:
1461
    // FIXME: Preserves zeroes only if zero pointers and null pointers have the
1462
    // same representation in all involved address spaces.
1463
18
    return false;
1464
1465
0
  case CK_ARCConsumeObject:
1466
0
  case CK_ARCExtendBlockObject:
1467
0
  case CK_ARCProduceObject:
1468
0
  case CK_ARCReclaimReturnedObject:
1469
0
  case CK_CopyAndAutoreleaseBlockObject:
1470
2
  case CK_ArrayToPointerDecay:
1471
2
  case CK_FunctionToPointerDecay:
1472
2
  case CK_BuiltinFnToFnPtr:
1473
2
  case CK_Dependent:
1474
2
  case CK_LValueBitCast:
1475
162
  case CK_LValueToRValue:
1476
162
  case CK_LValueToRValueBitCast:
1477
162
  case CK_UncheckedDerivedToBase:
1478
162
    return false;
1479
0
  }
1480
0
  llvm_unreachable("Unhandled clang::CastKind enum");
1481
0
}
1482
1483
/// isSimpleZero - If emitting this value will obviously just cause a store of
1484
/// zero to memory, return true.  This can return false if uncertain, so it just
1485
/// handles simple cases.
1486
4.75k
static bool isSimpleZero(const Expr *E, CodeGenFunction &CGF) {
1487
4.75k
  E = E->IgnoreParens();
1488
5.12k
  while (auto *CE = dyn_cast<CastExpr>(E)) {
1489
544
    if (!castPreservesZero(CE))
1490
180
      break;
1491
364
    E = CE->getSubExpr()->IgnoreParens();
1492
364
  }
1493
1494
  // 0
1495
4.75k
  if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E))
1496
451
    return IL->getValue() == 0;
1497
  // +0.0
1498
4.30k
  if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(E))
1499
84
    return FL->getValue().isPosZero();
1500
  // int()
1501
4.22k
  if ((isa<ImplicitValueInitExpr>(E) || 
isa<CXXScalarValueInitExpr>(E)4.05k
) &&
1502
170
      CGF.getTypes().isZeroInitializable(E->getType()))
1503
170
    return true;
1504
  // (int*)0 - Null pointer expressions.
1505
4.05k
  if (const CastExpr *ICE = dyn_cast<CastExpr>(E))
1506
180
    return ICE->getCastKind() == CK_NullToPointer &&
1507
18
           CGF.getTypes().isPointerZeroInitializable(E->getType()) &&
1508
18
           !E->HasSideEffects(CGF.getContext());
1509
  // '\0'
1510
3.87k
  if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E))
1511
1
    return CL->getValue() == 0;
1512
1513
  // Otherwise, hard case: conservatively return false.
1514
3.87k
  return false;
1515
3.87k
}
1516
1517
1518
void
1519
8.33k
AggExprEmitter::EmitInitializationToLValue(Expr *E, LValue LV) {
1520
8.33k
  QualType type = LV.getType();
1521
  // FIXME: Ignore result?
1522
  // FIXME: Are initializers affected by volatile?
1523
8.33k
  if (Dest.isZeroed() && 
isSimpleZero(E, CGF)189
) {
1524
    // Storing "i32 0" to a zero'd memory location is a noop.
1525
115
    return;
1526
8.22k
  } else if (isa<ImplicitValueInitExpr>(E) || 
isa<CXXScalarValueInitExpr>(E)8.15k
) {
1527
63
    return EmitNullInitializationToLValue(LV);
1528
8.15k
  } else if (isa<NoInitExpr>(E)) {
1529
    // Do nothing.
1530
14
    return;
1531
8.14k
  } else if (type->isReferenceType()) {
1532
2.40k
    RValue RV = CGF.EmitReferenceBindingToExpr(E);
1533
2.40k
    return CGF.EmitStoreThroughLValue(RV, LV);
1534
2.40k
  }
1535
1536
5.74k
  switch (CGF.getEvaluationKind(type)) {
1537
9
  case TEK_Complex:
1538
9
    CGF.EmitComplexExprIntoLValue(E, LV, /*isInit*/ true);
1539
9
    return;
1540
3.14k
  case TEK_Aggregate:
1541
3.14k
    CGF.EmitAggExpr(
1542
3.14k
        E, AggValueSlot::forLValue(LV, CGF, AggValueSlot::IsDestructed,
1543
3.14k
                                   AggValueSlot::DoesNotNeedGCBarriers,
1544
3.14k
                                   AggValueSlot::IsNotAliased,
1545
3.14k
                                   AggValueSlot::MayOverlap, Dest.isZeroed()));
1546
3.14k
    return;
1547
2.58k
  case TEK_Scalar:
1548
2.58k
    if (LV.isSimple()) {
1549
2.57k
      CGF.EmitScalarInit(E, /*D=*/nullptr, LV, /*Captured=*/false);
1550
11
    } else {
1551
11
      CGF.EmitStoreThroughLValue(RValue::get(CGF.EmitScalarExpr(E)), LV);
1552
11
    }
1553
2.58k
    return;
1554
0
  }
1555
0
  llvm_unreachable("bad evaluation kind");
1556
0
}
1557
1558
81
void AggExprEmitter::EmitNullInitializationToLValue(LValue lv) {
1559
81
  QualType type = lv.getType();
1560
1561
  // If the destination slot is already zeroed out before the aggregate is
1562
  // copied into it, we don't have to emit any zeros here.
1563
81
  if (Dest.isZeroed() && 
CGF.getTypes().isZeroInitializable(type)10
)
1564
10
    return;
1565
1566
71
  if (CGF.hasScalarEvaluationKind(type)) {
1567
    // For non-aggregates, we can store the appropriate null constant.
1568
53
    llvm::Value *null = CGF.CGM.EmitNullConstant(type);
1569
    // Note that the following is not equivalent to
1570
    // EmitStoreThroughBitfieldLValue for ARC types.
1571
53
    if (lv.isBitField()) {
1572
1
      CGF.EmitStoreThroughBitfieldLValue(RValue::get(null), lv);
1573
52
    } else {
1574
52
      assert(lv.isSimple());
1575
52
      CGF.EmitStoreOfScalar(null, lv, /* isInitialization */ true);
1576
52
    }
1577
18
  } else {
1578
    // There's a potential optimization opportunity in combining
1579
    // memsets; that would be easy for arrays, but relatively
1580
    // difficult for structures with the current code.
1581
18
    CGF.EmitNullInitialization(lv.getAddress(CGF), lv.getType());
1582
18
  }
1583
71
}
1584
1585
3.14k
void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
1586
#if 0
1587
  // FIXME: Assess perf here?  Figure out what cases are worth optimizing here
1588
  // (Length of globals? Chunks of zeroed-out space?).
1589
  //
1590
  // If we can, prefer a copy from a global; this is a lot less code for long
1591
  // globals, and it's easier for the current optimizers to analyze.
1592
  if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
1593
    llvm::GlobalVariable* GV =
1594
    new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
1595
                             llvm::GlobalValue::InternalLinkage, C, "");
1596
    EmitFinalDestCopy(E->getType(), CGF.MakeAddrLValue(GV, E->getType()));
1597
    return;
1598
  }
1599
#endif
1600
3.14k
  if (E->hadArrayRangeDesignator())
1601
0
    CGF.ErrorUnsupported(E, "GNU array range designator extension");
1602
1603
3.14k
  if (E->isTransparent())
1604
10
    return Visit(E->getInit(0));
1605
1606
3.13k
  AggValueSlot Dest = EnsureSlot(E->getType());
1607
1608
3.13k
  LValue DestLV = CGF.MakeAddrLValue(Dest.getAddress(), E->getType());
1609
1610
  // Handle initialization of an array.
1611
3.13k
  if (E->getType()->isArrayType()) {
1612
1.66k
    auto AType = cast<llvm::ArrayType>(Dest.getAddress().getElementType());
1613
1.66k
    EmitArrayInit(Dest.getAddress(), AType, E->getType(), E);
1614
1.66k
    return;
1615
1.66k
  }
1616
1617
1.47k
  assert(E->getType()->isRecordType() && "Only support structs/unions here!");
1618
1619
  // Do struct initialization; this code just sets each individual member
1620
  // to the approprate value.  This makes bitfield support automatic;
1621
  // the disadvantage is that the generated code is more difficult for
1622
  // the optimizer, especially with bitfields.
1623
1.47k
  unsigned NumInitElements = E->getNumInits();
1624
1.47k
  RecordDecl *record = E->getType()->castAs<RecordType>()->getDecl();
1625
1626
  // We'll need to enter cleanup scopes in case any of the element
1627
  // initializers throws an exception.
1628
1.47k
  SmallVector<EHScopeStack::stable_iterator, 16> cleanups;
1629
1.47k
  llvm::Instruction *cleanupDominator = nullptr;
1630
16
  auto addCleanup = [&](const EHScopeStack::stable_iterator &cleanup) {
1631
16
    cleanups.push_back(cleanup);
1632
16
    if (!cleanupDominator) // create placeholder once needed
1633
8
      cleanupDominator = CGF.Builder.CreateAlignedLoad(
1634
8
          CGF.Int8Ty, llvm::Constant::getNullValue(CGF.Int8PtrTy),
1635
8
          CharUnits::One());
1636
16
  };
1637
1638
1.47k
  unsigned curInitIndex = 0;
1639
1640
  // Emit initialization of base classes.
1641
1.47k
  if (auto *CXXRD = dyn_cast<CXXRecordDecl>(record)) {
1642
895
    assert(E->getNumInits() >= CXXRD->getNumBases() &&
1643
895
           "missing initializer for base class");
1644
14
    for (auto &Base : CXXRD->bases()) {
1645
14
      assert(!Base.isVirtual() && "should not see vbases here");
1646
14
      auto *BaseRD = Base.getType()->getAsCXXRecordDecl();
1647
14
      Address V = CGF.GetAddressOfDirectBaseInCompleteClass(
1648
14
          Dest.getAddress(), CXXRD, BaseRD,
1649
14
          /*isBaseVirtual*/ false);
1650
14
      AggValueSlot AggSlot = AggValueSlot::forAddr(
1651
14
          V, Qualifiers(),
1652
14
          AggValueSlot::IsDestructed,
1653
14
          AggValueSlot::DoesNotNeedGCBarriers,
1654
14
          AggValueSlot::IsNotAliased,
1655
14
          CGF.getOverlapForBaseInit(CXXRD, BaseRD, Base.isVirtual()));
1656
14
      CGF.EmitAggExpr(E->getInit(curInitIndex++), AggSlot);
1657
1658
14
      if (QualType::DestructionKind dtorKind =
1659
8
              Base.getType().isDestructedType()) {
1660
8
        CGF.pushDestroy(dtorKind, V, Base.getType());
1661
8
        addCleanup(CGF.EHStack.stable_begin());
1662
8
      }
1663
14
    }
1664
895
  }
1665
1666
  // Prepare a 'this' for CXXDefaultInitExprs.
1667
1.47k
  CodeGenFunction::FieldConstructionScope FCS(CGF, Dest.getAddress());
1668
1669
1.47k
  if (record->isUnion()) {
1670
    // Only initialize one field of a union. The field itself is
1671
    // specified by the initializer list.
1672
47
    if (!E->getInitializedFieldInUnion()) {
1673
      // Empty union; we have nothing to do.
1674
1675
12
#ifndef NDEBUG
1676
      // Make sure that it's really an empty and not a failure of
1677
      // semantic analysis.
1678
12
      for (const auto *Field : record->fields())
1679
12
        assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
1680
12
#endif
1681
12
      return;
1682
12
    }
1683
1684
    // FIXME: volatility
1685
35
    FieldDecl *Field = E->getInitializedFieldInUnion();
1686
1687
35
    LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestLV, Field);
1688
35
    if (NumInitElements) {
1689
      // Store the initializer into the field
1690
35
      EmitInitializationToLValue(E->getInit(0), FieldLoc);
1691
0
    } else {
1692
      // Default-initialize to null.
1693
0
      EmitNullInitializationToLValue(FieldLoc);
1694
0
    }
1695
1696
35
    return;
1697
35
  }
1698
1699
  // Here we iterate over the fields; this makes it simpler to both
1700
  // default-initialize fields and skip over unnamed fields.
1701
2.38k
  
for (const auto *field : record->fields())1.42k
{
1702
    // We're done once we hit the flexible array member.
1703
2.38k
    if (field->getType()->isIncompleteArrayType())
1704
0
      break;
1705
1706
    // Always skip anonymous bitfields.
1707
2.38k
    if (field->isUnnamedBitfield())
1708
24
      continue;
1709
1710
    // We're done if we reach the end of the explicit initializers, we
1711
    // have a zeroed object, and the rest of the fields are
1712
    // zero-initializable.
1713
2.36k
    if (curInitIndex == NumInitElements && 
Dest.isZeroed()0
&&
1714
0
        CGF.getTypes().isZeroInitializable(E->getType()))
1715
0
      break;
1716
1717
1718
2.36k
    LValue LV = CGF.EmitLValueForFieldInitialization(DestLV, field);
1719
    // We never generate write-barries for initialized fields.
1720
2.36k
    LV.setNonGC(true);
1721
1722
2.36k
    if (curInitIndex < NumInitElements) {
1723
      // Store the initializer into the field.
1724
2.36k
      EmitInitializationToLValue(E->getInit(curInitIndex++), LV);
1725
0
    } else {
1726
      // We're out of initializers; default-initialize to null
1727
0
      EmitNullInitializationToLValue(LV);
1728
0
    }
1729
1730
    // Push a destructor if necessary.
1731
    // FIXME: if we have an array of structures, all explicitly
1732
    // initialized, we can end up pushing a linear number of cleanups.
1733
2.36k
    bool pushedCleanup = false;
1734
2.36k
    if (QualType::DestructionKind dtorKind
1735
62
          = field->getType().isDestructedType()) {
1736
62
      assert(LV.isSimple());
1737
62
      if (CGF.needsEHCleanup(dtorKind)) {
1738
8
        CGF.pushDestroy(EHCleanup, LV.getAddress(CGF), field->getType(),
1739
8
                        CGF.getDestroyer(dtorKind), false);
1740
8
        addCleanup(CGF.EHStack.stable_begin());
1741
8
        pushedCleanup = true;
1742
8
      }
1743
62
    }
1744
1745
    // If the GEP didn't get used because of a dead zero init or something
1746
    // else, clean it up for -O0 builds and general tidiness.
1747
2.36k
    if (!pushedCleanup && 
LV.isSimple()2.35k
)
1748
2.34k
      if (llvm::GetElementPtrInst *GEP =
1749
2.25k
              dyn_cast<llvm::GetElementPtrInst>(LV.getPointer(CGF)))
1750
2.25k
        if (GEP->use_empty())
1751
137
          GEP->eraseFromParent();
1752
2.36k
  }
1753
1754
  // Deactivate all the partial cleanups in reverse order, which
1755
  // generally means popping them.
1756
1.42k
  assert((cleanupDominator || cleanups.empty()) &&
1757
1.42k
         "Missing cleanupDominator before deactivating cleanup blocks");
1758
1.44k
  for (unsigned i = cleanups.size(); i != 0; 
--i16
)
1759
16
    CGF.DeactivateCleanupBlock(cleanups[i-1], cleanupDominator);
1760
1761
  // Destroy the placeholder if we made one.
1762
1.42k
  if (cleanupDominator)
1763
8
    cleanupDominator->eraseFromParent();
1764
1.42k
}
1765
1766
void AggExprEmitter::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E,
1767
28
                                            llvm::Value *outerBegin) {
1768
  // Emit the common subexpression.
1769
28
  CodeGenFunction::OpaqueValueMapping binding(CGF, E->getCommonExpr());
1770
1771
28
  Address destPtr = EnsureSlot(E->getType()).getAddress();
1772
28
  uint64_t numElements = E->getArraySize().getZExtValue();
1773
1774
28
  if (!numElements)
1775
0
    return;
1776
1777
  // destPtr is an array*. Construct an elementType* by drilling down a level.
1778
28
  llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
1779
28
  llvm::Value *indices[] = {zero, zero};
1780
28
  llvm::Value *begin = Builder.CreateInBoundsGEP(destPtr.getPointer(), indices,
1781
28
                                                 "arrayinit.begin");
1782
1783
  // Prepare to special-case multidimensional array initialization: we avoid
1784
  // emitting multiple destructor loops in that case.
1785
28
  if (!outerBegin)
1786
21
    outerBegin = begin;
1787
28
  ArrayInitLoopExpr *InnerLoop = dyn_cast<ArrayInitLoopExpr>(E->getSubExpr());
1788
1789
28
  QualType elementType =
1790
28
      CGF.getContext().getAsArrayType(E->getType())->getElementType();
1791
28
  CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType);
1792
28
  CharUnits elementAlign =
1793
28
      destPtr.getAlignment().alignmentOfArrayElement(elementSize);
1794
1795
28
  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1796
28
  llvm::BasicBlock *bodyBB = CGF.createBasicBlock("arrayinit.body");
1797
1798
  // Jump into the body.
1799
28
  CGF.EmitBlock(bodyBB);
1800
28
  llvm::PHINode *index =
1801
28
      Builder.CreatePHI(zero->getType(), 2, "arrayinit.index");
1802
28
  index->addIncoming(zero, entryBB);
1803
28
  llvm::Value *element = Builder.CreateInBoundsGEP(begin, index);
1804
1805
  // Prepare for a cleanup.
1806
28
  QualType::DestructionKind dtorKind = elementType.isDestructedType();
1807
28
  EHScopeStack::stable_iterator cleanup;
1808
28
  if (CGF.needsEHCleanup(dtorKind) && 
!InnerLoop6
) {
1809
5
    if (outerBegin->getType() != element->getType())
1810
1
      outerBegin = Builder.CreateBitCast(outerBegin, element->getType());
1811
5
    CGF.pushRegularPartialArrayCleanup(outerBegin, element, elementType,
1812
5
                                       elementAlign,
1813
5
                                       CGF.getDestroyer(dtorKind));
1814
5
    cleanup = CGF.EHStack.stable_begin();
1815
23
  } else {
1816
23
    dtorKind = QualType::DK_none;
1817
23
  }
1818
1819
  // Emit the actual filler expression.
1820
28
  {
1821
    // Temporaries created in an array initialization loop are destroyed
1822
    // at the end of each iteration.
1823
28
    CodeGenFunction::RunCleanupsScope CleanupsScope(CGF);
1824
28
    CodeGenFunction::ArrayInitLoopExprScope Scope(CGF, index);
1825
28
    LValue elementLV =
1826
28
        CGF.MakeAddrLValue(Address(element, elementAlign), elementType);
1827
1828
28
    if (InnerLoop) {
1829
      // If the subexpression is an ArrayInitLoopExpr, share its cleanup.
1830
7
      auto elementSlot = AggValueSlot::forLValue(
1831
7
          elementLV, CGF, AggValueSlot::IsDestructed,
1832
7
          AggValueSlot::DoesNotNeedGCBarriers, AggValueSlot::IsNotAliased,
1833
7
          AggValueSlot::DoesNotOverlap);
1834
7
      AggExprEmitter(CGF, elementSlot, false)
1835
7
          .VisitArrayInitLoopExpr(InnerLoop, outerBegin);
1836
7
    } else
1837
21
      EmitInitializationToLValue(E->getSubExpr(), elementLV);
1838
28
  }
1839
1840
  // Move on to the next element.
1841
28
  llvm::Value *nextIndex = Builder.CreateNUWAdd(
1842
28
      index, llvm::ConstantInt::get(CGF.SizeTy, 1), "arrayinit.next");
1843
28
  index->addIncoming(nextIndex, Builder.GetInsertBlock());
1844
1845
  // Leave the loop if we're done.
1846
28
  llvm::Value *done = Builder.CreateICmpEQ(
1847
28
      nextIndex, llvm::ConstantInt::get(CGF.SizeTy, numElements),
1848
28
      "arrayinit.done");
1849
28
  llvm::BasicBlock *endBB = CGF.createBasicBlock("arrayinit.end");
1850
28
  Builder.CreateCondBr(done, endBB, bodyBB);
1851
1852
28
  CGF.EmitBlock(endBB);
1853
1854
  // Leave the partial-array cleanup if we entered one.
1855
28
  if (dtorKind)
1856
5
    CGF.DeactivateCleanupBlock(cleanup, index);
1857
28
}
1858
1859
8
void AggExprEmitter::VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E) {
1860
8
  AggValueSlot Dest = EnsureSlot(E->getType());
1861
1862
8
  LValue DestLV = CGF.MakeAddrLValue(Dest.getAddress(), E->getType());
1863
8
  EmitInitializationToLValue(E->getBase(), DestLV);
1864
8
  VisitInitListExpr(E->getUpdater());
1865
8
}
1866
1867
//===----------------------------------------------------------------------===//
1868
//                        Entry Points into this File
1869
//===----------------------------------------------------------------------===//
1870
1871
/// GetNumNonZeroBytesInInit - Get an approximate count of the number of
1872
/// non-zero bytes that will be stored when outputting the initializer for the
1873
/// specified initializer expression.
1874
4.56k
static CharUnits GetNumNonZeroBytesInInit(const Expr *E, CodeGenFunction &CGF) {
1875
4.56k
  if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
1876
71
    E = MTE->getSubExpr();
1877
4.56k
  E = E->IgnoreParenNoopCasts(CGF.getContext());
1878
1879
  // 0 and 0.0 won't require any non-zero stores!
1880
4.56k
  if (isSimpleZero(E, CGF)) 
return CharUnits::Zero()180
;
1881
1882
  // If this is an initlist expr, sum up the size of sizes of the (present)
1883
  // elements.  If this is something weird, assume the whole thing is non-zero.
1884
4.38k
  const InitListExpr *ILE = dyn_cast<InitListExpr>(E);
1885
4.39k
  while (ILE && 
ILE->isTransparent()241
)
1886
6
    ILE = dyn_cast<InitListExpr>(ILE->getInit(0));
1887
4.38k
  if (!ILE || 
!CGF.getTypes().isZeroInitializable(ILE->getType())235
)
1888
4.15k
    return CGF.getContext().getTypeSizeInChars(E->getType());
1889
1890
  // InitListExprs for structs have to be handled carefully.  If there are
1891
  // reference members, we need to consider the size of the reference, not the
1892
  // referencee.  InitListExprs for unions and arrays can't have references.
1893
235
  if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
1894
120
    if (!RT->isUnionType()) {
1895
117
      RecordDecl *SD = RT->getDecl();
1896
117
      CharUnits NumNonZeroBytes = CharUnits::Zero();
1897
1898
117
      unsigned ILEElement = 0;
1899
117
      if (auto *CXXRD = dyn_cast<CXXRecordDecl>(SD))
1900
79
        while (ILEElement != CXXRD->getNumBases())
1901
0
          NumNonZeroBytes +=
1902
0
              GetNumNonZeroBytesInInit(ILE->getInit(ILEElement++), CGF);
1903
470
      for (const auto *Field : SD->fields()) {
1904
        // We're done once we hit the flexible array member or run out of
1905
        // InitListExpr elements.
1906
470
        if (Field->getType()->isIncompleteArrayType() ||
1907
470
            ILEElement == ILE->getNumInits())
1908
0
          break;
1909
470
        if (Field->isUnnamedBitfield())
1910
0
          continue;
1911
1912
470
        const Expr *E = ILE->getInit(ILEElement++);
1913
1914
        // Reference values are always non-null and have the width of a pointer.
1915
470
        if (Field->getType()->isReferenceType())
1916
4
          NumNonZeroBytes += CGF.getContext().toCharUnitsFromBits(
1917
4
              CGF.getTarget().getPointerWidth(0));
1918
466
        else
1919
466
          NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
1920
470
      }
1921
1922
117
      return NumNonZeroBytes;
1923
117
    }
1924
118
  }
1925
1926
  // FIXME: This overestimates the number of non-zero bytes for bit-fields.
1927
118
  CharUnits NumNonZeroBytes = CharUnits::Zero();
1928
491
  for (unsigned i = 0, e = ILE->getNumInits(); i != e; 
++i373
)
1929
373
    NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);
1930
118
  return NumNonZeroBytes;
1931
118
}
1932
1933
/// CheckAggExprForMemSetUse - If the initializer is large and has a lot of
1934
/// zeros in it, emit a memset and avoid storing the individual zeros.
1935
///
1936
static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
1937
76.7k
                                     CodeGenFunction &CGF) {
1938
  // If the slot is already known to be zeroed, nothing to do.  Don't mess with
1939
  // volatile stores.
1940
76.7k
  if (Slot.isZeroed() || 
Slot.isVolatile()76.7k
||
!Slot.getAddress().isValid()76.6k
)
1941
1.55k
    return;
1942
1943
  // C++ objects with a user-declared constructor don't need zero'ing.
1944
75.1k
  if (CGF.getLangOpts().CPlusPlus)
1945
69.8k
    if (const RecordType *RT = CGF.getContext()
1946
69.6k
                       .getBaseElementType(E->getType())->getAs<RecordType>()) {
1947
69.6k
      const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1948
69.6k
      if (RD->hasUserDeclaredConstructor())
1949
49.9k
        return;
1950
25.2k
    }
1951
1952
  // If the type is 16-bytes or smaller, prefer individual stores over memset.
1953
25.2k
  CharUnits Size = Slot.getPreferredSize(CGF.getContext(), E->getType());
1954
25.2k
  if (Size <= CharUnits::fromQuantity(16))
1955
21.5k
    return;
1956
1957
  // Check to see if over 3/4 of the initializer are known to be zero.  If so,
1958
  // we prefer to emit memset + individual stores for the rest.
1959
3.72k
  CharUnits NumNonZeroBytes = GetNumNonZeroBytesInInit(E, CGF);
1960
3.72k
  if (NumNonZeroBytes*4 > Size)
1961
3.66k
    return;
1962
1963
  // Okay, it seems like a good idea to use an initial memset, emit the call.
1964
61
  llvm::Constant *SizeVal = CGF.Builder.getInt64(Size.getQuantity());
1965
1966
61
  Address Loc = Slot.getAddress();
1967
61
  Loc = CGF.Builder.CreateElementBitCast(Loc, CGF.Int8Ty);
1968
61
  CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, false);
1969
1970
  // Tell the AggExprEmitter that the slot is known zero.
1971
61
  Slot.setZeroed();
1972
61
}
1973
1974
1975
1976
1977
/// EmitAggExpr - Emit the computation of the specified expression of aggregate
1978
/// type.  The result is computed into DestPtr.  Note that if DestPtr is null,
1979
/// the value of the aggregate expression is not needed.  If VolatileDest is
1980
/// true, DestPtr cannot be 0.
1981
76.7k
void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot) {
1982
76.7k
  assert(E && hasAggregateEvaluationKind(E->getType()) &&
1983
76.7k
         "Invalid aggregate expression to emit");
1984
76.7k
  assert((Slot.getAddress().isValid() || Slot.isIgnored()) &&
1985
76.7k
         "slot has bits but no address");
1986
1987
  // Optimize the slot if possible.
1988
76.7k
  CheckAggExprForMemSetUse(Slot, E, *this);
1989
1990
76.7k
  AggExprEmitter(*this, Slot, Slot.isIgnored()).Visit(const_cast<Expr*>(E));
1991
76.7k
}
1992
1993
85
LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
1994
85
  assert(hasAggregateEvaluationKind(E->getType()) && "Invalid argument!");
1995
85
  Address Temp = CreateMemTemp(E->getType());
1996
85
  LValue LV = MakeAddrLValue(Temp, E->getType());
1997
85
  EmitAggExpr(E, AggValueSlot::forLValue(
1998
85
                     LV, *this, AggValueSlot::IsNotDestructed,
1999
85
                     AggValueSlot::DoesNotNeedGCBarriers,
2000
85
                     AggValueSlot::IsNotAliased, AggValueSlot::DoesNotOverlap));
2001
85
  return LV;
2002
85
}
2003
2004
AggValueSlot::Overlap_t
2005
4.34k
CodeGenFunction::getOverlapForFieldInit(const FieldDecl *FD) {
2006
4.34k
  if (!FD->hasAttr<NoUniqueAddressAttr>() || 
!FD->getType()->isRecordType()3
)
2007
4.34k
    return AggValueSlot::DoesNotOverlap;
2008
2009
  // If the field lies entirely within the enclosing class's nvsize, its tail
2010
  // padding cannot overlap any already-initialized object. (The only subobjects
2011
  // with greater addresses that might already be initialized are vbases.)
2012
3
  const RecordDecl *ClassRD = FD->getParent();
2013
3
  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(ClassRD);
2014
3
  if (Layout.getFieldOffset(FD->getFieldIndex()) +
2015
3
          getContext().getTypeSize(FD->getType()) <=
2016
3
      (uint64_t)getContext().toBits(Layout.getNonVirtualSize()))
2017
1
    return AggValueSlot::DoesNotOverlap;
2018
2019
  // The tail padding may contain values we need to preserve.
2020
2
  return AggValueSlot::MayOverlap;
2021
2
}
2022
2023
AggValueSlot::Overlap_t CodeGenFunction::getOverlapForBaseInit(
2024
9.61k
    const CXXRecordDecl *RD, const CXXRecordDecl *BaseRD, bool IsVirtual) {
2025
  // If the most-derived object is a field declared with [[no_unique_address]],
2026
  // the tail padding of any virtual base could be reused for other subobjects
2027
  // of that field's class.
2028
9.61k
  if (IsVirtual)
2029
781
    return AggValueSlot::MayOverlap;
2030
2031
  // If the base class is laid out entirely within the nvsize of the derived
2032
  // class, its tail padding cannot yet be initialized, so we can issue
2033
  // stores at the full width of the base class.
2034
8.83k
  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2035
8.83k
  if (Layout.getBaseClassOffset(BaseRD) +
2036
8.83k
          getContext().getASTRecordLayout(BaseRD).getSize() <=
2037
8.83k
      Layout.getNonVirtualSize())
2038
8.54k
    return AggValueSlot::DoesNotOverlap;
2039
2040
  // The tail padding may contain values we need to preserve.
2041
293
  return AggValueSlot::MayOverlap;
2042
293
}
2043
2044
void CodeGenFunction::EmitAggregateCopy(LValue Dest, LValue Src, QualType Ty,
2045
                                        AggValueSlot::Overlap_t MayOverlap,
2046
13.0k
                                        bool isVolatile) {
2047
13.0k
  assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
2048
2049
13.0k
  Address DestPtr = Dest.getAddress(*this);
2050
13.0k
  Address SrcPtr = Src.getAddress(*this);
2051
2052
13.0k
  if (getLangOpts().CPlusPlus) {
2053
10.2k
    if (const RecordType *RT = Ty->getAs<RecordType>()) {
2054
8.76k
      CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
2055
8.76k
      assert((Record->hasTrivialCopyConstructor() ||
2056
8.76k
              Record->hasTrivialCopyAssignment() ||
2057
8.76k
              Record->hasTrivialMoveConstructor() ||
2058
8.76k
              Record->hasTrivialMoveAssignment() ||
2059
8.76k
              Record->isUnion()) &&
2060
8.76k
             "Trying to aggregate-copy a type without a trivial copy/move "
2061
8.76k
             "constructor or assignment operator");
2062
      // Ignore empty classes in C++.
2063
8.76k
      if (Record->isEmpty())
2064
2.52k
        return;
2065
10.5k
    }
2066
10.2k
  }
2067
2068
10.5k
  if (getLangOpts().CUDAIsDevice) {
2069
3
    if (Ty->isCUDADeviceBuiltinSurfaceType()) {
2070
1
      if (getTargetHooks().emitCUDADeviceBuiltinSurfaceDeviceCopy(*this, Dest,
2071
1
                                                                  Src))
2072
1
        return;
2073
2
    } else if (Ty->isCUDADeviceBuiltinTextureType()) {
2074
2
      if (getTargetHooks().emitCUDADeviceBuiltinTextureDeviceCopy(*this, Dest,
2075
2
                                                                  Src))
2076
2
        return;
2077
10.5k
    }
2078
3
  }
2079
2080
  // Aggregate assignment turns into llvm.memcpy.  This is almost valid per
2081
  // C99 6.5.16.1p3, which states "If the value being stored in an object is
2082
  // read from another object that overlaps in anyway the storage of the first
2083
  // object, then the overlap shall be exact and the two objects shall have
2084
  // qualified or unqualified versions of a compatible type."
2085
  //
2086
  // memcpy is not defined if the source and destination pointers are exactly
2087
  // equal, but other compilers do this optimization, and almost every memcpy
2088
  // implementation handles this case safely.  If there is a libc that does not
2089
  // safely handle this, we can add a target hook.
2090
2091
  // Get data size info for this aggregate. Don't copy the tail padding if this
2092
  // might be a potentially-overlapping subobject, since the tail padding might
2093
  // be occupied by a different object. Otherwise, copying it is fine.
2094
10.5k
  TypeInfoChars TypeInfo;
2095
10.5k
  if (MayOverlap)
2096
4.71k
    TypeInfo = getContext().getTypeInfoDataSizeInChars(Ty);
2097
5.79k
  else
2098
5.79k
    TypeInfo = getContext().getTypeInfoInChars(Ty);
2099
2100
10.5k
  llvm::Value *SizeVal = nullptr;
2101
10.5k
  if (TypeInfo.Width.isZero()) {
2102
    // But note that getTypeInfo returns 0 for a VLA.
2103
138
    if (auto *VAT = dyn_cast_or_null<VariableArrayType>(
2104
84
            getContext().getAsArrayType(Ty))) {
2105
84
      QualType BaseEltTy;
2106
84
      SizeVal = emitArrayLength(VAT, BaseEltTy, DestPtr);
2107
84
      TypeInfo = getContext().getTypeInfoInChars(BaseEltTy);
2108
84
      assert(!TypeInfo.Width.isZero());
2109
84
      SizeVal = Builder.CreateNUWMul(
2110
84
          SizeVal,
2111
84
          llvm::ConstantInt::get(SizeTy, TypeInfo.Width.getQuantity()));
2112
84
    }
2113
138
  }
2114
10.5k
  if (!SizeVal) {
2115
10.4k
    SizeVal = llvm::ConstantInt::get(SizeTy, TypeInfo.Width.getQuantity());
2116
10.4k
  }
2117
2118
  // FIXME: If we have a volatile struct, the optimizer can remove what might
2119
  // appear to be `extra' memory ops:
2120
  //
2121
  // volatile struct { int i; } a, b;
2122
  //
2123
  // int main() {
2124
  //   a = b;
2125
  //   a = b;
2126
  // }
2127
  //
2128
  // we need to use a different call here.  We use isVolatile to indicate when
2129
  // either the source or the destination is volatile.
2130
2131
10.5k
  DestPtr = Builder.CreateElementBitCast(DestPtr, Int8Ty);
2132
10.5k
  SrcPtr = Builder.CreateElementBitCast(SrcPtr, Int8Ty);
2133
2134
  // Don't do any of the memmove_collectable tests if GC isn't set.
2135
10.5k
  if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
2136
    // fall through
2137
30
  } else if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
2138
28
    RecordDecl *Record = RecordTy->getDecl();
2139
28
    if (Record->hasObjectMember()) {
2140
23
      CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
2141
23
                                                    SizeVal);
2142
23
      return;
2143
23
    }
2144
2
  } else if (Ty->isArrayType()) {
2145
2
    QualType BaseType = getContext().getBaseElementType(Ty);
2146
2
    if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
2147
1
      if (RecordTy->getDecl()->hasObjectMember()) {
2148
1
        CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
2149
1
                                                      SizeVal);
2150
1
        return;
2151
1
      }
2152
10.4k
    }
2153
2
  }
2154
2155
10.4k
  auto Inst = Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, isVolatile);
2156
2157
  // Determine the metadata to describe the position of any padding in this
2158
  // memcpy, as well as the TBAA tags for the members of the struct, in case
2159
  // the optimizer wishes to expand it in to scalar memory operations.
2160
10.4k
  if (llvm::MDNode *TBAAStructTag = CGM.getTBAAStructInfo(Ty))
2161
447
    Inst->setMetadata(llvm::LLVMContext::MD_tbaa_struct, TBAAStructTag);
2162
2163
10.4k
  if (CGM.getCodeGenOpts().NewStructPathTBAA) {
2164
7
    TBAAAccessInfo TBAAInfo = CGM.mergeTBAAInfoForMemoryTransfer(
2165
7
        Dest.getTBAAInfo(), Src.getTBAAInfo());
2166
7
    CGM.DecorateInstructionWithTBAA(Inst, TBAAInfo);
2167
7
  }
2168
10.4k
}