x64 REGVAR optimization causes odd compilation failure

Summary

When compiling the sample project below for x86-64 with -O2, FPC 3.3.1 errors out with the message "Error: Asm: word value exceeds bounds 65536". The error message itself is confusing since it's not an ASM block, and it also doesn't occur when using '-O2 -OoNoRegVar', when compiling for arm64 on macOS, or when compiling with FPC 3.2.2.

Commenting out various lines of the function suppresses the error, and compiling with "-OoRegVar" without "-O2" also succeeds, so it seems to be an interaction between two parts of the optimizer.

System Information

  • Operating system: Linux Mint
  • Processor architecture: x86-64
  • Compiler version: trunk, updated through e4fc454e (Oct 9)
  • Device: PC

Steps to reproduce

Save the example project below as "main.pp" and using FPC 3.3.1, compile it using "fpc -O2 main.pp"

Example Project

program main;

procedure Foo(i: UInt32);
var
  w: Word;
begin
  w := 0;
  if i < $10000 then
    w := word(i)
  else if i < $20000 then
    w := word(i - $10000)
  else if i < $30000 then
    w := word(i - $20000);
end;

begin
end.

What is the current bug behavior?

Unexpected error message

What is the expected (correct) behavior?

Compile the code correctly