Index: llvm/include/llvm/Analysis/ValueTracking.h =================================================================== --- llvm/include/llvm/Analysis/ValueTracking.h +++ llvm/include/llvm/Analysis/ValueTracking.h @@ -102,7 +102,10 @@ const DominatorTree *DT = nullptr); /// Return true if the two given values are negation. - bool isKnownNegation(const Value *X, const Value *Y); + /// Currently can recoginze Value pair: + /// 1: if X = sub (0, Y) or Y = sub (0, X) + /// 2: if X = sub (A, B) and Y = sub (B, A) + bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW = false); /// Returns true if the give value is known to be non-negative. bool isKnownNonNegative(const Value *V, const DataLayout &DL, Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -1081,6 +1081,10 @@ /// If not, this returns null. static Value *SimplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse) { + // If two operands are negated and no signed overflow, return -1. + if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true)) + return Constant::getAllOnesValue(Op0->getType()); + return simplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse); } Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -4511,21 +4511,25 @@ return {SPF_UNKNOWN, SPNB_NA, false}; } -bool llvm::isKnownNegation(const Value *X, const Value *Y) { +bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) { assert(X && Y && "Invalid operand"); - // X = sub (0, Y) - if (match(X, m_Neg(m_Specific(Y)))) + // X = sub (0, Y) || X = sub nsw (0, Y) + if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) || + (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y))))) return true; - // Y = sub (0, X) - if (match(Y, m_Neg(m_Specific(X)))) + // Y = sub (0, X) || Y = sub nsw (0, X) + if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) || + (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X))))) return true; - // X = sub (A, B), Y = sub (B, A) + // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A) Value *A, *B; - return match(X, m_Sub(m_Value(A), m_Value(B))) && - match(Y, m_Sub(m_Specific(B), m_Specific(A))); + return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) && + match(Y, m_Sub(m_Specific(B), m_Specific(A))))) || + (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) && + match(Y, m_NSWSub(m_Specific(B), m_Specific(A))))); } static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, Index: llvm/test/Transforms/InstSimplify/sdiv.ll =================================================================== --- llvm/test/Transforms/InstSimplify/sdiv.ll +++ llvm/test/Transforms/InstSimplify/sdiv.ll @@ -1,10 +1,9 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -instsimplify -S | FileCheck %s define i32 @negated_operand(i32 %x) { ; CHECK-LABEL: @negated_operand( -; CHECK-NEXT: [[NEGX:%.*]] = sub nsw i32 0, [[X:%.*]] -; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[NEGX]], [[X]] -; CHECK-NEXT: ret i32 [[DIV]] +; CHECK-NEXT: ret i32 -1 ; %negx = sub nsw i32 0, %x %div = sdiv i32 %negx, %x @@ -13,9 +12,7 @@ define <2 x i32> @negated_operand_commute_vec(<2 x i32> %x) { ; CHECK-LABEL: @negated_operand_commute_vec( -; CHECK-NEXT: [[NEGX:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X:%.*]] -; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[NEGX]], [[X]] -; CHECK-NEXT: ret <2 x i32> [[DIV]] +; CHECK-NEXT: ret <2 x i32> ; %negx = sub nsw <2 x i32> zeroinitializer, %x %div = sdiv <2 x i32> %negx, %x @@ -24,26 +21,159 @@ define i32 @knownnegation(i32 %x, i32 %y) { ; CHECK-LABEL: @knownnegation( +; CHECK-NEXT: ret i32 -1 +; + %xy = sub nsw i32 %x, %y + %yx = sub nsw i32 %y, %x + %div = sdiv i32 %xy, %yx + ret i32 %div +} + +define <2 x i32> @knownnegation_commute_vec(<2 x i32> %x, <2 x i32> %y) { +; CHECK-LABEL: @knownnegation_commute_vec( +; CHECK-NEXT: ret <2 x i32> +; + %xy = sub nsw <2 x i32> %x, %y + %yx = sub nsw <2 x i32> %y, %x + %div = sdiv <2 x i32> %xy, %yx + ret <2 x i32> %div +} + +define i32 @negated_operand_2(i32 %t) { +; CHECK-LABEL: @negated_operand_2( +; CHECK-NEXT: ret i32 -1 +; + %x = sub i32 %t, 5 + %negx = sub nsw i32 0, %x + %div = sdiv i32 %negx, %x + ret i32 %div +} + +define i32 @negated_operand_commute(i32 %x) { +; CHECK-LABEL: @negated_operand_commute( +; CHECK-NEXT: ret i32 -1 +; + %negx = sub nsw i32 0, %x + %div = sdiv i32 %x, %negx + ret i32 %div +} + +define i32 @negated_operand_bad(i32 %x) { +; CHECK-LABEL: @negated_operand_bad( +; CHECK-NEXT: [[NEGX:%.*]] = sub i32 0, [[X:%.*]] +; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[NEGX]], [[X]] +; CHECK-NEXT: ret i32 [[DIV]] +; + %negx = sub i32 0, %x ; not nsw + %div = sdiv i32 %negx, %x + ret i32 %div +} + +define i32 @knownnegation_bad_1(i32 %x, i32 %y) { +; CHECK-LABEL: @knownnegation_bad_1( ; CHECK-NEXT: [[XY:%.*]] = sub nsw i32 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[YX:%.*]] = sub nsw i32 [[Y]], [[X]] +; CHECK-NEXT: [[YX:%.*]] = sub i32 [[Y]], [[X]] ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[XY]], [[YX]] ; CHECK-NEXT: ret i32 [[DIV]] ; %xy = sub nsw i32 %x, %y + %yx = sub i32 %y, %x ; not nsw + %div = sdiv i32 %xy, %yx + ret i32 %div +} + +define i32 @knownnegation_bad_2(i32 %x, i32 %y) { +; CHECK-LABEL: @knownnegation_bad_2( +; CHECK-NEXT: [[XY:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[YX:%.*]] = sub nsw i32 [[Y]], [[X]] +; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[XY]], [[YX]] +; CHECK-NEXT: ret i32 [[DIV]] +; + %xy = sub i32 %x, %y ; not nsw %yx = sub nsw i32 %y, %x %div = sdiv i32 %xy, %yx ret i32 %div } -define <2 x i32> @knownnegation_commute_vec(<2 x i32> %x, <2 x i32> %y) { -; CHECK-LABEL: @knownnegation_commute_vec( +define i32 @knownnegation_bad_3(i32 %x, i32 %y) { +; CHECK-LABEL: @knownnegation_bad_3( +; CHECK-NEXT: [[XY:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[YX:%.*]] = sub i32 [[Y]], [[X]] +; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[XY]], [[YX]] +; CHECK-NEXT: ret i32 [[DIV]] +; + %xy = sub i32 %x, %y ; not nsw + %yx = sub i32 %y, %x ; not nsw + %div = sdiv i32 %xy, %yx + ret i32 %div +} + +define <2 x i32> @negated_operand_commute_vec_bad(<2 x i32> %x) { +; CHECK-LABEL: @negated_operand_commute_vec_bad( +; CHECK-NEXT: [[NEGX:%.*]] = sub <2 x i32> zeroinitializer, [[X:%.*]] +; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[NEGX]], [[X]] +; CHECK-NEXT: ret <2 x i32> [[DIV]] +; + %negx = sub <2 x i32> zeroinitializer, %x ; not nsw + %div = sdiv <2 x i32> %negx, %x + ret <2 x i32> %div +} + +define <2 x i32> @knownnegation_commute_vec_bad1(<2 x i32> %x, <2 x i32> %y) { +; CHECK-LABEL: @knownnegation_commute_vec_bad1( ; CHECK-NEXT: [[XY:%.*]] = sub nsw <2 x i32> [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[YX:%.*]] = sub nsw <2 x i32> [[Y]], [[X]] +; CHECK-NEXT: [[YX:%.*]] = sub <2 x i32> [[Y]], [[X]] ; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[XY]], [[YX]] ; CHECK-NEXT: ret <2 x i32> [[DIV]] ; %xy = sub nsw <2 x i32> %x, %y + %yx = sub <2 x i32> %y, %x ; not nsw + %div = sdiv <2 x i32> %xy, %yx + ret <2 x i32> %div +} + +define <2 x i32> @knownnegation_commute_vec_bad2(<2 x i32> %x, <2 x i32> %y) { +; CHECK-LABEL: @knownnegation_commute_vec_bad2( +; CHECK-NEXT: [[XY:%.*]] = sub <2 x i32> [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[YX:%.*]] = sub nsw <2 x i32> [[Y]], [[X]] +; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[XY]], [[YX]] +; CHECK-NEXT: ret <2 x i32> [[DIV]] +; + %xy = sub <2 x i32> %x, %y ; not nsw %yx = sub nsw <2 x i32> %y, %x %div = sdiv <2 x i32> %xy, %yx ret <2 x i32> %div } + +define <2 x i32> @knownnegation_commute_vec_bad3(<2 x i32> %x, <2 x i32> %y) { +; CHECK-LABEL: @knownnegation_commute_vec_bad3( +; CHECK-NEXT: [[XY:%.*]] = sub <2 x i32> [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[YX:%.*]] = sub <2 x i32> [[Y]], [[X]] +; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[XY]], [[YX]] +; CHECK-NEXT: ret <2 x i32> [[DIV]] +; + %xy = sub <2 x i32> %x, %y ; not nsw + %yx = sub <2 x i32> %y, %x ; not nsw + %div = sdiv <2 x i32> %xy, %yx + ret <2 x i32> %div +} + +define <3 x i32> @negated_operand_vec_undef(<3 x i32> %x) { +; CHECK-LABEL: @negated_operand_vec_undef( +; CHECK-NEXT: ret <3 x i32> +; + %negx = sub nsw <3 x i32> , %x + %div = sdiv <3 x i32> %negx, %x + ret <3 x i32> %div +} + +define <2 x i32> @negated_operand_vec_nonsplat(<2 x i32> %x) { +; CHECK-LABEL: @negated_operand_vec_nonsplat( +; CHECK-NEXT: [[NEGX:%.*]] = sub nsw <2 x i32> , [[X:%.*]] +; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[NEGX]], [[X]] +; CHECK-NEXT: ret <2 x i32> [[DIV]] +; + %negx = sub nsw <2 x i32> , %x ; not 0, don't fold + %div = sdiv <2 x i32> %negx, %x + ret <2 x i32> %div +}