public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/66127] New: Division by zero gets folded away
@ 2015-05-12 15:55 mpolacek at gcc dot gnu.org
  2015-05-12 16:06 ` [Bug middle-end/66127] " joseph at codesourcery dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-05-12 15:55 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 66127
           Summary: Division by zero gets folded away
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mpolacek at gcc dot gnu.org
  Target Milestone: ---

In match.pd, we have

(simplify
 (mult @0 integer_zerop@1)
 @1)

so anything * 0 -> 0.  That seems to be undesirable in case "anything" contains
a division by zero.  And a few lines below we have

/* Make sure to preserve divisions by zero.  This is the reason why
   we don't simplify x / x to 1 or 0 / x to 0.  */
(for op (mult trunc_div ceil_div floor_div round_div exact_div)
  (simplify
    (op @0 integer_onep)
    (non_lvalue @0)))

This means that
int
main (void)
{
  int z = 0;
  int a = 0 * (1 / z);
  return a;
}
$ xgcc f.c; ./a.out
is "ok", but e.g.
int
main (void)
{
  int z = 0;
  int a = 1 * (1 / z);
  return a;
}
naturally results in SIGFPE.

Yes, I know that division by zero is UB and there are no guarantees whatsoever,
but this folding is causing me a grief in the C FE because while fold doesn't
fold away "1 / 0", it folds "0 * (1 / 0)" into 0.  That's bad when we find
ourselves in a situation where a constant integer expression is required.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
@ 2015-05-12 16:06 ` joseph at codesourcery dot com
  2015-05-12 16:26 ` manu at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: joseph at codesourcery dot com @ 2015-05-12 16:06 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
Ideally the front-end folding of expressions-of-constants might avoid 
folding-for-optimization such as this (instead just folding cases where 
the evaluated operands are actually constants, so not folding anything 
where 1 / 0 is an evaluated operand).


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
  2015-05-12 16:06 ` [Bug middle-end/66127] " joseph at codesourcery dot com
@ 2015-05-12 16:26 ` manu at gcc dot gnu.org
  2015-05-12 16:42 ` mpolacek at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: manu at gcc dot gnu.org @ 2015-05-12 16:26 UTC (permalink / raw)
  To: gcc-bugs

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

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu.org

--- Comment #2 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
(In reply to joseph@codesourcery.com from comment #1)
> Ideally the front-end folding of expressions-of-constants might avoid 
> folding-for-optimization such as this (instead just folding cases where 
> the evaluated operands are actually constants, so not folding anything 
> where 1 / 0 is an evaluated operand).

I understand that Marek is saying that currently: "1 / 0" is not folded, but "0
* (1 / 0)" is folded into 0.

The answer is that we really need to separate the FE folding from the ME one,
and do only FE folding for language conformance purposes, while we can do ME
folding for warnings and when the FE is finished.  Isn't this what you explain
in text quoted at A.5 at https://gcc.gnu.org/wiki/Better_Diagnostics?

This will also liberate the ME to do optimizations that before could not do
because we wanted to reject invalid programs with -pedantic-errors.

I seem to remember there has been further discussion about this after or while
match.pd was implemented, discussing whether it was worth it that the FE saves
the GIMPLE generated when doing FE folding (or ME folding in case of FE
warnings requesting it), but I cannot find a link now.
>From gcc-bugs-return-486153-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Tue May 12 16:26:56 2015
Return-Path: <gcc-bugs-return-486153-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 99541 invoked by alias); 12 May 2015 16:26:56 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 99461 invoked by uid 48); 12 May 2015 16:26:53 -0000
From: "tromey at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/53553] misleading locations for error in initializers
Date: Tue, 12 May 2015 16:26:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: c++
X-Bugzilla-Version: 4.8.0
X-Bugzilla-Keywords:
X-Bugzilla-Severity: normal
X-Bugzilla-Who: tromey at gcc dot gnu.org
X-Bugzilla-Status: RESOLVED
X-Bugzilla-Resolution: DUPLICATE
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields: bug_status cc resolution
Message-ID: <bug-53553-4-APXmW7H0ZJ@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-53553-4@http.gcc.gnu.org/bugzilla/>
References: <bug-53553-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2015-05/txt/msg00993.txt.bz2
Content-length: 604

https://gcc.gnu.org/bugzilla/show_bug.cgi?idS553

Tom Tromey <tromey at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |tromey at gcc dot gnu.org
         Resolution|---                         |DUPLICATE

--- Comment #2 from Tom Tromey <tromey at gcc dot gnu.org> ---
Dup; preferring the other bug since it is assigned.

*** This bug has been marked as a duplicate of bug 61940 ***


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
  2015-05-12 16:06 ` [Bug middle-end/66127] " joseph at codesourcery dot com
  2015-05-12 16:26 ` manu at gcc dot gnu.org
@ 2015-05-12 16:42 ` mpolacek at gcc dot gnu.org
  2015-05-13 11:05 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-05-12 16:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I suppose this particular issue might be even relevant to e.g.
-mcheck-zero-division on MIPS, i.e. everywhere where we're expect to trap on
integer division by zero.

(ubsan's -fsanitize=integer-divide-by-zero isn't affected by this, because we
add the instrumentation very early, before the division is lost.  Otherwise
we'd miss a diagnostic.)


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2015-05-12 16:42 ` mpolacek at gcc dot gnu.org
@ 2015-05-13 11:05 ` rguenth at gcc dot gnu.org
  2015-05-13 12:10 ` mpolacek at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-05-13 11:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note that the inconsistent handling of */% 0 has been 1:1 translated from
fold-const.c (also the comments).


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2015-05-13 11:05 ` rguenth at gcc dot gnu.org
@ 2015-05-13 12:10 ` mpolacek at gcc dot gnu.org
  2015-05-13 14:35 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-05-13 12:10 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2015-05-13
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
   Target Milestone|---                         |6.0
     Ever confirmed|0                           |1

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I have some patch...


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2015-05-13 12:10 ` mpolacek at gcc dot gnu.org
@ 2015-05-13 14:35 ` rguenth at gcc dot gnu.org
  2015-05-13 14:42 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-05-13 14:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think the proper solution is to mark all problematic operations with
TREE_SIDE_EFFECTS so that the side-effects are preserved when omitting
operands.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2015-05-13 14:35 ` rguenth at gcc dot gnu.org
@ 2015-05-13 14:42 ` rguenth at gcc dot gnu.org
  2015-05-13 14:47 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-05-13 14:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
Like a more complete variant of

Index: gcc/tree.c
===================================================================
--- gcc/tree.c  (revision 223151)
+++ gcc/tree.c  (working copy)
@@ -4402,6 +4402,11 @@ build2_stat (enum tree_code code, tree t
   PROCESS_ARG (0);
   PROCESS_ARG (1);

+  if (code == TRUNC_DIV_EXPR
+      && (TREE_CODE (arg1) != INTEGER_CST
+         || integer_zerop (arg1)))
+    side_effects = 1;
+
   TREE_SIDE_EFFECTS (t) = side_effects;
   if (code == MEM_REF)
     {


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2015-05-13 14:42 ` rguenth at gcc dot gnu.org
@ 2015-05-13 14:47 ` rguenth at gcc dot gnu.org
  2015-05-14  8:46 ` [Bug c/66127] " mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-05-13 14:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
Where I get for the testcase

    int z = 0;
    int a = 1, 0;
  return a;

for some odd reason (1?! ... fold_ignored_results fault...)


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2015-05-13 14:47 ` rguenth at gcc dot gnu.org
@ 2015-05-14  8:46 ` mpolacek at gcc dot gnu.org
  2015-05-14 11:43 ` mpolacek at gcc dot gnu.org
  2015-05-14 11:44 ` mpolacek at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-05-14  8:46 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|middle-end                  |c

--- Comment #9 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
This is going to be solved in c_fully_fold_internal so changing the component.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2015-05-14  8:46 ` [Bug c/66127] " mpolacek at gcc dot gnu.org
@ 2015-05-14 11:43 ` mpolacek at gcc dot gnu.org
  2015-05-14 11:44 ` mpolacek at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-05-14 11:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Author: mpolacek
Date: Thu May 14 11:42:53 2015
New Revision: 223193

URL: https://gcc.gnu.org/viewcvs?rev=223193&root=gcc&view=rev
Log:
        PR c/66066
        PR c/66127
        * c-common.c (c_fully_fold): Pass false down to c_fully_fold_internal.
        (c_fully_fold_internal): Fold C_MAYBE_CONST_EXPRs with
        C_MAYBE_CONST_EXPR_INT_OPERANDS set.  Add FOR_INT_CONST argument and
        use it.  If FOR_INT_CONST, require that all evaluated operands be
        INTEGER_CSTs.

        * c-typeck.c (digest_init): Call pedwarn_init with OPT_Wpedantic
        rather than with 0.

        * gcc.dg/pr14649-1.c: Add -Wpedantic.
        * gcc.dg/pr19984.c: Likewise.
        * gcc.dg/pr66066-1.c: New test.
        * gcc.dg/pr66066-2.c: New test.
        * gcc.dg/pr66066-3.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr66066-1.c
    trunk/gcc/testsuite/gcc.dg/pr66066-2.c
    trunk/gcc/testsuite/gcc.dg/pr66066-3.c
Modified:
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-common.c
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-typeck.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/pr14649-1.c
    trunk/gcc/testsuite/gcc.dg/pr19984.c


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/66127] Division by zero gets folded away
  2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
                   ` (9 preceding siblings ...)
  2015-05-14 11:43 ` mpolacek at gcc dot gnu.org
@ 2015-05-14 11:44 ` mpolacek at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-05-14 11:44 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #11 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Ought to be fixed.


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2015-05-14 11:44 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-12 15:55 [Bug middle-end/66127] New: Division by zero gets folded away mpolacek at gcc dot gnu.org
2015-05-12 16:06 ` [Bug middle-end/66127] " joseph at codesourcery dot com
2015-05-12 16:26 ` manu at gcc dot gnu.org
2015-05-12 16:42 ` mpolacek at gcc dot gnu.org
2015-05-13 11:05 ` rguenth at gcc dot gnu.org
2015-05-13 12:10 ` mpolacek at gcc dot gnu.org
2015-05-13 14:35 ` rguenth at gcc dot gnu.org
2015-05-13 14:42 ` rguenth at gcc dot gnu.org
2015-05-13 14:47 ` rguenth at gcc dot gnu.org
2015-05-14  8:46 ` [Bug c/66127] " mpolacek at gcc dot gnu.org
2015-05-14 11:43 ` mpolacek at gcc dot gnu.org
2015-05-14 11:44 ` mpolacek at gcc dot gnu.org

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).