[PATCH] D123486: [InstCombine] fold more constant remainder to select-of-constants remainder

LiqinWeng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 11 02:20:38 PDT 2022


Miss_Grape updated this revision to Diff 421851.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D123486/new/

https://reviews.llvm.org/D123486

Files:
  llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  llvm/test/Transforms/InstCombine/rem.ll


Index: llvm/test/Transforms/InstCombine/rem.ll
===================================================================
--- llvm/test/Transforms/InstCombine/rem.ll
+++ llvm/test/Transforms/InstCombine/rem.ll
@@ -742,8 +742,8 @@
 
 define i1 @positive_and_odd_eq(i32 %A) {
 ; CHECK-LABEL: @positive_and_odd_eq(
-; CHECK-NEXT:    [[B:%.*]] = and i32 [[A:%.*]], -2147483647
-; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[B]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[A:%.*]], -2147483647
+; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[TMP1]], 1
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %B = srem i32 %A, 2
@@ -764,8 +764,8 @@
 
 define i1 @positive_and_odd_ne(i32 %A) {
 ; CHECK-LABEL: @positive_and_odd_ne(
-; CHECK-NEXT:    [[B:%.*]] = and i32 [[A:%.*]], -2147483647
-; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[B]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[A:%.*]], -2147483647
+; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[TMP1]], 1
 ; CHECK-NEXT:    ret i1 [[C]]
 ;
   %B = srem i32 %A, 2
@@ -807,13 +807,11 @@
   ret i32 %r
 }
 
-; TODO: srem should still be replaced by select.
-
 define i32 @srem_constant_dividend_select_of_constants_divisor_use(i1 %b) {
 ; CHECK-LABEL: @srem_constant_dividend_select_of_constants_divisor_use(
 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[B:%.*]], i32 12, i32 -3
 ; CHECK-NEXT:    call void @use(i32 [[S]])
-; CHECK-NEXT:    [[R:%.*]] = srem i32 42, [[S]]
+; CHECK-NEXT:    [[R:%.*]] = select i1 [[B]], i32 6, i32 0
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %s = select i1 %b, i32 12, i32 -3
@@ -915,13 +913,11 @@
   ret i32 %r
 }
 
-; TODO: urem should still be replaced by select.
-
 define i32 @urem_constant_dividend_select_of_constants_divisor_use(i1 %b) {
 ; CHECK-LABEL: @urem_constant_dividend_select_of_constants_divisor_use(
 ; CHECK-NEXT:    [[S:%.*]] = select i1 [[B:%.*]], i32 12, i32 -3
 ; CHECK-NEXT:    call void @use(i32 [[S]])
-; CHECK-NEXT:    [[R:%.*]] = urem i32 42, [[S]]
+; CHECK-NEXT:    [[R:%.*]] = select i1 [[B]], i32 6, i32 42
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
   %s = select i1 %b, i32 12, i32 -3
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1428,7 +1428,8 @@
   // TODO: Adapt simplifyDivRemOfSelectWithZeroOp to allow this and other folds.
   if (match(Op0, m_ImmConstant()) &&
       match(Op1, m_Select(m_Value(), m_ImmConstant(), m_ImmConstant()))) {
-    if (Instruction *R = FoldOpIntoSelect(I, cast<SelectInst>(Op1)))
+    if (Instruction *R = FoldOpIntoSelect(I, cast<SelectInst>(Op1),
+                                          /*FoldWithMultiUse*/ true))
       return R;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123486.421851.patch
Type: text/x-patch
Size: 2756 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220411/a6510e16/attachment.bin>


More information about the llvm-commits mailing list