19 m(Type, type) m(Univ, univ) m(UMax, umax) m(UInc, uinc) \
20 m(Pi, pi) m(Lam, lam) m(App, app) \
21 m(Sigma, sigma) m(Tuple, tuple) m(Extract, extract) m(Insert, insert) \
22 m(Arr, arr) m(Pack, pack) \
23 m(Join, join) m(Vel, vel) m(Test, test) m(Top, top) \
24 m(Meet, meet) m(Ac, ac ) m(Pick, pick) m(Bot, bot) \
28 m(Nat, nat) m(Idx, idx) \
32 m(Singleton, singleton)
39#define CODE(node, name) node,
43#define CODE(node, name) +size_t(1)
94 operator const Def*()
const {
return refer(def_); }
95 explicit operator bool()
const {
return def_; }
99 const Def* def_ =
nullptr;
110 static constexpr size_t Type = -1_s;
117 size_t index()
const {
return index_; }
119 operator const Def*()
const {
return def_; }
121 bool operator==(
Use other)
const {
return this->def_ == other.def_ && this->index_ == other.index_; }
136using Uses = absl::flat_hash_set<Use, UseHash, UseEq>;
146using fe::operator<=>;
152enum class Dep :
unsigned {
164template<>
struct fe::is_bit_enum<
mim::
Dep> : std::true_type {};
170#define MIM_PROJ(NAME, CONST) \
171 nat_t num_##NAME##s() CONST { return ((const Def*)NAME())->num_projs(); } \
172 nat_t num_t##NAME##s() CONST { return ((const Def*)NAME())->num_tprojs(); } \
173 Ref NAME(nat_t a, nat_t i) CONST { return ((const Def*)NAME())->proj(a, i); } \
174 Ref NAME(nat_t i) CONST { return ((const Def*)NAME())->proj(i); } \
175 Ref t##NAME(nat_t i) CONST { return ((const Def*)NAME())->tproj(i); } \
176 template<nat_t A = std::dynamic_extent, class F> auto NAME##s(F f) CONST { \
177 return ((const Def*)NAME())->projs<A, F>(f); \
179 template<class F> auto t##NAME##s(F f) CONST { return ((const Def*)NAME())->tprojs<F>(f); } \
180 template<nat_t A = std::dynamic_extent> auto NAME##s() CONST { return ((const Def*)NAME())->projs<A>(); } \
181 auto t##NAME##s() CONST { return ((const Def*)NAME())->tprojs(); } \
182 template<class F> auto NAME##s(nat_t a, F f) CONST { return ((const Def*)NAME())->projs<F>(a, f); } \
183 auto NAME##s(nat_t a) CONST { return ((const Def*)NAME())->projs(a); }
186template<
class P,
class D = Def>
class Setters {
188 P* super() {
return static_cast<P*
>(
this); }
189 const P* super()
const {
return static_cast<const P*
>(
this); }
193 template<
bool Ow = false>
const P*
set(Loc l )
const { super()->D::template
set<Ow>(l);
return super(); }
194 template<
bool Ow = false> P*
set(Loc l ) { super()->D::template
set<Ow>(l);
return super(); }
195 template<
bool Ow = false>
const P*
set( Sym s )
const { super()->D::template
set<Ow>(s);
return super(); }
196 template<
bool Ow = false> P*
set( Sym s ) { super()->D::template
set<Ow>(s);
return super(); }
197 template<
bool Ow = false>
const P*
set( std::string s)
const { super()->D::template
set<Ow>(std::move(s));
return super(); }
198 template<
bool Ow = false> P*
set( std::string s) { super()->D::template
set<Ow>(std::move(s));
return super(); }
199 template<
bool Ow = false>
const P*
set(Loc l, Sym s )
const { super()->D::template
set<Ow>(l, s);
return super(); }
200 template<
bool Ow = false> P*
set(Loc l, Sym s ) { super()->D::template
set<Ow>(l, s);
return super(); }
201 template<
bool Ow = false>
const P*
set(Loc l, std::string s)
const { super()->D::template
set<Ow>(l, std::move(s));
return super(); }
202 template<
bool Ow = false> P*
set(Loc l, std::string s) { super()->D::template
set<Ow>(l, std::move(s));
return super(); }
203 template<
bool Ow = false>
const P*
set(
Dbg d )
const { super()->D::template
set<Ow>(d);
return super(); }
204 template<
bool Ow = false> P*
set(
Dbg d ) { super()->D::template
set<Ow>(d);
return super(); }
223class Def :
public fe::RuntimeCast<Def> {
225 Def& operator=(
const Def&) =
delete;
261 assert(a.has_value());
335 unsigned dep()
const {
return dep_; }
368 using R = std::decay_t<
decltype(f(
this))>;
369 if constexpr (A == -1_n) {
373 std::array<R, A> array;
374 for (
nat_t i = 0; i != A; ++i) array[i] = f(
proj(A, i));
382 using R = std::decay_t<
decltype(f(
this))>;
386 return projs<A>([](
const Def* def) {
return def; });
389 return tprojs([](
const Def* def) {
return def; });
392 return projs(a, [](
const Def* def) {
return def; });
447 template<
class T = Def,
bool invert = false> T*
isa_mut()
const {
448 if constexpr (std::is_same<T, Def>::value)
449 return mut_ ^ invert ?
const_cast<Def*
>(
this) :
nullptr;
451 return mut_ ^ invert ?
const_cast<Def*
>(
this)->
template isa<T>() :
nullptr;
455 template<
class T = Def,
bool invert = false> T*
as_mut()
const {
456 assert(mut_ ^ invert);
457 if constexpr (std::is_same<T, Def>::value)
458 return const_cast<Def*
>(
this);
460 return const_cast<Def*
>(
this)->
template as<T>();
480 template<
bool Ow = false>
const Def*
set( std::string s)
const {
set(
sym(std::move(s)));
return this; }
481 template<
bool Ow = false>
Def*
set( std::string s) {
set(
sym(std::move(s)));
return this; }
482 template<
bool Ow = false>
const Def*
set(Loc l, Sym s )
const {
set(l);
set(s);
return this; }
483 template<
bool Ow = false>
Def*
set(Loc l, Sym s ) {
set(l);
set(s);
return this; }
484 template<
bool Ow = false>
const Def*
set(Loc l, std::string s)
const {
set(l);
set(
sym(std::move(s)));
return this; }
485 template<
bool Ow = false>
Def*
set(Loc l, std::string s) {
set(l);
set(
sym(std::move(s)));
return this; }
486 template<
bool Ow = false>
const Def*
set(
Dbg d)
const {
set(d.loc(), d.sym());
return this; }
487 template<
bool Ow = false>
Def*
set(
Dbg d) {
set(d.loc(), d.sym());
return this; }
499 const Def*
debug_suffix(std::string)
const {
return this; }
534 void dump(
int max)
const;
535 void write(
int max)
const;
536 void write(
int max,
const char* file)
const;
537 std::ostream&
stream(std::ostream&,
int max)
const;
544 void dot(std::ostream& os, uint32_t max = 0xFFFFFF,
bool types =
false)
const;
546 void dot(
const char* file =
nullptr, uint32_t max = 0xFFFFFF,
bool types =
false)
const;
547 void dot(
const std::string& file, uint32_t max = 0xFFFFFF,
bool types =
false)
const {
548 return dot(file.c_str(), max, types);
556 Sym
sym(
const char*)
const;
557 Sym
sym(std::string_view)
const;
558 Sym
sym(std::string)
const;
569 const Def** ops_ptr()
const {
570 return reinterpret_cast<const Def**
>(
reinterpret_cast<char*
>(
const_cast<Def*
>(
this + 1)));
573 bool equal(
const Def* other)
const;
586 mutable World* world_;
603 union LocalOrFreeVars {
610 union LocalOrConsumerMuts {
611 LocalOrConsumerMuts() {}
631using DefDef = std::tuple<const Def*, const Def*>;
646template<
class To>
using DefDefMap = absl::flat_hash_map<DefDef, To, DefDefHash, DefDefEq>;
647using DefDefSet = absl::flat_hash_set<DefDef, DefDefHash, DefDefEq>;
751 template<
class T = flags_t> T
get()
const {
752 static_assert(
sizeof(T) <= 8);
763 template<
class T = nat_t>
static std::optional<T>
isa(
Ref def) {
765 if (
auto lit = def->isa<
Lit>())
return lit->get<T>();
768 template<
class T = nat_t>
static T
as(
Ref def) {
return def->as<
Lit>()->
get<T>(); }
bool is_set() const
Yields true if empty or the last op is set.
Def * set(size_t i, const Def *def)
Successively set from left to right.
const Def * set(Dbg d) const
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
const Uses & uses() const
void dot(std::ostream &os, uint32_t max=0xFFFFFF, bool types=false) const
T * as_mut() const
Asserts that this is a mutable, casts constness away and performs a static_cast to T.
const Var * has_var() const
As above if this is a mutable.
Ref var()
Not necessarily a Var: E.g., if the return type is [], this will yield ().
const T * isa_imm(R(T::*f)() const) const
const Def * op(size_t i) const
bool has_dep(Dep d) const
nat_t num_tprojs() const
As above but yields 1, if Flags::scalarize_threshold is exceeded.
nat_t as_lit_arity() const
bool has_dep(unsigned u) const
const Def * refine(size_t i, const Def *new_op) const
Def * set_type(const Def *)
void dot(const std::string &file, uint32_t max=0xFFFFFF, bool types=false) const
std::string_view node_name() const
Defs extended_ops() const
auto projs(nat_t a, F f) const
const Def * set(std::string s) const
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
Def * stub(World &w, Ref type)
auto projs(nat_t a) const
size_t num_extended_ops() const
bool is_term() const
Yields true if this:T and T:(Type 0).
const Def * debug_prefix(std::string) const
DefVec reduce(const Def *arg) const
Rewrites Def::ops by substituting this mutable's Var with arg.
void transfer_external(Def *to)
const Def * unfold_type() const
Yields the type of this Def and builds a new Type (UInc n) if necessary.
const Def * proj(nat_t i) const
As above but takes Def::num_projs as arity.
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == -1_n) or std::array (otherwise).
virtual Def * stub_(World &, Ref)
bool is_open() const
Has free_vars()?
virtual const Def * immutabilize()
Tries to make an immutable from a mutable.
Ref rebuild(Ref type, Defs ops) const
void update()
Resolves Infers of this Def's type.
const Def * debug_suffix(std::string) const
const Def * set(Sym s) const
const Def * set(Loc l, Sym s) const
const Def * set(Loc l) const
void write(int max) const
Def * set(Loc l, std::string s)
virtual Ref rebuild_(World &w, Ref type, Defs ops) const =0
std::optional< nat_t > isa_lit_arity() const
std::ostream & stream(std::ostream &, int max) const
friend void swap(World &, World &) noexcept
Ref rebuild(World &w, Ref type, Defs ops) const
Def::rebuilds this Def while using new_op as substitute for its i'th Def::op.
Def * unset()
Unsets all Def::ops; works even, if not set at all or partially.
std::string unique_name() const
name + "_" + Def::gid
const Def * set(Loc l, std::string s) const
const T * isa_imm() const
bool is_closed() const
Has no free_vars()?
Def * reset(size_t i, const Def *def)
Successively reset from left to right.
const Def * extended_op(size_t i) const
const Def * partial_op(size_t i) const
const Def * tproj(nat_t i) const
As above but takes Def::num_tprojs.
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
size_t num_partial_ops() const
void set(const Def *init)
const Def * alloced_type() const
Ref rebuild_(World &, Ref, Defs) const override
Global * stub_(World &, Ref) override
static constexpr auto Node
A built-in constant of type Nat -> *.
static Ref size(Ref def)
Checks if def is a Idx s and returns s or nullptr otherwise.
static constexpr auto Node
static constexpr nat_t size2bitwidth(nat_t n)
static constexpr nat_t bitwidth2size(nat_t n)
Ref rebuild_(World &, Ref, Defs) const override
This node is a hole in the IR that is inferred by its context later on.
Ref rebuild_(World &, Ref, Defs) const override
static std::optional< T > isa(Ref def)
static constexpr auto Node
Ref rebuild_(World &, Ref, Defs) const override
static constexpr auto Node
Ref rebuild_(World &, Ref, Defs) const override
static constexpr auto Node
u32 pass() const
IPass::index within PassMan.
Helper class to retrieve Infer::arg if present.
const Def * operator*() const
static const Def * refer(const Def *def)
Retrieves Infer::arg from def.
const Def * operator->() const
CRTP-based Mixin to declare setters for Def::loc & Def::name using a covariant return type.
const P * set(Sym s) const
const P * set(Loc l, Sym s) const
const P * set(Loc l) const
const P * set(Dbg d) const
const P * set(Loc l, std::string s) const
P * set(Loc l, std::string s)
const P * set(std::string s) const
This is a thin wrapper for std::span<T, N> with the following additional features:
Ref rebuild_(World &, Ref, Defs) const override
static constexpr auto Node
const Def * level() const
static constexpr auto Node
Ref rebuild_(World &, Ref, Defs) const override
static constexpr auto Node
Ref rebuild_(World &, Ref, Defs) const override
static constexpr auto Node
Ref rebuild_(World &, Ref, Defs) const override
Use(const Def *def, size_t index)
const Def * operator->() const
bool operator==(Use other) const
static constexpr auto Node
Ref rebuild_(World &, Ref, Defs) const override
The World represents the whole program and manages creation of MimIR nodes (Defs).
#define MIM_PROJ(NAME, CONST)
Use as mixin to wrap all kind of Def::proj and Def::projs variants.
DefMap< const Def * > Def2Def
absl::flat_hash_map< DefDef, To, DefDefHash, DefDefEq > DefDefMap
D bitcast(const S &src)
A bitcast from src of type S to D.
hash_t hash_combine(hash_t seed, T v)
Returns a new hash by combining the hash seed with val.
std::tuple< const Def *, const Def * > DefDef
absl::flat_hash_set< K, GIDHash< K >, GIDEq< K > > GIDSet
GIDSet< const Var * > VarSet
GIDMap< const Var *, To > VarMap
GIDMap< const Def *, To > DefMap
hash_t murmur3(hash_t h, uint32_t key)
absl::flat_hash_map< K, V, GIDHash< K >, GIDEq< K > > GIDMap
GIDSet< const Def * > DefSet
absl::flat_hash_set< Use, UseHash, UseEq > Uses
PooledSet< const Var * > Vars
absl::flat_hash_set< DefDef, DefDefHash, DefDefEq > DefDefSet
hash_t hash(const char *)
hash_t murmur3_finalize(hash_t h, hash_t len)
Ref(*)(Ref, Ref, Ref) NormalizeFn
std::ostream & operator<<(std::ostream &, const CFNode *)
Vector(I, I, A=A()) -> Vector< typename std::iterator_traits< I >::value_type, Default_Inlined_Size< typename std::iterator_traits< I >::value_type >, A >
GIDMap< Def *, To > MutMap
VarMap< const Var * > Var2Var
constexpr decltype(auto) get(mim::Span< T, N > span)
bool operator()(DefDef p1, DefDef p2) const
hash_t operator()(DefDef pair) const
bool operator()(Use u1, Use u2) const
hash_t operator()(Use) const