https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125867
Bug ID: 125867
Summary: lshift_cheap_p should be used more
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: internal-improvement, missed-optimization
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: pinskia at gcc dot gnu.org
Target Milestone: ---
Right now lshift_cheap_p is only used reassociation but maybe it should be used
also for:
/* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
(if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
(with {
tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
}
(lshift (convert (convert:boolean_type_node @0)) { shift; })))))
...
/* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
(if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
(with {
tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
}
(lshift (convert (bit_xor (convert:boolean_type_node @0)
{ boolean_true_node; })) { shift; })))))))
And:
/* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
convert this into a shift followed by ANDing with D. */
(simplify
(cond
(ne (bit_and @0 integer_pow2p@1) integer_zerop)
INTEGER_CST@2 integer_zerop)
(if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
(with {
int shift = (wi::exact_log2 (wi::to_wide (@2))
- wi::exact_log2 (wi::to_wide (@1)));
}
(if (shift > 0)
(bit_and
(lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
(bit_and
(convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
@2)))))
in match.pd.