[PATCH] [Jump-Threading] Fixed jump threading hang issues (PR15386, PR15851)

Dinesh Dwivedi dinesh.d at samsung.com
Mon Jun 2 07:45:05 PDT 2014


Hi rafael, bkramer, nicholas, nadav,

For specific IR patterns, jump threading pass goes in to infinite loop transforming one pattern to other and then back to previous one.
For these kind of pattern just checking if BB changed is not enough. I have added a set of BBs to maintain how many BBs are updated
and code to terminate pass if count is not increased to previous iteration.

If required, we can tweak this to check if last few iterations have not updated any new BB to terminate pass.

I am not very familiar with Jump Threading pass so this patch might not correct. Please let me know if I should look in any other direction
to fix these issues.

http://reviews.llvm.org/D3991

Files:
  lib/Transforms/Scalar/JumpThreading.cpp
  test/Transforms/JumpThreading/pr15851_hang.ll

Index: lib/Transforms/Scalar/JumpThreading.cpp
===================================================================
--- lib/Transforms/Scalar/JumpThreading.cpp
+++ lib/Transforms/Scalar/JumpThreading.cpp
@@ -160,10 +160,12 @@
 
   FindLoopHeaders(F);
 
-  bool Changed, EverChanged = false;
+  unsigned UpdatedBBCount;
+  SmallPtrSet<BasicBlock *, 16> UpdatedBB;
   do {
-    Changed = false;
+    UpdatedBBCount = UpdatedBB.size();
     for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
+      bool Changed = false;
       BasicBlock *BB = I;
       // Thread all of the branches we can over this block.
       while (ProcessBlock(BB))
@@ -180,7 +182,7 @@
         LoopHeaders.erase(BB);
         LVI->eraseBlock(BB);
         DeleteDeadBlock(BB);
-        Changed = true;
+        UpdatedBB.insert(BB);
         continue;
       }
 
@@ -214,12 +216,13 @@
         if (ErasedFromLoopHeaders)
           LoopHeaders.insert(BB);
       }
+      if (Changed)
+        UpdatedBB.insert(BB);
     }
-    EverChanged |= Changed;
-  } while (Changed);
+  } while (UpdatedBBCount < UpdatedBB.size());
 
   LoopHeaders.clear();
-  return EverChanged;
+  return UpdatedBBCount > 0;
 }
 
 /// getJumpThreadDuplicationCost - Return the cost of duplicating this block to
Index: test/Transforms/JumpThreading/pr15851_hang.ll
===================================================================
--- test/Transforms/JumpThreading/pr15851_hang.ll
+++ test/Transforms/JumpThreading/pr15851_hang.ll
@@ -0,0 +1,27 @@
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @f() {
+entry:
+  call void @f()
+  unreachable
+
+for.cond1:                                        ; preds = %for.cond1, %for.inc
+  %i.0 = phi i32 [ %inc, %for.inc ], [ 0, %for.cond1 ]
+  %cmp = icmp slt i32 %i.0, 2
+  br i1 %cmp, label %for.body, label %for.cond1
+
+for.body:                                         ; preds = %for.cond1
+  %div = sdiv i32 %i.0, 2
+  %i.0.off = add i32 %i.0, 1
+  %0 = icmp ugt i32 %i.0.off, 2
+  br i1 %0, label %land.rhs, label %for.inc
+
+land.rhs:                                         ; preds = %for.body
+  store i32 0, i32* undef, align 4
+  br label %for.inc
+
+for.inc:                                          ; preds = %for.body
+  %inc = add nsw i32 %div, 1
+  br label %for.cond1
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3991.10017.patch
Type: text/x-patch
Size: 2365 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140602/b745effd/attachment.bin>


More information about the llvm-commits mailing list