diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index 669162cb3d8..183a599bc12 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -3070,6 +3070,11 @@ static int compareNames(const void *A, const void *B) { static void setUsedInitializer(GlobalVariable &V, SmallPtrSet Init) { + if (Init.empty()) { + V.eraseFromParent(); + return; + } + SmallVector UsedArray; PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext()); diff --git a/test/Transforms/GlobalOpt/compiler-used.ll b/test/Transforms/GlobalOpt/compiler-used.ll new file mode 100644 index 00000000000..a710d272edc --- /dev/null +++ b/test/Transforms/GlobalOpt/compiler-used.ll @@ -0,0 +1,16 @@ +; RUN: opt < %s -globalopt -S | FileCheck %s + +; Test that when all members of llvm.compiler.used are found to be redundant +; we delete it instead of crashing. + +define void @foo() { + ret void +} + +@llvm.used = appending global [1 x i8*] [i8* bitcast (void ()* @foo to i8*)], section "llvm.metadata" + +@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (void ()* @foo to i8*)], section "llvm.metadata" + +; CHECK-NOT: @llvm.compiler.used +; CHECK: @llvm.used = appending global [1 x i8*] [i8* bitcast (void ()* @foo to i8*)], section "llvm.metadata" +; CHECK-NOT: @llvm.compiler.used