[PATCH] Fix llvm::removeUnreachableBlocks to handle unreachable loops

Evgeniy Stepanov eugenis at google.com
Mon Mar 18 01:30:00 PDT 2013


Hi chandlerc,

Turns out, http://llvm-reviews.chandlerc.com/D208 is broken and does not handle unreachable loops at all, see the test in this change. Fails with:

While deleting: label %zzz
Use still stuck around after Def is destroyed:  br label %zzz
opt: ../lib/IR/Value.cpp:79: virtual llvm::Value::~Value(): Assertion `use_empty() && "Uses remain when a value is destroyed!"' failed.

Switching to dropAllReferences - essentially a copy of removeUnreachableBlocksFromFn from SimplifyCFG.

Another approach would be replacing all uses of unreachable labels with the function entry label. Let me know if this is preferrable.

http://llvm-reviews.chandlerc.com/D548

Files:
  lib/Transforms/Utils/Local.cpp
  test/Instrumentation/MemorySanitizer/unreachable.ll

Index: lib/Transforms/Utils/Local.cpp
===================================================================
--- lib/Transforms/Utils/Local.cpp
+++ lib/Transforms/Utils/Local.cpp
@@ -985,22 +985,17 @@
     if (Reachable.count(I))
       continue;
 
-    // Remove the block as predecessor of all its reachable successors.
-    // Unreachable successors don't matter as they'll soon be removed, too.
     for (succ_iterator SI = succ_begin(I), SE = succ_end(I); SI != SE; ++SI)
       if (Reachable.count(*SI))
         (*SI)->removePredecessor(I);
+    I->dropAllReferences();
+  }
 
-    // Zap all instructions in this basic block.
-    while (!I->empty()) {
-      Instruction &Inst = I->back();
-      if (!Inst.use_empty())
-        Inst.replaceAllUsesWith(UndefValue::get(Inst.getType()));
-      I->getInstList().pop_back();
-    }
+  for (Function::iterator I = llvm::next(F.begin()), E=F.end(); I != E;)
+    if (!Reachable.count(I))
+      I = F.getBasicBlockList().erase(I);
+    else
+      ++I;
 
-    --I;
-    llvm::next(I)->eraseFromParent();
-  }
   return true;
 }
Index: test/Instrumentation/MemorySanitizer/unreachable.ll
===================================================================
--- test/Instrumentation/MemorySanitizer/unreachable.ll
+++ test/Instrumentation/MemorySanitizer/unreachable.ll
@@ -21,3 +21,19 @@
 ; CHECK: @Func
 ; CHECK: store i32 0, {{.*}} @__msan_retval_tls
 ; CHECK: ret i32 42
+
+
+define i32 @UnreachableLoop() nounwind uwtable {
+entry:
+  ret i32 0
+
+zzz:
+  br label %xxx
+
+xxx:
+  br label %zzz
+}
+
+; CHECK: @UnreachableLoop
+; CHECK: store i32 0, {{.*}} @__msan_retval_tls
+; CHECK: ret i32 0
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D548.1.patch
Type: text/x-patch
Size: 1650 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130318/124a7290/attachment.bin>


More information about the llvm-commits mailing list