From ecb7ea1ae7c6658771f6de4a957f0ddcc1cf4a8d Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Thu, 5 Sep 2019 17:40:49 +0000 Subject: [PATCH] [InstCombine] foldICmpBinOp(): consider inverted check in 'unsigned add overflow' check A follow-up for r342004. This will be changed to produce @llvm.add.with.overflow in a later patch, but for now just make things more consistent overall. https://rise4fun.com/Alive/qxE Name: (Op1 + X) u< Op1 --> ~Op1 u< X %t0 = add i8 %Op1, %X %r = icmp ult i8 %t0, %Op1 => %n = xor i8 %Op1, -1 %r = icmp ult i8 %n, %X Name: (Op1 + X) u>= Op1 --> ~Op1 u>= X %t0 = add i8 %Op1, %X %r = icmp uge i8 %t0, %Op1 => %n = xor i8 %Op1, -1 %r = icmp uge i8 %n, %X ;------------------------------------------------------------------------------- Name: Op0 u> (Op0 + X) --> X u> ~Op0 %t0 = add i8 %Op0, %X %r = icmp ugt i8 %Op0, %t0 => %n = xor i8 %Op0, -1 %r = icmp ugt i8 %X, %n Name: Op0 u<= (Op0 + X) --> X u<= ~Op0 %t0 = add i8 %Op0, %X %r = icmp ule i8 %Op0, %t0 => %n = xor i8 %Op0, -1 %r = icmp ule i8 %X, %n llvm-svn: 371100 --- .../Transforms/InstCombine/InstCombineCompares.cpp | 8 ++++---- .../unsigned-add-lack-of-overflow-check.ll | 24 +++++++++++----------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 8cb0a26..65c0d2d 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3609,13 +3609,13 @@ Instruction *InstCombiner::foldICmpBinOp(ICmpInst &I) { Value *X; // Convert add-with-unsigned-overflow comparisons into a 'not' with compare. - // (Op1 + X) ~Op1 u (Op0 + X) --> X >u ~Op0 + // (Op1 + X) u= Op1 --> ~Op1 u= X if (match(Op0, m_OneUse(m_c_Add(m_Specific(Op1), m_Value(X)))) && - Pred == ICmpInst::ICMP_ULT) + (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_UGE)) return new ICmpInst(Pred, Builder.CreateNot(Op1), X); + // Op0 u>/u<= (Op0 + X) --> X u>/u<= ~Op0 if (match(Op1, m_OneUse(m_c_Add(m_Specific(Op0), m_Value(X)))) && - Pred == ICmpInst::ICMP_UGT) + (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULE)) return new ICmpInst(Pred, X, Builder.CreateNot(Op0)); bool NoOp0WrapProblem = false, NoOp1WrapProblem = false; diff --git a/llvm/test/Transforms/InstCombine/unsigned-add-lack-of-overflow-check.ll b/llvm/test/Transforms/InstCombine/unsigned-add-lack-of-overflow-check.ll index e9ba7d4..30e7d54 100644 --- a/llvm/test/Transforms/InstCombine/unsigned-add-lack-of-overflow-check.ll +++ b/llvm/test/Transforms/InstCombine/unsigned-add-lack-of-overflow-check.ll @@ -10,8 +10,8 @@ define i1 @t0_basic(i8 %x, i8 %y) { ; CHECK-LABEL: @t0_basic( -; CHECK-NEXT: [[T0:%.*]] = add i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[T0]], [[Y]] +; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[Y:%.*]], -1 +; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %t0 = add i8 %x, %y @@ -21,8 +21,8 @@ define i1 @t0_basic(i8 %x, i8 %y) { define <2 x i1> @t1_vec(<2 x i8> %x, <2 x i8> %y) { ; CHECK-LABEL: @t1_vec( -; CHECK-NEXT: [[T0:%.*]] = add <2 x i8> [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp uge <2 x i8> [[T0]], [[Y]] +; CHECK-NEXT: [[TMP1:%.*]] = xor <2 x i8> [[Y:%.*]], +; CHECK-NEXT: [[R:%.*]] = icmp uge <2 x i8> [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret <2 x i1> [[R]] ; %t0 = add <2 x i8> %x, %y @@ -34,8 +34,8 @@ define <2 x i1> @t1_vec(<2 x i8> %x, <2 x i8> %y) { define i1 @t2_symmetry(i8 %x, i8 %y) { ; CHECK-LABEL: @t2_symmetry( -; CHECK-NEXT: [[T0:%.*]] = add i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[T0]], [[X]] +; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X:%.*]], -1 +; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[TMP1]], [[Y:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %t0 = add i8 %x, %y @@ -48,8 +48,8 @@ declare i8 @gen8() define i1 @t3_commutative(i8 %x) { ; CHECK-LABEL: @t3_commutative( ; CHECK-NEXT: [[Y:%.*]] = call i8 @gen8() -; CHECK-NEXT: [[T0:%.*]] = add i8 [[Y]], [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[T0]], [[Y]] +; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[Y]], -1 +; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %y = call i8 @gen8() @@ -60,8 +60,8 @@ define i1 @t3_commutative(i8 %x) { define i1 @t4_commutative(i8 %x, i8 %y) { ; CHECK-LABEL: @t4_commutative( -; CHECK-NEXT: [[T0:%.*]] = add i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[T0]], [[Y]] +; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[Y:%.*]], -1 +; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %t0 = add i8 %x, %y @@ -72,8 +72,8 @@ define i1 @t4_commutative(i8 %x, i8 %y) { define i1 @t5_commutative(i8 %x) { ; CHECK-LABEL: @t5_commutative( ; CHECK-NEXT: [[Y:%.*]] = call i8 @gen8() -; CHECK-NEXT: [[T0:%.*]] = add i8 [[Y]], [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp ule i8 [[Y]], [[T0]] +; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[Y]], -1 +; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %y = call i8 @gen8() -- 2.7.4