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 <fe/assert.h>
4
5#include "mim/world.h"
6
7namespace mim {
8
9[[nodiscard]] int commute_(const Def* a, const Def* b) {
10 auto& world = a->world();
11
12 if (a == b) return 0;
13 if (a->isa_imm() && b->isa_mut()) return -1;
14 if (a->isa_mut() && b->isa_imm()) return +1;
15
16 if (a->isa_mut() && b->isa_mut()) {
17 world.WLOG("resorting to unstable gid-based compare for commute check");
18 return a->gid() < b->gid() ? -1 : +1;
19 }
20
21 assert(a->isa_imm() && b->isa_imm());
22
23 // clang-format off
24 if (a->node() != b->node() ) return a->node() < b->node() ? -1 : +1;
25 if (a->num_ops() != b->num_ops()) return a->num_ops() < b->num_ops() ? -1 : +1;
26 if (a->flags() != b->flags() ) return a->flags() < b->flags() ? -1 : +1;
27 // clang-format on
28
29 if (auto var1 = a->isa<Var>()) {
30 auto var2 = b->as<Var>();
31 world.WLOG("resorting to unstable gid-based compare for commute check");
32 return var1->gid() < var2->gid() ? -1 : +1;
33 }
34
35 for (size_t i = 0, e = a->num_ops(); i != e; ++i)
36 if (int cmp = commute_(a->op(i), b->op(i)); cmp != 0) return cmp;
37
38 fe::unreachable();
39}
40
41} // 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:425
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:419
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:9