summaryrefslogtreecommitdiff
path: root/src/passes/PostEmscripten.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes/PostEmscripten.cpp')
-rw-r--r--src/passes/PostEmscripten.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/passes/PostEmscripten.cpp b/src/passes/PostEmscripten.cpp
index 51b8c2ec1..937a70e36 100644
--- a/src/passes/PostEmscripten.cpp
+++ b/src/passes/PostEmscripten.cpp
@@ -21,6 +21,9 @@
#include <wasm.h>
#include <pass.h>
+#include <wasm-builder.h>
+#include <ast/localize.h>
+#include <asmjs/shared-constants.h>
namespace wasm {
@@ -88,6 +91,26 @@ struct PostEmscripten : public WalkerPass<PostWalker<PostEmscripten>> {
void visitStore(Store* curr) {
optimizeMemoryAccess(curr->ptr, curr->offset);
}
+
+ void visitCallImport(CallImport* curr) {
+ // special asm.js imports can be optimized
+ auto* import = getModule()->getImport(curr->target);
+ if (import->module == GLOBAL_MATH) {
+ if (import->base == POW) {
+ if (auto* exponent = curr->operands[1]->dynCast<Const>()) {
+ if (exponent->value == Literal(double(2.0))) {
+ // This is just a square operation, do a multiply
+ Localizer localizer(curr->operands[0], getFunction(), getModule());
+ Builder builder(*getModule());
+ replaceCurrent(builder.makeBinary(MulFloat64, localizer.expr, builder.makeGetLocal(localizer.index, localizer.expr->type)));
+ } else if (exponent->value == Literal(double(0.5))) {
+ // This is just a square root operation
+ replaceCurrent(Builder(*getModule()).makeUnary(SqrtFloat64, curr->operands[0]));
+ }
+ }
+ }
+ }
+ }
};
Pass *createPostEmscriptenPass() {