operand: Implement operand hashing and use hashed set for declarations

Instead of manually searching each element in the declarations vector,
use an unordered_set to emplace new declarations avoiding repetition.
This commit is contained in:
ReinUsesLisp
2019-06-08 05:23:15 -03:00
parent f7c4b07a7e
commit 1e665afa36
10 changed files with 66 additions and 13 deletions

View File

@@ -5,6 +5,7 @@
*/
#include <cassert>
#include <climits>
#include "common_types.h"
#include "literal_number.h"
@@ -47,6 +48,21 @@ bool Op::operator==(const Operand& other) const {
return false;
}
std::size_t Op::Hash() const {
std::size_t hash = Operand::Hash();
hash ^= static_cast<std::size_t>(opcode) << 20;
if (result_type) {
hash ^= result_type->Hash() << 16;
}
hash ^= static_cast<std::size_t>(id.value_or(0)) << 8;
std::size_t wrap = 32;
for (const auto operand : operands) {
wrap = (wrap + 7) % (sizeof(std::size_t) * CHAR_BIT);
hash ^= operand->Hash() << wrap;
}
return hash;
}
void Op::Write(Stream& stream) const {
stream.Write(static_cast<u16>(opcode));
stream.Write(WordCount());