Skip to content

[x86] Fixed "backward optimisation" that would incorrectly remove modifications to the stack pointer

J. Gareth "Kit" Moreton requested to merge CuriousKit/optimisations:i39555 into main

Summary

This merge request fixes a bug with the optimisation over at !123 (merged) that caused modifications to the stack pointer to get removed and cause fatal errors to appear in a program later on. This fixes the bug reported in #39555 (comment 832854378).

System

  • Operating system: Windows (maybe others)
  • Processor architecture: i386 (possibly i8086 - doesn't seem to occur in x86_64, but not impossible)

What is the current bug behavior?

If the following pair of instructions are found (it was usually this offset and destination register, even though they don't actually matter)...

leal	-16(%esp),%esp
movl	%esp,%edi

...they are incorrectly optimised to just leal -16(%esp),%edi, since LEA counts as a 'pure function' as described in the code's comments, thus removing the change to the stack pointer. This only seems to happen on pure -i386 because function prologues and the like are constructed differently on later targets that use the frame pointer in such operations rather than the stack pointer.

What is the behavior after applying this patch?

This 'optimisation' with the stack pointer no longer occur

Relevant logs and/or screenshots

See code sample above. The samples notably appeared in the Variants unit, and a routine in the System unit associated with variants, hence why it was those relevant tests that failed.

Merge request reports

Loading