From 83566a5512725f1940aa95f2c439fed016ec1ef5 Mon Sep 17 00:00:00 2001 From: dcode Date: Mon, 18 Mar 2019 18:27:48 +0100 Subject: [PATCH] eliminate unnecessary tee+drop in unchecked set --- src/builtins.ts | 7 +-- tests/compiler/std/string.optimized.wat | 61 +++++++++++++------------ tests/compiler/std/string.untouched.wat | 25 ++++------ 3 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/builtins.ts b/src/builtins.ts index e5a6253c..ce6796e2 100644 --- a/src/builtins.ts +++ b/src/builtins.ts @@ -2331,7 +2331,8 @@ export function compileCall( let flow = compiler.currentFlow; let alreadyUnchecked = flow.is(FlowFlags.UNCHECKED_CONTEXT); flow.set(FlowFlags.UNCHECKED_CONTEXT); - let expr = compiler.compileExpressionRetainType(operands[0], contextualType, WrapMode.NONE); + // eliminate unnecessary tees by preferring contextualType(=void): + let expr = compiler.compileExpression(operands[0], contextualType, ConversionKind.NONE, WrapMode.NONE); if (!alreadyUnchecked) flow.unset(FlowFlags.UNCHECKED_CONTEXT); return expr; } @@ -4218,10 +4219,6 @@ export function compileBuiltinArraySetWithValue( valueExpr: ExpressionRef, tee: bool ): ExpressionRef { - - // TODO: check offset - - var program = compiler.program; var type = assert(compiler.program.determineBuiltinArrayType(target)); var module = compiler.module; diff --git a/tests/compiler/std/string.optimized.wat b/tests/compiler/std/string.optimized.wat index fa751ca7..8d22b14d 100644 --- a/tests/compiler/std/string.optimized.wat +++ b/tests/compiler/std/string.optimized.wat @@ -3335,10 +3335,10 @@ if i32.const 1 call $~lib/array/Array#constructor - local.tee $7 + local.tee $3 local.get $0 call $~lib/array/Array#__set - local.get $7 + local.get $3 return end local.get $0 @@ -3347,7 +3347,7 @@ i32.load offset=4 i32.const 1 i32.shr_u - local.set $5 + local.set $6 i32.const 2147483647 local.get $2 local.get $2 @@ -3365,40 +3365,41 @@ local.set $9 local.get $3 if - local.get $5 + local.get $6 i32.eqz if i32.const 1 call $~lib/array/Array#constructor - local.tee $3 + local.tee $5 i32.load offset=4 i32.const 312 i32.store - local.get $3 + local.get $5 return end else - local.get $5 + local.get $6 i32.eqz if i32.const 0 call $~lib/array/Array#constructor return end - local.get $5 + local.get $6 + local.tee $3 local.get $2 - local.get $5 + local.get $3 local.get $2 i32.lt_s select - local.tee $5 + local.tee $6 call $~lib/array/Array#constructor - local.tee $7 + local.tee $3 i32.load offset=4 - local.set $3 + local.set $5 loop $repeat|0 local.get $4 - local.get $5 + local.get $6 i32.lt_s if i32.const 2 @@ -3414,7 +3415,7 @@ local.get $4 i32.const 2 i32.shl - local.get $3 + local.get $5 i32.add local.get $1 i32.const 1 @@ -3427,12 +3428,12 @@ br $repeat|0 end end - local.get $7 + local.get $3 return end i32.const 0 call $~lib/array/Array#constructor - local.set $6 + local.set $7 loop $continue|1 local.get $0 local.get $1 @@ -3445,30 +3446,30 @@ local.get $8 local.get $4 i32.sub - local.tee $3 + local.tee $5 i32.const 0 i32.gt_s if - local.get $3 + local.get $5 i32.const 1 i32.shl - local.tee $3 + local.tee $5 call $~lib/runtime/doAllocate - local.tee $7 + local.tee $3 local.get $4 i32.const 1 i32.shl local.get $0 i32.add - local.get $3 + local.get $5 call $~lib/memory/memory.copy - local.get $6 local.get $7 + local.get $3 i32.const 1 call $~lib/runtime/doRegister call $~lib/array/Array#push else - local.get $6 + local.get $7 i32.const 312 call $~lib/array/Array#push end @@ -3479,7 +3480,7 @@ local.get $2 i32.eq if - local.get $6 + local.get $7 return end local.get $8 @@ -3507,7 +3508,7 @@ local.get $3 return end - local.get $5 + local.get $6 local.get $4 i32.sub local.tee $1 @@ -3519,7 +3520,7 @@ i32.shl local.tee $1 call $~lib/runtime/doAllocate - local.tee $3 + local.tee $5 local.get $4 i32.const 1 i32.shl @@ -3527,17 +3528,17 @@ i32.add local.get $1 call $~lib/memory/memory.copy - local.get $6 - local.get $3 + local.get $7 + local.get $5 i32.const 1 call $~lib/runtime/doRegister call $~lib/array/Array#push else - local.get $6 + local.get $7 i32.const 312 call $~lib/array/Array#push end - local.get $6 + local.get $7 ) (func $~lib/array/Array#__get (; 39 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32) local.get $1 diff --git a/tests/compiler/std/string.untouched.wat b/tests/compiler/std/string.untouched.wat index 6e5eeebe..d67287ed 100644 --- a/tests/compiler/std/string.untouched.wat +++ b/tests/compiler/std/string.untouched.wat @@ -4336,23 +4336,18 @@ i32.const 1 call $~lib/array/Array#constructor local.set $6 - block (result i32) - local.get $6 - local.tee $3 - i32.load offset=4 - block $~lib/runtime/LINK>|inlined.0 (result i32) - local.get $0 - local.set $7 - local.get $7 - local.get $3 - call $~lib/runtime/doLink - local.get $7 - end - local.tee $3 - i32.store + local.get $6 + local.tee $3 + i32.load offset=4 + block $~lib/runtime/LINK>|inlined.0 (result i32) + local.get $0 + local.set $7 + local.get $7 local.get $3 + call $~lib/runtime/doLink + local.get $7 end - drop + i32.store local.get $6 return end