MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
legalize.h
Go to the documentation of this file.
1#pragma once
2
3#include <mim/phase.h>
4
5namespace mim::plug::mach {
6
7/// Turns every `Nat` value and every pointer into an `I64`, so that instruction selection only sees register types.
8/// `Nat` literals stay `Nat` as type-level arguments and become `I64` only as values.
9/// `mem.lea` becomes a `core.wrap.add` of the byte offset by the layout of `core.trait`, `mem.load`/`mem.store` become
10/// `mach.load`/`mach.store`, `mem.mslot` becomes `mach.slot`, and the `Nat` operations become their `I64` counterparts.
11class Legalize : public RWPhase {
12public:
13 using RWPhase::RWPhase;
14
15private:
16 /// Are the rewritten @p args no type-level `Nat`s, such as a size bound by a Pi?
17 bool is_value(const Def* args);
18 /// The rewritten @p def with a `Nat` literal as `I64`.
19 const Def* val(const Def* def);
20 /// The rewritten array index @p i at register size.
21 const Def* index(const Def* i);
22
23 /// Short-circuits a branch on `a ∨ b` or `a ∧ b` into branches on `a` and `b`, so isel branches on their flags.
24 const Def* split(const Def* cond, const Def* ff, const Def* tt, const Def* arg);
25
26 std::array<const Def*, 2> rewrite_filter_body(Lam*, Lam*) final;
27 const Def* rewrite_imm_Nat(const Nat*) final { return new_world().type_i64(); }
28 const Def* rewrite_imm_Lit(const Lit*) final;
29 const Def* rewrite_imm_App(const App*) final;
30 const Def* rewrite_imm_Extract(const Extract*) final;
31 const Def* coerce_leaf(const Def*, const Def*) final;
32};
33
34} // namespace mim::plug::mach
Base class for all Defs.
Definition def.h:285
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:360
A function.
Definition lam.h:91
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition phase.cpp:23
World & new_world()
Create new Defs into this.
Definition phase.h:418
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition phase.h:360
const Def * type_i64()
Definition world.h:657
Turns every Nat value and every pointer into an I64, so that instruction selection only sees register...
Definition legalize.h:11
std::array< const Def *, 2 > rewrite_filter_body(Lam *, Lam *) final
The rewritten filter and body of old_lam for its stub new_lam, before the body is coerce()d.
Definition legalize.cpp:62
const Def * rewrite_imm_Nat(const Nat *) final
Definition legalize.h:27
const Def * rewrite_imm_Lit(const Lit *) final
Definition legalize.cpp:90
const Def * rewrite_imm_Extract(const Extract *) final
Definition legalize.cpp:101
const Def * rewrite_imm_App(const App *) final
Definition legalize.cpp:110
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition phase.h:360
const Def * coerce_leaf(const Def *, const Def *) final
Converts the new leaf new_val into the new new_type, whose representation this phase changed.
Definition legalize.cpp:95
The mach Plugin
Definition asm.h:15