Skip to content

Commit 14d8f15

Browse files
committed
[SCEV] Fold (0 udiv %x) to 0
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.
1 parent 5e630a9 commit 14d8f15

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3268,6 +3268,11 @@ const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
32683268
if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
32693269
return S;
32703270

3271+
// 0 udiv Y == 0
3272+
if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS))
3273+
if (LHSC->getValue()->isZero())
3274+
return LHS;
3275+
32713276
if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
32723277
if (RHSC->getValue()->isOne())
32733278
return LHS; // X udiv 1 --> x

llvm/test/Analysis/ScalarEvolution/fold.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,6 @@ define i64 @test11(i64 %a) {
132132
; CHECK-LABEL: @test11
133133
%t0 = udiv i64 0, %a
134134
; CHECK: %t0
135-
; CHECK-NEXT: --> (0 /u %a)
135+
; CHECK-NEXT: --> 0
136136
ret i64 %t0
137137
}

0 commit comments

Comments
 (0)