317 auto app = lam->
body()->as<
App>();
320 if (app->callee() ==
root()->ret_var()) {
321 std::vector<std::string> values;
322 std::vector<const Def*> types;
324 for (
auto arg : app->args()) {
326 values.emplace_back(val);
327 types.emplace_back(arg->type());
331 switch (values.size()) {
332 case 0:
return bb.tail(
"ret void");
333 case 1:
return bb.tail(
"ret {} {}", convert(types[0]), values[0]);
335 std::string prev =
"undef";
336 auto type = convert(
world().sigma(types));
337 for (
size_t i = 0, n = values.size(); i != n; ++i) {
338 auto v_elem = values[i];
339 auto t_elem = convert(types[i]);
340 auto namei =
"%ret_val." + std::to_string(i);
341 bb.tail(
"{} = insertvalue {} {}, {} {}, {}", namei, type, prev, t_elem, v_elem, i);
345 bb.tail(
"ret {} {}", type, prev);
348 }
else if (
auto dispatch =
Dispatch(app)) {
349 for (
auto callee : dispatch.tuple()->projs([](
const Def* def) { return def->isa_mut<Lam>(); })) {
350 size_t n = callee->num_tvars();
351 for (
size_t i = 0; i != n; ++i) {
352 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
353 auto phi = callee->var(n, i);
355 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
361 auto v_index =
emit(dispatch.index());
362 size_t n = dispatch.num_targets();
363 auto bbs = absl::FixedArray<std::string>(n);
364 for (
size_t i = 0; i != n; ++i)
365 bbs[i] =
emit(dispatch.target(i));
367 if (
auto branch =
Branch(app))
return bb.tail(
"br i1 {}, label {}, label {}", v_index, bbs[1], bbs[0]);
369 auto t_index = convert(dispatch.index()->type());
370 bb.tail(
"switch {} {}, label {} [ ", t_index, v_index, bbs[0]);
371 for (
size_t i = 1; i != n; ++i)
372 print(bb.tail().back(),
"{} {}, label {} ", t_index, std::to_string(i), bbs[i]);
373 print(bb.tail().back(),
"]");
374 }
else if (app->callee()->isa<
Bot>()) {
375 return bb.tail(
"ret ; bottom: unreachable");
377 size_t n = callee->num_tvars();
378 for (
size_t i = 0; i != n; ++i) {
379 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
380 auto phi = callee->var(n, i);
382 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
386 return bb.tail(
"br label {}",
id(callee));
388 declare(
"void @longjmp(i8*, i32) noreturn");
390 auto [mem, jbuf, tag] = app->args<3>();
392 auto v_jb =
emit(jbuf);
393 auto v_tag =
emit(tag);
394 bb.tail(
"call void @longjmp(i8* {}, i32 {})", v_jb, v_tag);
395 return bb.tail(
"unreachable");
397 auto v_callee =
emit(app->callee());
399 std::vector<std::string> args;
400 auto app_args = app->args();
401 for (
auto arg : app_args.view().rsubspan(1))
402 if (
auto v_arg =
emit_unsafe(arg); !v_arg.empty()) args.emplace_back(convert(arg->type()) +
" " + v_arg);
404 if (app->args().back()->isa<
Bot>()) {
406 assert(convert_ret_pi(app->callee_type()->ret_pi()) ==
"void");
407 bb.tail(
"call void {}({, })", v_callee, args);
408 return bb.tail(
"unreachable");
411 auto ret_lam = app->args().back()->as_mut<
Lam>();
412 size_t num_vars = ret_lam->
num_vars();
416 for (
auto var : ret_lam->vars()) {
419 types[n] = var->type();
424 bb.tail(
"call void {}({, })", v_callee, args);
426 auto name =
"%" + app->unique_name() +
"ret";
427 auto t_ret = convert_ret_pi(ret_lam->type());
428 bb.tail(
"{} = call {} {}({, })",
name, t_ret, v_callee, args);
430 for (
size_t i = 0, e = ret_lam->num_vars(); i != e; ++i) {
431 auto phi = ret_lam->var(i);
436 namei +=
'.' + std::to_string(i - 1);
437 bb.tail(
"{} = extractvalue {} {}, {}", namei, t_ret,
name, i - 1);
440 lam2bb_[ret_lam].phis[phi].emplace_back(namei,
id(lam,
true));
445 return bb.tail(
"br label {}",
id(ret_lam));
450 if (
auto lam = def->isa<
Lam>())
return id(lam);
455 auto emit_tuple = [&](
const Def* tuple) {
456 if (isa_mem_sigma_2(tuple->type())) {
458 return emit(tuple->proj(2, 1));
461 if (is_const(tuple)) {
462 bool is_array = tuple->type()->isa<
Arr>();
465 s += is_array ?
"[" :
"{";
467 for (
size_t i = 0, n = tuple->num_projs(); i != n; ++i) {
468 auto e = tuple->proj(n, i);
469 if (
auto v_elem =
emit_unsafe(e); !v_elem.empty()) {
470 auto t_elem = convert(e->type());
471 s += sep + t_elem +
" " + v_elem;
476 return s += is_array ?
"]" :
"}";
479 std::string prev =
"undef";
480 auto t = convert(tuple->type());
481 for (
size_t src = 0, dst = 0, n = tuple->num_projs(); src != n; ++src) {
482 auto e = tuple->proj(n, src);
484 auto elem_t = convert(e->type());
486 auto namei =
name +
"." + std::to_string(dst);
487 prev = bb.
assign(namei,
"insertvalue {} {}, {} {}, {}", t, prev, elem_t, elem, dst);
494 if (def->isa<
Var>()) {
496 if (std::ranges::any_of(ts, [](
auto t) {
return Axm::isa<mem::M>(t); }))
return {};
497 return emit_tuple(def);
500 auto emit_gep_index = [&](
const Def* index) {
501 auto v_i =
emit(index);
502 auto t_i = convert(index->type());
504 if (
auto size =
Idx::isa(index->type())) {
507 "zext {} {} to i{} ; add one more bit for gep index as it is treated as signed value",
509 t_i =
"i" + std::to_string(*w + 1);
513 return std::pair(v_i, t_i);
516 if (
auto lit = def->isa<
Lit>()) {
517 if (lit->type()->isa<
Nat>() ||
Idx::isa(lit->type())) {
518 return std::to_string(lit->get());
519 }
else if (
auto w = math::isa_f(lit->type())) {
525 s <<
"0xH" << std::setfill(
'0') << std::setw(4) << std::right << std::hex << lit->get<
u16>();
528 hex = std::bit_cast<u64>(
f64(lit->get<
f32>()));
531 case 64: hex = lit->get<
u64>();
break;
532 default: fe::unreachable();
535 s <<
"0x" << std::setfill(
'0') << std::setw(16) << std::right << std::hex << hex;
539 }
else if (def->isa<
Bot>()) {
541 }
else if (
auto top = def->isa<
Top>()) {
544 }
else if (
auto tuple = def->isa<
Tuple>()) {
545 return emit_tuple(tuple);
546 }
else if (
auto pack = def->isa<
Pack>()) {
547 if (
auto lit =
Lit::isa(pack->body()); lit && *lit == 0)
return "zeroinitializer";
548 return emit_tuple(pack);
549 }
else if (
auto sel =
Select(def)) {
550 auto t = convert(sel.extract()->type());
551 auto [elem_a, elem_b] = sel.pair()->projs<2>([&](
auto e) {
return emit_unsafe(e); });
552 auto cond_t = convert(sel.cond()->type());
553 auto cond =
emit(sel.cond());
554 return bb.
assign(
name,
"select {} {}, {} {}, {} {}", cond_t, cond, t, elem_b, t, elem_a);
555 }
else if (
auto extract = def->isa<
Extract>()) {
556 auto tuple = extract->tuple();
557 auto index = extract->index();
564 auto t_tup = convert(tuple->type());
566 if (isa_mem_sigma_2(tuple->type()))
return v_tup;
568 auto v_i =
Axm::isa<mem::M>(tuple->proj(0)->type()) ? std::to_string(*li - 1) : std::to_string(*li);
569 return bb.
assign(
name,
"extractvalue {} {}, {}", t_tup, v_tup, v_i);
572 auto t_elem = convert(extract->type());
573 auto [v_i, t_i] = emit_gep_index(index);
576 "{}.alloca = alloca {} ; copy to alloca to emulate extract with store + gep + load",
name, t_tup);
577 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup,
name);
578 print(bb.
body().emplace_back(),
"{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}",
name, t_tup,
579 t_tup,
name, t_i, v_i);
580 return bb.
assign(
name,
"load {}, {}* {}.gep", t_elem, t_elem,
name);
581 }
else if (
auto insert = def->isa<
Insert>()) {
583 auto t_tup = convert(insert->tuple()->type());
584 auto t_val = convert(insert->value()->type());
585 auto v_tup =
emit(insert->tuple());
586 auto v_val =
emit(insert->value());
587 if (
auto idx =
Lit::isa(insert->index())) {
588 auto v_idx =
emit(insert->index());
589 return bb.
assign(
name,
"insertvalue {} {}, {} {}, {}", t_tup, v_tup, t_val, v_val, v_idx);
591 auto t_elem = convert(insert->value()->type());
592 auto [v_i, t_i] = emit_gep_index(insert->index());
594 "{}.alloca = alloca {} ; copy to alloca to emulate insert with store + gep + load",
name, t_tup);
595 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup,
name);
596 print(bb.
body().emplace_back(),
"{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}",
name,
597 t_tup, t_tup,
name, t_i, v_i);
598 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.gep", t_val, v_val, t_val,
name);
599 return bb.
assign(
name,
"load {}, {}* {}.alloca", t_tup, t_tup,
name);
601 }
else if (
auto global = def->isa<
Global>()) {
602 auto v_init =
emit(global->init());
604 print(vars_decls_,
"{} = global {} {}\n",
name, convert(pointee), v_init);
607 auto [a, b] = nat->args<2>([
this](
auto def) {
return emit(def); });
610 case core::nat::add: op =
"add";
break;
611 case core::nat::sub: op =
"sub";
break;
612 case core::nat::mul: op =
"mul";
break;
615 return bb.
assign(
name,
"{} nsw nuw i64 {}, {}", op, a, b);
617 auto [a, b] = ncmp->args<2>([
this](
auto def) {
return emit(def); });
622 case core::ncmp::e: op +=
"eq" ;
break;
623 case core::ncmp::ne: op +=
"ne" ;
break;
624 case core::ncmp::g: op +=
"ugt";
break;
625 case core::ncmp::ge: op +=
"uge";
break;
626 case core::ncmp::l: op +=
"ult";
break;
627 case core::ncmp::le: op +=
"ule";
break;
629 default: fe::unreachable();
632 return bb.
assign(
name,
"{} i64 {}, {}", op, a, b);
634 auto x =
emit(idx->arg());
636 auto t = convert(idx->type());
637 if (s < 64)
return bb.
assign(
name,
"trunc i64 {} to {}", x, t);
640 assert(bit1.id() == core::bit1::neg);
641 auto x =
emit(bit1->arg());
642 auto t = convert(bit1->type());
643 return bb.
assign(
name,
"xor {} -1, {}", t, x);
645 auto [a, b] = bit2->args<2>([
this](
auto def) {
return emit(def); });
646 auto t = convert(bit2->type());
648 auto neg = [&](std::string_view x) {
return bb.
assign(
name +
".neg",
"xor {} -1, {}", t, x); };
652 case core::bit2::and_:
return bb.
assign(
name,
"and {} {}, {}", t, a, b);
653 case core::bit2:: or_:
return bb.
assign(
name,
"or {} {}, {}", t, a, b);
654 case core::bit2::xor_:
return bb.
assign(
name,
"xor {} {}, {}", t, a, b);
655 case core::bit2::nand:
return neg(bb.
assign(
name,
"and {} {}, {}", t, a, b));
656 case core::bit2:: nor:
return neg(bb.
assign(
name,
"or {} {}, {}", t, a, b));
657 case core::bit2::nxor:
return neg(bb.
assign(
name,
"xor {} {}, {}", t, a, b));
658 case core::bit2:: iff:
return bb.
assign(
name,
"and {} {}, {}", neg(a), b);
659 case core::bit2::niff:
return bb.
assign(
name,
"or {} {}, {}", neg(a), b);
661 default: fe::unreachable();
664 auto [a, b] = shr->args<2>([
this](
auto def) {
return emit(def); });
665 auto t = convert(shr->type());
668 case core::shr::a: op =
"ashr";
break;
669 case core::shr::l: op =
"lshr";
break;
672 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
674 auto [mode, ab] = wrap->uncurry_args<2>();
675 auto [a, b] = ab->projs<2>([
this](
auto def) {
return emit(def); });
676 auto t = convert(wrap->type());
680 case core::wrap::add: op =
"add";
break;
681 case core::wrap::sub: op =
"sub";
break;
682 case core::wrap::mul: op =
"mul";
break;
683 case core::wrap::shl: op =
"shl";
break;
686 if (lmode & core::Mode::nuw) op +=
" nuw";
687 if (lmode & core::Mode::nsw) op +=
" nsw";
689 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
691 auto [m, xy] = div->args<2>();
692 auto [x, y] = xy->projs<2>();
693 auto t = convert(x->type());
699 case core::div::sdiv: op =
"sdiv";
break;
700 case core::div::udiv: op =
"udiv";
break;
701 case core::div::srem: op =
"srem";
break;
702 case core::div::urem: op =
"urem";
break;
705 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
707 auto [a, b] = icmp->args<2>([
this](
auto def) {
return emit(def); });
708 auto t = convert(icmp->arg(0)->type());
713 case core::icmp::e: op +=
"eq" ;
break;
714 case core::icmp::ne: op +=
"ne" ;
break;
715 case core::icmp::sg: op +=
"sgt";
break;
716 case core::icmp::sge: op +=
"sge";
break;
717 case core::icmp::sl: op +=
"slt";
break;
718 case core::icmp::sle: op +=
"sle";
break;
719 case core::icmp::ug: op +=
"ugt";
break;
720 case core::icmp::uge: op +=
"uge";
break;
721 case core::icmp::ul: op +=
"ult";
break;
722 case core::icmp::ule: op +=
"ule";
break;
724 default: fe::unreachable();
727 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
729 auto [x, y] = extr->args<2>();
730 auto t = convert(x->type());
733 std::string f =
"llvm.";
735 case core::extrema::Sm: f +=
"smin.";
break;
736 case core::extrema::SM: f +=
"smax.";
break;
737 case core::extrema::sm: f +=
"umin.";
break;
738 case core::extrema::sM: f +=
"umax.";
break;
741 declare(
"{} @{}({}, {})", t, f, t, t);
742 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
744 auto [m, x] = abs->args<2>();
745 auto t = convert(x->type());
747 std::string f =
"llvm.abs." + t;
748 declare(
"{} @{}({}, {})", t, f, t,
"i1");
749 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a,
"i1",
"1");
751 auto v_src =
emit(conv->arg());
752 auto t_src = convert(conv->arg()->type());
753 auto t_dst = convert(conv->type());
758 if (w_src == w_dst)
return v_src;
761 case core::conv::s: op = w_src < w_dst ?
"sext" :
"trunc";
break;
762 case core::conv::u: op = w_src < w_dst ?
"zext" :
"trunc";
break;
765 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
770 auto t_src = convert(
bitcast->arg()->type());
771 auto t_dst = convert(
bitcast->type());
773 if (
auto lit =
Lit::isa(
bitcast->arg()); lit && *lit == 0)
return "zeroinitializer";
775 if (src_type_ptr && dst_type_ptr)
return bb.
assign(
name,
"bitcast {} {} to {}", t_src, v_src, t_dst);
776 if (src_type_ptr)
return bb.
assign(
name,
"ptrtoint {} {} to {}", t_src, v_src, t_dst);
777 if (dst_type_ptr)
return bb.
assign(
name,
"inttoptr {} {} to {}", t_src, v_src, t_dst);
780 auto size2width = [&](
const Def* type) {
781 if (type->isa<
Nat>())
return 64_n;
786 auto src_size = size2width(
bitcast->arg()->type());
787 auto dst_size = size2width(
bitcast->type());
790 if (src_size && dst_size) {
791 if (src_size == dst_size)
return v_src;
792 op = (src_size < dst_size) ?
"zext" :
"trunc";
794 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
796 auto [ptr, i] = lea->args<2>();
798 auto v_ptr =
emit(ptr);
799 auto t_pointee = convert(pointee);
800 auto t_ptr = convert(ptr->type());
801 if (pointee->isa<
Sigma>())
802 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, i32 {}", t_pointee, t_ptr, v_ptr,
805 assert(pointee->isa<
Arr>());
806 auto [v_i, t_i] = emit_gep_index(i);
808 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, {} {}", t_pointee, t_ptr, v_ptr, t_i, v_i);
813 auto size =
emit(malloc->arg(1));
815 bb.
assign(
name +
"i8",
"call i8* @malloc(i64 {})", size);
816 return bb.
assign(
name,
"bitcast i8* {} to {}",
name +
"i8", ptr_t);
820 auto ptr =
emit(free->arg(1));
823 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", ptr_t, ptr);
824 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
827 auto [Ta, msi] = mslot->uncurry_args<2>();
828 auto [pointee, addr_space] = Ta->projs<2>();
829 auto [mem, _, __] = msi->projs<3>();
833 print(bb.
body().emplace_back(),
"{} = alloca {}",
name, convert(pointee));
839 auto v_ptr =
emit(free->arg(1));
842 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", t_ptr, v_ptr);
843 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
847 auto v_ptr =
emit(load->arg(1));
848 auto t_ptr = convert(load->arg(1)->type());
850 return bb.
assign(
name,
"load {}, {} {}", t_pointee, t_ptr, v_ptr);
853 auto v_ptr =
emit(store->arg(1));
854 auto v_val =
emit(store->arg(2));
855 auto t_ptr = convert(store->arg(1)->type());
856 auto t_val = convert(store->arg(2)->type());
857 print(bb.
body().emplace_back(),
"store {} {}, {} {}", t_val, v_val, t_ptr, v_ptr);
863 auto size =
name +
".size";
864 bb.
assign(size,
"call i64 @jmpbuf_size()");
865 return bb.
assign(
name,
"alloca i8, i64 {}", size);
867 declare(
"i32 @_setjmp(i8*) returns_twice");
869 auto [mem, jmpbuf] = setjmp->arg()->projs<2>();
871 auto v_jb =
emit(jmpbuf);
872 return bb.
assign(
name,
"call i32 @_setjmp(i8* {})", v_jb);
874 auto [mode, ab] = arith->uncurry_args<2>();
875 auto [a, b] = ab->projs<2>([
this](
auto def) {
return emit(def); });
876 auto t = convert(arith->type());
879 switch (arith.id()) {
880 case math::arith::add: op =
"fadd";
break;
881 case math::arith::sub: op =
"fsub";
break;
882 case math::arith::mul: op =
"fmul";
break;
883 case math::arith::div: op =
"fdiv";
break;
884 case math::arith::rem: op =
"frem";
break;
887 if (lmode == math::Mode::fast)
891 if (lmode & math::Mode::nnan ) op +=
" nnan";
892 if (lmode & math::Mode::ninf ) op +=
" ninf";
893 if (lmode & math::Mode::nsz ) op +=
" nsz";
894 if (lmode & math::Mode::arcp ) op +=
" arcp";
895 if (lmode & math::Mode::contract) op +=
" contract";
896 if (lmode & math::Mode::afn ) op +=
" afn";
897 if (lmode & math::Mode::reassoc ) op +=
" reassoc";
901 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
903 auto a =
emit(tri->arg());
904 auto t = convert(tri->type());
908 if (tri.id() == math::tri::sin) {
909 f =
"llvm.sin"s + llvm_suffix(tri->type());
910 }
else if (tri.id() == math::tri::cos) {
911 f =
"llvm.cos"s + llvm_suffix(tri->type());
913 if (tri.sub() &
sub_t(math::tri::a)) f +=
"a";
916 case math::tri::sin: f +=
"sin";
break;
917 case math::tri::cos: f +=
"cos";
break;
918 case math::tri::tan: f +=
"tan";
break;
919 case math::tri::ahFF:
error(
"this axm is supposed to be unused");
920 default: fe::unreachable();
923 if (tri.sub() &
sub_t(math::tri::h)) f +=
"h";
924 f += math_suffix(tri->type());
927 declare(
"{} @{}({})", t, f, t);
928 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
930 auto [a, b] = extrema->args<2>([
this](
auto def) {
return emit(def); });
931 auto t = convert(extrema->type());
932 std::string f =
"llvm.";
933 switch (extrema.id()) {
934 case math::extrema::fmin: f +=
"minnum";
break;
935 case math::extrema::fmax: f +=
"maxnum";
break;
936 case math::extrema::ieee754min: f +=
"minimum";
break;
937 case math::extrema::ieee754max: f +=
"maximum";
break;
939 f += llvm_suffix(extrema->type());
941 declare(
"{} @{}({}, {})", t, f, t, t);
942 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
944 auto [a, b] = pow->args<2>([
this](
auto def) {
return emit(def); });
945 auto t = convert(pow->type());
946 std::string f =
"llvm.pow";
947 f += llvm_suffix(pow->type());
948 declare(
"{} @{}({}, {})", t, f, t, t);
949 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
951 auto a =
emit(rt->arg());
952 auto t = convert(rt->type());
954 if (rt.id() == math::rt::sq)
955 f =
"llvm.sqrt"s + llvm_suffix(rt->type());
957 f =
"cbrt"s += math_suffix(rt->type());
958 declare(
"{} @{}({})", t, f, t);
959 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
961 auto a =
emit(exp->arg());
962 auto t = convert(exp->type());
963 std::string f =
"llvm.";
964 f += (exp.sub() &
sub_t(math::exp::log)) ?
"log" :
"exp";
965 f += (exp.sub() &
sub_t(math::exp::bin)) ?
"2" : (exp.sub() &
sub_t(math::exp::dec)) ?
"10" :
"";
966 f += llvm_suffix(exp->type());
968 declare(
"{} @{}({})", t, f, t);
969 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
971 auto a =
emit(er->arg());
972 auto t = convert(er->type());
973 auto f = er.id() == math::er::f ?
"erf"s :
"erfc"s;
974 f += math_suffix(er->type());
975 declare(
"{} @{}({})", t, f, t);
976 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
978 auto a =
emit(gamma->arg());
979 auto t = convert(gamma->type());
980 std::string f = gamma.id() == math::gamma::t ?
"tgamma" :
"lgamma";
981 f += math_suffix(gamma->type());
982 declare(
"{} @{}({})", t, f, t);
983 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
985 auto [a, b] = cmp->args<2>([
this](
auto def) {
return emit(def); });
986 auto t = convert(cmp->arg(0)->type());
991 case math::cmp:: e: op +=
"oeq";
break;
992 case math::cmp:: l: op +=
"olt";
break;
993 case math::cmp:: le: op +=
"ole";
break;
994 case math::cmp:: g: op +=
"ogt";
break;
995 case math::cmp:: ge: op +=
"oge";
break;
996 case math::cmp:: ne: op +=
"one";
break;
997 case math::cmp:: o: op +=
"ord";
break;
998 case math::cmp:: u: op +=
"uno";
break;
999 case math::cmp:: ue: op +=
"ueq";
break;
1000 case math::cmp:: ul: op +=
"ult";
break;
1001 case math::cmp::ule: op +=
"ule";
break;
1002 case math::cmp:: ug: op +=
"ugt";
break;
1003 case math::cmp::uge: op +=
"uge";
break;
1004 case math::cmp::une: op +=
"une";
break;
1006 default: fe::unreachable();
1009 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
1011 auto v_src =
emit(conv->arg());
1012 auto t_src = convert(conv->arg()->type());
1013 auto t_dst = convert(conv->type());
1015 auto s_src = math::isa_f(conv->arg()->type());
1016 auto s_dst = math::isa_f(conv->type());
1018 switch (conv.id()) {
1019 case math::conv::f2f: op = s_src < s_dst ?
"fpext" :
"fptrunc";
break;
1020 case math::conv::s2f: op =
"sitofp";
break;
1021 case math::conv::u2f: op =
"uitofp";
break;
1022 case math::conv::f2s: op =
"fptosi";
break;
1023 case math::conv::f2u: op =
"fptoui";
break;
1026 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
1028 auto a =
emit(abs->arg());
1029 auto t = convert(abs->type());
1030 std::string f =
"llvm.fabs";
1031 f += llvm_suffix(abs->type());
1032 declare(
"{} @{}({})", t, f, t);
1033 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1035 auto a =
emit(round->arg());
1036 auto t = convert(round->type());
1037 std::string f =
"llvm.";
1038 switch (round.id()) {
1039 case math::round::f: f +=
"floor";
break;
1040 case math::round::c: f +=
"ceil";
break;
1041 case math::round::r: f +=
"round";
break;
1042 case math::round::t: f +=
"trunc";
break;
1044 f += llvm_suffix(round->type());
1045 declare(
"{} @{}({})", t, f, t);
1046 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1048 error(
"unhandled def in LLVM backend: {} : {}", def, def->
type());