]> granicus.if.org Git - llvm/commitdiff
[InstrSimplify] fold sdiv if two operands are negated and non-overflow
authorChen Zheng <shchenz@cn.ibm.com>
Sat, 21 Jul 2018 12:27:54 +0000 (12:27 +0000)
committerChen Zheng <shchenz@cn.ibm.com>
Sat, 21 Jul 2018 12:27:54 +0000 (12:27 +0000)
Differential Revision: https://reviews.llvm.org/D49382

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337642 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/ValueTracking.h
lib/Analysis/InstructionSimplify.cpp
lib/Analysis/ValueTracking.cpp
test/Transforms/InstSimplify/sdiv.ll

index 0b6bdf38cbfc2c1c4fc7d433fa68b0ccffb6a6d4..e6a219a8045b845fd8a5504bd111d25005e6ef98 100644 (file)
@@ -102,7 +102,10 @@ class Value;
                       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: <X, Y> if X = sub (0, Y) or Y = sub (0, X)
+  /// 2: <X, Y> 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 e095e95935bbe7924961946da520bbfeaa67c0de..519d6d67be510b322f6f70e28442f8e29215080b 100644 (file)
@@ -1081,6 +1081,10 @@ static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
 /// 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);
 }
 
@@ -1111,7 +1115,7 @@ static Value *SimplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
 
   // If the two operands are negated, return 0.
   if (isKnownNegation(Op0, Op1))
-   return ConstantInt::getNullValue(Op0->getType());
+    return ConstantInt::getNullValue(Op0->getType());
 
   return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
 }
index 054f981bab630ff8084a70393068412930261d28..54828e392d4aa1f11ee89e72b6e8cd580fa890b9 100644 (file)
@@ -4511,21 +4511,25 @@ static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
   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 be26f5712e7fe2a3078509c662a5d57fbebaf0a7..76ba16800c2741277a2c54c0a11fa6078d0b8ffe 100644 (file)
@@ -3,9 +3,7 @@
 
 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
@@ -14,9 +12,7 @@ define i32 @negated_operand(i32 %x) {
 
 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> <i32 -1, i32 -1>
 ;
   %negx = sub nsw <2 x i32> zeroinitializer, %x
   %div = sdiv <2 x i32> %negx, %x
@@ -25,10 +21,7 @@ define <2 x i32> @negated_operand_commute_vec(<2 x i32> %x) {
 
 define i32 @knownnegation(i32 %x, i32 %y) {
 ; CHECK-LABEL: @knownnegation(
-; CHECK-NEXT:    [[XY:%.*]] = sub nsw i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[YX:%.*]] = sub nsw i32 [[Y]], [[X]]
-; CHECK-NEXT:    [[DIV:%.*]] = sdiv i32 [[XY]], [[YX]]
-; CHECK-NEXT:    ret i32 [[DIV]]
+; CHECK-NEXT:    ret i32 -1
 ;
   %xy = sub nsw i32 %x, %y
   %yx = sub nsw i32 %y, %x
@@ -38,10 +31,7 @@ define i32 @knownnegation(i32 %x, i32 %y) {
 
 define <2 x i32> @knownnegation_commute_vec(<2 x i32> %x, <2 x i32> %y) {
 ; CHECK-LABEL: @knownnegation_commute_vec(
-; CHECK-NEXT:    [[XY:%.*]] = sub nsw <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]]
+; CHECK-NEXT:    ret <2 x i32> <i32 -1, i32 -1>
 ;
   %xy = sub nsw <2 x i32> %x, %y
   %yx = sub nsw <2 x i32> %y, %x
@@ -51,10 +41,7 @@ define <2 x i32> @knownnegation_commute_vec(<2 x i32> %x, <2 x i32> %y) {
 
 define i32 @negated_operand_2(i32 %t) {
 ; CHECK-LABEL: @negated_operand_2(
-; CHECK-NEXT:    [[X:%.*]] = sub i32 [[T:%.*]], 5
-; 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
 ;
   %x = sub i32 %t, 5 
   %negx = sub nsw i32 0, %x
@@ -64,9 +51,7 @@ define i32 @negated_operand_2(i32 %t) {
 
 define i32 @negated_operand_commute(i32 %x) {
 ; CHECK-LABEL: @negated_operand_commute(
-; CHECK-NEXT:    [[NEGX:%.*]] = sub nsw i32 0, [[X]]
-; CHECK-NEXT:    [[DIV:%.*]] = sdiv i32 [[X]], [[NEGX]]
-; CHECK-NEXT:    ret i32 [[DIV]]
+; CHECK-NEXT:    ret i32 -1
 ;
   %negx = sub nsw i32 0, %x
   %div = sdiv i32 %x, %negx
@@ -175,9 +160,7 @@ define <2 x i32> @knownnegation_commute_vec_bad3(<2 x i32> %x, <2 x i32> %y) {
 
 define <3 x i32> @negated_operand_vec_undef(<3 x i32> %x) {
 ; CHECK-LABEL: @negated_operand_vec_undef(
-; CHECK-NEXT:    [[NEGX:%.*]] = sub nsw <3 x i32> <i32 0, i32 undef, i32 0>, [[X:%.*]]
-; CHECK-NEXT:    [[DIV:%.*]] = sdiv <3 x i32> [[NEGX]], [[X]]
-; CHECK-NEXT:    ret <3 x i32> [[DIV]]
+; CHECK-NEXT:    ret <3 x i32> <i32 -1, i32 -1, i32 -1>
 ;
   %negx = sub nsw <3 x i32> <i32 0, i32 undef, i32 0>, %x
   %div = sdiv <3 x i32> %negx, %x