[InstCombine] allow icmp+binop folds before min/max bailout (PR43310)
authorSanjay Patel <spatel@rotateright.com>
Sun, 22 Sep 2019 14:31:53 +0000 (14:31 +0000)
committerSanjay Patel <spatel@rotateright.com>
Sun, 22 Sep 2019 14:31:53 +0000 (14:31 +0000)
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

llvm-svn: 372510

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

index e569ae8..5bafab4 100644 (file)
@@ -5394,6 +5394,9 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
   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(ICmpInst &I) {
   if (Instruction *R = foldICmpWithCastOp(I))
     return R;
 
-  if (Instruction *Res = foldICmpBinOp(I))
-    return Res;
-
   if (Instruction *Res = foldICmpWithMinMax(I))
     return Res;
 
index 6ccfac2..55ac820 100644 (file)
@@ -1126,6 +1126,7 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *ReplaceOp,
 ///   %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())
index 657f6ac..9008db9 100644 (file)
@@ -683,12 +683,9 @@ define i1 @test37_extra_uses(i32 %x, i32 %y, i32 %z) {
 
 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
index 264e579..4758856 100644 (file)
@@ -1080,8 +1080,8 @@ define i37 @add_umax_constant_limit(i37 %x) {
 
 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