From 04418d5617f6ab7015c5cf7b905b4775a5219f4b Mon Sep 17 00:00:00 2001 From: "Alon Zakai (kripken)" Date: Mon, 31 Jul 2017 14:11:33 -0700 Subject: use effective shifts in more places in optimize-instructions --- src/passes/OptimizeInstructions.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/passes/OptimizeInstructions.cpp') diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 4fd7cbe8d..578a01f11 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -779,7 +779,7 @@ private: return; } else if (binary->op == ShlInt32) { if (auto* c = binary->right->dynCast()) { - seek(binary->left, mul * Pow2(c->value.geti32())); + seek(binary->left, mul * Pow2(Bits::getEffectiveShifts(c))); return; } } else if (binary->op == MulInt32) { @@ -836,7 +836,7 @@ private: } } else if (curr->op == ShlInt32) { // shifting a 0 is a 0, unless the shift has side effects - if (left && left->value.geti32() == 0 && !EffectAnalyzer(passOptions, curr->right).hasSideEffects()) { + if (left && Bits::getEffectiveShifts(left) == 0 && !EffectAnalyzer(passOptions, curr->right).hasSideEffects()) { replaceCurrent(left); return; } -- cgit v1.2.3 From 6d686bd1a5b3610ad49fd607ae5e49c70410af51 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 1 Aug 2017 15:42:24 -0700 Subject: fix off-by-one error in clz/ctz/popcount used bits computation --- src/passes/OptimizeInstructions.cpp | 4 +-- test/passes/optimize-instructions.txt | 62 +++++++++++++++++++++++++++++----- test/passes/optimize-instructions.wast | 40 ++++++++++++++++++++++ 3 files changed, 95 insertions(+), 11 deletions(-) (limited to 'src/passes/OptimizeInstructions.cpp') diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 578a01f11..f458a58b2 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -228,8 +228,8 @@ Index getMaxBits(Expression* curr, LocalInfoProvider* localInfoProvider) { } } else if (auto* unary = curr->dynCast()) { switch (unary->op) { - case ClzInt32: case CtzInt32: case PopcntInt32: return 5; - case ClzInt64: case CtzInt64: case PopcntInt64: return 6; + case ClzInt32: case CtzInt32: case PopcntInt32: return 6; + case ClzInt64: case CtzInt64: case PopcntInt64: return 7; case EqZInt32: case EqZInt64: return 1; case WrapInt64: return std::min(Index(32), getMaxBits(unary->value, localInfoProvider)); default: {} diff --git a/test/passes/optimize-instructions.txt b/test/passes/optimize-instructions.txt index 2e46379fb..55501dfb8 100644 --- a/test/passes/optimize-instructions.txt +++ b/test/passes/optimize-instructions.txt @@ -7,6 +7,7 @@ (type $5 (func (param i32))) (type $6 (func (param i32 i32) (result i32))) (type $7 (func (param i64) (result i64))) + (type $8 (func (result i64))) (memory $0 0) (export "load-off-2" (func $load-off-2)) (func $f (type $0) (param $i1 i32) (param $i2 i64) @@ -827,11 +828,14 @@ ) ) (drop - (i32.shl - (i32.clz - (i32.const 0) + (i32.shr_s + (i32.shl + (i32.clz + (i32.const 0) + ) + (i32.const 26) ) - (i32.const 2) + (i32.const 24) ) ) (drop @@ -853,13 +857,16 @@ ) ) (drop - (i32.shl - (i32.wrap/i64 - (i64.clz - (i64.const 0) + (i32.shr_s + (i32.shl + (i32.wrap/i64 + (i64.clz + (i64.const 0) + ) ) + (i32.const 25) ) - (i32.const 1) + (i32.const 24) ) ) (drop @@ -2076,4 +2083,41 @@ (func $less-shifts-than-it-seems (type $3) (param $x i32) (result i32) (i32.const 4800) ) + (func $and-popcount32 (type $2) (result i32) + (i32.and + (i32.popcnt + (i32.const -1) + ) + (i32.const 31) + ) + ) + (func $and-popcount32-big (type $2) (result i32) + (i32.popcnt + (i32.const -1) + ) + ) + (func $and-popcount64 (type $8) (result i64) + (i64.and + (i64.popcnt + (i64.const -1) + ) + (i64.const 63) + ) + ) + (func $and-popcount64-big (type $8) (result i64) + (i64.and + (i64.popcnt + (i64.const -1) + ) + (i64.const 127) + ) + ) + (func $and-popcount64-bigger (type $8) (result i64) + (i64.and + (i64.popcnt + (i64.const -1) + ) + (i64.const 255) + ) + ) ) diff --git a/test/passes/optimize-instructions.wast b/test/passes/optimize-instructions.wast index 517c74ea6..c26aad196 100644 --- a/test/passes/optimize-instructions.wast +++ b/test/passes/optimize-instructions.wast @@ -2526,4 +2526,44 @@ ) ) ) + (func $and-popcount32 (result i32) + (i32.and + (i32.popcnt + (i32.const -1) + ) + (i32.const 31) + ) + ) + (func $and-popcount32-big (result i32) + (i32.and + (i32.popcnt + (i32.const -1) + ) + (i32.const 63) + ) + ) + (func $and-popcount64 (result i64) ;; these are TODOs + (i64.and + (i64.popcnt + (i64.const -1) + ) + (i64.const 63) + ) + ) + (func $and-popcount64-big (result i64) + (i64.and + (i64.popcnt + (i64.const -1) + ) + (i64.const 127) + ) + ) + (func $and-popcount64-bigger (result i64) + (i64.and + (i64.popcnt + (i64.const -1) + ) + (i64.const 255) + ) + ) ) -- cgit v1.2.3