#ifndef wasm_ir_properties_h
#define wasm_ir_properties_h
#include "ir/bits.h"
#include "ir/effects.h"
#include "wasm.h"
namespace wasm {
namespace Properties {
inline bool emitsBoolean(Expression* curr) {
if (auto* unary = curr->dynCast<Unary>()) {
return unary->isRelational();
} else if (auto* binary = curr->dynCast<Binary>()) {
return binary->isRelational();
}
return false;
}
inline bool isSymmetric(Binary* binary) {
switch (binary->op) {
case AddInt32:
case MulInt32:
case AndInt32:
case OrInt32:
case XorInt32:
case EqInt32:
case NeInt32:
case AddInt64:
case MulInt64:
case AndInt64:
case OrInt64:
case XorInt64:
case EqInt64:
case NeInt64:
case EqFloat32:
case NeFloat32:
case EqFloat64:
case NeFloat64:
return true;
default:
return false;
}
}
inline bool isControlFlowStructure(Expression* curr) {
return curr->is<Block>() || curr->is<If>() || curr->is<Loop>() ||
curr->is<Try>();
}
inline bool isNamedControlFlow(Expression* curr) {
if (auto* block = curr->dynCast<Block>()) {
return block->name.is();
} else if (auto* loop = curr->dynCast<Loop>()) {
return loop->name.is();
}
return false;
}
inline bool isSingleConstantExpression(const Expression* curr) {
return curr->is<Const>() || curr->is<RefNull>() || curr->is<RefFunc>() ||
(curr->is<I31New>() && curr->cast<I31New>()->value->is<Const>());
}
inline bool isConstantExpression(const Expression* curr) {
if (isSingleConstantExpression(curr)) {
return true;
}
if (auto* tuple = curr->dynCast<TupleMake>()) {
for (auto* op : tuple->operands) {
if (!isSingleConstantExpression(op)) {
return false;
}
}
return true;
}
return false;
}
inline Literal getLiteral(const Expression* curr) {
if (auto* c = curr->dynCast<Const>()) {
return c->value;
} else if (auto* n = curr->dynCast<RefNull>()) {
return Literal(n->type);
} else if (auto* r = curr->dynCast<RefFunc>()) {
return Literal(r->func);
} else if (auto* i = curr->dynCast<I31New>()) {
if (auto* c = i->value->dynCast<Const>()) {
return Literal::makeI31(c->value.geti32());
}
}
WASM_UNREACHABLE("non-constant expression");
}
inline Literals getLiterals(const Expression* curr) {
if (isSingleConstantExpression(curr)) {
return {getLiteral(curr)};
} else if (auto* tuple = curr->dynCast<TupleMake>()) {
Literals literals;
for (auto* op : tuple->operands) {
literals.push_back(getLiteral(op));
}
return literals;
} else {
WASM_UNREACHABLE("non-constant expression");
}
}
inline Expression* getSignExtValue(Expression* curr) {
if (auto* outer = curr->dynCast<Binary>()) {
if (outer->op == ShrSInt32) {
if (auto* outerConst = outer->right->dynCast<Const>()) {
if (outerConst->value.geti32() != 0) {
if (auto* inner = outer->left->dynCast<Binary>()) {
if (inner->op == ShlInt32) {
if (auto* innerConst = inner->right->dynCast<Const>()) {
if (outerConst->value == innerConst->value) {
return inner->left;
}
}
}
}
}
}
}
}
return nullptr;
}
inline Index getSignExtBits(Expression* curr) {
return 32 - Bits::getEffectiveShifts(curr->cast<Binary>()->right);
}
inline Expression* getAlmostSignExt(Expression* curr) {
if (auto* outer = curr->dynCast<Binary>()) {
if (outer->op == ShrSInt32) {
if (auto* outerConst = outer->right->dynCast<Const>()) {
if (outerConst->value.geti32() != 0) {
if (auto* inner = outer->left->dynCast<Binary>()) {
if (inner->op == ShlInt32) {
if (auto* innerConst = inner->right->dynCast<Const>()) {
if (Bits::getEffectiveShifts(outerConst) <=
Bits::getEffectiveShifts(innerConst)) {
return inner->left;
}
}
}
}
}
}
}
}
return nullptr;
}
inline Index getAlmostSignExtBits(Expression* curr, Index& extraShifts) {
extraShifts = Bits::getEffectiveShifts(
curr->cast<Binary>()->left->cast<Binary>()->right) -
Bits::getEffectiveShifts(curr->cast<Binary>()->right);
return getSignExtBits(curr);
}
inline Expression* getZeroExtValue(Expression* curr) {
if (auto* binary = curr->dynCast<Binary>()) {
if (binary->op == AndInt32) {
if (auto* c = binary->right->dynCast<Const>()) {
if (Bits::getMaskedBits(c->value.geti32())) {
return binary->right;
}
}
}
}
return nullptr;
}
inline Index getZeroExtBits(Expression* curr) {
return Bits::getMaskedBits(
curr->cast<Binary>()->right->cast<Const>()->value.geti32());
}
inline Expression* getFallthrough(Expression* curr,
const PassOptions& passOptions,
FeatureSet features) {
if (curr->type == Type::unreachable) {
return curr;
}
if (auto* set = curr->dynCast<LocalSet>()) {
if (set->isTee()) {
return getFallthrough(set->value, passOptions, features);
}
} else if (auto* block = curr->dynCast<Block>()) {
if (!block->name.is() && block->list.size() > 0) {
return getFallthrough(block->list.back(), passOptions, features);
}
} else if (auto* loop = curr->dynCast<Loop>()) {
return getFallthrough(loop->body, passOptions, features);
} else if (auto* iff = curr->dynCast<If>()) {
if (iff->ifFalse) {
if (iff->ifTrue->type == Type::unreachable) {
return getFallthrough(iff->ifFalse, passOptions, features);
} else if (iff->ifFalse->type == Type::unreachable) {
return getFallthrough(iff->ifTrue, passOptions, features);
}
}
} else if (auto* br = curr->dynCast<Break>()) {
if (br->condition && br->value) {
return getFallthrough(br->value, passOptions, features);
}
} else if (auto* tryy = curr->dynCast<Try>()) {
if (!EffectAnalyzer(passOptions, features, tryy->body).throws) {
return getFallthrough(tryy->body, passOptions, features);
}
}
return curr;
}
inline bool isResultFallthrough(Expression* curr) {
return curr->is<LocalSet>() || curr->is<Block>() || curr->is<If>() ||
curr->is<Loop>() || curr->is<Try>() || curr->is<Select>() ||
curr->is<Break>();
}
}
}
#endif