<div dir="ltr"><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">Hey Ben</span><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">
This is crashing our test suite.  Mind taking a look?  This may be related to what Richard has just pointed out too.</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">
Attached a reduced test which crashes the following "opt dce.ll -globaldce -o file.bc”</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">
Cheers,</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">Pete</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">
<br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jul 4, 2014 at 1:36 PM, Benjamin Kramer <span dir="ltr"><<a href="mailto:benny.kra@googlemail.com" target="_blank">benny.kra@googlemail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: d0k<br>
Date: Fri Jul  4 07:36:05 2014<br>
New Revision: 212337<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=212337&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=212337&view=rev</a><br>
Log:<br>
GlobalDCE: Delete available_externally initializers if it allows removing the value the initializer is referring to.<br>
<br>
This is useful for functions that are not actually available externally but<br>
referenced by a vtable of some kind. Clang emits functions like this for the MS<br>
ABI.<br>
<br>
PR20182.<br>
<br>
Modified:<br>
    llvm/trunk/lib/Transforms/IPO/GlobalDCE.cpp<br>
    llvm/trunk/test/Transforms/GlobalDCE/externally_available.ll<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/GlobalDCE.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalDCE.cpp?rev=212337&r1=212336&r2=212337&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalDCE.cpp?rev=212337&r1=212336&r2=212337&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/GlobalDCE.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/GlobalDCE.cpp Fri Jul  4 07:36:05 2014<br>
@@ -44,14 +44,19 @@ namespace {<br>
     bool runOnModule(Module &M) override;<br>
<br>
   private:<br>
-    SmallPtrSet<GlobalValue*, 32> AliveGlobals;<br>
+    SmallPtrSet<Constant *, 32> AliveGlobals;<br>
     SmallPtrSet<Constant *, 8> SeenConstants;<br>
+    SmallPtrSet<GlobalVariable *, 8> DiscardableGlobalInitializers;<br>
<br>
     /// GlobalIsNeeded - mark the specific global value as needed, and<br>
     /// recursively mark anything that it uses as also needed.<br>
     void GlobalIsNeeded(GlobalValue *GV);<br>
     void MarkUsedGlobalsAsNeeded(Constant *C);<br>
<br>
+    /// \brief Checks if C is alive or is a ConstantExpr that refers to an alive<br>
+    /// value.<br>
+    bool ContainsUsedGlobal(Constant *C);<br>
+<br>
     bool RemoveUnusedGlobalValue(GlobalValue &GV);<br>
   };<br>
 }<br>
@@ -162,6 +167,19 @@ bool GlobalDCE::runOnModule(Module &M) {<br>
       I->setAliasee(nullptr);<br>
     }<br>
<br>
+  // Look for available externally constants that we can turn into normal<br>
+  // externals by deleting their initalizers. This allows us to remove other<br>
+  // globals that are referenced by the initializer.<br>
+  if (!DiscardableGlobalInitializers.empty()) {<br>
+    for (GlobalVariable *GV : DiscardableGlobalInitializers) {<br>
+      if (!ContainsUsedGlobal(GV->getInitializer())) {<br>
+        GV->setInitializer(nullptr);<br>
+        GV->setLinkage(GlobalValue::ExternalLinkage);<br>
+        Changed = true;<br>
+      }<br>
+    }<br>
+  }<br>
+<br>
   if (!DeadFunctions.empty()) {<br>
     // Now that all interferences have been dropped, delete the actual objects<br>
     // themselves.<br>
@@ -209,8 +227,12 @@ void GlobalDCE::GlobalIsNeeded(GlobalVal<br>
   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {<br>
     // If this is a global variable, we must make sure to add any global values<br>
     // referenced by the initializer to the alive set.<br>
-    if (GV->hasInitializer())<br>
-      MarkUsedGlobalsAsNeeded(GV->getInitializer());<br>
+    if (GV->hasInitializer()) {<br>
+      if (GV->hasAvailableExternallyLinkage())<br>
+        DiscardableGlobalInitializers.insert(GV);<br>
+      else<br>
+        MarkUsedGlobalsAsNeeded(GV->getInitializer());<br>
+    }<br>
   } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {<br>
     // The target of a global alias is needed.<br>
     MarkUsedGlobalsAsNeeded(GA->getAliasee());<br>
@@ -248,6 +270,21 @@ void GlobalDCE::MarkUsedGlobalsAsNeeded(<br>
   }<br>
 }<br>
<br>
+bool GlobalDCE::ContainsUsedGlobal(Constant *C) {<br>
+  // C contains a used global If C is alive or we visited it while marking<br>
+  // values alive.<br>
+  if (AliveGlobals.count(C) || SeenConstants.count(C))<br>
+    return true;<br>
+<br>
+  // Now check all operands of a ConstantExpr.<br>
+  for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) {<br>
+    Constant *Op = dyn_cast<Constant>(*I);<br>
+    if (Op && ContainsUsedGlobal(Op))<br>
+      return true;<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
 // RemoveUnusedGlobalValue - Loop over all of the uses of the specified<br>
 // GlobalValue, looking for the constant pointer ref that may be pointing to it.<br>
 // If found, check to see if the constant pointer ref is safe to destroy, and if<br>
<br>
Modified: llvm/trunk/test/Transforms/GlobalDCE/externally_available.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalDCE/externally_available.ll?rev=212337&r1=212336&r2=212337&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalDCE/externally_available.ll?rev=212337&r1=212336&r2=212337&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/test/Transforms/GlobalDCE/externally_available.ll (original)<br>
+++ llvm/trunk/test/Transforms/GlobalDCE/externally_available.ll Fri Jul  4 07:36:05 2014<br>
@@ -1,10 +1,43 @@<br>
-; RUN: opt < %s -globaldce -S | not grep test_<br>
+; RUN: opt < %s -globaldce -S | FileCheck %s<br>
<br>
 ; test_function should not be emitted to the .s file.<br>
+; CHECK-NOT: @test_function<br>
 define available_externally i32 @test_function() {<br>
   ret i32 4<br>
 }<br>
<br>
 ; test_global should not be emitted to the .s file.<br>
+; CHECK-NOT: @test_global<br>
 @test_global = available_externally global i32 4<br>
<br>
+; CHECK: @x = external constant void ()*<br>
+@x = available_externally constant void()* @f<br>
+; CHECK: @y = external constant i32<br>
+@y = available_externally constant i32 ptrtoint (void()* @g to i32)<br>
+; @h is still alive, so don't remove the initializer too eagerly.<br>
+; CHECK: @z = available_externally constant i8 ptrtoint (void (i8)* @h to i8)<br>
+@z = available_externally constant i8 ptrtoint (void(i8)* @h to i8)<br>
+<br>
+; CHECK-NOT: @f<br>
+define linkonce_odr void @f() {<br>
+  ret void<br>
+}<br>
+<br>
+; CHECK-NOT: @g<br>
+define linkonce_odr void @g() {<br>
+  ret void<br>
+}<br>
+<br>
+; CHECK: define linkonce_odr void @h<br>
+define linkonce_odr void @h(i8) {<br>
+  ret void<br>
+}<br>
+<br>
+define i32 @main() {<br>
+  %f = load void()** @x<br>
+  call void %f()<br>
+  %g = load i32* @y<br>
+  %h = load i8* @z<br>
+  call void @h(i8 %h)<br>
+  ret i32 %g<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>