Coverage Report

Created: 2022-01-25 06:29

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