1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#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.type = Signature(Type::none, Type::none);
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.type = Signature(Type::none, Type::none);
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.type = 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.type = 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.type = 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.type = Signature(Type::none, Type::none);
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.type = Signature(Type::none, Type::none);
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;
}
|