[llvm] 77b0104 - [InstSimplify] Move fold into isDivZero() (NFCI)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 24 03:45:01 PDT 2023


Author: Nikita Popov
Date: 2023-04-24T12:44:52+02:00
New Revision: 77b010474e3dab38f73f4225b54c8963a8e90744

URL: https://github.com/llvm/llvm-project/commit/77b010474e3dab38f73f4225b54c8963a8e90744
DIFF: https://github.com/llvm/llvm-project/commit/77b010474e3dab38f73f4225b54c8963a8e90744.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index fb6a395eea081..fd11331f1a5fe 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 &&


        


More information about the llvm-commits mailing list