Hi! As the testcase shows, arithmetics that for -ftrapv would need multiple basic blocks can show up not just in nb_bytes expressions where we are calling rewrite_to_non_trapping_overflow for a while already, but also in the pointer expression to the start of the region. While the testcase covers just the first hunk and I've failed to create a testcase for the latter, it is at least in theory possible too, so I've adjusted that hunk too.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2020-03-11 Jakub Jelinek <ja...@redhat.com> PR tree-optimization/94114 * tree-loop-distribution.c (generate_memset_builtin): Call rewrite_to_non_trapping_overflow even on mem. (generate_memcpy_builtin): Call rewrite_to_non_trapping_overflow even on dest and src. * gcc.dg/pr94114.c: New test. --- gcc/tree-loop-distribution.c.jj 2020-01-12 11:54:38.497381967 +0100 +++ gcc/tree-loop-distribution.c 2020-03-10 08:20:42.830265273 +0100 @@ -1151,7 +1151,7 @@ generate_memset_builtin (class loop *loo nb_bytes = rewrite_to_non_trapping_overflow (builtin->size); nb_bytes = force_gimple_operand_gsi (&gsi, nb_bytes, true, NULL_TREE, false, GSI_CONTINUE_LINKING); - mem = builtin->dst_base; + mem = rewrite_to_non_trapping_overflow (builtin->dst_base); mem = force_gimple_operand_gsi (&gsi, mem, true, NULL_TREE, false, GSI_CONTINUE_LINKING); @@ -1205,8 +1205,8 @@ generate_memcpy_builtin (class loop *loo nb_bytes = rewrite_to_non_trapping_overflow (builtin->size); nb_bytes = force_gimple_operand_gsi (&gsi, nb_bytes, true, NULL_TREE, false, GSI_CONTINUE_LINKING); - dest = builtin->dst_base; - src = builtin->src_base; + dest = rewrite_to_non_trapping_overflow (builtin->dst_base); + src = rewrite_to_non_trapping_overflow (builtin->src_base); if (partition->kind == PKIND_MEMCPY || ! ptr_derefs_may_alias_p (dest, src)) kind = BUILT_IN_MEMCPY; --- gcc/testsuite/gcc.dg/pr94114.c.jj 2020-03-10 08:21:07.369900379 +0100 +++ gcc/testsuite/gcc.dg/pr94114.c 2020-03-10 08:19:06.691694811 +0100 @@ -0,0 +1,13 @@ +/* PR tree-optimization/94114 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -ftree-loop-distribute-patterns -ftrapv" } */ + +void +foo (int *x, int *y, int *z, long int w) +{ + while (y + w > z) + { + x[w] = 0; + --w; + } +} Jakub