[Bug tree-optimization/125405] Missed optimization: mask/sub sign-extension idiom not canonicalized to sign extension
Milan.Tripkovic@rt-rk.com
gcc-bugzilla@gcc.gnu.org
Thu Jun 4 14:09:54 GMT 2026
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125405
--- Comment #6 from Milan Tripkovic <Milan.Tripkovic@rt-rk.com> ---
Hello,
Thank you for the suggestions. I updated the pattern incorporating the feedback
from comments #3, #4, and #5:
(simplify
(minus (bit_and @0 INTEGER_CST@1) (bit_and @0 integer_pow2p@2))
(if (INTEGRAL_TYPE_P (type)
&& TYPE_PRECISION (type) <= MAX_FIXED_MODE_SIZE
&& wi::to_wide (@1) + 1 == wi::to_wide (@2))
(with {
int width = wi::exact_log2 (wi::to_wide (@2)) + 1;
tree ntype = NULL_TREE;
if (width > 0 && width < TYPE_PRECISION (type))
ntype = build_nonstandard_integer_type (width, SIGNED);
}
(if (ntype && type_has_mode_precision_p (ntype))
(convert (convert:ntype @0))))))
I tested the updated pattern on RISC-V (rv64gc) covering: standard long/int
types, _BitInt with power-of-2 widths (32), non-power-of-2 widths (33, 37, 63),
unsigned BitInt types, negative cases where the pattern should not fire.
Example tests are available at: https://godbolt.org/z/4ndW7rG9W
The asm outputs with my pattern are shown in the comments in the test file.
More information about the Gcc-bugs
mailing list