Re: [tip: x86/cleanups] x86/segment: Use MOVL when reading segment registers

From: H. Peter Anvin

Date: Thu Mar 12 2026 - 14:58:26 EST


On 2026-03-12 02:30, Uros Bizjak wrote:
>
> so one can use this variant when the segment register is really saved
> to a memory. The above code improves to:
>
> 32333: 8c a2 0c 0c 00 00 mov %fs,0xc0c(%rdx)
> 32339: 8c aa 0e 0c 00 00 mov %gs,0xc0e(%rdx)
>
> Unfortunately, inline asm does not support alternative instructions
> that would depend on output argument type (reg vs mem), so the
> selection can not be automatic. But by having specialized macro, a
> developer can use the variant that results in the most optimal code.
>
> A prototype patch is attached to the message.
>

On a more general note, there is a *really* ugly way to distinguish operand
type on the gas side:


.section ".rodata","a"

.macro classify typevar:req argument:req
\typevar = 0
.irpc char,\argument
.ifeq .L_type
.ifc \char,%
\typevar = 1 /* Register */
.endif
.ifc \char,(
\typevar = 2 /* Memory */
.endif
.ifc \char,$
\typevar = 3 /* Immediate */
.endif
.endif
.endr
.ifne .L_type
\typevar = 2
.endif
.endm

classify .L_type,%rax

.ascii "Type = "
.byte .L_type + 0x30
.asciz "\n"


-hpa