https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128658
--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
The miscompilation in n() is caused by an invalid interaction between Basic
Block (BB) SLP reduction vectorization (slp_inst_kind_bb_reduc) and
over-widening pattern detection (vect_recog_over_widening_pattern):
1. Range & Precision Propagation:
- In f = 2147483647 / e, the range of e is determined to be within
[0xc6067a0, 0x339f985e], bounding f to [2, 10], which fits in 31 bits.
- vect_determine_precisions_from_range sets min_input_precision = 31 on
the division statement.
- Backward precision propagation (vect_determine_min_output_precision_1
and vect_determine_precisions_from_users) propagates this narrowing
backward through e = (c & j) ^ C1 and _4 = c & j.
- Because ^ and & are truncatable operations, vect_determine_precisions
determines that _4, c, and j only need 32 bits of precision.
2. Over-widening Pattern Detection:
- vect_recog_over_widening_pattern converts the 64-bit operations for j,
c, and _4 into 32-bit operations followed by promotions back to long
int
(e.g. patt_29 = (long int) patt_28;).
- The contract of over-widening pattern statements is that the promotion
statement only exists to preserve IR type validity, assuming all
consumers will consume the narrow pattern statement and leave the
promotion dead.
- vect_recog_over_widening_pattern explicitly contains a check to skip
reductions:
1 /* Punt for reductions where we don't handle the type conversions.
*/
2 if (vect_is_reduction (last_stmt_info))
3 return NULL;
However, vect_is_reduction() only checked STMT_VINFO_REDUC_IDX
(stmt_info) != -1, which is only set for loop reductions. BB reduction roots
(slp_inst_kind_bb_reduc) were neither marked nor recognized by
vect_is_reduction().
3. BB Reduction Vectorization vs. Scalar Consumer Mismatch:
- vect_slp_check_for_roots previously registered _4 = c & j as a BB
reduction root with inputs [j, c].
- In vect_analyze_slp, pattern statements were applied to the reduction
inputs, replacing them with patt_29 and patt_36 ((vector(2) long int)
vector(2) signed int).
- The SLP tree vectorized [j, c] as 32-bit operations, sign-extended the
vectors to vector(2) long int, and emitted .REDUC_AND in the reduction
epilogue.
- The scalar result was assigned back to the scalar SSA variable _4.
- Because e = _4 ^ C1 and f = 2147483647 / e are single scalar
statements,
they were not vectorized. They remained as scalar 64-bit statements in
GIMPLE.
- In 64-bit scalar execution, _4 had 0xffffffff in its upper 32 bits (due
to sign extension of bit 31) instead of the true upper bits 0x6c4eefa9.
- Therefore, _4 ^ C1 failed to cancel the high 32 bits of C1 =
7804438717590242691, producing a negative 64-bit value (~ -7.8e18).
- 64-bit division 2147483647 / -7.8e18 yielded 0, returning h = 0 & 5 = 0
instead of 4.
1 /* Reduced testcase: miscompiled with -O3 -fno-tree-forwprop
-fno-tree-vrp
-fno-vect-cost-model */
2
3 __attribute__((noinline))
4 int n(long o) {
5 long x = 536870911 - 1 / o;
6 long g = (x ^ 3124462952626119202L) & 5121363712956983201L;
7 long j = ~g;
8 long d = x ^ -3124462952626119203L;
9 long i = d & ~5121363712956983201L;
10 long c = ~i;
11 long e = (c & j) ^ 7804438717590242691L;
12 long f = 2147483647 / e;
13 return f & 5;
14 }
15
16 int main() {
17 volatile long o = 809516896;
18 if (n(o) != 4)
19 __builtin_abort();
20 return 0;
21 }