Skip to content

Commit

Permalink
opt: cancel out XCHG/XCHG
Browse files Browse the repository at this point in the history
  • Loading branch information
kspalaiologos committed Jan 3, 2021
1 parent 9a2f997 commit cffca08
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions asm_optimize.c
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) { \
Expand Down

0 comments on commit cffca08

Please sign in to comment.