On Thu, Nov 6, 2025 at 4:16 PM Andrew Pinski
<[email protected]> wrote:
>
> When optimize_unreachable was moved from fab to forwprop, I missed that due to
> the integrated copy prop, we might end up with an already true branch leading
> to a __builtin_unreachable block. optimize_unreachable would switch around
> the if and things go down hill from there since the other edge was already
> marked as non-executable, forwprop didn't process those blocks and didn't
> do copy prop into that block and the original assignment statement was 
> removed.
>
> This fixes the problem by having optimize_unreachable not touch the if
> statement was already changed to true/false.
>
> Note I placed the testcase in gcc.c-torture/compile as gcc.dg/torture
> is NOT currently testing -Og (see PR 122450 for that).

I figured out a gimple testcase just a few minutes after sending this
so I will be submitting a new version with that testcase added too.

Thanks,
Andrew


>
> Bootstrapped and tested on x86_64-linux-gnu.
>
>         PR tree-optimization/122588
>
> gcc/ChangeLog:
>
>         * tree-ssa-forwprop.cc (optimize_unreachable): Don't touch
>         if the condition was already true or false.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.c-torture/compile/pr122588-1.c: New test.
>
> Signed-off-by: Andrew Pinski <[email protected]>
> ---
>  .../gcc.c-torture/compile/pr122588-1.c        | 25 +++++++++++++++++++
>  gcc/tree-ssa-forwprop.cc                      |  7 +++++-
>  2 files changed, 31 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr122588-1.c
>
> diff --git a/gcc/testsuite/gcc.c-torture/compile/pr122588-1.c 
> b/gcc/testsuite/gcc.c-torture/compile/pr122588-1.c
> new file mode 100644
> index 00000000000..43ec621512c
> --- /dev/null
> +++ b/gcc/testsuite/gcc.c-torture/compile/pr122588-1.c
> @@ -0,0 +1,25 @@
> +/* Disable warnings about __sync_nand_and_fetch. */
> +/* { dg-options "-w" } */
> +/* PR tree-optimization/122588 */
> +
> +int i;
> +char c;
> +
> +static inline __attribute__((__always_inline__))
> +void foo0 (int a)
> +{
> +l5:
> +  __sync_nand_and_fetch (&i, 0);
> +  int x = __builtin_memcmp_eq (&a, 0, 4);
> +  if (__builtin_iseqsig (x, 0.))
> +    goto l5;
> +  if (a)
> +    __builtin_unreachable ();
> +  c = a;
> +}
> +
> +int
> +main ()
> +{
> +  foo0 (1);
> +}
> diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
> index ae7f0e770ba..9f8d4ad3b44 100644
> --- a/gcc/tree-ssa-forwprop.cc
> +++ b/gcc/tree-ssa-forwprop.cc
> @@ -5080,7 +5080,12 @@ optimize_unreachable (basic_block bb)
>        stmt = gsi_stmt (gsi);
>        if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
>         {
> -         if (e->flags & EDGE_TRUE_VALUE)
> +         /* If the condition is already true/false
> +            ignore it. This can happen during copy prop of forwprop. */
> +         if (gimple_cond_true_p (cond_stmt)
> +             || gimple_cond_false_p (cond_stmt))
> +           continue;
> +         else if (e->flags & EDGE_TRUE_VALUE)
>             gimple_cond_make_false (cond_stmt);
>           else if (e->flags & EDGE_FALSE_VALUE)
>             gimple_cond_make_true (cond_stmt);
> --
> 2.43.0
>

Reply via email to