[SCEV] Fold (0 udiv %x) to 0
authorPhilip Reames <listmail@philipreames.com>
Wed, 30 Jun 2021 15:31:13 +0000 (08:31 -0700)
committerPhilip Reames <listmail@philipreames.com>
Wed, 30 Jun 2021 15:31:13 +0000 (08:31 -0700)
We have analogous rules in instsimplify, etc.., but were missing the same in SCEV.  The fold is near trivial, but came up in the context of a larger change.

llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/fold.ll

index 990f3d6..97ea60f 100644 (file)
@@ -3268,6 +3268,11 @@ const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
     return S;
 
+  // 0 udiv Y == 0
+  if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS))
+    if (LHSC->getValue()->isZero())
+      return LHS;
+
   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
     if (RHSC->getValue()->isOne())
       return LHS;                               // X udiv 1 --> x
index 72b62ec..c23029d 100644 (file)
@@ -132,6 +132,6 @@ define i64 @test11(i64 %a) {
 ; CHECK-LABEL: @test11
   %t0 = udiv i64 0, %a
 ; CHECK: %t0
-; CHECK-NEXT: -->  (0 /u %a)
+; CHECK-NEXT: -->  0
   ret i64 %t0
 }