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> Ref fold(World& world, Ref type,
const Def*&
a,
const Def*& b, Ref
mode = {}) {
87 if (
a->isa<
Bot>() || b->isa<
Bot>())
return world.bot(type);
93 bool nsw =
false,
nuw =
false;
94 if constexpr (std::is_same_v<Id, wrap>) {
103 case i: res = fold<Id, id, i>(*la, *lb, nsw, nuw); break;
108 res = fold<Id, id, 64>(*la, *lb,
false,
false);
109 if (res && !std::is_same_v<Id, icmp>) *res %=
size;
112 return res ? world.lit(type, *res) : world.bot(type);
120template<
class Id, nat_t w> Res fold(
u64 a, [[maybe_unused]]
bool nsw, [[maybe_unused]]
bool nuw) {
124 if constexpr (std::is_same_v<Id, abs>)
127 []<
bool flag =
false>() {
static_assert(flag,
"missing tag"); }
131template<
class Id> Ref fold(World& world, Ref type,
const Def*&
a) {
132 if (
a->isa<
Bot>())
return world.bot(type);
137 bool nsw =
false,
nuw =
false;
141 case i: res = fold<Id, i>(*la, nsw, nuw); break;
145 res = fold<Id, 64>(*la,
false,
false);
146 if (res && !std::is_same_v<Id, icmp>) *res %=
size;
149 return res ? world.lit(type, *res) : world.bot(type);
165template<
class Id> Ref reassociate(Id
id, World& world, [[maybe_unused]]
const App* ab, Ref
a, Ref b) {
170 auto la =
a->isa<
Lit>();
171 auto [x, y] = xy ? xy->template args<2>() :
std::array<const Def*, 2>{
nullptr,
nullptr};
172 auto [z,
w] = zw ? zw->template args<2>() :
std::array<const Def*, 2>{
nullptr,
nullptr};
177 auto make_op = [&world,
id](Ref
a, Ref b) {
return world.call(
id,
Mode::none,
Defs{
a, b}); };
179 if (la && lz)
return make_op(make_op(
a, z), w);
180 if (lx && lz)
return make_op(make_op(x, z), make_op(y, w));
181 if (lz)
return make_op(z, make_op(
a, w));
182 if (lx)
return make_op(x, make_op(y, b));
186template<
class Id> Ref merge_cmps(std::array<std::array<u64, 2>, 2> tab, Ref
a, Ref b) {
187 static_assert(
sizeof(
sub_t) == 1,
"if this ever changes, please adjust the logic below");
188 static constexpr size_t num_bits = std::bit_width(
Annex::Num<Id> - 1_u64);
190 auto& world =
a->world();
194 if (a_cmp && b_cmp && a_cmp->arg() == b_cmp->arg()) {
197 sub_t a_sub = a_cmp.sub();
198 sub_t b_sub = b_cmp.sub();
199 for (
size_t i = 0; i != num_bits; ++i, res >>= 1, a_sub >>= 1, b_sub >>= 1)
200 res |= tab[a_sub & 1][b_sub & 1] << 7_u8;
201 res >>= (7_u8 -
u8(num_bits));
203 if constexpr (std::is_same_v<Id, math::cmp>)
204 return world.call(
math::cmp(res), a_cmp->decurry()->arg(), a_cmp->arg());
215 auto& world = type->
world();
216 auto [
a, b] = arg->
projs<2>();
224 case nat::add:
return world.lit_nat(*la + *lb);
225 case nat::sub:
return *la < *lb ? world.lit_nat_0() : world.lit_nat(*la - *lb);
226 case nat::mul:
return world.lit_nat(*la * *lb);
238 if (*la == 1 &&
id ==
nat::mul)
return b;
241 if (lb && *lb == 0 &&
id ==
nat::sub)
return a;
243 return world.raw_app(type, callee, {
a, b});
247 auto& world = type->
world();
250 if (
id ==
ncmp::f)
return world.lit_ff();
252 auto [
a, b] = arg->
projs<2>();
259 case ncmp:: e:
return world.lit_bool(*la == *lb);
260 case ncmp::ne:
return world.lit_bool(*la != *lb);
261 case ncmp::l :
return world.lit_bool(*la < *lb);
262 case ncmp::le:
return world.lit_bool(*la <= *lb);
263 case ncmp::g :
return world.lit_bool(*la > *lb);
264 case ncmp::ge:
return world.lit_bool(*la >= *lb);
265 default: fe::unreachable();
271 return world.raw_app(type, callee, {
a, b});
275 auto& world = type->
world();
276 auto callee = c->as<
App>();
277 auto [
a, b] = arg->
projs<2>();
279 if (
auto result = fold<icmp, id>(world, type,
a, b))
return result;
280 if (
id ==
icmp::f)
return world.lit_ff();
281 if (
id ==
icmp::t)
return world.lit_tt();
283 if (
id & (
icmp::e & 0xff))
return world.lit_tt();
284 if (
id ==
icmp::ne)
return world.lit_ff();
287 return world.raw_app(type, callee, {
a, b});
291 auto& world = type->
world();
292 auto callee = c->as<
App>();
293 auto [
a, b] = arg->
projs<2>();
294 if (
auto result = fold<extrema, id>(world, type,
a, b))
return result;
295 return world.raw_app(type, callee, {
a, b});
299 auto& world = type->
world();
300 auto callee = c->as<
App>();
302 auto [_, actual_type] = type->
projs<2>();
303 auto make_res = [&,
mem =
mem](
Ref res) {
return world.tuple({
mem, res}); };
305 if (
auto result = fold<abs>(world, actual_type,
a))
return make_res(result);
306 return world.raw_app(type, callee, arg);
310 auto& world = type->
world();
311 auto callee = c->as<
App>();
319 case bit1::f:
return world.lit_idx(*ls, 0);
320 case bit1::t:
return world.lit_idx(*ls, *ls - 1_u64);
326 if (
auto la =
Lit::isa(
a))
return world.lit_idx_mod(*ls, ~*la);
329 return world.raw_app(type, callee,
a);
333 auto& world = type->
world();
334 auto callee = c->as<
App>();
335 auto [
a, b] = arg->
projs<2>();
336 auto s = callee->decurry()->arg();
343 if (
auto res = merge_cmps<icmp>(tab,
a, b))
return res;
344 if (
auto res = merge_cmps<math::cmp>(tab,
a, b))
return res;
351 case bit2:: f:
return world.lit(type, 0);
352 case bit2:: t:
if (ls)
return world.lit(type, *ls-1_u64);
break;
362 if (la && lb && ls) {
364 case bit2::and_:
return world.lit_idx (*ls, *la & *lb);
365 case bit2:: or_:
return world.lit_idx (*ls, *la | *lb);
366 case bit2::xor_:
return world.lit_idx (*ls, *la ^ *lb);
367 case bit2::nand:
return world.lit_idx_mod(*ls, ~(*la & *lb));
368 case bit2:: nor:
return world.lit_idx_mod(*ls, ~(*la | *lb));
369 case bit2::nxor:
return world.lit_idx_mod(*ls, ~(*la ^ *lb));
370 case bit2:: iff:
return world.lit_idx_mod(*ls, ~ *la | *lb);
371 case bit2::niff:
return world.lit_idx (*ls, *la & ~*lb);
372 default: fe::unreachable();
377 auto unary = [&](
bool x,
bool y,
Ref a) ->
Ref {
378 if (!x && !y)
return world.lit(type, 0);
379 if ( x && y)
return ls ? world.lit(type, *ls-1_u64) :
nullptr;
380 if (!x && y)
return a;
387 if (
auto res = unary(tab[0][0], tab[1][1],
a))
return res;
392 if (
auto res = unary(tab[0][0], tab[0][1], b))
return res;
393 }
else if (ls && *la == *ls - 1_u64) {
394 if (
auto res = unary(tab[1][0], tab[1][1], b))
return res;
400 if (
auto res = unary(tab[0][0], tab[1][0],
a))
return res;
401 }
else if (ls && *lb == *ls - 1_u64) {
402 if (
auto res = unary(tab[0][1], tab[1][1],
a))
return res;
406 if (
auto res = reassociate<bit2>(
id, world, callee,
a, b))
return res;
408 return world.raw_app(type, callee, {
a, b});
412 auto& world = type->
world();
413 auto callee = c->as<
App>();
416 if (*i < *
s)
return world.lit_idx(*
s, *i);
417 if (
auto m =
Lit::isa(callee->arg()))
return *m ? world.bot(type) : world.lit_idx_mod(*
s, *i);
421 return world.raw_app(type, c, arg);
425 auto& world = type->
world();
426 auto callee = c->as<
App>();
427 auto [
a, b] = arg->
projs<2>();
431 if (
auto result = fold<shr, id>(world, type,
a, b))
return result;
433 if (
auto la =
Lit::isa(
a); la && *la == 0) {
441 if (ls && *lb > *ls)
return world.bot(type);
451 return world.raw_app(type, callee, {
a, b});
455 auto& world = type->
world();
456 auto callee = c->as<
App>();
457 auto [
a, b] = arg->
projs<2>();
458 auto mode = callee->arg();
462 if (
auto result = fold<wrap, id>(world, type,
a, b))
return result;
473 }
else if (*la == 1) {
488 default: fe::unreachable();
494 return world.call(
wrap::add,
mode,
Defs{a, world.lit_idx_mod(*ls, ~*lb + 1_u64)});
495 else if (
id ==
wrap::shl && ls && *lb > *ls)
496 return world.bot(type);
502 case wrap::sub:
return world.lit(type, 0);
509 if (
auto res = reassociate<wrap>(
id, world, callee,
a, b))
return res;
511 return world.raw_app(type, callee, {
a, b});
515 auto& world = full_type->
world();
516 auto callee = c->as<
App>();
518 auto [
a, b] = ab->projs<2>();
519 auto [_, type] = full_type->
projs<2>();
520 auto make_res = [&,
mem =
mem](
Ref 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));
550 return world.raw_app(full_type, callee, arg);
554 auto& world = dst_t->
world();
555 auto callee = c->as<
App>();
556 auto s_t = x->
type()->as<
App>();
557 auto d_t = dst_t->as<
App>();
563 if (s_t == d_t)
return x;
564 if (x->isa<
Bot>())
return world.bot(d_t);
566 if (ls && ld && *ld < *ls)
return world.call(
conv::u, d, x);
571 if (*ld == 0)
return world.lit(d_t, *
l);
572 return world.lit(d_t, *
l % *ld);
581 else if (S == sw && D == dw) return world.lit(d_t, w2s<D>(mim::bitcast<w2s<S>>(*l)));
582 M( 1, 8)
M( 1, 16)
M( 1, 32)
M( 1, 64)
583 M( 8, 16)
M( 8, 32)
M( 8, 64)
586 else assert(
false &&
"TODO: conversion between different Idx sizes");
590 return world.raw_app(dst_t, callee, x);
594 auto& world = dst_t->
world();
595 auto src_t = src->
type();
597 if (src->isa<
Bot>())
return world.bot(dst_t);
598 if (src_t == dst_t)
return src;
601 return other->arg()->
type() == dst_t ? other->arg() : world.call<
bitcast>(dst_t, other->arg());
604 if (dst_t->isa<
Nat>())
return world.lit(dst_t, *
l);
605 if (
Idx::size(dst_t))
return world.lit(dst_t, *
l);
608 return world.raw_app(dst_t, callee, src);
616 auto& world = type->
world();
618 return world.lit_nat(8);
619 }
else if (type->isa<
Pi>()) {
620 return world.lit_nat(8);
625 case 16:
return world.lit_nat(2);
626 case 32:
return world.lit_nat(4);
627 case 64:
return world.lit_nat(8);
628 default: fe::unreachable();
630 }
else if (type->isa<
Sigma>() || type->isa<
Meet>()) {
633 for (
auto t : type->
ops()) {
636 if (!
a || !
s)
goto out;
639 offset =
pad(offset, *
a) + *
s;
643 u64 size = std::max(1_u64, offset);
654 }
else if (
auto join = type->isa<
Join>()) {
663 auto& w = type->
world();
664 auto callee = c->as<
App>();
665 auto is_os = callee->
arg();
666 auto [n_i, Is, n_o, Os,
f] = is_os->
projs<5>();
667 auto [r,
s] = callee->decurry()->args<2>();
676 if (lr && ls && *lr == 1 && *ls == 1)
return w.app(
f, arg);
679 auto args = arg->
projs(*l_in);
681 if (lr && std::ranges::all_of(args, [](
Ref arg) {
return arg->isa<
Tuple,
Pack>(); })) {
682 auto shapes =
s->projs(*lr);
683 auto s_n =
Lit::isa(shapes.front());
686 auto elems =
DefVec(*s_n, [&,
f =
f](
size_t s_i) {
687 auto inner_args =
DefVec(args.size(), [&](
size_t i) { return args[i]->proj(*s_n, s_i); });
689 return w.app(
f, inner_args);
691 auto app_zip = w.app(w.annex<
zip>(), {w.lit_nat(*lr - 1), w.tuple(shapes.view().subspan(1))});
692 return w.app(w.app(app_zip, is_os), inner_args);
695 return w.tuple(elems);
700 return w.raw_app(type, callee, arg);
704 auto& world = type->
world();
708 if (arg->
dep_const())
return world.lit_tt();
711 return world.
raw_app(type, callee, arg);
const App * decurry() const
Returns App::callee again as App.
A (possibly paramterized) Array.
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == -1_n) or std::array (otherwise).
const T * isa_imm() const
static Ref size(Ref def)
Checks if def is a Idx s and returns s or nullptr otherwise.
static constexpr nat_t size2bitwidth(nat_t n)
static std::optional< T > isa(Ref def)
A (possibly paramterized) Tuple.
A dependent function type.
Helper class to retrieve Infer::arg if present.
This is a thin wrapper for std::span<T, N> with the following additional features:
Specific Bound depending on Up.
Extremum. Either Top (Up) or Bottom.
Data constructor for a Sigma.
Ref raw_app(Ref type, Ref callee, Ref arg)
#define MIM_core_NORMALIZER_IMPL
Ref normalize_extrema(Ref type, Ref c, Ref arg)
Ref normalize_bit1(Ref type, Ref c, Ref a)
const Sigma * convert(const TBound< up > *b)
Ref normalize_bitcast(Ref dst_t, Ref callee, Ref src)
Ref normalize_ncmp(Ref type, Ref callee, Ref arg)
Ref normalize_div(Ref full_type, Ref c, Ref arg)
Ref normalize_nat(Ref type, Ref callee, Ref arg)
Ref normalize_conv(Ref dst_t, Ref c, Ref x)
Ref normalize_trait(Ref nat, Ref callee, Ref type)
Ref normalize_icmp(Ref type, Ref c, Ref arg)
Ref normalize_bit2(Ref type, Ref c, Ref arg)
Ref normalize_wrap(Ref type, Ref c, Ref arg)
Ref normalize_pe(Ref type, Ref callee, Ref arg)
Ref op(trait o, Ref type)
Ref normalize_abs(Ref type, Ref c, Ref arg)
constexpr std::array< std::array< u64, 2 >, 2 > make_truth_table(bit2 id)
Ref normalize_zip(Ref type, Ref c, Ref arg)
Ref normalize_shr(Ref type, Ref c, Ref arg)
@ nuw
No Unsigned Wrap around.
@ nsw
No Signed Wrap around.
Ref normalize_idx(Ref type, Ref c, Ref arg)
std::optional< nat_t > isa_f(Ref 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
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)