summaryrefslogtreecommitdiff
path: root/libs/luajit-cmake/luajit/src/lj_opt_dce.c
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-03-11 15:58:20 -0600
committersanine <sanine.not@pm.me>2023-03-11 15:58:20 -0600
commitebc50b387ab209c9f9a0d92e340ac293d5697274 (patch)
treeea8c8b3677a18c994d2b9d33dbef3461dcf18113 /libs/luajit-cmake/luajit/src/lj_opt_dce.c
parentc2329b4c8258baa9429c77566c9def97d00e96d7 (diff)
build & link with luajit instead of lua5.1
Diffstat (limited to 'libs/luajit-cmake/luajit/src/lj_opt_dce.c')
-rw-r--r--libs/luajit-cmake/luajit/src/lj_opt_dce.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/libs/luajit-cmake/luajit/src/lj_opt_dce.c b/libs/luajit-cmake/luajit/src/lj_opt_dce.c
new file mode 100644
index 0000000..cff54a8
--- /dev/null
+++ b/libs/luajit-cmake/luajit/src/lj_opt_dce.c
@@ -0,0 +1,75 @@
+/*
+** DCE: Dead Code Elimination. Pre-LOOP only -- ASM already performs DCE.
+** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#define lj_opt_dce_c
+#define LUA_CORE
+
+#include "lj_obj.h"
+
+#if LJ_HASJIT
+
+#include "lj_ir.h"
+#include "lj_jit.h"
+#include "lj_iropt.h"
+
+/* Some local macros to save typing. Undef'd at the end. */
+#define IR(ref) (&J->cur.ir[(ref)])
+
+/* Scan through all snapshots and mark all referenced instructions. */
+static void dce_marksnap(jit_State *J)
+{
+ SnapNo i, nsnap = J->cur.nsnap;
+ for (i = 0; i < nsnap; i++) {
+ SnapShot *snap = &J->cur.snap[i];
+ SnapEntry *map = &J->cur.snapmap[snap->mapofs];
+ MSize n, nent = snap->nent;
+ for (n = 0; n < nent; n++) {
+ IRRef ref = snap_ref(map[n]);
+ if (ref >= REF_FIRST)
+ irt_setmark(IR(ref)->t);
+ }
+ }
+}
+
+/* Backwards propagate marks. Replace unused instructions with NOPs. */
+static void dce_propagate(jit_State *J)
+{
+ IRRef1 *pchain[IR__MAX];
+ IRRef ins;
+ uint32_t i;
+ for (i = 0; i < IR__MAX; i++) pchain[i] = &J->chain[i];
+ for (ins = J->cur.nins-1; ins >= REF_FIRST; ins--) {
+ IRIns *ir = IR(ins);
+ if (irt_ismarked(ir->t)) {
+ irt_clearmark(ir->t);
+ pchain[ir->o] = &ir->prev;
+ } else if (!ir_sideeff(ir)) {
+ *pchain[ir->o] = ir->prev; /* Reroute original instruction chain. */
+ lj_ir_nop(ir);
+ continue;
+ }
+ if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t);
+ if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t);
+ }
+}
+
+/* Dead Code Elimination.
+**
+** First backpropagate marks for all used instructions. Then replace
+** the unused ones with a NOP. Note that compressing the IR to eliminate
+** the NOPs does not pay off.
+*/
+void lj_opt_dce(jit_State *J)
+{
+ if ((J->flags & JIT_F_OPT_DCE)) {
+ dce_marksnap(J);
+ dce_propagate(J);
+ memset(J->bpropcache, 0, sizeof(J->bpropcache)); /* Invalidate cache. */
+ }
+}
+
+#undef IR
+
+#endif