From 77b010474e3dab38f73f4225b54c8963a8e90744 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 24 Apr 2023 12:40:46 +0200 Subject: [PATCH] [InstSimplify] Move fold into isDivZero() (NFCI) This is a special case where we know the division result is zero. For the unsigned case this is handled by generic icmp code, for the signed case we do need to special case it. --- llvm/lib/Analysis/InstructionSimplify.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index fb6a395..fd11331 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -1100,6 +1100,10 @@ static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q, return false; if (IsSigned) { + // (X srem Y) sdiv Y --> 0 + if (match(X, m_SRem(m_Value(), m_Specific(Y)))) + return true; + // |X| / |Y| --> 0 // // We require that 1 operand is a simple constant. That could be extended to @@ -1175,11 +1179,6 @@ static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool IsSigned = Opcode == Instruction::SDiv; - // (X rem Y) / Y -> 0 - if ((IsSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || - (!IsSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) - return Constant::getNullValue(Op0->getType()); - // (X /u C1) /u C2 -> 0 if C1 * C2 overflow ConstantInt *C1, *C2; if (!IsSigned && match(Op0, m_UDiv(m_Value(), m_ConstantInt(C1))) && @@ -1217,13 +1216,6 @@ static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse)) return V; - // (X % Y) % Y -> X % Y - if ((Opcode == Instruction::SRem && - match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || - (Opcode == Instruction::URem && - match(Op0, m_URem(m_Value(), m_Specific(Op1))))) - return Op0; - // (X << Y) % X -> 0 if (Q.IIQ.UseInstrInfo && ((Opcode == Instruction::SRem && -- 2.7.4