[Bug c++/92297] The expression 0 / X is simplified to 0 even when the variable X is 0

2019-10-31 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92297

--- Comment #7 from Andrew Pinski  ---
> Then if X = 0 we should expect an exception triggered at runtime, as we have 
> for example for 1 / 0.

No undefined does not mean trap at runtime, it means anything can happen ...

[Bug c++/92297] The expression 0 / X is simplified to 0 even when the variable X is 0

2019-10-31 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92297

--- Comment #6 from Jonathan Wakely  ---
(In reply to Luca Rocca from comment #2)
> Consider also for comparison the approach of GCC up to gcc-6.4.0,
> reading this comment from the corresponding file gcc-6.4.0/gcc/match.pd:
> 
> /* 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 the same code, GCC up to 6.4.0 does not perform the simplification and
> when
> X = 0 we have an exception raised at runtime as expected

And that was changed intentionally by r242636.

The code is undefined, so GCC is not required to always compile it the same
way.

[Bug c++/92297] The expression 0 / X is simplified to 0 even when the variable X is 0

2019-10-31 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92297

--- Comment #5 from Jonathan Wakely  ---
*** Bug 92299 has been marked as a duplicate of this bug. ***

[Bug c++/92297] The expression 0 / X is simplified to 0 even when the variable X is 0

2019-10-31 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92297

--- Comment #4 from Jonathan Wakely  ---
*** Bug 92298 has been marked as a duplicate of this bug. ***

[Bug c++/92297] The expression 0 / X is simplified to 0 even when the variable X is 0

2019-10-31 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92297

Jonathan Wakely  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |INVALID

--- Comment #3 from Jonathan Wakely  ---
(In reply to Luca Rocca from comment #2)
> Division by 0 is always undefined, regardless of the numerator.
> So, 0 / X should not be simplified if we cannot exclude that X = 0.

No. You don't need to exclude X=0 because if X=0 the behaviour is undefined and
so any result is valid (including the result of simplifying it).

> Then if X = 0 we should expect an exception triggered at runtime,
> as we have for example for 1 / 0.

No, that's not what undefined behaviour means. You are wrong to expect a
runtime error, that would be *defined* behaviour.

[Bug c++/92297] The expression 0 / X is simplified to 0 even when the variable X is 0

2019-10-31 Thread disquisitiones at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92297

Luca Rocca  changed:

   What|Removed |Added

 Status|RESOLVED|UNCONFIRMED
 Resolution|INVALID |---

--- Comment #2 from Luca Rocca  ---
Division by 0 is always undefined, regardless of the numerator.
So, 0 / X should not be simplified if we cannot exclude that X = 0.
Then if X = 0 we should expect an exception triggered at runtime,
as we have for example for 1 / 0.

Consider the relevant section of the file gcc-9.2.0/gcc/match.pd:

/* 0 / X is always zero.  */
 (simplify
  (div integer_zerop@0 @1)
  /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
  (if (!integer_zerop (@1))
   @0))

It seems that the intention is in fact to perform the simplification
except for the case 0 / 0, but for some reason this is not implemented
correctly.

Consider also for comparison the approach of GCC up to gcc-6.4.0,
reading this comment from the corresponding file gcc-6.4.0/gcc/match.pd:

/* 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 the same code, GCC up to 6.4.0 does not perform the simplification and when
X = 0 we have an exception raised at runtime as expected

[Bug c++/92297] The expression 0 / X is simplified to 0 even when the variable X is 0

2019-10-30 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92297

Andrew Pinski  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |INVALID

--- Comment #1 from Andrew Pinski  ---
0 / 0 is undefined so having 0 / X simplify to 0 is a valid thing to do.

What were you expecting?