Skip to content
GitLab
  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
    • Switch to GitLab Next
  • Sign in / Register
  • FPC Source FPC Source
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 1,275
    • Issues 1,275
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 58
    • Merge requests 58
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • External wiki
    • External wiki
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • FPC
  • FPC
  • FPC SourceFPC Source
  • Issues
  • #34653
Closed
Open
Created Dec 05, 2018 by FPC Admin account@fpc_adminOwner

Compiler misoptimizes the logical expression

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:

  1. 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.
  1. 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
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking