public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* basic VRP min/max range overflow question
@ 2005-06-17  4:28 Paul Schlie
  2005-06-17  7:03 ` Paolo Bonzini
  2005-06-17 12:28 ` Diego Novillo
  0 siblings, 2 replies; 70+ messages in thread
From: Paul Schlie @ 2005-06-17  4:28 UTC (permalink / raw)
  To: Diego Novillo; +Cc: Eric Botcazou, gcc

Upon a potential integer overflow of either it's min or max range,
shouldn't the result be set to [min:type-min-value, max:type-max-value],
without the necessity of any further designations?

As this would seem to most accurately and simply represent the effective
bounds of an underflowed integer value constrained to an arbitrarily sized
integer type size for all targets I can think of, with the exception of
DSP's which tend to uniquely support saturating integer arithmetic, in
which case only the min or max range would be saturated correspondingly.


^ permalink raw reply	[flat|nested] 70+ messages in thread
* Re: basic VRP min/max range overflow question
@ 2005-06-17 12:00 Paul Schlie
  2005-06-17 12:09 ` Paolo Bonzini
  0 siblings, 1 reply; 70+ messages in thread
From: Paul Schlie @ 2005-06-17 12:00 UTC (permalink / raw)
  To: Paolo Bonzini, gcc

> Paolo Bonsini wrote:
>> Upon a potential integer overflow of either it's min or max range,
>> shouldn't the result be set to [min:type-min-value, max:type-max-value],
>> without the necessity of any further designations?
>
> No.
> 
> [10, INT_MAX] + [ 1, 1 ] == [ 11, INT_MAX ] because of the famous signed
> int overflow definition in the C standard.
>
> [10U, UINT_MAX] + [ 1U, 1U ] == ~[ 1U, 10U ]

??? Do you mean:

H.2.2  Integer types

       [#1] The signed C integer types int,  long,  long  long  and  the
       corresponding  unsigned  types  are compatible with LIA-1.  If an
       implementation adds support  for  the  LIA-1  exceptional  values
       integer_overflow  and  undefined,  then  those  types  are  LIA-1
       conformant types.  C's unsigned integer types are "modulo" in the
       LIA-1  sense  in that overflows or out-of-bounds results silently
       wrap. An implementation that defines signed integer types as also
       being  modulo  need  not  detect integer overflow, in which case,
       only integer divide-by-zero need be detected.

Where in combination with:

5.1.2.3  Program execution

Examples 2.  In executing the fragment

                     char c1, c2;
                     /* ... */
                     c1 = c1 + c2;

             the ``integer promotions'' require that  the  abstract
             machine promote the value of each variable to int size
             and then add  the  two  ints  and  truncate  the  sum.
             Provided the addition of two chars can be done without
             overflow,  or  with  overflow  wrapping  silently   to
             produce  the correct result, the actual execution need
             only produce the same result,  possibly  omitting  the
             promotions.

It seems pretty clear given that for all practical purposes all typical
machines do silently wrap integer overflow, they all correspondingly yield:

  [10, INT_MAX] + [ 1, 1 ] == [INT_MIN, INT_MAX ]

  [10U, UINT_MAX] + [ 1U, 1U ] == [UINT_MIN, UINT_MAX]

or more generally: [<INTEGER-TYPE>_MIN, <INTEGER-TYPE>_MAX] upon overflow.


^ permalink raw reply	[flat|nested] 70+ messages in thread
* Re: basic VRP min/max range overflow question
@ 2005-06-18 14:15 Paul Schlie
  2005-06-18 16:19 ` Joseph S. Myers
                   ` (3 more replies)
  0 siblings, 4 replies; 70+ messages in thread
From: Paul Schlie @ 2005-06-18 14:15 UTC (permalink / raw)
  To: Dale Johannesen, Robert Dewar, Mike Stump, Andrew Pinski; +Cc: GCC Development

> Mike Stump wrote:
>> Paul Schlie wrote:
>> - If the semantics of an operation are "undefined", I'd agree; but if
>>   control is returned to the program, the program's remaining specified
>>   semantics must be correspondingly obeyed, including the those which
>>   may utilize the resulting value of the "undefined" operation.
>
> I am sorry the standard disagrees with you:
>
>        [#3] A  program  that  is  correct  in  all  other  aspects,
>        operating  on  correct data, containing unspecified behavior
>        shall be a  correct  program  and  act  in  accordance  with
>        5.1.2.3.
>
> :-(  Maybe you just mean that you'd like it if the compiler should
> follow the remaining semantics as best it can, on that point, I'd agree.

Maybe I didn't phrase my statement well; I fully agree with the cited
paragraph above which specifically says a program containing unspecified
behavior "shall be a  correct  program  and  act  in  accordance  with
5.1.2.3". Which specifies program execution, in terms of an abstract machine
model, which correspondingly requires:

       [#2] ... At  certain  specified
       points in the execution sequence called sequence points, all
       side effects of previous evaluations shall be  complete  and
       no  side  effects of subsequent evaluations shall have taken
       place.

       [#3] In the abstract machine, all expressions are  evaluated
       as  specified  by  the  semantics.  An actual implementation
       need not evaluate part of an expression  if  it  can  deduce
       that  its  value is not used and that no needed side effects
       are produced ...

Therefore regardless of the result of an "undefined" result/operation at
it's enclosing sequence point, the remaining program must continue to abide
by the specified semantics of the language.

> Robert Dewar writes:
> You are forgetting the "as if" semantics that is always inherent in
> the standard. So after an operation with an undefined result, we can
> do anything we like with the value, since it is "as if" the operation
> had produced that value. So for example, if we skip an operation because
> we know it will overflow, and end up using uninitialized junk in a register,
> it is "as if" that undefined operation produced the particular uninitialized
> junk value that we ended up with.
> 
> So the above is inventive but wrong.

No, as above, the standard clearly requires that the side-effects of an
undefined operation be effectively bounded at it's enclosing sequence
points. Therefore any optimizations performed beyond these bounds must be
"as if" consistent with any resulting stored value/state side effects which
may have resulted from an operation's undefined behavior.

Therefore clearly, subsequent evaluations/optimizations must comply with
whatever logical state is defined to exist subsequent to an implementation's
choice of an undefined behavior past it's delineated sequence point if
logical execution is allowed to proceed past them.

Therefore clearly:

- an optimization which presume a value which differs from the effective
stored value past a sequence point may result in erroneous behavior; which
as would be the case in the example I provided.

- an optimization which presumes the execution state of a program does not
proceed past a sequence point. but in fact does, may result in erroneous
behavior; which would be the case if NULL pointer comparisons were optimized
away presuming an earlier pointer dereference would have prevented execution
from proceeding past it's enclosing sequence point if in fact it does not.

> Dale Johannesen writes:
> You are wrong, and this really isn't a matter of opinion.  The standard
> defines exactly what it means by "undefined behavior":
> 
> 3.4.3 1 undefined behavior
> behavior, upon use of a nonportable or erroneous program construct or
> of erroneous data, for which this International Standard imposes no
> requirements
>
> 2 NOTE  Possible undefined behavior ranges from ignoring the situation
> completely with unpredictable results, to behaving during translation or
> program execution in a documented manner characteristic of the environment
> (with or without the issuance of a diagnostic message), to terminating a
> translation or execution (with the issuance of a diagnostic message).

- as above, the logical side effects of an undefined behavior (what ever
  it's defined to be by an implementation) are clearly logically constrained
  to within the bounds of the it's enclosing sequence points.



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

end of thread, other threads:[~2005-07-19 10:22 UTC | newest]

Thread overview: 70+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-17  4:28 basic VRP min/max range overflow question Paul Schlie
2005-06-17  7:03 ` Paolo Bonzini
2005-06-17 12:28 ` Diego Novillo
2005-06-17 12:00 Paul Schlie
2005-06-17 12:09 ` Paolo Bonzini
2005-06-17 18:29   ` Paul Schlie
2005-06-17 22:09     ` Paolo Bonzini
2005-06-17 22:48     ` Diego Novillo
2005-06-18  0:20       ` Paul Schlie
2005-06-18  0:23         ` Andrew Pinski
2005-06-18  0:59           ` Paul Schlie
2005-06-18  1:10             ` Dale Johannesen
2005-06-18  4:09             ` Mike Stump
     [not found]             ` <25364524.1119085038744.JavaMail.root@dtm1eusosrv72.dtm.ops.eu.uu.net>
2005-06-18 11:47               ` Toon Moene
2005-06-18  2:02         ` Robert Dewar
2005-06-18 14:15 Paul Schlie
2005-06-18 16:19 ` Joseph S. Myers
2005-06-18 17:44   ` Paul Schlie
2005-06-18 18:05     ` Paul Schlie
2005-06-18 18:21       ` Joseph S. Myers
2005-06-18 18:50         ` Paul Schlie
2005-06-18 19:57           ` Joseph S. Myers
2005-06-18 21:01           ` Florian Weimer
2005-06-19 10:18             ` Kai Henningsen
2005-06-20 22:20           ` Mike Stump
2005-06-18 19:16         ` Paul Schlie
2005-06-18 21:26           ` Robert Dewar
2005-06-19 18:23             ` Paul Schlie
2005-06-20  2:44               ` Robert Dewar
2005-06-20  5:55                 ` Paul Schlie
2005-06-20 10:14                   ` Michael Veksler
2005-06-20 11:04                     ` Paul Schlie
2005-06-20 11:32                       ` Robert Dewar
2005-06-20 12:08                         ` Paul Schlie
2005-06-20 12:39                           ` Robert Dewar
2005-06-20 13:01                             ` Paul Schlie
2005-06-20 13:14                               ` Robert Dewar
2005-06-20 12:23                       ` Michael Veksler
2005-06-20 12:46                         ` Robert Dewar
2005-06-20 10:43                   ` Robert Dewar
2005-06-20 10:54                     ` Robert Dewar
2005-06-20 11:26                       ` Paul Schlie
2005-06-20 11:34                         ` Robert Dewar
2005-06-20 12:09                           ` Paul Schlie
2005-06-20 11:14                     ` Paul Schlie
2005-06-20 12:53                       ` Michael Veksler
2005-06-20 12:59                         ` Robert Dewar
2005-06-20 13:09                         ` Paul Schlie
2005-06-20 13:17                           ` Robert Dewar
2005-06-20 14:54                           ` Michael Veksler
2005-06-20 18:01                             ` Paul Schlie
2005-06-18 18:08     ` Joseph S. Myers
2005-06-18 21:08   ` Robert Dewar
2005-06-19 10:18     ` Kai Henningsen
2005-06-19 10:49       ` Robert Dewar
2005-06-20 13:22         ` Sebastian Pop
2005-06-20 18:10           ` DJ Delorie
2005-07-13  7:57             ` Sebastian Pop
2005-07-13 15:18               ` DJ Delorie
2005-06-21 15:21           ` Robert Dewar
2005-07-18 16:34             ` Sebastian Pop
2005-07-18 16:44               ` Robert Dewar
2005-07-19  6:44                 ` Sebastian Pop
2005-07-19  7:07                   ` Michael Veksler
2005-07-19  9:55                     ` Sebastian Pop
2005-07-19 10:22                       ` Michael Veksler
2005-06-20 19:53         ` Kai Henningsen
2005-06-18 20:55 ` Robert Dewar
2005-06-18 22:45 ` Tristan Wibberley
2005-06-20 21:47 ` Mike Stump

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