fix: x64 peephole
authoryu.dongliang <18588496441@163.com>
Sun, 6 Aug 2023 10:56:13 +0000 (18:56 +0800)
committeryu.dongliang <18588496441@163.com>
Sun, 6 Aug 2023 10:56:13 +0000 (18:56 +0800)
native/x64/scf_x64.c
native/x64/scf_x64_bb_color.c
native/x64/scf_x64_peephole.c
test/scf_malloc.c

index dba3224e7f07d9de8d5949e48ac59f337dd725e1..cbec2432ea343d56ddb781e14a5bfab92a37daa5 100644 (file)
@@ -290,9 +290,8 @@ static void _x64_rcg_node_printf(x64_rcg_node_t* rn)
                scf_variable_t* v = rn->dag_node->var;
 
                if (v->w) {
-                       scf_logw("v_%d_%d/%s, %p,%p, ",
-                                       v->w->line, v->w->pos, v->w->text->data,
-                                       v, rn->dag_node);
+                       scf_logw("v_%d_%d/%s, ",
+                                       v->w->line, v->w->pos, v->w->text->data);
 
                        if (v->bp_offset < 0)
                                printf("bp_offset: -%#x, ", -v->bp_offset);
index 16ce76d4de56a79b5b4763d54bddf82c175ee356..4200d13431364ad3a519e3fa15e33636dce518cf 100644 (file)
@@ -138,7 +138,7 @@ int x64_bb_load_dn(intptr_t color, scf_dag_node_t* dn, scf_3ac_code_t* c, scf_ba
 int x64_bb_save_dn(intptr_t color, scf_dag_node_t* dn, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_function_t* f)
 {
        scf_variable_t*     v = dn->var;
-       scf_register_t* r;
+       scf_register_t*     r;
        scf_instruction_t*  inst;
 
        int inst_bytes;
@@ -154,6 +154,9 @@ int x64_bb_save_dn(intptr_t color, scf_dag_node_t* dn, scf_3ac_code_t* c, scf_ba
                        return -ENOMEM;
        }
 
+       if (color < 0)
+               return 0;
+
        r   = x64_find_register_color(color);
 
        ret = x64_save_var2(dn, r, c, f);
index 05c731d0afe19fda81cd9b0728d6260e57367d1f..0c2ec1ad7d597f54690e1a93d96a9043520d949c 100644 (file)
@@ -177,9 +177,6 @@ static int _x64_peephole_common(scf_vector_t* std_insts, scf_instruction_t* inst
 
 static int _x64_peephole_cmp(scf_vector_t* std_insts, scf_instruction_t* inst)
 {
-       if (0 == inst->src.flag && 0 == inst->dst.flag)
-               return 0;
-
        scf_3ac_code_t*    c  = inst->c;
        scf_basic_block_t* bb = c->basic_block;
 
@@ -258,7 +255,14 @@ check:
                                        || inst->dst.base  == std->dst.base)
                                std->nb_used++;
                }
+
+               if (scf_inst_data_same(&inst->src, &std->dst)
+                || scf_inst_data_same(&inst->dst, &std->dst)) {
+
+                       assert(0 == scf_vector_del(std_insts, std));
+               }
        }
+
        return 0;
 }
 
@@ -472,6 +476,8 @@ int x64_optimize_peephole(scf_native_t* ctx, scf_function_t* f)
                                        goto next;
                                }
 
+                               scf_instruction_print(inst);
+
                                if (SCF_X64_CMP == inst->OpCode->type || SCF_X64_TEST == inst->OpCode->type) {
 
                                        ret = _x64_peephole_cmp(std_insts, inst);
index 8eab60242b0df2f2d7f62358262d0d71e9585bec..3807802a2d59a3f4b6577b5c3da764376eef3700 100644 (file)
@@ -1,12 +1,11 @@
 
-int      scf__brk (uint8_t* addr);
-uint8_t* scf__sbrk(uintptr_t inc);
+int      brk (uint8_t* addr);
+uint8_t* sbrk(uintptr_t inc);
 
-int      scf_printf(const char* fmt, ...);
+int      printf(const char* fmt, ...);
 
-int      scf__memseti(uint8_t* dst, uint32_t i,   uintptr_t n);
-int      scf__memsetq(uint8_t* dst, uint64_t q,   uintptr_t n);
-int      scf__memcpy (uint8_t* dst, uint8_t* src, uintptr_t size);
+uint8_t* memset(uint8_t* dst, int c,        uintptr_t n);
+uint8_t* memcpy(uint8_t* dst, uint8_t* src, uintptr_t n);
 
 // min size of block is 64, so low 6 bits can be used for flags
 // flags in prev_size:
@@ -50,7 +49,7 @@ uint8_t* scf__malloc(uintptr_t size)
 
                uintptr_t pages = (bytes + 4095) >> 12;
 
-               p = scf__sbrk(pages << 12);
+               p = sbrk(pages << 12);
                if (!p)
                        return NULL;
                scf__last_brk = p + (pages << 12);
@@ -77,7 +76,7 @@ uint8_t* scf__malloc(uintptr_t size)
        addr = p + sizeof(scf_mblock_t);
 
        if (0 == rest) {
-               scf_printf("%s(),%d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk);
+               printf("%s(),%d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk);
                return addr;
        }
 
@@ -103,7 +102,7 @@ uint8_t* scf__malloc(uintptr_t size)
                b2->prev_size |= 0x2;
        }
 
-       scf_printf("%s(),%d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk);
+       printf("%s(),%d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk);
        return addr;
 }
 
@@ -121,12 +120,12 @@ int scf__free(uint8_t* p)
        intptr_t  i;
 
        if (b->prev_size & 0x4) {
-               scf_printf("%s(), %d, error: double free: %p\n", __func__, __LINE__, p);
+               printf("%s(), %d, error: double free: %p\n", __func__, __LINE__, p);
                return -1;
        }
 
        if (0x10f0 != b->magic) {
-               scf_printf("%s(), %d, error: corruption free: %p\n", __func__, __LINE__, p);
+               printf("%s(), %d, error: corruption free: %p\n", __func__, __LINE__, p);
                return -1;
        }
 
@@ -210,9 +209,9 @@ int scf__free(uint8_t* p)
 
                if (scf__last_brk == (uint8_t*)b + bytes) {
 
-                       scf_printf("%s(), %d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk);
+                       printf("%s(), %d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk);
                        scf__last_brk = (uint8_t*)b;
-                       scf__brk((uint8_t*)b);
+                       brk((uint8_t*)b);
 
                        int flag = 1;
                        while (flag) {
@@ -231,16 +230,16 @@ int scf__free(uint8_t* p)
                                        }
                                        *pb = (scf_mblock_t*)b->free;
 
-                                       scf_printf("%s(), %d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk);
+                                       printf("%s(), %d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk);
                                        scf__last_brk = (uint8_t*)b;
-                                       scf__brk((uint8_t*)b);
+                                       brk((uint8_t*)b);
                                        flag = 1;
                                }
                        }
                } else {
                        b->free = (uintptr_t)scf__free_blocks;
                        scf__free_blocks = b;
-                       scf_printf("%s(), %d, b: %p\n", __func__, __LINE__, b);
+                       printf("%s(), %d, b: %p\n", __func__, __LINE__, b);
                }
                return 0;
        }
@@ -253,7 +252,7 @@ int scf__free(uint8_t* p)
        b->free = (uintptr_t)scf__mblocks[i];
        scf__mblocks[i] = b;
 
-       scf_printf("%s(), %d, b: %p\n", __func__, __LINE__, b);
+       printf("%s(), %d, b: %p\n", __func__, __LINE__, b);
        return 0;
 }
 
@@ -273,9 +272,9 @@ uint8_t* scf__calloc(uintptr_t n, uintptr_t size)
 
        bytes  = b->cur_size;
        bytes -= sizeof(scf_mblock_t);
-       scf_printf("%s(),%d, calloc, b: %p, bytes: %ld\n", __func__, __LINE__, b, bytes);
+       printf("%s(),%d, calloc, b: %p, bytes: %ld\n", __func__, __LINE__, b, bytes);
 
-       scf__memsetq(p, 0, bytes >> 3);
+       memset(p, 0, bytes);
 
        return p;
 }
@@ -298,10 +297,10 @@ uint8_t* scf__realloc(uint8_t* p, uintptr_t size)
                if (!p2)
                        return NULL;
 
-               scf__memcpy(p2, p, bytes);
+               memcpy(p2, p, bytes);
                scf__free(p);
 
-               scf_printf("%s(), %d, realloc: %p->%p, size: %ld\n", __func__, __LINE__, p, p2, size);
+               printf("%s(), %d, realloc: %p->%p, size: %ld\n", __func__, __LINE__, p, p2, size);
                return p2;
        }
 
@@ -331,7 +330,7 @@ uint8_t* scf__realloc(uint8_t* p, uintptr_t size)
        p2 = (uint8_t*)b2 + sizeof(scf_mblock_t);
        scf__free(p2);
 
-       scf_printf("%s(), %d, realloc: %p, free b2: %p, size: %ld\n", __func__, __LINE__, p, p2, size);
+       printf("%s(), %d, realloc: %p, free b2: %p, size: %ld\n", __func__, __LINE__, p, p2, size);
        return p;
 }