Coverage Report

Created: 2020-02-25 14:32

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