Skip to content

Potential optimization: (AX << 8) | value => AH<-AL; AL=value #135

@asiekierka

Description

@asiekierka

In my tests, the following code (changed a little for a minimal test case):

typedef unsigned char uint8_t;

typedef struct {
	uint8_t port;
	uint8_t bits;
} ws_eeprom_handle_t;

ws_eeprom_handle_t ws_eeprom_handle_internal(void) {
	ws_eeprom_handle_t handle = {0xBA, *((uint8_t*)0)};
	return handle;
}

compiled to the following code under -O2 -mcmodel=medium (as well as -Os -mcmodel=medium and -O3 -mcmodel=medium):

00000000 <ws_eeprom_handle_internal>:
   0:	a0 00 00             	mov    0x0,%al
   3:	c1 e0 08             	shl    $0x8,%ax
   6:	0c ba                	or     $0xba,%al
   8:	cb                   	lret

I think that SHL/OR pair could be replaced with two MOVs, given that we can actually do so for AX/BX/CX/DX:

mov    0x0,%al
mov    %al,%ah
mov    $0xba,%al

which hopefully the compiler could optimize further to:

mov    0x0,%ah
mov    $0xba,%al

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions