[InstSimplify] use m_Specific and commutative matcher to reduce code; NFCI

llvm-svn: 322955
This commit is contained in:
Sanjay Patel 2018-01-19 16:12:55 +00:00
parent bf4cddaafb
commit 33cb84571f
1 changed files with 8 additions and 9 deletions

View File

@ -978,17 +978,16 @@ static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
bool IsSigned = Opcode == Instruction::SDiv; bool IsSigned = Opcode == Instruction::SDiv;
// (X * Y) / Y -> X if the multiplication does not overflow. // (X * Y) / Y -> X if the multiplication does not overflow.
Value *X = nullptr, *Y = nullptr; Value *X;
if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) {
if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1 auto *Mul = cast<OverflowingBinaryOperator>(Op0);
OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0); // If the Mul does not overflow, then we are good to go.
// If the Mul knows it does not overflow, then we are good to go.
if ((IsSigned && Mul->hasNoSignedWrap()) || if ((IsSigned && Mul->hasNoSignedWrap()) ||
(!IsSigned && Mul->hasNoUnsignedWrap())) (!IsSigned && Mul->hasNoUnsignedWrap()))
return X; return X;
// If X has the form X = A / Y then X * Y cannot overflow. // If X has the form X = A / Y, then X * Y cannot overflow.
if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) if ((IsSigned && match(X, m_SDiv(m_Value(), m_Specific(Op1)))) ||
if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) (!IsSigned && match(X, m_UDiv(m_Value(), m_Specific(Op1)))))
return X; return X;
} }