[llvm] [Instcombine] Fold a zero-select hexadecimal digit count into ctlz(x | 1) (PR #215073)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 9 04:46:43 PDT 2026
================
@@ -1528,6 +1528,103 @@ static Instruction *foldSelectCtlzToCttz(ICmpInst *ICI, Value *TrueVal,
return CallInst::Create(F, {X, Builder.getFalse()});
}
+static Value *foldSelectCtlzWithNonZeroInput(ICmpInst *ICI, Value *TrueVal,
----------------
ParkHanbum wrote:
I think the cleanest way to implement this is to extend `foldSelectValueEquivalence()` rather than matching the specific `sub/lshr` pattern.
The general form is:
```text
select (X == 0), T, F(ctlz(X, true))
->
F(ctlz(X | 1, true))
```
if replacing `ctlz(X, true)` with `BW - 1` simplifies `F(...)` to `T`.
This fits `foldSelectValueEquivalence()` well because it already uses `simplifyWithOpReplaced()` to evaluate an expression under information provided by the select condition.
So the implementation could:
1. Find `ctlz(X, true)` in the one-use chain producing the false value.
2. Check whether `simplifyWithOpReplaced(FalseVal, Ctlz, BW - 1)` equals the true value.
3. If so, replace the `ctlz` operand with `X | 1` and remove the select.
This avoids recognizing `67`, `sub`, or `lshr` specifically; they are just one instance of `F`.
https://github.com/llvm/llvm-project/pull/215073
More information about the llvm-commits
mailing list