318 auto app = lam->
body()->as<
App>();
322 std::vector<std::string> values;
323 std::vector<const Def*> types;
325 for (
auto arg : app->args()) {
327 values.emplace_back(val);
328 types.emplace_back(arg->type());
332 switch (values.size()) {
333 case 0:
return bb.tail(
"ret void");
334 case 1:
return bb.tail(
"ret {} {}", convert(types[0]), values[0]);
336 std::string prev =
"undef";
337 auto type = convert(
world().sigma(types));
338 for (
size_t i = 0, n = values.size(); i != n; ++i) {
339 auto v_elem = values[i];
340 auto t_elem = convert(types[i]);
341 auto namei =
"%ret_val." + std::to_string(i);
342 bb.tail(
"{} = insertvalue {} {}, {} {}, {}", namei, type, prev, t_elem, v_elem, i);
346 bb.tail(
"ret {} {}", type, prev);
353 for (
auto callee_def : ex->tuple()->projs()) {
355 auto callee = callee_def->as_mut<
Lam>();
359 assert(callee->num_tvars() == app->num_targs());
360 size_t n = callee->num_tvars();
361 for (
size_t i = 0; i != n; ++i) {
363 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
364 auto phi = callee->var(n, i);
366 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
372 auto c =
emit(ex->index());
373 if (ex->tuple()->num_projs() == 2) {
374 auto [f, t] = ex->tuple()->projs<2>([
this](
auto def) {
return emit(def); });
375 return bb.tail(
"br i1 {}, label {}, label {}", c, t, f);
377 auto t_c = convert(ex->index()->type());
378 bb.tail(
"switch {} {}, label {} [ ", t_c, c,
emit(ex->tuple()->proj(0)));
379 for (
auto i = 1u; i < ex->tuple()->num_projs(); i++)
380 print(bb.tail().back(),
"{} {}, label {} ", t_c, std::to_string(i),
emit(ex->tuple()->proj(i)));
381 print(bb.tail().back(),
"]");
383 }
else if (app->callee()->isa<
Bot>()) {
384 return bb.tail(
"ret ; bottom: unreachable");
386 size_t n = callee->num_tvars();
387 for (
size_t i = 0; i != n; ++i) {
388 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
389 auto phi = callee->var(n, i);
391 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
395 return bb.tail(
"br label {}",
id(callee));
397 declare(
"void @longjmp(i8*, i32) noreturn");
399 auto [
mem, jbuf, tag] = app->args<3>();
401 auto v_jb =
emit(jbuf);
402 auto v_tag =
emit(tag);
403 bb.tail(
"call void @longjmp(i8* {}, i32 {})", v_jb, v_tag);
404 return bb.tail(
"unreachable");
406 auto v_callee =
emit(app->callee());
408 std::vector<std::string> args;
409 auto app_args = app->args();
410 for (
auto arg : app_args.view().rsubspan(1))
411 if (
auto v_arg =
emit_unsafe(arg); !v_arg.empty()) args.emplace_back(convert(arg->type()) +
" " + v_arg);
413 if (app->args().back()->isa<
Bot>()) {
415 assert(convert_ret_pi(app->callee_type()->ret_pi()) ==
"void");
416 bb.tail(
"call void {}({, })", v_callee, args);
417 return bb.tail(
"unreachable");
420 auto ret_lam = app->args().back()->as_mut<
Lam>();
421 size_t num_vars = ret_lam->
num_vars();
425 for (
auto var : ret_lam->vars()) {
428 types[n] = var->type();
433 bb.tail(
"call void {}({, })", v_callee, args);
435 auto name =
"%" + app->unique_name() +
"ret";
436 auto t_ret = convert_ret_pi(ret_lam->type());
437 bb.tail(
"{} = call {} {}({, })",
name, t_ret, v_callee, args);
439 for (
size_t i = 0, e = ret_lam->num_vars(); i != e; ++i) {
440 auto phi = ret_lam->var(i);
445 namei +=
'.' + std::to_string(i - 1);
446 bb.tail(
"{} = extractvalue {} {}, {}", namei, t_ret,
name, i - 1);
449 lam2bb_[ret_lam].phis[phi].emplace_back(namei,
id(lam,
true));
454 return bb.tail(
"br label {}",
id(ret_lam));
459 if (
auto lam = def->isa<
Lam>())
return id(lam);
464 auto emit_tuple = [&](
const Def* tuple) {
465 if (isa_mem_sigma_2(tuple->type())) {
467 return emit(tuple->proj(2, 1));
470 if (is_const(tuple)) {
471 bool is_array = tuple->type()->isa<
Arr>();
474 s += is_array ?
"[" :
"{";
476 for (
size_t i = 0, n = tuple->num_projs(); i != n; ++i) {
477 auto e = tuple->proj(n, i);
478 if (
auto v_elem =
emit_unsafe(e); !v_elem.empty()) {
479 auto t_elem = convert(e->type());
480 s += sep + t_elem +
" " + v_elem;
485 return s += is_array ?
"]" :
"}";
488 std::string prev =
"undef";
489 auto t = convert(tuple->type());
490 for (
size_t src = 0, dst = 0, n = tuple->num_projs(); src != n; ++src) {
491 auto e = tuple->proj(n, src);
493 auto elem_t = convert(e->type());
495 auto namei =
name +
"." + std::to_string(dst);
496 prev = bb.
assign(namei,
"insertvalue {} {}, {} {}, {}", t, prev, elem_t, elem, dst);
503 if (def->isa<
Var>()) {
505 if (std::ranges::any_of(ts, [](
auto t) {
return match<mem::M>(t); }))
return {};
506 return emit_tuple(def);
509 auto emit_gep_index = [&](
const Def* index) {
510 auto v_i =
emit(index);
511 auto t_i = convert(index->type());
513 if (
auto size =
Idx::size(index->type())) {
516 "zext {} {} to i{} ; add one more bit for gep index as it is treated as signed value",
518 t_i =
"i" + std::to_string(*w + 1);
522 return std::pair(v_i, t_i);
525 if (
auto lit = def->isa<
Lit>()) {
526 if (lit->type()->isa<
Nat>() ||
Idx::size(lit->type())) {
527 return std::to_string(lit->get());
534 s <<
"0xH" << std::setfill(
'0') << std::setw(4) << std::right << std::hex << lit->get<
u16>();
537 hex = std::bit_cast<u64>(
f64(lit->get<
f32>()));
540 case 64: hex = lit->get<
u64>();
break;
541 default: fe::unreachable();
544 s <<
"0x" << std::setfill(
'0') << std::setw(16) << std::right << std::hex << hex;
548 }
else if (def->isa<
Bot>()) {
550 }
else if (
auto top = def->isa<
Top>()) {
553 }
else if (
auto tuple = def->isa<
Tuple>()) {
554 return emit_tuple(tuple);
555 }
else if (
auto pack = def->isa<
Pack>()) {
556 if (
auto lit =
Lit::isa(pack->body()); lit && *lit == 0)
return "zeroinitializer";
557 return emit_tuple(pack);
558 }
else if (
auto extract = def->isa<
Extract>()) {
559 auto tuple = extract->tuple();
560 auto index = extract->index();
565 if (
auto app = extract->type()->isa<
App>();
567 if (
auto arity = tuple->type()->isa_lit_arity(); arity && *arity == 2) {
568 auto t = convert(extract->type());
569 auto [elem_a, elem_b] = tuple->projs<2>([&](
auto e) {
return emit_unsafe(e); });
571 return bb.
assign(
name,
"select {} {}, {} {}, {} {}", convert(index->type()),
emit(index), t, elem_b, t,
582 auto t_tup = convert(tuple->type());
584 if (isa_mem_sigma_2(tuple->type()))
return v_tup;
586 auto v_i =
match<mem::M>(tuple->proj(0)->type()) ? std::to_string(*li - 1) : std::to_string(*li);
587 return bb.
assign(
name,
"extractvalue {} {}, {}", t_tup, v_tup, v_i);
590 auto t_elem = convert(extract->type());
591 auto [v_i, t_i] = emit_gep_index(index);
594 "{}.alloca = alloca {} ; copy to alloca to emulate extract 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, t_tup,
597 t_tup,
name, t_i, v_i);
598 return bb.
assign(
name,
"load {}, {}* {}.gep", t_elem, t_elem,
name);
599 }
else if (
auto insert = def->isa<
Insert>()) {
601 auto t_tup = convert(insert->tuple()->type());
602 auto t_val = convert(insert->value()->type());
603 auto v_tup =
emit(insert->tuple());
604 auto v_val =
emit(insert->value());
605 if (
auto idx =
Lit::isa(insert->index())) {
606 auto v_idx =
emit(insert->index());
607 return bb.
assign(
name,
"insertvalue {} {}, {} {}, {}", t_tup, v_tup, t_val, v_val, v_idx);
609 auto t_elem = convert(insert->value()->type());
610 auto [v_i, t_i] = emit_gep_index(insert->index());
612 "{}.alloca = alloca {} ; copy to alloca to emulate insert with store + gep + load",
name, t_tup);
613 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup,
name);
614 print(bb.
body().emplace_back(),
"{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}",
name,
615 t_tup, t_tup,
name, t_i, v_i);
616 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.gep", t_val, v_val, t_val,
name);
617 return bb.
assign(
name,
"load {}, {}* {}.alloca", t_tup, t_tup,
name);
619 }
else if (
auto global = def->isa<
Global>()) {
620 auto v_init =
emit(global->init());
621 auto [pointee, addr_space] =
force<mem::Ptr>(global->type())->args<2>();
622 print(vars_decls_,
"{} = global {} {}\n",
name, convert(pointee), v_init);
625 auto [a, b] = nat->args<2>([
this](
auto def) {
return emit(def); });
628 case core::nat::add: op =
"add";
break;
629 case core::nat::sub: op =
"sub";
break;
630 case core::nat::mul: op =
"mul";
break;
633 return bb.
assign(
name,
"{} nsw nuw i64 {}, {}", op, a, b);
635 auto [a, b] = ncmp->args<2>([
this](
auto def) {
return emit(def); });
640 case core::ncmp::e: op +=
"eq" ;
break;
641 case core::ncmp::ne: op +=
"ne" ;
break;
642 case core::ncmp::g: op +=
"ugt";
break;
643 case core::ncmp::ge: op +=
"uge";
break;
644 case core::ncmp::l: op +=
"ult";
break;
645 case core::ncmp::le: op +=
"ule";
break;
647 default: fe::unreachable();
650 return bb.
assign(
name,
"{} i64 {}, {}", op, a, b);
652 auto x =
emit(idx->arg());
654 auto t = convert(idx->type());
655 if (s < 64)
return bb.
assign(
name,
"trunc i64 {} to {}", x, t);
658 assert(bit1.id() == core::bit1::neg);
659 auto x =
emit(bit1->arg());
660 auto t = convert(bit1->type());
661 return bb.
assign(
name,
"xor {} -1, {}", t, x);
663 auto [a, b] = bit2->args<2>([
this](
auto def) {
return emit(def); });
664 auto t = convert(bit2->type());
666 auto neg = [&](std::string_view x) {
return bb.
assign(
name +
".neg",
"xor {} -1, {}", t, x); };
670 case core::bit2::and_:
return bb.
assign(
name,
"and {} {}, {}", t, a, b);
671 case core::bit2:: or_:
return bb.
assign(
name,
"or {} {}, {}", t, a, b);
672 case core::bit2::xor_:
return bb.
assign(
name,
"xor {} {}, {}", t, a, b);
673 case core::bit2::nand:
return neg(bb.
assign(
name,
"and {} {}, {}", t, a, b));
674 case core::bit2:: nor:
return neg(bb.
assign(
name,
"or {} {}, {}", t, a, b));
675 case core::bit2::nxor:
return neg(bb.
assign(
name,
"xor {} {}, {}", t, a, b));
676 case core::bit2:: iff:
return bb.
assign(
name,
"and {} {}, {}", neg(a), b);
677 case core::bit2::niff:
return bb.
assign(
name,
"or {} {}, {}", neg(a), b);
679 default: fe::unreachable();
682 auto [a, b] = shr->args<2>([
this](
auto def) {
return emit(def); });
683 auto t = convert(shr->type());
686 case core::shr::a: op =
"ashr";
break;
687 case core::shr::l: op =
"lshr";
break;
690 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
692 auto [a, b] = wrap->args<2>([
this](
auto def) {
return emit(def); });
693 auto t = convert(wrap->type());
694 auto mode =
Lit::as(wrap->decurry()->arg());
697 case core::wrap::add: op =
"add";
break;
698 case core::wrap::sub: op =
"sub";
break;
699 case core::wrap::mul: op =
"mul";
break;
700 case core::wrap::shl: op =
"shl";
break;
703 if (mode & core::Mode::nuw) op +=
" nuw";
704 if (mode & core::Mode::nsw) op +=
" nsw";
706 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
708 auto [m, xy] = div->args<2>();
709 auto [x, y] = xy->projs<2>();
710 auto t = convert(x->type());
716 case core::div::sdiv: op =
"sdiv";
break;
717 case core::div::udiv: op =
"udiv";
break;
718 case core::div::srem: op =
"srem";
break;
719 case core::div::urem: op =
"urem";
break;
722 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
724 auto [a, b] = icmp->args<2>([
this](
auto def) {
return emit(def); });
725 auto t = convert(icmp->arg(0)->type());
730 case core::icmp::e: op +=
"eq" ;
break;
731 case core::icmp::ne: op +=
"ne" ;
break;
732 case core::icmp::sg: op +=
"sgt";
break;
733 case core::icmp::sge: op +=
"sge";
break;
734 case core::icmp::sl: op +=
"slt";
break;
735 case core::icmp::sle: op +=
"sle";
break;
736 case core::icmp::ug: op +=
"ugt";
break;
737 case core::icmp::uge: op +=
"uge";
break;
738 case core::icmp::ul: op +=
"ult";
break;
739 case core::icmp::ule: op +=
"ule";
break;
741 default: fe::unreachable();
744 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
746 auto [x, y] = extr->args<2>();
747 auto t = convert(x->type());
750 std::string f =
"llvm.";
752 case core::extrema::Sm: f +=
"smin.";
break;
753 case core::extrema::SM: f +=
"smax.";
break;
754 case core::extrema::sm: f +=
"umin.";
break;
755 case core::extrema::sM: f +=
"umax.";
break;
758 declare(
"{} @{}({}, {})", t, f, t, t);
759 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
761 auto [m, x] = abs->args<2>();
762 auto t = convert(x->type());
764 std::string f =
"llvm.abs." + t;
765 declare(
"{} @{}({}, {})", t, f, t,
"i1");
766 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a,
"i1",
"1");
768 auto v_src =
emit(conv->arg());
769 auto t_src = convert(conv->arg()->type());
770 auto t_dst = convert(conv->type());
775 if (w_src == w_dst)
return v_src;
778 case core::conv::s: op = w_src < w_dst ?
"sext" :
"trunc";
break;
779 case core::conv::u: op = w_src < w_dst ?
"zext" :
"trunc";
break;
782 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
787 auto t_src = convert(
bitcast->arg()->type());
788 auto t_dst = convert(
bitcast->type());
790 if (
auto lit =
Lit::isa(
bitcast->arg()); lit && *lit == 0)
return "zeroinitializer";
792 if (src_type_ptr && dst_type_ptr)
return bb.
assign(
name,
"bitcast {} {} to {}", t_src, v_src, t_dst);
793 if (src_type_ptr)
return bb.
assign(
name,
"ptrtoint {} {} to {}", t_src, v_src, t_dst);
794 if (dst_type_ptr)
return bb.
assign(
name,
"inttoptr {} {} to {}", t_src, v_src, t_dst);
797 auto size2width = [&](
const Def* type) {
798 if (type->isa<
Nat>())
return 64_n;
803 auto src_size = size2width(
bitcast->arg()->type());
804 auto dst_size = size2width(
bitcast->type());
807 if (src_size && dst_size) {
808 if (src_size == dst_size)
return v_src;
809 op = (src_size < dst_size) ?
"zext" :
"trunc";
811 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
813 auto [ptr, i] = lea->args<2>();
815 auto v_ptr =
emit(ptr);
816 auto t_pointee = convert(pointee);
817 auto t_ptr = convert(ptr->type());
818 if (pointee->isa<
Sigma>())
819 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, i32 {}", t_pointee, t_ptr, v_ptr,
822 assert(pointee->isa<
Arr>());
823 auto [v_i, t_i] = emit_gep_index(i);
825 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, {} {}", t_pointee, t_ptr, v_ptr, t_i, v_i);
830 auto size =
emit(malloc->arg(1));
832 bb.
assign(
name +
"i8",
"call i8* @malloc(i64 {})", size);
833 return bb.
assign(
name,
"bitcast i8* {} to {}",
name +
"i8", ptr_t);
837 auto ptr =
emit(free->arg(1));
840 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", ptr_t, ptr);
841 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
847 auto [pointee, addr_space] = mslot->decurry()->args<2>();
848 print(bb.
body().emplace_back(),
"{} = alloca {}",
name, convert(pointee));
854 auto v_ptr =
emit(free->arg(1));
857 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", t_ptr, v_ptr);
858 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
862 auto v_ptr =
emit(load->arg(1));
863 auto t_ptr = convert(load->arg(1)->type());
864 auto t_pointee = convert(
force<mem::Ptr>(load->arg(1)->type())->arg(0));
865 return bb.
assign(
name,
"load {}, {} {}", t_pointee, t_ptr, v_ptr);
868 auto v_ptr =
emit(store->arg(1));
869 auto v_val =
emit(store->arg(2));
870 auto t_ptr = convert(store->arg(1)->type());
871 auto t_val = convert(store->arg(2)->type());
872 print(bb.
body().emplace_back(),
"store {} {}, {} {}", t_val, v_val, t_ptr, v_ptr);
878 auto size =
name +
".size";
879 bb.
assign(size,
"call i64 @jmpbuf_size()");
880 return bb.
assign(
name,
"alloca i8, i64 {}", size);
882 declare(
"i32 @_setjmp(i8*) returns_twice");
884 auto [
mem, jmpbuf] = setjmp->arg()->projs<2>();
886 auto v_jb =
emit(jmpbuf);
887 return bb.
assign(
name,
"call i32 @_setjmp(i8* {})", v_jb);
889 auto [a, b] = arith->args<2>([
this](
auto def) {
return emit(def); });
890 auto t = convert(arith->type());
891 auto mode =
Lit::as(arith->decurry()->arg());
893 switch (arith.id()) {
894 case math::arith::add: op =
"fadd";
break;
895 case math::arith::sub: op =
"fsub";
break;
896 case math::arith::mul: op =
"fmul";
break;
897 case math::arith::div: op =
"fdiv";
break;
898 case math::arith::rem: op =
"frem";
break;
901 if (mode == math::Mode::fast)
905 if (mode & math::Mode::nnan ) op +=
" nnan";
906 if (mode & math::Mode::ninf ) op +=
" ninf";
907 if (mode & math::Mode::nsz ) op +=
" nsz";
908 if (mode & math::Mode::arcp ) op +=
" arcp";
909 if (mode & math::Mode::contract) op +=
" contract";
910 if (mode & math::Mode::afn ) op +=
" afn";
911 if (mode & math::Mode::reassoc ) op +=
" reassoc";
915 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
917 auto a =
emit(tri->arg());
918 auto t = convert(tri->type());
922 if (tri.id() == math::tri::sin) {
923 f =
"llvm.sin"s + llvm_suffix(tri->type());
924 }
else if (tri.id() == math::tri::cos) {
925 f =
"llvm.cos"s + llvm_suffix(tri->type());
927 if (tri.sub() &
sub_t(math::tri::a)) f +=
"a";
930 case math::tri::sin: f +=
"sin";
break;
931 case math::tri::cos: f +=
"cos";
break;
932 case math::tri::tan: f +=
"tan";
break;
933 case math::tri::ahFF:
error(
"this axiom is supposed to be unused");
934 default: fe::unreachable();
937 if (tri.sub() &
sub_t(math::tri::h)) f +=
"h";
938 f += math_suffix(tri->type());
941 declare(
"{} @{}({})", t, f, t);
942 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
944 auto [a, b] = extrema->args<2>([
this](
auto def) {
return emit(def); });
945 auto t = convert(extrema->type());
946 std::string f =
"llvm.";
947 switch (extrema.id()) {
948 case math::extrema::fmin: f +=
"minnum";
break;
949 case math::extrema::fmax: f +=
"maxnum";
break;
950 case math::extrema::ieee754min: f +=
"minimum";
break;
951 case math::extrema::ieee754max: f +=
"maximum";
break;
953 f += llvm_suffix(extrema->type());
955 declare(
"{} @{}({}, {})", t, f, t, t);
956 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
958 auto [a, b] = pow->args<2>([
this](
auto def) {
return emit(def); });
959 auto t = convert(pow->type());
960 std::string f =
"llvm.pow";
961 f += llvm_suffix(pow->type());
962 declare(
"{} @{}({}, {})", t, f, t, t);
963 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
965 auto a =
emit(rt->arg());
966 auto t = convert(rt->type());
968 if (rt.id() == math::rt::sq)
969 f =
"llvm.sqrt"s + llvm_suffix(rt->type());
971 f =
"cbrt"s += math_suffix(rt->type());
972 declare(
"{} @{}({})", t, f, t);
973 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
975 auto a =
emit(exp->arg());
976 auto t = convert(exp->type());
977 std::string f =
"llvm.";
978 f += (exp.sub() &
sub_t(math::exp::log)) ?
"log" :
"exp";
979 f += (exp.sub() &
sub_t(math::exp::bin)) ?
"2" : (exp.sub() &
sub_t(math::exp::dec)) ?
"10" :
"";
980 f += llvm_suffix(exp->type());
982 declare(
"{} @{}({})", t, f, t);
983 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
985 auto a =
emit(er->arg());
986 auto t = convert(er->type());
987 auto f = er.id() == math::er::f ?
"erf"s :
"erfc"s;
988 f += math_suffix(er->type());
989 declare(
"{} @{}({})", t, f, t);
990 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
992 auto a =
emit(gamma->arg());
993 auto t = convert(gamma->type());
994 std::string f = gamma.id() == math::gamma::t ?
"tgamma" :
"lgamma";
995 f += math_suffix(gamma->type());
996 declare(
"{} @{}({})", t, f, t);
997 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
999 auto [a, b] = cmp->args<2>([
this](
auto def) {
return emit(def); });
1000 auto t = convert(cmp->arg(0)->type());
1005 case math::cmp:: e: op +=
"oeq";
break;
1006 case math::cmp:: l: op +=
"olt";
break;
1007 case math::cmp:: le: op +=
"ole";
break;
1008 case math::cmp:: g: op +=
"ogt";
break;
1009 case math::cmp:: ge: op +=
"oge";
break;
1010 case math::cmp:: ne: op +=
"one";
break;
1011 case math::cmp:: o: op +=
"ord";
break;
1012 case math::cmp:: u: op +=
"uno";
break;
1013 case math::cmp:: ue: op +=
"ueq";
break;
1014 case math::cmp:: ul: op +=
"ult";
break;
1015 case math::cmp::ule: op +=
"ule";
break;
1016 case math::cmp:: ug: op +=
"ugt";
break;
1017 case math::cmp::uge: op +=
"uge";
break;
1018 case math::cmp::une: op +=
"une";
break;
1020 default: fe::unreachable();
1023 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
1025 auto v_src =
emit(conv->arg());
1026 auto t_src = convert(conv->arg()->type());
1027 auto t_dst = convert(conv->type());
1032 switch (conv.id()) {
1033 case math::conv::f2f: op = s_src < s_dst ?
"fpext" :
"fptrunc";
break;
1034 case math::conv::s2f: op =
"sitofp";
break;
1035 case math::conv::u2f: op =
"uitofp";
break;
1036 case math::conv::f2s: op =
"fptosi";
break;
1037 case math::conv::f2u: op =
"fptoui";
break;
1040 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
1042 auto a =
emit(abs->arg());
1043 auto t = convert(abs->type());
1044 std::string f =
"llvm.fabs";
1045 f += llvm_suffix(abs->type());
1046 declare(
"{} @{}({})", t, f, t);
1047 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1049 auto a =
emit(round->arg());
1050 auto t = convert(round->type());
1051 std::string f =
"llvm.";
1052 switch (round.id()) {
1053 case math::round::f: f +=
"floor";
break;
1054 case math::round::c: f +=
"ceil";
break;
1055 case math::round::r: f +=
"round";
break;
1056 case math::round::t: f +=
"trunc";
break;
1058 f += llvm_suffix(round->type());
1059 declare(
"{} @{}({})", t, f, t);
1060 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1062 error(
"unhandled def in LLVM backend: {} : {}", def, def->
type());