diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index b76be357f316..754b791aff87 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3157,10 +3157,12 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant( } break; case Instruction::Add: { - // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. - if (Constant *BOC = dyn_cast(BOp1)) { + // (A + C2) == C --> A == (C - C2) + // (A + C2) != C --> A != (C - C2) + // TODO: Remove the one-use limitation? See discussion in D58633. + if (Constant *C2 = dyn_cast(BOp1)) { if (BO->hasOneUse()) - return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, BOC)); + return new ICmpInst(Pred, BOp0, ConstantExpr::getSub(RHS, C2)); } else if (C.isZero()) { // Replace ((add A, B) != 0) with (A != -B) if A or B is // efficiently invertible, or if the add has just this one use.