From cffca086150500a77c5201f9ac361b079674cba9 Mon Sep 17 00:00:00 2001 From: Kamila Szewczyk Date: Sun, 3 Jan 2021 17:27:49 +0100 Subject: [PATCH] opt: cancel out XCHG/XCHG --- asm_optimize.c | 20 ++++++++++++++++++-- vector.h | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/asm_optimize.c b/asm_optimize.c index 7ded064..852cd4e 100644 --- a/asm_optimize.c +++ b/asm_optimize.c @@ -1,7 +1,15 @@ #include "asm_common.h" + +#define INSERT_HERE(i, x) vector_insert(*data, n - vector_begin(*data) + (i), (x)) +#define ERASE_HERE(i) vector_erase(*data, n - vector_begin(*data) + (i)) + +#define HAS(x) ((uintptr_t) (n - vector_begin(*data) - (x)) > vector_size(*data)) + void asm_optimize(vector(struct node_t) * data) { - vector_foreach(struct node_t, n, *data) { + for(uint32_t idx = 0; idx < vector_size(*data); idx++) { + struct node_t * n = (*data) + idx; + switch(n->type) { case MUL: if(n->data1.type == IMM_VALUE && n->data1.value == 2) { @@ -10,7 +18,15 @@ void asm_optimize(vector(struct node_t) * data) { n->data1.value = 0; n->type = ADD; - vector_insert(*data, n - vector_begin(*data), *n); + INSERT_HERE(0, *n); + } + + break; + case XCHG: + if(HAS(1) && n[1].type == XCHG) { + // cancel out XCHG / XCHG + ERASE_HERE(0); + ERASE_HERE(0); } break; diff --git a/vector.h b/vector.h index bc1c3f8..d803900 100644 --- a/vector.h +++ b/vector.h @@ -66,7 +66,7 @@ do { \ if (vec) { \ const size_t cv_sz = vector_size(vec); \ - if ((i) < cv_sz) { \ + if (((uintptr_t) (i)) < cv_sz) { \ _internal_vecsiz((vec), cv_sz - 1); \ size_t cv_x; \ for (cv_x = (i); cv_x < (cv_sz - 1); ++cv_x) { \