In particular, pre-.init_array ELF uses the .ctors section mechanism.
MinGW COFF also uses .ctors, now that I think about it. Therefore,
restrict this optimization to the two platforms that are currently known
to work: ELF with .init_array and COFF with .CRT$XCU.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@218287
91177308-0d34-0410-b5e6-
96231b3b80d8
CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
PerformInit);
- llvm::GlobalVariable *Key = supportsCOMDAT() ? Addr : nullptr;
+ llvm::GlobalVariable *Key = supportsCOMDATInitializers() ? Addr : nullptr;
if (D->getTLSKind()) {
// FIXME: Should we support init_priority for thread_local?
const TargetInfo &getTarget() const { return Target; }
const llvm::Triple &getTriple() const;
bool supportsCOMDAT() const;
+ bool supportsCOMDATInitializers() const;
CGCXXABI &getCXXABI() const { return *ABI; }
llvm::LLVMContext &getLLVMContext() { return VMContext; }
// The ABI says: It is suggested that it be emitted in the same COMDAT group
// as the associated data object
- if (!D.isLocalVarDecl() && var->isWeakForLinker() && CGM.supportsCOMDAT()) {
+ if (!D.isLocalVarDecl() && var->isWeakForLinker() &&
+ CGM.supportsCOMDATInitializers()) {
llvm::Comdat *C = CGM.getModule().getOrInsertComdat(var->getName());
guard->setComdat(C);
var->setComdat(C);
return !getTriple().isOSBinFormatMachO();
}
+bool CodeGenModule::supportsCOMDATInitializers() const {
+ // We can only put initializers in comdat groups on ELF with .init_array and
+ // COFF with .CRT$XCU.
+ return supportsCOMDAT() &&
+ ((getTriple().isOSBinFormatELF() && getCodeGenOpts().UseInitArray) ||
+ (getTriple().isWindowsMSVCEnvironment()));
+}
+
const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
if (TheTargetCodeGenInfo)
return *TheTargetCodeGenInfo;
-// RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -fuse-init-array -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -o - %s | \
+// RUN: FileCheck %s --check-prefix=CTORS
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | \
-// RUN: FileCheck --check-prefix=MACHO %s
+// RUN: FileCheck --check-prefix=MACHO %s
// CHECK: @_ZN5test11A1aE = constant i32 10, align 4
// CHECK: @_ZN5test212_GLOBAL__N_11AIiE1xE = internal global i32 0, align 4
// CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0, comdat $_ZN5test31AIiE1xE
// MACHO: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
// MACHO-NOT: comdat
+// CTORS: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
+// CTORS-NOT: comdat
// CHECK: _ZN5test51U2k0E = global i32 0
// CHECK: _ZN5test51U2k1E = global i32 0
// CHECK-LABEL: define internal void @__cxx_global_var_init1() {{.*}} comdat $_ZN5test31AIiE1xE
// MACHO-LABEL: define internal void @__cxx_global_var_init1()
// MACHO-NOT: comdat
+ // CTORS-LABEL: define internal void @__cxx_global_var_init1()
+ // CTORS-NOT: comdat
// CHECK: [[GUARDBYTE:%.*]] = load i8* bitcast (i64* @_ZGVN5test31AIiE1xE to i8*)
// CHECK-NEXT: [[UNINITIALIZED:%.*]] = icmp eq i8 [[GUARDBYTE]], 0
// CHECK-NEXT: br i1 [[UNINITIALIZED]]
-// RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -o - | FileCheck --check-prefix=ELF --check-prefix=ALL %s
+// RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -fuse-init-array -emit-llvm -o - | FileCheck --check-prefix=ELF --check-prefix=ALL %s
// RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck --check-prefix=MACHO --check-prefix=ALL %s
// ALL: ; ModuleID