[InstCombine] Missed funnel shift recognition after replacing lshr with select
InstCombine recognizes a masked 16-bit rotation expressed using lshr, shl, and or on i32 values followed by truncation, and canonicalizes it to llvm.fshr.i16.
Replacing the right shift with an icmp and select expression preserves the behavior of this complete function but prevents recognition. The optimization difference persists through default<O2>.
Reproducer
define i16 @check_rotate_masked_16bit(i8 %shamt, i32 %cond) {
%maskx = and i32 %cond, 1
%masky = and i8 %shamt, 15
%z = zext i8 %masky to i32
%iszero = icmp eq i32 %z, 0
%shr = select i1 %iszero, i32 %maskx, i32 0
%sub = sub i8 0, %shamt
%maskw = and i8 %sub, 15
%z2 = zext i8 %maskw to i32
%shl = shl nuw nsw i32 %maskx, %z2
%or = or i32 %shr, %shl
%trunc = trunc i32 %or to i16
ret i16 %trunc
}
Save the reproducer as repro.ll and the original test below as original.ll, then run:
$ opt -S -passes=instcombine original.ll
$ opt -S -passes=instcombine repro.ll
$ opt -S -passes='default<O2>' original.ll
$ opt -S -passes='default<O2>' repro.ll
Actual result
InstCombine produces:
define i16 @check_rotate_masked_16bit(i8 %shamt, i32 %cond) {
%maskx = and i32 %cond, 1
%masky = and i8 %shamt, 15
%iszero = icmp eq i8 %masky, 0
%shr = select i1 %iszero, i32 %maskx, i32 0
%sub = sub i8 0, %shamt
%maskw = and i8 %sub, 15
%z2 = zext nneg i8 %maskw to i32
%shl = shl nuw nsw i32 %maskx, %z2
%or = or i32 %shr, %shl
%trunc = trunc nuw i32 %or to i16
ret i16 %trunc
}
default<O2> retains the same instruction sequence, with additional function and return attributes. Both outputs contain 11 instructions, including ret, and neither contains llvm.fshr.i16.
Original test and successful optimization
The original test (from llvm/test/Transforms/InstCombine/rotate.ll)uses lshr instead of the icmp and select pair. This is the complete standalone input (original.ll):
define i16 @check_rotate_masked_16bit(i8 %shamt, i32 %cond) {
%maskx = and i32 %cond, 1
%masky = and i8 %shamt, 15
%z = zext i8 %masky to i32
%shr = lshr i32 %maskx, %z
%sub = sub i8 0, %shamt
%maskw = and i8 %sub, 15
%z2 = zext i8 %maskw to i32
%shl = shl nuw nsw i32 %maskx, %z2
%or = or i32 %shr, %shl
%trunc = trunc i32 %or to i16
ret i16 %trunc
}
InstCombine recognizes the rotation and produces this function:
define i16 @check_rotate_masked_16bit(i8 %shamt, i32 %cond) {
%1 = zext i8 %shamt to i16
%2 = trunc i32 %cond to i16
%3 = and i16 %2, 1
%trunc = call i16 @llvm.fshr.i16(i16 %3, i16 %3, i16 %1)
ret i16 %trunc
}
The output module also declares llvm.fshr.i16. The original test's O2 output has the same 5-instruction sequence, including ret, with tail call and additional attributes.
Expected result
The original input is canonicalized to llvm.fshr.i16 by InstCombine.
However, the select-based input does not recover the funnel-shift representation or produce an alternative comparably simplified form. Instead, it retains the comparison, select, shift, and OR operations, even after the default O2 pipeline.
One valid expected simplification is the llvm.fshr.i16 form produced for the original input.
The transformation is legal: for defined operands, %maskx is either 0 or 1, and %z is in the range 0–15. Therefore, shifting %maskx right by a nonzero amount yields zero, matching the select expression.
The complete original and mutated functions are semantically equivalent under the LLVM refinement model, as verified by Alive2.
Semantic validation
Verified using Alive2 (5d0eb277235b73f71000501c3a4f5ab9b2586915):
-
Original ↔ Mutant: PROVED (bidirectional refinement).
-
Mutant → Original optimized result: PROVED.
The equivalence applies to the complete functions, not necessarily to the isolated lshr and select expressions, which can differ in poison propagation.
Verification was performed without adding noundef, freeze, or assume, and without disabling poison/undef checks.
LLVM version
LLVM 24.0.0git, official main snapshot at commit 8719e1932a81499996fcdaf5bce5a364c2e46074, optimized build with assertions enabled.
The standalone reproducer and the lshr control were run with this build; the optimization difference persists with both InstCombine and default<O2>.