Hi! John Regehr discovered a couple of spots in GCC sources that invoke undefined behavior during bootstrap/regtest, the following patch fixes most of them. I couldn't reproduce the diagnostic.c failure and would like to leave the ipa hunk to Honza, I think the probability/frequency code often might go out of the expected limits and then invoke undefined signed overflow.
The double_int_mask change is because e.g. with -E, ptr_mode is VOIDmode and so double_int_mask is called with 0 precision during compiler initialization. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2012-08-13 Jakub Jelinek <ja...@redhat.com> PR c/53968 * tree.c (integer_pow2p): Avoid undefined signed overflows. * simplify-rtx.c (neg_const_int): Likewise. * expr.c (fixup_args_size_notes): Likewise. * stor-layout.c (set_min_and_max_values_for_integral_type): Likewise. * double-int.c (mul_double_wide_with_sign): Likewise. (double_int_mask): Likewise. * tree-ssa-loop-ivopts.c (get_address_cost): Likewise. --- gcc/tree.c.jj 2012-08-10 15:48:53.000000000 +0200 +++ gcc/tree.c 2012-08-13 10:25:46.610502929 +0200 @@ -1849,7 +1849,7 @@ int integer_pow2p (const_tree expr) { int prec; - HOST_WIDE_INT high, low; + unsigned HOST_WIDE_INT high, low; STRIP_NOPS (expr); --- gcc/simplify-rtx.c.jj 2012-08-10 15:49:20.000000000 +0200 +++ gcc/simplify-rtx.c 2012-08-13 09:51:43.628508537 +0200 @@ -66,7 +66,7 @@ static rtx simplify_binary_operation_1 ( static rtx neg_const_int (enum machine_mode mode, const_rtx i) { - return gen_int_mode (- INTVAL (i), mode); + return gen_int_mode (-(unsigned HOST_WIDE_INT) INTVAL (i), mode); } /* Test whether expression, X, is an immediate constant that represents --- gcc/expr.c.jj 2012-08-10 15:49:07.000000000 +0200 +++ gcc/expr.c 2012-08-13 10:40:01.182501639 +0200 @@ -3828,7 +3828,7 @@ fixup_args_size_notes (rtx prev, rtx las add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size)); #ifdef STACK_GROWS_DOWNWARD - this_delta = -this_delta; + this_delta = -(unsigned HOST_WIDE_INT) this_delta; #endif args_size -= this_delta; } --- gcc/stor-layout.c.jj 2012-08-10 15:49:20.000000000 +0200 +++ gcc/stor-layout.c 2012-08-13 10:14:14.388505253 +0200 @@ -2568,10 +2568,14 @@ set_min_and_max_values_for_integral_type = build_int_cst_wide (type, (precision - HOST_BITS_PER_WIDE_INT > 0 ? -1 - : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1), + : (HOST_WIDE_INT) + (((unsigned HOST_WIDE_INT) 1 + << (precision - 1)) - 1)), (precision - HOST_BITS_PER_WIDE_INT - 1 > 0 - ? (((HOST_WIDE_INT) 1 - << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1 + ? (HOST_WIDE_INT) + ((((unsigned HOST_WIDE_INT) 1 + << (precision - HOST_BITS_PER_WIDE_INT + - 1))) - 1) : 0)); } --- gcc/double-int.c.jj 2012-08-10 15:49:07.000000000 +0200 +++ gcc/double-int.c 2012-08-13 11:24:17.816495757 +0200 @@ -170,7 +170,7 @@ mul_double_wide_with_sign (unsigned HOST { k = i + j; /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */ - carry += arg1[i] * arg2[j]; + carry += (unsigned HOST_WIDE_INT) arg1[i] * arg2[j]; /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */ carry += prod[k]; prod[k] = LOWPART (carry); @@ -625,7 +625,7 @@ double_int_mask (unsigned prec) else { mask.high = 0; - mask.low = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1; + mask.low = prec ? ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1 : 0; } return mask; --- gcc/tree-ssa-loop-ivopts.c.jj 2012-08-10 15:49:07.000000000 +0200 +++ gcc/tree-ssa-loop-ivopts.c 2012-08-13 10:17:51.227504425 +0200 @@ -3173,7 +3173,7 @@ get_address_cost (bool symbol_present, b for (i = width; i >= 0; i--) { - off = -((HOST_WIDE_INT) 1 << i); + off = -((unsigned HOST_WIDE_INT) 1 << i); XEXP (addr, 1) = gen_int_mode (off, address_mode); if (memory_address_addr_space_p (mem_mode, addr, as)) break; @@ -3182,7 +3182,7 @@ get_address_cost (bool symbol_present, b for (i = width; i >= 0; i--) { - off = ((HOST_WIDE_INT) 1 << i) - 1; + off = ((unsigned HOST_WIDE_INT) 1 << i) - 1; XEXP (addr, 1) = gen_int_mode (off, address_mode); if (memory_address_addr_space_p (mem_mode, addr, as)) break; Jakub