public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/108789] __builtin_(add|mul|sub)_overflow methods generate duplicate operations if both operands are const which in turn causes wrong code due to overlapping arguments
Date: Tue, 04 Jun 2024 10:28:52 +0000	[thread overview]
Message-ID: <bug-108789-4-Vq2JCRRKJi@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-108789-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108789

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:b8e28381cb5c0cddfe5201faf799d8b27f5d7d6c

commit r15-1009-gb8e28381cb5c0cddfe5201faf799d8b27f5d7d6c
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Jun 4 12:28:01 2024 +0200

    builtins: Force SAVE_EXPR for __builtin_{add,sub,mul}_overflow and
__builtin{add,sub}c [PR108789]

    The following testcase is miscompiled, because we use save_expr
    on the .{ADD,SUB,MUL}_OVERFLOW call we are creating, but if the first
    two operands are not INTEGER_CSTs (in that case we just fold it right away)
    but are TREE_READONLY/!TREE_SIDE_EFFECTS, save_expr doesn't actually
    create a SAVE_EXPR at all and so we lower it to
    *arg2 = REALPART_EXPR (.ADD_OVERFLOW (arg0, arg1)), \
    IMAGPART_EXPR (.ADD_OVERFLOW (arg0, arg1))
    which evaluates the ifn twice and just hope it will be CSEd back.
    As *arg2 aliases *arg0, that is not the case.
    The builtins are really never const/pure as they store into what
    the third arguments points to, so after handling the
INTEGER_CST+INTEGER_CST
    case, I think we should just always use SAVE_EXPR.  Just building SAVE_EXPR
    by hand and setting TREE_SIDE_EFFECTS on it doesn't work, because
    c_fully_fold optimizes it away again, so the following patch marks the
    ifn calls as TREE_SIDE_EFFECTS (but doesn't do it for the
    __builtin_{add,sub,mul}_overflow_p case which were designed for use
    especially in constant expressions and don't really evaluate the
    realpart side, so we don't really need a SAVE_EXPR in that case).

    2024-06-04  Jakub Jelinek  <jakub@redhat.com>

            PR middle-end/108789
            * builtins.cc (fold_builtin_arith_overflow): For ovf_only,
            don't call save_expr and don't build REALPART_EXPR, otherwise
            set TREE_SIDE_EFFECTS on call before calling save_expr.
            (fold_builtin_addc_subc): Set TREE_SIDE_EFFECTS on call before
            calling save_expr.

            * gcc.c-torture/execute/pr108789.c: New test.

  parent reply	other threads:[~2024-06-04 10:28 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-14 17:51 [Bug c/108789] New: __builtin_(add|mul)_overflow methods generate duplicate operations if both operands are const dpejesh at yahoo dot com
2023-02-14 17:53 ` [Bug middle-end/108789] " pinskia at gcc dot gnu.org
2023-11-09  6:29 ` [Bug middle-end/108789] __builtin_(add|mul)_overflow methods generate duplicate operations if both operands are const which in turn causes wrong code due to overlapping arguments pinskia at gcc dot gnu.org
2024-06-03 13:56 ` pinskia at gcc dot gnu.org
2024-06-03 13:59 ` [Bug middle-end/108789] __builtin_(add|mul|sub)_overflow " pinskia at gcc dot gnu.org
2024-06-03 15:35 ` jakub at gcc dot gnu.org
2024-06-04 10:28 ` cvs-commit at gcc dot gnu.org [this message]
2024-06-04 10:33 ` jakub at gcc dot gnu.org
2024-06-04 14:26 ` cvs-commit at gcc dot gnu.org
2024-06-11  6:17 ` cvs-commit at gcc dot gnu.org
2024-06-11 10:39 ` cvs-commit at gcc dot gnu.org
2024-06-11 10:56 ` jakub at gcc dot gnu.org
2024-06-20 13:23 ` cvs-commit at gcc dot gnu.org
2024-06-20 13:25 ` jakub at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-108789-4-Vq2JCRRKJi@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).