optimized local.set followed by local.get
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 15 Aug 2020 15:42:13 +0000 (10:42 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 15 Aug 2020 15:42:13 +0000 (10:42 -0500)
onyx
src/onyxwasm.c

diff --git a/onyx b/onyx
index 77ebf6e3fbae2fecd5b18ece9fba250d61bbbb04..2a1eabd4518fcdbff5f67c3c3dbdfdedca4a9bb7 100755 (executable)
Binary files a/onyx and b/onyx differ
index 822480c5249e13b6d5e4c1737695a126c057fb7e..edffc14ab37aa310bd75254ac0e5eeff5bb4abac 100644 (file)
@@ -1085,10 +1085,12 @@ COMPILE_FUNC(struct_load, Type* type, u64 offset) {
     }
 
     u64 tmp_idx = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
-    WIL(WI_LOCAL_SET, tmp_idx);
+    WIL(WI_LOCAL_TEE, tmp_idx);
 
+    b32 first = 1;
     bh_arr_each(StructMember *, smem, type->Struct.memarr) {
-        WIL(WI_LOCAL_GET, tmp_idx);
+        if (first) first = 0;
+        else       WIL(WI_LOCAL_GET, tmp_idx);
         compile_load_instruction(mod, &code, (*smem)->type, offset + (*smem)->offset);
     }
 
@@ -1154,7 +1156,11 @@ COMPILE_FUNC(expression, AstTyped* expr) {
         case Ast_Kind_Local: {
             u64 tmp = bh_imap_get(&mod->local_map, (u64) expr);
             if (tmp & LOCAL_IS_WASM) {
-                WIL(WI_LOCAL_GET, tmp);
+                if (bh_arr_last(code).type == WI_LOCAL_SET && bh_arr_last(code).data.l == tmp) {
+                    bh_arr_last(code).type = WI_LOCAL_TEE;
+                } else {
+                    WIL(WI_LOCAL_GET, tmp);
+                }
 
             } else {
                 u64 offset = 0;