diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2928,12 +2928,9 @@ break; case Instruction::Add: { // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. - const APInt *BOC; - if (match(BOp1, m_APInt(BOC))) { - if (BO->hasOneUse()) { - Constant *SubC = ConstantExpr::getSub(RHS, cast(BOp1)); - return new ICmpInst(Pred, BOp0, SubC); - } + if (Constant *BOC = dyn_cast(BOp1)) { + if (BO->hasOneUse()) + return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, BOC)); } else if (C.isNullValue()) { // Replace ((add A, B) != 0) with (A != -B) if A or B is // efficiently invertible, or if the add has just this one use. @@ -2963,11 +2960,11 @@ break; case Instruction::Sub: if (BO->hasOneUse()) { - const APInt *BOC; - if (match(BOp0, m_APInt(BOC))) { + // Only check for constant LHS here, as constant RHS will be canonicalized + // to add and use the fold above. + if (Constant *BOC = dyn_cast(BOp0)) { // Replace ((sub BOC, B) != C) with (B != BOC-C). - Constant *SubC = ConstantExpr::getSub(cast(BOp0), RHS); - return new ICmpInst(Pred, BOp1, SubC); + return new ICmpInst(Pred, BOp1, ConstantExpr::getSub(BOC, RHS)); } else if (C.isNullValue()) { // Replace ((sub A, B) != 0) with (A != B). return new ICmpInst(Pred, BOp0, BOp1); diff --git a/llvm/test/Transforms/InstCombine/icmp-add.ll b/llvm/test/Transforms/InstCombine/icmp-add.ll --- a/llvm/test/Transforms/InstCombine/icmp-add.ll +++ b/llvm/test/Transforms/InstCombine/icmp-add.ll @@ -624,8 +624,7 @@ define <2 x i1> @icmp_eq_add_undef(<2 x i32> %a) { ; CHECK-LABEL: @icmp_eq_add_undef( -; CHECK-NEXT: [[ADD:%.*]] = add <2 x i32> [[A:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[ADD]], +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[A:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %add = add <2 x i32> %a, @@ -635,8 +634,7 @@ define <2 x i1> @icmp_eq_add_non_splat(<2 x i32> %a) { ; CHECK-LABEL: @icmp_eq_add_non_splat( -; CHECK-NEXT: [[ADD:%.*]] = add <2 x i32> [[A:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[ADD]], +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[A:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %add = add <2 x i32> %a, diff --git a/llvm/test/Transforms/InstCombine/icmp-sub.ll b/llvm/test/Transforms/InstCombine/icmp-sub.ll --- a/llvm/test/Transforms/InstCombine/icmp-sub.ll +++ b/llvm/test/Transforms/InstCombine/icmp-sub.ll @@ -146,8 +146,7 @@ define <2 x i1> @icmp_eq_sub_undef(<2 x i32> %a) { ; CHECK-LABEL: @icmp_eq_sub_undef( -; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i32> , [[A:%.*]] -; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[SUB]], +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[A:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %sub = sub <2 x i32> , %a @@ -157,8 +156,7 @@ define <2 x i1> @icmp_eq_sub_non_splat(<2 x i32> %a) { ; CHECK-LABEL: @icmp_eq_sub_non_splat( -; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i32> , [[A:%.*]] -; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[SUB]], +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[A:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %sub = sub <2 x i32> , %a