[PATCH] D88276: [IsKnownNonZero] Handle the case with non-constant phi nodes

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 20:19:56 PDT 2020


skatkov created this revision.
skatkov added reviewers: aqjune, nikic, efriedma.
Herald added subscribers: dantrushin, hiraditya.
Herald added a project: LLVM.
skatkov requested review of this revision.

Handle the case when all inputs of phi iare proven is not zero.

Constants are checked in beginning of this method before check for depth of recursion,
so it is a partial case of non-constant phi.

Recursion depth is already handled by the function.


https://reviews.llvm.org/D88276

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Transforms/InstCombine/phi.ll


Index: llvm/test/Transforms/InstCombine/phi.ll
===================================================================
--- llvm/test/Transforms/InstCombine/phi.ll
+++ llvm/test/Transforms/InstCombine/phi.ll
@@ -1056,6 +1056,35 @@
   ret i1 %cmp1
 }
 
+define i1 @phi_allnonzerononconstant(i1 %c, i32 %a, i32* nonnull %b1, i32* nonnull %b2) {
+; CHECK-LABEL: @phi_allnonzerononconstant(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br i1 [[C:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
+; CHECK:       if.then:
+; CHECK-NEXT:    br label [[IF_END:%.*]]
+; CHECK:       if.else:
+; CHECK-NEXT:    call void @dummy()
+; CHECK-NEXT:    br label [[IF_END]]
+; CHECK:       if.end:
+; CHECK-NEXT:    ret i1 false
+;
+entry:
+  br i1 %c, label %if.then, label %if.else
+
+if.then:                                          ; preds = %entry
+  br label %if.end
+
+if.else:                                          ; preds = %entry
+  call void @dummy()
+
+  br label %if.end
+
+if.end:                                           ; preds = %if.else, %if.then
+  %x.0 = phi i32* [ %b1, %if.then ], [ %b2, %if.else ]
+  %cmp1 = icmp eq i32* %x.0, null
+  ret i1 %cmp1
+}
+
 declare void @dummy()
 
 define i1 @phi_knownnonzero_eq(i32 %n, i32 %s, i32* nocapture readonly %P) {
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2561,11 +2561,12 @@
         }
       }
     }
-    // Check if all incoming values are non-zero constant.
-    bool AllNonZeroConstants = llvm::all_of(PN->operands(), [](Value *V) {
-      return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZero();
+    // Check if all incoming values are non-zero using recursion.
+    // TODO: Support the case when phi is a transitive input of itself.
+    bool AllNonZero = llvm::all_of(PN->operands(), [&](Value *V) {
+      return isKnownNonZero(V, DemandedElts, Depth, Q);
     });
-    if (AllNonZeroConstants)
+    if (AllNonZero)
       return true;
   }
   // ExtractElement


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88276.294221.patch
Type: text/x-patch
Size: 2082 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200925/b31076cf/attachment.bin>


More information about the llvm-commits mailing list