MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
libcall.cpp
Go to the documentation of this file.
2
4#include <mim/plug/mem/mem.h>
5
7
8namespace mim::plug::mach {
9
10namespace {
11
12/// The libm name of @p app without the format suffix and its arity, or an empty name.
13std::pair<std::string, size_t> libm(const App* app) {
14 if (auto exp = Axm::isa<math::exp>(app)) {
15 if (exp.id() == math::exp::lBB || exp.id() == math::exp::LBB) return {};
16 auto name = std::string(exp.sub() & sub_t(math::exp::log) ? "log" : "exp");
17 if (exp.sub() & sub_t(math::exp::bin)) name += "2";
18 if (exp.sub() & sub_t(math::exp::dec)) name += "10";
19 return {name, 1};
20 }
21 if (auto tri = Axm::isa<math::tri>(app)) {
22 auto name = std::string(tri.sub() & sub_t(math::tri::a) ? "a" : "");
23 switch (math::tri((fe::to_underlying(tri.id()) & 0x3) | Annex::base<math::tri>())) {
24 case math::tri::sin: name += "sin"; break;
25 case math::tri::cos: name += "cos"; break;
26 case math::tri::tan: name += "tan"; break;
27 default: return {};
28 }
29 if (tri.sub() & sub_t(math::tri::h)) name += "h";
30 return {name, 1};
31 }
32 if (Axm::isa<math::pow>(app)) return {"pow", 2};
33 if (auto rt = Axm::isa<math::rt>(app); rt && rt.id() == math::rt::cb) return {"cbrt", 1};
34 if (auto er = Axm::isa<math::er>(app)) return {er.id() == math::er::f ? "erf" : "erfc", 1};
35 if (auto gamma = Axm::isa<math::gamma>(app)) return {gamma.id() == math::gamma::t ? "tgamma" : "lgamma", 1};
36 if (auto round = Axm::isa<math::round>(app)) {
37 switch (round.id()) {
38 case math::round::f: return {"floor", 1};
39 case math::round::c: return {"ceil", 1};
40 case math::round::r: return {"round", 1};
41 case math::round::t: return {"trunc", 1};
42 }
43 }
44 if (auto arith = Axm::isa<math::arith>(app); arith && arith.id() == math::arith::rem) return {"fmod", 2};
45 if (auto extrema = Axm::isa<math::extrema>(app)) {
46 if (extrema.id() == math::extrema::fmin) return {"fmin", 2};
47 if (extrema.id() == math::extrema::fmax) return {"fmax", 2};
48 }
49 return {};
50}
51
52} // namespace
53
54const Def* LibCall::call(Sym name, const App* app, Defs args) {
55 auto& w = new_world();
56 auto type = rewrite(app->type());
57 auto doms = DefVec(args.size(), [&](size_t i) { return args[i]->type(); });
58 doms.emplace_back(w.cn(type));
59 // One declaration per type: `malloc` of a `Ptr (T, 0)` for each `T`; all call the same symbol.
60 auto& decl = decls_[name][w.cn(doms)];
61 if (!decl) decl = w.mut_con(doms)->set(name);
62 return lift(decl, args, type);
63}
64
65const Def* LibCall::rewrite_imm_App(const App* app) {
66 if (liftable()) {
67 auto& w = new_world();
68 if (Axm::isa<mem::strlen>(app)) return call(w.sym("strlen"), app, rewrite(app->args()));
69 if (auto malloc = Axm::isa<mem::malloc>(app); malloc || Axm::isa<mem::free>(app)) {
70 check_addr_space(app, app->decurry()->arg(2, 1));
71 return call(w.sym(malloc ? "malloc" : "free"), app, rewrite(app->args()));
72 }
73
74 auto [name, n] = libm(app);
75 auto width = math::isa_f(app->type());
76 if (!name.empty() && (width == 32 || width == 64)) {
77 auto args = DefVec(n, [&](size_t i) { return rewrite(app->arg(n, i)); });
78 return call(w.sym(*width == 32 ? name + "f" : name), app, args);
79 }
80 }
81 return DS2CPS::rewrite_imm_App(app);
82}
83
84} // namespace mim::plug::mach
auto args(Projector auto f) const noexcept
Definition lam.h:252
const App * decurry() const
Returns App::callee again as App.
Definition lam.h:238
const Def * arg() const
Definition lam.h:251
static auto isa(const Def *def)
Definition axm.h:112
const Def * lift(const Def *k, Defs new_args, const Def *new_type, bool filter=false, Sym name={})
Records the call k (new_args..., cont) as pending and returns the Var of cont: Cn new_type as its res...
Definition ds2cps.cpp:11
bool liftable() const
Is the body of a continuation being rewritten, so that lift() is legal?
Definition ds2cps.h:55
Base class for all Defs.
Definition def.h:285
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1116
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
Definition phase.cpp:23
std::string_view name() const
Definition phase.h:63
World & new_world()
Create new Defs into this.
Definition phase.h:418
const Def * rewrite_imm_App(const App *) override
Definition phase.cpp:317
virtual const Def * rewrite(const Def *)
Definition rewrite.cpp:62
const Def * rewrite_imm_App(const App *) final
Definition libcall.cpp:65
The mach Plugin
Definition asm.h:15
void check_addr_space(const App *app, const Def *a)
Bails unless the address space a of the mem operation app is 0.
Definition mach.h:31
std::optional< nat_t > isa_f(const Def *def)
Definition math.h:55
u8 sub_t
Definition types.h:42
fe::View< const Def * > Defs
Definition def.h:94
fe::Vector< const Def * > DefVec
Definition def.h:96
static consteval flags_t base()
Definition plugin.h:278