Last active
October 23, 2022 10:55
-
-
Save pervognsen/592365b12c1295fa49907349054c3089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ValueRef add(ValueRef left, ValueRef right) { | |
// Canonical commutation: x + y => y + x (constants have negative pos and move to the left) | |
if (left.pos > right.pos) SWAP(left, right); | |
switch (left.op) { | |
case NEG: | |
// Strength reduction: (-x) + y => y - x | |
return sub(right, getleft(left)); | |
case NUM: | |
// Strength reduction: 0 + x => x | |
if (getnum(left) == 0) return right; | |
// Constant folding: n1 + n2 => n | |
if (right.op == NUM) return num(getnum(left) + getnum(right)); | |
} | |
switch (right.op) { | |
case NEG: | |
// Strength reduction: x + (-y) => x - y | |
return sub(left, getleft(right)); | |
case ADD: | |
// Reassociation: x + (y + z) => (x + y) + z | |
return add(add(left, getleft(right)), getright(right)); | |
} | |
return binary_cse(left, right, ADD); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add: | |
cmp rdi, rdx | |
mov r9, rcx | |
mov r10, rdx | |
cmovle r9, rsi | |
cmovle r10, rdi | |
cmovle rsi, rcx | |
cmovle rdi, rdx | |
test r9, r9 | |
je .L33 | |
cmp r9, 1 | |
jne .L34 | |
movsx rdx, WORD PTR buffer[262148+r10*8] | |
movzx ecx, BYTE PTR buffer[262144+rdx*8] | |
jmp sub | |
.L33: | |
mov eax, DWORD PTR buffer[262148+r10*8] | |
test eax, eax | |
je .L49 | |
test rsi, rsi | |
je .L50 | |
.L34: | |
cmp rsi, 1 | |
je .L40 | |
push r13 | |
push r12 | |
sub rsp, 8 | |
cmp rsi, 2 | |
je .L41 | |
mov rdx, rdi | |
mov rcx, rsi | |
mov r8d, 2 | |
mov rdi, r10 | |
mov rsi, r9 | |
call binary_cse_pos | |
add rsp, 8 | |
mov edx, 2 | |
pop r12 | |
pop r13 | |
ret | |
.L41: | |
lea rax, buffer[262144+rdi*8] | |
mov rsi, r9 | |
mov rdi, r10 | |
movsx rdx, WORD PTR [rax+4] | |
movsx r12, WORD PTR [rax+6] | |
movzx ecx, BYTE PTR buffer[262144+rdx*8] | |
movzx r13d, BYTE PTR buffer[262144+r12*8] | |
call add | |
add rsp, 8 | |
mov rcx, r13 | |
mov rsi, rdx | |
mov rdi, rax | |
mov rdx, r12 | |
pop r12 | |
pop r13 | |
jmp add | |
.L40: | |
movsx rdx, WORD PTR buffer[262148+rdi*8] | |
mov rsi, r9 | |
mov rdi, r10 | |
movzx ecx, BYTE PTR buffer[262144+rdx*8] | |
jmp sub | |
.L49: | |
mov rax, rdi | |
mov rdx, rsi | |
ret | |
.L50: | |
movsx rdx, WORD PTR bottom[rip] | |
add eax, DWORD PTR buffer[262148+rdi*8] | |
mov rcx, rdx | |
test rdx, rdx | |
js .L39 | |
jmp .L37 | |
.L51: | |
add rdx, 1 | |
je .L37 | |
.L39: | |
cmp eax, DWORD PTR buffer[262148+rdx*8] | |
jne .L51 | |
.L38: | |
mov rax, rdx | |
xor edx, edx | |
ret | |
.L37: | |
lea edx, [rcx-1] | |
mov WORD PTR bottom[rip], dx | |
movsx rdx, dx | |
mov QWORD PTR buffer[262144+rdx*8], 0 | |
mov DWORD PTR buffer[262148+rdx*8], eax | |
jmp .L38 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment