Re: Optimization techniques and undefined behavior

Kaz Kylheku <847-115-0292@kylheku.com>
Thu, 2 May 2019 17:18:06 +0000 (UTC)

          From comp.compilers

Related articles
[9 earlier articles]
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-01)
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-01)
Re: Optimization techniques and undefined behavior anw@cuboid.co.uk (Andy Walker) (2019-05-02)
Re: Optimization techniques and undefined behavior martin@gkc.org.uk (Martin Ward) (2019-05-02)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-02)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-02)
Re: Optimization techniques and undefined behavior 847-115-0292@kylheku.com (Kaz Kylheku) (2019-05-02)
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-02)
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-02)
Re: Optimization techniques and undefined behavior auriocus@gmx.de (Christian Gollwitzer) (2019-05-02)
Re: Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-03)
Re: Optimization techniques and undefined behavior martin@gkc.org.uk (Martin Ward) (2019-05-03)
Re: Optimization techniques and undefined behavior anw@cuboid.co.uk (Andy Walker) (2019-05-03)
[17 later articles]
| List of all articles for this month |

From: Kaz Kylheku <847-115-0292@kylheku.com>
Newsgroups: comp.compilers
Date: Thu, 2 May 2019 17:18:06 +0000 (UTC)
Organization: Aioe.org NNTP Server
References: <72d208c9-169f-155c-5e73-9ca74f78e390@gkc.org.uk> 19-04-021 19-04-024 19-04-038
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="94877"; mail-complaints-to="abuse@iecc.com"
Keywords: optimize, errors, C
Posted-Date: 02 May 2019 14:15:12 EDT

On 2019-04-28, David Brown <david.brown@hesbynett.no> wrote:
> On 26/04/2019 04:26, Kaz Kylheku wrote:
>> On 2019-04-25, David Brown <david.brown@hesbynett.no> wrote:
>>> It knows that "x * 2 / 2" is "x". It knows that "x + 1 > x" is true.
>>
>> Also, we could have it so that multiplication wraps, and yet in the same
>> breath allow algebraic reductions like this anyway.
>>
>> Basically it can be so that, if overflow takes place in the abstract
>> semantics of "x * 2 / 2", the result can be either the truncated
>> version, or just x, if the algebraic reduction took place.
>>
>
> I am afraid I don't understand you here. Are you saying that you could
> define the language's arithmetic so that, given "x * 2 / 2", the
> compiler could return /either/ "x" or the result you would get from
> wrapped overflow calculations? You are defining two possible "correct"
> answers?


I'm fairly comfortable with this, because it maps to the familiar ISO C
concept of "unspecified behavior".


For instance, this program can produce one of the results -12, -6, -2
or 4, based on which of the six permutations over the order in which the
three functions are called.


    #include <stdio.h>


    int x = 1;


    int add2(void)
    {
        x += 2;
        return 0;
    }


    int mul4(void)
    {
        x *= 4;
        return 0;
    }


    int neg(void)
    {
        x = -x;
        return 0;
    }


    void dummy(int a, int b, int c)
    {
    }


    int main()
    {
        dummy(add2(), mul4(), neg());
        printf("%d\n", x);
    }


Unspecified behavior isn't exactly wonderful, but it's better than
undefined behavior.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.