diff options
author | Alon Zakai <azakai@google.com> | 2021-08-24 13:41:39 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-24 13:41:39 -0700 |
commit | 0a6de1700938c419d0e549232c35a56c5718be2c (patch) | |
tree | a1be1439d2e370eac3bf30a9e3772deb524cd3ab /src/ir/ordering.h | |
parent | a2323f2cfd90089c54100ab98c439b9438cc4dc1 (diff) | |
download | binaryen-0a6de1700938c419d0e549232c35a56c5718be2c.tar.gz binaryen-0a6de1700938c419d0e549232c35a56c5718be2c.tar.bz2 binaryen-0a6de1700938c419d0e549232c35a56c5718be2c.zip |
OptimizeInstructions: Handle trivial ref.cast and ref.test (#4097)
If the types are completely incompatible, we know the cast will fail. However,
ref.cast does allow a null to pass through, which makes it a little more
complicated.
Diffstat (limited to 'src/ir/ordering.h')
-rw-r--r-- | src/ir/ordering.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/ir/ordering.h b/src/ir/ordering.h new file mode 100644 index 000000000..8e178dd35 --- /dev/null +++ b/src/ir/ordering.h @@ -0,0 +1,59 @@ +/* + * Copyright 2021 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_ir_reorderer_h +#define wasm_ir_reorderer_h + +#include <ir/effects.h> +#include <wasm-builder.h> + +namespace wasm { + +// +// Given two expressions that appear in a specific order - first and then +// second - this helper can create a sequence in which we return the value of +// the first. If the two expressions can be reordered, this simply returns +// +// (second, first) +// +// If side effects prevent that, it will use a local to save the value of the +// first, and return it at the end, +// +// (temp = first, second, temp) +// +Expression* getResultOfFirst(Expression* first, + Expression* second, + Function* func, + Module* wasm, + const PassOptions& passOptions) { + assert(first->type.isConcrete()); + + Builder builder(*wasm); + + if (EffectAnalyzer::canReorder(passOptions, wasm->features, first, second)) { + return builder.makeSequence(second, first); + } + + auto type = first->type; + auto index = Builder::addVar(func, type); + return builder.makeBlock({builder.makeLocalSet(index, first), + second, + builder.makeLocalGet(index, type)}); +} + +} // namespace wasm + +#endif // wasm_ir_reorderer_h |