diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/example/local-graph.cpp | 118 | ||||
-rw-r--r-- | test/example/local-graph.txt | 1 |
2 files changed, 119 insertions, 0 deletions
diff --git a/test/example/local-graph.cpp b/test/example/local-graph.cpp new file mode 100644 index 000000000..0ac435883 --- /dev/null +++ b/test/example/local-graph.cpp @@ -0,0 +1,118 @@ +#include <iostream> + +#include <ir/local-graph.h> +#include <wasm-builder.h> +#include <wasm.h> + +using namespace wasm; + +int main() { + Module wasm; + Builder builder(wasm); + + { + Function foo; + foo.vars = {Type::i32}; + auto* get1 = builder.makeLocalGet(0, Type::i32); + auto* get2 = builder.makeLocalGet(0, Type::i32); + foo.body = builder.makeBlock({ + builder.makeLocalSet(0, builder.makeConst(Literal(int32_t(0)))), + // two equivalent gets, as both are preceded by the same single set + builder.makeDrop(get1), + builder.makeDrop(get2), + }); + LocalGraph graph(&foo); + assert(graph.equivalent(get1, get2)); + } + + { + Function foo; + foo.vars = {Type::i32}; + auto* get1 = builder.makeLocalGet(0, Type::i32); + auto* get2 = builder.makeLocalGet(0, Type::i32); + foo.body = builder.makeBlock({ + // two non-equivalent gets, as there is a set in between them + builder.makeDrop(get1), + builder.makeLocalSet(0, builder.makeConst(Literal(int32_t(0)))), + builder.makeDrop(get2), + }); + LocalGraph graph(&foo); + assert(!graph.equivalent(get1, get2)); + } + + { + Function foo; + foo.sig = Signature({Type::i32}, Type::none); + auto* get1 = builder.makeLocalGet(0, Type::i32); + auto* get2 = builder.makeLocalGet(0, Type::i32); + foo.body = builder.makeBlock({ + // two equivalent gets of the same parameter + builder.makeDrop(get1), + builder.makeDrop(get2), + }); + LocalGraph graph(&foo); + assert(graph.equivalent(get1, get2)); + } + + { + Function foo; + foo.sig = Signature({Type::i32}, Type::none); + auto* get1 = builder.makeLocalGet(0, Type::i32); + auto* get2 = builder.makeLocalGet(0, Type::i32); + foo.body = builder.makeBlock({ + // two non-equivalent gets of the same parameter, as there is a set + builder.makeDrop(get1), + builder.makeLocalSet(0, builder.makeConst(Literal(int32_t(0)))), + builder.makeDrop(get2), + }); + LocalGraph graph(&foo); + assert(!graph.equivalent(get1, get2)); + } + + { + Function foo; + foo.sig = Signature({Type::i32, Type::i32}, Type::none); + auto* get1 = builder.makeLocalGet(0, Type::i32); + auto* get2 = builder.makeLocalGet(1, Type::i32); + foo.body = builder.makeBlock({ + // two non-equivalent gets as they are of different parameters + builder.makeDrop(get1), + builder.makeDrop(get2), + }); + LocalGraph graph(&foo); + assert(!graph.equivalent(get1, get2)); + } + + { + Function foo; + foo.vars = {Type::i32, Type::i32}; + auto* get1 = builder.makeLocalGet(0, Type::i32); + auto* get2 = builder.makeLocalGet(1, Type::i32); + foo.body = builder.makeBlock({ + // two equivalent gets, even though they have a different index, as both + // use the zero initialized value + builder.makeDrop(get1), + builder.makeDrop(get2), + }); + LocalGraph graph(&foo); + assert(graph.equivalent(get1, get2)); + } + + { + Function foo; + foo.vars = {Type::i32, Type::f64}; + auto* get1 = builder.makeLocalGet(0, Type::i32); + auto* get2 = builder.makeLocalGet(1, Type::f64); + foo.body = builder.makeBlock({ + // two non-equivalent gets as their zero-init value is different + builder.makeDrop(get1), + builder.makeDrop(get2), + }); + LocalGraph graph(&foo); + assert(!graph.equivalent(get1, get2)); + } + + std::cout << "Success." << std::endl; + + return 0; +} diff --git a/test/example/local-graph.txt b/test/example/local-graph.txt new file mode 100644 index 000000000..a9d787cc5 --- /dev/null +++ b/test/example/local-graph.txt @@ -0,0 +1 @@ +Success. |