[PATCH] D25913: [InstCombine] Fold nuw left-shifts in `ugt`/`ule` comparisons.

bryant via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 24 10:04:58 PDT 2016


bryant created this revision.
bryant added reviewers: spatel, RKSimon.
bryant added a subscriber: llvm-commits.
bryant set the repository for this revision to rL LLVM.

This transforms

  %a = shl nuw %x, c1
  %b = icmp {ugt|ule} %a, c0

into

  %b = icmp {ugt|ule} %x, (c0 >> c1)

z3:

  (declare-const x (_ BitVec 64))
  (declare-const c0 (_ BitVec 64))
  (declare-const c1 (_ BitVec 64))
  
  (push)
  (assert (= x (bvlshr (bvshl x c1) c1)))  ; nuw
  (assert (not (= (bvugt (bvshl x c1) c0)
                  (bvugt x
                         (bvlshr c0 c1)))))
  (check-sat)
  (get-model)
  (pop)
  
  (push)
  (assert (= x (bvlshr (bvshl x c1) c1)))  ; nuw
  (assert (not (= (bvule (bvshl x c1) c0)
                  (bvule x
                         (bvlshr c0 c1)))))
  (check-sat)
  (get-model)
  (pop)


Repository:
  rL LLVM

https://reviews.llvm.org/D25913

Files:
  lib/Transforms/InstCombine/InstCombineCompares.cpp
  test/Transforms/InstCombine/icmp-shl-zext-simplify.ll


Index: test/Transforms/InstCombine/icmp-shl-zext-simplify.ll
===================================================================
--- /dev/null
+++ test/Transforms/InstCombine/icmp-shl-zext-simplify.ll
@@ -0,0 +1,73 @@
+; RUN: opt %s -instcombine -S | FileCheck %s
+
+define i32 @icmp_ugt_16(i16) {
+; CHECK-LABEL: @icmp_ugt_16
+; CHECK-NEXT: icmp ne i16 %0, 0
+; CHECK-NEXT: zext
+; CHECK-NEXT: ret
+  %b = zext i16 %0 to i64
+  %c = shl i64 %b, 16
+  %d = icmp ugt i64 %c, 65535
+  %rv = zext i1 %d to i32
+  ret i32 %rv
+}
+
+define i32 @icmp_ule_16(i16) {
+; CHECK-LABEL: @icmp_ule_16
+; CHECK-NEXT: icmp eq i16 %0, 0
+; CHECK-NEXT: zext
+; CHECK-NEXT: ret
+  %b = zext i16 %0 to i64
+  %c = shl i64 %b, 16
+  %d = icmp ule i64 %c, 65535
+  %rv = zext i1 %d to i32
+  ret i32 %rv
+}
+
+define i32 @icmp_ugt_32(i32) {
+; CHECK-LABEL: @icmp_ugt_32
+; CHECK-NEXT: icmp ne i32 %0, 0
+; CHECK-NEXT: zext
+; CHECK-NEXT: ret
+  %b = zext i32 %0 to i64
+  %c = shl i64 %b, 32
+  %d = icmp ugt i64 %c, 4294967295
+  %rv = zext i1 %d to i32
+  ret i32 %rv
+}
+
+define i32 @icmp_ule_32(i32) {
+; CHECK-LABEL: @icmp_ule_32
+; CHECK-NEXT: icmp eq i32 %0, 0
+; CHECK-NEXT: zext
+; CHECK-NEXT: ret
+  %b = zext i32 %0 to i64
+  %c = shl i64 %b, 32
+  %d = icmp ule i64 %c, 4294967295
+  %rv = zext i1 %d to i32
+  ret i32 %rv
+}
+
+define i32 @icmp_ugt_64(i64) {
+; CHECK-LABEL: @icmp_ugt_64
+; CHECK-NEXT: icmp ne i64 %0, 0
+; CHECK-NEXT: zext
+; CHECK-NEXT: ret
+  %b = zext i64 %0 to i128
+  %c = shl i128 %b, 64
+  %d = icmp ugt i128 %c, 18446744073709551615
+  %rv = zext i1 %d to i32
+  ret i32 %rv
+}
+
+define i32 @icmp_ule_64(i64) {
+; CHECK-LABEL: @icmp_ule_64
+; CHECK-NEXT: icmp eq i64 %0, 0
+; CHECK-NEXT: zext
+; CHECK-NEXT: ret
+  %b = zext i64 %0 to i128
+  %c = shl i128 %b, 64
+  %d = icmp ule i128 %c, 18446744073709551615
+  %rv = zext i1 %d to i32
+  ret i32 %rv
+}
Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1966,6 +1966,15 @@
     return new ICmpInst(Pred, Builder->CreateTrunc(X, TruncTy), NewC);
   }
 
+  // when the shift is nuw and pred is >u or <=, then comparison only really
+  // happens in the pre-shifted bits.
+  if (Shl->hasNoUnsignedWrap() &&
+      (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) {
+    Type *CTy = IntegerType::get(Cmp.getContext(), C->getBitWidth());
+    Value *NewC = ConstantInt::get(CTy, C->lshr(*ShiftAmt));
+    return new ICmpInst(Pred, X, NewC);
+  }
+
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25913.75607.patch
Type: text/x-patch
Size: 2640 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161024/82163fa5/attachment.bin>


More information about the llvm-commits mailing list