Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/CodeGen/CGDecl.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
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 Decl nodes as LLVM code.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CGBlocks.h"
14
#include "CGCXXABI.h"
15
#include "CGCleanup.h"
16
#include "CGDebugInfo.h"
17
#include "CGOpenCLRuntime.h"
18
#include "CGOpenMPRuntime.h"
19
#include "CodeGenFunction.h"
20
#include "CodeGenModule.h"
21
#include "ConstantEmitter.h"
22
#include "PatternInit.h"
23
#include "TargetInfo.h"
24
#include "clang/AST/ASTContext.h"
25
#include "clang/AST/CharUnits.h"
26
#include "clang/AST/Decl.h"
27
#include "clang/AST/DeclObjC.h"
28
#include "clang/AST/DeclOpenMP.h"
29
#include "clang/Basic/CodeGenOptions.h"
30
#include "clang/Basic/SourceManager.h"
31
#include "clang/Basic/TargetInfo.h"
32
#include "clang/CodeGen/CGFunctionInfo.h"
33
#include "llvm/Analysis/ValueTracking.h"
34
#include "llvm/IR/DataLayout.h"
35
#include "llvm/IR/GlobalVariable.h"
36
#include "llvm/IR/Intrinsics.h"
37
#include "llvm/IR/Type.h"
38
39
using namespace clang;
40
using namespace CodeGen;
41
42
397k
void CodeGenFunction::EmitDecl(const Decl &D) {
43
397k
  switch (D.getKind()) {
44
397k
  case Decl::BuiltinTemplate:
45
0
  case Decl::TranslationUnit:
46
0
  case Decl::ExternCContext:
47
0
  case Decl::Namespace:
48
0
  case Decl::UnresolvedUsingTypename:
49
0
  case Decl::ClassTemplateSpecialization:
50
0
  case Decl::ClassTemplatePartialSpecialization:
51
0
  case Decl::VarTemplateSpecialization:
52
0
  case Decl::VarTemplatePartialSpecialization:
53
0
  case Decl::TemplateTypeParm:
54
0
  case Decl::UnresolvedUsingValue:
55
0
  case Decl::NonTypeTemplateParm:
56
0
  case Decl::CXXDeductionGuide:
57
0
  case Decl::CXXMethod:
58
0
  case Decl::CXXConstructor:
59
0
  case Decl::CXXDestructor:
60
0
  case Decl::CXXConversion:
61
0
  case Decl::Field:
62
0
  case Decl::MSProperty:
63
0
  case Decl::IndirectField:
64
0
  case Decl::ObjCIvar:
65
0
  case Decl::ObjCAtDefsField:
66
0
  case Decl::ParmVar:
67
0
  case Decl::ImplicitParam:
68
0
  case Decl::ClassTemplate:
69
0
  case Decl::VarTemplate:
70
0
  case Decl::FunctionTemplate:
71
0
  case Decl::TypeAliasTemplate:
72
0
  case Decl::TemplateTemplateParm:
73
0
  case Decl::ObjCMethod:
74
0
  case Decl::ObjCCategory:
75
0
  case Decl::ObjCProtocol:
76
0
  case Decl::ObjCInterface:
77
0
  case Decl::ObjCCategoryImpl:
78
0
  case Decl::ObjCImplementation:
79
0
  case Decl::ObjCProperty:
80
0
  case Decl::ObjCCompatibleAlias:
81
0
  case Decl::PragmaComment:
82
0
  case Decl::PragmaDetectMismatch:
83
0
  case Decl::AccessSpec:
84
0
  case Decl::LinkageSpec:
85
0
  case Decl::Export:
86
0
  case Decl::ObjCPropertyImpl:
87
0
  case Decl::FileScopeAsm:
88
0
  case Decl::Friend:
89
0
  case Decl::FriendTemplate:
90
0
  case Decl::Block:
91
0
  case Decl::Captured:
92
0
  case Decl::ClassScopeFunctionSpecialization:
93
0
  case Decl::UsingShadow:
94
0
  case Decl::ConstructorUsingShadow:
95
0
  case Decl::ObjCTypeParam:
96
0
  case Decl::Binding:
97
0
    llvm_unreachable("Declaration should not be in declstmts!");
98
3.57k
  case Decl::Function:  // void X();
99
3.57k
  case Decl::Record:    // struct/union/class X;
100
3.57k
  case Decl::Enum:      // enum X;
101
3.57k
  case Decl::EnumConstant: // enum ? { X = ? }
102
3.57k
  case Decl::CXXRecord: // struct/union/class X; [C++]
103
3.57k
  case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
104
3.57k
  case Decl::Label:        // __label__ x;
105
3.57k
  case Decl::Import:
106
3.57k
  case Decl::OMPThreadPrivate:
107
3.57k
  case Decl::OMPAllocate:
108
3.57k
  case Decl::OMPCapturedExpr:
109
3.57k
  case Decl::OMPRequires:
110
3.57k
  case Decl::Empty:
111
3.57k
  case Decl::Concept:
112
3.57k
    // None of these decls require codegen support.
113
3.57k
    return;
114
3.57k
115
3.57k
  case Decl::NamespaceAlias:
116
17
    if (CGDebugInfo *DI = getDebugInfo())
117
8
        DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
118
17
    return;
119
3.57k
  case Decl::Using:          // using X; [C++]
120
60
    if (CGDebugInfo *DI = getDebugInfo())
121
36
        DI->EmitUsingDecl(cast<UsingDecl>(D));
122
60
    return;
123
3.57k
  case Decl::UsingPack:
124
0
    for (auto *Using : cast<UsingPackDecl>(D).expansions())
125
0
      EmitDecl(*Using);
126
0
    return;
127
3.57k
  case Decl::UsingDirective: // using namespace X; [C++]
128
41
    if (CGDebugInfo *DI = getDebugInfo())
129
33
      DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D));
130
41
    return;
131
390k
  case Decl::Var:
132
390k
  case Decl::Decomposition: {
133
390k
    const VarDecl &VD = cast<VarDecl>(D);
134
390k
    assert(VD.isLocalVarDecl() &&
135
390k
           "Should not see file-scope variables inside a function!");
136
390k
    EmitVarDecl(VD);
137
390k
    if (auto *DD = dyn_cast<DecompositionDecl>(&VD))
138
7
      for (auto *B : DD->bindings())
139
17
        if (auto *HD = B->getHoldingVar())
140
4
          EmitVarDecl(*HD);
141
390k
    return;
142
390k
  }
143
390k
144
390k
  case Decl::OMPDeclareReduction:
145
56
    return CGM.EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(&D), this);
146
390k
147
390k
  case Decl::OMPDeclareMapper:
148
0
    return CGM.EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(&D), this);
149
390k
150
390k
  case Decl::Typedef:      // typedef int X;
151
3.81k
  case Decl::TypeAlias: {  // using X = int; [C++0x]
152
3.81k
    const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
153
3.81k
    QualType Ty = TD.getUnderlyingType();
154
3.81k
155
3.81k
    if (Ty->isVariablyModifiedType())
156
17
      EmitVariablyModifiedType(Ty);
157
3.81k
158
3.81k
    return;
159
3.81k
  }
160
397k
  }
161
397k
}
162
163
/// EmitVarDecl - This method handles emission of any variable declaration
164
/// inside a function, including static vars etc.
165
433k
void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
166
433k
  if (D.hasExternalStorage())
167
175
    // Don't emit it now, allow it to be emitted lazily on its first use.
168
175
    return;
169
433k
170
433k
  // Some function-scope variable does not have static storage but still
171
433k
  // needs to be emitted like a static variable, e.g. a function-scope
172
433k
  // variable in constant address space in OpenCL.
173
433k
  if (D.getStorageDuration() != SD_Automatic) {
174
5.16k
    // Static sampler variables translated to function calls.
175
5.16k
    if (D.getType()->isSamplerT())
176
3
      return;
177
5.16k
178
5.16k
    llvm::GlobalValue::LinkageTypes Linkage =
179
5.16k
        CGM.getLLVMLinkageVarDefinition(&D, /*IsConstant=*/false);
180
5.16k
181
5.16k
    // FIXME: We need to force the emission/use of a guard variable for
182
5.16k
    // some variables even if we can constant-evaluate them because
183
5.16k
    // we can't guarantee every translation unit will constant-evaluate them.
184
5.16k
185
5.16k
    return EmitStaticVarDecl(D, Linkage);
186
5.16k
  }
187
427k
188
427k
  if (D.getType().getAddressSpace() == LangAS::opencl_local)
189
122
    return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D);
190
427k
191
427k
  assert(D.hasLocalStorage());
192
427k
  return EmitAutoVarDecl(D);
193
427k
}
194
195
5.16k
static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D) {
196
5.16k
  if (CGM.getLangOpts().CPlusPlus)
197
3.24k
    return CGM.getMangledName(&D).str();
198
1.92k
199
1.92k
  // If this isn't C++, we don't need a mangled name, just a pretty one.
200
1.92k
  assert(!D.isExternallyVisible() && "name shouldn't matter");
201
1.92k
  std::string ContextName;
202
1.92k
  const DeclContext *DC = D.getDeclContext();
203
1.92k
  if (auto *CD = dyn_cast<CapturedDecl>(DC))
204
1
    DC = cast<DeclContext>(CD->getNonClosureContext());
205
1.92k
  if (const auto *FD = dyn_cast<FunctionDecl>(DC))
206
1.91k
    ContextName = CGM.getMangledName(FD);
207
8
  else if (const auto *BD = dyn_cast<BlockDecl>(DC))
208
5
    ContextName = CGM.getBlockMangledName(GlobalDecl(), BD);
209
3
  else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(DC))
210
3
    ContextName = OMD->getSelector().getAsString();
211
3
  else
212
3
    
llvm_unreachable0
("Unknown context for static var decl");
213
1.92k
214
1.92k
  ContextName += "." + D.getNameAsString();
215
1.92k
  return ContextName;
216
1.92k
}
217
218
llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl(
219
5.88k
    const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) {
220
5.88k
  // In general, we don't always emit static var decls once before we reference
221
5.88k
  // them. It is possible to reference them before emitting the function that
222
5.88k
  // contains them, and it is possible to emit the containing function multiple
223
5.88k
  // times.
224
5.88k
  if (llvm::Constant *ExistingGV = StaticLocalDeclMap[&D])
225
595
    return ExistingGV;
226
5.28k
227
5.28k
  QualType Ty = D.getType();
228
5.28k
  assert(Ty->isConstantSizeType() && "VLAs can't be static");
229
5.28k
230
5.28k
  // Use the label if the variable is renamed with the asm-label extension.
231
5.28k
  std::string Name;
232
5.28k
  if (D.hasAttr<AsmLabelAttr>())
233
124
    Name = getMangledName(&D);
234
5.16k
  else
235
5.16k
    Name = getStaticDeclName(*this, D);
236
5.28k
237
5.28k
  llvm::Type *LTy = getTypes().ConvertTypeForMem(Ty);
238
5.28k
  LangAS AS = GetGlobalVarAddressSpace(&D);
239
5.28k
  unsigned TargetAS = getContext().getTargetAddressSpace(AS);
240
5.28k
241
5.28k
  // OpenCL variables in local address space and CUDA shared
242
5.28k
  // variables cannot have an initializer.
243
5.28k
  llvm::Constant *Init = nullptr;
244
5.28k
  if (Ty.getAddressSpace() == LangAS::opencl_local ||
245
5.28k
      
D.hasAttr<CUDASharedAttr>()5.16k
)
246
140
    Init = llvm::UndefValue::get(LTy);
247
5.14k
  else
248
5.14k
    Init = EmitNullConstant(Ty);
249
5.28k
250
5.28k
  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
251
5.28k
      getModule(), LTy, Ty.isConstant(getContext()), Linkage, Init, Name,
252
5.28k
      nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
253
5.28k
  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
254
5.28k
255
5.28k
  if (supportsCOMDAT() && 
GV->isWeakForLinker()2.22k
)
256
231
    GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
257
5.28k
258
5.28k
  if (D.getTLSKind())
259
111
    setTLSMode(GV, D);
260
5.28k
261
5.28k
  setGVProperties(GV, &D);
262
5.28k
263
5.28k
  // Make sure the result is of the correct type.
264
5.28k
  LangAS ExpectedAS = Ty.getAddressSpace();
265
5.28k
  llvm::Constant *Addr = GV;
266
5.28k
  if (AS != ExpectedAS) {
267
24
    Addr = getTargetCodeGenInfo().performAddrSpaceCast(
268
24
        *this, GV, AS, ExpectedAS,
269
24
        LTy->getPointerTo(getContext().getTargetAddressSpace(ExpectedAS)));
270
24
  }
271
5.28k
272
5.28k
  setStaticLocalDeclAddress(&D, Addr);
273
5.28k
274
5.28k
  // Ensure that the static local gets initialized by making sure the parent
275
5.28k
  // function gets emitted eventually.
276
5.28k
  const Decl *DC = cast<Decl>(D.getDeclContext());
277
5.28k
278
5.28k
  // We can't name blocks or captured statements directly, so try to emit their
279
5.28k
  // parents.
280
5.28k
  if (isa<BlockDecl>(DC) || 
isa<CapturedDecl>(DC)5.24k
) {
281
275
    DC = DC->getNonClosureContext();
282
275
    // FIXME: Ensure that global blocks get emitted.
283
275
    if (!DC)
284
21
      return Addr;
285
5.26k
  }
286
5.26k
287
5.26k
  GlobalDecl GD;
288
5.26k
  if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
289
27
    GD = GlobalDecl(CD, Ctor_Base);
290
5.24k
  else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
291
4
    GD = GlobalDecl(DD, Dtor_Base);
292
5.23k
  else if (const auto *FD = dyn_cast<FunctionDecl>(DC))
293
5.22k
    GD = GlobalDecl(FD);
294
9
  else {
295
9
    // Don't do anything for Obj-C method decls or global closures. We should
296
9
    // never defer them.
297
9
    assert(isa<ObjCMethodDecl>(DC) && "unexpected parent code decl");
298
9
  }
299
5.26k
  if (GD.getDecl()) {
300
5.25k
    // Disable emission of the parent function for the OpenMP device codegen.
301
5.25k
    CGOpenMPRuntime::DisableAutoDeclareTargetRAII NoDeclTarget(*this);
302
5.25k
    (void)GetAddrOfGlobal(GD);
303
5.25k
  }
304
5.26k
305
5.26k
  return Addr;
306
5.26k
}
307
308
/// hasNontrivialDestruction - Determine whether a type's destruction is
309
/// non-trivial. If so, and the variable uses static initialization, we must
310
/// register its destructor to run on exit.
311
2.02k
static bool hasNontrivialDestruction(QualType T) {
312
2.02k
  CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
313
2.02k
  return RD && 
!RD->hasTrivialDestructor()276
;
314
2.02k
}
315
316
/// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
317
/// global variable that has already been created for it.  If the initializer
318
/// has a different type than GV does, this may free GV and return a different
319
/// one.  Otherwise it just returns GV.
320
llvm::GlobalVariable *
321
CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
322
2.48k
                                               llvm::GlobalVariable *GV) {
323
2.48k
  ConstantEmitter emitter(*this);
324
2.48k
  llvm::Constant *Init = emitter.tryEmitForInitializer(D);
325
2.48k
326
2.48k
  // If constant emission failed, then this should be a C++ static
327
2.48k
  // initializer.
328
2.48k
  if (!Init) {
329
463
    if (!getLangOpts().CPlusPlus)
330
0
      CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
331
463
    else if (HaveInsertPoint()) {
332
461
      // Since we have a static initializer, this global variable can't
333
461
      // be constant.
334
461
      GV->setConstant(false);
335
461
336
461
      EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
337
461
    }
338
463
    return GV;
339
463
  }
340
2.02k
341
2.02k
  // The initializer may differ in type from the global. Rewrite
342
2.02k
  // the global to match the initializer.  (We have to do this
343
2.02k
  // because some types, like unions, can't be completely represented
344
2.02k
  // in the LLVM type system.)
345
2.02k
  if (GV->getType()->getElementType() != Init->getType()) {
346
36
    llvm::GlobalVariable *OldGV = GV;
347
36
348
36
    GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
349
36
                                  OldGV->isConstant(),
350
36
                                  OldGV->getLinkage(), Init, "",
351
36
                                  /*InsertBefore*/ OldGV,
352
36
                                  OldGV->getThreadLocalMode(),
353
36
                           CGM.getContext().getTargetAddressSpace(D.getType()));
354
36
    GV->setVisibility(OldGV->getVisibility());
355
36
    GV->setDSOLocal(OldGV->isDSOLocal());
356
36
    GV->setComdat(OldGV->getComdat());
357
36
358
36
    // Steal the name of the old global
359
36
    GV->takeName(OldGV);
360
36
361
36
    // Replace all uses of the old global with the new global
362
36
    llvm::Constant *NewPtrForOldDecl =
363
36
    llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
364
36
    OldGV->replaceAllUsesWith(NewPtrForOldDecl);
365
36
366
36
    // Erase the old global, since it is no longer used.
367
36
    OldGV->eraseFromParent();
368
36
  }
369
2.02k
370
2.02k
  GV->setConstant(CGM.isTypeConstant(D.getType(), true));
371
2.02k
  GV->setInitializer(Init);
372
2.02k
373
2.02k
  emitter.finalize(GV);
374
2.02k
375
2.02k
  if (hasNontrivialDestruction(D.getType()) && 
HaveInsertPoint()45
) {
376
44
    // We have a constant initializer, but a nontrivial destructor. We still
377
44
    // need to perform a guarded "initialization" in order to register the
378
44
    // destructor.
379
44
    EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
380
44
  }
381
2.02k
382
2.02k
  return GV;
383
2.02k
}
384
385
void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
386
5.29k
                                      llvm::GlobalValue::LinkageTypes Linkage) {
387
5.29k
  // Check to see if we already have a global variable for this
388
5.29k
  // declaration.  This can happen when double-emitting function
389
5.29k
  // bodies, e.g. with complete and base constructors.
390
5.29k
  llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage);
391
5.29k
  CharUnits alignment = getContext().getDeclAlign(&D);
392
5.29k
393
5.29k
  // Store into LocalDeclMap before generating initializer to handle
394
5.29k
  // circular references.
395
5.29k
  setAddrOfLocalVar(&D, Address(addr, alignment));
396
5.29k
397
5.29k
  // We can't have a VLA here, but we can have a pointer to a VLA,
398
5.29k
  // even though that doesn't really make any sense.
399
5.29k
  // Make sure to evaluate VLA bounds now so that we have them for later.
400
5.29k
  if (D.getType()->isVariablyModifiedType())
401
1
    EmitVariablyModifiedType(D.getType());
402
5.29k
403
5.29k
  // Save the type in case adding the initializer forces a type change.
404
5.29k
  llvm::Type *expectedType = addr->getType();
405
5.29k
406
5.29k
  llvm::GlobalVariable *var =
407
5.29k
    cast<llvm::GlobalVariable>(addr->stripPointerCasts());
408
5.29k
409
5.29k
  // CUDA's local and local static __shared__ variables should not
410
5.29k
  // have any non-empty initializers. This is ensured by Sema.
411
5.29k
  // Whatever initializer such variable may have when it gets here is
412
5.29k
  // a no-op and should not be emitted.
413
5.29k
  bool isCudaSharedVar = getLangOpts().CUDA && 
getLangOpts().CUDAIsDevice22
&&
414
5.29k
                         
D.hasAttr<CUDASharedAttr>()22
;
415
5.29k
  // If this value has an initializer, emit it.
416
5.29k
  if (D.getInit() && 
!isCudaSharedVar2.49k
)
417
2.48k
    var = AddInitializerToStaticVarDecl(D, var);
418
5.29k
419
5.29k
  var->setAlignment(alignment.getQuantity());
420
5.29k
421
5.29k
  if (D.hasAttr<AnnotateAttr>())
422
3
    CGM.AddGlobalAnnotations(&D, var);
423
5.29k
424
5.29k
  if (auto *SA = D.getAttr<PragmaClangBSSSectionAttr>())
425
1
    var->addAttribute("bss-section", SA->getName());
426
5.29k
  if (auto *SA = D.getAttr<PragmaClangDataSectionAttr>())
427
0
    var->addAttribute("data-section", SA->getName());
428
5.29k
  if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>())
429
1
    var->addAttribute("rodata-section", SA->getName());
430
5.29k
431
5.29k
  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
432
121
    var->setSection(SA->getName());
433
5.29k
434
5.29k
  if (D.hasAttr<UsedAttr>())
435
1
    CGM.addUsedGlobal(var);
436
5.29k
437
5.29k
  // We may have to cast the constant because of the initializer
438
5.29k
  // mismatch above.
439
5.29k
  //
440
5.29k
  // FIXME: It is really dangerous to store this in the map; if anyone
441
5.29k
  // RAUW's the GV uses of this constant will be invalid.
442
5.29k
  llvm::Constant *castedAddr =
443
5.29k
    llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType);
444
5.29k
  if (var != castedAddr)
445
62
    LocalDeclMap.find(&D)->second = Address(castedAddr, alignment);
446
5.29k
  CGM.setStaticLocalDeclAddress(&D, castedAddr);
447
5.29k
448
5.29k
  CGM.getSanitizerMetadata()->reportGlobalToASan(var, D);
449
5.29k
450
5.29k
  // Emit global variable debug descriptor for static vars.
451
5.29k
  CGDebugInfo *DI = getDebugInfo();
452
5.29k
  if (DI &&
453
5.29k
      
CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo817
) {
454
68
    DI->setLocation(D.getLocation());
455
68
    DI->EmitGlobalVariable(var, &D);
456
68
  }
457
5.29k
}
458
459
namespace {
460
  struct DestroyObject final : EHScopeStack::Cleanup {
461
    DestroyObject(Address addr, QualType type,
462
                  CodeGenFunction::Destroyer *destroyer,
463
                  bool useEHCleanupForArray)
464
      : addr(addr), type(type), destroyer(destroyer),
465
29.0k
        useEHCleanupForArray(useEHCleanupForArray) {}
466
467
    Address addr;
468
    QualType type;
469
    CodeGenFunction::Destroyer *destroyer;
470
    bool useEHCleanupForArray;
471
472
33.4k
    void Emit(CodeGenFunction &CGF, Flags flags) override {
473
33.4k
      // Don't use an EH cleanup recursively from an EH cleanup.
474
33.4k
      bool useEHCleanupForArray =
475
33.4k
        flags.isForNormalCleanup() && 
this->useEHCleanupForArray26.1k
;
476
33.4k
477
33.4k
      CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
478
33.4k
    }
479
  };
480
481
  template <class Derived>
482
  struct DestroyNRVOVariable : EHScopeStack::Cleanup {
483
    DestroyNRVOVariable(Address addr, QualType type, llvm::Value *NRVOFlag)
484
377
        : NRVOFlag(NRVOFlag), Loc(addr), Ty(type) {}
CGDecl.cpp:(anonymous namespace)::DestroyNRVOVariable<(anonymous namespace)::DestroyNRVOVariableCXX>::DestroyNRVOVariable(clang::CodeGen::Address, clang::QualType, llvm::Value*)
Line
Count
Source
484
374
        : NRVOFlag(NRVOFlag), Loc(addr), Ty(type) {}
CGDecl.cpp:(anonymous namespace)::DestroyNRVOVariable<(anonymous namespace)::DestroyNRVOVariableC>::DestroyNRVOVariable(clang::CodeGen::Address, clang::QualType, llvm::Value*)
Line
Count
Source
484
3
        : NRVOFlag(NRVOFlag), Loc(addr), Ty(type) {}
485
486
    llvm::Value *NRVOFlag;
487
    Address Loc;
488
    QualType Ty;
489
490
546
    void Emit(CodeGenFunction &CGF, Flags flags) override {
491
546
      // Along the exceptions path we always execute the dtor.
492
546
      bool NRVO = flags.isForNormalCleanup() && 
NRVOFlag377
;
493
546
494
546
      llvm::BasicBlock *SkipDtorBB = nullptr;
495
546
      if (NRVO) {
496
377
        // If we exited via NRVO, we skip the destructor call.
497
377
        llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
498
377
        SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
499
377
        llvm::Value *DidNRVO =
500
377
          CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
501
377
        CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
502
377
        CGF.EmitBlock(RunDtorBB);
503
377
      }
504
546
505
546
      static_cast<Derived *>(this)->emitDestructorCall(CGF);
506
546
507
546
      if (NRVO) 
CGF.EmitBlock(SkipDtorBB)377
;
508
546
    }
CGDecl.cpp:(anonymous namespace)::DestroyNRVOVariable<(anonymous namespace)::DestroyNRVOVariableCXX>::Emit(clang::CodeGen::CodeGenFunction&, clang::CodeGen::EHScopeStack::Cleanup::Flags)
Line
Count
Source
490
543
    void Emit(CodeGenFunction &CGF, Flags flags) override {
491
543
      // Along the exceptions path we always execute the dtor.
492
543
      bool NRVO = flags.isForNormalCleanup() && 
NRVOFlag374
;
493
543
494
543
      llvm::BasicBlock *SkipDtorBB = nullptr;
495
543
      if (NRVO) {
496
374
        // If we exited via NRVO, we skip the destructor call.
497
374
        llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
498
374
        SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
499
374
        llvm::Value *DidNRVO =
500
374
          CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
501
374
        CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
502
374
        CGF.EmitBlock(RunDtorBB);
503
374
      }
504
543
505
543
      static_cast<Derived *>(this)->emitDestructorCall(CGF);
506
543
507
543
      if (NRVO) 
CGF.EmitBlock(SkipDtorBB)374
;
508
543
    }
CGDecl.cpp:(anonymous namespace)::DestroyNRVOVariable<(anonymous namespace)::DestroyNRVOVariableC>::Emit(clang::CodeGen::CodeGenFunction&, clang::CodeGen::EHScopeStack::Cleanup::Flags)
Line
Count
Source
490
3
    void Emit(CodeGenFunction &CGF, Flags flags) override {
491
3
      // Along the exceptions path we always execute the dtor.
492
3
      bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
493
3
494
3
      llvm::BasicBlock *SkipDtorBB = nullptr;
495
3
      if (NRVO) {
496
3
        // If we exited via NRVO, we skip the destructor call.
497
3
        llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
498
3
        SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
499
3
        llvm::Value *DidNRVO =
500
3
          CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
501
3
        CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
502
3
        CGF.EmitBlock(RunDtorBB);
503
3
      }
504
3
505
3
      static_cast<Derived *>(this)->emitDestructorCall(CGF);
506
3
507
3
      if (NRVO) CGF.EmitBlock(SkipDtorBB);
508
3
    }
509
510
0
    virtual ~DestroyNRVOVariable() = default;
Unexecuted instantiation: CGDecl.cpp:(anonymous namespace)::DestroyNRVOVariable<(anonymous namespace)::DestroyNRVOVariableCXX>::~DestroyNRVOVariable()
Unexecuted instantiation: CGDecl.cpp:(anonymous namespace)::DestroyNRVOVariable<(anonymous namespace)::DestroyNRVOVariableC>::~DestroyNRVOVariable()
511
  };
512
513
  struct DestroyNRVOVariableCXX final
514
      : DestroyNRVOVariable<DestroyNRVOVariableCXX> {
515
    DestroyNRVOVariableCXX(Address addr, QualType type,
516
                           const CXXDestructorDecl *Dtor, llvm::Value *NRVOFlag)
517
        : DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, type, NRVOFlag),
518
374
          Dtor(Dtor) {}
519
520
    const CXXDestructorDecl *Dtor;
521
522
543
    void emitDestructorCall(CodeGenFunction &CGF) {
523
543
      CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
524
543
                                /*ForVirtualBase=*/false,
525
543
                                /*Delegating=*/false, Loc, Ty);
526
543
    }
527
  };
528
529
  struct DestroyNRVOVariableC final
530
      : DestroyNRVOVariable<DestroyNRVOVariableC> {
531
    DestroyNRVOVariableC(Address addr, llvm::Value *NRVOFlag, QualType Ty)
532
3
        : DestroyNRVOVariable<DestroyNRVOVariableC>(addr, Ty, NRVOFlag) {}
533
534
3
    void emitDestructorCall(CodeGenFunction &CGF) {
535
3
      CGF.destroyNonTrivialCStruct(CGF, Loc, Ty);
536
3
    }
537
  };
538
539
  struct CallStackRestore final : EHScopeStack::Cleanup {
540
    Address Stack;
541
1.24k
    CallStackRestore(Address Stack) : Stack(Stack) {}
542
1.24k
    void Emit(CodeGenFunction &CGF, Flags flags) override {
543
1.24k
      llvm::Value *V = CGF.Builder.CreateLoad(Stack);
544
1.24k
      llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
545
1.24k
      CGF.Builder.CreateCall(F, V);
546
1.24k
    }
547
  };
548
549
  struct ExtendGCLifetime final : EHScopeStack::Cleanup {
550
    const VarDecl &Var;
551
1
    ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
552
553
1
    void Emit(CodeGenFunction &CGF, Flags flags) override {
554
1
      // Compute the address of the local variable, in case it's a
555
1
      // byref or something.
556
1
      DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
557
1
                      Var.getType(), VK_LValue, SourceLocation());
558
1
      llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE),
559
1
                                                SourceLocation());
560
1
      CGF.EmitExtendGCLifetime(value);
561
1
    }
562
  };
563
564
  struct CallCleanupFunction final : EHScopeStack::Cleanup {
565
    llvm::Constant *CleanupFn;
566
    const CGFunctionInfo &FnInfo;
567
    const VarDecl &Var;
568
569
    CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
570
                        const VarDecl *Var)
571
9
      : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
572
573
13
    void Emit(CodeGenFunction &CGF, Flags flags) override {
574
13
      DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
575
13
                      Var.getType(), VK_LValue, SourceLocation());
576
13
      // Compute the address of the local variable, in case it's a byref
577
13
      // or something.
578
13
      llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getPointer();
579
13
580
13
      // In some cases, the type of the function argument will be different from
581
13
      // the type of the pointer. An example of this is
582
13
      // void f(void* arg);
583
13
      // __attribute__((cleanup(f))) void *g;
584
13
      //
585
13
      // To fix this we insert a bitcast here.
586
13
      QualType ArgTy = FnInfo.arg_begin()->type;
587
13
      llvm::Value *Arg =
588
13
        CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
589
13
590
13
      CallArgList Args;
591
13
      Args.add(RValue::get(Arg),
592
13
               CGF.getContext().getPointerType(Var.getType()));
593
13
      auto Callee = CGCallee::forDirect(CleanupFn);
594
13
      CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args);
595
13
    }
596
  };
597
} // end anonymous namespace
598
599
/// EmitAutoVarWithLifetime - Does the setup required for an automatic
600
/// variable with lifetime.
601
static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
602
                                    Address addr,
603
466
                                    Qualifiers::ObjCLifetime lifetime) {
604
466
  switch (lifetime) {
605
466
  case Qualifiers::OCL_None:
606
0
    llvm_unreachable("present but none");
607
466
608
466
  case Qualifiers::OCL_ExplicitNone:
609
234
    // nothing to do
610
234
    break;
611
466
612
466
  case Qualifiers::OCL_Strong: {
613
224
    CodeGenFunction::Destroyer *destroyer =
614
224
      (var.hasAttr<ObjCPreciseLifetimeAttr>()
615
224
       ? 
CodeGenFunction::destroyARCStrongPrecise1
616
224
       : 
CodeGenFunction::destroyARCStrongImprecise223
);
617
224
618
224
    CleanupKind cleanupKind = CGF.getARCCleanupKind();
619
224
    CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
620
224
                    cleanupKind & EHCleanup);
621
224
    break;
622
466
  }
623
466
  case Qualifiers::OCL_Autoreleasing:
624
1
    // nothing to do
625
1
    break;
626
466
627
466
  case Qualifiers::OCL_Weak:
628
7
    // __weak objects always get EH cleanups; otherwise, exceptions
629
7
    // could cause really nasty crashes instead of mere leaks.
630
7
    CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
631
7
                    CodeGenFunction::destroyARCWeak,
632
7
                    /*useEHCleanup*/ true);
633
7
    break;
634
466
  }
635
466
}
636
637
1.25k
static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
638
1.25k
  if (const Expr *e = dyn_cast<Expr>(s)) {
639
1.24k
    // Skip the most common kinds of expressions that make
640
1.24k
    // hierarchy-walking expensive.
641
1.24k
    s = e = e->IgnoreParenCasts();
642
1.24k
643
1.24k
    if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
644
516
      return (ref->getDecl() == &var);
645
725
    if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
646
117
      const BlockDecl *block = be->getBlockDecl();
647
366
      for (const auto &I : block->captures()) {
648
366
        if (I.getVariable() == &var)
649
2
          return true;
650
366
      }
651
117
    }
652
725
  }
653
1.25k
654
1.25k
  
for (const Stmt *SubStmt : s->children())739
655
364
    // SubStmt might be null; as in missing decl or conditional of an if-stmt.
656
364
    if (SubStmt && isAccessedBy(var, SubStmt))
657
15
      return true;
658
739
659
739
  
return false724
;
660
739
}
661
662
976
static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
663
976
  if (!decl) 
return false52
;
664
924
  if (!isa<VarDecl>(decl)) 
return false31
;
665
893
  const VarDecl *var = cast<VarDecl>(decl);
666
893
  return isAccessedBy(*var, e);
667
893
}
668
669
static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF,
670
319
                                   const LValue &destLV, const Expr *init) {
671
319
  bool needsCast = false;
672
319
673
330
  while (auto castExpr = dyn_cast<CastExpr>(init->IgnoreParens())) {
674
190
    switch (castExpr->getCastKind()) {
675
190
    // Look through casts that don't require representation changes.
676
190
    case CK_NoOp:
677
11
    case CK_BitCast:
678
11
    case CK_BlockPointerToObjCPointerCast:
679
11
      needsCast = true;
680
11
      break;
681
11
682
11
    // If we find an l-value to r-value cast from a __weak variable,
683
11
    // emit this operation as a copy or move.
684
166
    case CK_LValueToRValue: {
685
166
      const Expr *srcExpr = castExpr->getSubExpr();
686
166
      if (srcExpr->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
687
9
        return false;
688
157
689
157
      // Emit the source l-value.
690
157
      LValue srcLV = CGF.EmitLValue(srcExpr);
691
157
692
157
      // Handle a formal type change to avoid asserting.
693
157
      auto srcAddr = srcLV.getAddress();
694
157
      if (needsCast) {
695
3
        srcAddr = CGF.Builder.CreateElementBitCast(srcAddr,
696
3
                                         destLV.getAddress().getElementType());
697
3
      }
698
157
699
157
      // If it was an l-value, use objc_copyWeak.
700
157
      if (srcExpr->getValueKind() == VK_LValue) {
701
154
        CGF.EmitARCCopyWeak(destLV.getAddress(), srcAddr);
702
154
      } else {
703
3
        assert(srcExpr->getValueKind() == VK_XValue);
704
3
        CGF.EmitARCMoveWeak(destLV.getAddress(), srcAddr);
705
3
      }
706
157
      return true;
707
157
    }
708
157
709
157
    // Stop at anything else.
710
157
    default:
711
13
      return false;
712
11
    }
713
11
714
11
    init = castExpr->getSubExpr();
715
11
  }
716
319
  
return false140
;
717
319
}
718
719
static void drillIntoBlockVariable(CodeGenFunction &CGF,
720
                                   LValue &lvalue,
721
15
                                   const VarDecl *var) {
722
15
  lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(), var));
723
15
}
724
725
void CodeGenFunction::EmitNullabilityCheck(LValue LHS, llvm::Value *RHS,
726
703k
                                           SourceLocation Loc) {
727
703k
  if (!SanOpts.has(SanitizerKind::NullabilityAssign))
728
703k
    return;
729
2
730
2
  auto Nullability = LHS.getType()->getNullability(getContext());
731
2
  if (!Nullability || *Nullability != NullabilityKind::NonNull)
732
0
    return;
733
2
734
2
  // Check if the right hand side of the assignment is nonnull, if the left
735
2
  // hand side must be nonnull.
736
2
  SanitizerScope SanScope(this);
737
2
  llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
738
2
  llvm::Constant *StaticData[] = {
739
2
      EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(LHS.getType()),
740
2
      llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused.
741
2
      llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)};
742
2
  EmitCheck({{IsNotNull, SanitizerKind::NullabilityAssign}},
743
2
            SanitizerHandler::TypeMismatch, StaticData, RHS);
744
2
}
745
746
void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,
747
283k
                                     LValue lvalue, bool capturedByInit) {
748
283k
  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
749
283k
  if (!lifetime) {
750
282k
    llvm::Value *value = EmitScalarExpr(init);
751
282k
    if (capturedByInit)
752
4
      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
753
282k
    EmitNullabilityCheck(lvalue, value, init->getExprLoc());
754
282k
    EmitStoreThroughLValue(RValue::get(value), lvalue, true);
755
282k
    return;
756
282k
  }
757
1.07k
758
1.07k
  if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init))
759
1
    init = DIE->getExpr();
760
1.07k
761
1.07k
  // If we're emitting a value with lifetime, we have to do the
762
1.07k
  // initialization *before* we leave the cleanup scopes.
763
1.07k
  if (const FullExpr *fe = dyn_cast<FullExpr>(init)) {
764
338
    enterFullExpression(fe);
765
338
    init = fe->getSubExpr();
766
338
  }
767
1.07k
  CodeGenFunction::RunCleanupsScope Scope(*this);
768
1.07k
769
1.07k
  // We have to maintain the illusion that the variable is
770
1.07k
  // zero-initialized.  If the variable might be accessed in its
771
1.07k
  // initializer, zero-initialize before running the initializer, then
772
1.07k
  // actually perform the initialization with an assign.
773
1.07k
  bool accessedByInit = false;
774
1.07k
  if (lifetime != Qualifiers::OCL_ExplicitNone)
775
978
    accessedByInit = (capturedByInit || 
isAccessedBy(D, init)976
);
776
1.07k
  if (accessedByInit) {
777
19
    LValue tempLV = lvalue;
778
19
    // Drill down to the __block object if necessary.
779
19
    if (capturedByInit) {
780
2
      // We can use a simple GEP for this because it can't have been
781
2
      // moved yet.
782
2
      tempLV.setAddress(emitBlockByrefAddress(tempLV.getAddress(),
783
2
                                              cast<VarDecl>(D),
784
2
                                              /*follow*/ false));
785
2
    }
786
19
787
19
    auto ty = cast<llvm::PointerType>(tempLV.getAddress().getElementType());
788
19
    llvm::Value *zero = CGM.getNullPointer(ty, tempLV.getType());
789
19
790
19
    // If __weak, we want to use a barrier under certain conditions.
791
19
    if (lifetime == Qualifiers::OCL_Weak)
792
4
      EmitARCInitWeak(tempLV.getAddress(), zero);
793
15
794
15
    // Otherwise just do a simple store.
795
15
    else
796
15
      EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
797
19
  }
798
1.07k
799
1.07k
  // Emit the initializer.
800
1.07k
  llvm::Value *value = nullptr;
801
1.07k
802
1.07k
  switch (lifetime) {
803
1.07k
  case Qualifiers::OCL_None:
804
0
    llvm_unreachable("present but none");
805
1.07k
806
1.07k
  case Qualifiers::OCL_Strong: {
807
650
    if (!D || 
!isa<VarDecl>(D)604
||
!cast<VarDecl>(D)->isARCPseudoStrong()581
) {
808
646
      value = EmitARCRetainScalarExpr(init);
809
646
      break;
810
646
    }
811
4
    // If D is pseudo-strong, treat it like __unsafe_unretained here. This means
812
4
    // that we omit the retain, and causes non-autoreleased return values to be
813
4
    // immediately released.
814
4
    LLVM_FALLTHROUGH;
815
4
  }
816
4
817
101
  case Qualifiers::OCL_ExplicitNone:
818
101
    value = EmitARCUnsafeUnretainedScalarExpr(init);
819
101
    break;
820
4
821
323
  case Qualifiers::OCL_Weak: {
822
323
    // If it's not accessed by the initializer, try to emit the
823
323
    // initialization with a copy or move.
824
323
    if (!accessedByInit && 
tryEmitARCCopyWeakInit(*this, lvalue, init)319
) {
825
157
      return;
826
157
    }
827
166
828
166
    // No way to optimize a producing initializer into this.  It's not
829
166
    // worth optimizing for, because the value will immediately
830
166
    // disappear in the common case.
831
166
    value = EmitScalarExpr(init);
832
166
833
166
    if (capturedByInit) 
drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D))0
;
834
166
    if (accessedByInit)
835
4
      EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
836
162
    else
837
162
      EmitARCInitWeak(lvalue.getAddress(), value);
838
166
    return;
839
166
  }
840
166
841
166
  case Qualifiers::OCL_Autoreleasing:
842
5
    value = EmitARCRetainAutoreleaseScalarExpr(init);
843
5
    break;
844
752
  }
845
752
846
752
  if (capturedByInit) 
drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D))2
;
847
752
848
752
  EmitNullabilityCheck(lvalue, value, init->getExprLoc());
849
752
850
752
  // If the variable might have been accessed by its initializer, we
851
752
  // might have to initialize with a barrier.  We have to do this for
852
752
  // both __weak and __strong, but __weak got filtered out above.
853
752
  if (accessedByInit && 
lifetime == Qualifiers::OCL_Strong15
) {
854
11
    llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc());
855
11
    EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
856
11
    EmitARCRelease(oldValue, ARCImpreciseLifetime);
857
11
    return;
858
11
  }
859
741
860
741
  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
861
741
}
862
863
/// Decide whether we can emit the non-zero parts of the specified initializer
864
/// with equal or fewer than NumStores scalar stores.
865
static bool canEmitInitWithFewStoresAfterBZero(llvm::Constant *Init,
866
102k
                                               unsigned &NumStores) {
867
102k
  // Zero and Undef never requires any extra stores.
868
102k
  if (isa<llvm::ConstantAggregateZero>(Init) ||
869
102k
      
isa<llvm::ConstantPointerNull>(Init)102k
||
870
102k
      
isa<llvm::UndefValue>(Init)102k
)
871
102
    return true;
872
102k
  if (isa<llvm::ConstantInt>(Init) || 
isa<llvm::ConstantFP>(Init)571
||
873
102k
      
isa<llvm::ConstantVector>(Init)366
||
isa<llvm::BlockAddress>(Init)366
||
874
102k
      
isa<llvm::ConstantExpr>(Init)361
)
875
102k
    return Init->isNullValue() || 
NumStores--975
;
876
292
877
292
  // See if we can emit each element.
878
292
  if (isa<llvm::ConstantArray>(Init) || 
isa<llvm::ConstantStruct>(Init)240
) {
879
661
    for (unsigned i = 0, e = Init->getNumOperands(); i != e; 
++i500
) {
880
574
      llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
881
574
      if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
882
74
        return false;
883
574
    }
884
161
    
return true87
;
885
131
  }
886
131
887
131
  if (llvm::ConstantDataSequential *CDS =
888
123
        dyn_cast<llvm::ConstantDataSequential>(Init)) {
889
102k
    for (unsigned i = 0, e = CDS->getNumElements(); i != e; 
++i102k
) {
890
102k
      llvm::Constant *Elt = CDS->getElementAsConstant(i);
891
102k
      if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
892
85
        return false;
893
102k
    }
894
123
    
return true38
;
895
8
  }
896
8
897
8
  // Anything else is hard and scary.
898
8
  return false;
899
8
}
900
901
/// For inits that canEmitInitWithFewStoresAfterBZero returned true for, emit
902
/// the scalar stores that would be required.
903
static void emitStoresForInitAfterBZero(CodeGenModule &CGM,
904
                                        llvm::Constant *Init, Address Loc,
905
201
                                        bool isVolatile, CGBuilderTy &Builder) {
906
201
  assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
907
201
         "called emitStoresForInitAfterBZero for zero or undef value.");
908
201
909
201
  if (isa<llvm::ConstantInt>(Init) || 
isa<llvm::ConstantFP>(Init)125
||
910
201
      
isa<llvm::ConstantVector>(Init)89
||
isa<llvm::BlockAddress>(Init)89
||
911
201
      
isa<llvm::ConstantExpr>(Init)84
) {
912
142
    Builder.CreateStore(Init, Loc, isVolatile);
913
142
    return;
914
142
  }
915
59
916
59
  if (llvm::ConstantDataSequential *CDS =
917
17
          dyn_cast<llvm::ConstantDataSequential>(Init)) {
918
101k
    for (unsigned i = 0, e = CDS->getNumElements(); i != e; 
++i101k
) {
919
101k
      llvm::Constant *Elt = CDS->getElementAsConstant(i);
920
101k
921
101k
      // If necessary, get a pointer to the element and emit it.
922
101k
      if (!Elt->isNullValue() && 
!isa<llvm::UndefValue>(Elt)35
)
923
35
        emitStoresForInitAfterBZero(
924
35
            CGM, Elt, Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), isVolatile,
925
35
            Builder);
926
101k
    }
927
17
    return;
928
17
  }
929
42
930
42
  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
931
42
         "Unknown value type!");
932
42
933
210
  for (unsigned i = 0, e = Init->getNumOperands(); i != e; 
++i168
) {
934
168
    llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
935
168
936
168
    // If necessary, get a pointer to the element and emit it.
937
168
    if (!Elt->isNullValue() && 
!isa<llvm::UndefValue>(Elt)117
)
938
115
      emitStoresForInitAfterBZero(CGM, Elt,
939
115
                                  Builder.CreateConstInBoundsGEP2_32(Loc, 0, i),
940
115
                                  isVolatile, Builder);
941
168
  }
942
42
}
943
944
/// Decide whether we should use bzero plus some stores to initialize a local
945
/// variable instead of using a memcpy from a constant global.  It is beneficial
946
/// to use bzero if the global is all zeros, or mostly zeros and large.
947
static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init,
948
4.95k
                                                 uint64_t GlobalSize) {
949
4.95k
  // If a global is all zeros, always use a bzero.
950
4.95k
  if (isa<llvm::ConstantAggregateZero>(Init)) 
return true1.16k
;
951
3.78k
952
3.78k
  // If a non-zero global is <= 32 bytes, always use a memcpy.  If it is large,
953
3.78k
  // do it if it will require 6 or fewer scalar stores.
954
3.78k
  // TODO: Should budget depends on the size?  Avoiding a large global warrants
955
3.78k
  // plopping in more stores.
956
3.78k
  unsigned StoreBudget = 6;
957
3.78k
  uint64_t SizeLimit = 32;
958
3.78k
959
3.78k
  return GlobalSize > SizeLimit &&
960
3.78k
         
canEmitInitWithFewStoresAfterBZero(Init, StoreBudget)178
;
961
3.78k
}
962
963
/// Decide whether we should use memset to initialize a local variable instead
964
/// of using a memcpy from a constant global. Assumes we've already decided to
965
/// not user bzero.
966
/// FIXME We could be more clever, as we are for bzero above, and generate
967
///       memset followed by stores. It's unclear that's worth the effort.
968
static llvm::Value *shouldUseMemSetToInitialize(llvm::Constant *Init,
969
                                                uint64_t GlobalSize,
970
3.73k
                                                const llvm::DataLayout &DL) {
971
3.73k
  uint64_t SizeLimit = 32;
972
3.73k
  if (GlobalSize <= SizeLimit)
973
3.61k
    return nullptr;
974
127
  return llvm::isBytewiseValue(Init, DL);
975
127
}
976
977
/// Decide whether we want to split a constant structure or array store into a
978
/// sequence of its fields' stores. This may cost us code size and compilation
979
/// speed, but plays better with store optimizations.
980
static bool shouldSplitConstantStore(CodeGenModule &CGM,
981
3.69k
                                     uint64_t GlobalByteSize) {
982
3.69k
  // Don't break things that occupy more than one cacheline.
983
3.69k
  uint64_t ByteSizeLimit = 64;
984
3.69k
  if (CGM.getCodeGenOpts().OptimizationLevel == 0)
985
1.21k
    return false;
986
2.48k
  if (GlobalByteSize <= ByteSizeLimit)
987
2.43k
    return true;
988
53
  return false;
989
53
}
990
991
enum class IsPattern { No, Yes };
992
993
/// Generate a constant filled with either a pattern or zeroes.
994
static llvm::Constant *patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern,
995
308
                                        llvm::Type *Ty) {
996
308
  if (isPattern == IsPattern::Yes)
997
129
    return initializationPatternFor(CGM, Ty);
998
179
  else
999
179
    return llvm::Constant::getNullValue(Ty);
1000
308
}
1001
1002
static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1003
                                        llvm::Constant *constant);
1004
1005
/// Helper function for constWithPadding() to deal with padding in structures.
1006
static llvm::Constant *constStructWithPadding(CodeGenModule &CGM,
1007
                                              IsPattern isPattern,
1008
                                              llvm::StructType *STy,
1009
785
                                              llvm::Constant *constant) {
1010
785
  const llvm::DataLayout &DL = CGM.getDataLayout();
1011
785
  const llvm::StructLayout *Layout = DL.getStructLayout(STy);
1012
785
  llvm::Type *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext());
1013
785
  unsigned SizeSoFar = 0;
1014
785
  SmallVector<llvm::Constant *, 8> Values;
1015
785
  bool NestedIntact = true;
1016
2.50k
  for (unsigned i = 0, e = STy->getNumElements(); i != e; 
i++1.71k
) {
1017
1.71k
    unsigned CurOff = Layout->getElementOffset(i);
1018
1.71k
    if (SizeSoFar < CurOff) {
1019
82
      assert(!STy->isPacked());
1020
82
      auto *PadTy = llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar);
1021
82
      Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1022
82
    }
1023
1.71k
    llvm::Constant *CurOp;
1024
1.71k
    if (constant->isZeroValue())
1025
414
      CurOp = llvm::Constant::getNullValue(STy->getElementType(i));
1026
1.30k
    else
1027
1.30k
      CurOp = cast<llvm::Constant>(constant->getAggregateElement(i));
1028
1.71k
    auto *NewOp = constWithPadding(CGM, isPattern, CurOp);
1029
1.71k
    if (CurOp != NewOp)
1030
35
      NestedIntact = false;
1031
1.71k
    Values.push_back(NewOp);
1032
1.71k
    SizeSoFar = CurOff + DL.getTypeAllocSize(CurOp->getType());
1033
1.71k
  }
1034
785
  unsigned TotalSize = Layout->getSizeInBytes();
1035
785
  if (SizeSoFar < TotalSize) {
1036
156
    auto *PadTy = llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar);
1037
156
    Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1038
156
  }
1039
785
  if (NestedIntact && 
Values.size() == STy->getNumElements()764
)
1040
526
    return constant;
1041
259
  return llvm::ConstantStruct::getAnon(Values, STy->isPacked());
1042
259
}
1043
1044
/// Replace all padding bytes in a given constant with either a pattern byte or
1045
/// 0x00.
1046
static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1047
69.8k
                                        llvm::Constant *constant) {
1048
69.8k
  llvm::Type *OrigTy = constant->getType();
1049
69.8k
  if (const auto STy = dyn_cast<llvm::StructType>(OrigTy))
1050
785
    return constStructWithPadding(CGM, isPattern, STy, constant);
1051
69.0k
  if (auto *STy = dyn_cast<llvm::SequentialType>(OrigTy)) {
1052
295
    llvm::SmallVector<llvm::Constant *, 8> Values;
1053
295
    unsigned Size = STy->getNumElements();
1054
295
    if (!Size)
1055
14
      return constant;
1056
281
    llvm::Type *ElemTy = STy->getElementType();
1057
281
    bool ZeroInitializer = constant->isZeroValue();
1058
281
    llvm::Constant *OpValue, *PaddedOp;
1059
281
    if (ZeroInitializer) {
1060
101
      OpValue = llvm::Constant::getNullValue(ElemTy);
1061
101
      PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1062
101
    }
1063
395k
    for (unsigned Op = 0; Op != Size; 
++Op395k
) {
1064
395k
      if (!ZeroInitializer) {
1065
66.9k
        OpValue = constant->getAggregateElement(Op);
1066
66.9k
        PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1067
66.9k
      }
1068
395k
      Values.push_back(PaddedOp);
1069
395k
    }
1070
281
    auto *NewElemTy = Values[0]->getType();
1071
281
    if (NewElemTy == ElemTy)
1072
253
      return constant;
1073
28
    if (OrigTy->isArrayTy()) {
1074
28
      auto *ArrayTy = llvm::ArrayType::get(NewElemTy, Size);
1075
28
      return llvm::ConstantArray::get(ArrayTy, Values);
1076
28
    } else {
1077
0
      return llvm::ConstantVector::get(Values);
1078
0
    }
1079
68.7k
  }
1080
68.7k
  return constant;
1081
68.7k
}
1082
1083
Address CodeGenModule::createUnnamedGlobalFrom(const VarDecl &D,
1084
                                               llvm::Constant *Constant,
1085
3.59k
                                               CharUnits Align) {
1086
3.59k
  auto FunctionName = [&](const DeclContext *DC) -> std::string {
1087
3.58k
    if (const auto *FD = dyn_cast<FunctionDecl>(DC)) {
1088
3.57k
      if (const auto *CC = dyn_cast<CXXConstructorDecl>(FD))
1089
9
        return CC->getNameAsString();
1090
3.56k
      if (const auto *CD = dyn_cast<CXXDestructorDecl>(FD))
1091
0
        return CD->getNameAsString();
1092
3.56k
      return getMangledName(FD);
1093
3.56k
    } else 
if (const auto *11
OM11
= dyn_cast<ObjCMethodDecl>(DC)) {
1094
6
      return OM->getNameAsString();
1095
6
    } else 
if (5
isa<BlockDecl>(DC)5
) {
1096
0
      return "<block>";
1097
5
    } else if (isa<CapturedDecl>(DC)) {
1098
5
      return "<captured>";
1099
5
    } else {
1100
0
      llvm_unreachable("expected a function or method");
1101
0
    }
1102
3.58k
  };
1103
3.59k
1104
3.59k
  // Form a simple per-variable cache of these values in case we find we
1105
3.59k
  // want to reuse them.
1106
3.59k
  llvm::GlobalVariable *&CacheEntry = InitializerConstants[&D];
1107
3.59k
  if (!CacheEntry || 
CacheEntry->getInitializer() != Constant6
) {
1108
3.58k
    auto *Ty = Constant->getType();
1109
3.58k
    bool isConstant = true;
1110
3.58k
    llvm::GlobalVariable *InsertBefore = nullptr;
1111
3.58k
    unsigned AS =
1112
3.58k
        getContext().getTargetAddressSpace(getStringLiteralAddressSpace());
1113
3.58k
    std::string Name;
1114
3.58k
    if (D.hasGlobalStorage())
1115
1
      Name = getMangledName(&D).str() + ".const";
1116
3.58k
    else if (const DeclContext *DC = D.getParentFunctionOrMethod())
1117
3.58k
      Name = ("__const." + FunctionName(DC) + "." + D.getName()).str();
1118
3.58k
    else
1119
3.58k
      
llvm_unreachable0
("local variable has no parent function or method");
1120
3.58k
    llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1121
3.58k
        getModule(), Ty, isConstant, llvm::GlobalValue::PrivateLinkage,
1122
3.58k
        Constant, Name, InsertBefore, llvm::GlobalValue::NotThreadLocal, AS);
1123
3.58k
    GV->setAlignment(Align.getQuantity());
1124
3.58k
    GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1125
3.58k
    CacheEntry = GV;
1126
3.58k
  } else 
if (6
CacheEntry->getAlignment() < Align.getQuantity()6
) {
1127
0
    CacheEntry->setAlignment(Align.getQuantity());
1128
0
  }
1129
3.59k
1130
3.59k
  return Address(CacheEntry, Align);
1131
3.59k
}
1132
1133
static Address createUnnamedGlobalForMemcpyFrom(CodeGenModule &CGM,
1134
                                                const VarDecl &D,
1135
                                                CGBuilderTy &Builder,
1136
                                                llvm::Constant *Constant,
1137
3.58k
                                                CharUnits Align) {
1138
3.58k
  Address SrcPtr = CGM.createUnnamedGlobalFrom(D, Constant, Align);
1139
3.58k
  llvm::Type *BP = llvm::PointerType::getInt8PtrTy(CGM.getLLVMContext(),
1140
3.58k
                                                   SrcPtr.getAddressSpace());
1141
3.58k
  if (SrcPtr.getType() != BP)
1142
3.58k
    SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
1143
3.58k
  return SrcPtr;
1144
3.58k
}
1145
1146
static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D,
1147
                                  Address Loc, bool isVolatile,
1148
                                  CGBuilderTy &Builder,
1149
5.62k
                                  llvm::Constant *constant) {
1150
5.62k
  auto *Ty = constant->getType();
1151
5.62k
  uint64_t ConstantSize = CGM.getDataLayout().getTypeAllocSize(Ty);
1152
5.62k
  if (!ConstantSize)
1153
19
    return;
1154
5.60k
1155
5.60k
  bool canDoSingleStore = Ty->isIntOrIntVectorTy() ||
1156
5.60k
                          
Ty->isPtrOrPtrVectorTy()5.17k
||
Ty->isFPOrFPVectorTy()5.08k
;
1157
5.60k
  if (canDoSingleStore) {
1158
655
    Builder.CreateStore(constant, Loc, isVolatile);
1159
655
    return;
1160
655
  }
1161
4.95k
1162
4.95k
  auto *SizeVal = llvm::ConstantInt::get(CGM.IntPtrTy, ConstantSize);
1163
4.95k
1164
4.95k
  // If the initializer is all or mostly the same, codegen with bzero / memset
1165
4.95k
  // then do a few stores afterward.
1166
4.95k
  if (shouldUseBZeroPlusStoresToInitialize(constant, ConstantSize)) {
1167
1.21k
    Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, 0), SizeVal,
1168
1.21k
                         isVolatile);
1169
1.21k
1170
1.21k
    bool valueAlreadyCorrect =
1171
1.21k
        constant->isNullValue() || 
isa<llvm::UndefValue>(constant)51
;
1172
1.21k
    if (!valueAlreadyCorrect) {
1173
51
      Loc = Builder.CreateBitCast(Loc, Ty->getPointerTo(Loc.getAddressSpace()));
1174
51
      emitStoresForInitAfterBZero(CGM, constant, Loc, isVolatile, Builder);
1175
51
    }
1176
1.21k
    return;
1177
1.21k
  }
1178
3.73k
1179
3.73k
  // If the initializer is a repeated byte pattern, use memset.
1180
3.73k
  llvm::Value *Pattern =
1181
3.73k
      shouldUseMemSetToInitialize(constant, ConstantSize, CGM.getDataLayout());
1182
3.73k
  if (Pattern) {
1183
38
    uint64_t Value = 0x00;
1184
38
    if (!isa<llvm::UndefValue>(Pattern)) {
1185
38
      const llvm::APInt &AP = cast<llvm::ConstantInt>(Pattern)->getValue();
1186
38
      assert(AP.getBitWidth() <= 8);
1187
38
      Value = AP.getLimitedValue();
1188
38
    }
1189
38
    Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, Value), SizeVal,
1190
38
                         isVolatile);
1191
38
    return;
1192
38
  }
1193
3.69k
1194
3.69k
  // If the initializer is small, use a handful of stores.
1195
3.69k
  if (shouldSplitConstantStore(CGM, ConstantSize)) {
1196
2.43k
    if (auto *STy = dyn_cast<llvm::StructType>(Ty)) {
1197
2.21k
      // FIXME: handle the case when STy != Loc.getElementType().
1198
2.21k
      if (STy == Loc.getElementType()) {
1199
256
        for (unsigned i = 0; i != constant->getNumOperands(); 
i++154
) {
1200
154
          Address EltPtr = Builder.CreateStructGEP(Loc, i);
1201
154
          emitStoresForConstant(
1202
154
              CGM, D, EltPtr, isVolatile, Builder,
1203
154
              cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)));
1204
154
        }
1205
102
        return;
1206
102
      }
1207
218
    } else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) {
1208
218
      // FIXME: handle the case when ATy != Loc.getElementType().
1209
218
      if (ATy == Loc.getElementType()) {
1210
64
        for (unsigned i = 0; i != ATy->getNumElements(); 
i++48
) {
1211
48
          Address EltPtr = Builder.CreateConstArrayGEP(Loc, i);
1212
48
          emitStoresForConstant(
1213
48
              CGM, D, EltPtr, isVolatile, Builder,
1214
48
              cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)));
1215
48
        }
1216
16
        return;
1217
16
      }
1218
3.58k
    }
1219
2.43k
  }
1220
3.58k
1221
3.58k
  // Copy from a global.
1222
3.58k
  Builder.CreateMemCpy(Loc,
1223
3.58k
                       createUnnamedGlobalForMemcpyFrom(
1224
3.58k
                           CGM, D, Builder, constant, Loc.getAlignment()),
1225
3.58k
                       SizeVal, isVolatile);
1226
3.58k
}
1227
1228
static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D,
1229
                                  Address Loc, bool isVolatile,
1230
374
                                  CGBuilderTy &Builder) {
1231
374
  llvm::Type *ElTy = Loc.getElementType();
1232
374
  llvm::Constant *constant =
1233
374
      constWithPadding(CGM, IsPattern::No, llvm::Constant::getNullValue(ElTy));
1234
374
  emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant);
1235
374
}
1236
1237
static void emitStoresForPatternInit(CodeGenModule &CGM, const VarDecl &D,
1238
                                     Address Loc, bool isVolatile,
1239
495
                                     CGBuilderTy &Builder) {
1240
495
  llvm::Type *ElTy = Loc.getElementType();
1241
495
  llvm::Constant *constant = constWithPadding(
1242
495
      CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy));
1243
495
  assert(!isa<llvm::UndefValue>(constant));
1244
495
  emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant);
1245
495
}
1246
1247
1.01k
static bool containsUndef(llvm::Constant *constant) {
1248
1.01k
  auto *Ty = constant->getType();
1249
1.01k
  if (isa<llvm::UndefValue>(constant))
1250
49
    return true;
1251
968
  if (Ty->isStructTy() || 
Ty->isArrayTy()687
||
Ty->isVectorTy()625
)
1252
343
    for (llvm::Use &Op : constant->operands())
1253
827
      if (containsUndef(cast<llvm::Constant>(Op)))
1254
49
        return true;
1255
968
  
return false919
;
1256
968
}
1257
1258
static llvm::Constant *replaceUndef(CodeGenModule &CGM, IsPattern isPattern,
1259
323
                                    llvm::Constant *constant) {
1260
323
  auto *Ty = constant->getType();
1261
323
  if (isa<llvm::UndefValue>(constant))
1262
70
    return patternOrZeroFor(CGM, isPattern, Ty);
1263
253
  if (!(Ty->isStructTy() || 
Ty->isArrayTy()105
||
Ty->isVectorTy()63
))
1264
63
    return constant;
1265
190
  if (!containsUndef(constant))
1266
141
    return constant;
1267
49
  llvm::SmallVector<llvm::Constant *, 8> Values(constant->getNumOperands());
1268
175
  for (unsigned Op = 0, NumOp = constant->getNumOperands(); Op != NumOp; 
++Op126
) {
1269
126
    auto *OpValue = cast<llvm::Constant>(constant->getOperand(Op));
1270
126
    Values[Op] = replaceUndef(CGM, isPattern, OpValue);
1271
126
  }
1272
49
  if (Ty->isStructTy())
1273
49
    return llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Values);
1274
0
  if (Ty->isArrayTy())
1275
0
    return llvm::ConstantArray::get(cast<llvm::ArrayType>(Ty), Values);
1276
0
  assert(Ty->isVectorTy());
1277
0
  return llvm::ConstantVector::get(Values);
1278
0
}
1279
1280
/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
1281
/// variable declaration with auto, register, or no storage class specifier.
1282
/// These turn into simple stack objects, or GlobalValues depending on target.
1283
427k
void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
1284
427k
  AutoVarEmission emission = EmitAutoVarAlloca(D);
1285
427k
  EmitAutoVarInit(emission);
1286
427k
  EmitAutoVarCleanups(emission);
1287
427k
}
1288
1289
/// Emit a lifetime.begin marker if some criteria are satisfied.
1290
/// \return a pointer to the temporary size Value if a marker was emitted, null
1291
/// otherwise
1292
llvm::Value *CodeGenFunction::EmitLifetimeStart(uint64_t Size,
1293
465k
                                                llvm::Value *Addr) {
1294
465k
  if (!ShouldEmitLifetimeMarkers)
1295
100k
    return nullptr;
1296
364k
1297
364k
  assert(Addr->getType()->getPointerAddressSpace() ==
1298
364k
             CGM.getDataLayout().getAllocaAddrSpace() &&
1299
364k
         "Pointer should be in alloca address space");
1300
364k
  llvm::Value *SizeV = llvm::ConstantInt::get(Int64Ty, Size);
1301
364k
  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1302
364k
  llvm::CallInst *C =
1303
364k
      Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr});
1304
364k
  C->setDoesNotThrow();
1305
364k
  return SizeV;
1306
364k
}
1307
1308
381k
void CodeGenFunction::EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr) {
1309
381k
  assert(Addr->getType()->getPointerAddressSpace() ==
1310
381k
             CGM.getDataLayout().getAllocaAddrSpace() &&
1311
381k
         "Pointer should be in alloca address space");
1312
381k
  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1313
381k
  llvm::CallInst *C =
1314
381k
      Builder.CreateCall(CGM.getLLVMLifetimeEndFn(), {Size, Addr});
1315
381k
  C->setDoesNotThrow();
1316
381k
}
1317
1318
void CodeGenFunction::EmitAndRegisterVariableArrayDimensions(
1319
1.85k
    CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo) {
1320
1.85k
  // For each dimension stores its QualType and corresponding
1321
1.85k
  // size-expression Value.
1322
1.85k
  SmallVector<CodeGenFunction::VlaSizePair, 4> Dimensions;
1323
1.85k
  SmallVector<IdentifierInfo *, 4> VLAExprNames;
1324
1.85k
1325
1.85k
  // Break down the array into individual dimensions.
1326
1.85k
  QualType Type1D = D.getType();
1327
4.40k
  while (getContext().getAsVariableArrayType(Type1D)) {
1328
2.55k
    auto VlaSize = getVLAElements1D(Type1D);
1329
2.55k
    if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1330
548
      Dimensions.emplace_back(C, Type1D.getUnqualifiedType());
1331
2.00k
    else {
1332
2.00k
      // Generate a locally unique name for the size expression.
1333
2.00k
      Twine Name = Twine("__vla_expr") + Twine(VLAExprCounter++);
1334
2.00k
      SmallString<12> Buffer;
1335
2.00k
      StringRef NameRef = Name.toStringRef(Buffer);
1336
2.00k
      auto &Ident = getContext().Idents.getOwn(NameRef);
1337
2.00k
      VLAExprNames.push_back(&Ident);
1338
2.00k
      auto SizeExprAddr =
1339
2.00k
          CreateDefaultAlignTempAlloca(VlaSize.NumElts->getType(), NameRef);
1340
2.00k
      Builder.CreateStore(VlaSize.NumElts, SizeExprAddr);
1341
2.00k
      Dimensions.emplace_back(SizeExprAddr.getPointer(),
1342
2.00k
                              Type1D.getUnqualifiedType());
1343
2.00k
    }
1344
2.55k
    Type1D = VlaSize.Type;
1345
2.55k
  }
1346
1.85k
1347
1.85k
  if (!EmitDebugInfo)
1348
1.84k
    return;
1349
15
1350
15
  // Register each dimension's size-expression with a DILocalVariable,
1351
15
  // so that it can be used by CGDebugInfo when instantiating a DISubrange
1352
15
  // to describe this array.
1353
15
  unsigned NameIdx = 0;
1354
16
  for (auto &VlaSize : Dimensions) {
1355
16
    llvm::Metadata *MD;
1356
16
    if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1357
1
      MD = llvm::ConstantAsMetadata::get(C);
1358
15
    else {
1359
15
      // Create an artificial VarDecl to generate debug info for.
1360
15
      IdentifierInfo *NameIdent = VLAExprNames[NameIdx++];
1361
15
      auto VlaExprTy = VlaSize.NumElts->getType()->getPointerElementType();
1362
15
      auto QT = getContext().getIntTypeForBitwidth(
1363
15
          VlaExprTy->getScalarSizeInBits(), false);
1364
15
      auto *ArtificialDecl = VarDecl::Create(
1365
15
          getContext(), const_cast<DeclContext *>(D.getDeclContext()),
1366
15
          D.getLocation(), D.getLocation(), NameIdent, QT,
1367
15
          getContext().CreateTypeSourceInfo(QT), SC_Auto);
1368
15
      ArtificialDecl->setImplicit();
1369
15
1370
15
      MD = DI->EmitDeclareOfAutoVariable(ArtificialDecl, VlaSize.NumElts,
1371
15
                                         Builder);
1372
15
    }
1373
16
    assert(MD && "No Size expression debug node created");
1374
16
    DI->registerVLASizeExpression(VlaSize.Type, MD);
1375
16
  }
1376
15
}
1377
1378
/// EmitAutoVarAlloca - Emit the alloca and debug information for a
1379
/// local variable.  Does not emit initialization or destruction.
1380
CodeGenFunction::AutoVarEmission
1381
440k
CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
1382
440k
  QualType Ty = D.getType();
1383
440k
  assert(
1384
440k
      Ty.getAddressSpace() == LangAS::Default ||
1385
440k
      (Ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL));
1386
440k
1387
440k
  AutoVarEmission emission(D);
1388
440k
1389
440k
  bool isEscapingByRef = D.isEscapingByref();
1390
440k
  emission.IsEscapingByRef = isEscapingByRef;
1391
440k
1392
440k
  CharUnits alignment = getContext().getDeclAlign(&D);
1393
440k
1394
440k
  // If the type is variably-modified, emit all the VLA sizes for it.
1395
440k
  if (Ty->isVariablyModifiedType())
1396
1.88k
    EmitVariablyModifiedType(Ty);
1397
440k
1398
440k
  auto *DI = getDebugInfo();
1399
440k
  bool EmitDebugInfo = DI && CGM.getCodeGenOpts().getDebugInfo() >=
1400
162k
                                 codegenoptions::LimitedDebugInfo;
1401
440k
1402
440k
  Address address = Address::invalid();
1403
440k
  Address AllocaAddr = Address::invalid();
1404
440k
  Address OpenMPLocalAddr =
1405
440k
      getLangOpts().OpenMP
1406
440k
          ? 
CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D)78.5k
1407
440k
          : 
Address::invalid()362k
;
1408
440k
  bool NRVO = getLangOpts().ElideConstructors && 
D.isNRVOVariable()440k
;
1409
440k
1410
440k
  if (getLangOpts().OpenMP && 
OpenMPLocalAddr.isValid()78.5k
) {
1411
55
    address = OpenMPLocalAddr;
1412
440k
  } else if (Ty->isConstantSizeType()) {
1413
438k
    // If this value is an array or struct with a statically determinable
1414
438k
    // constant initializer, there are optimizations we can do.
1415
438k
    //
1416
438k
    // TODO: We should constant-evaluate the initializer of any variable,
1417
438k
    // as long as it is initialized by a constant expression. Currently,
1418
438k
    // isConstantInitializer produces wrong answers for structs with
1419
438k
    // reference or bitfield members, and a few other cases, and checking
1420
438k
    // for POD-ness protects us from some of these.
1421
438k
    if (D.getInit() && 
(304k
Ty->isArrayType()304k
||
Ty->isRecordType()301k
) &&
1422
438k
        
(46.8k
D.isConstexpr()46.8k
||
1423
46.8k
         
(46.8k
(46.8k
Ty.isPODType(getContext())46.8k
||
1424
46.8k
           
getContext().getBaseElementType(Ty)->isObjCObjectPointerType()33.4k
) &&
1425
46.8k
          
D.getInit()->isConstantInitializer(getContext(), false)13.4k
))) {
1426
7.53k
1427
7.53k
      // If the variable's a const type, and it's neither an NRVO
1428
7.53k
      // candidate nor a __block variable and has no mutable members,
1429
7.53k
      // emit it as a global instead.
1430
7.53k
      // Exception is if a variable is located in non-constant address space
1431
7.53k
      // in OpenCL.
1432
7.53k
      if ((!getLangOpts().OpenCL ||
1433
7.53k
           
Ty.getAddressSpace() == LangAS::opencl_constant79
) &&
1434
7.53k
          
(7.45k
CGM.getCodeGenOpts().MergeAllConstants7.45k
&&
!NRVO15
&&
1435
7.45k
           
!isEscapingByRef15
&&
CGM.isTypeConstant(Ty, true)15
)) {
1436
6
        EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
1437
6
1438
6
        // Signal this condition to later callbacks.
1439
6
        emission.Addr = Address::invalid();
1440
6
        assert(emission.wasEmittedAsGlobal());
1441
6
        return emission;
1442
6
      }
1443
7.52k
1444
7.52k
      // Otherwise, tell the initialization code that we're in this case.
1445
7.52k
      emission.IsConstantAggregate = true;
1446
7.52k
    }
1447
438k
1448
438k
    // A normal fixed sized variable becomes an alloca in the entry block,
1449
438k
    // unless:
1450
438k
    // - it's an NRVO variable.
1451
438k
    // - we are compiling OpenMP and it's an OpenMP local variable.
1452
438k
    
if (438k
NRVO438k
) {
1453
1.83k
      // The named return value optimization: allocate this variable in the
1454
1.83k
      // return slot, so that we can elide the copy when returning this
1455
1.83k
      // variable (C++0x [class.copy]p34).
1456
1.83k
      address = ReturnValue;
1457
1.83k
1458
1.83k
      if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1459
1.83k
        const auto *RD = RecordTy->getDecl();
1460
1.83k
        const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1461
1.83k
        if ((CXXRD && 
!CXXRD->hasTrivialDestructor()957
) ||
1462
1.83k
            
RD->isNonTrivialToPrimitiveDestroy()1.45k
) {
1463
377
          // Create a flag that is used to indicate when the NRVO was applied
1464
377
          // to this variable. Set it to zero to indicate that NRVO was not
1465
377
          // applied.
1466
377
          llvm::Value *Zero = Builder.getFalse();
1467
377
          Address NRVOFlag =
1468
377
            CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo");
1469
377
          EnsureInsertPoint();
1470
377
          Builder.CreateStore(Zero, NRVOFlag);
1471
377
1472
377
          // Record the NRVO flag for this variable.
1473
377
          NRVOFlags[&D] = NRVOFlag.getPointer();
1474
377
          emission.NRVOFlag = NRVOFlag.getPointer();
1475
377
        }
1476
1.83k
      }
1477
436k
    } else {
1478
436k
      CharUnits allocaAlignment;
1479
436k
      llvm::Type *allocaTy;
1480
436k
      if (isEscapingByRef) {
1481
326
        auto &byrefInfo = getBlockByrefInfo(&D);
1482
326
        allocaTy = byrefInfo.Type;
1483
326
        allocaAlignment = byrefInfo.ByrefAlignment;
1484
436k
      } else {
1485
436k
        allocaTy = ConvertTypeForMem(Ty);
1486
436k
        allocaAlignment = alignment;
1487
436k
      }
1488
436k
1489
436k
      // Create the alloca.  Note that we set the name separately from
1490
436k
      // building the instruction so that it's there even in no-asserts
1491
436k
      // builds.
1492
436k
      address = CreateTempAlloca(allocaTy, allocaAlignment, D.getName(),
1493
436k
                                 /*ArraySize=*/nullptr, &AllocaAddr);
1494
436k
1495
436k
      // Don't emit lifetime markers for MSVC catch parameters. The lifetime of
1496
436k
      // the catch parameter starts in the catchpad instruction, and we can't
1497
436k
      // insert code in those basic blocks.
1498
436k
      bool IsMSCatchParam =
1499
436k
          D.isExceptionVariable() && 
getTarget().getCXXABI().isMicrosoft()447
;
1500
436k
1501
436k
      // Emit a lifetime intrinsic if meaningful. There's no point in doing this
1502
436k
      // if we don't have a valid insertion point (?).
1503
436k
      if (HaveInsertPoint() && 
!IsMSCatchParam436k
) {
1504
436k
        // If there's a jump into the lifetime of this variable, its lifetime
1505
436k
        // gets broken up into several regions in IR, which requires more work
1506
436k
        // to handle correctly. For now, just omit the intrinsics; this is a
1507
436k
        // rare case, and it's better to just be conservatively correct.
1508
436k
        // PR28267.
1509
436k
        //
1510
436k
        // We have to do this in all language modes if there's a jump past the
1511
436k
        // declaration. We also have to do it in C if there's a jump to an
1512
436k
        // earlier point in the current block because non-VLA lifetimes begin as
1513
436k
        // soon as the containing block is entered, not when its variables
1514
436k
        // actually come into scope; suppressing the lifetime annotations
1515
436k
        // completely in this case is unnecessarily pessimistic, but again, this
1516
436k
        // is rare.
1517
436k
        if (!Bypasses.IsBypassed(&D) &&
1518
436k
            
!(436k
!getLangOpts().CPlusPlus436k
&&
hasLabelBeenSeenInCurrentScope()149k
)) {
1519
436k
          uint64_t size = CGM.getDataLayout().getTypeAllocSize(allocaTy);
1520
436k
          emission.SizeForLifetimeMarkers =
1521
436k
              EmitLifetimeStart(size, AllocaAddr.getPointer());
1522
436k
        }
1523
436k
      } else {
1524
96
        assert(!emission.useLifetimeMarkers());
1525
96
      }
1526
436k
    }
1527
438k
  } else {
1528
1.85k
    EnsureInsertPoint();
1529
1.85k
1530
1.85k
    if (!DidCallStackSave) {
1531
1.24k
      // Save the stack.
1532
1.24k
      Address Stack =
1533
1.24k
        CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack");
1534
1.24k
1535
1.24k
      llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
1536
1.24k
      llvm::Value *V = Builder.CreateCall(F);
1537
1.24k
      Builder.CreateStore(V, Stack);
1538
1.24k
1539
1.24k
      DidCallStackSave = true;
1540
1.24k
1541
1.24k
      // Push a cleanup block and restore the stack there.
1542
1.24k
      // FIXME: in general circumstances, this should be an EH cleanup.
1543
1.24k
      pushStackRestore(NormalCleanup, Stack);
1544
1.24k
    }
1545
1.85k
1546
1.85k
    auto VlaSize = getVLASize(Ty);
1547
1.85k
    llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type);
1548
1.85k
1549
1.85k
    // Allocate memory for the array.
1550
1.85k
    address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts,
1551
1.85k
                               &AllocaAddr);
1552
1.85k
1553
1.85k
    // If we have debug info enabled, properly describe the VLA dimensions for
1554
1.85k
    // this type by registering the vla size expression for each of the
1555
1.85k
    // dimensions.
1556
1.85k
    EmitAndRegisterVariableArrayDimensions(DI, D, EmitDebugInfo);
1557
1.85k
  }
1558
440k
1559
440k
  setAddrOfLocalVar(&D, address);
1560
440k
  emission.Addr = address;
1561
440k
  emission.AllocaAddr = AllocaAddr;
1562
440k
1563
440k
  // Emit debug info for local var declaration.
1564
440k
  if (EmitDebugInfo && 
HaveInsertPoint()1.57k
) {
1565
1.57k
    Address DebugAddr = address;
1566
1.57k
    bool UsePointerValue = NRVO && 
ReturnValuePointer.isValid()13
;
1567
1.57k
    DI->setLocation(D.getLocation());
1568
1.57k
1569
1.57k
    // If NRVO, use a pointer to the return address.
1570
1.57k
    if (UsePointerValue)
1571
1
      DebugAddr = ReturnValuePointer;
1572
1.57k
1573
1.57k
    (void)DI->EmitDeclareOfAutoVariable(&D, DebugAddr.getPointer(), Builder,
1574
1.57k
                                        UsePointerValue);
1575
1.57k
  }
1576
440k
1577
440k
  if (D.hasAttr<AnnotateAttr>() && 
HaveInsertPoint()5
)
1578
4
    EmitVarAnnotations(&D, address.getPointer());
1579
440k
1580
440k
  // Make sure we call @llvm.lifetime.end.
1581
440k
  if (emission.useLifetimeMarkers())
1582
336k
    EHStack.pushCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker,
1583
336k
                                         emission.getOriginalAllocatedAddress(),
1584
336k
                                         emission.getSizeForLifetimeMarkers());
1585
440k
1586
440k
  return emission;
1587
440k
}
1588
1589
static bool isCapturedBy(const VarDecl &, const Expr *);
1590
1591
/// Determines whether the given __block variable is potentially
1592
/// captured by the given statement.
1593
745
static bool isCapturedBy(const VarDecl &Var, const Stmt *S) {
1594
745
  if (const Expr *E = dyn_cast<Expr>(S))
1595
743
    return isCapturedBy(Var, E);
1596
2
  for (const Stmt *SubStmt : S->children())
1597
2
    if (isCapturedBy(Var, SubStmt))
1598
0
      return true;
1599
2
  return false;
1600
2
}
1601
1602
/// Determines whether the given __block variable is potentially
1603
/// captured by the given expression.
1604
958
static bool isCapturedBy(const VarDecl &Var, const Expr *E) {
1605
958
  // Skip the most common kinds of expressions that make
1606
958
  // hierarchy-walking expensive.
1607
958
  E = E->IgnoreParenCasts();
1608
958
1609
958
  if (const BlockExpr *BE = dyn_cast<BlockExpr>(E)) {
1610
6
    const BlockDecl *Block = BE->getBlockDecl();
1611
6
    for (const auto &I : Block->captures()) {
1612
6
      if (I.getVariable() == &Var)
1613
6
        return true;
1614
6
    }
1615
6
1616
6
    // No need to walk into the subexpressions.
1617
6
    
return false0
;
1618
952
  }
1619
952
1620
952
  if (const StmtExpr *SE = dyn_cast<StmtExpr>(E)) {
1621
0
    const CompoundStmt *CS = SE->getSubStmt();
1622
0
    for (const auto *BI : CS->body())
1623
0
      if (const auto *BIE = dyn_cast<Expr>(BI)) {
1624
0
        if (isCapturedBy(Var, BIE))
1625
0
          return true;
1626
0
      }
1627
0
      else if (const auto *DS = dyn_cast<DeclStmt>(BI)) {
1628
0
          // special case declarations
1629
0
          for (const auto *I : DS->decls()) {
1630
0
              if (const auto *VD = dyn_cast<VarDecl>((I))) {
1631
0
                const Expr *Init = VD->getInit();
1632
0
                if (Init && isCapturedBy(Var, Init))
1633
0
                  return true;
1634
0
              }
1635
0
          }
1636
0
      }
1637
0
      else
1638
0
        // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1639
0
        // Later, provide code to poke into statements for capture analysis.
1640
0
        return true;
1641
0
    return false;
1642
952
  }
1643
952
1644
952
  for (const Stmt *SubStmt : E->children())
1645
743
    if (isCapturedBy(Var, SubStmt))
1646
3
      return true;
1647
952
1648
952
  
return false949
;
1649
952
}
1650
1651
/// Determine whether the given initializer is trivial in the sense
1652
/// that it requires no code to be generated.
1653
428k
bool CodeGenFunction::isTrivialInitializer(const Expr *Init) {
1654
428k
  if (!Init)
1655
124k
    return true;
1656
304k
1657
304k
  if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1658
30.0k
    if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1659
30.0k
      if (Constructor->isTrivial() &&
1660
30.0k
          
Constructor->isDefaultConstructor()4.37k
&&
1661
30.0k
          
!Construct->requiresZeroInitialization()3.21k
)
1662
3.19k
        return true;
1663
300k
1664
300k
  return false;
1665
300k
}
1666
1667
void CodeGenFunction::emitZeroOrPatternForAutoVarInit(QualType type,
1668
                                                      const VarDecl &D,
1669
882
                                                      Address Loc) {
1670
882
  auto trivialAutoVarInit = getContext().getLangOpts().getTrivialAutoVarInit();
1671
882
  CharUnits Size = getContext().getTypeSizeInChars(type);
1672
882
  bool isVolatile = type.isVolatileQualified();
1673
882
  if (!Size.isZero()) {
1674
869
    switch (trivialAutoVarInit) {
1675
869
    case LangOptions::TrivialAutoVarInitKind::Uninitialized:
1676
0
      llvm_unreachable("Uninitialized handled by caller");
1677
869
    case LangOptions::TrivialAutoVarInitKind::Zero:
1678
374
      emitStoresForZeroInit(CGM, D, Loc, isVolatile, Builder);
1679
374
      break;
1680
869
    case LangOptions::TrivialAutoVarInitKind::Pattern:
1681
495
      emitStoresForPatternInit(CGM, D, Loc, isVolatile, Builder);
1682
495
      break;
1683
869
    }
1684
869
    return;
1685
869
  }
1686
13
1687
13
  // VLAs look zero-sized to getTypeInfo. We can't emit constant stores to
1688
13
  // them, so emit a memcpy with the VLA size to initialize each element.
1689
13
  // Technically zero-sized or negative-sized VLAs are undefined, and UBSan
1690
13
  // will catch that code, but there exists code which generates zero-sized
1691
13
  // VLAs. Be nice and initialize whatever they requested.
1692
13
  const auto *VlaType = getContext().getAsVariableArrayType(type);
1693
13
  if (!VlaType)
1694
9
    return;
1695
4
  auto VlaSize = getVLASize(VlaType);
1696
4
  auto SizeVal = VlaSize.NumElts;
1697
4
  CharUnits EltSize = getContext().getTypeSizeInChars(VlaSize.Type);
1698
4
  switch (trivialAutoVarInit) {
1699
4
  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
1700
0
    llvm_unreachable("Uninitialized handled by caller");
1701
4
1702
4
  case LangOptions::TrivialAutoVarInitKind::Zero:
1703
2
    if (!EltSize.isOne())
1704
2
      SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1705
2
    Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1706
2
                         isVolatile);
1707
2
    break;
1708
4
1709
4
  case LangOptions::TrivialAutoVarInitKind::Pattern: {
1710
2
    llvm::Type *ElTy = Loc.getElementType();
1711
2
    llvm::Constant *Constant = constWithPadding(
1712
2
        CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy));
1713
2
    CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type);
1714
2
    llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop");
1715
2
    llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop");
1716
2
    llvm::BasicBlock *ContBB = createBasicBlock("vla-init.cont");
1717
2
    llvm::Value *IsZeroSizedVLA = Builder.CreateICmpEQ(
1718
2
        SizeVal, llvm::ConstantInt::get(SizeVal->getType(), 0),
1719
2
        "vla.iszerosized");
1720
2
    Builder.CreateCondBr(IsZeroSizedVLA, ContBB, SetupBB);
1721
2
    EmitBlock(SetupBB);
1722
2
    if (!EltSize.isOne())
1723
2
      SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1724
2
    llvm::Value *BaseSizeInChars =
1725
2
        llvm::ConstantInt::get(IntPtrTy, EltSize.getQuantity());
1726
2
    Address Begin = Builder.CreateElementBitCast(Loc, Int8Ty, "vla.begin");
1727
2
    llvm::Value *End =
1728
2
        Builder.CreateInBoundsGEP(Begin.getPointer(), SizeVal, "vla.end");
1729
2
    llvm::BasicBlock *OriginBB = Builder.GetInsertBlock();
1730
2
    EmitBlock(LoopBB);
1731
2
    llvm::PHINode *Cur = Builder.CreatePHI(Begin.getType(), 2, "vla.cur");
1732
2
    Cur->addIncoming(Begin.getPointer(), OriginBB);
1733
2
    CharUnits CurAlign = Loc.getAlignment().alignmentOfArrayElement(EltSize);
1734
2
    Builder.CreateMemCpy(Address(Cur, CurAlign),
1735
2
                         createUnnamedGlobalForMemcpyFrom(
1736
2
                             CGM, D, Builder, Constant, ConstantAlign),
1737
2
                         BaseSizeInChars, isVolatile);
1738
2
    llvm::Value *Next =
1739
2
        Builder.CreateInBoundsGEP(Int8Ty, Cur, BaseSizeInChars, "vla.next");
1740
2
    llvm::Value *Done = Builder.CreateICmpEQ(Next, End, "vla-init.isdone");
1741
2
    Builder.CreateCondBr(Done, ContBB, LoopBB);
1742
2
    Cur->addIncoming(Next, LoopBB);
1743
2
    EmitBlock(ContBB);
1744
2
  } break;
1745
4
  }
1746
4
}
1747
1748
428k
void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
1749
428k
  assert(emission.Variable && "emission was not valid!");
1750
428k
1751
428k
  // If this was emitted as a global constant, we're done.
1752
428k
  if (emission.wasEmittedAsGlobal()) 
return6
;
1753
428k
1754
428k
  const VarDecl &D = *emission.Variable;
1755
428k
  auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, D.getLocation());
1756
428k
  QualType type = D.getType();
1757
428k
1758
428k
  // If this local has an initializer, emit it now.
1759
428k
  const Expr *Init = D.getInit();
1760
428k
1761
428k
  // If we are at an unreachable point, we don't need to emit the initializer
1762
428k
  // unless it contains a label.
1763
428k
  if (!HaveInsertPoint()) {
1764
74
    if (!Init || 
!ContainsLabel(Init)59
) return;
1765
0
    EnsureInsertPoint();
1766
0
  }
1767
428k
1768
428k
  // Initialize the structure of a __block variable.
1769
428k
  
if (428k
emission.IsEscapingByRef428k
)
1770
326
    emitByrefStructureInit(emission);
1771
428k
1772
428k
  // Initialize the variable here if it doesn't have a initializer and it is a
1773
428k
  // C struct that is non-trivial to initialize or an array containing such a
1774
428k
  // struct.
1775
428k
  if (!Init &&
1776
428k
      type.isNonTrivialToPrimitiveDefaultInitialize() ==
1777
124k
          QualType::PDIK_Struct) {
1778
31
    LValue Dst = MakeAddrLValue(emission.getAllocatedAddress(), type);
1779
31
    if (emission.IsEscapingByRef)
1780
9
      drillIntoBlockVariable(*this, Dst, &D);
1781
31
    defaultInitNonTrivialCStructVar(Dst);
1782
31
    return;
1783
31
  }
1784
428k
1785
428k
  // Check whether this is a byref variable that's potentially
1786
428k
  // captured and moved by its own initializer.  If so, we'll need to
1787
428k
  // emit the initializer first, then copy into the variable.
1788
428k
  bool capturedByInit =
1789
428k
      Init && 
emission.IsEscapingByRef303k
&&
isCapturedBy(D, Init)215
;
1790
428k
1791
428k
  bool locIsByrefHeader = !capturedByInit;
1792
428k
  const Address Loc =
1793
428k
      locIsByrefHeader ? 
emission.getObjectAddress(*this)428k
:
emission.Addr6
;
1794
428k
1795
428k
  // Note: constexpr already initializes everything correctly.
1796
428k
  LangOptions::TrivialAutoVarInitKind trivialAutoVarInit =
1797
428k
      (D.isConstexpr()
1798
428k
           ? 
LangOptions::TrivialAutoVarInitKind::Uninitialized170
1799
428k
           : (D.getAttr<UninitializedAttr>()
1800
428k
                  ? 
LangOptions::TrivialAutoVarInitKind::Uninitialized3
1801
428k
                  : 
getContext().getLangOpts().getTrivialAutoVarInit()428k
));
1802
428k
1803
428k
  auto initializeWhatIsTechnicallyUninitialized = [&](Address Loc) {
1804
422k
    if (trivialAutoVarInit ==
1805
422k
        LangOptions::TrivialAutoVarInitKind::Uninitialized)
1806
421k
      return;
1807
882
1808
882
    // Only initialize a __block's storage: we always initialize the header.
1809
882
    if (emission.IsEscapingByRef && 
!locIsByrefHeader4
)
1810
2
      Loc = emitBlockByrefAddress(Loc, &D, /*follow=*/false);
1811
882
1812
882
    return emitZeroOrPatternForAutoVarInit(type, D, Loc);
1813
882
  };
1814
428k
1815
428k
  if (isTrivialInitializer(Init))
1816
127k
    return initializeWhatIsTechnicallyUninitialized(Loc);
1817
300k
1818
300k
  llvm::Constant *constant = nullptr;
1819
300k
  if (emission.IsConstantAggregate ||
1820
300k
      
D.mightBeUsableInConstantExpressions(getContext())296k
) {
1821
23.6k
    assert(!capturedByInit && "constant init contains a capturing block?");
1822
23.6k
    constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(D);
1823
23.6k
    if (constant && 
!constant->isZeroValue()5.64k
&&
1824
23.6k
        (trivialAutoVarInit !=
1825
4.57k
         LangOptions::TrivialAutoVarInitKind::Uninitialized)) {
1826
197
      IsPattern isPattern =
1827
197
          (trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Pattern)
1828
197
              ? 
IsPattern::Yes112
1829
197
              : 
IsPattern::No85
;
1830
197
      // C guarantees that brace-init with fewer initializers than members in
1831
197
      // the aggregate will initialize the rest of the aggregate as-if it were
1832
197
      // static initialization. In turn static initialization guarantees that
1833
197
      // padding is initialized to zero bits. We could instead pattern-init if D
1834
197
      // has any ImplicitValueInitExpr, but that seems to be unintuitive
1835
197
      // behavior.
1836
197
      constant = constWithPadding(CGM, IsPattern::No,
1837
197
                                  replaceUndef(CGM, isPattern, constant));
1838
197
    }
1839
23.6k
  }
1840
300k
1841
300k
  if (!constant) {
1842
294k
    initializeWhatIsTechnicallyUninitialized(Loc);
1843
294k
    LValue lv = MakeAddrLValue(Loc, type);
1844
294k
    lv.setNonGC(true);
1845
294k
    return EmitExprAsInit(Init, &D, lv, capturedByInit);
1846
294k
  }
1847
5.64k
1848
5.64k
  if (!emission.IsConstantAggregate) {
1849
1.09k
    // For simple scalar/complex initialization, store the value directly.
1850
1.09k
    LValue lv = MakeAddrLValue(Loc, type);
1851
1.09k
    lv.setNonGC(true);
1852
1.09k
    return EmitStoreThroughLValue(RValue::get(constant), lv, true);
1853
1.09k
  }
1854
4.55k
1855
4.55k
  llvm::Type *BP = CGM.Int8Ty->getPointerTo(Loc.getAddressSpace());
1856
4.55k
  emitStoresForConstant(
1857
4.55k
      CGM, D, (Loc.getType() == BP) ? 
Loc0
: Builder.CreateBitCast(Loc, BP),
1858
4.55k
      type.isVolatileQualified(), Builder, constant);
1859
4.55k
}
1860
1861
/// Emit an expression as an initializer for an object (variable, field, etc.)
1862
/// at the given location.  The expression is not necessarily the normal
1863
/// initializer for the object, and the address is not necessarily
1864
/// its normal location.
1865
///
1866
/// \param init the initializing expression
1867
/// \param D the object to act as if we're initializing
1868
/// \param loc the address to initialize; its type is a pointer
1869
///   to the LLVM mapping of the object's type
1870
/// \param alignment the alignment of the address
1871
/// \param capturedByInit true if \p D is a __block variable
1872
///   whose address is potentially changed by the initializer
1873
void CodeGenFunction::EmitExprAsInit(const Expr *init, const ValueDecl *D,
1874
317k
                                     LValue lvalue, bool capturedByInit) {
1875
317k
  QualType type = D->getType();
1876
317k
1877
317k
  if (type->isReferenceType()) {
1878
8.25k
    RValue rvalue = EmitReferenceBindingToExpr(init);
1879
8.25k
    if (capturedByInit)
1880
0
      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1881
8.25k
    EmitStoreThroughLValue(rvalue, lvalue, true);
1882
8.25k
    return;
1883
8.25k
  }
1884
309k
  switch (getEvaluationKind(type)) {
1885
309k
  case TEK_Scalar:
1886
270k
    EmitScalarInit(init, D, lvalue, capturedByInit);
1887
270k
    return;
1888
309k
  case TEK_Complex: {
1889
141
    ComplexPairTy complex = EmitComplexExpr(init);
1890
141
    if (capturedByInit)
1891
0
      drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1892
141
    EmitStoreOfComplex(complex, lvalue, /*init*/ true);
1893
141
    return;
1894
309k
  }
1895
309k
  case TEK_Aggregate:
1896
38.6k
    if (type->isAtomicType()) {
1897
4
      EmitAtomicInit(const_cast<Expr*>(init), lvalue);
1898
38.6k
    } else {
1899
38.6k
      AggValueSlot::Overlap_t Overlap = AggValueSlot::MayOverlap;
1900
38.6k
      if (isa<VarDecl>(D))
1901
38.6k
        Overlap = AggValueSlot::DoesNotOverlap;
1902
0
      else if (auto *FD = dyn_cast<FieldDecl>(D))
1903
0
        Overlap = getOverlapForFieldInit(FD);
1904
38.6k
      // TODO: how can we delay here if D is captured by its initializer?
1905
38.6k
      EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
1906
38.6k
                                              AggValueSlot::IsDestructed,
1907
38.6k
                                         AggValueSlot::DoesNotNeedGCBarriers,
1908
38.6k
                                              AggValueSlot::IsNotAliased,
1909
38.6k
                                              Overlap));
1910
38.6k
    }
1911
38.6k
    return;
1912
0
  }
1913
0
  llvm_unreachable("bad evaluation kind");
1914
0
}
1915
1916
/// Enter a destroy cleanup for the given local variable.
1917
void CodeGenFunction::emitAutoVarTypeCleanup(
1918
                            const CodeGenFunction::AutoVarEmission &emission,
1919
18.5k
                            QualType::DestructionKind dtorKind) {
1920
18.5k
  assert(dtorKind != QualType::DK_none);
1921
18.5k
1922
18.5k
  // Note that for __block variables, we want to destroy the
1923
18.5k
  // original stack object, not the possibly forwarded object.
1924
18.5k
  Address addr = emission.getObjectAddress(*this);
1925
18.5k
1926
18.5k
  const VarDecl *var = emission.Variable;
1927
18.5k
  QualType type = var->getType();
1928
18.5k
1929
18.5k
  CleanupKind cleanupKind = NormalAndEHCleanup;
1930
18.5k
  CodeGenFunction::Destroyer *destroyer = nullptr;
1931
18.5k
1932
18.5k
  switch (dtorKind) {
1933
18.5k
  case QualType::DK_none:
1934
0
    llvm_unreachable("no cleanup for trivially-destructible variable");
1935
18.5k
1936
18.5k
  case QualType::DK_cxx_destructor:
1937
17.8k
    // If there's an NRVO flag on the emission, we need a different
1938
17.8k
    // cleanup.
1939
17.8k
    if (emission.NRVOFlag) {
1940
374
      assert(!type->isArrayType());
1941
374
      CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
1942
374
      EHStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr, type, dtor,
1943
374
                                                  emission.NRVOFlag);
1944
374
      return;
1945
374
    }
1946
17.5k
    break;
1947
17.5k
1948
17.5k
  case QualType::DK_objc_strong_lifetime:
1949
479
    // Suppress cleanups for pseudo-strong variables.
1950
479
    if (var->isARCPseudoStrong()) 
return13
;
1951
466
1952
466
    // Otherwise, consider whether to use an EH cleanup or not.
1953
466
    cleanupKind = getARCCleanupKind();
1954
466
1955
466
    // Use the imprecise destroyer by default.
1956
466
    if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
1957
461
      destroyer = CodeGenFunction::destroyARCStrongImprecise;
1958
466
    break;
1959
466
1960
466
  case QualType::DK_objc_weak_lifetime:
1961
168
    break;
1962
466
1963
466
  case QualType::DK_nontrivial_c_struct:
1964
56
    destroyer = CodeGenFunction::destroyNonTrivialCStruct;
1965
56
    if (emission.NRVOFlag) {
1966
3
      assert(!type->isArrayType());
1967
3
      EHStack.pushCleanup<DestroyNRVOVariableC>(cleanupKind, addr,
1968
3
                                                emission.NRVOFlag, type);
1969
3
      return;
1970
3
    }
1971
53
    break;
1972
18.1k
  }
1973
18.1k
1974
18.1k
  // If we haven't chosen a more specific destroyer, use the default.
1975
18.1k
  if (!destroyer) 
destroyer = getDestroyer(dtorKind)17.6k
;
1976
18.1k
1977
18.1k
  // Use an EH cleanup in array destructors iff the destructor itself
1978
18.1k
  // is being pushed as an EH cleanup.
1979
18.1k
  bool useEHCleanup = (cleanupKind & EHCleanup);
1980
18.1k
  EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
1981
18.1k
                                     useEHCleanup);
1982
18.1k
}
1983
1984
440k
void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
1985
440k
  assert(emission.Variable && "emission was not valid!");
1986
440k
1987
440k
  // If this was emitted as a global constant, we're done.
1988
440k
  if (emission.wasEmittedAsGlobal()) 
return6
;
1989
440k
1990
440k
  // If we don't have an insertion point, we're done.  Sema prevents
1991
440k
  // us from jumping into any of these scopes anyway.
1992
440k
  if (!HaveInsertPoint()) 
return74
;
1993
440k
1994
440k
  const VarDecl &D = *emission.Variable;
1995
440k
1996
440k
  // Check the type for a cleanup.
1997
440k
  if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
1998
18.5k
    emitAutoVarTypeCleanup(emission, dtorKind);
1999
440k
2000
440k
  // In GC mode, honor objc_precise_lifetime.
2001
440k
  if (getLangOpts().getGC() != LangOptions::NonGC &&
2002
440k
      
D.hasAttr<ObjCPreciseLifetimeAttr>()126
) {
2003
1
    EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
2004
1
  }
2005
440k
2006
440k
  // Handle the cleanup attribute.
2007
440k
  if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
2008
9
    const FunctionDecl *FD = CA->getFunctionDecl();
2009
9
2010
9
    llvm::Constant *F = CGM.GetAddrOfFunction(FD);
2011
9
    assert(F && "Could not find function!");
2012
9
2013
9
    const CGFunctionInfo &Info = CGM.getTypes().arrangeFunctionDeclaration(FD);
2014
9
    EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
2015
9
  }
2016
440k
2017
440k
  // If this is a block variable, call _Block_object_destroy
2018
440k
  // (on the unforwarded address). Don't enter this cleanup if we're in pure-GC
2019
440k
  // mode.
2020
440k
  if (emission.IsEscapingByRef &&
2021
440k
      
CGM.getLangOpts().getGC() != LangOptions::GCOnly326
) {
2022
326
    BlockFieldFlags Flags = BLOCK_FIELD_IS_BYREF;
2023
326
    if (emission.Variable->getType().isObjCGCWeak())
2024
7
      Flags |= BLOCK_FIELD_IS_WEAK;
2025
326
    enterByrefCleanup(NormalAndEHCleanup, emission.Addr, Flags,
2026
326
                      /*LoadBlockVarAddr*/ false,
2027
326
                      cxxDestructorCanThrow(emission.Variable->getType()));
2028
326
  }
2029
440k
}
2030
2031
CodeGenFunction::Destroyer *
2032
25.0k
CodeGenFunction::getDestroyer(QualType::DestructionKind kind) {
2033
25.0k
  switch (kind) {
2034
25.0k
  
case QualType::DK_none: 0
llvm_unreachable0
("no destroyer for trivial dtor");
2035
25.0k
  case QualType::DK_cxx_destructor:
2036
24.4k
    return destroyCXXObject;
2037
25.0k
  case QualType::DK_objc_strong_lifetime:
2038
46
    return destroyARCStrongPrecise;
2039
25.0k
  case QualType::DK_objc_weak_lifetime:
2040
496
    return destroyARCWeak;
2041
25.0k
  case QualType::DK_nontrivial_c_struct:
2042
35
    return destroyNonTrivialCStruct;
2043
0
  }
2044
0
  llvm_unreachable("Unknown DestructionKind");
2045
0
}
2046
2047
/// pushEHDestroy - Push the standard destructor for the given type as
2048
/// an EH-only cleanup.
2049
void CodeGenFunction::pushEHDestroy(QualType::DestructionKind dtorKind,
2050
2.39k
                                    Address addr, QualType type) {
2051
2.39k
  assert(dtorKind && "cannot push destructor for trivial type");
2052
2.39k
  assert(needsEHCleanup(dtorKind));
2053
2.39k
2054
2.39k
  pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
2055
2.39k
}
2056
2057
/// pushDestroy - Push the standard destructor for the given type as
2058
/// at least a normal cleanup.
2059
void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind,
2060
212
                                  Address addr, QualType type) {
2061
212
  assert(dtorKind && "cannot push destructor for trivial type");
2062
212
2063
212
  CleanupKind cleanupKind = getCleanupKind(dtorKind);
2064
212
  pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
2065
212
              cleanupKind & EHCleanup);
2066
212
}
2067
2068
void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, Address addr,
2069
                                  QualType type, Destroyer *destroyer,
2070
10.4k
                                  bool useEHCleanupForArray) {
2071
10.4k
  pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
2072
10.4k
                                     destroyer, useEHCleanupForArray);
2073
10.4k
}
2074
2075
1.24k
void CodeGenFunction::pushStackRestore(CleanupKind Kind, Address SPMem) {
2076
1.24k
  EHStack.pushCleanup<CallStackRestore>(Kind, SPMem);
2077
1.24k
}
2078
2079
void CodeGenFunction::pushLifetimeExtendedDestroy(
2080
    CleanupKind cleanupKind, Address addr, QualType type,
2081
144
    Destroyer *destroyer, bool useEHCleanupForArray) {
2082
144
  // Push an EH-only cleanup for the object now.
2083
144
  // FIXME: When popping normal cleanups, we need to keep this EH cleanup
2084
144
  // around in case a temporary's destructor throws an exception.
2085
144
  if (cleanupKind & EHCleanup)
2086
141
    EHStack.pushCleanup<DestroyObject>(
2087
141
        static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type,
2088
141
        destroyer, useEHCleanupForArray);
2089
144
2090
144
  // Remember that we need to push a full cleanup for the object at the
2091
144
  // end of the full-expression.
2092
144
  pushCleanupAfterFullExpr<DestroyObject>(
2093
144
      cleanupKind, addr, type, destroyer, useEHCleanupForArray);
2094
144
}
2095
2096
/// emitDestroy - Immediately perform the destruction of the given
2097
/// object.
2098
///
2099
/// \param addr - the address of the object; a type*
2100
/// \param type - the type of the object; if an array type, all
2101
///   objects are destroyed in reverse order
2102
/// \param destroyer - the function to call to destroy individual
2103
///   elements
2104
/// \param useEHCleanupForArray - whether an EH cleanup should be
2105
///   used when destroying array elements, in case one of the
2106
///   destructions throws an exception
2107
void CodeGenFunction::emitDestroy(Address addr, QualType type,
2108
                                  Destroyer *destroyer,
2109
38.0k
                                  bool useEHCleanupForArray) {
2110
38.0k
  const ArrayType *arrayType = getContext().getAsArrayType(type);
2111
38.0k
  if (!arrayType)
2112
35.6k
    return destroyer(*this, addr, type);
2113
2.34k
2114
2.34k
  llvm::Value *length = emitArrayLength(arrayType, type, addr);
2115
2.34k
2116
2.34k
  CharUnits elementAlign =
2117
2.34k
    addr.getAlignment()
2118
2.34k
        .alignmentOfArrayElement(getContext().getTypeSizeInChars(type));
2119
2.34k
2120
2.34k
  // Normally we have to check whether the array is zero-length.
2121
2.34k
  bool checkZeroLength = true;
2122
2.34k
2123
2.34k
  // But if the array length is constant, we can suppress that.
2124
2.34k
  if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
2125
2.31k
    // ...and if it's constant zero, we can just skip the entire thing.
2126
2.31k
    if (constLength->isZero()) 
return0
;
2127
2.31k
    checkZeroLength = false;
2128
2.31k
  }
2129
2.34k
2130
2.34k
  llvm::Value *begin = addr.getPointer();
2131
2.34k
  llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
2132
2.34k
  emitArrayDestroy(begin, end, type, elementAlign, destroyer,
2133
2.34k
                   checkZeroLength, useEHCleanupForArray);
2134
2.34k
}
2135
2136
/// emitArrayDestroy - Destroys all the elements of the given array,
2137
/// beginning from last to first.  The array cannot be zero-length.
2138
///
2139
/// \param begin - a type* denoting the first element of the array
2140
/// \param end - a type* denoting one past the end of the array
2141
/// \param elementType - the element type of the array
2142
/// \param destroyer - the function to call to destroy elements
2143
/// \param useEHCleanup - whether to push an EH cleanup to destroy
2144
///   the remaining elements in case the destruction of a single
2145
///   element throws
2146
void CodeGenFunction::emitArrayDestroy(llvm::Value *begin,
2147
                                       llvm::Value *end,
2148
                                       QualType elementType,
2149
                                       CharUnits elementAlign,
2150
                                       Destroyer *destroyer,
2151
                                       bool checkZeroLength,
2152
2.60k
                                       bool useEHCleanup) {
2153
2.60k
  assert(!elementType->isArrayType());
2154
2.60k
2155
2.60k
  // The basic structure here is a do-while loop, because we don't
2156
2.60k
  // need to check for the zero-element case.
2157
2.60k
  llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
2158
2.60k
  llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
2159
2.60k
2160
2.60k
  if (checkZeroLength) {
2161
291
    llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
2162
291
                                                "arraydestroy.isempty");
2163
291
    Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
2164
291
  }
2165
2.60k
2166
2.60k
  // Enter the loop body, making that address the current address.
2167
2.60k
  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
2168
2.60k
  EmitBlock(bodyBB);
2169
2.60k
  llvm::PHINode *elementPast =
2170
2.60k
    Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
2171
2.60k
  elementPast->addIncoming(end, entryBB);
2172
2.60k
2173
2.60k
  // Shift the address back by one element.
2174
2.60k
  llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
2175
2.60k
  llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
2176
2.60k
                                                   "arraydestroy.element");
2177
2.60k
2178
2.60k
  if (useEHCleanup)
2179
1.77k
    pushRegularPartialArrayCleanup(begin, element, elementType, elementAlign,
2180
1.77k
                                   destroyer);
2181
2.60k
2182
2.60k
  // Perform the actual destruction there.
2183
2.60k
  destroyer(*this, Address(element, elementAlign), elementType);
2184
2.60k
2185
2.60k
  if (useEHCleanup)
2186
1.77k
    PopCleanupBlock();
2187
2.60k
2188
2.60k
  // Check whether we've reached the end.
2189
2.60k
  llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
2190
2.60k
  Builder.CreateCondBr(done, doneBB, bodyBB);
2191
2.60k
  elementPast->addIncoming(element, Builder.GetInsertBlock());
2192
2.60k
2193
2.60k
  // Done.
2194
2.60k
  EmitBlock(doneBB);
2195
2.60k
}
2196
2197
/// Perform partial array destruction as if in an EH cleanup.  Unlike
2198
/// emitArrayDestroy, the element type here may still be an array type.
2199
static void emitPartialArrayDestroy(CodeGenFunction &CGF,
2200
                                    llvm::Value *begin, llvm::Value *end,
2201
                                    QualType type, CharUnits elementAlign,
2202
214
                                    CodeGenFunction::Destroyer *destroyer) {
2203
214
  // If the element type is itself an array, drill down.
2204
214
  unsigned arrayDepth = 0;
2205
227
  while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
2206
13
    // VLAs don't require a GEP index to walk into.
2207
13
    if (!isa<VariableArrayType>(arrayType))
2208
13
      arrayDepth++;
2209
13
    type = arrayType->getElementType();
2210
13
  }
2211
214
2212
214
  if (arrayDepth) {
2213
13
    llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
2214
13
2215
13
    SmallVector<llvm::Value*,4> gepIndices(arrayDepth+1, zero);
2216
13
    begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
2217
13
    end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
2218
13
  }
2219
214
2220
214
  // Destroy the array.  We don't ever need an EH cleanup because we
2221
214
  // assume that we're in an EH cleanup ourselves, so a throwing
2222
214
  // destructor causes an immediate terminate.
2223
214
  CGF.emitArrayDestroy(begin, end, type, elementAlign, destroyer,
2224
214
                       /*checkZeroLength*/ true, /*useEHCleanup*/ false);
2225
214
}
2226
2227
namespace {
2228
  /// RegularPartialArrayDestroy - a cleanup which performs a partial
2229
  /// array destroy where the end pointer is regularly determined and
2230
  /// does not need to be loaded from a local.
2231
  class RegularPartialArrayDestroy final : public EHScopeStack::Cleanup {
2232
    llvm::Value *ArrayBegin;
2233
    llvm::Value *ArrayEnd;
2234
    QualType ElementType;
2235
    CodeGenFunction::Destroyer *Destroyer;
2236
    CharUnits ElementAlign;
2237
  public:
2238
    RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
2239
                               QualType elementType, CharUnits elementAlign,
2240
                               CodeGenFunction::Destroyer *destroyer)
2241
      : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
2242
        ElementType(elementType), Destroyer(destroyer),
2243
1.82k
        ElementAlign(elementAlign) {}
2244
2245
56
    void Emit(CodeGenFunction &CGF, Flags flags) override {
2246
56
      emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
2247
56
                              ElementType, ElementAlign, Destroyer);
2248
56
    }
2249
  };
2250
2251
  /// IrregularPartialArrayDestroy - a cleanup which performs a
2252
  /// partial array destroy where the end pointer is irregularly
2253
  /// determined and must be loaded from a local.
2254
  class IrregularPartialArrayDestroy final : public EHScopeStack::Cleanup {
2255
    llvm::Value *ArrayBegin;
2256
    Address ArrayEndPointer;
2257
    QualType ElementType;
2258
    CodeGenFunction::Destroyer *Destroyer;
2259
    CharUnits ElementAlign;
2260
  public:
2261
    IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
2262
                                 Address arrayEndPointer,
2263
                                 QualType elementType,
2264
                                 CharUnits elementAlign,
2265
                                 CodeGenFunction::Destroyer *destroyer)
2266
      : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
2267
        ElementType(elementType), Destroyer(destroyer),
2268
158
        ElementAlign(elementAlign) {}
2269
2270
158
    void Emit(CodeGenFunction &CGF, Flags flags) override {
2271
158
      llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
2272
158
      emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
2273
158
                              ElementType, ElementAlign, Destroyer);
2274
158
    }
2275
  };
2276
} // end anonymous namespace
2277
2278
/// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
2279
/// already-constructed elements of the given array.  The cleanup
2280
/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
2281
///
2282
/// \param elementType - the immediate element type of the array;
2283
///   possibly still an array type
2284
void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
2285
                                                       Address arrayEndPointer,
2286
                                                       QualType elementType,
2287
                                                       CharUnits elementAlign,
2288
158
                                                       Destroyer *destroyer) {
2289
158
  pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
2290
158
                                                    arrayBegin, arrayEndPointer,
2291
158
                                                    elementType, elementAlign,
2292
158
                                                    destroyer);
2293
158
}
2294
2295
/// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
2296
/// already-constructed elements of the given array.  The cleanup
2297
/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
2298
///
2299
/// \param elementType - the immediate element type of the array;
2300
///   possibly still an array type
2301
void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
2302
                                                     llvm::Value *arrayEnd,
2303
                                                     QualType elementType,
2304
                                                     CharUnits elementAlign,
2305
1.82k
                                                     Destroyer *destroyer) {
2306
1.82k
  pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
2307
1.82k
                                                  arrayBegin, arrayEnd,
2308
1.82k
                                                  elementType, elementAlign,
2309
1.82k
                                                  destroyer);
2310
1.82k
}
2311
2312
/// Lazily declare the @llvm.lifetime.start intrinsic.
2313
364k
llvm::Function *CodeGenModule::getLLVMLifetimeStartFn() {
2314
364k
  if (LifetimeStartFn)
2315
358k
    return LifetimeStartFn;
2316
6.10k
  LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(),
2317
6.10k
    llvm::Intrinsic::lifetime_start, AllocaInt8PtrTy);
2318
6.10k
  return LifetimeStartFn;
2319
6.10k
}
2320
2321
/// Lazily declare the @llvm.lifetime.end intrinsic.
2322
381k
llvm::Function *CodeGenModule::getLLVMLifetimeEndFn() {
2323
381k
  if (LifetimeEndFn)
2324
375k
    return LifetimeEndFn;
2325
6.09k
  LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(),
2326
6.09k
    llvm::Intrinsic::lifetime_end, AllocaInt8PtrTy);
2327
6.09k
  return LifetimeEndFn;
2328
6.09k
}
2329
2330
namespace {
2331
  /// A cleanup to perform a release of an object at the end of a
2332
  /// function.  This is used to balance out the incoming +1 of a
2333
  /// ns_consumed argument when we can't reasonably do that just by
2334
  /// not doing the initial retain for a __block argument.
2335
  struct ConsumeARCParameter final : EHScopeStack::Cleanup {
2336
    ConsumeARCParameter(llvm::Value *param,
2337
                        ARCPreciseLifetime_t precise)
2338
0
      : Param(param), Precise(precise) {}
2339
2340
    llvm::Value *Param;
2341
    ARCPreciseLifetime_t Precise;
2342
2343
0
    void Emit(CodeGenFunction &CGF, Flags flags) override {
2344
0
      CGF.EmitARCRelease(Param, Precise);
2345
0
    }
2346
  };
2347
} // end anonymous namespace
2348
2349
/// Emit an alloca (or GlobalValue depending on target)
2350
/// for the specified parameter and set up LocalDeclMap.
2351
void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg,
2352
657k
                                   unsigned ArgNo) {
2353
657k
  // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
2354
657k
  assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
2355
657k
         "Invalid argument to EmitParmDecl");
2356
657k
2357
657k
  Arg.getAnyValue()->setName(D.getName());
2358
657k
2359
657k
  QualType Ty = D.getType();
2360
657k
2361
657k
  // Use better IR generation for certain implicit parameters.
2362
657k
  if (auto IPD = dyn_cast<ImplicitParamDecl>(&D)) {
2363
230k
    // The only implicit argument a block has is its literal.
2364
230k
    // This may be passed as an inalloca'ed value on Windows x86.
2365
230k
    if (BlockInfo) {
2366
1.20k
      llvm::Value *V = Arg.isIndirect()
2367
1.20k
                           ? 
Builder.CreateLoad(Arg.getIndirectAddress())1
2368
1.20k
                           : 
Arg.getDirectValue()1.20k
;
2369
1.20k
      setBlockContextParameter(IPD, ArgNo, V);
2370
1.20k
      return;
2371
1.20k
    }
2372
656k
  }
2373
656k
2374
656k
  Address DeclPtr = Address::invalid();
2375
656k
  bool DoStore = false;
2376
656k
  bool IsScalar = hasScalarEvaluationKind(Ty);
2377
656k
  // If we already have a pointer to the argument, reuse the input pointer.
2378
656k
  if (Arg.isIndirect()) {
2379
14.6k
    DeclPtr = Arg.getIndirectAddress();
2380
14.6k
    // If we have a prettier pointer type at this point, bitcast to that.
2381
14.6k
    unsigned AS = DeclPtr.getType()->getAddressSpace();
2382
14.6k
    llvm::Type *IRTy = ConvertTypeForMem(Ty)->getPointerTo(AS);
2383
14.6k
    if (DeclPtr.getType() != IRTy)
2384
2
      DeclPtr = Builder.CreateBitCast(DeclPtr, IRTy, D.getName());
2385
14.6k
    // Indirect argument is in alloca address space, which may be different
2386
14.6k
    // from the default address space.
2387
14.6k
    auto AllocaAS = CGM.getASTAllocaAddressSpace();
2388
14.6k
    auto *V = DeclPtr.getPointer();
2389
14.6k
    auto SrcLangAS = getLangOpts().OpenCL ? 
LangAS::opencl_private141
:
AllocaAS14.4k
;
2390
14.6k
    auto DestLangAS =
2391
14.6k
        getLangOpts().OpenCL ? 
LangAS::opencl_private141
:
LangAS::Default14.4k
;
2392
14.6k
    if (SrcLangAS != DestLangAS) {
2393
7
      assert(getContext().getTargetAddressSpace(SrcLangAS) ==
2394
7
             CGM.getDataLayout().getAllocaAddrSpace());
2395
7
      auto DestAS = getContext().getTargetAddressSpace(DestLangAS);
2396
7
      auto *T = V->getType()->getPointerElementType()->getPointerTo(DestAS);
2397
7
      DeclPtr = Address(getTargetHooks().performAddrSpaceCast(
2398
7
                            *this, V, SrcLangAS, DestLangAS, T, true),
2399
7
                        DeclPtr.getAlignment());
2400
7
    }
2401
14.6k
2402
14.6k
    // Push a destructor cleanup for this parameter if the ABI requires it.
2403
14.6k
    // Don't push a cleanup in a thunk for a method that will also emit a
2404
14.6k
    // cleanup.
2405
14.6k
    if (hasAggregateEvaluationKind(Ty) && 
!CurFuncIsThunk13.9k
&&
2406
14.6k
        
Ty->getAs<RecordType>()->getDecl()->isParamDestroyedInCallee()13.8k
) {
2407
359
      if (QualType::DestructionKind DtorKind = Ty.isDestructedType()) {
2408
110
        assert((DtorKind == QualType::DK_cxx_destructor ||
2409
110
                DtorKind == QualType::DK_nontrivial_c_struct) &&
2410
110
               "unexpected destructor type");
2411
110
        pushDestroy(DtorKind, DeclPtr, Ty);
2412
110
        CalleeDestructedParamCleanups[cast<ParmVarDecl>(&D)] =
2413
110
            EHStack.stable_begin();
2414
110
      }
2415
359
    }
2416
641k
  } else {
2417
641k
    // Check if the parameter address is controlled by OpenMP runtime.
2418
641k
    Address OpenMPLocalAddr =
2419
641k
        getLangOpts().OpenMP
2420
641k
            ? 
CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D)72.5k
2421
641k
            : 
Address::invalid()569k
;
2422
641k
    if (getLangOpts().OpenMP && 
OpenMPLocalAddr.isValid()72.5k
) {
2423
11
      DeclPtr = OpenMPLocalAddr;
2424
641k
    } else {
2425
641k
      // Otherwise, create a temporary to hold the value.
2426
641k
      DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D),
2427
641k
                              D.getName() + ".addr");
2428
641k
    }
2429
641k
    DoStore = true;
2430
641k
  }
2431
656k
2432
656k
  llvm::Value *ArgVal = (DoStore ? 
Arg.getDirectValue()641k
:
nullptr14.6k
);
2433
656k
2434
656k
  LValue lv = MakeAddrLValue(DeclPtr, Ty);
2435
656k
  if (IsScalar) {
2436
641k
    Qualifiers qs = Ty.getQualifiers();
2437
641k
    if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
2438
466
      // We honor __attribute__((ns_consumed)) for types with lifetime.
2439
466
      // For __strong, it's handled by just skipping the initial retain;
2440
466
      // otherwise we have to balance out the initial +1 with an extra
2441
466
      // cleanup to do the release at the end of the function.
2442
466
      bool isConsumed = D.hasAttr<NSConsumedAttr>();
2443
466
2444
466
      // If a parameter is pseudo-strong then we can omit the implicit retain.
2445
466
      if (D.isARCPseudoStrong()) {
2446
231
        assert(lt == Qualifiers::OCL_Strong &&
2447
231
               "pseudo-strong variable isn't strong?");
2448
231
        assert(qs.hasConst() && "pseudo-strong variable should be const!");
2449
231
        lt = Qualifiers::OCL_ExplicitNone;
2450
231
      }
2451
466
2452
466
      // Load objects passed indirectly.
2453
466
      if (Arg.isIndirect() && 
!ArgVal8
)
2454
8
        ArgVal = Builder.CreateLoad(DeclPtr);
2455
466
2456
466
      if (lt == Qualifiers::OCL_Strong) {
2457
224
        if (!isConsumed) {
2458
194
          if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2459
142
            // use objc_storeStrong(&dest, value) for retaining the
2460
142
            // object. But first, store a null into 'dest' because
2461
142
            // objc_storeStrong attempts to release its old value.
2462
142
            llvm::Value *Null = CGM.EmitNullConstant(D.getType());
2463
142
            EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
2464
142
            EmitARCStoreStrongCall(lv.getAddress(), ArgVal, true);
2465
142
            DoStore = false;
2466
142
          }
2467
52
          else
2468
52
          // Don't use objc_retainBlock for block pointers, because we
2469
52
          // don't want to Block_copy something just because we got it
2470
52
          // as a parameter.
2471
52
            ArgVal = EmitARCRetainNonBlock(ArgVal);
2472
194
        }
2473
242
      } else {
2474
242
        // Push the cleanup for a consumed parameter.
2475
242
        if (isConsumed) {
2476
0
          ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
2477
0
                                ? ARCPreciseLifetime : ARCImpreciseLifetime);
2478
0
          EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal,
2479
0
                                                   precise);
2480
0
        }
2481
242
2482
242
        if (lt == Qualifiers::OCL_Weak) {
2483
7
          EmitARCInitWeak(DeclPtr, ArgVal);
2484
7
          DoStore = false; // The weak init is a store, no need to do two.
2485
7
        }
2486
242
      }
2487
466
2488
466
      // Enter the cleanup scope.
2489
466
      EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
2490
466
    }
2491
641k
  }
2492
656k
2493
656k
  // Store the initial value into the alloca.
2494
656k
  if (DoStore)
2495
641k
    EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);
2496
656k
2497
656k
  setAddrOfLocalVar(&D, DeclPtr);
2498
656k
2499
656k
  // Emit debug info for param declaration.
2500
656k
  if (CGDebugInfo *DI = getDebugInfo()) {
2501
209k
    if (CGM.getCodeGenOpts().getDebugInfo() >=
2502
209k
        codegenoptions::LimitedDebugInfo) {
2503
2.65k
      DI->EmitDeclareOfArgVariable(&D, DeclPtr.getPointer(), ArgNo, Builder);
2504
2.65k
    }
2505
209k
  }
2506
656k
2507
656k
  if (D.hasAttr<AnnotateAttr>())
2508
3
    EmitVarAnnotations(&D, DeclPtr.getPointer());
2509
656k
2510
656k
  // We can only check return value nullability if all arguments to the
2511
656k
  // function satisfy their nullability preconditions. This makes it necessary
2512
656k
  // to emit null checks for args in the function body itself.
2513
656k
  if (requiresReturnValueNullabilityCheck()) {
2514
0
    auto Nullability = Ty->getNullability(getContext());
2515
0
    if (Nullability && *Nullability == NullabilityKind::NonNull) {
2516
0
      SanitizerScope SanScope(this);
2517
0
      RetValNullabilityPrecondition =
2518
0
          Builder.CreateAnd(RetValNullabilityPrecondition,
2519
0
                            Builder.CreateIsNotNull(Arg.getAnyValue()));
2520
0
    }
2521
0
  }
2522
656k
}
2523
2524
void CodeGenModule::EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D,
2525
140
                                            CodeGenFunction *CGF) {
2526
140
  if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && 
!D->isUsed()72
))
2527
72
    return;
2528
68
  getOpenMPRuntime().emitUserDefinedReduction(CGF, D);
2529
68
}
2530
2531
void CodeGenModule::EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D,
2532
8
                                            CodeGenFunction *CGF) {
2533
8
  if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed()))
2534
8
    return;
2535
8
  // FIXME: need to implement mapper code generation
2536
8
}
2537
2538
19
void CodeGenModule::EmitOMPRequiresDecl(const OMPRequiresDecl *D) {
2539
19
  getOpenMPRuntime().checkArchForUnifiedAddressing(D);
2540
19
}