summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/dataflow/graph.h4
-rw-r--r--src/ir/literal-utils.h2
-rw-r--r--src/ir/properties.h6
-rw-r--r--src/literal.h8
-rw-r--r--src/passes/MemoryPacking.cpp3
-rw-r--r--src/passes/OptimizeInstructions.cpp14
-rw-r--r--src/passes/Precompute.cpp3
-rw-r--r--src/passes/RedundantSetElimination.cpp2
-rw-r--r--src/tools/execution-results.h2
-rw-r--r--src/tools/fuzzing.h6
-rw-r--r--src/tools/wasm-ctor-eval.cpp4
-rw-r--r--src/tools/wasm-reduce.cpp4
-rw-r--r--src/tools/wasm-shell.cpp2
-rw-r--r--src/wasm-builder.h2
-rw-r--r--src/wasm-interpreter.h4
-rw-r--r--src/wasm.h2
-rw-r--r--src/wasm/literal.cpp38
-rw-r--r--src/wasm/wasm.cpp10
18 files changed, 73 insertions, 43 deletions
diff --git a/src/dataflow/graph.h b/src/dataflow/graph.h
index 114a38f11..4cf5022e3 100644
--- a/src/dataflow/graph.h
+++ b/src/dataflow/graph.h
@@ -157,9 +157,7 @@ struct Graph : public UnifiedExpressionVisitor<Graph, Node*> {
return ret;
}
- Node* makeZero(wasm::Type type) {
- return makeConst(Literal::makeSingleZero(type));
- }
+ Node* makeZero(wasm::Type type) { return makeConst(Literal::makeZero(type)); }
// Add a new node to our list of owned nodes.
Node* addNode(Node* node) {
diff --git a/src/ir/literal-utils.h b/src/ir/literal-utils.h
index 8b00ddcbf..3c2f36d9a 100644
--- a/src/ir/literal-utils.h
+++ b/src/ir/literal-utils.h
@@ -38,7 +38,7 @@ inline Expression* makeZero(Type type, Module& wasm) {
if (type == Type::v128) {
return builder.makeUnary(SplatVecI32x4, builder.makeConst(int32_t(0)));
}
- return builder.makeConstantExpression(Literal::makeZero(type));
+ return builder.makeConstantExpression(Literal::makeZeros(type));
}
} // namespace LiteralUtils
diff --git a/src/ir/properties.h b/src/ir/properties.h
index d0495f4a9..a2c70a205 100644
--- a/src/ir/properties.h
+++ b/src/ir/properties.h
@@ -99,7 +99,7 @@ inline bool isConstantExpression(const Expression* curr) {
return false;
}
-inline Literal getSingleLiteral(const Expression* curr) {
+inline Literal getLiteral(const Expression* curr) {
if (auto* c = curr->dynCast<Const>()) {
return c->value;
} else if (auto* n = curr->dynCast<RefNull>()) {
@@ -116,11 +116,11 @@ inline Literal getSingleLiteral(const Expression* curr) {
inline Literals getLiterals(const Expression* curr) {
if (isSingleConstantExpression(curr)) {
- return {getSingleLiteral(curr)};
+ return {getLiteral(curr)};
} else if (auto* tuple = curr->dynCast<TupleMake>()) {
Literals literals;
for (auto* op : tuple->operands) {
- literals.push_back(getSingleLiteral(op));
+ literals.push_back(getLiteral(op));
}
return literals;
} else {
diff --git a/src/literal.h b/src/literal.h
index 86e460223..4f300291e 100644
--- a/src/literal.h
+++ b/src/literal.h
@@ -145,8 +145,12 @@ public:
}
}
- static Literals makeZero(Type type);
- static Literal makeSingleZero(Type type);
+ static Literals makeZeros(Type type);
+ static Literals makeOnes(Type type);
+ static Literals makeNegOnes(Type type);
+ static Literal makeZero(Type type);
+ static Literal makeOne(Type type);
+ static Literal makeNegOne(Type type);
static Literal makeFromInt32(int32_t x, Type type) {
switch (type.getBasic()) {
case Type::i32:
diff --git a/src/passes/MemoryPacking.cpp b/src/passes/MemoryPacking.cpp
index 1018ce1c7..4a183e7d7 100644
--- a/src/passes/MemoryPacking.cpp
+++ b/src/passes/MemoryPacking.cpp
@@ -594,8 +594,7 @@ void MemoryPacking::createReplacements(Module* module,
// Create new memory.init or memory.fill
if (range.isZero) {
- Expression* value =
- builder.makeConst(Literal::makeSingleZero(Type::i32));
+ Expression* value = builder.makeConst(Literal::makeZero(Type::i32));
appendResult(builder.makeMemoryFill(dest, value, size));
} else {
size_t offsetBytes = std::max(start, range.start) - range.start;
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 85f777eb5..dbee3f457 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -287,7 +287,7 @@ struct OptimizeInstructions
if (c->value.isSignedMin()) {
c->value = Literal::makeSignedMax(c->type);
} else {
- c->value = c->value.abs().sub(Literal::makeFromInt32(1, c->type));
+ c->value = c->value.abs().sub(Literal::makeOne(c->type));
}
return curr;
}
@@ -1321,7 +1321,7 @@ private:
// Operations on one
// (signed)x % 1 ==> 0
if (matches(curr, binary(Abstract::RemS, pure(&left), ival(1)))) {
- right->value = Literal::makeSingleZero(type);
+ right->value = Literal::makeZero(type);
return right;
}
// (signed)x % C_pot != 0 ==> (x & (abs(C_pot) - 1)) != 0
@@ -1338,7 +1338,7 @@ private:
if (c->value.isSignedMin()) {
c->value = Literal::makeSignedMax(c->type);
} else {
- c->value = c->value.abs().sub(Literal::makeFromInt32(1, c->type));
+ c->value = c->value.abs().sub(Literal::makeOne(c->type));
}
return curr;
}
@@ -1382,12 +1382,12 @@ private:
}
// (signed)x % -1 ==> 0
if (matches(curr, binary(Abstract::RemS, pure(&left), ival(-1)))) {
- right->value = Literal::makeSingleZero(type);
+ right->value = Literal::makeZero(type);
return right;
}
// (unsigned)x > -1 ==> 0
if (matches(curr, binary(Abstract::GtU, pure(&left), ival(-1)))) {
- right->value = Literal::makeSingleZero(Type::i32);
+ right->value = Literal::makeZero(Type::i32);
right->type = Type::i32;
return right;
}
@@ -1406,7 +1406,7 @@ private:
}
// x * -1 ==> 0 - x
if (matches(curr, binary(Abstract::Mul, any(&left), ival(-1)))) {
- right->value = Literal::makeSingleZero(type);
+ right->value = Literal::makeZero(type);
curr->op = Abstract::getBinary(type, Abstract::Sub);
curr->left = right;
curr->right = left;
@@ -1414,7 +1414,7 @@ private:
}
// (unsigned)x <= -1 ==> 1
if (matches(curr, binary(Abstract::LeU, pure(&left), ival(-1)))) {
- right->value = Literal::makeFromInt32(1, Type::i32);
+ right->value = Literal::makeOne(Type::i32);
right->type = Type::i32;
return right;
}
diff --git a/src/passes/Precompute.cpp b/src/passes/Precompute.cpp
index d8a1fa9fc..c82a880b1 100644
--- a/src/passes/Precompute.cpp
+++ b/src/passes/Precompute.cpp
@@ -300,7 +300,8 @@ private:
Literals curr;
if (set == nullptr) {
if (getFunction()->isVar(get->index)) {
- curr = Literal::makeZero(getFunction()->getLocalType(get->index));
+ curr =
+ Literal::makeZeros(getFunction()->getLocalType(get->index));
} else {
// it's a param, so it's hopeless
values = {};
diff --git a/src/passes/RedundantSetElimination.cpp b/src/passes/RedundantSetElimination.cpp
index e36cefc9e..8b0914ecf 100644
--- a/src/passes/RedundantSetElimination.cpp
+++ b/src/passes/RedundantSetElimination.cpp
@@ -191,7 +191,7 @@ struct RedundantSetElimination
start[i] = getUniqueValue();
} else {
start[i] =
- getLiteralValue(Literal::makeZero(func->getLocalType(i)));
+ getLiteralValue(Literal::makeZeros(func->getLocalType(i)));
}
}
} else {
diff --git a/src/tools/execution-results.h b/src/tools/execution-results.h
index b63a276ae..427100628 100644
--- a/src/tools/execution-results.h
+++ b/src/tools/execution-results.h
@@ -147,7 +147,7 @@ struct ExecutionResults {
// call the method
for (const auto& param : func->sig.params) {
// zeros in arguments TODO: more?
- arguments.push_back(Literal::makeSingleZero(param));
+ arguments.push_back(Literal::makeZero(param));
}
return instance.callFunction(func->name, arguments);
} catch (const TrapException&) {
diff --git a/src/tools/fuzzing.h b/src/tools/fuzzing.h
index 475747056..ec22d306a 100644
--- a/src/tools/fuzzing.h
+++ b/src/tools/fuzzing.h
@@ -1573,10 +1573,10 @@ private:
// +- 1
switch (upTo(5)) {
case 0:
- value = value.add(Literal::makeFromInt32(-1, type));
+ value = value.add(Literal::makeNegOne(type));
break;
case 1:
- value = value.add(Literal::makeFromInt32(1, type));
+ value = value.add(Literal::makeOne(type));
break;
default: {
}
@@ -1595,7 +1595,7 @@ private:
}
// Flip sign.
if (oneIn(2)) {
- value = value.mul(Literal::makeFromInt32(-1, type));
+ value = value.mul(Literal::makeNegOne(type));
}
return value;
};
diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp
index c6734fdc1..a38ca4027 100644
--- a/src/tools/wasm-ctor-eval.cpp
+++ b/src/tools/wasm-ctor-eval.cpp
@@ -193,12 +193,12 @@ struct CtorEvalExternalInterface : EvallingModuleInstance::ExternalInterface {
// fill in fake values for everything else, which is dangerous to use
ModuleUtils::iterDefinedGlobals(wasm_, [&](Global* defined) {
if (globals.find(defined->name) == globals.end()) {
- globals[defined->name] = Literal::makeZero(defined->type);
+ globals[defined->name] = Literal::makeZeros(defined->type);
}
});
ModuleUtils::iterImportedGlobals(wasm_, [&](Global* import) {
if (globals.find(import->name) == globals.end()) {
- globals[import->name] = Literal::makeZero(import->type);
+ globals[import->name] = Literal::makeZeros(import->type);
}
});
}
diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp
index 7a81007dc..bb2de0896 100644
--- a/src/tools/wasm-reduce.cpp
+++ b/src/tools/wasm-reduce.cpp
@@ -1031,14 +1031,14 @@ struct Reducer
}
if (curr->type.isTuple()) {
Expression* n =
- builder->makeConstantExpression(Literal::makeZero(curr->type));
+ builder->makeConstantExpression(Literal::makeZeros(curr->type));
return tryToReplaceCurrent(n);
}
Const* c = builder->makeConst(int32_t(0));
if (tryToReplaceCurrent(c)) {
return true;
}
- c->value = Literal::makeFromInt32(1, curr->type);
+ c->value = Literal::makeOne(curr->type);
c->type = curr->type;
return tryToReplaceCurrent(c);
}
diff --git a/src/tools/wasm-shell.cpp b/src/tools/wasm-shell.cpp
index 69670192b..c0d02ee15 100644
--- a/src/tools/wasm-shell.cpp
+++ b/src/tools/wasm-shell.cpp
@@ -74,7 +74,7 @@ struct Operation {
name = element[i++]->str();
for (size_t j = i; j < element.size(); j++) {
Expression* argument = builder.parseExpression(*element[j]);
- arguments.push_back(getSingleLiteralFromConstExpression(argument));
+ arguments.push_back(getLiteralFromConstExpression(argument));
}
}
diff --git a/src/wasm-builder.h b/src/wasm-builder.h
index 6433c1b57..97877a0a1 100644
--- a/src/wasm-builder.h
+++ b/src/wasm-builder.h
@@ -904,7 +904,7 @@ public:
// input node
template<typename T> Expression* replaceWithIdenticalType(T* curr) {
if (curr->type.isTuple()) {
- return makeConstantExpression(Literal::makeZero(curr->type));
+ return makeConstantExpression(Literal::makeZeros(curr->type));
}
Literal value;
// TODO: reuse node conditionally when possible for literals
diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h
index 1f61685b8..b683de632 100644
--- a/src/wasm-interpreter.h
+++ b/src/wasm-interpreter.h
@@ -1995,7 +1995,7 @@ private:
locals[i] = {arguments[i]};
} else {
assert(function->isVar(i));
- locals[i] = Literal::makeZero(function->getLocalType(i));
+ locals[i] = Literal::makeZeros(function->getLocalType(i));
}
}
}
@@ -2371,7 +2371,7 @@ private:
Address src = instance.getFinalAddress(
curr, flow.getSingleValue(), curr->op == Load32Zero ? 32 : 64);
auto zero =
- Literal::makeSingleZero(curr->op == Load32Zero ? Type::i32 : Type::i64);
+ Literal::makeZero(curr->op == Load32Zero ? Type::i32 : Type::i64);
if (curr->op == Load32Zero) {
auto val = Literal(instance.externalInterface->load32u(src));
return Literal(std::array<Literal, 4>{{val, zero, zero, zero}});
diff --git a/src/wasm.h b/src/wasm.h
index 024a8dd06..c75c47d1f 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -623,7 +623,7 @@ public:
const char* getExpressionName(Expression* curr);
-Literal getSingleLiteralFromConstExpression(Expression* curr);
+Literal getLiteralFromConstExpression(Expression* curr);
Literals getLiteralsFromConstExpression(Expression* curr);
typedef ArenaVector<Expression*> ExpressionList;
diff --git a/src/wasm/literal.cpp b/src/wasm/literal.cpp
index f42727aeb..360b514c5 100644
--- a/src/wasm/literal.cpp
+++ b/src/wasm/literal.cpp
@@ -127,16 +127,34 @@ Literal::Literal(const LaneArray<2>& lanes) : type(Type::v128) {
extractBytes<uint64_t, 2>(v128, lanes);
}
-Literals Literal::makeZero(Type type) {
+Literals Literal::makeZeros(Type type) {
assert(type.isConcrete());
Literals zeroes;
for (const auto& t : type) {
- zeroes.push_back(makeSingleZero(t));
+ zeroes.push_back(makeZero(t));
}
return zeroes;
}
-Literal Literal::makeSingleZero(Type type) {
+Literals Literal::makeOnes(Type type) {
+ assert(type.isConcrete());
+ Literals units;
+ for (const auto& t : type) {
+ units.push_back(makeOne(t));
+ }
+ return units;
+}
+
+Literals Literal::makeNegOnes(Type type) {
+ assert(type.isConcrete());
+ Literals units;
+ for (const auto& t : type) {
+ units.push_back(makeNegOne(t));
+ }
+ return units;
+}
+
+Literal Literal::makeZero(Type type) {
assert(type.isSingle());
if (type.isRef()) {
if (type == Type::i31ref) {
@@ -149,6 +167,16 @@ Literal Literal::makeSingleZero(Type type) {
}
}
+Literal Literal::makeOne(Type type) {
+ assert(type.isNumber());
+ return makeFromInt32(1, type);
+}
+
+Literal Literal::makeNegOne(Type type) {
+ assert(type.isNumber());
+ return makeFromInt32(-1, type);
+}
+
std::array<uint8_t, 16> Literal::getv128() const {
assert(type == Type::v128);
std::array<uint8_t, 16> ret;
@@ -1705,7 +1733,7 @@ template<int Lanes, LaneArray<Lanes> (Literal::*IntoLanes)() const>
static Literal any_true(const Literal& val) {
LaneArray<Lanes> lanes = (val.*IntoLanes)();
for (size_t i = 0; i < Lanes; ++i) {
- if (lanes[i] != Literal::makeSingleZero(lanes[i].type)) {
+ if (lanes[i] != Literal::makeZero(lanes[i].type)) {
return Literal(int32_t(1));
}
}
@@ -1716,7 +1744,7 @@ template<int Lanes, LaneArray<Lanes> (Literal::*IntoLanes)() const>
static Literal all_true(const Literal& val) {
LaneArray<Lanes> lanes = (val.*IntoLanes)();
for (size_t i = 0; i < Lanes; ++i) {
- if (lanes[i] == Literal::makeSingleZero(lanes[i].type)) {
+ if (lanes[i] == Literal::makeZero(lanes[i].type)) {
return Literal(int32_t(0));
}
}
diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp
index dce6ac7de..2052afa89 100644
--- a/src/wasm/wasm.cpp
+++ b/src/wasm/wasm.cpp
@@ -239,11 +239,11 @@ const char* getExpressionName(Expression* curr) {
WASM_UNREACHABLE("invalid expr id");
}
-Literal getSingleLiteralFromConstExpression(Expression* curr) {
- // TODO: Do we need this function given that Properties::getSingleLiteral
+Literal getLiteralFromConstExpression(Expression* curr) {
+ // TODO: Do we need this function given that Properties::getLiteral
// (currently) does the same?
assert(Properties::isConstantExpression(curr));
- return Properties::getSingleLiteral(curr);
+ return Properties::getLiteral(curr);
}
Literals getLiteralsFromConstExpression(Expression* curr) {
@@ -252,11 +252,11 @@ Literals getLiteralsFromConstExpression(Expression* curr) {
if (auto* t = curr->dynCast<TupleMake>()) {
Literals values;
for (auto* operand : t->operands) {
- values.push_back(getSingleLiteralFromConstExpression(operand));
+ values.push_back(getLiteralFromConstExpression(operand));
}
return values;
} else {
- return {getSingleLiteralFromConstExpression(curr)};
+ return {getLiteralFromConstExpression(curr)};
}
}