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);
281 return (
flags_t(
id) & rel) != 0;
284template<
class Id, Id
id>
285std::optional<u64> fold_idx(
u64 size,
u64 a,
u64 b, [[maybe_unused]]
bool nsw, [[maybe_unused]]
bool nuw) {
288 if constexpr (std::is_same_v<Id, wrap>) {
290 if (
nuw && idx_add_nuw(
size,
a, b))
return {};
291 if (
nsw && idx_add_nsw(
size,
a, b))
return {};
292 return idx_add(
size,
a, b);
295 if (
nuw && idx_sub_nuw(
size,
a, b))
return {};
296 if (
nsw && idx_sub_nsw(
size,
a, b))
return {};
297 return idx_sub(
size,
a, b);
300 if (
nuw && idx_mul_nuw(
size,
a, b))
return {};
301 if (
nsw && idx_mul_nsw(
size,
a, b))
return {};
302 return idx_mul(
size,
a, b);
308 static_assert(
false,
"missing wrap subtag");
311 }
else if constexpr (std::is_same_v<Id, shr>) {
312 if constexpr (
id ==
shr::a)
313 return idx_ashr(
size,
a, b);
314 else if constexpr (
id ==
shr::l)
315 return idx_lshr(
size,
a, b);
317 static_assert(
false,
"missing shr subtag");
319 }
else if constexpr (std::is_same_v<Id, div>) {
321 return idx_udiv(
size,
a, b);
324 return idx_urem(
size,
a, b);
327 if (idx_sdivrem_ub(
size,
a, b))
return {};
328 return idx_sdiv(
size,
a, b);
331 if (idx_sdivrem_ub(
size,
a, b))
return {};
332 return idx_srem(
size,
a, b);
335 static_assert(
false,
"missing div subtag");
338 }
else if constexpr (std::is_same_v<Id, icmp>) {
339 return u64(fold_icmp_idx<id>(
size,
a, b));
341 }
else if constexpr (std::is_same_v<Id, extrema>) {
343 return std::min(
a, b);
346 return std::max(
a, b);
349 return idx_slt(
size,
a, b) ?
a : b;
352 return idx_sgt(
size,
a, b) ?
a : b;
355 static_assert(
false,
"missing extrema subtag");
358 static_assert(
false,
"missing tag");
362template<
class Id, Id
id>
363const Def*
fold(World& world,
const Def* type,
const Def*&
a,
const Def*& b,
const Def*
mode = {}) {
364 if (
a->isa<
Bot>() || b->isa<
Bot>())
return world.bot(type);
368 assert(
a->type() == b->type());
372 bool nsw =
false,
nuw =
false;
373 if constexpr (std::is_same_v<Id, wrap>) {
380 if constexpr (std::is_same_v<Id, div>) {
381 if (*lb == 0)
return world.bot(type);
383 if constexpr (std::is_same_v<Id, icmp>)
384 return world.lit(type,
u64(fold_icmp_idx<id>(1, 0, 0)));
386 return world.lit(type, 0);
389 auto res = fold_idx<Id, id>(
size, *la, *lb,
nsw,
nuw);
390 return res ? world.lit(type, *res) : world.bot(type);
399const Def*
fold(World& world,
const Def* type,
const Def*&
a) {
400 if (
a->isa<
Bot>())
return world.bot(type);
405 if constexpr (std::is_same_v<Id, abs>) {
406 auto x = idx_sext(
size, *la);
407 if (x >= 0)
return world.lit(type,
static_cast<u64>(x));
409 auto y = idx_signed_abs(x);
410 if ((
size == 0 && x == std::numeric_limits<s64>::min())
411 || (
size % 2_u64 == 0 && y == idx_signed_min_abs(
size)))
412 return world.lit(type, *la);
414 return world.lit(type, y);
416 static_assert(
false,
"missing tag");
435const Def* reassociate(Id
id, World& world, [[maybe_unused]]
const App* ab,
const Def*
a,
const Def* b) {
440 auto la =
a->isa<
Lit>();
441 auto [x, y] = xy ? xy->template args<2>() : std::array<const Def*, 2>{
nullptr,
nullptr};
442 auto [z,
w] = zw ? zw->template args<2>() : std::array<const Def*, 2>{
nullptr,
nullptr};
447 auto make_op = [&world,
id](
const Def*
a,
const Def* b) {
return world.call(
id,
Mode::none,
Defs{
a, b}); };
449 if (la && lz)
return make_op(make_op(
a, z), w);
450 if (lx && lz)
return make_op(make_op(x, z), make_op(y, w));
451 if (lz)
return make_op(z, make_op(
a, w));
452 if (lx)
return make_op(x, make_op(y, b));
457const Def* merge_cmps(std::array<std::array<u64, 2>, 2> tab,
const Def*
a,
const Def* b) {
458 static_assert(
sizeof(
sub_t) == 1,
"if this ever changes, please adjust the logic below");
459 static constexpr size_t num_bits = std::bit_width(
Annex::num<Id>() - 1_u64);
461 auto& world =
a->world();
465 if (a_cmp && b_cmp && a_cmp->arg() == b_cmp->arg()) {
468 sub_t a_sub = a_cmp.sub();
469 sub_t b_sub = b_cmp.sub();
470 for (
size_t i = 0; i != num_bits; ++i, res >>= 1, a_sub >>= 1, b_sub >>= 1)
471 res |= tab[a_sub & 1][b_sub & 1] << 7_u8;
472 res >>= (7_u8 -
u8(num_bits));
474 if constexpr (std::is_same_v<Id, math::cmp>)
475 return world.call(
math::cmp(res), a_cmp->decurry()->arg(), a_cmp->arg());
487 auto& world = type->world();
488 auto [
a, b] = arg->
projs<2>();
496 case nat::add:
return world.lit_nat(*la + *lb);
497 case nat::sub:
return *la < *lb ? world.lit_nat_0() : world.lit_nat(*la - *lb);
498 case nat::mul:
return world.lit_nat(*la * *lb);
510 if (*la == 1 &&
id ==
nat::mul)
return b;
513 if (lb && *lb == 0 &&
id ==
nat::sub)
return a;
518 case nat::sub:
return world.lit_nat(0);
523 return world.raw_app(type, callee, {
a, b});
528 auto& world = type->world();
530 if (
id ==
ncmp::t)
return world.lit_tt();
531 if (
id ==
ncmp::f)
return world.lit_ff();
533 auto [
a, b] = arg->
projs<2>();
537 constexpr auto eq_mask = fe::to_underlying(
ncmp::e) & 0xff;
538 if ((fe::to_underlying(
id) & eq_mask) != 0)
return world.lit_tt();
539 if (
id ==
ncmp::ne)
return world.lit_ff();
546 case ncmp:: e:
return world.lit_bool(*la == *lb);
547 case ncmp::ne:
return world.lit_bool(*la != *lb);
548 case ncmp::l :
return world.lit_bool(*la < *lb);
549 case ncmp::le:
return world.lit_bool(*la <= *lb);
550 case ncmp::g :
return world.lit_bool(*la > *lb);
551 case ncmp::ge:
return world.lit_bool(*la >= *lb);
552 default: fe::unreachable();
558 return world.raw_app(type, callee, {
a, b});
563 auto& world = type->world();
564 auto callee = c->as<
App>();
565 auto [
a, b] = arg->
projs<2>();
567 if (
auto result = fold<icmp, id>(world, type,
a, b))
return result;
568 if (
id ==
icmp::f)
return world.lit_ff();
569 if (
id ==
icmp::t)
return world.lit_tt();
571 constexpr auto eq_mask = fe::to_underlying(
icmp::e) & 0xff;
572 if ((fe::to_underlying(
id) & eq_mask) != 0)
return world.lit_tt();
573 if (
id ==
icmp::ne)
return world.lit_ff();
576 return world.raw_app(type, callee, {
a, b});
581 auto& world = type->world();
582 auto callee = c->as<
App>();
583 auto [
a, b] = arg->
projs<2>();
584 if (
auto result = fold<extrema, id>(world, type,
a, b))
return result;
585 return world.raw_app(type, callee, {
a, b});
589 auto& world = type->world();
591 auto [_, actual_type] = type->projs<2>();
592 auto make_res = [&,
mem =
mem](
const Def* res) {
return world.tuple({
mem, res}); };
594 if (
auto result = fold<abs>(world, actual_type,
a))
return make_res(result);
600 auto& world = type->world();
601 auto callee = c->as<
App>();
609 case bit1::f:
return world.lit_idx(*ls, 0);
610 case bit1::t:
return world.lit_idx(*ls, *ls - 1_u64);
616 if (
auto la =
Lit::isa(
a))
return world.lit_idx_mod(*ls, ~*la);
624 auto& world = type->world();
625 auto callee = c->as<
App>();
626 auto [
a, b] = arg->
projs<2>();
627 auto s = callee->decurry()->arg();
634 if (
auto res = merge_cmps<icmp>(tab,
a, b))
return res;
635 if (
auto res = merge_cmps<math::cmp>(tab,
a, b))
return res;
642 case bit2:: f:
return world.lit(type, 0);
643 case bit2:: t:
if (ls)
return world.lit(type, *ls-1_u64);
break;
644 case bit2:: fst:
return a;
645 case bit2:: snd:
return b;
653 if (la && lb && ls) {
655 case bit2::and_:
return world.lit_idx (*ls, *la & *lb);
656 case bit2:: or_:
return world.lit_idx (*ls, *la | *lb);
657 case bit2::xor_:
return world.lit_idx (*ls, *la ^ *lb);
658 case bit2::nand:
return world.lit_idx_mod(*ls, ~(*la & *lb));
659 case bit2:: nor:
return world.lit_idx_mod(*ls, ~(*la | *lb));
660 case bit2::nxor:
return world.lit_idx_mod(*ls, ~(*la ^ *lb));
661 case bit2:: iff:
return world.lit_idx_mod(*ls, ~ *la | *lb);
662 case bit2::niff:
return world.lit_idx (*ls, *la & ~*lb);
663 default: fe::unreachable();
668 auto unary = [&](
bool x,
bool y,
const Def*
a) ->
const Def* {
669 if (!x && !y)
return world.lit(type, 0);
670 if ( x && y)
return ls ? world.lit(type, *ls-1_u64) :
nullptr;
671 if (!x && y)
return a;
678 if (
auto res = unary(tab[0][0], tab[1][1],
a))
return res;
683 if (
auto res = unary(tab[0][0], tab[0][1], b))
return res;
684 }
else if (ls && *la == *ls - 1_u64) {
685 if (
auto res = unary(tab[1][0], tab[1][1], b))
return res;
691 if (
auto res = unary(tab[0][0], tab[1][0],
a))
return res;
692 }
else if (ls && *lb == *ls - 1_u64) {
693 if (
auto res = unary(tab[0][1], tab[1][1],
a))
return res;
697 if (
auto res = reassociate<bit2>(
id, world, callee,
a, b))
return res;
699 return world.raw_app(type, callee, {
a, b});
703 auto& world = type->world();
704 auto callee = c->as<
App>();
707 if (*i < *
s)
return world.lit_idx(*
s, *i);
708 if (
auto m =
Lit::isa(callee->arg()))
return *m ? world.bot(type) : world.lit_idx_mod(*
s, *i);
716 auto& world = arg->
world();
723 auto& world = type->world();
724 auto callee = c->as<
App>();
725 auto [
a, b] = arg->
projs<2>();
728 auto width = ls ? std::optional<nat_t>(idx_shift_width(*ls)) : std::optional<nat_t>();
730 if (
auto result = fold<shr, id>(world, type,
a, b))
return result;
732 if (
auto la =
Lit::isa(
a); la && *la == 0) {
740 if (width && *lb >= *width)
return world.bot(type);
750 return world.raw_app(type, callee, {
a, b});
755 auto& world = type->world();
756 auto callee = c->as<
App>();
757 auto [
a, b] = arg->
projs<2>();
758 auto mode = callee->arg();
761 auto width = ls.transform(idx_shift_width);
763 if (
auto result = fold<wrap, id>(world, type,
a, b,
mode))
return result;
774 }
else if (*la == 1) {
789 default: fe::unreachable();
795 return world.call(
wrap::add,
mode,
Defs{a, world.lit_idx_mod(*ls, ~*lb + 1_u64)});
796 else if (
id ==
wrap::shl && width && *lb >= *width)
797 return world.bot(type);
803 case wrap::sub:
return world.lit(type, 0);
810 if (
auto res = reassociate<wrap>(
id, world, callee,
a, b))
return res;
812 return world.raw_app(type, callee, {
a, b});
817 auto& world = full_type->
world();
819 auto [
a, b] = ab->projs<2>();
820 auto [_, type] = full_type->
projs<2>();
821 auto make_res = [&,
mem =
mem](
const Def* res) {
return world.tuple({
mem, res}); };
823 if (
auto result = fold<div, id>(world, type,
a, b))
return make_res(result);
826 if (*la == 0)
return make_res(
a);
830 if (*lb == 0)
return make_res(world.bot(type));
836 case div::srem:
return make_res(world.lit(type, 0));
837 case div::urem:
return make_res(world.lit(type, 0));
844 case div::sdiv:
return make_res(world.lit(type, 1));
845 case div::udiv:
return make_res(world.lit(type, 1));
846 case div::srem:
return make_res(world.lit(type, 0));
847 case div::urem:
return make_res(world.lit(type, 0));
856 auto& world = dst_t->
world();
857 auto s_t = x->
type()->as<
App>();
858 auto d_t = dst_t->as<
App>();
864 if (s_t == d_t)
return x;
865 if (x->isa<
Bot>())
return world.bot(d_t);
869 if (*ld == 0)
return world.lit(d_t, *
l);
870 return world.lit(d_t, *
l % *ld);
873 return world.lit(d_t, idx_from_signed_mod(*ld, idx_sext(*ls, *
l)));
880 if (*ls > *ls1 || *ls == 0)
881 if (*ld == *ls1)
return x1;
888 auto& world = dst_t->
world();
889 auto src_t = src->
type();
891 if (src->isa<
Bot>())
return world.bot(dst_t);
892 if (src_t == dst_t)
return src;
895 return other->arg()->
type() == dst_t ? other->arg() : world.call<
bitcast>(dst_t, other->arg());
898 if (dst_t->isa<
Nat>())
return world.lit(dst_t, *
l);
899 if (
Idx::isa(dst_t))
return world.lit(dst_t, *
l);
911 auto& world = type->world();
913 return world.lit_nat(8);
914 }
else if (type->isa<
Pi>()) {
915 return world.lit_nat(8);
920 case 16:
return world.lit_nat(2);
921 case 32:
return world.lit_nat(4);
922 case 64:
return world.lit_nat(8);
923 default: fe::unreachable();
925 }
else if (type->isa<
Sigma>() || type->isa<
Meet>()) {
928 for (
auto t : type->ops()) {
931 if (!
a || !
s)
return {};
934 offset =
pad(offset, *
a) + *
s;
938 u64 size = std::max(1_u64, offset);
944 }
else if (
auto arr = type->isa_imm<
Arr>()) {
949 }
else if (
auto join = type->isa<
Join>()) {
958 auto& world = type->world();
962 if (arg->
is_closed())
return world.lit_tt();
const App * decurry() const
Returns App::callee again as App.
A (possibly paramterized) Array.
static auto isa(const Def *def)
World & world() 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).
static bool greater(const Def *a, const Def *b)
bool is_closed() const
Has no 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 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)
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)
float rem(float a, float b)
TBound< true > Join
AKA union.
constexpr bool is_commutative(Id)
constexpr std::uint64_t pad(std::uint64_t offset, std::uint64_t align) noexcept
constexpr bool is_associative(Id id)
TBound< false > Meet
AKA intersection.
static consteval size_t num()
static consteval flags_t base()