HU Yunfei, GU Fei, HAN Puyu
Accepted: 2026-06-03
In the process of compiler optimization for dynamically typed languages, a large number of runtime check nodes must be inserted due to the uncertainty of runtime types. Existing Redundant Code Elimination (RCE) algorithms based on reachability analysis generally treat all control flow nodes as potentially having side effects. As a result, computations and control flow structures associated with these check nodes are preserved during analysis, making it difficult to safely remove semantically redundant computations and control flows. To address this issue, a semantics-driven RCE method for the Ark runtime is proposed based on a systematic analysis of its compilation workflow and Intermediate Representation (IR) structure. The method begins with observable program semantics, where program behavior is abstracted as a sequence of observable events, including input/output operations, exception throwing, system calls, and termination with specific return values. Based on this abstraction, the RCE problem is formulated as the removal of IR subgraphs under the constraint that observable program semantics remain unchanged. On this basis, a criterion for eliminating runtime check nodes is introduced: a check node and its dependent computations can be safely removed if they produce no side effects and their results are not used by any node that affects observable program behavior. This criterion overcomes the interference introduced by runtime checks and allows the removal of computations that are preserved by traditional approaches despite being semantically redundant. Around this criterion, a semantics-constrained live node propagation mechanism is designed. The process starts with initializing an live node set containing side-effect nodes, followed by expanding this set along data dependency relations. Only nodes that may influence observable program behavior are retained in the set, enabling the identification and elimination of redundant computations and their associated check nodes. Furthermore, to address redundant control flows that cannot be handled by existing methods, control flow graph construction, dominance analysis, and loop structure identification algorithms are incorporated. A method for detecting and eliminating redundant loops and redundant branches is proposed, enabling the overall removal of structures such as empty loops and branches. The proposed method has been integrated into the Ark runtime compilation framework, achieving optimization of redundant computations and control flows at the IR level. Experimental results demonstrate that, in terms of executed instructions, the average reduction across all test cases reaches 3.4%, while representative cases containing redundant control flows achieve an average reduction of 27.4%, with a maximum of 98.26%. In terms of execution time, an average reduction of 3.4% is observed across all test cases, with representative cases achieving an average reduction of 26.4%, and up to 99.99% reduction in loop-intensive programs. Regarding compilation overhead, the execution time of the proposed algorithm accounts for only 2.28% of the total compilation time on average, indicating low additional cost. In overall performance evaluation, the total time of compilation and execution decreases in most representative cases, with a maximum reduction of 94.55%. In addition, validation on 913 runtime unit test cases and 19749 test262 standard test cases shows that no semantic deviation is introduced. Compared with source-level redundancy elimination approaches, further performance gains are achieved at the fine-grained computation and control flow levels, demonstrating the unique advantages of IR-level optimization. The proposed method effectively overcomes the limitations imposed by runtime checks on RCE in dynamically typed languages, significantly improves execution efficiency while preserving semantic equivalence, and maintains low compilation overhead, providing a new approach for compiler optimization in dynamic language runtimes.