Don't crash when llvm.compiler.used becomes empty.
authorRafael Espindola <rafael.espindola@gmail.com>
Sat, 20 Jul 2013 23:33:15 +0000 (23:33 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Sat, 20 Jul 2013 23:33:15 +0000 (23:33 +0000)
GlobalOpt simplifies llvm.compiler.used by removing any members that are also
in the more strict llvm.used. Handle the special case where llvm.compiler.used
becomes empty.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186778 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/IPO/GlobalOpt.cpp
test/Transforms/GlobalOpt/compiler-used.ll [new file with mode: 0644]

index 669162cb3d8792978abdc31ee001a850c321021c..183a599bc1226d476101d741efda8aa12f51b438 100644 (file)
@@ -3070,6 +3070,11 @@ static int compareNames(const void *A, const void *B) {
 
 static void setUsedInitializer(GlobalVariable &V,
                                SmallPtrSet<GlobalValue *, 8> Init) {
+  if (Init.empty()) {
+    V.eraseFromParent();
+    return;
+  }
+
   SmallVector<llvm::Constant *, 8> 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 (file)
index 0000000..a710d27
--- /dev/null
@@ -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