304 auto app = lam->
body()->as<
App>();
307 if (app->callee() ==
root()->ret_var()) {
308 std::vector<std::string> values;
309 std::vector<const Def*> types;
311 for (
auto arg : app->args()) {
313 values.emplace_back(val);
314 types.emplace_back(arg->type());
318 switch (values.size()) {
319 case 0:
return bb.tail(
"ret void");
320 case 1:
return bb.tail(
"ret {} {}", convert(types[0]), values[0]);
322 std::string prev =
"undef";
323 auto type = convert(
world().sigma(types));
324 for (
size_t i = 0, n = values.size(); i != n; ++i) {
325 auto v_elem = values[i];
326 auto t_elem = convert(types[i]);
327 auto namei =
"%ret_val." + std::to_string(i);
328 bb.tail(
"{} = insertvalue {} {}, {} {}, {}", namei, type, prev, t_elem, v_elem, i);
332 bb.tail(
"ret {} {}", type, prev);
335 }
else if (
auto dispatch =
Dispatch(app)) {
336 for (
auto callee : dispatch.tuple()->projs([](
const Def* def) { return def->isa_mut<Lam>(); })) {
337 size_t n = callee->num_tvars();
338 for (
size_t i = 0; i != n; ++i) {
339 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
340 auto phi = callee->var(n, i);
342 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
348 auto v_index =
emit(dispatch.index());
349 size_t n = dispatch.num_targets();
350 auto bbs = absl::FixedArray<std::string>(n);
351 for (
size_t i = 0; i != n; ++i)
352 bbs[i] =
emit(dispatch.target(i));
354 if (
auto branch =
Branch(app))
return bb.tail(
"br i1 {}, label {}, label {}", v_index, bbs[1], bbs[0]);
356 auto t_index = convert(dispatch.index()->type());
357 bb.tail(
"switch {} {}, label {} [ ", t_index, v_index, bbs[0]);
358 for (
size_t i = 1; i != n; ++i)
359 print(bb.tail().back(),
"{} {}, label {} ", t_index, std::to_string(i), bbs[i]);
360 print(bb.tail().back(),
"]");
361 }
else if (app->callee()->isa<
Bot>()) {
362 return bb.tail(
"ret ; bottom: unreachable");
364 size_t n = callee->num_tvars();
365 for (
size_t i = 0; i != n; ++i) {
366 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
367 auto phi = callee->var(n, i);
369 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
373 return bb.tail(
"br label {}",
id(callee));
375 declare(
"void @longjmp(i8*, i32) noreturn");
377 auto [mem, jbuf, tag] = app->args<3>();
379 auto v_jb =
emit(jbuf);
380 auto v_tag =
emit(tag);
381 bb.tail(
"call void @longjmp(i8* {}, i32 {})", v_jb, v_tag);
382 return bb.tail(
"unreachable");
384 auto v_callee =
emit(app->callee());
386 std::vector<std::string> args;
387 auto app_args = app->args();
388 for (
auto arg : app_args.view().rsubspan(1))
389 if (
auto v_arg =
emit_unsafe(arg); !v_arg.empty()) args.emplace_back(convert(arg->type()) +
" " + v_arg);
391 if (app->args().back()->isa<
Bot>()) {
393 assert(convert_ret_pi(app->callee_type()->ret_pi()) ==
"void");
394 bb.tail(
"call void {}({, })", v_callee, args);
395 return bb.tail(
"unreachable");
398 auto ret_lam = app->args().back()->as_mut<
Lam>();
399 size_t num_vars = ret_lam->
num_vars();
403 for (
auto var : ret_lam->vars()) {
406 types[n] = var->type();
411 bb.tail(
"call void {}({, })", v_callee, args);
413 auto name =
"%" + app->unique_name() +
"ret";
414 auto t_ret = convert_ret_pi(ret_lam->type());
415 bb.tail(
"{} = call {} {}({, })",
name, t_ret, v_callee, args);
417 for (
size_t i = 0, j = 0, e = ret_lam->num_vars(); i != e; ++i) {
418 auto phi = ret_lam->var(i);
423 namej +=
'.' + std::to_string(j);
424 bb.tail(
"{} = extractvalue {} {}, {}", namej, t_ret,
name, j);
427 lam2bb_[ret_lam].phis[phi].emplace_back(namej,
id(lam,
true));
433 return bb.tail(
"br label {}",
id(ret_lam));
438 if (
auto lam = def->isa<
Lam>())
return id(lam);
443 auto emit_tuple = [&](
const Def* tuple) {
444 if (isa_mem_sigma_2(tuple->type())) {
446 return emit(tuple->proj(2, 1));
449 if (tuple->is_closed()) {
450 bool is_array = tuple->type()->isa<
Arr>();
453 s += is_array ?
"[" :
"{";
455 for (
size_t i = 0, n = tuple->num_projs(); i != n; ++i) {
456 auto e = tuple->proj(n, i);
457 if (
auto v_elem =
emit_unsafe(e); !v_elem.empty()) {
458 auto t_elem = convert(e->type());
459 s += sep + t_elem +
" " + v_elem;
464 return s += is_array ?
"]" :
"}";
467 std::string prev =
"undef";
468 auto t = convert(tuple->type());
469 for (
size_t src = 0, dst = 0, n = tuple->num_projs(); src != n; ++src) {
470 auto e = tuple->proj(n, src);
472 auto elem_t = convert(e->type());
474 auto namei =
name +
"." + std::to_string(dst);
475 prev = bb.
assign(namei,
"insertvalue {} {}, {} {}, {}", t, prev, elem_t, elem, dst);
482 if (def->isa<
Var>()) {
484 if (std::ranges::any_of(ts, [](
auto t) {
return Axm::isa<mem::M>(t); }))
return {};
485 return emit_tuple(def);
488 auto emit_gep_index = [&](
const Def* index) {
489 auto v_i =
emit(index);
490 auto t_i = convert(index->type());
492 if (
auto size =
Idx::isa(index->type())) {
495 "zext {} {} to i{} ; add one more bit for gep index as it is treated as signed value",
497 t_i =
"i" + std::to_string(*w + 1);
501 return std::pair(v_i, t_i);
504 if (
auto lit = def->isa<
Lit>()) {
505 if (lit->type()->isa<
Nat>() ||
Idx::isa(lit->type())) {
506 return std::to_string(lit->get());
507 }
else if (
auto w = math::isa_f(lit->type())) {
513 s <<
"0xH" << std::setfill(
'0') << std::setw(4) << std::right << std::hex << lit->get<
u16>();
516 hex = std::bit_cast<u64>(
f64(lit->get<
f32>()));
519 case 64: hex = lit->get<
u64>();
break;
520 default: fe::unreachable();
523 s <<
"0x" << std::setfill(
'0') << std::setw(16) << std::right << std::hex << hex;
527 }
else if (def->isa<
Bot>()) {
529 }
else if (
auto top = def->isa<
Top>()) {
532 }
else if (
auto tuple = def->isa<
Tuple>()) {
533 return emit_tuple(tuple);
534 }
else if (
auto pack = def->isa<
Pack>()) {
535 if (
auto lit =
Lit::isa(pack->body()); lit && *lit == 0)
return "zeroinitializer";
536 return emit_tuple(pack);
537 }
else if (
auto sel =
Select(def)) {
538 auto t = convert(sel.extract()->type());
539 auto [elem_a, elem_b] = sel.pair()->projs<2>([&](
auto e) {
return emit_unsafe(e); });
540 auto cond_t = convert(sel.cond()->type());
541 auto cond =
emit(sel.cond());
542 return bb.
assign(
name,
"select {} {}, {} {}, {} {}", cond_t, cond, t, elem_b, t, elem_a);
543 }
else if (
auto extract = def->isa<
Extract>()) {
544 auto tuple = extract->tuple();
545 auto index = extract->index();
552 auto t_tup = convert(tuple->type());
554 if (isa_mem_sigma_2(tuple->type()))
return v_tup;
556 auto v_i =
Axm::isa<mem::M>(tuple->proj(0)->type()) ? std::to_string(*li - 1) : std::to_string(*li);
557 return bb.
assign(
name,
"extractvalue {} {}, {}", t_tup, v_tup, v_i);
560 auto t_elem = convert(extract->type());
561 auto [v_i, t_i] = emit_gep_index(index);
564 "{}.alloca = alloca {} ; copy to alloca to emulate extract with store + gep + load",
name, t_tup);
565 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup,
name);
566 print(bb.
body().emplace_back(),
"{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}",
name, t_tup,
567 t_tup,
name, t_i, v_i);
568 return bb.
assign(
name,
"load {}, {}* {}.gep", t_elem, t_elem,
name);
569 }
else if (
auto insert = def->isa<
Insert>()) {
571 auto t_tup = convert(insert->tuple()->type());
572 auto t_val = convert(insert->value()->type());
573 auto v_tup =
emit(insert->tuple());
574 auto v_val =
emit(insert->value());
575 if (
auto idx =
Lit::isa(insert->index())) {
576 auto v_idx =
emit(insert->index());
577 return bb.
assign(
name,
"insertvalue {} {}, {} {}, {}", t_tup, v_tup, t_val, v_val, v_idx);
579 auto t_elem = convert(insert->value()->type());
580 auto [v_i, t_i] = emit_gep_index(insert->index());
582 "{}.alloca = alloca {} ; copy to alloca to emulate insert with store + gep + load",
name, t_tup);
583 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup,
name);
584 print(bb.
body().emplace_back(),
"{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}",
name,
585 t_tup, t_tup,
name, t_i, v_i);
586 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.gep", t_val, v_val, t_val,
name);
587 return bb.
assign(
name,
"load {}, {}* {}.alloca", t_tup, t_tup,
name);
589 }
else if (
auto global = def->isa<
Global>()) {
590 auto v_init =
emit(global->init());
592 print(vars_decls_,
"{} = global {} {}\n",
name, convert(pointee), v_init);
595 auto [a, b] = nat->args<2>([
this](
auto def) {
return emit(def); });
598 case core::nat::add: op =
"add";
break;
599 case core::nat::sub: op =
"sub";
break;
600 case core::nat::mul: op =
"mul";
break;
603 return bb.
assign(
name,
"{} nsw nuw i64 {}, {}", op, a, b);
605 auto [a, b] = ncmp->args<2>([
this](
auto def) {
return emit(def); });
610 case core::ncmp::e: op +=
"eq" ;
break;
611 case core::ncmp::ne: op +=
"ne" ;
break;
612 case core::ncmp::g: op +=
"ugt";
break;
613 case core::ncmp::ge: op +=
"uge";
break;
614 case core::ncmp::l: op +=
"ult";
break;
615 case core::ncmp::le: op +=
"ule";
break;
617 default: fe::unreachable();
620 return bb.
assign(
name,
"{} i64 {}, {}", op, a, b);
622 auto x =
emit(idx->arg());
624 auto t = convert(idx->type());
625 if (s < 64)
return bb.
assign(
name,
"trunc i64 {} to {}", x, t);
628 assert(bit1.id() == core::bit1::neg);
629 auto x =
emit(bit1->arg());
630 auto t = convert(bit1->type());
631 return bb.
assign(
name,
"xor {} -1, {}", t, x);
633 auto [a, b] = bit2->args<2>([
this](
auto def) {
return emit(def); });
634 auto t = convert(bit2->type());
636 auto neg = [&](std::string_view x) {
return bb.
assign(
name +
".neg",
"xor {} -1, {}", t, x); };
640 case core::bit2::and_:
return bb.
assign(
name,
"and {} {}, {}", t, a, b);
641 case core::bit2:: or_:
return bb.
assign(
name,
"or {} {}, {}", t, a, b);
642 case core::bit2::xor_:
return bb.
assign(
name,
"xor {} {}, {}", t, a, b);
643 case core::bit2::nand:
return neg(bb.
assign(
name,
"and {} {}, {}", t, a, b));
644 case core::bit2:: nor:
return neg(bb.
assign(
name,
"or {} {}, {}", t, a, b));
645 case core::bit2::nxor:
return neg(bb.
assign(
name,
"xor {} {}, {}", t, a, b));
646 case core::bit2:: iff:
return bb.
assign(
name,
"and {} {}, {}", neg(a), b);
647 case core::bit2::niff:
return bb.
assign(
name,
"or {} {}, {}", neg(a), b);
649 default: fe::unreachable();
652 auto [a, b] = shr->args<2>([
this](
auto def) {
return emit(def); });
653 auto t = convert(shr->type());
656 case core::shr::a: op =
"ashr";
break;
657 case core::shr::l: op =
"lshr";
break;
660 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
662 auto [mode, ab] = wrap->uncurry_args<2>();
663 auto [a, b] = ab->projs<2>([
this](
auto def) {
return emit(def); });
664 auto t = convert(wrap->type());
668 case core::wrap::add: op =
"add";
break;
669 case core::wrap::sub: op =
"sub";
break;
670 case core::wrap::mul: op =
"mul";
break;
671 case core::wrap::shl: op =
"shl";
break;
674 if (lmode & core::Mode::nuw) op +=
" nuw";
675 if (lmode & core::Mode::nsw) op +=
" nsw";
677 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
679 auto [m, xy] = div->args<2>();
680 auto [x, y] = xy->projs<2>();
681 auto t = convert(x->type());
687 case core::div::sdiv: op =
"sdiv";
break;
688 case core::div::udiv: op =
"udiv";
break;
689 case core::div::srem: op =
"srem";
break;
690 case core::div::urem: op =
"urem";
break;
693 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
695 auto [a, b] = icmp->args<2>([
this](
auto def) {
return emit(def); });
696 auto t = convert(icmp->arg(0)->type());
701 case core::icmp::e: op +=
"eq" ;
break;
702 case core::icmp::ne: op +=
"ne" ;
break;
703 case core::icmp::sg: op +=
"sgt";
break;
704 case core::icmp::sge: op +=
"sge";
break;
705 case core::icmp::sl: op +=
"slt";
break;
706 case core::icmp::sle: op +=
"sle";
break;
707 case core::icmp::ug: op +=
"ugt";
break;
708 case core::icmp::uge: op +=
"uge";
break;
709 case core::icmp::ul: op +=
"ult";
break;
710 case core::icmp::ule: op +=
"ule";
break;
712 default: fe::unreachable();
715 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
717 auto [x, y] = extr->args<2>();
718 auto t = convert(x->type());
721 std::string f =
"llvm.";
723 case core::extrema::Sm: f +=
"smin.";
break;
724 case core::extrema::SM: f +=
"smax.";
break;
725 case core::extrema::sm: f +=
"umin.";
break;
726 case core::extrema::sM: f +=
"umax.";
break;
729 declare(
"{} @{}({}, {})", t, f, t, t);
730 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
732 auto [m, x] = abs->args<2>();
733 auto t = convert(x->type());
735 std::string f =
"llvm.abs." + t;
736 declare(
"{} @{}({}, {})", t, f, t,
"i1");
737 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a,
"i1",
"1");
739 auto v_src =
emit(conv->arg());
740 auto t_src = convert(conv->arg()->type());
741 auto t_dst = convert(conv->type());
746 if (w_src == w_dst)
return v_src;
749 case core::conv::s: op = w_src < w_dst ?
"sext" :
"trunc";
break;
750 case core::conv::u: op = w_src < w_dst ?
"zext" :
"trunc";
break;
753 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
758 auto t_src = convert(
bitcast->arg()->type());
759 auto t_dst = convert(
bitcast->type());
761 if (
auto lit =
Lit::isa(
bitcast->arg()); lit && *lit == 0)
return "zeroinitializer";
763 if (src_type_ptr && dst_type_ptr)
return bb.
assign(
name,
"bitcast {} {} to {}", t_src, v_src, t_dst);
764 if (src_type_ptr)
return bb.
assign(
name,
"ptrtoint {} {} to {}", t_src, v_src, t_dst);
765 if (dst_type_ptr)
return bb.
assign(
name,
"inttoptr {} {} to {}", t_src, v_src, t_dst);
768 auto size2width = [&](
const Def* type) {
769 if (type->isa<
Nat>())
return 64_n;
774 auto src_size = size2width(
bitcast->arg()->type());
775 auto dst_size = size2width(
bitcast->type());
778 if (src_size && dst_size) {
779 if (src_size == dst_size)
return v_src;
780 op = (src_size < dst_size) ?
"zext" :
"trunc";
782 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
784 auto [ptr, i] = lea->args<2>();
786 auto v_ptr =
emit(ptr);
787 auto t_pointee = convert(pointee);
788 auto t_ptr = convert(ptr->type());
789 if (pointee->isa<
Sigma>())
790 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, i32 {}", t_pointee, t_ptr, v_ptr,
793 assert(pointee->isa<
Arr>());
794 auto [v_i, t_i] = emit_gep_index(i);
796 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, {} {}", t_pointee, t_ptr, v_ptr, t_i, v_i);
801 auto size =
emit(malloc->arg(1));
803 bb.
assign(
name +
"i8",
"call i8* @malloc(i64 {})", size);
804 return bb.
assign(
name,
"bitcast i8* {} to {}",
name +
"i8", ptr_t);
808 auto ptr =
emit(free->arg(1));
811 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", ptr_t, ptr);
812 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
815 auto [Ta, msi] = mslot->uncurry_args<2>();
816 auto [pointee, addr_space] = Ta->projs<2>();
817 auto [mem, _, __] = msi->projs<3>();
821 print(bb.
body().emplace_back(),
"{} = alloca {}",
name, convert(pointee));
827 auto v_ptr =
emit(free->arg(1));
830 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", t_ptr, v_ptr);
831 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
835 auto v_ptr =
emit(load->arg(1));
836 auto t_ptr = convert(load->arg(1)->type());
838 return bb.
assign(
name,
"load {}, {} {}", t_pointee, t_ptr, v_ptr);
841 auto v_ptr =
emit(store->arg(1));
842 auto v_val =
emit(store->arg(2));
843 auto t_ptr = convert(store->arg(1)->type());
844 auto t_val = convert(store->arg(2)->type());
845 print(bb.
body().emplace_back(),
"store {} {}, {} {}", t_val, v_val, t_ptr, v_ptr);
851 auto size =
name +
".size";
852 bb.
assign(size,
"call i64 @jmpbuf_size()");
853 return bb.
assign(
name,
"alloca i8, i64 {}", size);
855 declare(
"i32 @_setjmp(i8*) returns_twice");
857 auto [mem, jmpbuf] = setjmp->arg()->projs<2>();
859 auto v_jb =
emit(jmpbuf);
860 return bb.
assign(
name,
"call i32 @_setjmp(i8* {})", v_jb);
862 auto [mode, ab] = arith->uncurry_args<2>();
863 auto [a, b] = ab->projs<2>([
this](
auto def) {
return emit(def); });
864 auto t = convert(arith->type());
867 switch (arith.id()) {
868 case math::arith::add: op =
"fadd";
break;
869 case math::arith::sub: op =
"fsub";
break;
870 case math::arith::mul: op =
"fmul";
break;
871 case math::arith::div: op =
"fdiv";
break;
872 case math::arith::rem: op =
"frem";
break;
875 if (lmode == math::Mode::fast)
879 if (lmode & math::Mode::nnan ) op +=
" nnan";
880 if (lmode & math::Mode::ninf ) op +=
" ninf";
881 if (lmode & math::Mode::nsz ) op +=
" nsz";
882 if (lmode & math::Mode::arcp ) op +=
" arcp";
883 if (lmode & math::Mode::contract) op +=
" contract";
884 if (lmode & math::Mode::afn ) op +=
" afn";
885 if (lmode & math::Mode::reassoc ) op +=
" reassoc";
889 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
891 auto a =
emit(tri->arg());
892 auto t = convert(tri->type());
896 if (tri.id() == math::tri::sin) {
897 f =
"llvm.sin"s + llvm_suffix(tri->type());
898 }
else if (tri.id() == math::tri::cos) {
899 f =
"llvm.cos"s + llvm_suffix(tri->type());
901 if (tri.sub() &
sub_t(math::tri::a)) f +=
"a";
904 case math::tri::sin: f +=
"sin";
break;
905 case math::tri::cos: f +=
"cos";
break;
906 case math::tri::tan: f +=
"tan";
break;
907 case math::tri::ahFF:
error(
"this axm is supposed to be unused");
908 default: fe::unreachable();
911 if (tri.sub() &
sub_t(math::tri::h)) f +=
"h";
912 f += math_suffix(tri->type());
915 declare(
"{} @{}({})", t, f, t);
916 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
918 auto [a, b] = extrema->args<2>([
this](
auto def) {
return emit(def); });
919 auto t = convert(extrema->type());
920 std::string f =
"llvm.";
921 switch (extrema.id()) {
922 case math::extrema::fmin: f +=
"minnum";
break;
923 case math::extrema::fmax: f +=
"maxnum";
break;
924 case math::extrema::ieee754min: f +=
"minimum";
break;
925 case math::extrema::ieee754max: f +=
"maximum";
break;
927 f += llvm_suffix(extrema->type());
929 declare(
"{} @{}({}, {})", t, f, t, t);
930 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
932 auto [a, b] = pow->args<2>([
this](
auto def) {
return emit(def); });
933 auto t = convert(pow->type());
934 std::string f =
"llvm.pow";
935 f += llvm_suffix(pow->type());
936 declare(
"{} @{}({}, {})", t, f, t, t);
937 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
939 auto a =
emit(rt->arg());
940 auto t = convert(rt->type());
942 if (rt.id() == math::rt::sq)
943 f =
"llvm.sqrt"s + llvm_suffix(rt->type());
945 f =
"cbrt"s += math_suffix(rt->type());
946 declare(
"{} @{}({})", t, f, t);
947 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
949 auto a =
emit(exp->arg());
950 auto t = convert(exp->type());
951 std::string f =
"llvm.";
952 f += (exp.sub() &
sub_t(math::exp::log)) ?
"log" :
"exp";
953 f += (exp.sub() &
sub_t(math::exp::bin)) ?
"2" : (exp.sub() &
sub_t(math::exp::dec)) ?
"10" :
"";
954 f += llvm_suffix(exp->type());
956 declare(
"{} @{}({})", t, f, t);
957 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
959 auto a =
emit(er->arg());
960 auto t = convert(er->type());
961 auto f = er.id() == math::er::f ?
"erf"s :
"erfc"s;
962 f += math_suffix(er->type());
963 declare(
"{} @{}({})", t, f, t);
964 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
966 auto a =
emit(gamma->arg());
967 auto t = convert(gamma->type());
968 std::string f = gamma.id() == math::gamma::t ?
"tgamma" :
"lgamma";
969 f += math_suffix(gamma->type());
970 declare(
"{} @{}({})", t, f, t);
971 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
973 auto [a, b] = cmp->args<2>([
this](
auto def) {
return emit(def); });
974 auto t = convert(cmp->arg(0)->type());
979 case math::cmp:: e: op +=
"oeq";
break;
980 case math::cmp:: l: op +=
"olt";
break;
981 case math::cmp:: le: op +=
"ole";
break;
982 case math::cmp:: g: op +=
"ogt";
break;
983 case math::cmp:: ge: op +=
"oge";
break;
984 case math::cmp:: ne: op +=
"one";
break;
985 case math::cmp:: o: op +=
"ord";
break;
986 case math::cmp:: u: op +=
"uno";
break;
987 case math::cmp:: ue: op +=
"ueq";
break;
988 case math::cmp:: ul: op +=
"ult";
break;
989 case math::cmp::ule: op +=
"ule";
break;
990 case math::cmp:: ug: op +=
"ugt";
break;
991 case math::cmp::uge: op +=
"uge";
break;
992 case math::cmp::une: op +=
"une";
break;
994 default: fe::unreachable();
997 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
999 auto v_src =
emit(conv->arg());
1000 auto t_src = convert(conv->arg()->type());
1001 auto t_dst = convert(conv->type());
1003 auto s_src = math::isa_f(conv->arg()->type());
1004 auto s_dst = math::isa_f(conv->type());
1006 switch (conv.id()) {
1007 case math::conv::f2f: op = s_src < s_dst ?
"fpext" :
"fptrunc";
break;
1008 case math::conv::s2f: op =
"sitofp";
break;
1009 case math::conv::u2f: op =
"uitofp";
break;
1010 case math::conv::f2s: op =
"fptosi";
break;
1011 case math::conv::f2u: op =
"fptoui";
break;
1014 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
1016 auto a =
emit(abs->arg());
1017 auto t = convert(abs->type());
1018 std::string f =
"llvm.fabs";
1019 f += llvm_suffix(abs->type());
1020 declare(
"{} @{}({})", t, f, t);
1021 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1023 auto a =
emit(round->arg());
1024 auto t = convert(round->type());
1025 std::string f =
"llvm.";
1026 switch (round.id()) {
1027 case math::round::f: f +=
"floor";
break;
1028 case math::round::c: f +=
"ceil";
break;
1029 case math::round::r: f +=
"round";
break;
1030 case math::round::t: f +=
"trunc";
break;
1032 f += llvm_suffix(round->type());
1033 declare(
"{} @{}({})", t, f, t);
1034 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1036 error(
"unhandled def in LLVM backend: {} : {}", def, def->
type());