[llvm] r336881 - [InstSimplify] simplify add instruction if two operands are negative

Chen Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 11 20:06:05 PDT 2018


Author: shchenz
Date: Wed Jul 11 20:06:04 2018
New Revision: 336881

URL: http://llvm.org/viewvc/llvm-project?rev=336881&view=rev
Log:
[InstSimplify] simplify add instruction if two operands are negative

Differential Revision: https://reviews.llvm.org/D49216

Modified:
    llvm/trunk/include/llvm/Analysis/ValueTracking.h
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/lib/Analysis/ValueTracking.cpp
    llvm/trunk/test/Transforms/InstSimplify/add.ll

Modified: llvm/trunk/include/llvm/Analysis/ValueTracking.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/ValueTracking.h?rev=336881&r1=336880&r2=336881&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/ValueTracking.h (original)
+++ llvm/trunk/include/llvm/Analysis/ValueTracking.h Wed Jul 11 20:06:04 2018
@@ -101,6 +101,9 @@ class Value;
                       const Instruction *CxtI = nullptr,
                       const DominatorTree *DT = nullptr);
 
+  /// Return true if the two given values are negation.
+  bool isKnownNegation(const Value *X, const Value *Y);
+
   /// Returns true if the give value is known to be non-negative.
   bool isKnownNonNegative(const Value *V, const DataLayout &DL,
                           unsigned Depth = 0,

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=336881&r1=336880&r2=336881&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Wed Jul 11 20:06:04 2018
@@ -540,6 +540,10 @@ static Value *SimplifyAddInst(Value *Op0
   if (match(Op1, m_Zero()))
     return Op0;
 
+  // If two operands are negative, return 0.
+  if (isKnownNegation(Op0, Op1))
+    return Constant::getNullValue(Op0->getType());
+
   // X + (Y - X) -> Y
   // (Y - X) + X -> Y
   // Eg: X + -X -> 0

Modified: llvm/trunk/lib/Analysis/ValueTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ValueTracking.cpp?rev=336881&r1=336880&r2=336881&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ValueTracking.cpp (original)
+++ llvm/trunk/lib/Analysis/ValueTracking.cpp Wed Jul 11 20:06:04 2018
@@ -4511,6 +4511,26 @@ static SelectPatternResult matchMinMax(C
   return {SPF_UNKNOWN, SPNB_NA, false};
 }
 
+bool llvm::isKnownNegation(const Value *X, const Value *Y) {
+  assert(X && Y && "Invalid operand");
+
+  // X = sub (0, Y)
+  if (match(X, m_Neg(m_Specific(Y))))
+    return true;
+
+  // Y = sub (0, X)
+  if (match(Y, m_Neg(m_Specific(X))))
+    return true;
+
+  // X = sub (A, B), Y = sub (B, A)
+  Value *A, *B;
+  if (match(X, m_Sub(m_Value(A), m_Value(B))) &&
+      match(Y, m_Sub(m_Specific(B), m_Specific(A))))
+    return true;
+
+  return false;
+}
+
 static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
                                               FastMathFlags FMF,
                                               Value *CmpLHS, Value *CmpRHS,

Modified: llvm/trunk/test/Transforms/InstSimplify/add.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/add.ll?rev=336881&r1=336880&r2=336881&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/add.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/add.ll Wed Jul 11 20:06:04 2018
@@ -30,10 +30,7 @@ define <2 x i32> @negated_operand_commut
 
 define i8 @knownnegation(i8 %x, i8 %y) {
 ; CHECK-LABEL: @knownnegation(
-; CHECK-NEXT:    [[XY:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[YX:%.*]] = sub i8 [[Y]], [[X]]
-; CHECK-NEXT:    [[R:%.*]] = add i8 [[XY]], [[YX]]
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 0 
 ;
   %xy = sub i8 %x, %y
   %yx = sub i8 %y, %x
@@ -43,10 +40,7 @@ define i8 @knownnegation(i8 %x, i8 %y) {
 
 define <2 x i8> @knownnegation_commute_vec(<2 x i8> %x, <2 x i8> %y) {
 ; CHECK-LABEL: @knownnegation_commute_vec(
-; CHECK-NEXT:    [[XY:%.*]] = sub <2 x i8> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[YX:%.*]] = sub <2 x i8> [[Y]], [[X]]
-; CHECK-NEXT:    [[R:%.*]] = add <2 x i8> [[YX]], [[XY]]
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    ret <2 x i8> zeroinitializer
 ;
   %xy = sub <2 x i8> %x, %y
   %yx = sub <2 x i8> %y, %x




More information about the llvm-commits mailing list