[llvm] r372510 - [InstCombine] allow icmp+binop folds before min/max bailout (PR43310)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 22 07:31:53 PDT 2019


Author: spatel
Date: Sun Sep 22 07:31:53 2019
New Revision: 372510

URL: http://llvm.org/viewvc/llvm-project?rev=372510&view=rev
Log:
[InstCombine] allow icmp+binop folds before min/max bailout (PR43310)

This has the potential to uncover missed analysis/folds as shown in the
min/max code comment/test, but fewer restrictions on icmp folds should
be better in general to solve cases like:
https://bugs.llvm.org/show_bug.cgi?id=43310

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/trunk/test/Transforms/InstCombine/icmp.ll
    llvm/trunk/test/Transforms/InstCombine/minmax-fold.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=372510&r1=372509&r2=372510&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Sun Sep 22 07:31:53 2019
@@ -5394,6 +5394,9 @@ Instruction *InstCombiner::visitICmpInst
   if (Instruction *Res = foldICmpWithDominatingICmp(I))
     return Res;
 
+  if (Instruction *Res = foldICmpBinOp(I))
+    return Res;
+
   if (Instruction *Res = foldICmpUsingKnownBits(I))
     return Res;
 
@@ -5471,9 +5474,6 @@ Instruction *InstCombiner::visitICmpInst
   if (Instruction *R = foldICmpWithCastOp(I))
     return R;
 
-  if (Instruction *Res = foldICmpBinOp(I))
-    return Res;
-
   if (Instruction *Res = foldICmpWithMinMax(I))
     return Res;
 

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp?rev=372510&r1=372509&r2=372510&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp Sun Sep 22 07:31:53 2019
@@ -1126,6 +1126,7 @@ static Value *simplifyWithOpReplaced(Val
 ///   %sel = select i1 %cmp, i32 -2147483648, i32 %add
 ///
 /// We can't replace %sel with %add unless we strip away the flags.
+/// TODO: Wrapping flags could be preserved in some cases with better analysis.
 static Value *foldSelectValueEquivalence(SelectInst &Sel, ICmpInst &Cmp,
                                          const SimplifyQuery &Q) {
   if (!Cmp.isEquality())

Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=372510&r1=372509&r2=372510&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Sun Sep 22 07:31:53 2019
@@ -683,12 +683,9 @@ define i1 @test37_extra_uses(i32 %x, i32
 
 define i32 @neg_max_s32(i32 %x, i32 %y) {
 ; CHECK-LABEL: @neg_max_s32(
-; CHECK-NEXT:    [[NX:%.*]] = sub nsw i32 0, [[X:%.*]]
-; CHECK-NEXT:    [[NY:%.*]] = sub nsw i32 0, [[Y:%.*]]
-; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[NX]], [[NY]]
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i32 [[NY]], i32 [[NX]]
-; CHECK-NEXT:    [[R:%.*]] = sub nsw i32 0, [[S]]
-; CHECK-NEXT:    ret i32 [[R]]
+; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT:    [[S_V:%.*]] = select i1 [[C]], i32 [[Y]], i32 [[X]]
+; CHECK-NEXT:    ret i32 [[S_V]]
 ;
   %nx = sub nsw i32 0, %x
   %ny = sub nsw i32 0, %y
@@ -700,12 +697,9 @@ define i32 @neg_max_s32(i32 %x, i32 %y)
 
 define <4 x i32> @neg_max_v4s32(<4 x i32> %x, <4 x i32> %y) {
 ; CHECK-LABEL: @neg_max_v4s32(
-; CHECK-NEXT:    [[NX:%.*]] = sub nsw <4 x i32> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT:    [[NY:%.*]] = sub nsw <4 x i32> zeroinitializer, [[Y:%.*]]
-; CHECK-NEXT:    [[C:%.*]] = icmp sgt <4 x i32> [[NX]], [[NY]]
-; CHECK-NEXT:    [[S:%.*]] = select <4 x i1> [[C]], <4 x i32> [[NX]], <4 x i32> [[NY]]
-; CHECK-NEXT:    [[R:%.*]] = sub <4 x i32> zeroinitializer, [[S]]
-; CHECK-NEXT:    ret <4 x i32> [[R]]
+; CHECK-NEXT:    [[C:%.*]] = icmp sgt <4 x i32> [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT:    [[S_V:%.*]] = select <4 x i1> [[C]], <4 x i32> [[X]], <4 x i32> [[Y]]
+; CHECK-NEXT:    ret <4 x i32> [[S_V]]
 ;
   %nx = sub nsw <4 x i32> zeroinitializer, %x
   %ny = sub nsw <4 x i32> zeroinitializer, %y

Modified: llvm/trunk/test/Transforms/InstCombine/minmax-fold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/minmax-fold.ll?rev=372510&r1=372509&r2=372510&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/minmax-fold.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/minmax-fold.ll Sun Sep 22 07:31:53 2019
@@ -1080,8 +1080,8 @@ define i37 @add_umax_constant_limit(i37
 
 define i37 @add_umax_simplify(i37 %x) {
 ; CHECK-LABEL: @add_umax_simplify(
-; CHECK-NEXT:    [[R:%.*]] = add nuw i37 [[X:%.*]], 42
-; CHECK-NEXT:    ret i37 [[R]]
+; CHECK-NEXT:    [[A:%.*]] = add i37 [[X:%.*]], 42
+; CHECK-NEXT:    ret i37 [[A]]
 ;
   %a = add nuw i37 %x, 42
   %c = icmp ugt i37 %a, 42




More information about the llvm-commits mailing list