[InstSimplify] Move fold into isDivZero() (NFCI)
authorNikita Popov <npopov@redhat.com>
Mon, 24 Apr 2023 10:40:46 +0000 (12:40 +0200)
committerNikita Popov <npopov@redhat.com>
Mon, 24 Apr 2023 10:44:52 +0000 (12:44 +0200)
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

index fb6a395..fd11331 100644 (file)
@@ -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 &&