Optimizing 0-displacement jumps on i386

Jan Beulich jbeulich@suse.com
Tue Aug 12 06:07:17 GMT 2025


On 12.08.2025 03:57, H. Peter Anvin wrote:
> On 2025-08-11 11:19, H. Peter Anvin wrote:
>>
>> Arguably this should be merged with subsequent alignment padding if it
>> fits within one instruction.  However, in order to actually be effective
>> in any meaningful way, this version of the optimization probably needs
>> to be performed in the linker, or it will simply never happen if the
>> code is compiled with linker garbage collection, which is pretty common
>> these days.
>>
>> The optimization in gas would still apply to intra-function jumps.
>>
>> Note that it may very well be necessary to disable this optimization, in
>> case something expects to be able to create a jump instruction and patch
>> its target; such code might use a zero-displacement jump to ensure a
>> two-byte as opposed to a 5/6-byte jump. It should be rare though.
>>
> 
> One more issue: a zero-offset jump may also be used to sync the prefetch
> pipe on the CPUs that require that. For example, the Linux boot code
> contains the following sequence:
> 
>         /* Self-modify the INT instruction.  Ugly, but works. */
>         cmpb    %al, 3f
>         je      1f
>         movb    %al, 3f
>         jmp     1f              /* Synchronize pipeline */
> 1:

Hence why I think such optimizations may not be turned on by default.

Jan


More information about the Binutils mailing list