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);
352 for (
auto callee_def : ex->tuple()->projs()) {
354 auto callee = callee_def->as_mut<
Lam>();
358 assert(callee->num_tvars() == app->num_targs());
359 size_t n = callee->num_tvars();
360 for (
size_t i = 0; i != n; ++i) {
362 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
363 auto phi = callee->var(n, i);
365 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
371 auto c =
emit(ex->index());
372 if (ex->tuple()->num_projs() == 2) {
373 auto [f, t] = ex->tuple()->projs<2>([
this](
auto def) {
return emit(def); });
374 return bb.tail(
"br i1 {}, label {}, label {}", c, t, f);
376 auto t_c = convert(ex->index()->type());
377 bb.tail(
"switch {} {}, label {} [ ", t_c, c,
emit(ex->tuple()->proj(0)));
378 for (
auto i = 1u; i < ex->tuple()->num_projs(); i++)
379 print(bb.tail().back(),
"{} {}, label {} ", t_c, std::to_string(i),
emit(ex->tuple()->proj(i)));
380 print(bb.tail().back(),
"]");
382 }
else if (app->callee()->isa<
Bot>()) {
383 return bb.tail(
"ret ; bottom: unreachable");
385 size_t n = callee->num_tvars();
386 for (
size_t i = 0; i != n; ++i) {
387 if (
auto arg =
emit_unsafe(app->arg(n, i)); !arg.empty()) {
388 auto phi = callee->var(n, i);
390 lam2bb_[callee].phis[phi].emplace_back(arg,
id(lam,
true));
394 return bb.tail(
"br label {}",
id(callee));
396 declare(
"void @longjmp(i8*, i32) noreturn");
398 auto [mem, jbuf, tag] = app->args<3>();
400 auto v_jb =
emit(jbuf);
401 auto v_tag =
emit(tag);
402 bb.tail(
"call void @longjmp(i8* {}, i32 {})", v_jb, v_tag);
403 return bb.tail(
"unreachable");
405 auto v_callee =
emit(app->callee());
407 std::vector<std::string> args;
408 auto app_args = app->args();
409 for (
auto arg : app_args.view().rsubspan(1))
410 if (
auto v_arg =
emit_unsafe(arg); !v_arg.empty()) args.emplace_back(convert(arg->type()) +
" " + v_arg);
412 if (app->args().back()->isa<
Bot>()) {
414 assert(convert_ret_pi(app->callee_type()->ret_pi()) ==
"void");
415 bb.tail(
"call void {}({, })", v_callee, args);
416 return bb.tail(
"unreachable");
419 auto ret_lam = app->args().back()->as_mut<
Lam>();
420 size_t num_vars = ret_lam->
num_vars();
424 for (
auto var : ret_lam->vars()) {
427 types[n] = var->type();
432 bb.tail(
"call void {}({, })", v_callee, args);
434 auto name =
"%" + app->unique_name() +
"ret";
435 auto t_ret = convert_ret_pi(ret_lam->type());
436 bb.tail(
"{} = call {} {}({, })",
name, t_ret, v_callee, args);
438 for (
size_t i = 0, e = ret_lam->num_vars(); i != e; ++i) {
439 auto phi = ret_lam->var(i);
444 namei +=
'.' + std::to_string(i - 1);
445 bb.tail(
"{} = extractvalue {} {}, {}", namei, t_ret,
name, i - 1);
448 lam2bb_[ret_lam].phis[phi].emplace_back(namei,
id(lam,
true));
453 return bb.tail(
"br label {}",
id(ret_lam));
458 if (
auto lam = def->isa<
Lam>())
return id(lam);
463 auto emit_tuple = [&](
const Def* tuple) {
464 if (isa_mem_sigma_2(tuple->type())) {
466 return emit(tuple->proj(2, 1));
469 if (is_const(tuple)) {
470 bool is_array = tuple->type()->isa<
Arr>();
473 s += is_array ?
"[" :
"{";
475 for (
size_t i = 0, n = tuple->num_projs(); i != n; ++i) {
476 auto e = tuple->proj(n, i);
477 if (
auto v_elem =
emit_unsafe(e); !v_elem.empty()) {
478 auto t_elem = convert(e->type());
479 s += sep + t_elem +
" " + v_elem;
484 return s += is_array ?
"]" :
"}";
487 std::string prev =
"undef";
488 auto t = convert(tuple->type());
489 for (
size_t src = 0, dst = 0, n = tuple->num_projs(); src != n; ++src) {
490 auto e = tuple->proj(n, src);
492 auto elem_t = convert(e->type());
494 auto namei =
name +
"." + std::to_string(dst);
495 prev = bb.
assign(namei,
"insertvalue {} {}, {} {}, {}", t, prev, elem_t, elem, dst);
502 if (def->isa<
Var>()) {
504 if (std::ranges::any_of(ts, [](
auto t) {
return Axm::isa<mem::M>(t); }))
return {};
505 return emit_tuple(def);
508 auto emit_gep_index = [&](
const Def* index) {
509 auto v_i =
emit(index);
510 auto t_i = convert(index->type());
512 if (
auto size =
Idx::isa(index->type())) {
515 "zext {} {} to i{} ; add one more bit for gep index as it is treated as signed value",
517 t_i =
"i" + std::to_string(*w + 1);
521 return std::pair(v_i, t_i);
524 if (
auto lit = def->isa<
Lit>()) {
525 if (lit->type()->isa<
Nat>() ||
Idx::isa(lit->type())) {
526 return std::to_string(lit->get());
527 }
else if (
auto w = math::isa_f(lit->type())) {
533 s <<
"0xH" << std::setfill(
'0') << std::setw(4) << std::right << std::hex << lit->get<
u16>();
536 hex = std::bit_cast<u64>(
f64(lit->get<
f32>()));
539 case 64: hex = lit->get<
u64>();
break;
540 default: fe::unreachable();
543 s <<
"0x" << std::setfill(
'0') << std::setw(16) << std::right << std::hex << hex;
547 }
else if (def->isa<
Bot>()) {
549 }
else if (
auto top = def->isa<
Top>()) {
552 }
else if (
auto tuple = def->isa<
Tuple>()) {
553 return emit_tuple(tuple);
554 }
else if (
auto pack = def->isa<
Pack>()) {
555 if (
auto lit =
Lit::isa(pack->body()); lit && *lit == 0)
return "zeroinitializer";
556 return emit_tuple(pack);
557 }
else if (
auto sel =
Select(def)) {
558 auto t = convert(sel.extract()->type());
559 auto [elem_a, elem_b] = sel.pair()->projs<2>([&](
auto e) {
return emit_unsafe(e); });
560 auto cond_t = convert(sel.cond()->type());
561 auto cond =
emit(sel.cond());
562 return bb.
assign(
name,
"select {} {}, {} {}, {} {}", cond_t, cond, t, elem_b, t, elem_a);
563 }
else if (
auto extract = def->isa<
Extract>()) {
564 auto tuple = extract->tuple();
565 auto index = extract->index();
572 auto t_tup = convert(tuple->type());
574 if (isa_mem_sigma_2(tuple->type()))
return v_tup;
576 auto v_i =
Axm::isa<mem::M>(tuple->proj(0)->type()) ? std::to_string(*li - 1) : std::to_string(*li);
577 return bb.
assign(
name,
"extractvalue {} {}, {}", t_tup, v_tup, v_i);
580 auto t_elem = convert(extract->type());
581 auto [v_i, t_i] = emit_gep_index(index);
584 "{}.alloca = alloca {} ; copy to alloca to emulate extract with store + gep + load",
name, t_tup);
585 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup,
name);
586 print(bb.
body().emplace_back(),
"{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}",
name, t_tup,
587 t_tup,
name, t_i, v_i);
588 return bb.
assign(
name,
"load {}, {}* {}.gep", t_elem, t_elem,
name);
589 }
else if (
auto insert = def->isa<
Insert>()) {
591 auto t_tup = convert(insert->tuple()->type());
592 auto t_val = convert(insert->value()->type());
593 auto v_tup =
emit(insert->tuple());
594 auto v_val =
emit(insert->value());
595 if (
auto idx =
Lit::isa(insert->index())) {
596 auto v_idx =
emit(insert->index());
597 return bb.
assign(
name,
"insertvalue {} {}, {} {}, {}", t_tup, v_tup, t_val, v_val, v_idx);
599 auto t_elem = convert(insert->value()->type());
600 auto [v_i, t_i] = emit_gep_index(insert->index());
602 "{}.alloca = alloca {} ; copy to alloca to emulate insert with store + gep + load",
name, t_tup);
603 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.alloca", t_tup, v_tup, t_tup,
name);
604 print(bb.
body().emplace_back(),
"{}.gep = getelementptr inbounds {}, {}* {}.alloca, i64 0, {} {}",
name,
605 t_tup, t_tup,
name, t_i, v_i);
606 print(bb.
body().emplace_back(),
"store {} {}, {}* {}.gep", t_val, v_val, t_val,
name);
607 return bb.
assign(
name,
"load {}, {}* {}.alloca", t_tup, t_tup,
name);
609 }
else if (
auto global = def->isa<
Global>()) {
610 auto v_init =
emit(global->init());
612 print(vars_decls_,
"{} = global {} {}\n",
name, convert(pointee), v_init);
615 auto [a, b] = nat->args<2>([
this](
auto def) {
return emit(def); });
618 case core::nat::add: op =
"add";
break;
619 case core::nat::sub: op =
"sub";
break;
620 case core::nat::mul: op =
"mul";
break;
623 return bb.
assign(
name,
"{} nsw nuw i64 {}, {}", op, a, b);
625 auto [a, b] = ncmp->args<2>([
this](
auto def) {
return emit(def); });
630 case core::ncmp::e: op +=
"eq" ;
break;
631 case core::ncmp::ne: op +=
"ne" ;
break;
632 case core::ncmp::g: op +=
"ugt";
break;
633 case core::ncmp::ge: op +=
"uge";
break;
634 case core::ncmp::l: op +=
"ult";
break;
635 case core::ncmp::le: op +=
"ule";
break;
637 default: fe::unreachable();
640 return bb.
assign(
name,
"{} i64 {}, {}", op, a, b);
642 auto x =
emit(idx->arg());
644 auto t = convert(idx->type());
645 if (s < 64)
return bb.
assign(
name,
"trunc i64 {} to {}", x, t);
648 assert(bit1.id() == core::bit1::neg);
649 auto x =
emit(bit1->arg());
650 auto t = convert(bit1->type());
651 return bb.
assign(
name,
"xor {} -1, {}", t, x);
653 auto [a, b] = bit2->args<2>([
this](
auto def) {
return emit(def); });
654 auto t = convert(bit2->type());
656 auto neg = [&](std::string_view x) {
return bb.
assign(
name +
".neg",
"xor {} -1, {}", t, x); };
660 case core::bit2::and_:
return bb.
assign(
name,
"and {} {}, {}", t, a, b);
661 case core::bit2:: or_:
return bb.
assign(
name,
"or {} {}, {}", t, a, b);
662 case core::bit2::xor_:
return bb.
assign(
name,
"xor {} {}, {}", t, a, b);
663 case core::bit2::nand:
return neg(bb.
assign(
name,
"and {} {}, {}", t, a, b));
664 case core::bit2:: nor:
return neg(bb.
assign(
name,
"or {} {}, {}", t, a, b));
665 case core::bit2::nxor:
return neg(bb.
assign(
name,
"xor {} {}, {}", t, a, b));
666 case core::bit2:: iff:
return bb.
assign(
name,
"and {} {}, {}", neg(a), b);
667 case core::bit2::niff:
return bb.
assign(
name,
"or {} {}, {}", neg(a), b);
669 default: fe::unreachable();
672 auto [a, b] = shr->args<2>([
this](
auto def) {
return emit(def); });
673 auto t = convert(shr->type());
676 case core::shr::a: op =
"ashr";
break;
677 case core::shr::l: op =
"lshr";
break;
680 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
682 auto [mode, ab] = wrap->uncurry_args<2>();
683 auto [a, b] = ab->projs<2>([
this](
auto def) {
return emit(def); });
684 auto t = convert(wrap->type());
688 case core::wrap::add: op =
"add";
break;
689 case core::wrap::sub: op =
"sub";
break;
690 case core::wrap::mul: op =
"mul";
break;
691 case core::wrap::shl: op =
"shl";
break;
694 if (lmode & core::Mode::nuw) op +=
" nuw";
695 if (lmode & core::Mode::nsw) op +=
" nsw";
697 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
699 auto [m, xy] = div->args<2>();
700 auto [x, y] = xy->projs<2>();
701 auto t = convert(x->type());
707 case core::div::sdiv: op =
"sdiv";
break;
708 case core::div::udiv: op =
"udiv";
break;
709 case core::div::srem: op =
"srem";
break;
710 case core::div::urem: op =
"urem";
break;
713 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
715 auto [a, b] = icmp->args<2>([
this](
auto def) {
return emit(def); });
716 auto t = convert(icmp->arg(0)->type());
721 case core::icmp::e: op +=
"eq" ;
break;
722 case core::icmp::ne: op +=
"ne" ;
break;
723 case core::icmp::sg: op +=
"sgt";
break;
724 case core::icmp::sge: op +=
"sge";
break;
725 case core::icmp::sl: op +=
"slt";
break;
726 case core::icmp::sle: op +=
"sle";
break;
727 case core::icmp::ug: op +=
"ugt";
break;
728 case core::icmp::uge: op +=
"uge";
break;
729 case core::icmp::ul: op +=
"ult";
break;
730 case core::icmp::ule: op +=
"ule";
break;
732 default: fe::unreachable();
735 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
737 auto [x, y] = extr->args<2>();
738 auto t = convert(x->type());
741 std::string f =
"llvm.";
743 case core::extrema::Sm: f +=
"smin.";
break;
744 case core::extrema::SM: f +=
"smax.";
break;
745 case core::extrema::sm: f +=
"umin.";
break;
746 case core::extrema::sM: f +=
"umax.";
break;
749 declare(
"{} @{}({}, {})", t, f, t, t);
750 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
752 auto [m, x] = abs->args<2>();
753 auto t = convert(x->type());
755 std::string f =
"llvm.abs." + t;
756 declare(
"{} @{}({}, {})", t, f, t,
"i1");
757 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a,
"i1",
"1");
759 auto v_src =
emit(conv->arg());
760 auto t_src = convert(conv->arg()->type());
761 auto t_dst = convert(conv->type());
766 if (w_src == w_dst)
return v_src;
769 case core::conv::s: op = w_src < w_dst ?
"sext" :
"trunc";
break;
770 case core::conv::u: op = w_src < w_dst ?
"zext" :
"trunc";
break;
773 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
778 auto t_src = convert(
bitcast->arg()->type());
779 auto t_dst = convert(
bitcast->type());
781 if (
auto lit =
Lit::isa(
bitcast->arg()); lit && *lit == 0)
return "zeroinitializer";
783 if (src_type_ptr && dst_type_ptr)
return bb.
assign(
name,
"bitcast {} {} to {}", t_src, v_src, t_dst);
784 if (src_type_ptr)
return bb.
assign(
name,
"ptrtoint {} {} to {}", t_src, v_src, t_dst);
785 if (dst_type_ptr)
return bb.
assign(
name,
"inttoptr {} {} to {}", t_src, v_src, t_dst);
788 auto size2width = [&](
const Def* type) {
789 if (type->isa<
Nat>())
return 64_n;
794 auto src_size = size2width(
bitcast->arg()->type());
795 auto dst_size = size2width(
bitcast->type());
798 if (src_size && dst_size) {
799 if (src_size == dst_size)
return v_src;
800 op = (src_size < dst_size) ?
"zext" :
"trunc";
802 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
804 auto [ptr, i] = lea->args<2>();
806 auto v_ptr =
emit(ptr);
807 auto t_pointee = convert(pointee);
808 auto t_ptr = convert(ptr->type());
809 if (pointee->isa<
Sigma>())
810 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, i32 {}", t_pointee, t_ptr, v_ptr,
813 assert(pointee->isa<
Arr>());
814 auto [v_i, t_i] = emit_gep_index(i);
816 return bb.
assign(
name,
"getelementptr inbounds {}, {} {}, i64 0, {} {}", t_pointee, t_ptr, v_ptr, t_i, v_i);
821 auto size =
emit(malloc->arg(1));
823 bb.
assign(
name +
"i8",
"call i8* @malloc(i64 {})", size);
824 return bb.
assign(
name,
"bitcast i8* {} to {}",
name +
"i8", ptr_t);
828 auto ptr =
emit(free->arg(1));
831 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", ptr_t, ptr);
832 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
835 auto [Ta, msi] = mslot->uncurry_args<2>();
836 auto [pointee, addr_space] = Ta->projs<2>();
837 auto [mem, _, __] = msi->projs<3>();
841 print(bb.
body().emplace_back(),
"{} = alloca {}",
name, convert(pointee));
847 auto v_ptr =
emit(free->arg(1));
850 bb.
assign(
name +
"i8",
"bitcast {} {} to i8*", t_ptr, v_ptr);
851 bb.
tail(
"call void @free(i8* {})",
name +
"i8");
855 auto v_ptr =
emit(load->arg(1));
856 auto t_ptr = convert(load->arg(1)->type());
858 return bb.
assign(
name,
"load {}, {} {}", t_pointee, t_ptr, v_ptr);
861 auto v_ptr =
emit(store->arg(1));
862 auto v_val =
emit(store->arg(2));
863 auto t_ptr = convert(store->arg(1)->type());
864 auto t_val = convert(store->arg(2)->type());
865 print(bb.
body().emplace_back(),
"store {} {}, {} {}", t_val, v_val, t_ptr, v_ptr);
871 auto size =
name +
".size";
872 bb.
assign(size,
"call i64 @jmpbuf_size()");
873 return bb.
assign(
name,
"alloca i8, i64 {}", size);
875 declare(
"i32 @_setjmp(i8*) returns_twice");
877 auto [mem, jmpbuf] = setjmp->arg()->projs<2>();
879 auto v_jb =
emit(jmpbuf);
880 return bb.
assign(
name,
"call i32 @_setjmp(i8* {})", v_jb);
882 auto [mode, ab] = arith->uncurry_args<2>();
883 auto [a, b] = ab->projs<2>([
this](
auto def) {
return emit(def); });
884 auto t = convert(arith->type());
887 switch (arith.id()) {
888 case math::arith::add: op =
"fadd";
break;
889 case math::arith::sub: op =
"fsub";
break;
890 case math::arith::mul: op =
"fmul";
break;
891 case math::arith::div: op =
"fdiv";
break;
892 case math::arith::rem: op =
"frem";
break;
895 if (lmode == math::Mode::fast)
899 if (lmode & math::Mode::nnan ) op +=
" nnan";
900 if (lmode & math::Mode::ninf ) op +=
" ninf";
901 if (lmode & math::Mode::nsz ) op +=
" nsz";
902 if (lmode & math::Mode::arcp ) op +=
" arcp";
903 if (lmode & math::Mode::contract) op +=
" contract";
904 if (lmode & math::Mode::afn ) op +=
" afn";
905 if (lmode & math::Mode::reassoc ) op +=
" reassoc";
909 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
911 auto a =
emit(tri->arg());
912 auto t = convert(tri->type());
916 if (tri.id() == math::tri::sin) {
917 f =
"llvm.sin"s + llvm_suffix(tri->type());
918 }
else if (tri.id() == math::tri::cos) {
919 f =
"llvm.cos"s + llvm_suffix(tri->type());
921 if (tri.sub() &
sub_t(math::tri::a)) f +=
"a";
924 case math::tri::sin: f +=
"sin";
break;
925 case math::tri::cos: f +=
"cos";
break;
926 case math::tri::tan: f +=
"tan";
break;
927 case math::tri::ahFF:
error(
"this axm is supposed to be unused");
928 default: fe::unreachable();
931 if (tri.sub() &
sub_t(math::tri::h)) f +=
"h";
932 f += math_suffix(tri->type());
935 declare(
"{} @{}({})", t, f, t);
936 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
938 auto [a, b] = extrema->args<2>([
this](
auto def) {
return emit(def); });
939 auto t = convert(extrema->type());
940 std::string f =
"llvm.";
941 switch (extrema.id()) {
942 case math::extrema::fmin: f +=
"minnum";
break;
943 case math::extrema::fmax: f +=
"maxnum";
break;
944 case math::extrema::ieee754min: f +=
"minimum";
break;
945 case math::extrema::ieee754max: f +=
"maximum";
break;
947 f += llvm_suffix(extrema->type());
949 declare(
"{} @{}({}, {})", t, f, t, t);
950 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
952 auto [a, b] = pow->args<2>([
this](
auto def) {
return emit(def); });
953 auto t = convert(pow->type());
954 std::string f =
"llvm.pow";
955 f += llvm_suffix(pow->type());
956 declare(
"{} @{}({}, {})", t, f, t, t);
957 return bb.
assign(
name,
"tail call {} @{}({} {}, {} {})", t, f, t, a, t, b);
959 auto a =
emit(rt->arg());
960 auto t = convert(rt->type());
962 if (rt.id() == math::rt::sq)
963 f =
"llvm.sqrt"s + llvm_suffix(rt->type());
965 f =
"cbrt"s += math_suffix(rt->type());
966 declare(
"{} @{}({})", t, f, t);
967 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
969 auto a =
emit(exp->arg());
970 auto t = convert(exp->type());
971 std::string f =
"llvm.";
972 f += (exp.sub() &
sub_t(math::exp::log)) ?
"log" :
"exp";
973 f += (exp.sub() &
sub_t(math::exp::bin)) ?
"2" : (exp.sub() &
sub_t(math::exp::dec)) ?
"10" :
"";
974 f += llvm_suffix(exp->type());
976 declare(
"{} @{}({})", t, f, t);
977 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
979 auto a =
emit(er->arg());
980 auto t = convert(er->type());
981 auto f = er.id() == math::er::f ?
"erf"s :
"erfc"s;
982 f += math_suffix(er->type());
983 declare(
"{} @{}({})", t, f, t);
984 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
986 auto a =
emit(gamma->arg());
987 auto t = convert(gamma->type());
988 std::string f = gamma.id() == math::gamma::t ?
"tgamma" :
"lgamma";
989 f += math_suffix(gamma->type());
990 declare(
"{} @{}({})", t, f, t);
991 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
993 auto [a, b] = cmp->args<2>([
this](
auto def) {
return emit(def); });
994 auto t = convert(cmp->arg(0)->type());
999 case math::cmp:: e: op +=
"oeq";
break;
1000 case math::cmp:: l: op +=
"olt";
break;
1001 case math::cmp:: le: op +=
"ole";
break;
1002 case math::cmp:: g: op +=
"ogt";
break;
1003 case math::cmp:: ge: op +=
"oge";
break;
1004 case math::cmp:: ne: op +=
"one";
break;
1005 case math::cmp:: o: op +=
"ord";
break;
1006 case math::cmp:: u: op +=
"uno";
break;
1007 case math::cmp:: ue: op +=
"ueq";
break;
1008 case math::cmp:: ul: op +=
"ult";
break;
1009 case math::cmp::ule: op +=
"ule";
break;
1010 case math::cmp:: ug: op +=
"ugt";
break;
1011 case math::cmp::uge: op +=
"uge";
break;
1012 case math::cmp::une: op +=
"une";
break;
1014 default: fe::unreachable();
1017 return bb.
assign(
name,
"{} {} {}, {}", op, t, a, b);
1019 auto v_src =
emit(conv->arg());
1020 auto t_src = convert(conv->arg()->type());
1021 auto t_dst = convert(conv->type());
1023 auto s_src = math::isa_f(conv->arg()->type());
1024 auto s_dst = math::isa_f(conv->type());
1026 switch (conv.id()) {
1027 case math::conv::f2f: op = s_src < s_dst ?
"fpext" :
"fptrunc";
break;
1028 case math::conv::s2f: op =
"sitofp";
break;
1029 case math::conv::u2f: op =
"uitofp";
break;
1030 case math::conv::f2s: op =
"fptosi";
break;
1031 case math::conv::f2u: op =
"fptoui";
break;
1034 return bb.
assign(
name,
"{} {} {} to {}", op, t_src, v_src, t_dst);
1036 auto a =
emit(abs->arg());
1037 auto t = convert(abs->type());
1038 std::string f =
"llvm.fabs";
1039 f += llvm_suffix(abs->type());
1040 declare(
"{} @{}({})", t, f, t);
1041 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1043 auto a =
emit(round->arg());
1044 auto t = convert(round->type());
1045 std::string f =
"llvm.";
1046 switch (round.id()) {
1047 case math::round::f: f +=
"floor";
break;
1048 case math::round::c: f +=
"ceil";
break;
1049 case math::round::r: f +=
"round";
break;
1050 case math::round::t: f +=
"trunc";
break;
1052 f += llvm_suffix(round->type());
1053 declare(
"{} @{}({})", t, f, t);
1054 return bb.
assign(
name,
"tail call {} @{}({} {})", t, f, t, a);
1056 error(
"unhandled def in LLVM backend: {} : {}", def, def->
type());