diff options
author | Alon Zakai <azakai@google.com> | 2019-05-15 11:04:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-15 11:04:20 -0700 |
commit | e54b1d3bf6470dde708abca4442edaeb0d01d0b3 (patch) | |
tree | bc7885d28244d5313ccf6e713cd1b5b670d853d8 /src | |
parent | 104d52a0e8c2e76e9496de7dae5f3e28fdd9b49e (diff) | |
download | binaryen-e54b1d3bf6470dde708abca4442edaeb0d01d0b3.tar.gz binaryen-e54b1d3bf6470dde708abca4442edaeb0d01d0b3.tar.bz2 binaryen-e54b1d3bf6470dde708abca4442edaeb0d01d0b3.zip |
wasm2js: optimize away unneeded load coercions (#2107)
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/wasm2js.cpp | 50 | ||||
-rw-r--r-- | src/wasm2js.h | 3 |
2 files changed, 40 insertions, 13 deletions
diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp index 71e7a28fd..1c128390f 100644 --- a/src/tools/wasm2js.cpp +++ b/src/tools/wasm2js.cpp @@ -128,15 +128,13 @@ static void traversePost(Ref node, std::function<void(Ref)> visit) { traversePrePost(node, [](Ref node) {}, visit); } -#if 0 static void replaceInPlace(Ref target, Ref value) { assert(target->isArray() && value->isArray()); - target->resize(value->size()); + target->setSize(value->size()); for (size_t i = 0; i < value->size(); i++) { target[i] = value[i]; } } -#endif static void optimizeJS(Ref ast) { // Helpers @@ -272,18 +270,44 @@ static void optimizeJS(Ref ast) { node[3] = child[3]; } } - // A load into an & may allow using a simpler heap, e.g. HEAPU8[..] & 1 - // (a load of a boolean) may be HEAP8[..] & 1. The signed heaps are more - // commonly used, so it compresses better, and also they seem to have - // better performance (perhaps since HEAPU32 is at risk of not being a - // smallint). - if (node[1] == AND && isHeapAccess(node[2])) { + if (isHeapAccess(node[2])) { auto heap = getHeapFromAccess(node[2]); - if (isConstantBitwise(node, AND, 1)) { - if (heap == HEAPU8) { - setHeapOnAccess(node[2], HEAP8); + IString replacementHeap; + // We can avoid a cast of a load by using the load to do it instead. + if (isOrZero(node)) { + if (isIntegerHeap(heap)) { + replacementHeap = heap; + } + } else if (isConstantBitwise(node, TRSHIFT, 0)) { + // For signed or unsigned loads smaller than 32 bits, doing an | 0 + // was safe either way - they aren't in the range an | 0 can affect. + // For >>> 0 however, a negative value would change, so we still + // need the cast. + if (heap == HEAP32 || heap == HEAPU32) { + replacementHeap = HEAPU32; } else if (heap == HEAPU16) { - setHeapOnAccess(node[2], HEAP16); + replacementHeap = HEAPU16; + } else if (heap == HEAPU8) { + replacementHeap = HEAPU8; + } + } + if (!replacementHeap.isNull()) { + setHeapOnAccess(node[2], replacementHeap); + replaceInPlace(node, node[2]); + return; + } + // A load into an & may allow using a simpler heap, e.g. HEAPU8[..] & 1 + // (a load of a boolean) may be HEAP8[..] & 1. The signed heaps are more + // commonly used, so it compresses better, and also they seem to have + // better performance (perhaps since HEAPU32 is at risk of not being a + // smallint). + if (node[1] == AND) { + if (isConstantBitwise(node, AND, 1)) { + if (heap == HEAPU8) { + setHeapOnAccess(node[2], HEAP8); + } else if (heap == HEAPU16) { + setHeapOnAccess(node[2], HEAP16); + } } } } diff --git a/src/wasm2js.h b/src/wasm2js.h index 3b563a7db..2ed531b8d 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -439,6 +439,7 @@ void Wasm2JSBuilder::addBasics(Ref ast) { addHeap(HEAP32, INT32ARRAY); addHeap(HEAPU8, UINT8ARRAY); addHeap(HEAPU16, UINT16ARRAY); + addHeap(HEAPU32, UINT32ARRAY); addHeap(HEAPF32, FLOAT32ARRAY); addHeap(HEAPF64, FLOAT64ARRAY); // core asm.js imports @@ -1708,10 +1709,12 @@ void Wasm2JSBuilder::addMemoryGrowthFuncs(Ref ast, Module* wasm) { ValueBuilder::makeName(IString("newBuffer")))))); }; + setHeap(HEAP8, INT8ARRAY); setHeap(HEAP16, INT16ARRAY); setHeap(HEAP32, INT32ARRAY); setHeap(HEAPU8, UINT8ARRAY); setHeap(HEAPU16, UINT16ARRAY); + setHeap(HEAPU32, UINT32ARRAY); setHeap(HEAPF32, FLOAT32ARRAY); setHeap(HEAPF64, FLOAT64ARRAY); |