diff options
| author | Chen Zheng <shchenz@cn.ibm.com> | 2018-07-12 03:06:04 +0000 |
|---|---|---|
| committer | Chen Zheng <shchenz@cn.ibm.com> | 2018-07-12 03:06:04 +0000 |
| commit | fdf13ef342e29d22f96f64a82679609873229d92 (patch) | |
| tree | 1f97d7a6e459eb0081f2faf9ea1d5ca32c82d891 /llvm/lib | |
| parent | 71f1ec7ea1108937251d3681fb91d882009f0a97 (diff) | |
| download | bcm5719-llvm-fdf13ef342e29d22f96f64a82679609873229d92.tar.gz bcm5719-llvm-fdf13ef342e29d22f96f64a82679609873229d92.zip | |
[InstSimplify] simplify add instruction if two operands are negative
Differential Revision: https://reviews.llvm.org/D49216
llvm-svn: 336881
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 4 | ||||
| -rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 20 |
2 files changed, 24 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 061ce8f0bd1..87b1fc5b8ee 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -540,6 +540,10 @@ static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, 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 diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index bdfd1783236..88ed6562c38 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -4511,6 +4511,26 @@ static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, 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, |

