2 ** DCE: Dead Code Elimination. Pre-LOOP only -- ASM already performs DCE.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
17 /* Some local macros to save typing. Undef'd at the end. */
18 #define IR(ref) (&J->cur.ir[(ref)])
20 /* Scan through all snapshots and mark all referenced instructions. */
21 static void dce_marksnap(jit_State
*J
)
23 SnapNo i
, nsnap
= J
->cur
.nsnap
;
24 for (i
= 0; i
< nsnap
; i
++) {
25 SnapShot
*snap
= &J
->cur
.snap
[i
];
26 SnapEntry
*map
= &J
->cur
.snapmap
[snap
->mapofs
];
27 MSize n
, nent
= snap
->nent
;
28 for (n
= 0; n
< nent
; n
++) {
29 IRRef ref
= snap_ref(map
[n
]);
31 irt_setmark(IR(ref
)->t
);
36 /* Backwards propagate marks. Replace unused instructions with NOPs. */
37 static void dce_propagate(jit_State
*J
)
39 IRRef1
*pchain
[IR__MAX
];
42 for (i
= 0; i
< IR__MAX
; i
++) pchain
[i
] = &J
->chain
[i
];
43 for (ins
= J
->cur
.nins
-1; ins
>= REF_FIRST
; ins
--) {
45 if (irt_ismarked(ir
->t
)) {
47 pchain
[ir
->o
] = &ir
->prev
;
48 } else if (!ir_sideeff(ir
)) {
49 *pchain
[ir
->o
] = ir
->prev
; /* Reroute original instruction chain. */
51 ir
->o
= IR_NOP
; /* Replace instruction with NOP. */
52 ir
->op1
= ir
->op2
= 0;
56 if (ir
->op1
>= REF_FIRST
) irt_setmark(IR(ir
->op1
)->t
);
57 if (ir
->op2
>= REF_FIRST
) irt_setmark(IR(ir
->op2
)->t
);
61 /* Dead Code Elimination.
63 ** First backpropagate marks for all used instructions. Then replace
64 ** the unused ones with a NOP. Note that compressing the IR to eliminate
65 ** the NOPs does not pay off.
67 void lj_opt_dce(jit_State
*J
)
69 if ((J
->flags
& JIT_F_OPT_DCE
)) {