[PATCH] D45855: [InstCombine] Support BitTests in ThreeWayComparison. General case, part 1

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 20 02:24:00 PDT 2018


mkazantsev updated this revision to Diff 143263.
mkazantsev added a comment.

Formatted tests.


https://reviews.llvm.org/D45855

Files:
  lib/Transforms/InstCombine/InstCombineCompares.cpp
  test/Transforms/InstCombine/three-way-comparison.ll


Index: test/Transforms/InstCombine/three-way-comparison.ll
===================================================================
--- test/Transforms/InstCombine/three-way-comparison.ll
+++ test/Transforms/InstCombine/three-way-comparison.ll
@@ -241,3 +241,69 @@
 exit:
   ret i32 42
 }
+
+
+; Same as @compare_against_arbitrary_value, but now the three-way comparison
+; returns not idiomatic comparator's result (-1, 0, 1) but some other constants.
+define i32 @compare_against_arbitrary_value_non_idiomatic_1(i32 %x, i32 %c) {
+; TODO: We can prove that if %x s< %c then %x != c, so there should be no actual
+;       calculations in callfoo block. @foo can be invoked with 425.
+; CHECK-LABEL: @compare_against_arbitrary_value_non_idiomatic_1(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], [[C:%.*]]
+; CHECK-NEXT:    br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]]
+; CHECK:       callfoo:
+; CHECK-NEXT:    call void @foo(i32 425)
+; CHECK-NEXT:    br label [[EXIT]]
+; CHECK:       exit:
+; CHECK-NEXT:    ret i32 42
+;
+
+entry:
+  %cmp1 = icmp eq i32 %x, %c
+  %cmp2 = icmp slt i32 %x, %c
+  %select1 = select i1 %cmp2, i32 -6, i32 425
+  %select2 = select i1 %cmp1, i32 0, i32 %select1
+  %cond = icmp sgt i32 %select2, 0
+  br i1 %cond, label %callfoo, label %exit
+
+callfoo:
+  call void @foo(i32 %select2)
+  br label %exit
+
+exit:
+  ret i32 42
+}
+
+define i32 @compare_against_zero_non_idiomatic_add(i32 %x) {
+; TODO: We can prove that if %x s< %c then %x != c, so there should be no actual
+;       calculations in callfoo block. @foo can be invoked with 425.
+; CHECK-LABEL: @compare_against_zero_non_idiomatic_add(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 0
+; CHECK-NEXT:    br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]]
+; CHECK:       callfoo:
+; CHECK-NEXT:    [[TMP1:%.*]] = ashr i32 [[X]], 31
+; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], -431
+; CHECK-NEXT:    [[TMP3:%.*]] = add nsw i32 [[TMP2]], 425
+; CHECK-NEXT:    call void @foo(i32 [[TMP3]])
+; CHECK-NEXT:    br label [[EXIT]]
+; CHECK:       exit:
+; CHECK-NEXT:    ret i32 42
+;
+
+entry:
+  %cmp1 = icmp eq i32 %x, 0
+  %cmp2 = icmp slt i32 %x, 0
+  %select1 = select i1 %cmp2, i32 -6, i32 425
+  %select2 = select i1 %cmp1, i32 0, i32 %select1
+  %cond = icmp sgt i32 %select2, 0
+  br i1 %cond, label %callfoo, label %exit
+
+callfoo:
+  call void @foo(i32 %select2)
+  br label %exit
+
+exit:
+  ret i32 42
+}
Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2427,6 +2427,20 @@
         Less = ConstantInt::get(Greater->getType(), -1, true);
         return true;
       }
+
+      // Try to recognize:
+      //   select i1 (a < 0), i32 Less, i32 Greater
+      // simplified in form:
+      //   (a s>> 31) & (Less - Greater) + Greater
+      ConstantInt *LessMinusGreater;
+      if (match(SI->getFalseValue(),
+                m_Add(m_And(m_AShr(m_Specific(LHS), m_Specific(ShiftValue)),
+                            m_ConstantInt(LessMinusGreater)),
+                      m_ConstantInt(Greater)))) {
+        APInt LessI = LessMinusGreater->getValue() + Greater->getValue();
+        Less = ConstantInt::get(Greater->getContext(), LessI);
+        return true;
+      }
     }
   }
   return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45855.143263.patch
Type: text/x-patch
Size: 3486 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180420/7e7c5ffd/attachment.bin>


More information about the llvm-commits mailing list