public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/111151] [12/13/14 Regression] Wrong code at -O0 on x86_64-pc-linux-gnu
Date: Fri, 22 Mar 2024 18:48:42 +0000	[thread overview]
Message-ID: <bug-111151-4-s3aHCjh08u@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-111151-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #11 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Perhaps
--- fold-const.cc.jj8   2024-03-11 22:37:29.000000000 +0100
+++ fold-const.cc       2024-03-22 19:31:44.189686120 +0100
@@ -7104,6 +7104,27 @@ extract_muldiv_1 (tree t, tree c, enum t
       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
        break;

+      /* Punt for multiplication altogether.
+        MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
+        MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
+        0U, the latter is 2U.
+        MAX (INT_MIN / 2, 0) * -2 is not equivalent to
+        MIN (INT_MIN / 2 * -2, 0 * -2), the former is
+        well defined 0, the latter invokes UB.
+        MAX (INT_MIN / 2, 5) * 5 is not equivalent to
+        MAX (INT_MIN / 2 * 5, 5 * 5), the former is
+        well defined 25, the latter invokes UB.  */
+      if (code == MULT_EXPR)
+       break;
+      /* For division/modulo, punt on c being -1 for MAX, as
+        MAX (INT_MIN, 0) / -1 is not equivalent to
+        MIN (INT_MIN / -1, 0 / -1), the former is well defined
+        0, the latter invokes UB (or for -fwrapv is INT_MIN).
+        MIN (INT_MIN, 0) / -1 already invokes UB, so the
+        transformation won't make it worse.  */
+      else if (tcode == MAX_EXPR && integer_minus_onep (c))
+       break;
+
       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
       sub_strict_overflow_p = false;
       if ((t1 = extract_muldiv (op0, c, code, wide_type,
?
Though
int
main ()
{
  unsigned a = 1U + __INT_MAX__;
  unsigned b = 1U;
  unsigned c = (a > b ? a : b) * 2U;
  if (c != 0U)
    __builtin_abort ();
  int d = (-__INT_MAX__ - 1) / 2;
  int e = 5;
  int f = (d > e ? d : e) * 5;
  if (f != 25)
    __builtin_abort ();
  int g = -__INT_MAX__ - 1;
  int h = 0;
  int i = (g > h ? g : h) / -1;
  if (i != 0)
    __builtin_abort ();
}
doesn't seem to be miscompiled, we just don't do that transformation at all
even without the patch.  Need to tweak such that the min/max arguments are both
something on which extract_muldiv returns non-NULL.

  parent reply	other threads:[~2024-03-22 18:48 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-25  6:39 [Bug middle-end/111151] New: " jwzeng at nuaa dot edu.cn
2023-08-25  7:16 ` [Bug middle-end/111151] " rguenth at gcc dot gnu.org
2023-08-25  7:16 ` rguenth at gcc dot gnu.org
2023-08-25 11:51 ` mikpelinux at gmail dot com
2023-08-25 12:27 ` rguenth at gcc dot gnu.org
2023-08-25 12:28 ` rguenth at gcc dot gnu.org
2023-08-25 12:35 ` rguenth at gcc dot gnu.org
2023-08-26  3:27 ` pinskia at gcc dot gnu.org
2024-03-22 13:40 ` law at gcc dot gnu.org
2024-03-22 17:29 ` jakub at gcc dot gnu.org
2024-03-22 17:37 ` jakub at gcc dot gnu.org
2024-03-22 17:41 ` jakub at gcc dot gnu.org
2024-03-22 17:57 ` jakub at gcc dot gnu.org
2024-03-22 18:48 ` jakub at gcc dot gnu.org [this message]
2024-03-22 19:15 ` jakub at gcc dot gnu.org
2024-03-25  7:23 ` rguenth at gcc dot gnu.org
2024-03-25 11:01 ` jakub at gcc dot gnu.org
2024-03-25 12:45 ` rguenth at gcc dot gnu.org
2024-03-26 10:22 ` cvs-commit at gcc dot gnu.org
2024-03-26 10:32 ` [Bug middle-end/111151] [12/13 " jakub at gcc dot gnu.org
2024-03-26 15:43 ` cvs-commit at gcc dot gnu.org
2024-03-30  3:55 ` cvs-commit 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-111151-4-s3aHCjh08u@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).