Skip to content

Commit 962be5d

Browse files
committed
[SCEV] Generalize (C * A /u C) -> A fold to (C1 * A /u C2) -> C1/C2 * A.
Generalize fold added in 74ec38f to support multiplying and dividing by different constants, given they are both powers-of-2 and C1 is a multiple of C2, checked via their trailing zeros. https://alive2.llvm.org/ce/z/eqJ2xj
1 parent 9b1b937 commit 962be5d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,13 +3216,16 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
32163216
};
32173217
}
32183218

3219-
// Try to fold (C * D /u C) -> D, if C is a power-of-2 and D is a multiple
3220-
// of C.
3219+
// Try to fold (C1 * D /u C2) -> C1/C2 * D, if C1 and C2 are powers-of-2,
3220+
// D is a multiple of C2, and C1 is a multiple of C1.
32213221
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;
3222+
const SCEVConstant *C2;
3223+
if (LHSC->getAPInt().isPowerOf2() &&
3224+
match(Ops[1], m_scev_UDiv(m_SCEV(D), m_SCEVConstant(C2))) &&
3225+
C2->getAPInt().isPowerOf2() &&
3226+
getMinTrailingZeros(LHSC) >= getMinTrailingZeros(C2) &&
3227+
getMinTrailingZeros(LHSC) <= getMinTrailingZeros(D)) {
3228+
return getMulExpr(getUDivExpr(LHSC, C2), D);
32263229
}
32273230
}
32283231
}

0 commit comments

Comments
 (0)