#ifndef _wasm_ir_hashed_h
#define _wasm_ir_hashed_h
#include "ir/utils.h"
#include "support/hash.h"
#include "wasm.h"
#include <functional>
namespace wasm {
struct FunctionHasher : public WalkerPass<PostWalker<FunctionHasher>> {
bool isFunctionParallel() override { return true; }
bool modifiesBinaryenIR() override { return false; }
struct Map : public std::map<Function*, size_t> {};
FunctionHasher(Map* output, ExpressionAnalyzer::ExprHasher customHasher)
: output(output), customHasher(customHasher) {}
FunctionHasher(Map* output)
: output(output), customHasher(ExpressionAnalyzer::nothingHasher) {}
std::unique_ptr<Pass> create() override {
return std::make_unique<FunctionHasher>(output, customHasher);
}
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) = flexibleHashFunction(func, customHasher);
}
static size_t
flexibleHashFunction(Function* func,
ExpressionAnalyzer::ExprHasher customHasher) {
auto digest = hash(func->type);
for (auto type : func->vars) {
rehash(digest, type.getID());
}
hash_combine(digest,
ExpressionAnalyzer::flexibleHash(func->body, customHasher));
return digest;
}
static size_t hashFunction(Function* func) {
return flexibleHashFunction(func, ExpressionAnalyzer::nothingHasher);
}
private:
Map* output;
ExpressionAnalyzer::ExprHasher customHasher;
};
}
#endif