Add back a couple checks removed by r129128; the fact that an intitializer
authorEli Friedman <eli.friedman@gmail.com>
Sat, 9 Apr 2011 09:11:09 +0000 (09:11 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sat, 9 Apr 2011 09:11:09 +0000 (09:11 +0000)
is an array of structures doesn't imply it's a ConstantArray of
ConstantStruct.

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

lib/Transforms/IPO/GlobalOpt.cpp
test/Transforms/GlobalOpt/2011-04-09-EmptyGlobalCtors.ll [new file with mode: 0644]

index 5b6ed2c98699b526df602698fc0c44693249acf7..71d13edc96f753dd1c2b9ec154bd115533ef88b0 100644 (file)
@@ -1954,11 +1954,13 @@ GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) {
   // only allowed to optimize the initializer if it is unique.
   if (!GV->hasUniqueInitializer()) return 0;
   
-  ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
-  
+  ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
+  if (!CA) return 0;
+
   for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
-    ConstantStruct *CS = cast<ConstantStruct>(*i);
-    
+    ConstantStruct *CS = dyn_cast<ConstantStruct>(*i);
+    if (!CS) return 0;
+
     if (isa<ConstantPointerNull>(CS->getOperand(1)))
       continue;
 
diff --git a/test/Transforms/GlobalOpt/2011-04-09-EmptyGlobalCtors.ll b/test/Transforms/GlobalOpt/2011-04-09-EmptyGlobalCtors.ll
new file mode 100644 (file)
index 0000000..321a487
--- /dev/null
@@ -0,0 +1,5 @@
+; RUN: opt < %s -globalopt -disable-output
+
+%0 = type { i32, void ()* }
+@llvm.global_ctors = appending global [0 x %0] zeroinitializer
+