MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
normalize.cpp
Go to the documentation of this file.
1#include "mim/normalize.h"
2
3#include "mim/world.h"
4
5namespace mim {
6
7[[nodiscard]] int commute_(const Def* a, const Def* b) {
8 auto& world = a->world();
9
10 if (a == b) return 0;
11 if (a->isa_imm() && b->isa_mut()) return -1;
12 if (a->isa_mut() && b->isa_imm()) return +1;
13
14 if (a->isa_mut() && b->isa_mut()) {
15 world.WLOG("resorting to unstable gid-based compare for commute check");
16 return a->gid() < a->gid() ? -1 : +1;
17 }
18
19 assert(a->isa_imm() && b->isa_imm());
20
21 // clang-format off
22 if (a->node() != b->node() ) return a->node() < b->node() ? -1 : +1;
23 if (a->num_ops() != b->num_ops()) return a->num_ops() < b->num_ops() ? -1 : +1;
24 if (a->flags() != b->flags() ) return a->flags() < b->flags() ? -1 : +1;
25 // clang-format on
26
27 if (auto var1 = a->isa<Var>()) {
28 auto var2 = b->as<Var>();
29 world.WLOG("resorting to unstable gid-based compare for commute check");
30 return var1->gid() < var2->gid() ? -1 : +1;
31 }
32
33 for (size_t i = 0, e = a->num_ops(); i != e; ++i)
34 if (int cmp = commute_(a->op(i), b->op(i)); cmp != 0) return cmp;
35 assert(false);
36}
37
38} // namespace mim
Base class for all Defs.
Definition def.h:198
constexpr Node node() const noexcept
Definition def.h:221
constexpr flags_t flags() const noexcept
Definition def.h:216
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:430
const Def * op(size_t i) const noexcept
Definition def.h:264
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
Definition def.h:217
const T * isa_imm() const
Definition def.h:424
constexpr size_t num_ops() const noexcept
Definition def.h:265
Definition ast.h:14
int commute_(const Def *a, const Def *b)
Definition normalize.cpp:7