[PATCH] D16735: [ValueTracking] Improve isKnownNonZero for PHI of non-zero constants

Jun Bum Lim via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 29 13:01:29 PST 2016


junbuml created this revision.
junbuml added reviewers: majnemer, sanjoy, jmolloy, mcrosier.
junbuml added a subscriber: llvm-commits.
Herald added a subscriber: mcrosier.

It is clear that a PHI is a non-zero if all incoming values are non-zero constants.

http://reviews.llvm.org/D16735

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

Index: test/Transforms/InstCombine/phi.ll
===================================================================
--- test/Transforms/InstCombine/phi.ll
+++ test/Transforms/InstCombine/phi.ll
@@ -760,3 +760,24 @@
 ; CHECK-NEXT: ret i1 %[[RES]]
 }
 
+; CHECK-LABEL: phi_allnonzeroconstant
+; CHECK-NOT: phi i32
+; CHECK: ret i1 false
+define i1 @phi_allnonzeroconstant(i1 %c, i32 %a, i32 %b) {
+entry:
+  %cmp = icmp sgt i1 %c, 0
+  br i1 %cmp, label %if.then, label %if.else
+
+if.then:                                          ; preds = %entry
+  br label %if.end
+
+if.else:                                          ; preds = %entry
+  br label %if.end
+
+if.end:                                           ; preds = %if.else, %if.then
+  %x.0 = phi i32 [ 1, %if.then ], [ 2, %if.else ]
+  %or = or i32 %x.0, %a
+  %cmp1 = icmp eq i32 %or, 0
+  ret i1 %cmp1
+}
+
Index: lib/Analysis/ValueTracking.cpp
===================================================================
--- lib/Analysis/ValueTracking.cpp
+++ lib/Analysis/ValueTracking.cpp
@@ -2062,6 +2062,17 @@
         }
       }
     }
+    // Check if all incoming values are non-zero constant.
+    bool HasUnknowValue = false;
+    for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
+      if (Constant *CI = dyn_cast<Constant>(PN->getIncomingValue(i)))
+        if (!CI->isZeroValue())
+          continue;
+      HasUnknowValue = true;
+      break;
+    }
+    if (!HasUnknowValue)
+      return true;
   }
 
   if (!BitWidth) return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16735.46418.patch
Type: text/x-patch
Size: 1509 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160129/07df3c8e/attachment.bin>


More information about the llvm-commits mailing list