[PATCH] D66216: [ValueTracking] Fix recurrence detection to check both PHI operands.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 14 05:43:09 PDT 2019


fhahn created this revision.
fhahn added reviewers: lebedev.ri, spatel, nikic.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

Currently we fail to compute known bits for recurrences where the
first incoming value is the start value of the recurrence.

Instead of exiting the loop when the first incoming value is not
the step of the recurrence, continue to check the second incoming
value.

The original code uses a loop to handle both cases, but incorrectly
exits instead of continuing.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66216

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Transforms/InstCombine/phi-known-bits-operand-order.ll


Index: llvm/test/Transforms/InstCombine/phi-known-bits-operand-order.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/phi-known-bits-operand-order.ll
@@ -0,0 +1,73 @@
+; RUN: opt -instcombine -S < %s | FileCheck %s
+
+; Check that we can turn the icmp sle into icmp ult, regardless of the
+; order of the incoming values of the PHI node.
+
+declare i1 @cond()
+
+define void @phi_recurrence_start_first() {
+; CHECK-LABEL: define void @phi_recurrence_start_first() {
+; CHECK-LABEL: for.cond11:
+; CHECK-NEXT:   %i.1 = phi i32 [ %start, %if.then ], [ %step, %for.cond11 ]
+; CHECK-NEXT:   %cmp13 = icmp ult i32 %i.1, 100
+; CHECK-NEXT:   %step = add nsw i32 %i.1, 1
+; CHECK-NEXT:   br i1 %cmp13, label %for.cond11, label %while.end
+;
+entry:
+  br label %while.cond
+
+while.cond:                                       ; preds = %entry, %for.cond26
+  %cell.0 = phi i32 [ 0, %entry ], [ %start, %for.cond26 ]
+  %cond.v = call i1 @cond()
+  br i1 %cond.v, label %if.then, label %while.end
+
+if.then:                                          ; preds = %while.cond
+  %start = add nsw i32 %cell.0, 1
+  br i1 %cond.v, label %for.cond11, label %for.cond26
+
+for.cond11:                                       ; preds = %for.cond11, %if.then
+  %i.1 = phi i32 [ %start, %if.then ], [ %step, %for.cond11 ]
+  %cmp13 = icmp sle i32 %i.1, 99
+  %step = add nsw i32 %i.1, 1
+  br i1 %cmp13, label %for.cond11, label %while.end
+
+for.cond26:                                       ; preds = %if.then
+  br label %while.cond
+
+while.end:                                        ; preds = %while.cond, %for.cond11
+  ret void
+}
+
+define void @phi_recurrence_step_first() {
+; CHECK-LABEL: define void @phi_recurrence_step_first() {
+; CHECK-LABEL: for.cond11:
+; CHECK-NEXT:   %i.1 = phi i32 [ %start, %if.then ], [ %step, %for.cond11 ]
+; CHECK-NEXT:   %cmp13 = icmp ult i32 %i.1, 100
+; CHECK-NEXT:   %step = add nsw i32 %i.1, 1
+; CHECK-NEXT:   br i1 %cmp13, label %for.cond11, label %while.end
+;
+entry:
+  br label %while.cond
+
+while.cond:                                       ; preds = %entry, %for.cond26
+  %cell.0 = phi i32 [ 0, %entry ], [ %start, %for.cond26 ]
+  %cond.v = call i1 @cond()
+  br i1 %cond.v, label %if.then, label %while.end
+
+if.then:                                          ; preds = %while.cond
+  %start = add nsw i32 %cell.0, 1
+  br i1 %cond.v, label %for.cond11, label %for.cond26
+
+for.cond11:                                       ; preds = %for.cond11, %if.then
+  %i.1 = phi i32 [ %step, %for.cond11 ], [ %start, %if.then]
+  %cmp13 = icmp sle i32 %i.1, 99
+  %step = add nsw i32 %i.1, 1
+  br i1 %cmp13, label %for.cond11, label %while.end
+
+for.cond26:                                       ; preds = %if.then
+  br label %while.cond
+
+while.end:                                        ; preds = %while.cond, %for.cond11
+  ret void
+}
+
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -1373,7 +1373,7 @@
           else if (LR == I)
             L = LL;
           else
-            break;
+            continue;
           // Ok, we have a PHI of the form L op= R. Check for low
           // zero bits.
           computeKnownBits(R, Known2, Depth + 1, Q);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66216.215091.patch
Type: text/x-patch
Size: 3422 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190814/ef0b7848/attachment.bin>


More information about the llvm-commits mailing list