#ifndef _wasm_ir_hashed_h
#define _wasm_ir_hashed_h
#include "ir/utils.h"
#include "support/hash.h"
#include "wasm.h"
namespace wasm {
struct HashedExpression {
Expression* expr;
size_t digest;
HashedExpression(Expression* expr) : expr(expr) {
if (expr) {
digest = ExpressionAnalyzer::hash(expr);
} else {
digest = hash(0);
}
}
HashedExpression(const HashedExpression& other)
: expr(other.expr), digest(other.digest) {}
};
struct FunctionHasher : public WalkerPass<PostWalker<FunctionHasher>> {
bool isFunctionParallel() override { return true; }
struct Map : public std::map<Function*, size_t> {};
FunctionHasher(Map* output) : output(output) {}
FunctionHasher* create() override { return new FunctionHasher(output); }
static Map createMap(Module* module) {
Map hashes;
for (auto& func : module->functions) {
hashes[func.get()] = hash(0);
}
return hashes;
}
void doWalkFunction(Function* func) { output->at(func) = hashFunction(func); }
static size_t hashFunction(Function* func) {
auto digest = hash(func->sig.params.getID());
rehash(digest, func->sig.results.getID());
for (auto type : func->vars) {
rehash(digest, type.getID());
}
hash_combine(digest, ExpressionAnalyzer::hash(func->body));
return digest;
}
private:
Map* output;
};
}
#endif