Wrong code generated with -O3 option by current trunk compiler
<!-- See available text formatting: https://gitlab.com/help/user/markdown.md --> ## Summary <!-- Summarize the bug encountered concisely --> Current trunk compiler compiled with `-O3` option currently fails to run. The simple test below, adapted from `tests/test/cg/tbytetypecast.pp` displays a bug in optimization, which seems to be CPU independent (at least present both for ì386`and `x86_64`). ## System Information <!-- The more information are provided the easier it is to replicate the bug --> - **Operating system:** Any <!-- Windows, Linux (if possible, also name the distro), FreeBSD, Android, ... --> - **Processor architecture:** Any<!-- x86, x86-64, ARM, AARCH64, AVR, RISC-V, PowerPC, ... --> - **Compiler version:** trunk<!-- 3.2, 3.2.2, 3.3, trunk, beta, ... (if possible, give also the git hash) --> ## Steps to reproduce <!-- How one can reproduce the issue - this is very important! --> Copy example below and compile as explained after. ## Example Project <!-- If possible, please create an example project that exhibits the problematic behavior, and link to it here in the bug report. --> ``` muller@cfarm137:~/pas/check$ cat ./tbytetypecast-shortest.pp type TRecord = record w1,w2,w3,w4 : word; end; var w1 : word; b1 : byte; rec : TRecord; ErrorCount : longint; begin ErrorCount:=0; b1:=$57; w1:=$2D57; rec.w1:=w1; if (byte(rec.w1)<>b1) then inc(ErrorCount,3); if (rec.w1=b1) then inc(ErrorCount,4); halt(ErrorCount); end. ``` ## What is the current bug behavior? <!-- What actually happens --> ``` muller@cfarm137:~/pas/check$ fpc -O3 tbytetypecast-shortest.pp Free Pascal Compiler version 3.3.1-20105-gd93b8b83c3-unpushed [2026/04/25] for x86_64 Copyright (c) 1993-2026 by Florian Klaempfl and others muller@cfarm137:~/pas/check$ ./tbytetypecast-shortest ; echo "res=$?" res=4 ``` ## What is the expected (correct) behavior? <!-- What you should see instead --> The test should exit with zero exitcode. ## Relevant logs and/or screenshots <!-- Paste any relevant logs - please use code blocks (```) to format console output, logs, and code --> Using `gdb` shows that the executable is jumping from line `if (byte(rec.w1)<>b1) then`directly to line `inc(ErrorCount,4);` when it sholud jump to previous line `if (rec.w1=b1) then`. ## Possible fixes <!-- If you can, link to the line of code that might be responsible for the problem -->
issue