Skip to content

Commit 74ec38f

Browse files
authored
[SCEV] Fold (C * A /u C) -> A, if A is a multiple of C and C a pow-of-2. (#156730)
Alive2 Proof: https://alive2.llvm.org/ce/z/JoHJE9 PR: #156730
1 parent 22fb21a commit 74ec38f

File tree

10 files changed

+297
-256
lines changed

10 files changed

+297
-256
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,6 +3215,15 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
32153215
return getZeroExtendExpr(Res, Ops[1]->getType(), Depth + 1);
32163216
};
32173217
}
3218+
3219+
// Try to fold (C * D /u C) -> D, if C is a power-of-2 and D is a multiple
3220+
// of C.
3221+
const SCEV *D;
3222+
if (match(Ops[1], m_scev_UDiv(m_SCEV(D), m_scev_Specific(LHSC))) &&
3223+
LHSC->getAPInt().isPowerOf2() &&
3224+
LHSC->getAPInt().logBase2() <= getMinTrailingZeros(D)) {
3225+
return D;
3226+
}
32183227
}
32193228
}
32203229

llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info-rewrite-expressions.ll

Lines changed: 36 additions & 36 deletions
Large diffs are not rendered by default.

llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,9 +1390,9 @@ define void @ptr_induction_eq_2(ptr %a, i64 %n) {
13901390
; CHECK-NEXT: %b = getelementptr inbounds ptr, ptr %a, i64 %n
13911391
; CHECK-NEXT: --> ((8 * %n)<nsw> + %a) U: full-set S: full-set
13921392
; CHECK-NEXT: %ptr.iv = phi ptr [ %ptr.iv.next, %loop ], [ %a, %entry ]
1393-
; CHECK-NEXT: --> {%a,+,8}<nuw><%loop> U: full-set S: full-set Exits: ((8 * ((-8 + (8 * %n)<nsw>) /u 8))<nuw> + %a) LoopDispositions: { %loop: Computable }
1393+
; CHECK-NEXT: --> {%a,+,8}<nuw><%loop> U: full-set S: full-set Exits: (-8 + (8 * %n)<nsw> + %a) LoopDispositions: { %loop: Computable }
13941394
; CHECK-NEXT: %ptr.iv.next = getelementptr inbounds i8, ptr %ptr.iv, i64 8
1395-
; CHECK-NEXT: --> {(8 + %a),+,8}<nuw><%loop> U: full-set S: full-set Exits: (8 + (8 * ((-8 + (8 * %n)<nsw>) /u 8))<nuw> + %a) LoopDispositions: { %loop: Computable }
1395+
; CHECK-NEXT: --> {(8 + %a),+,8}<nuw><%loop> U: full-set S: full-set Exits: ((8 * %n)<nsw> + %a) LoopDispositions: { %loop: Computable }
13961396
; CHECK-NEXT: Determining loop execution counts for: @ptr_induction_eq_2
13971397
; CHECK-NEXT: Loop %loop: backedge-taken count is ((-8 + (8 * %n)<nsw>) /u 8)
13981398
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is i64 2305843009213693951

llvm/test/Analysis/ScalarEvolution/pr58402-large-number-of-zext-exprs.ll

Lines changed: 31 additions & 31 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)