Skip to content

Instantly share code, notes, and snippets.

@DocBohn
Last active May 9, 2025 11:50
Show Gist options
  • Save DocBohn/7e0ca7bdf241e9dffb069c0c4a6c51c7 to your computer and use it in GitHub Desktop.
Save DocBohn/7e0ca7bdf241e9dffb069c0c4a6c51c7 to your computer and use it in GitHub Desktop.
Code that demonstrations elimination of redundant stores
void multiply_add(int *accumulator, int multiplier, int addend) {
*accumulator *= multiplier;
*accumulator += addend;
}
// -fgcse-sm
multiply_add:
ldr w3, [x0]
madd w3, w3, w1, w2
str w3, [x0]
ret
# -fgcse-sm
multiply_add:
imull (%rdi), %esi
addl %edx, %esi
movl %esi, (%rdi)
ret
void optimized_multiply_add(int *accumulator, int multiplier, int addend) {
int local_accumulator = *accumulator;
local_accumulator *= multiplier;
local_accumulator += addend;
*accumulator = local_accumulator;
}
multiply_add:
ldr w3, [x0]
mul w1, w3, w1
str w1, [x0]
add w1, w1, w2
str w1, [x0]
ret
multiply_add:
imull (%rdi), %esi
movl %esi, (%rdi)
addl %edx, %esi
movl %esi, (%rdi)
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment