https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125405
--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jeff Law <[email protected]>: https://gcc.gnu.org/g:c05164c348242b00b33cdf2988cc5e93d29b0478 commit r17-1680-gc05164c348242b00b33cdf2988cc5e93d29b0478 Author: Milan Tripkovic <[email protected]> Date: Thu Jun 18 17:51:59 2026 -0600 [PATCH] PR tree-optimization/125405: Missed optimization: mask/sub sign-extension idiom not canonicalized to sign extension This patch implement new pattern for match.pd that recognize the sign-extension idiom (x & low_bits) - (x & sign_bit) and tests that covers it. Pattern is based on conversation from bug report. TRUNK riscv output: sext8_hd: andi a5,a0,127 andi a0,a0,128 sub a0,a5,a0 ret sext16_hd: slli a5,a0,49 li a4,32768 srli a5,a5,49 and a0,a0,a4 sub a0,a5,a0 ret NEW riscv output: sext8_hd: slliw a0,a0,24 sraiw a0,a0,24 ret sext16_hd: slliw a0,a0,16 sraiw a0,a0,16 ret TRUNK x86 output: "sext8_hd": mov rax, rdi and edi, 128 and eax, 127 sub rax, rdi ret "sext16_hd": mov rax, rdi and edi, 32768 and eax, 32767 sub rax, rdi ret new x86 output: sext8_hd: movsbq %dil, %rax ret sext16_hd: movswq %di, %rax ret PR target/125405 gcc/ChangeLog: * match.pd: New pattern added. gcc/testsuite/ChangeLog: * gcc.dg/pr125405-bitint.c: New test. * gcc.dg/tree-ssa/pr125405.c: New test. * gcc.target/riscv/pr125405.c: New test.
