summaryrefslogtreecommitdiff
path: root/src/ast
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2017-03-10 10:44:37 -0800
committerGitHub <noreply@github.com>2017-03-10 10:44:37 -0800
commitd54c03e99f9a43bde1b6cec94f05b0af412d0e4f (patch)
tree94128b81f1f1d2e9ecf83fe2e699e2528f6c49fc /src/ast
parentd79d71ac7dfe807a5e98b94c9d1f67df4da7998a (diff)
downloadbinaryen-d54c03e99f9a43bde1b6cec94f05b0af412d0e4f.tar.gz
binaryen-d54c03e99f9a43bde1b6cec94f05b0af412d0e4f.tar.bz2
binaryen-d54c03e99f9a43bde1b6cec94f05b0af412d0e4f.zip
optimize pow (#934)
* optimize pow(x,2) => x*x * optimize pow(x, 0.5) => sqrt(x)
Diffstat (limited to 'src/ast')
-rw-r--r--src/ast/localize.h47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/ast/localize.h b/src/ast/localize.h
new file mode 100644
index 000000000..9e7dd6653
--- /dev/null
+++ b/src/ast/localize.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2016 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef wasm_ast_localizer_h
+#define wasm_ast_localizer_h
+
+#include <wasm-builder.h>
+
+namespace wasm {
+
+// Make an expression available in a local. If already in one, just
+// use that local, otherwise use a new local
+
+struct Localizer {
+ Index index;
+ Expression* expr;
+
+ Localizer(Expression* input, Function* func, Module* wasm) {
+ expr = input;
+ if (auto* get = expr->dynCast<GetLocal>()) {
+ index = get->index;
+ } else if (auto* set = expr->dynCast<SetLocal>()) {
+ index = set->index;
+ } else {
+ index = Builder::addVar(func, expr->type);
+ expr = Builder(*wasm).makeTeeLocal(index, expr);
+ }
+ }
+};
+
+} // namespace wasm
+
+#endif // wasm_ast_localizer_h
+