369 auto app = lam->
body()->as<
App>();
371 if (app->callee() ==
root()->ret_var()) {
372 std::vector<std::string> values;
373 std::vector<const Def*> types;
374 for (
auto arg : app->args()) {
376 values.emplace_back(val);
377 types.emplace_back(arg->type());
381 switch (values.size()) {
382 case 0:
return bb.tail(
"ret void");
383 case 1:
return bb.tail(
"ret {} {}", convert(types[0]), values[0]);
391 auto v_src =
emit(common_src);
392 auto t = convert(common_src->type());
393 return bb.tail(
"ret {} {}", t, v_src);
395 auto [size, elem] = *se;
396 auto val_t = convert(elem);
398 type = std::format(
"<{} x {}>", size, val_t);
399 for (
auto val : values) {
404 prev += std::format(
"{} {}", val_t, val);
409 type = convert(
world().sigma(types));
410 for (
size_t i = 0, n = values.size(); i != n; ++i) {
411 auto v_elem = values[i];
412 auto t_elem = convert(types[i]);
413 auto namei =
"%ret_val." + std::to_string(i);
414 bb.tail(
"{} = insertvalue {} {}, {} {}, {}", namei, type, prev, t_elem, v_elem, i);
418 bb.tail(
"ret {} {}", type, prev);
422 }
else if (
auto dispatch =
Dispatch(app)) {
423 for (
auto callee : dispatch.tuple()->projs([](
const Def* def) { return def->isa_mut<Lam>(); })) {
424 size_t n = callee->num_tvars();
425 if (n == 1 &&
is_simd(callee->var(0)->type())) {
426 auto phi = callee->var(0);
427 auto arg =
emit(app->arg(n, 0));
428 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
431 for (
size_t i = 0; i != n; ++i) {
432 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
433 auto phi = callee->var(n, i);
435 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
442 auto v_index =
emit(dispatch.index());
443 size_t n = dispatch.num_targets();
444 auto bbs = absl::FixedArray<std::string>(n);
445 for (
size_t i = 0; i != n; ++i)
446 bbs[i] =
emit(dispatch.target(i));
448 if (
auto branch =
Branch(app))
return bb.tail(
"br i1 {}, label {}, label {}", v_index, bbs[1], bbs[0]);
450 auto t_index = convert(dispatch.index()->type());
451 bb.tail(
"switch {} {}, label {} [ ", t_index, v_index, bbs[0]);
452 for (
size_t i = 1; i != n; ++i)
453 print(bb.tail().back(),
"{} {}, label {} ", t_index, std::to_string(i), bbs[i]);
454 print(bb.tail().back(),
"]");
455 }
else if (app->callee()->isa<
Bot>()) {
456 return bb.tail(
"ret ; bottom: unreachable");
461 auto v_src =
emit(common_src);
462 auto callee_var = callee->var();
463 if (simd_phi_.find(callee) == simd_phi_.end()) simd_phi_[callee] = callee_var;
464 auto key = simd_phi_[callee];
465 lam2bb_[callee].phis[key].emplace_back(v_src,
id(lam,
true));
467 for (
auto var : callee->vars())
471 size_t n = callee->num_tvars();
472 for (
size_t i = 0; i != n; ++i) {
473 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
474 auto phi = callee->var(n, i);
476 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
481 return bb.tail(
"br label {}",
id(callee));
484 declare(
"void @longjmp(i8*, i32) noreturn");
486 auto [mem, jbuf, tag] = app->args<3>();
488 auto v_jb =
emit(jbuf);
489 auto v_tag =
emit(tag);
490 bb.tail(
"call void @longjmp(i8* {}, i32 {})", v_jb, v_tag);
491 return bb.tail(
"unreachable");
493 auto v_callee =
emit(app->callee());
495 std::vector<std::string> args;
496 auto app_args = app->args();
497 for (
auto arg : app_args.view().rsubspan(1))
498 if (
auto v_arg =
emit_unsafe(arg); !v_arg.empty()) args.emplace_back(convert(arg->type()) +
" " + v_arg);
500 if (app->args().back()->isa<
Bot>()) {
502 assert(convert_ret_pi(app->callee_type()->ret_pi()) ==
"void");
503 bb.tail(
"call void {}({, })", v_callee, args);
504 return bb.tail(
"unreachable");
507 auto ret_lam = app->args().back()->as_mut<
Lam>();
508 size_t num_vars = ret_lam->
num_vars();
512 for (
auto var : ret_lam->vars()) {
515 types[n] = var->type();
520 bb.tail(
"call void {}({, })", v_callee, args);
522 auto name =
"%" + app->unique_name() +
"ret";
523 auto t_ret = convert_ret_pi(ret_lam->type());
524 bb.tail(
"{} = call {} {}({, })",
name, t_ret, v_callee, args);
525 auto phi = ret_lam->
var();
526 lam2bb_[ret_lam].phis[phi].emplace_back(
name,
id(lam,
true));
530 return bb.tail(
"br label {}",
id(ret_lam));
535 if (
auto lam = def->isa<
Lam>())
return id(lam);
540 auto emit_tuple = [&](
const Def* tuple) {
541 if (isa_mem_sigma_2(tuple->type())) {
543 return emit(tuple->proj(2, 1));
546 if (tuple->is_closed()) {
547 bool is_array = tuple->type()->isa<
Arr>();
548 auto simd_array = convert(tuple->type()).front() ==
'<';
550 s += simd_array ?
"<" : is_array ?
"[" :
"{";
552 for (
size_t i = 0, n = tuple->num_projs(); i != n; ++i) {
553 auto e = tuple->proj(n, i);
554 if (
auto v_elem =
emit_unsafe(e); !v_elem.empty()) {
555 auto t_elem = convert(e->type());
556 s += sep + t_elem +
" " + v_elem;
561 return s += simd_array ?
">" : is_array ?
"]" :
"}";
564 std::string prev =
"undef";
565 auto t = convert(tuple->type());
566 for (
size_t src = 0, dst = 0, n = tuple->num_projs(); src != n; ++src) {
567 auto e = tuple->proj(n, src);
569 auto elem_t = convert(e->type());
571 auto namei =
name +
"." + std::to_string(dst);
572 if (t.front() ==
'<')
573 prev = bb.
assign(namei,
"insertelement {} {}, {} {}, {} {}", t, prev, elem_t, elem, elem_t, dst);
575 prev = bb.
assign(namei,
"insertvalue {} {}, {} {}, {}", t, prev, elem_t, elem, dst);
582 if (def->isa<
Var>()) {
585 if (std::ranges::any_of(ts, [](
auto t) {
return Axm::isa<mem::M>(t); }))
return {};
586 return emit_tuple(def);
589 auto emit_gep_index = [&](
const Def* index) {
590 auto v_i =
emit(index);
591 auto t_i = convert(index->type());
593 if (
auto size =
Idx::isa(index->type())) {
596 "zext {} {} to i{} ; add one more bit for gep index as it is treated as signed value",
598 t_i =
"i" + std::to_string(*w + 1);
602 return std::pair(v_i, t_i);
605 if (
auto lit = def->isa<
Lit>()) {
606 if (lit->type()->isa<
Nat>() ||
Idx::isa(lit->type())) {
607 return std::to_string(lit->get());
608 }
else if (
auto w = math::isa_f(lit->type())) {
614 s <<
"0xH" << std::setfill(
'0') << std::setw(4) << std::right << std::hex << lit->get<
u16>();
617 hex = std::bit_cast<u64>(
f64(lit->get<
f32>()));
620 case 64: hex = lit->get<
u64>();
break;
621 default: fe::unreachable();
624 s <<
"0x" << std::setfill(
'0') << std::setw(16) << std::right << std::hex << hex;
628 }
else if (def->isa<
Bot>()) {
630 }
else if (
auto top = def->isa<
Top>()) {
633 }
else if (
auto tuple = def->isa<
Tuple>()) {
634 return emit_tuple(tuple);
635 }
else if (
auto pack = def->isa<
Pack>()) {
636 if (
auto lit =
Lit::isa(pack->body()); lit && *lit == 0)
return "zeroinitializer";
637 return emit_tuple(pack);
638 }
else if (
auto sel =
Select(def)) {
639 auto t = convert(sel.extract()->type());
640 auto [elem_a, elem_b] = sel.pair()->projs<2>([&](
auto e) {
return emit_unsafe(e); });
641 auto cond_t = convert(sel.cond()->type());
642 auto cond =
emit(sel.cond());
643 return bb.
assign(
name,
"select {} {}, {} {}, {} {}", cond_t, cond, t, elem_b, t, elem_a);
644 }
else if (
auto extract = def->isa<
Extract>()) {
645 auto tuple = extract->tuple();
646 auto index = extract->index();
654 auto t_tup = convert(tuple->type());
656 if (isa_mem_sigma_2(tuple->type()))
return v_tup;
658 auto v_i =
Axm::isa<mem::M>(tuple->proj(0)->type()) ? std::to_string(*li - 1) : std::to_string(*li);
660 return bb.
assign(
name,
"extractvalue {} {}, {}", t_tup, v_tup, v_i);
663 auto t_elem = convert(extract->type());
664 auto [v_i, t_i] = emit_gep_index(index);
667 "{}.alloca = alloca {} ; copy to alloca to emulate extract with store + gep + load",
name, t_tup);
668 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup,
name);
669 print(bb.
body().emplace_back(),
"{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}",
name, t_tup,
670 t_tup,
name, t_i, v_i);
671 return bb.
assign(
name,
"load {}, {}* {}.gep", t_elem, t_elem,
name);
672 }
else if (
auto insert = def->isa<
Insert>()) {
674 auto t_tup = convert(insert->tuple()->type());
675 auto t_val = convert(insert->value()->type());
676 auto v_tup =
emit(insert->tuple());
677 auto v_val =
emit(insert->value());
678 if (
auto idx =
Lit::isa(insert->index())) {
679 auto v_idx =
emit(insert->index());
680 if (
is_simd(insert->tuple()->type()))
682 return bb.
assign(
name,
"insertelement {} {}, {} {}, i32 {}", t_tup, v_tup, t_val, v_val, v_idx);
685 return bb.
assign(
name,
" insertvalue {} {}, {} {}, {}", t_tup, v_tup, t_val, v_val, v_idx);
687 if (
is_simd(insert->tuple()->type())) {
688 auto v_i =
emit(insert->index());
689 auto t_i = convert(insert->index()->type());
692 v_i = bb.
assign(
name +
".idx",
"{} {} {} to i32", w_src < 32 ?
"zext" :
"trunc", t_i, v_i);
694 return bb.
assign(
name,
"insertelement {} {}, {} {}, i32 {}", t_tup, v_tup, t_val, v_val, v_i);
696 auto t_elem = convert(insert->value()->type());
697 auto [v_i, t_i] = emit_gep_index(insert->index());
699 "{}.alloca = alloca {} ; copy to alloca to emulate insert with store + gep + load",
name, t_tup);
700 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup,
name);
701 print(bb.
body().emplace_back(),
"{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}",
name,
702 t_tup, t_tup,
name, t_i, v_i);
703 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.gep", t_val, v_val, t_val,
name);
704 return bb.
assign(
name,
"load {}, {}* {}.alloca", t_tup, t_tup,
name);
706 }
else if (
auto global = def->isa<
Global>()) {
707 auto v_init =
emit(global->init());
709 print(vars_decls_,
"{} = global {} {}\n",
name, convert(pointee), v_init);
712 auto [a, b] = nat->args<2>([
this](
auto def) {
return emit(def); });
715 case core::nat::add: op =
"add";
break;
716 case core::nat::sub: op =
"sub";
break;
717 case core::nat::mul: op =
"mul";
break;
720 return bb.
assign(
name,
"{} nsw nuw i64 {}, {}", op, a, b);
722 auto [a, b] = ncmp->args<2>([
this](
auto def) {
return emit(def); });
727 case core::ncmp::e: op +=
"eq" ;
break;
728 case core::ncmp::ne: op +=
"ne" ;
break;
729 case core::ncmp::g: op +=
"ugt";
break;
730 case core::ncmp::ge: op +=
"uge";
break;
731 case core::ncmp::l: op +=
"ult";
break;
732 case core::ncmp::le: op +=
"ule";
break;
734 default: fe::unreachable();
737 return bb.
assign(
name,
"{} i64 {}, {}", op, a, b);
739 auto x =
emit(idx->arg());
741 auto t = convert(idx->type());
742 if (s < 64)
return bb.
assign(
name,
"trunc i64 {} to {}", x, t);
745 assert(bit1.id() == core::bit1::neg);
746 auto x =
emit(bit1->arg());
747 auto t = convert(bit1->type());
748 return bb.
assign(
name,
"xor {} -1, {}", t, x);
750 auto [a, b] = bit2->args<2>([
this](
auto def) {
return emit(def); });
751 auto t = convert(bit2->type());
753 auto neg = [&](std::string_view x) {
return bb.
assign(
name +
".neg",
"xor {} -1, {}", t, x); };
757 case core::bit2::and_:
return bb.
assign(
name,
"and {} {}, {}", t, a, b);
758 case core::bit2:: or_:
return bb.
assign(
name,
"or {} {}, {}", t, a, b);
759 case core::bit2::xor_:
return bb.
assign(
name,
"xor {} {}, {}", t, a, b);
760 case core::bit2::nand:
return neg(bb.
assign(
name,
"and {} {}, {}", t, a, b));
761 case core::bit2:: nor:
return neg(bb.
assign(
name,
"or {} {}, {}", t, a, b));
762 case core::bit2::nxor:
return neg(bb.
assign(
name,
"xor {} {}, {}", t, a, b));
763 case core::bit2:: iff:
return bb.
assign(
name,
"and {} {}, {}", neg(a), b);
764 case core::bit2::niff:
return bb.
assign(
name,
"or {} {}, {}", neg(a), b);
766 default: fe::unreachable();
769 auto [a, b] = shr->args<2>([
this](
auto def) {
return emit(def); });
770 auto t = convert(shr->type());
773 case core::shr::a: op =
"ashr";
break;
774 case core::shr::l: op =
"lshr";
break;
777 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
779 auto [mode, ab] = wrap->uncurry_args<2>();
780 auto [a, b] = ab->projs<2>([
this](
auto def) {
return emit(def); });
781 auto t = convert(wrap->type());
785 case core::wrap::add: op =
"add";
break;
786 case core::wrap::sub: op =
"sub";
break;
787 case core::wrap::mul: op =
"mul";
break;
788 case core::wrap::shl: op =
"shl";
break;
791 if (lmode & core::Mode::nuw) op +=
" nuw";
792 if (lmode & core::Mode::nsw) op +=
" nsw";
794 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
796 auto [m, xy] = div->args<2>();
797 auto [x, y] = xy->projs<2>();
798 auto t = convert(x->type());
804 case core::div::sdiv: op =
"sdiv";
break;
805 case core::div::udiv: op =
"udiv";
break;
806 case core::div::srem: op =
"srem";
break;
807 case core::div::urem: op =
"urem";
break;
810 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
812 auto [a, b] = icmp->args<2>([
this](
auto def) {
return emit(def); });
813 auto t = convert(icmp->arg(0)->type());
818 case core::icmp::e: op +=
"eq" ;
break;
819 case core::icmp::ne: op +=
"ne" ;
break;
820 case core::icmp::sg: op +=
"sgt";
break;
821 case core::icmp::sge: op +=
"sge";
break;
822 case core::icmp::sl: op +=
"slt";
break;
823 case core::icmp::sle: op +=
"sle";
break;
824 case core::icmp::ug: op +=
"ugt";
break;
825 case core::icmp::uge: op +=
"uge";
break;
826 case core::icmp::ul: op +=
"ult";
break;
827 case core::icmp::ule: op +=
"ule";
break;
829 default: fe::unreachable();
832 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
834 auto [x, y] = extr->args<2>();
835 auto t = convert(x->type());
838 std::string f =
"llvm.";
840 case core::extrema::Sm: f +=
"smin.";
break;
841 case core::extrema::SM: f +=
"smax.";
break;
842 case core::extrema::sm: f +=
"umin.";
break;
843 case core::extrema::sM: f +=
"umax.";
break;
846 declare(
"{} @{}({}, {})", t, f, t, t);
847 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
849 auto [m, x] = abs->args<2>();
850 auto t = convert(x->type());
852 std::string f =
"llvm.abs." + t;
853 declare(
"{} @{}({}, {})", t, f, t,
"i1");
854 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a,
"i1",
"1");
856 auto v_src =
emit(conv->arg());
857 auto t_src = convert(conv->arg()->type());
858 auto t_dst = convert(conv->type());
863 if (w_src == w_dst)
return v_src;
866 case core::conv::s: op = w_src < w_dst ?
"sext" :
"trunc";
break;
867 case core::conv::u: op = w_src < w_dst ?
"zext" :
"trunc";
break;
870 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
875 auto t_src = convert(
bitcast->arg()->type());
876 auto t_dst = convert(
bitcast->type());
878 if (
auto lit =
Lit::isa(
bitcast->arg()); lit && *lit == 0)
return "zeroinitializer";
880 if (src_type_ptr && dst_type_ptr)
return bb.
assign(
name,
"bitcast {} {} to {}", t_src, v_src, t_dst);
881 if (src_type_ptr)
return bb.
assign(
name,
"ptrtoint {} {} to {}", t_src, v_src, t_dst);
882 if (dst_type_ptr)
return bb.
assign(
name,
"inttoptr {} {} to {}", t_src, v_src, t_dst);
885 auto size2width = [&](
const Def* type) {
886 if (type->isa<
Nat>())
return 64_n;
891 auto src_size = size2width(
bitcast->arg()->type());
892 auto dst_size = size2width(
bitcast->type());
895 if (src_size && dst_size) {
896 if (src_size == dst_size)
return v_src;
897 op = (src_size < dst_size) ?
"zext" :
"trunc";
899 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
901 auto [ptr, i] = lea->args<2>();
903 auto v_ptr =
emit(ptr);
904 auto t_pointee = convert(pointee);
905 auto t_ptr = convert(ptr->type());
906 if (pointee->isa<
Sigma>())
907 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, i32 {}", t_pointee, t_ptr, v_ptr,
910 assert(pointee->isa<
Arr>());
911 auto [v_i, t_i] = emit_gep_index(i);
913 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, {} {}", t_pointee, t_ptr, v_ptr, t_i, v_i);
918 auto size =
emit(malloc->arg(1));
920 bb.
assign(
name +
"i8",
"call i8* @malloc(i64 {})", size);
921 return bb.
assign(
name,
"bitcast i8* {} to {}",
name +
"i8", ptr_t);
925 auto ptr =
emit(free->arg(1));
928 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", ptr_t, ptr);
929 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
932 auto [Ta, msi] = mslot->uncurry_args<2>();
933 auto [pointee, addr_space] = Ta->projs<2>();
934 auto [mem, _, __] = msi->projs<3>();
938 print(bb.
body().emplace_back(),
"{} = alloca {}",
name, convert(pointee,
false));
944 auto v_ptr =
emit(free->arg(1));
947 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", t_ptr, v_ptr);
948 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
952 auto v_ptr =
emit(load->arg(1));
953 auto t_ptr = convert(load->arg(1)->type());
954 auto t_pointee = convert(
Axm::as<mem::Ptr>(load->arg(1)->type())->arg(0),
false);
955 return bb.
assign(
name,
"load {}, {} {}", t_pointee, t_ptr, v_ptr);
958 auto v_ptr =
emit(store->arg(1));
959 auto v_val =
emit(store->arg(2));
960 auto t_ptr = convert(store->arg(1)->type());
961 auto t_val = convert(store->arg(2)->type(),
false);
962 print(bb.
body().emplace_back(),
"store {} {}, {} {}", t_val, v_val, t_ptr, v_ptr);
968 auto size =
name +
".size";
969 bb.
assign(size,
"call i64 @jmpbuf_size()");
970 return bb.
assign(
name,
"alloca i8, i64 {}", size);
972 declare(
"i32 @_setjmp(i8*) returns_twice");
974 auto [mem, jmpbuf] = setjmp->arg()->projs<2>();
976 auto v_jb =
emit(jmpbuf);
977 return bb.
assign(
name,
"call i32 @_setjmp(i8* {})", v_jb);
979 auto [mode, ab] = arith->uncurry_args<2>();
980 auto [a, b] = ab->projs<2>([
this](
auto def) {
return emit(def); });
981 auto t = convert(arith->type());
984 switch (arith.id()) {
985 case math::arith::add: op =
"fadd";
break;
986 case math::arith::sub: op =
"fsub";
break;
987 case math::arith::mul: op =
"fmul";
break;
988 case math::arith::div: op =
"fdiv";
break;
989 case math::arith::rem: op =
"frem";
break;
992 if (lmode == math::Mode::fast)
996 if (lmode & math::Mode::nnan ) op +=
" nnan";
997 if (lmode & math::Mode::ninf ) op +=
" ninf";
998 if (lmode & math::Mode::nsz ) op +=
" nsz";
999 if (lmode & math::Mode::arcp ) op +=
" arcp";
1000 if (lmode & math::Mode::contract) op +=
" contract";
1001 if (lmode & math::Mode::afn ) op +=
" afn";
1002 if (lmode & math::Mode::reassoc ) op +=
" reassoc";
1006 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
1008 auto a =
emit(tri->arg());
1009 auto t = convert(tri->type());
1013 if (tri.id() == math::tri::sin) {
1014 f =
"llvm.sin"s + llvm_suffix(tri->type());
1015 }
else if (tri.id() == math::tri::cos) {
1016 f =
"llvm.cos"s + llvm_suffix(tri->type());
1018 if (tri.sub() &
sub_t(math::tri::a)) f +=
"a";
1021 case math::tri::sin: f +=
"sin";
break;
1022 case math::tri::cos: f +=
"cos";
break;
1023 case math::tri::tan: f +=
"tan";
break;
1024 case math::tri::ahFF:
error(
"this axm is supposed to be unused");
1025 default: fe::unreachable();
1028 if (tri.sub() &
sub_t(math::tri::h)) f +=
"h";
1029 f += math_suffix(tri->type());
1032 declare(
"{} @{}({})", t, f, t);
1033 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1035 auto [a, b] = extrema->args<2>([
this](
auto def) {
return emit(def); });
1036 auto t = convert(extrema->type());
1037 std::string f =
"llvm.";
1038 switch (extrema.id()) {
1039 case math::extrema::fmin: f +=
"minnum";
break;
1040 case math::extrema::fmax: f +=
"maxnum";
break;
1041 case math::extrema::ieee754min: f +=
"minimum";
break;
1042 case math::extrema::ieee754max: f +=
"maximum";
break;
1044 f += llvm_suffix(extrema->type());
1046 declare(
"{} @{}({}, {})", t, f, t, t);
1047 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
1049 auto [a, b] = pow->args<2>([
this](
auto def) {
return emit(def); });
1050 auto t = convert(pow->type());
1051 std::string f =
"llvm.pow";
1052 f += llvm_suffix(pow->type());
1053 declare(
"{} @{}({}, {})", t, f, t, t);
1054 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
1056 auto a =
emit(rt->arg());
1057 auto t = convert(rt->type());
1059 if (rt.id() == math::rt::sq)
1060 f =
"llvm.sqrt"s + llvm_suffix(rt->type());
1062 f =
"cbrt"s += math_suffix(rt->type());
1063 declare(
"{} @{}({})", t, f, t);
1064 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1066 auto a =
emit(exp->arg());
1067 auto t = convert(exp->type());
1068 std::string f =
"llvm.";
1069 f += (exp.sub() &
sub_t(math::exp::log)) ?
"log" :
"exp";
1070 f += (exp.sub() &
sub_t(math::exp::bin)) ?
"2" : (exp.sub() &
sub_t(math::exp::dec)) ?
"10" :
"";
1071 f += llvm_suffix(exp->type());
1073 declare(
"{} @{}({})", t, f, t);
1074 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1076 auto a =
emit(er->arg());
1077 auto t = convert(er->type());
1078 auto f = er.id() == math::er::f ?
"erf"s :
"erfc"s;
1079 f += math_suffix(er->type());
1080 declare(
"{} @{}({})", t, f, t);
1081 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1083 auto a =
emit(gamma->arg());
1084 auto t = convert(gamma->type());
1085 std::string f = gamma.id() == math::gamma::t ?
"tgamma" :
"lgamma";
1086 f += math_suffix(gamma->type());
1087 declare(
"{} @{}({})", t, f, t);
1088 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1090 auto [a, b] = cmp->args<2>([
this](
auto def) {
return emit(def); });
1091 auto t = convert(cmp->arg(0)->type());
1096 case math::cmp:: e: op +=
"oeq";
break;
1097 case math::cmp:: l: op +=
"olt";
break;
1098 case math::cmp:: le: op +=
"ole";
break;
1099 case math::cmp:: g: op +=
"ogt";
break;
1100 case math::cmp:: ge: op +=
"oge";
break;
1101 case math::cmp:: ne: op +=
"one";
break;
1102 case math::cmp:: o: op +=
"ord";
break;
1103 case math::cmp:: u: op +=
"uno";
break;
1104 case math::cmp:: ue: op +=
"ueq";
break;
1105 case math::cmp:: ul: op +=
"ult";
break;
1106 case math::cmp::ule: op +=
"ule";
break;
1107 case math::cmp:: ug: op +=
"ugt";
break;
1108 case math::cmp::uge: op +=
"uge";
break;
1109 case math::cmp::une: op +=
"une";
break;
1111 default: fe::unreachable();
1114 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
1116 auto v_src =
emit(conv->arg());
1117 auto t_src = convert(conv->arg()->type());
1118 auto t_dst = convert(conv->type());
1120 auto s_src = math::isa_f(conv->arg()->type());
1121 auto s_dst = math::isa_f(conv->type());
1123 switch (conv.id()) {
1124 case math::conv::f2f: op = s_src < s_dst ?
"fpext" :
"fptrunc";
break;
1125 case math::conv::s2f: op =
"sitofp";
break;
1126 case math::conv::u2f: op =
"uitofp";
break;
1127 case math::conv::f2s: op =
"fptosi";
break;
1128 case math::conv::f2u: op =
"fptoui";
break;
1131 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
1133 auto a =
emit(abs->arg());
1134 auto t = convert(abs->type());
1135 std::string f =
"llvm.fabs";
1136 f += llvm_suffix(abs->type());
1137 declare(
"{} @{}({})", t, f, t);
1138 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1140 auto a =
emit(round->arg());
1141 auto t = convert(round->type());
1142 std::string f =
"llvm.";
1143 switch (round.id()) {
1144 case math::round::f: f +=
"floor";
break;
1145 case math::round::c: f +=
"ceil";
break;
1146 case math::round::r: f +=
"round";
break;
1147 case math::round::t: f +=
"trunc";
break;
1149 f += llvm_suffix(round->type());
1150 declare(
"{} @{}({})", t, f, t);
1151 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1153 auto ni_n = zip->decurry()->decurry()->decurry()->arg();
1154 auto nat_ni = *
Lit::isa(ni_n->proj(2, 0));
1155 auto f = zip->decurry()->arg();
1156 auto inputs = zip->arg();
1157 auto t_in = convert(inputs->proj(nat_ni, 0)->type());
1158 auto t_out = convert(def->
type());
1164 switch (nat_op.id()) {
1165 case core::nat::add: op =
"add nuw nsw";
break;
1166 case core::nat::sub: op =
"sub nuw nsw";
break;
1167 case core::nat::mul: op =
"mul nuw nsw";
break;
1171 switch (arith_op.id()) {
1172 case math::arith::add: op =
"fadd";
break;
1173 case math::arith::sub: op =
"fsub";
break;
1174 case math::arith::mul: op =
"fmul";
break;
1175 case math::arith::div: op =
"fdiv";
break;
1176 case math::arith::rem: op =
"frem";
break;
1179 if (lmode == math::Mode::fast)
1182 if (lmode & math::Mode::nnan) op +=
" nnan";
1183 if (lmode & math::Mode::ninf) op +=
" ninf";
1184 if (lmode & math::Mode::nsz) op +=
" nsz";
1185 if (lmode & math::Mode::arcp) op +=
" arcp";
1186 if (lmode & math::Mode::contract) op +=
" contract";
1187 if (lmode & math::Mode::afn) op +=
" afn";
1188 if (lmode & math::Mode::reassoc) op +=
" reassoc";
1192 switch (ncmp_op.id()) {
1193 case core::ncmp::e: op +=
"eq";
break;
1194 case core::ncmp::ne: op +=
"ne";
break;
1195 case core::ncmp::g: op +=
"ugt";
break;
1196 case core::ncmp::ge: op +=
"uge";
break;
1197 case core::ncmp::l: op +=
"ult";
break;
1198 case core::ncmp::le: op +=
"ule";
break;
1199 default: fe::unreachable();
1203 switch (icmp_op.id()) {
1204 case core::icmp::e: op +=
"eq";
break;
1205 case core::icmp::ne: op +=
"ne";
break;
1206 case core::icmp::sg: op +=
"sgt";
break;
1207 case core::icmp::sge: op +=
"sge";
break;
1208 case core::icmp::sl: op +=
"slt";
break;
1209 case core::icmp::sle: op +=
"sle";
break;
1210 case core::icmp::ug: op +=
"ugt";
break;
1211 case core::icmp::uge: op +=
"uge";
break;
1212 case core::icmp::ul: op +=
"ult";
break;
1213 case core::icmp::ule: op +=
"ule";
break;
1214 default: fe::unreachable();
1218 switch (mcmp_op.id()) {
1219 case math::cmp::e: op +=
"oeq";
break;
1220 case math::cmp::l: op +=
"olt";
break;
1221 case math::cmp::le: op +=
"ole";
break;
1222 case math::cmp::g: op +=
"ogt";
break;
1223 case math::cmp::ge: op +=
"oge";
break;
1224 case math::cmp::ne: op +=
"one";
break;
1225 case math::cmp::o: op +=
"ord";
break;
1226 case math::cmp::u: op +=
"uno";
break;
1227 case math::cmp::ue: op +=
"ueq";
break;
1228 case math::cmp::ul: op +=
"ult";
break;
1229 case math::cmp::ule: op +=
"ule";
break;
1230 case math::cmp::ug: op +=
"ugt";
break;
1231 case math::cmp::uge: op +=
"uge";
break;
1232 case math::cmp::une: op +=
"une";
break;
1233 default: fe::unreachable();
1236 error(
"unhandled vec.zip operation: {}", f);
1239 auto v1 =
emit(inputs->proj(nat_ni, 0));
1240 auto v2 =
emit(inputs->proj(nat_ni, 1));
1241 prev = bb.
assign(
name,
"{} {} {}, {}", op, t_in, v1, v2);
1243 error(
"unhandled vec.zip operation: {}", f);
1245 error(
"unhandled def in LLVM backend: {} : {}", def, def->
type());