11bool should_flatten(
const Def* def) {
12 auto type = (def->is_term() ? def->type() : def);
14 if (
auto arr =
type->isa<
Arr>()) {
15 if (
auto a =
Lit::isa(arr->arity()); a && *a > def->world().flags().scalarize_threshold)
return false;
21bool mut_val_or_typ(
const Def* def) {
22 auto typ = def->is_term() ? def->type() : def;
23 return typ->isa_mut();
27 if (!defs.empty() && defs[0]->type() == type)
return defs[j++];
29 flatten_muts == mut_val_or_typ(type) && a && *a != 1 && a <= type->world().flags().scalarize_threshold) {
30 auto& world =
type->world();
31 auto ops =
DefVec(*a, [&](
size_t i) {
return unflatten(defs,
type->proj(*a, i), j, flatten_muts); });
32 return world.tuple(type, ops);
46 if (
auto arr =
type()->isa<Arr>())
return arr->
arity();
68 if (
auto app = def->isa<
App>()) {
84 if (def ==
nullptr)
return {};
86 auto& w = def->
world();
87 auto res = std::string();
89 for (
size_t i = 0; i != *n; ++i) {
90 auto elem = def->
proj(*n, i);
91 if (elem->type() == w.type_i8()) {
106 if (
auto a =
Lit::isa(def->
arity()); a && *a != 1 && should_flatten(def) && flatten_muts == mut_val_or_typ(def)) {
108 for (
size_t i = 0; i != *a; ++i)
112 ops.emplace_back(def);
118 if (!should_flatten(def))
return def;
126 auto def =
unflatten(defs, type, j, flatten_muts);
127 assert(j == defs.size());
139 res.reserve(a.size() + b.size());
140 res.insert(res.end(), a.begin(), a.end());
141 res.insert(res.end(), b.begin(), b.end());
148 for (
size_t i = 0, e = n; i != e; ++i)
149 defs.emplace_back(a->proj(e, i));
150 for (
size_t i = 0, e = m; i != e; ++i)
151 defs.emplace_back(b->
proj(e, i));
163 auto& world = t->world();
164 if (
auto sigma = t->isa<
Sigma>())
return world.tuple(sigma->ops());
165 if (
auto arr = t->isa<
Arr>())
return world.pack(arr->arity(), arr->body());
const Def * callee() const
A (possibly paramterized) Array.
const Def * callee() 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.
World & world() const noexcept
bool is_intro() const noexcept
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
const Def * op(size_t i) const noexcept
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
virtual const Def * arity() const
constexpr size_t num_ops() const noexcept
const Def * callee() const
const Extract * extract() const
static std::optional< T > isa(const Def *def)
static T as(const Def *def)
const Def * arity() const final
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
const Extract * extract() const
Def(World *, Node, const Def *type, Defs ops, flags_t flags)
Constructor for an immutable Def.
const Def * arity() const final
The World represents the whole program and manages creation of MimIR nodes (Defs).
const Def * sigma(Defs ops)
const Def * tuple(Defs ops)
const Lit * lit_nat(nat_t a)
const Def * flatten(const Def *def)
Flattens a sigma/array/pack/tuple.
Vector< const Def * > DefVec
const Def * cat_tuple(nat_t n, nat_t m, const Def *a, const Def *b)
bool is_unit(const Def *)
std::string tuple2str(const Def *)
const Def * cat_sigma(nat_t n, nat_t m, const Def *a, const Def *b)
const Def * unflatten(const Def *def, const Def *type)
Applies the reverse transformation on a Pack / Tuple, given the original type.
const Def * tuple_of_types(const Def *t)