[PATCH] D117252: [InstCombine] Fold ashr-exact into a icmp-ugt.

Nadav Rotem via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 13 14:28:07 PST 2022


nadav created this revision.
nadav added reviewers: spatel, nikic, aqjune.
Herald added a subscriber: hiraditya.
nadav requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This commit optimizes the code sequence  icmp-ugt (ashr-exact (X, C1 <https://reviews.llvm.org/C1>), C2).  This pattern is found in the std::vector bounds checks code of the at() method.

  Instcombine already implements this optimization for sgt, and this patch adds support for ugt.

Alive2 check that the transformation is correct:

define i64 @src(i64 %a) {
%1 = ashr exact i64 %a, 2
%2 = icmp ugt i64 %1, 400
%3 = zext i1 %2 to i64
ret i64 %3
}

define i64 @tgt(i64 %a) {
%1 = icmp ugt i64 %a, 1600
%2 = zext i1 %1 to i64
ret i64 %2
}


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117252

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll


Index: llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll
+++ llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll
@@ -2216,6 +2216,15 @@
   ret i1 %c
 }
 
+define i1 @lashrugt_00_00_exact(i8 %x) {
+; CHECK-LABEL: @lashrugt_00_00_exact(
+; CHECK-NEXT:    %c = icmp ugt i8 %x, 80
+;
+  %s = ashr exact i8 %x, 3
+  %c = icmp ugt i8 %s, 10
+  ret i1 %c
+}
+
 define i1 @lshrult_01_00_exact(i4 %x) {
 ; CHECK-LABEL: @lshrult_01_00_exact(
 ; CHECK-NEXT:    ret i1 false
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2239,9 +2239,11 @@
   // those conditions rather than checking them. This is difficult because of
   // undef/poison (PR34838).
   if (IsAShr) {
-    if (Pred == CmpInst::ICMP_SLT || (Pred == CmpInst::ICMP_SGT && IsExact)) {
+    if (Pred == CmpInst::ICMP_SLT || (Pred == CmpInst::ICMP_SGT && IsExact) ||
+        (Pred == CmpInst::ICMP_UGT && IsExact)) {
       // icmp slt (ashr X, ShAmtC), C --> icmp slt X, (C << ShAmtC)
       // icmp sgt (ashr exact X, ShAmtC), C --> icmp sgt X, (C << ShAmtC)
+      // icmp ugt (ashr exact X, ShAmtC), C --> icmp ugt X, (C << ShAmtC)
       APInt ShiftedC = C.shl(ShAmtVal);
       if (ShiftedC.ashr(ShAmtVal) == C)
         return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117252.399787.patch
Type: text/x-patch
Size: 1572 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220113/f82f9ebe/attachment.bin>


More information about the llvm-commits mailing list