summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis
diff options
context:
space:
mode:
authorChen Zheng <shchenz@cn.ibm.com>2018-07-21 12:27:54 +0000
committerChen Zheng <shchenz@cn.ibm.com>2018-07-21 12:27:54 +0000
commit69bb064539612120250539afb1ee961c698b182b (patch)
tree8adf075c3bad159a730f374e891c562fc6af8a97 /llvm/lib/Analysis
parent23c2d1c15a9c89f313304c22180fb5cae97211bc (diff)
downloadbcm5719-llvm-69bb064539612120250539afb1ee961c698b182b.tar.gz
bcm5719-llvm-69bb064539612120250539afb1ee961c698b182b.zip
[InstrSimplify] fold sdiv if two operands are negated and non-overflow
Differential Revision: https://reviews.llvm.org/D49382 llvm-svn: 337642
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r--llvm/lib/Analysis/InstructionSimplify.cpp6
-rw-r--r--llvm/lib/Analysis/ValueTracking.cpp20
2 files changed, 17 insertions, 9 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index e095e95935b..519d6d67be5 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -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);
}
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 054f981bab6..54828e392d4 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -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,
OpenPOWER on IntegriCloud