Compiler misoptimizes the logical expression
Original Reporter info from Mantis: frol
-
Reporter name:
Original Reporter info from Mantis: frol
- Reporter name:
Description:
The following code outputs "YES YES" when compiled with -O0
, -O1
, -O3
, -O4
, but outputs "NO YES" with -O2
.
Notes:
* the if
expressions are completely the same, so there is no reason for the output to be different!
* the first WriteLn;
is essential to reproduce the bug, but it can be replaced with some other computations.
* the issue is reproducible on latest Debian, Ubuntu, CentOS, AlpineLinux x86_64 with FPC installed from the official repositories and the prebuilt-binaries from FreePascal.org
* the issue is NOT reproducible on Arch Linux
* the issue affects FPC 3.0.0+ and is not reproducible on FPC 2.6.4
Steps to reproduce:
- Compile the following code with
fpc -O2 qq.pas
and run the program:
var d: LongInt;
begin
WriteLn;
d := 828;
if ((d mod 2) = 0) xor ((d < 0) and ((d mod 3) = 0)) then
WriteLn('YES')
else
WriteLn('NO');
if ((d mod 2) = 0) xor ((d < 0) and ((d mod 3) = 0)) then
WriteLn('YES')
else
WriteLn('NO');
end.
- The expected output is "YES YES", but the output is "NO YES"
Mantis conversion info:
- Mantis ID: 34653
- OS: Debian
- OS Build: 9.6
- Platform: Linux x86_64
- Version: 3.0.4
- Fixed in version: 3.3.1
- Fixed in revision: 40934 (#e24449bc)
- Monitored by: » frol (frol), » Vincent (Vincent Snijders)
- Target version: 3.3.1