Path: csiph.com!goblin3!goblin.stu.neva.ru!news.misty.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Kaz Kylheku <847-115-0292@kylheku.com> Newsgroups: comp.compilers Subject: Re: Optimization techniques and undefined behavior Date: Thu, 2 May 2019 17:18:06 +0000 (UTC) Organization: Aioe.org NNTP Server Lines: 60 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <19-05-010@comp.compilers> References: <72d208c9-169f-155c-5e73-9ca74f78e390@gkc.org.uk> <19-04-021@comp.compilers> <19-04-024@comp.compilers> <19-04-038@comp.compilers> 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 X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:2246 On 2019-04-28, David Brown wrote: > On 26/04/2019 04:26, Kaz Kylheku wrote: >> On 2019-04-25, David Brown 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 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.