[PATCH] D12799: [ValueTracking] Teach isKnownNonZero a new trick

James Molloy via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 14:41:22 PDT 2015


jmolloy updated this revision to Diff 35308.
jmolloy added a comment.

Hi Hal, Sanjoy, Philip,

Thanks very much for the reviews - sorry it took so long to address.

The newest version should address these comments.

Cheers,

James


Repository:
  rL LLVM

http://reviews.llvm.org/D12799

Files:
  lib/Analysis/ValueTracking.cpp
  test/Analysis/ValueTracking/knownzero-shift.ll

Index: test/Analysis/ValueTracking/knownzero-shift.ll
===================================================================
--- /dev/null
+++ test/Analysis/ValueTracking/knownzero-shift.ll
@@ -0,0 +1,15 @@
+; RUN: opt -instsimplify -S < %s | FileCheck %s
+
+; CHECK-LABEL: @test
+define i8 @test(i8 %p, i8* %pq) {
+  %q = load i8, i8* %pq, !range !0 ; %q is known nonzero; no known bits
+  %1 = or i8 %p, 1                 ; %1[0] = 1, %1 is odd
+  %2 = shl i8 %1, %q               ; %2[0] = 0, rest unknown but cannot be zero
+  %A = lshr i8 %2, 1               ; We should know that %A is nonzero.
+  %x = icmp eq i8 %A, 0
+  %y = select i1 %x, i8 6, i8 7    ; ... and should return i8 7.
+  ; CHECK: ret i8 7
+  ret i8 %y
+}
+
+!0 = !{ i8 1, i8 5 }
Index: lib/Analysis/ValueTracking.cpp
===================================================================
--- lib/Analysis/ValueTracking.cpp
+++ lib/Analysis/ValueTracking.cpp
@@ -1824,6 +1824,23 @@
     ComputeSignBit(X, XKnownNonNegative, XKnownNegative, DL, Depth, Q);
     if (XKnownNegative)
       return true;
+
+    // If the shifter operand is a constant, and all of the bits shifted
+    // out are known to be zero, and X is known non-zero then at least one
+    // non-zero bit must remain.
+    if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y)) {
+      APInt KnownZero(BitWidth, 0);
+      APInt KnownOne(BitWidth, 0);
+      computeKnownBits(X, KnownZero, KnownOne, DL, Depth, Q);
+      
+      auto ShiftVal = Shift->getLimitedValue(BitWidth - 1);
+      // Is there a known one in the portion not shifted out?
+      if (KnownOne.countLeadingZeros() < BitWidth - ShiftVal)
+        return true;
+      // Are all the bits to be shifted out known zero?
+      if (KnownZero.countTrailingOnes() >= ShiftVal)
+        return isKnownNonZero(X, DL, Depth, Q);
+    }
   }
   // div exact can only produce a zero if the dividend is zero.
   else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12799.35308.patch
Type: text/x-patch
Size: 1965 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150921/05db836d/attachment.bin>


More information about the llvm-commits mailing list