14template<
class Id, Id
id, nat_t w>
15Res fold(
u64 a,
u64 b, [[maybe_unused]]
bool nsw, [[maybe_unused]]
bool nuw) {
21 if constexpr (std::is_same_v<Id, wrap>) {
24 if (
nuw && res <
u)
return {};
32 if constexpr (std::is_same_v<UT, bool>)
37 if (b >= w)
return {};
39 if constexpr (std::is_same_v<UT, bool>)
43 if (
nuw && res <
u)
return {};
47 []<
bool flag =
false>() {
static_assert(flag,
"missing sub tag"); }();
49 }
else if constexpr (std::is_same_v<Id, shr>) {
50 if (b >= w)
return {};
51 if constexpr (
false) {}
52 else if constexpr (
id ==
shr::a)
return s >>
t;
53 else if constexpr (
id ==
shr::l)
return u >> v;
54 else []<
bool flag =
false>() {
static_assert(flag,
"missing sub tag"); }();
55 }
else if constexpr (std::is_same_v<Id, div>) {
56 if (b == 0)
return {};
57 if constexpr (
false) {}
59 else if constexpr (
id ==
div::udiv)
return u / v;
61 else if constexpr (
id ==
div::urem)
return u % v;
62 else []<
bool flag =
false>() {
static_assert(flag,
"missing sub tag"); }();
63 }
else if constexpr (std::is_same_v<Id, icmp>) {
65 auto pm = !(
u >> UT(w - 1)) && (v >> UT(w - 1));
66 auto mp = (
u >> UT(w - 1)) && !(v >> UT(w - 1));
73 }
else if constexpr (std::is_same_v<Id, extrema>) {
74 if constexpr (
false) {}
80 []<
bool flag =
false>() {
static_assert(flag,
"missing tag"); }();
86template<
class Id, Id
id>
87const Def* fold(World& world,
const Def* type,
const Def*&
a,
const Def*& b,
const Def*
mode = {}) {
88 if (
a->isa<
Bot>() || b->isa<
Bot>())
return world.bot(type);
94 bool nsw =
false,
nuw =
false;
95 if constexpr (std::is_same_v<Id, wrap>) {
104 case i: res = fold<Id, id, i>(*la, *lb, nsw, nuw); break;
109 res = fold<Id, id, 64>(*la, *lb,
false,
false);
110 if (res && !std::is_same_v<Id, icmp>) *res %=
size;
113 return res ? world.lit(type, *res) : world.bot(type);
121template<
class Id, nat_t w> Res fold(
u64 a, [[maybe_unused]]
bool nsw, [[maybe_unused]]
bool nuw) {
125 if constexpr (std::is_same_v<Id, abs>)
128 []<
bool flag =
false>() {
static_assert(flag,
"missing tag"); }
132template<
class Id>
const Def* fold(World& world,
const Def* type,
const Def*&
a) {
133 if (
a->isa<
Bot>())
return world.bot(type);
138 bool nsw =
false,
nuw =
false;
142 case i: res = fold<Id, i>(*la, nsw, nuw); break;
146 res = fold<Id, 64>(*la,
false,
false);
147 if (res && !std::is_same_v<Id, icmp>) *res %=
size;
150 return res ? world.lit(type, *res) : world.bot(type);
167const Def* reassociate(Id
id, World& world, [[maybe_unused]]
const App* ab,
const Def*
a,
const Def* b) {
172 auto la =
a->isa<
Lit>();
173 auto [x, y] = xy ? xy->template args<2>() : std::array<const Def*, 2>{
nullptr,
nullptr};
174 auto [z,
w] = zw ? zw->template args<2>() : std::array<const Def*, 2>{
nullptr,
nullptr};
179 auto make_op = [&world,
id](
const Def*
a,
const Def* b) {
return world.call(
id,
Mode::none,
Defs{
a, b}); };
181 if (la && lz)
return make_op(make_op(
a, z), w);
182 if (lx && lz)
return make_op(make_op(x, z), make_op(y, w));
183 if (lz)
return make_op(z, make_op(
a, w));
184 if (lx)
return make_op(x, make_op(y, b));
188template<
class Id>
const Def* merge_cmps(std::array<std::array<u64, 2>, 2> tab,
const Def*
a,
const Def* b) {
189 static_assert(
sizeof(
sub_t) == 1,
"if this ever changes, please adjust the logic below");
190 static constexpr size_t num_bits = std::bit_width(
Annex::Num<Id> - 1_u64);
192 auto& world =
a->world();
196 if (a_cmp && b_cmp && a_cmp->arg() == b_cmp->arg()) {
199 sub_t a_sub = a_cmp.sub();
200 sub_t b_sub = b_cmp.sub();
201 for (
size_t i = 0; i != num_bits; ++i, res >>= 1, a_sub >>= 1, b_sub >>= 1)
202 res |= tab[a_sub & 1][b_sub & 1] << 7_u8;
203 res >>= (7_u8 -
u8(num_bits));
205 if constexpr (std::is_same_v<Id, math::cmp>)
206 return world.call(
math::cmp(res), a_cmp->decurry()->arg(), a_cmp->arg());
217 auto& world = type->
world();
218 auto [
a, b] = arg->
projs<2>();
226 case nat::add:
return world.lit_nat(*la + *lb);
227 case nat::sub:
return *la < *lb ? world.lit_nat_0() : world.lit_nat(*la - *lb);
228 case nat::mul:
return world.lit_nat(*la * *lb);
240 if (*la == 1 &&
id ==
nat::mul)
return b;
243 if (lb && *lb == 0 &&
id ==
nat::sub)
return a;
245 return world.raw_app(type, callee, {
a, b});
249 auto& world = type->
world();
252 if (
id ==
ncmp::f)
return world.lit_ff();
254 auto [
a, b] = arg->
projs<2>();
261 case ncmp:: e:
return world.lit_bool(*la == *lb);
262 case ncmp::ne:
return world.lit_bool(*la != *lb);
263 case ncmp::l :
return world.lit_bool(*la < *lb);
264 case ncmp::le:
return world.lit_bool(*la <= *lb);
265 case ncmp::g :
return world.lit_bool(*la > *lb);
266 case ncmp::ge:
return world.lit_bool(*la >= *lb);
267 default: fe::unreachable();
273 return world.raw_app(type, callee, {
a, b});
277 auto& world = type->
world();
278 auto callee = c->as<
App>();
279 auto [
a, b] = arg->
projs<2>();
281 if (
auto result = fold<icmp, id>(world, type,
a, b))
return result;
282 if (
id ==
icmp::f)
return world.lit_ff();
283 if (
id ==
icmp::t)
return world.lit_tt();
285 if (
id & (
icmp::e & 0xff))
return world.lit_tt();
286 if (
id ==
icmp::ne)
return world.lit_ff();
289 return world.raw_app(type, callee, {
a, b});
293 auto& world = type->
world();
294 auto callee = c->as<
App>();
295 auto [
a, b] = arg->
projs<2>();
296 if (
auto result = fold<extrema, id>(world, type,
a, b))
return result;
297 return world.raw_app(type, callee, {
a, b});
301 auto& world = type->
world();
303 auto [_, actual_type] = type->
projs<2>();
304 auto make_res = [&,
mem =
mem](
const Def* res) {
return world.tuple({
mem, res}); };
306 if (
auto result = fold<abs>(world, actual_type,
a))
return make_res(result);
311 auto& world = type->
world();
312 auto callee = c->as<
App>();
313 auto s = callee->decurry()->arg();
320 case bit1::f:
return world.lit_idx(*ls, 0);
321 case bit1::t:
return world.lit_idx(*ls, *ls - 1_u64);
327 if (
auto la =
Lit::isa(
a))
return world.lit_idx_mod(*ls, ~*la);
334 auto& world = type->
world();
335 auto callee = c->as<
App>();
336 auto [
a, b] = arg->
projs<2>();
337 auto s = callee->decurry()->arg();
344 if (
auto res = merge_cmps<icmp>(tab,
a, b))
return res;
345 if (
auto res = merge_cmps<math::cmp>(tab,
a, b))
return res;
352 case bit2:: f:
return world.lit(type, 0);
353 case bit2:: t:
if (ls)
return world.lit(type, *ls-1_u64);
break;
354 case bit2:: fst:
return a;
355 case bit2:: snd:
return b;
363 if (la && lb && ls) {
365 case bit2::and_:
return world.lit_idx (*ls, *la & *lb);
366 case bit2:: or_:
return world.lit_idx (*ls, *la | *lb);
367 case bit2::xor_:
return world.lit_idx (*ls, *la ^ *lb);
368 case bit2::nand:
return world.lit_idx_mod(*ls, ~(*la & *lb));
369 case bit2:: nor:
return world.lit_idx_mod(*ls, ~(*la | *lb));
370 case bit2::nxor:
return world.lit_idx_mod(*ls, ~(*la ^ *lb));
371 case bit2:: iff:
return world.lit_idx_mod(*ls, ~ *la | *lb);
372 case bit2::niff:
return world.lit_idx (*ls, *la & ~*lb);
373 default: fe::unreachable();
378 auto unary = [&](
bool x,
bool y,
const Def*
a) ->
const Def* {
379 if (!x && !y)
return world.lit(type, 0);
380 if ( x && y)
return ls ? world.lit(type, *ls-1_u64) :
nullptr;
381 if (!x && y)
return a;
388 if (
auto res = unary(tab[0][0], tab[1][1],
a))
return res;
393 if (
auto res = unary(tab[0][0], tab[0][1], b))
return res;
394 }
else if (ls && *la == *ls - 1_u64) {
395 if (
auto res = unary(tab[1][0], tab[1][1], b))
return res;
401 if (
auto res = unary(tab[0][0], tab[1][0],
a))
return res;
402 }
else if (ls && *lb == *ls - 1_u64) {
403 if (
auto res = unary(tab[0][1], tab[1][1],
a))
return res;
407 if (
auto res = reassociate<bit2>(
id, world, callee,
a, b))
return res;
409 return world.raw_app(type, callee, {
a, b});
413 auto& world = type->
world();
414 auto callee = c->as<
App>();
417 if (*i < *
s)
return world.lit_idx(*
s, *i);
418 if (
auto m =
Lit::isa(callee->arg()))
return *m ? world.bot(type) : world.lit_idx_mod(*
s, *i);
426 auto& world = type->
world();
427 auto callee = c->as<
App>();
428 auto [
a, b] = arg->
projs<2>();
432 if (
auto result = fold<shr, id>(world, type,
a, b))
return result;
434 if (
auto la =
Lit::isa(
a); la && *la == 0) {
442 if (ls && *lb > *ls)
return world.bot(type);
452 return world.raw_app(type, callee, {
a, b});
456 auto& world = type->
world();
457 auto callee = c->as<
App>();
458 auto [
a, b] = arg->
projs<2>();
459 auto mode = callee->arg();
463 if (
auto result = fold<wrap, id>(world, type,
a, b))
return result;
474 }
else if (*la == 1) {
489 default: fe::unreachable();
495 return world.call(
wrap::add,
mode,
Defs{a, world.lit_idx_mod(*ls, ~*lb + 1_u64)});
496 else if (
id ==
wrap::shl && ls && *lb > *ls)
497 return world.bot(type);
503 case wrap::sub:
return world.lit(type, 0);
510 if (
auto res = reassociate<wrap>(
id, world, callee,
a, b))
return res;
512 return world.raw_app(type, callee, {
a, b});
516 auto& world = full_type->
world();
518 auto [
a, b] = ab->projs<2>();
519 auto [_, type] = full_type->
projs<2>();
520 auto make_res = [&,
mem =
mem](
const Def* res) {
return world.tuple({
mem, res}); };
522 if (
auto result = fold<div, id>(world, type,
a, b))
return make_res(result);
525 if (*la == 0)
return make_res(
a);
529 if (*lb == 0)
return make_res(world.bot(type));
535 case div::srem:
return make_res(world.lit(type, 0));
536 case div::urem:
return make_res(world.lit(type, 0));
543 case div::sdiv:
return make_res(world.lit(type, 1));
544 case div::udiv:
return make_res(world.lit(type, 1));
545 case div::srem:
return make_res(world.lit(type, 0));
546 case div::urem:
return make_res(world.lit(type, 0));
554 auto& world = dst_t->
world();
555 auto s_t = x->
type()->as<
App>();
556 auto d_t = dst_t->as<
App>();
562 if (s_t == d_t)
return x;
563 if (x->isa<
Bot>())
return world.bot(d_t);
565 if (ls && ld && *ld < *ls)
return world.call(
conv::u, d, x);
570 if (*ld == 0)
return world.lit(d_t, *
l);
571 return world.lit(d_t, *
l % *ld);
580 else if (S == sw && D == dw) return world.lit(d_t, w2s<D>(mim::bitcast<w2s<S>>(*l)));
581 M( 1, 8)
M( 1, 16)
M( 1, 32)
M( 1, 64)
582 M( 8, 16)
M( 8, 32)
M( 8, 64)
585 else assert(
false &&
"TODO: conversion between different Idx sizes");
593 auto& world = dst_t->
world();
594 auto src_t = src->
type();
596 if (src->isa<
Bot>())
return world.bot(dst_t);
597 if (src_t == dst_t)
return src;
600 return other->arg()->
type() == dst_t ? other->arg() : world.call<
bitcast>(dst_t, other->arg());
603 if (dst_t->isa<
Nat>())
return world.lit(dst_t, *
l);
604 if (
Idx::isa(dst_t))
return world.lit(dst_t, *
l);
615 auto& world = type->
world();
617 return world.lit_nat(8);
618 }
else if (type->isa<
Pi>()) {
619 return world.lit_nat(8);
624 case 16:
return world.lit_nat(2);
625 case 32:
return world.lit_nat(4);
626 case 64:
return world.lit_nat(8);
627 default: fe::unreachable();
629 }
else if (type->isa<
Sigma>() || type->isa<
Meet>()) {
632 for (
auto t : type->
ops()) {
635 if (!
a || !
s)
return {};
638 offset =
pad(offset, *
a) + *
s;
642 u64 size = std::max(1_u64, offset);
653 }
else if (
auto join = type->isa<
Join>()) {
661 auto& w = type->
world();
662 auto callee = c->as<
App>();
663 auto is_os = callee->
arg();
664 auto [n_i, Is, n_o, Os,
f] = is_os->
projs<5>();
665 auto [r,
s] = callee->decurry()->args<2>();
674 if (lr && ls && *lr == 1 && *ls == 1)
return w.app(
f, arg);
677 auto args = arg->
projs(*l_in);
679 if (lr && std::ranges::all_of(args, [](
const Def* arg) {
return arg->isa<
Tuple,
Pack>(); })) {
680 auto shapes =
s->projs(*lr);
681 auto s_n =
Lit::isa(shapes.front());
684 auto elems =
DefVec(*s_n, [&,
f =
f](
size_t s_i) {
685 auto inner_args =
DefVec(args.size(), [&](
size_t i) { return args[i]->proj(*s_n, s_i); });
687 return w.app(
f, inner_args);
689 auto app_zip = w.app(w.annex<
zip>(), {w.lit_nat(*lr - 1), w.tuple(shapes.view().subspan(1))});
690 return w.app(w.app(app_zip, is_os), inner_args);
693 return w.tuple(elems);
702 auto& world = type->
world();
706 if (arg->
is_closed())
return world.lit_tt();
A (possibly paramterized) Array.
World & world() const noexcept
constexpr auto ops() 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).
const T * isa_imm() const
bool is_closed() const
Has no free_vars()?
static constexpr nat_t size2bitwidth(nat_t n)
static const Def * isa(const Def *def)
Checks if def is a Idx s and returns s or nullptr otherwise.
static std::optional< T > isa(const Def *def)
static T as(const Def *def)
A (possibly paramterized) Tuple.
A dependent function type.
Data constructor for a Sigma.
#define MIM_core_NORMALIZER_IMPL
const Def * normalize_nat(const Def *type, const Def *callee, const Def *arg)
const Sigma * convert(const TBound< up > *b)
const Def * normalize_div(const Def *full_type, const Def *, const Def *arg)
const Def * normalize_pe(const Def *type, const Def *, const Def *arg)
const Def * normalize_extrema(const Def *type, const Def *c, const Def *arg)
const Def * normalize_icmp(const Def *type, const Def *c, const Def *arg)
const Def * normalize_bit1(const Def *type, const Def *c, const Def *a)
const Def * normalize_conv(const Def *dst_t, const Def *, const Def *x)
const Def * normalize_bit2(const Def *type, const Def *c, const Def *arg)
const Def * normalize_wrap(const Def *type, const Def *c, const Def *arg)
const Def * normalize_trait(const Def *, const Def *, const Def *type)
const Def * op(trait o, const Def *type)
const Def * normalize_abs(const Def *type, const Def *, const Def *arg)
const Def * normalize_idx(const Def *type, const Def *c, const Def *arg)
constexpr std::array< std::array< u64, 2 >, 2 > make_truth_table(bit2 id)
const Def * normalize_bitcast(const Def *dst_t, const Def *, const Def *src)
const Def * normalize_ncmp(const Def *type, const Def *callee, const Def *arg)
@ nuw
No Unsigned Wrap around.
@ nsw
No Signed Wrap around.
const Def * normalize_zip(const Def *type, const Def *c, const Def *arg)
const Def * normalize_shr(const Def *type, const Def *c, const Def *arg)
std::optional< nat_t > isa_f(const Def *def)
Vector< const Def * > DefVec
D bitcast(const S &src)
A bitcast from src of type S to D.
void commute(const Def *&a, const Def *&b)
Swap Lit to left - or smaller Def::gid, if no lit present.
u64 pad(u64 offset, u64 align)
constexpr bool is_commutative(Id)
typename detail::w2s_< w >::type w2s
auto match(const Def *def)
constexpr bool is_associative(Id id)
typename detail::w2u_< w >::type w2u
static constexpr size_t Num
static constexpr flags_t Base
#define MIM_1_8_16_32_64(m)