[InstrSimplify] fold sdiv if two operands are negated and non-overflow
Differential Revision: https://reviews.llvm.org/D49382
llvm-svn: 337642
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index e095e95..519d6d6 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/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);
}
@@ -1111,7 +1115,7 @@
// 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);
}