13 if (
size == 0)
return 64;
15 return width == 0 ? 1 : width;
18constexpr std::optional<unsigned> idx_shift_amount(
u64 size,
u64 b) {
19 auto width = idx_shift_width(
size);
20 if (b >= width)
return {};
21 return static_cast<unsigned>(b);
24constexpr u64 idx_unsigned_max(
u64 size) {
return size == 0 ? std::numeric_limits<u64>::max() :
size - 1; }
27 return size == 0 ?
static_cast<u64>(std::numeric_limits<s64>::max()) : (
size - 1) / 2;
31 return size == 0 ?
static_cast<u64>(std::numeric_limits<s64>::max()) + 1_u64 :
size / 2;
34constexpr u64 idx_signed_abs(
s64 x) {
return x >= 0 ?
static_cast<u64>(x) : static_cast<
u64>(-(x + 1)) + 1_u64; }
37 if (
abs ==
static_cast<u64>(std::numeric_limits<s64>::max()) + 1_u64)
return std::numeric_limits<s64>::min();
38 return -
static_cast<s64>(
abs);
45 if (
size == 0)
return x >
static_cast<u64>(std::numeric_limits<s64>::max());
48 return x > (
size - 1) / 2;
53 if (
size == 0)
return static_cast<s64>(x);
55 const u64 max_pos = (
size - 1) / 2;
56 if (x <= max_pos)
return static_cast<s64>(x);
59 return -
static_cast<s64>(
size - x);
63 if (
size == 0)
return static_cast<u64>(x);
64 return x >= 0 ?
static_cast<u64>(x) :
size - static_cast<
u64>(-x);
68 if (
size == 0)
return static_cast<u64>(x);
69 if (x >= 0)
return static_cast<u64>(x) %
size;
71 auto rem = idx_signed_abs(x) %
size;
76 if (
size == 0)
return a + b <
a;
77 return a >
size - 1 - b;
80constexpr bool idx_sub_nuw(
u64,
u64 a,
u64 b) {
return a < b; }
83 if (
a == 0 || b == 0)
return false;
85 if (
size == 0)
return b > std::numeric_limits<u64>::max() /
a;
86 return b > (
size - 1) /
a;
90 if (
size == 0)
return a + b;
91 return (
a + b) %
size;
101 if (
size == 0)
return a - b;
102 return (
a >= b) ? (
a - b) : (
size - (b -
a));
106 if (
size == 0)
return a * b;
111 if (b % 2_u64 != 0)
r = idx_add(
size, r,
a);
113 if (b)
a = idx_add(
size,
a,
a);
119 const bool sa = idx_sign(
size,
a);
120 const bool sb = idx_sign(
size, b);
122 const bool sr = idx_sign(
size, r);
123 return (sa == sb) && (sr != sa);
127 const bool sa = idx_sign(
size,
a);
128 const bool sb = idx_sign(
size, b);
130 const bool sr = idx_sign(
size, r);
131 return (sa != sb) && (sr != sa);
136 const s64 y = idx_sext(
size, b);
138 if (x == 0 || y == 0)
return false;
140 const s64 min_val =
size == 0 ? std::numeric_limits<s64>::min() : -static_cast<
s64>(
size / 2);
141 const s64 max_val =
size == 0 ? std::numeric_limits<s64>::max() : static_cast<
s64>((
size - 1) / 2);
143 if (x == -1)
return y == min_val;
144 if (y == -1)
return x == min_val;
148 return x > max_val / y;
150 return y < min_val / x;
152 return x < min_val / y;
154 return x < max_val / y;
157constexpr std::optional<u64> idx_udiv([[maybe_unused]]
u64 size,
u64 a,
u64 b) {
158 if (b == 0)
return {};
162constexpr std::optional<u64> idx_urem([[maybe_unused]]
u64 size,
u64 a,
u64 b) {
163 if (b == 0)
return {};
168 const bool sa = idx_sign(
size,
a);
169 const bool sb = idx_sign(
size, b);
171 if (
a == b)
return false;
172 if (!sa && sb)
return false;
173 if (sa && !sb)
return true;
181 const s64 y = idx_sext(
size, b);
183 if (y == 0)
return true;
185 const s64 min_val = [&] {
186 if (
size == 0)
return std::numeric_limits<s64>::min();
187 return -
static_cast<s64>(
size / 2);
190 return x == min_val && y == -1;
195 const s64 y = idx_sext(
size, b);
196 return idx_from_signed(
size, x / y);
201 const s64 y = idx_sext(
size, b);
202 return idx_from_signed(
size, x % y);
205constexpr bool idx_shl_nuw(
u64 size,
u64 a,
unsigned k) {
207 u64 max = idx_unsigned_max(
size);
210 if (x > max / 2_u64)
return true;
217constexpr bool idx_shl_nsw(
u64 size,
u64 a,
unsigned k) {
220 u64 y =
static_cast<u64>(x);
221 u64 max = idx_signed_max(
size);
223 if (y > max / 2_u64)
return true;
227 u64 y = idx_signed_abs(x);
228 u64 min = idx_signed_min_abs(
size);
230 if (y > min / 2_u64)
return true;
239 auto k = idx_shift_amount(
size, b);
242 if (
nuw && idx_shl_nuw(
size,
a, *k))
return {};
243 if (
nsw && idx_shl_nsw(
size,
a, *k))
return {};
245 return idx_mul_pow2(
size,
a, *k);
249 auto k = idx_shift_amount(
size, b);
251 return a / idx_pow2(*k);
255 auto k = idx_shift_amount(
size, b);
258 auto divisor = idx_pow2(*k);
259 auto x = idx_sext(
size,
a);
260 if (x >= 0)
return idx_from_signed(
size,
static_cast<s64>(
static_cast<u64>(x) / divisor));
262 auto q = (idx_signed_abs(x) + divisor - 1_u64) / divisor;
263 return idx_from_signed(
size, idx_neg(q));
268 const bool su = idx_sign(
size,
a);
269 const bool sv = idx_sign(
size, b);
280 return (
flags_t(
id) & rel) != 0;
283template<
class Id, Id
id>
284std::optional<u64> fold_idx(
u64 size,
u64 a,
u64 b, [[maybe_unused]]
bool nsw, [[maybe_unused]]
bool nuw) {
287 if constexpr (std::is_same_v<Id, wrap>) {
289 if (
nuw && idx_add_nuw(
size,
a, b))
return {};
290 if (
nsw && idx_add_nsw(
size,
a, b))
return {};
291 return idx_add(
size,
a, b);
294 if (
nuw && idx_sub_nuw(
size,
a, b))
return {};
295 if (
nsw && idx_sub_nsw(
size,
a, b))
return {};
296 return idx_sub(
size,
a, b);
299 if (
nuw && idx_mul_nuw(
size,
a, b))
return {};
300 if (
nsw && idx_mul_nsw(
size,
a, b))
return {};
301 return idx_mul(
size,
a, b);
307 static_assert(
false,
"missing wrap subtag");
310 }
else if constexpr (std::is_same_v<Id, shr>) {
311 if constexpr (
id ==
shr::a)
312 return idx_ashr(
size,
a, b);
313 else if constexpr (
id ==
shr::l)
314 return idx_lshr(
size,
a, b);
316 static_assert(
false,
"missing shr subtag");
318 }
else if constexpr (std::is_same_v<Id, div>) {
320 return idx_udiv(
size,
a, b);
323 return idx_urem(
size,
a, b);
326 if (idx_sdivrem_ub(
size,
a, b))
return {};
327 return idx_sdiv(
size,
a, b);
330 if (idx_sdivrem_ub(
size,
a, b))
return {};
331 return idx_srem(
size,
a, b);
334 static_assert(
false,
"missing div subtag");
337 }
else if constexpr (std::is_same_v<Id, icmp>) {
338 return u64(fold_icmp_idx<id>(
size,
a, b));
340 }
else if constexpr (std::is_same_v<Id, extrema>) {
342 return std::min(
a, b);
345 return std::max(
a, b);
348 return idx_slt(
size,
a, b) ?
a : b;
351 return idx_sgt(
size,
a, b) ?
a : b;
354 static_assert(
false,
"missing extrema subtag");
357 static_assert(
false,
"missing tag");
361template<
class Id, Id
id>
362const Def*
fold(World& world,
const Def* type,
const Def*&
a,
const Def*& b,
const Def*
mode = {}) {
363 if (
a->isa<
Bot>() || b->isa<
Bot>())
return world.bot(type);
367 assert(
a->type() == b->type());
371 bool nsw =
false,
nuw =
false;
372 if constexpr (std::is_same_v<Id, wrap>) {
379 if constexpr (std::is_same_v<Id, div>) {
380 if (*lb == 0)
return world.bot(type);
382 if constexpr (std::is_same_v<Id, icmp>)
383 return world.lit(type,
u64(fold_icmp_idx<id>(1, 0, 0)));
385 return world.lit(type, 0);
388 auto res = fold_idx<Id, id>(
size, *la, *lb,
nsw,
nuw);
389 return res ? world.lit(type, *res) : world.bot(type);
398const Def*
fold(World& world,
const Def* type,
const Def*&
a) {
399 if (
a->isa<
Bot>())
return world.bot(type);
404 if constexpr (std::is_same_v<Id, abs>) {
405 auto x = idx_sext(
size, *la);
406 if (x >= 0)
return world.lit(type,
static_cast<u64>(x));
408 auto y = idx_signed_abs(x);
409 if ((
size == 0 && x == std::numeric_limits<s64>::min())
410 || (
size % 2_u64 == 0 && y == idx_signed_min_abs(
size)))
411 return world.lit(type, *la);
413 return world.lit(type, y);
415 static_assert(
false,
"missing tag");
434const Def* reassociate(Id
id, World& world,
const Def*
a,
const Def* b) {
439 auto la =
a->isa<
Lit>();
440 auto [x, y] = xy ? xy->template args<2>() : std::array<const Def*, 2>{
nullptr,
nullptr};
441 auto [z,
w] = zw ? zw->template args<2>() : std::array<const Def*, 2>{
nullptr,
nullptr};
446 auto make_op = [&world,
id](
const Def*
a,
const Def* b) {
447 if constexpr (std::is_same_v<Id, nat>)
448 return world.call(
id,
Defs{a, b});
453 if (la && lz)
return make_op(make_op(
a, z), w);
454 if (lx && lz)
return make_op(make_op(x, z), make_op(y, w));
455 if (lz)
return make_op(z, make_op(
a, w));
456 if (lx)
return make_op(x, make_op(y, b));
461const Def* merge_cmps(std::array<std::array<u64, 2>, 2> tab,
const Def*
a,
const Def* b) {
462 static_assert(
sizeof(
sub_t) == 1,
"if this ever changes, please adjust the logic below");
463 static constexpr size_t num_bits = std::bit_width(
Annex::num<Id>() - 1_u64);
465 auto& world =
a->world();
469 if (a_cmp && b_cmp && a_cmp->arg() == b_cmp->arg()) {
472 sub_t a_sub = a_cmp.sub();
473 sub_t b_sub = b_cmp.sub();
474 for (
size_t i = 0; i != num_bits; ++i, res >>= 1, a_sub >>= 1, b_sub >>= 1)
475 res |= tab[a_sub & 1][b_sub & 1] << 7_u8;
476 res >>= (7_u8 -
u8(num_bits));
478 if constexpr (std::is_same_v<Id, math::cmp>)
479 return world.call(
math::cmp(res), a_cmp->decurry()->decurry()->arg(), a_cmp->arg());
491 auto& world = type->world();
492 auto [
a, b] = arg->
projs<2>();
500 case nat::add:
return world.lit_nat(*la + *lb);
501 case nat::sub:
return *la < *lb ? world.lit_nat_0() : world.lit_nat(*la - *lb);
502 case nat::mul:
return world.lit_nat(*la * *lb);
503 case nat::div:
return *lb == 0 ? world.lit_nat_0() : world.lit_nat(*la / *lb);
504 case nat::rem:
return *lb == 0 ?
a : world.lit_nat(*la % *lb);
513 case nat::div:
return world.lit_nat_0();
514 case nat::rem:
return world.lit_nat_0();
518 if (*la == 1 &&
id ==
nat::mul)
return b;
525 case nat::div:
return world.lit_nat_0();
533 case nat::rem:
return world.lit_nat_0();
542 const Def* marg = m->arg();
543 auto [c, x] = marg->
projs<2>();
544 if (
auto lc =
Lit::isa(c); lc && *lc != 0 && *lc % *lb == 0) {
545 if (
id ==
nat::rem)
return world.lit_nat_0();
546 return world.call(
nat::mul,
Defs{world.lit_nat(*lc / *lb), x});
554 case nat::sub:
return world.lit_nat(0);
557 case nat::rem:
return world.lit_nat_0();
561 if (
auto res = reassociate<nat>(
id, world,
a, b))
return res;
563 return world.raw_app(type, callee, {
a, b});
568 auto& world = type->world();
570 if (
id ==
ncmp::t)
return world.lit_tt();
571 if (
id ==
ncmp::f)
return world.lit_ff();
573 auto [
a, b] = arg->
projs<2>();
577 constexpr auto eq_mask = fe::to_underlying(
ncmp::e) & 0xff;
578 if ((fe::to_underlying(
id) & eq_mask) != 0)
return world.lit_tt();
579 if (
id ==
ncmp::ne)
return world.lit_ff();
586 case ncmp:: e:
return world.lit_bool(*la == *lb);
587 case ncmp::ne:
return world.lit_bool(*la != *lb);
588 case ncmp::l :
return world.lit_bool(*la < *lb);
589 case ncmp::le:
return world.lit_bool(*la <= *lb);
590 case ncmp::g :
return world.lit_bool(*la > *lb);
591 case ncmp::ge:
return world.lit_bool(*la >= *lb);
592 default: fe::unreachable();
598 return world.raw_app(type, callee, {
a, b});
603 auto& world = type->world();
604 auto callee = c->as<
App>();
605 auto [
a, b] = arg->
projs<2>();
607 if (
auto result = fold<icmp, id>(world, type,
a, b))
return result;
608 if (
id ==
icmp::f)
return world.lit_ff();
609 if (
id ==
icmp::t)
return world.lit_tt();
611 constexpr auto eq_mask = fe::to_underlying(
icmp::e) & 0xff;
612 if ((fe::to_underlying(
id) & eq_mask) != 0)
return world.lit_tt();
613 if (
id ==
icmp::ne)
return world.lit_ff();
616 return world.raw_app(type, callee, {
a, b});
621 auto& world = type->world();
622 auto callee = c->as<
App>();
623 auto [
a, b] = arg->
projs<2>();
624 if (
auto result = fold<extrema, id>(world, type,
a, b))
return result;
625 return world.raw_app(type, callee, {
a, b});
629 auto& world = type->world();
631 auto [_, actual_type] = type->projs<2>();
632 auto make_res = [&,
mem =
mem](
const Def* res) {
return world.tuple({
mem, res}); };
634 if (
auto result = fold<abs>(world, actual_type,
a))
return make_res(result);
640 auto& world = type->world();
641 auto callee = c->as<
App>();
642 auto s = callee->arg();
649 case bit1::f:
return world.lit_idx(*ls, 0);
650 case bit1::t:
return world.lit_idx(*ls, *ls - 1_u64);
656 if (
auto la =
Lit::isa(
a))
return world.lit_idx_mod(*ls, ~*la);
664 auto& world = type->world();
665 auto callee = c->as<
App>();
666 auto [
a, b] = arg->
projs<2>();
667 auto mode = callee->decurry()->arg();
668 auto s = callee->arg();
675 if (
auto res = merge_cmps<icmp>(tab,
a, b))
return res;
676 if (
auto res = merge_cmps<math::cmp>(tab,
a, b))
return res;
683 case bit2:: f:
return world.lit(type, 0);
684 case bit2:: t:
if (ls)
return world.lit(type, *ls-1_u64);
break;
685 case bit2:: fst:
return a;
686 case bit2:: snd:
return b;
694 if (la && lb && ls) {
696 case bit2::and_:
return world.lit_idx (*ls, *la & *lb);
697 case bit2:: or_:
return world.lit_idx (*ls, *la | *lb);
698 case bit2::xor_:
return world.lit_idx (*ls, *la ^ *lb);
699 case bit2::nand:
return world.lit_idx_mod(*ls, ~(*la & *lb));
700 case bit2:: nor:
return world.lit_idx_mod(*ls, ~(*la | *lb));
701 case bit2::nxor:
return world.lit_idx_mod(*ls, ~(*la ^ *lb));
702 case bit2:: iff:
return world.lit_idx_mod(*ls, ~ *la | *lb);
703 case bit2::niff:
return world.lit_idx (*ls, *la & ~*lb);
704 default: fe::unreachable();
709 auto unary = [&](
bool x,
bool y,
const Def*
a) ->
const Def* {
710 if (!x && !y)
return world.lit(type, 0);
711 if ( x && y)
return ls ? world.lit(type, *ls-1_u64) :
nullptr;
712 if (!x && y)
return a;
719 if (
auto res = unary(tab[0][0], tab[1][1],
a))
return res;
724 if (
auto res = unary(tab[0][0], tab[0][1], b))
return res;
725 }
else if (ls && *la == *ls - 1_u64) {
726 if (
auto res = unary(tab[1][0], tab[1][1], b))
return res;
732 if (
auto res = unary(tab[0][0], tab[1][0],
a))
return res;
733 }
else if (ls && *lb == *ls - 1_u64) {
734 if (
auto res = unary(tab[0][1], tab[1][1],
a))
return res;
738 if (
auto res = reassociate<bit2>(
id, world,
a, b))
return res;
740 return world.raw_app(type, callee, {
a, b});
744 auto& world = type->world();
745 auto callee = c->as<
App>();
748 if (*i < *
s)
return world.lit_idx(*
s, *i);
749 if (
auto m =
Lit::isa(callee->decurry()->arg()))
return *m ? world.bot(type) : world.lit_idx_mod(*
s, *i);
757 auto& world = arg->
world();
764 auto& world = type->world();
765 auto callee = c->as<
App>();
766 auto [
a, b] = arg->
projs<2>();
769 auto width = ls ? std::optional<nat_t>(idx_shift_width(*ls)) : std::optional<nat_t>();
771 if (
auto result = fold<shr, id>(world, type,
a, b))
return result;
773 if (
auto la =
Lit::isa(
a); la && *la == 0) {
781 if (width && *lb >= *width)
return world.bot(type);
791 return world.raw_app(type, callee, {
a, b});
796 auto& world = type->world();
797 auto callee = c->as<
App>();
798 auto [
a, b] = arg->
projs<2>();
799 auto mode = callee->decurry()->arg();
802 auto width = ls.transform(idx_shift_width);
804 if (
auto result = fold<wrap, id>(world, type,
a, b,
mode))
return result;
815 }
else if (*la == 1) {
830 default: fe::unreachable();
836 return world.call(
wrap::add,
mode,
Defs{a, world.lit_idx_mod(*ls, ~*lb + 1_u64)});
837 else if (
id ==
wrap::shl && width && *lb >= *width)
838 return world.bot(type);
844 case wrap::sub:
return world.lit(type, 0);
851 if (
auto res = reassociate<wrap>(
id, world,
a, b))
return res;
853 return world.raw_app(type, callee, {
a, b});
858 auto& world = full_type->
world();
860 auto [
a, b] = ab->projs<2>();
861 auto [_, type] = full_type->
projs<2>();
862 auto make_res = [&,
mem =
mem](
const Def* res) {
return world.tuple({
mem, res}); };
864 if (
auto result = fold<div, id>(world, type,
a, b))
return make_res(result);
867 if (*la == 0)
return make_res(
a);
871 if (*lb == 0)
return make_res(world.bot(type));
877 case div::srem:
return make_res(world.lit(type, 0));
878 case div::urem:
return make_res(world.lit(type, 0));
885 case div::sdiv:
return make_res(world.lit(type, 1));
886 case div::udiv:
return make_res(world.lit(type, 1));
887 case div::srem:
return make_res(world.lit(type, 0));
888 case div::urem:
return make_res(world.lit(type, 0));
897 auto& world = dst_t->
world();
898 auto s_t = x->
type()->as<
App>();
899 auto d_t = dst_t->as<
App>();
905 if (s_t == d_t)
return x;
906 if (x->isa<
Bot>())
return world.bot(d_t);
910 if (ld && *ld == 1)
return world.lit(d_t, 0);
911 if (ls && *ls == 1)
return world.lit(d_t, 0);
915 if (*ld == 0)
return world.lit(d_t, *
l);
916 return world.lit(d_t, *
l % *ld);
919 return world.lit(d_t, idx_from_signed_mod(*ld, idx_sext(*ls, *
l)));
926 if (*ls > *ls1 || *ls == 0)
927 if (*ld == *ls1)
return x1;
934 auto& world = dst_t->
world();
935 auto src_t = src->
type();
937 if (src->isa<
Bot>())
return world.bot(dst_t);
938 if (src_t == dst_t)
return src;
941 return other->arg()->
type() == dst_t ? other->arg() : world.call<
bitcast>(dst_t, other->arg());
944 if (dst_t->isa<
Nat>())
return world.lit(dst_t, *
l);
945 if (
Idx::isa(dst_t))
return world.lit(dst_t, *
l);
957 auto& world = type->world();
959 return world.lit_nat(8);
960 }
else if (type->isa<
Pi>()) {
961 return world.lit_nat(8);
962 }
else if (
auto size =
Idx::isa(type)) {
963 if (
auto w =
Idx::size2bitwidth(size))
return world.lit_nat(std::max(1_n, std::bit_ceil(*w) / 8_n));
966 case 16:
return world.lit_nat(2);
967 case 32:
return world.lit_nat(4);
968 case 64:
return world.lit_nat(8);
969 default: fe::unreachable();
971 }
else if (type->isa<
Sigma>()) {
974 for (
auto t : type->ops()) {
977 if (!
a || !
s)
return {};
980 offset = fe::pad(offset, *
a) + *
s;
983 offset = fe::pad(offset,
align);
984 u64 size = std::max(1_u64, offset);
990 }
else if (
auto arr = type->isa_imm<
Arr>()) {
992 auto elem = arr->elem();
993 if (!elem)
return {};
1005 auto& world = type->world();
1009 if (arg->
is_closed())
return world.lit_tt();
A (possibly paramterized) Array.
static auto isa(const Def *def)
World & world() const noexcept
auto projs(Projector auto 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).
static bool greater(const Def *a, const Def *b)
bool is_closed() const
Same as !has_free_vars().
static constexpr nat_t size2bitwidth(nat_t n)
static constexpr nat_t bitwidth2size(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 dependent function type.
const Lit * lit_idx_unsafe(u64 val)
#define MIM_core_NORMALIZER_IMPL
const Def * normalize_nat(const Def *type, const Def *callee, const Def *arg)
const Def * normalize_idx_unsafe(const Def *, const Def *, const Def *arg)
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)
constexpr flags_t icmp_mask
@ nuw
No Unsigned Wrap around.
@ nsw
No Signed Wrap around.
const Def * normalize_shr(const Def *type, const Def *c, const Def *arg)
std::optional< nat_t > isa_f(const Def *def)
fe::View< const Def * > Defs
constexpr bool is_commutative(Id)
constexpr bool is_associative(Id id)
static consteval size_t num()
static consteval flags_t base()