[llvm] [InstCombine] fold icmp of select with invertible shl (PR #147182)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 8 03:19:31 PST 2025
================
@@ -5948,9 +5954,29 @@ static Instruction *foldICmpEqualityWithOffset(ICmpInst &I,
collectOffsetOp(Op1, OffsetOps, /*AllowRecursion=*/true);
auto ApplyOffsetImpl = [&](Value *V, unsigned BinOpc, Value *RHS) -> Value * {
+ switch (BinOpc) {
+ // V = shl nsw X, RHS => X = ashr V, RHS
+ case Instruction::AShr: {
+ const APInt *CV, *CRHS;
+ if (match(V, m_APInt(CV)) && match(RHS, m_APInt(CRHS)) &&
+ CV->ashr(*CRHS).shl(*CRHS) != *CV)
+ return nullptr;
+ break;
+ }
+ // V = shl nuw X, RHS => X = lshr V, RHS
+ case Instruction::LShr: {
+ const APInt *CV, *CRHS;
+ if (match(V, m_APInt(CV)) && match(RHS, m_APInt(CRHS)) &&
+ CV->lshr(*CRHS).shl(*CRHS) != *CV)
+ return nullptr;
+ break;
+ }
+ default:
+ break;
+ }
+
Value *Simplified = simplifyBinOp(BinOpc, V, RHS, SQ);
- // Avoid infinite loops by checking if RHS is an identity for the BinOp.
- if (!Simplified || Simplified == V)
----------------
dtcxzyw wrote:
It was intended to bail out on something like `X +/-/>> 0 -> 0` or more complicated patterns. It is ok to drop this for now. If we hit an infinite loop in the future, we can defer the check into `ApplyOffset`.
https://github.com/llvm/llvm-project/pull/147182
More information about the llvm-commits
mailing list