Bug 98322 - optimizes to false instead true
Summary: optimizes to false instead true
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-16 17:09 UTC by Ivan Sučić
Modified: 2020-12-16 17:48 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ivan Sučić 2020-12-16 17:09:46 UTC
bool always_true (bool a, bool b)
{
	return (a == b) == (~a ^ b);
} 

is optimized to 

xorl	%eax, %eax
	ret

instead return 1.
Comment 1 ktkachov 2020-12-16 17:16:06 UTC
I don't think that's true.
Note, that with -Wall you get the warning:
true.c: In function 'always_true':
true.c:3:29: warning: '~' on a boolean expression [-Wbool-operation]
    3 |         return (a == b) == (~a ^ b);
      |                             ^
true.c:3:29: note: did you mean to use logical not?
    3 |         return (a == b) == (~a ^ b);
      |                             ^
      |
Comment 2 Jakub Jelinek 2020-12-16 17:41:47 UTC
Yeah, folding it to return false; is the right thing.
~a ^ b is ~(int) (a ^ b) in C++, so if a == b, it is ~0 (on two's complement -1),
and if a != b, then it is ~1 (on two's complement -2).
1 != -1
and
0 != -2
so it is always false.
Comment 3 Ivan Sučić 2020-12-16 17:46:44 UTC
Sorry for false report.
Comment 4 Jakub Jelinek 2020-12-16 17:48:06 UTC
.