Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/CodeGen/CGDeclCXX.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CGDeclCXX.cpp - Emit LLVM Code for C++ 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 dealing with code generation of C++ declarations
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CodeGenFunction.h"
14
#include "CGCXXABI.h"
15
#include "CGObjCRuntime.h"
16
#include "CGOpenMPRuntime.h"
17
#include "TargetInfo.h"
18
#include "clang/Basic/CodeGenOptions.h"
19
#include "llvm/ADT/StringExtras.h"
20
#include "llvm/IR/Intrinsics.h"
21
#include "llvm/IR/MDBuilder.h"
22
#include "llvm/Support/Path.h"
23
24
using namespace clang;
25
using namespace CodeGen;
26
27
static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
28
4.85k
                         ConstantAddress DeclPtr) {
29
4.85k
  assert(
30
4.85k
      (D.hasGlobalStorage() ||
31
4.85k
       (D.hasLocalStorage() && CGF.getContext().getLangOpts().OpenCLCPlusPlus)) &&
32
4.85k
      "VarDecl must have global or local (in the case of OpenCL) storage!");
33
4.85k
  assert(!D.getType()->isReferenceType() &&
34
4.85k
         "Should not call EmitDeclInit on a reference!");
35
4.85k
36
4.85k
  QualType type = D.getType();
37
4.85k
  LValue lv = CGF.MakeAddrLValue(DeclPtr, type);
38
4.85k
39
4.85k
  const Expr *Init = D.getInit();
40
4.85k
  switch (CGF.getEvaluationKind(type)) {
41
4.85k
  case TEK_Scalar: {
42
992
    CodeGenModule &CGM = CGF.CGM;
43
992
    if (lv.isObjCStrong())
44
3
      CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
45
3
                                                DeclPtr, D.getTLSKind());
46
989
    else if (lv.isObjCWeak())
47
2
      CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
48
2
                                              DeclPtr);
49
987
    else
50
987
      CGF.EmitScalarInit(Init, &D, lv, false);
51
992
    return;
52
4.85k
  }
53
4.85k
  case TEK_Complex:
54
1
    CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
55
1
    return;
56
4.85k
  case TEK_Aggregate:
57
3.86k
    CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
58
3.86k
                                          AggValueSlot::DoesNotNeedGCBarriers,
59
3.86k
                                                  AggValueSlot::IsNotAliased,
60
3.86k
                                                  AggValueSlot::DoesNotOverlap));
61
3.86k
    return;
62
0
  }
63
0
  llvm_unreachable("bad evaluation kind");
64
0
}
65
66
/// Emit code to cause the destruction of the given variable with
67
/// static storage duration.
68
static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
69
4.93k
                            ConstantAddress Addr) {
70
4.93k
  // Honor __attribute__((no_destroy)) and bail instead of attempting
71
4.93k
  // to emit a reference to a possibly nonexistent destructor, which
72
4.93k
  // in turn can cause a crash. This will result in a global constructor
73
4.93k
  // that isn't balanced out by a destructor call as intended by the
74
4.93k
  // attribute. This also checks for -fno-c++-static-destructors and
75
4.93k
  // bails even if the attribute is not present.
76
4.93k
  if (D.isNoDestroy(CGF.getContext()))
77
20
    return;
78
4.91k
79
4.91k
  CodeGenModule &CGM = CGF.CGM;
80
4.91k
81
4.91k
  // FIXME:  __attribute__((cleanup)) ?
82
4.91k
83
4.91k
  QualType Type = D.getType();
84
4.91k
  QualType::DestructionKind DtorKind = Type.isDestructedType();
85
4.91k
86
4.91k
  switch (DtorKind) {
87
4.91k
  case QualType::DK_none:
88
1.75k
    return;
89
4.91k
90
4.91k
  case QualType::DK_cxx_destructor:
91
3.15k
    break;
92
4.91k
93
4.91k
  case QualType::DK_objc_strong_lifetime:
94
6
  case QualType::DK_objc_weak_lifetime:
95
6
  case QualType::DK_nontrivial_c_struct:
96
6
    // We don't care about releasing objects during process teardown.
97
6
    assert(!D.getTLSKind() && "should have rejected this");
98
6
    return;
99
3.15k
  }
100
3.15k
101
3.15k
  llvm::FunctionCallee Func;
102
3.15k
  llvm::Constant *Argument;
103
3.15k
104
3.15k
  // Special-case non-array C++ destructors, if they have the right signature.
105
3.15k
  // Under some ABIs, destructors return this instead of void, and cannot be
106
3.15k
  // passed directly to __cxa_atexit if the target does not allow this
107
3.15k
  // mismatch.
108
3.15k
  const CXXRecordDecl *Record = Type->getAsCXXRecordDecl();
109
3.15k
  bool CanRegisterDestructor =
110
3.15k
      Record && 
(2.83k
!CGM.getCXXABI().HasThisReturn(
111
2.83k
                     GlobalDecl(Record->getDestructor(), Dtor_Complete)) ||
112
2.83k
                 
CGM.getCXXABI().canCallMismatchedFunctionType()220
);
113
3.15k
  // If __cxa_atexit is disabled via a flag, a different helper function is
114
3.15k
  // generated elsewhere which uses atexit instead, and it takes the destructor
115
3.15k
  // directly.
116
3.15k
  bool UsingExternalHelper = !CGM.getCodeGenOpts().CXAAtExit;
117
3.15k
  if (Record && 
(2.83k
CanRegisterDestructor2.83k
||
UsingExternalHelper1
)) {
118
2.83k
    assert(!Record->hasTrivialDestructor());
119
2.83k
    CXXDestructorDecl *Dtor = Record->getDestructor();
120
2.83k
121
2.83k
    Func = CGM.getAddrAndTypeOfCXXStructor(GlobalDecl(Dtor, Dtor_Complete));
122
2.83k
    if (CGF.getContext().getLangOpts().OpenCL) {
123
7
      auto DestAS =
124
7
          CGM.getTargetCodeGenInfo().getAddrSpaceOfCxaAtexitPtrParam();
125
7
      auto DestTy = CGF.getTypes().ConvertType(Type)->getPointerTo(
126
7
          CGM.getContext().getTargetAddressSpace(DestAS));
127
7
      auto SrcAS = D.getType().getQualifiers().getAddressSpace();
128
7
      if (DestAS == SrcAS)
129
0
        Argument = llvm::ConstantExpr::getBitCast(Addr.getPointer(), DestTy);
130
7
      else
131
7
        // FIXME: On addr space mismatch we are passing NULL. The generation
132
7
        // of the global destructor function should be adjusted accordingly.
133
7
        Argument = llvm::ConstantPointerNull::get(DestTy);
134
2.82k
    } else {
135
2.82k
      Argument = llvm::ConstantExpr::getBitCast(
136
2.82k
          Addr.getPointer(), CGF.getTypes().ConvertType(Type)->getPointerTo());
137
2.82k
    }
138
2.83k
  // Otherwise, the standard logic requires a helper function.
139
2.83k
  } else {
140
321
    Func = CodeGenFunction(CGM)
141
321
           .generateDestroyHelper(Addr, Type, CGF.getDestroyer(DtorKind),
142
321
                                  CGF.needsEHCleanup(DtorKind), &D);
143
321
    Argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
144
321
  }
145
3.15k
146
3.15k
  CGM.getCXXABI().registerGlobalDtor(CGF, D, Func, Argument);
147
3.15k
}
148
149
/// Emit code to cause the variable at the given address to be considered as
150
/// constant from this point onwards.
151
static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
152
152
                              llvm::Constant *Addr) {
153
152
  return CGF.EmitInvariantStart(
154
152
      Addr, CGF.getContext().getTypeSizeInChars(D.getType()));
155
152
}
156
157
188
void CodeGenFunction::EmitInvariantStart(llvm::Constant *Addr, CharUnits Size) {
158
188
  // Do not emit the intrinsic if we're not optimizing.
159
188
  if (!CGM.getCodeGenOpts().OptimizationLevel)
160
99
    return;
161
89
162
89
  // Grab the llvm.invariant.start intrinsic.
163
89
  llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
164
89
  // Overloaded address space type.
165
89
  llvm::Type *ObjectPtr[1] = {Int8PtrTy};
166
89
  llvm::Function *InvariantStart = CGM.getIntrinsic(InvStartID, ObjectPtr);
167
89
168
89
  // Emit a call with the size in bytes of the object.
169
89
  uint64_t Width = Size.getQuantity();
170
89
  llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(Int64Ty, Width),
171
89
                           llvm::ConstantExpr::getBitCast(Addr, Int8PtrTy)};
172
89
  Builder.CreateCall(InvariantStart, Args);
173
89
}
174
175
void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
176
                                               llvm::Constant *DeclPtr,
177
5.17k
                                               bool PerformInit) {
178
5.17k
179
5.17k
  const Expr *Init = D.getInit();
180
5.17k
  QualType T = D.getType();
181
5.17k
182
5.17k
  // The address space of a static local variable (DeclPtr) may be different
183
5.17k
  // from the address space of the "this" argument of the constructor. In that
184
5.17k
  // case, we need an addrspacecast before calling the constructor.
185
5.17k
  //
186
5.17k
  // struct StructWithCtor {
187
5.17k
  //   __device__ StructWithCtor() {...}
188
5.17k
  // };
189
5.17k
  // __device__ void foo() {
190
5.17k
  //   __shared__ StructWithCtor s;
191
5.17k
  //   ...
192
5.17k
  // }
193
5.17k
  //
194
5.17k
  // For example, in the above CUDA code, the static local variable s has a
195
5.17k
  // "shared" address space qualifier, but the constructor of StructWithCtor
196
5.17k
  // expects "this" in the "generic" address space.
197
5.17k
  unsigned ExpectedAddrSpace = getContext().getTargetAddressSpace(T);
198
5.17k
  unsigned ActualAddrSpace = DeclPtr->getType()->getPointerAddressSpace();
199
5.17k
  if (ActualAddrSpace != ExpectedAddrSpace) {
200
11
    llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(T);
201
11
    llvm::PointerType *PTy = llvm::PointerType::get(LTy, ExpectedAddrSpace);
202
11
    DeclPtr = llvm::ConstantExpr::getAddrSpaceCast(DeclPtr, PTy);
203
11
  }
204
5.17k
205
5.17k
  ConstantAddress DeclAddr(DeclPtr, getContext().getDeclAlign(&D));
206
5.17k
207
5.17k
  if (!T->isReferenceType()) {
208
5.08k
    if (getLangOpts().OpenMP && 
!getLangOpts().OpenMPSimd2.40k
&&
209
5.08k
        
D.hasAttr<OMPThreadPrivateDeclAttr>()1.01k
) {
210
39
      (void)CGM.getOpenMPRuntime().emitThreadPrivateVarDefinition(
211
39
          &D, DeclAddr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(),
212
39
          PerformInit, this);
213
39
    }
214
5.08k
    if (PerformInit)
215
4.85k
      EmitDeclInit(*this, D, DeclAddr);
216
5.08k
    if (CGM.isTypeConstant(D.getType(), true))
217
152
      EmitDeclInvariant(*this, D, DeclPtr);
218
4.93k
    else
219
4.93k
      EmitDeclDestroy(*this, D, DeclAddr);
220
5.08k
    return;
221
5.08k
  }
222
87
223
87
  assert(PerformInit && "cannot have constant initializer which needs "
224
87
         "destruction for reference");
225
87
  RValue RV = EmitReferenceBindingToExpr(Init);
226
87
  EmitStoreOfScalar(RV.getScalarVal(), DeclAddr, false, T);
227
87
}
228
229
/// Create a stub function, suitable for being passed to atexit,
230
/// which passes the given address to the given destructor function.
231
llvm::Function *CodeGenFunction::createAtExitStub(const VarDecl &VD,
232
                                                  llvm::FunctionCallee dtor,
233
172
                                                  llvm::Constant *addr) {
234
172
  // Get the destructor function type, void(*)(void).
235
172
  llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
236
172
  SmallString<256> FnName;
237
172
  {
238
172
    llvm::raw_svector_ostream Out(FnName);
239
172
    CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out);
240
172
  }
241
172
242
172
  const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
243
172
  llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(
244
172
      ty, FnName.str(), FI, VD.getLocation());
245
172
246
172
  CodeGenFunction CGF(CGM);
247
172
248
172
  CGF.StartFunction(GlobalDecl(&VD, DynamicInitKind::AtExit),
249
172
                    CGM.getContext().VoidTy, fn, FI, FunctionArgList());
250
172
251
172
  llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
252
172
253
172
 // Make sure the call and the callee agree on calling convention.
254
172
  if (llvm::Function *dtorFn =
255
172
          dyn_cast<llvm::Function>(dtor.getCallee()->stripPointerCasts()))
256
172
    call->setCallingConv(dtorFn->getCallingConv());
257
172
258
172
  CGF.FinishFunction();
259
172
260
172
  return fn;
261
172
}
262
263
/// Register a global destructor using the C atexit runtime function.
264
void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD,
265
                                                   llvm::FunctionCallee dtor,
266
166
                                                   llvm::Constant *addr) {
267
166
  // Create a function which calls the destructor.
268
166
  llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr);
269
166
  registerGlobalDtorWithAtExit(dtorStub);
270
166
}
271
272
171
void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtorStub) {
273
171
  // extern "C" int atexit(void (*f)(void));
274
171
  llvm::FunctionType *atexitTy =
275
171
    llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
276
171
277
171
  llvm::FunctionCallee atexit =
278
171
      CGM.CreateRuntimeFunction(atexitTy, "atexit", llvm::AttributeList(),
279
171
                                /*Local=*/true);
280
171
  if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit.getCallee()))
281
171
    atexitFn->setDoesNotThrow();
282
171
283
171
  EmitNounwindRuntimeCall(atexit, dtorStub);
284
171
}
285
286
void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
287
                                         llvm::GlobalVariable *DeclPtr,
288
717
                                         bool PerformInit) {
289
717
  // If we've been asked to forbid guard variables, emit an error now.
290
717
  // This diagnostic is hard-coded for Darwin's use case;  we can find
291
717
  // better phrasing if someone else needs it.
292
717
  if (CGM.getCodeGenOpts().ForbidGuardVariables)
293
1
    CGM.Error(D.getLocation(),
294
1
              "this initialization requires a guard variable, which "
295
1
              "the kernel does not support");
296
717
297
717
  CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
298
717
}
299
300
void CodeGenFunction::EmitCXXGuardedInitBranch(llvm::Value *NeedsInit,
301
                                               llvm::BasicBlock *InitBlock,
302
                                               llvm::BasicBlock *NoInitBlock,
303
                                               GuardKind Kind,
304
708
                                               const VarDecl *D) {
305
708
  assert((Kind == GuardKind::TlsGuard || D) && "no guarded variable");
306
708
307
708
  // A guess at how many times we will enter the initialization of a
308
708
  // variable, depending on the kind of variable.
309
708
  static const uint64_t InitsPerTLSVar = 1024;
310
708
  static const uint64_t InitsPerLocalVar = 1024 * 1024;
311
708
312
708
  llvm::MDNode *Weights;
313
708
  if (Kind == GuardKind::VariableGuard && 
!D->isLocalVarDecl()672
) {
314
167
    // For non-local variables, don't apply any weighting for now. Due to our
315
167
    // use of COMDATs, we expect there to be at most one initialization of the
316
167
    // variable per DSO, but we have no way to know how many DSOs will try to
317
167
    // initialize the variable.
318
167
    Weights = nullptr;
319
541
  } else {
320
541
    uint64_t NumInits;
321
541
    // FIXME: For the TLS case, collect and use profiling information to
322
541
    // determine a more accurate brach weight.
323
541
    if (Kind == GuardKind::TlsGuard || 
D->getTLSKind()505
)
324
105
      NumInits = InitsPerTLSVar;
325
436
    else
326
436
      NumInits = InitsPerLocalVar;
327
541
328
541
    // The probability of us entering the initializer is
329
541
    //   1 / (total number of times we attempt to initialize the variable).
330
541
    llvm::MDBuilder MDHelper(CGM.getLLVMContext());
331
541
    Weights = MDHelper.createBranchWeights(1, NumInits - 1);
332
541
  }
333
708
334
708
  Builder.CreateCondBr(NeedsInit, InitBlock, NoInitBlock, Weights);
335
708
}
336
337
llvm::Function *CodeGenModule::CreateGlobalInitOrDestructFunction(
338
    llvm::FunctionType *FTy, const Twine &Name, const CGFunctionInfo &FI,
339
10.1k
    SourceLocation Loc, bool TLS) {
340
10.1k
  llvm::Function *Fn =
341
10.1k
    llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
342
10.1k
                           Name, &getModule());
343
10.1k
  if (!getLangOpts().AppleKext && 
!TLS10.1k
) {
344
10.0k
    // Set the section if needed.
345
10.0k
    if (const char *Section = getTarget().getStaticInitSectionSpecifier())
346
2.74k
      Fn->setSection(Section);
347
10.0k
  }
348
10.1k
349
10.1k
  SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
350
10.1k
351
10.1k
  Fn->setCallingConv(getRuntimeCC());
352
10.1k
353
10.1k
  if (!getLangOpts().Exceptions)
354
8.85k
    Fn->setDoesNotThrow();
355
10.1k
356
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::Address) &&
357
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::Address, Fn, Loc)91
)
358
86
    Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
359
10.1k
360
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::KernelAddress) &&
361
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::KernelAddress, Fn, Loc)0
)
362
0
    Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
363
10.1k
364
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::HWAddress) &&
365
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::HWAddress, Fn, Loc)0
)
366
0
    Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
367
10.1k
368
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::KernelHWAddress) &&
369
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::KernelHWAddress, Fn, Loc)0
)
370
0
    Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
371
10.1k
372
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::MemTag) &&
373
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::MemTag, Fn, Loc)0
)
374
0
    Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
375
10.1k
376
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::Thread) &&
377
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::Thread, Fn, Loc)6
)
378
3
    Fn->addFnAttr(llvm::Attribute::SanitizeThread);
379
10.1k
380
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::Memory) &&
381
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::Memory, Fn, Loc)41
)
382
41
    Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
383
10.1k
384
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::KernelMemory) &&
385
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::KernelMemory, Fn, Loc)0
)
386
0
    Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
387
10.1k
388
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::SafeStack) &&
389
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::SafeStack, Fn, Loc)0
)
390
0
    Fn->addFnAttr(llvm::Attribute::SafeStack);
391
10.1k
392
10.1k
  if (getLangOpts().Sanitize.has(SanitizerKind::ShadowCallStack) &&
393
10.1k
      
!isInSanitizerBlacklist(SanitizerKind::ShadowCallStack, Fn, Loc)0
)
394
0
    Fn->addFnAttr(llvm::Attribute::ShadowCallStack);
395
10.1k
396
10.1k
  auto RASignKind = getCodeGenOpts().getSignReturnAddress();
397
10.1k
  if (RASignKind != CodeGenOptions::SignReturnAddressScope::None) {
398
16
    Fn->addFnAttr("sign-return-address",
399
16
                  RASignKind == CodeGenOptions::SignReturnAddressScope::All
400
16
                      ? 
"all"8
401
16
                      : 
"non-leaf"8
);
402
16
    auto RASignKey = getCodeGenOpts().getSignReturnAddressKey();
403
16
    Fn->addFnAttr("sign-return-address-key",
404
16
                  RASignKey == CodeGenOptions::SignReturnAddressKeyValue::AKey
405
16
                      ? 
"a_key"10
406
16
                      : 
"b_key"6
);
407
16
  }
408
10.1k
409
10.1k
  if (getCodeGenOpts().BranchTargetEnforcement)
410
6
    Fn->addFnAttr("branch-target-enforcement");
411
10.1k
412
10.1k
  return Fn;
413
10.1k
}
414
415
/// Create a global pointer to a function that will initialize a global
416
/// variable.  The user has requested that this pointer be emitted in a specific
417
/// section.
418
void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D,
419
                                          llvm::GlobalVariable *GV,
420
                                          llvm::Function *InitFunc,
421
6
                                          InitSegAttr *ISA) {
422
6
  llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable(
423
6
      TheModule, InitFunc->getType(), /*isConstant=*/true,
424
6
      llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr");
425
6
  PtrArray->setSection(ISA->getSection());
426
6
  addUsedGlobal(PtrArray);
427
6
428
6
  // If the GV is already in a comdat group, then we have to join it.
429
6
  if (llvm::Comdat *C = GV->getComdat())
430
3
    PtrArray->setComdat(C);
431
6
}
432
433
void
434
CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
435
                                            llvm::GlobalVariable *Addr,
436
4.73k
                                            bool PerformInit) {
437
4.73k
438
4.73k
  // According to E.2.3.1 in CUDA-7.5 Programming guide: __device__,
439
4.73k
  // __constant__ and __shared__ variables defined in namespace scope,
440
4.73k
  // that are of class type, cannot have a non-empty constructor. All
441
4.73k
  // the checks have been done in Sema by now. Whatever initializers
442
4.73k
  // are allowed are empty and we just need to ignore them here.
443
4.73k
  if (getLangOpts().CUDA && 
getLangOpts().CUDAIsDevice31
&&
444
4.73k
      
(24
D->hasAttr<CUDADeviceAttr>()24
||
D->hasAttr<CUDAConstantAttr>()14
||
445
24
       
D->hasAttr<CUDASharedAttr>()4
))
446
24
    return;
447
4.70k
448
4.70k
  if (getLangOpts().OpenMP &&
449
4.70k
      
getOpenMPRuntime().emitDeclareTargetVarDefinition(D, Addr, PerformInit)2.37k
)
450
19
    return;
451
4.68k
452
4.68k
  // Check if we've already initialized this decl.
453
4.68k
  auto I = DelayedCXXInitPosition.find(D);
454
4.68k
  if (I != DelayedCXXInitPosition.end() && 
I->second == ~0U948
)
455
20
    return;
456
4.66k
457
4.66k
  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
458
4.66k
  SmallString<256> FnName;
459
4.66k
  {
460
4.66k
    llvm::raw_svector_ostream Out(FnName);
461
4.66k
    getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out);
462
4.66k
  }
463
4.66k
464
4.66k
  // Create a variable initialization function.
465
4.66k
  llvm::Function *Fn =
466
4.66k
      CreateGlobalInitOrDestructFunction(FTy, FnName.str(),
467
4.66k
                                         getTypes().arrangeNullaryFunction(),
468
4.66k
                                         D->getLocation());
469
4.66k
470
4.66k
  auto *ISA = D->getAttr<InitSegAttr>();
471
4.66k
  CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
472
4.66k
                                                          PerformInit);
473
4.66k
474
4.66k
  llvm::GlobalVariable *COMDATKey =
475
4.66k
      supportsCOMDAT() && 
D->isExternallyVisible()3.08k
?
Addr2.67k
:
nullptr1.99k
;
476
4.66k
477
4.66k
  if (D->getTLSKind()) {
478
144
    // FIXME: Should we support init_priority for thread_local?
479
144
    // FIXME: We only need to register one __cxa_thread_atexit function for the
480
144
    // entire TU.
481
144
    CXXThreadLocalInits.push_back(Fn);
482
144
    CXXThreadLocalInitVars.push_back(D);
483
4.52k
  } else if (PerformInit && 
ISA4.37k
) {
484
6
    EmitPointerToInitFunc(D, Addr, Fn, ISA);
485
4.51k
  } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
486
495
    OrderGlobalInits Key(IPA->getPriority(), PrioritizedCXXGlobalInits.size());
487
495
    PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
488
4.02k
  } else if (isTemplateInstantiation(D->getTemplateSpecializationKind()) ||
489
4.02k
             
getContext().GetGVALinkageForVariable(D) == GVA_DiscardableODR3.91k
) {
490
127
    // C++ [basic.start.init]p2:
491
127
    //   Definitions of explicitly specialized class template static data
492
127
    //   members have ordered initialization. Other class template static data
493
127
    //   members (i.e., implicitly or explicitly instantiated specializations)
494
127
    //   have unordered initialization.
495
127
    //
496
127
    // As a consequence, we can put them into their own llvm.global_ctors entry.
497
127
    //
498
127
    // If the global is externally visible, put the initializer into a COMDAT
499
127
    // group with the global being initialized.  On most platforms, this is a
500
127
    // minor startup time optimization.  In the MS C++ ABI, there are no guard
501
127
    // variables, so this COMDAT key is required for correctness.
502
127
    AddGlobalCtor(Fn, 65535, COMDATKey);
503
127
    if (getTarget().getCXXABI().isMicrosoft() && 
COMDATKey35
) {
504
31
      // In The MS C++, MS add template static data member in the linker
505
31
      // drective.
506
31
      addUsedGlobal(COMDATKey);
507
31
    }
508
3.89k
  } else if (D->hasAttr<SelectAnyAttr>()) {
509
6
    // SelectAny globals will be comdat-folded. Put the initializer into a
510
6
    // COMDAT group associated with the global, so the initializers get folded
511
6
    // too.
512
6
    AddGlobalCtor(Fn, 65535, COMDATKey);
513
3.88k
  } else {
514
3.88k
    I = DelayedCXXInitPosition.find(D); // Re-do lookup in case of re-hash.
515
3.88k
    if (I == DelayedCXXInitPosition.end()) {
516
3.16k
      CXXGlobalInits.push_back(Fn);
517
3.16k
    } else 
if (729
I->second != ~0U729
) {
518
729
      assert(I->second < CXXGlobalInits.size() &&
519
729
             CXXGlobalInits[I->second] == nullptr);
520
729
      CXXGlobalInits[I->second] = Fn;
521
729
    }
522
3.88k
  }
523
4.66k
524
4.66k
  // Remember that we already emitted the initializer for this global.
525
4.66k
  DelayedCXXInitPosition[D] = ~0U;
526
4.66k
}
527
528
17.4k
void CodeGenModule::EmitCXXThreadLocalInitFunc() {
529
17.4k
  getCXXABI().EmitThreadLocalInitFuncs(
530
17.4k
      *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars);
531
17.4k
532
17.4k
  CXXThreadLocalInits.clear();
533
17.4k
  CXXThreadLocalInitVars.clear();
534
17.4k
  CXXThreadLocals.clear();
535
17.4k
}
536
537
void
538
17.4k
CodeGenModule::EmitCXXGlobalInitFunc() {
539
60.7k
  while (!CXXGlobalInits.empty() && 
!CXXGlobalInits.back()44.3k
)
540
43.2k
    CXXGlobalInits.pop_back();
541
17.4k
542
17.4k
  if (CXXGlobalInits.empty() && 
PrioritizedCXXGlobalInits.empty()16.3k
)
543
16.3k
    return;
544
1.13k
545
1.13k
  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
546
1.13k
  const CGFunctionInfo &FI = getTypes().arrangeNullaryFunction();
547
1.13k
548
1.13k
  // Create our global initialization function.
549
1.13k
  if (!PrioritizedCXXGlobalInits.empty()) {
550
164
    SmallVector<llvm::Function *, 8> LocalCXXGlobalInits;
551
164
    llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
552
164
                         PrioritizedCXXGlobalInits.end());
553
164
    // Iterate over "chunks" of ctors with same priority and emit each chunk
554
164
    // into separate function. Note - everything is sorted first by priority,
555
164
    // second - by lex order, so we emit ctor functions in proper order.
556
164
    for (SmallVectorImpl<GlobalInitData >::iterator
557
164
           I = PrioritizedCXXGlobalInits.begin(),
558
494
           E = PrioritizedCXXGlobalInits.end(); I != E; ) {
559
330
      SmallVectorImpl<GlobalInitData >::iterator
560
330
        PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
561
330
562
330
      LocalCXXGlobalInits.clear();
563
330
      unsigned Priority = I->first.priority;
564
330
      // Compute the function suffix from priority. Prepend with zeroes to make
565
330
      // sure the function names are also ordered as priorities.
566
330
      std::string PrioritySuffix = llvm::utostr(Priority);
567
330
      // Priority is always <= 65535 (enforced by sema).
568
330
      PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
569
330
      llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
570
330
          FTy, "_GLOBAL__I_" + PrioritySuffix, FI);
571
330
572
825
      for (; I < PrioE; 
++I495
)
573
495
        LocalCXXGlobalInits.push_back(I->second);
574
330
575
330
      CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
576
330
      AddGlobalCtor(Fn, Priority);
577
330
    }
578
164
    PrioritizedCXXGlobalInits.clear();
579
164
  }
580
1.13k
581
1.13k
  // Include the filename in the symbol name. Including "sub_" matches gcc and
582
1.13k
  // makes sure these symbols appear lexicographically behind the symbols with
583
1.13k
  // priority emitted above.
584
1.13k
  SmallString<128> FileName = llvm::sys::path::filename(getModule().getName());
585
1.13k
  if (FileName.empty())
586
0
    FileName = "<null>";
587
1.13k
588
36.8k
  for (size_t i = 0; i < FileName.size(); 
++i35.7k
) {
589
35.7k
    // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
590
35.7k
    // to be the set of C preprocessing numbers.
591
35.7k
    if (!isPreprocessingNumberBody(FileName[i]))
592
818
      FileName[i] = '_';
593
35.7k
  }
594
1.13k
595
1.13k
  llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
596
1.13k
      FTy, llvm::Twine("_GLOBAL__sub_I_", FileName), FI);
597
1.13k
598
1.13k
  CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
599
1.13k
  AddGlobalCtor(Fn);
600
1.13k
601
1.13k
  // In OpenCL global init functions must be converted to kernels in order to
602
1.13k
  // be able to launch them from the host.
603
1.13k
  // FIXME: Some more work might be needed to handle destructors correctly.
604
1.13k
  // Current initialization function makes use of function pointers callbacks.
605
1.13k
  // We can't support function pointers especially between host and device.
606
1.13k
  // However it seems global destruction has little meaning without any
607
1.13k
  // dynamic resource allocation on the device and program scope variables are
608
1.13k
  // destroyed by the runtime when program is released.
609
1.13k
  if (getLangOpts().OpenCL) {
610
6
    GenOpenCLArgMetadata(Fn);
611
6
    Fn->setCallingConv(llvm::CallingConv::SPIR_KERNEL);
612
6
  }
613
1.13k
614
1.13k
  CXXGlobalInits.clear();
615
1.13k
}
616
617
17.4k
void CodeGenModule::EmitCXXGlobalDtorFunc() {
618
17.4k
  if (CXXGlobalDtors.empty())
619
17.4k
    return;
620
3
621
3
  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
622
3
623
3
  // Create our global destructor function.
624
3
  const CGFunctionInfo &FI = getTypes().arrangeNullaryFunction();
625
3
  llvm::Function *Fn =
626
3
      CreateGlobalInitOrDestructFunction(FTy, "_GLOBAL__D_a", FI);
627
3
628
3
  CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
629
3
  AddGlobalDtor(Fn);
630
3
}
631
632
/// Emit the code necessary to initialize the given global variable.
633
void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
634
                                                       const VarDecl *D,
635
                                                 llvm::GlobalVariable *Addr,
636
4.66k
                                                       bool PerformInit) {
637
4.66k
  // Check if we need to emit debug info for variable initializer.
638
4.66k
  if (D->hasAttr<NoDebugAttr>())
639
1
    DebugInfo = nullptr; // disable debug info indefinitely for this function
640
4.66k
641
4.66k
  CurEHLocation = D->getBeginLoc();
642
4.66k
643
4.66k
  StartFunction(GlobalDecl(D, DynamicInitKind::Initializer),
644
4.66k
                getContext().VoidTy, Fn, getTypes().arrangeNullaryFunction(),
645
4.66k
                FunctionArgList(), D->getLocation(),
646
4.66k
                D->getInit()->getExprLoc());
647
4.66k
648
4.66k
  // Use guarded initialization if the global variable is weak. This
649
4.66k
  // occurs for, e.g., instantiated static data members and
650
4.66k
  // definitions explicitly marked weak.
651
4.66k
  //
652
4.66k
  // Also use guarded initialization for a variable with dynamic TLS and
653
4.66k
  // unordered initialization. (If the initialization is ordered, the ABI
654
4.66k
  // layer will guard the whole-TU initialization for us.)
655
4.66k
  if (Addr->hasWeakLinkage() || 
Addr->hasLinkOnceLinkage()4.60k
||
656
4.66k
      
(4.45k
D->getTLSKind() == VarDecl::TLS_Dynamic4.45k
&&
657
4.45k
       
isTemplateInstantiation(D->getTemplateSpecializationKind())67
)) {
658
212
    EmitCXXGuardedInit(*D, Addr, PerformInit);
659
4.45k
  } else {
660
4.45k
    EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
661
4.45k
  }
662
4.66k
663
4.66k
  FinishFunction();
664
4.66k
}
665
666
void
667
CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
668
                                           ArrayRef<llvm::Function *> Decls,
669
1.49k
                                           ConstantAddress Guard) {
670
1.49k
  {
671
1.49k
    auto NL = ApplyDebugLocation::CreateEmpty(*this);
672
1.49k
    StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
673
1.49k
                  getTypes().arrangeNullaryFunction(), FunctionArgList());
674
1.49k
    // Emit an artificial location for this function.
675
1.49k
    auto AL = ApplyDebugLocation::CreateArtificial(*this);
676
1.49k
677
1.49k
    llvm::BasicBlock *ExitBlock = nullptr;
678
1.49k
    if (Guard.isValid()) {
679
36
      // If we have a guard variable, check whether we've already performed
680
36
      // these initializations. This happens for TLS initialization functions.
681
36
      llvm::Value *GuardVal = Builder.CreateLoad(Guard);
682
36
      llvm::Value *Uninit = Builder.CreateIsNull(GuardVal,
683
36
                                                 "guard.uninitialized");
684
36
      llvm::BasicBlock *InitBlock = createBasicBlock("init");
685
36
      ExitBlock = createBasicBlock("exit");
686
36
      EmitCXXGuardedInitBranch(Uninit, InitBlock, ExitBlock,
687
36
                               GuardKind::TlsGuard, nullptr);
688
36
      EmitBlock(InitBlock);
689
36
      // Mark as initialized before initializing anything else. If the
690
36
      // initializers use previously-initialized thread_local vars, that's
691
36
      // probably supposed to be OK, but the standard doesn't say.
692
36
      Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard);
693
36
694
36
      // The guard variable can't ever change again.
695
36
      EmitInvariantStart(
696
36
          Guard.getPointer(),
697
36
          CharUnits::fromQuantity(
698
36
              CGM.getDataLayout().getTypeAllocSize(GuardVal->getType())));
699
36
    }
700
1.49k
701
1.49k
    RunCleanupsScope Scope(*this);
702
1.49k
703
1.49k
    // When building in Objective-C++ ARC mode, create an autorelease pool
704
1.49k
    // around the global initializers.
705
1.49k
    if (getLangOpts().ObjCAutoRefCount && 
getLangOpts().CPlusPlus4
) {
706
4
      llvm::Value *token = EmitObjCAutoreleasePoolPush();
707
4
      EmitObjCAutoreleasePoolCleanup(token);
708
4
    }
709
1.49k
710
11.7k
    for (unsigned i = 0, e = Decls.size(); i != e; 
++i10.2k
)
711
10.2k
      if (Decls[i])
712
4.44k
        EmitRuntimeCall(Decls[i]);
713
1.49k
714
1.49k
    Scope.ForceCleanup();
715
1.49k
716
1.49k
    if (ExitBlock) {
717
36
      Builder.CreateBr(ExitBlock);
718
36
      EmitBlock(ExitBlock);
719
36
    }
720
1.49k
  }
721
1.49k
722
1.49k
  FinishFunction();
723
1.49k
}
724
725
void CodeGenFunction::GenerateCXXGlobalDtorsFunc(
726
    llvm::Function *Fn,
727
    const std::vector<std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
728
3
                                 llvm::Constant *>> &DtorsAndObjects) {
729
3
  {
730
3
    auto NL = ApplyDebugLocation::CreateEmpty(*this);
731
3
    StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
732
3
                  getTypes().arrangeNullaryFunction(), FunctionArgList());
733
3
    // Emit an artificial location for this function.
734
3
    auto AL = ApplyDebugLocation::CreateArtificial(*this);
735
3
736
3
    // Emit the dtors, in reverse order from construction.
737
9
    for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; 
++i6
) {
738
6
      llvm::FunctionType *CalleeTy;
739
6
      llvm::Value *Callee;
740
6
      llvm::Constant *Arg;
741
6
      std::tie(CalleeTy, Callee, Arg) = DtorsAndObjects[e - i - 1];
742
6
      llvm::CallInst *CI = Builder.CreateCall(CalleeTy, Callee, Arg);
743
6
      // Make sure the call and the callee agree on calling convention.
744
6
      if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
745
6
        CI->setCallingConv(F->getCallingConv());
746
6
    }
747
3
  }
748
3
749
3
  FinishFunction();
750
3
}
751
752
/// generateDestroyHelper - Generates a helper function which, when
753
/// invoked, destroys the given object.  The address of the object
754
/// should be in global memory.
755
llvm::Function *CodeGenFunction::generateDestroyHelper(
756
    Address addr, QualType type, Destroyer *destroyer,
757
325
    bool useEHCleanupForArray, const VarDecl *VD) {
758
325
  FunctionArgList args;
759
325
  ImplicitParamDecl Dst(getContext(), getContext().VoidPtrTy,
760
325
                        ImplicitParamDecl::Other);
761
325
  args.push_back(&Dst);
762
325
763
325
  const CGFunctionInfo &FI =
764
325
    CGM.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy, args);
765
325
  llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
766
325
  llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(
767
325
      FTy, "__cxx_global_array_dtor", FI, VD->getLocation());
768
325
769
325
  CurEHLocation = VD->getBeginLoc();
770
325
771
325
  StartFunction(VD, getContext().VoidTy, fn, FI, args);
772
325
773
325
  emitDestroy(addr, type, destroyer, useEHCleanupForArray);
774
325
775
325
  FinishFunction();
776
325
777
325
  return fn;
778
325
}