DCE - LLM : Dead Code Elimination with Large Language Models (2025)

Abstract: Dead code introduces several challenges in software development, such as increased binary size and maintenance difficulties. It can also obscure logical errors and be exploited for obfuscation in malware. For LLM-based code-related tasks, dead code introduces vulnerabilities that can mislead these models, raising security concerns. Although modern compilers and IDEs offer dead code elimination, sophisticated patterns can bypass these tools. A universal approach that includes classification, location, explanation, and correction is needed, yet current tools often require significant manual effort. We present DCE-LLM, a framework for automated dead code elimination using a small CodeBERT model with an attribution-based line selector to efficiently locate suspect code. LLMs then generate judgments and explanations, fine-tuned on a large-scale, annotated dead code dataset to provide detailed explanations and patches. DCE-LLM outperforms existing tools, with advanced unreachability detection, automated correction, and support for multiple programming languages. Experimental results show DCE-LLM achieves over 94% F1 scores for unused and unreachable code, significantly surpassing GPT-4o by 30%.

## introduction dead code, defined as segments of a program that are never executed or that do not affect program functionality, can introduce performance bottlenecks and obscure potential security vulnerabilities. the presence of dead code increases binary size, which negatively impacts system performance, particularly in resource-constrained environments like web applications @xcite . additionally, dead code complicates software maintenance by cluttering the codebase, thus consuming developers' time as they attempt to comprehend its purpose. in more critical cases, dead code can serve as a vector for obfuscation techniques employed in adversarial attacks, complicating code comprehension and analysis. though large language models (llms) have achieved impressive results in various code-related tasks @xcite @xcite @xcite , they are particularly vulnerable when faced with dead code. dead code, whether introduced unintentionally or as part of an attack, can disrupt a model's ability to accurately interpret program logic. in particular, dead code injection attacks exploit this vulnerability by adding redundant or unreachable code, which confuses the model and leads to incorrect predictions or faulty analyses. studies have shown that this can cause a significant drop in accuracy for tasks like vulnerability detection, with an observed reduction of up to 12.7% @xcite . these weaknesses highlight the need for more robust approaches to handling dead code in llm-based tasks. while modern compilers can remove basic dead variables and ides can flag some dead code @xcite , effectively eliminating all forms of dead code remains a challenge. moreover, sophisticated attack patterns using unreachable statements can bypass these tools, leading to potential security vulnerabilities and performance issues. these attacks exploit weaknesses in language models, embedding dead code that remains undetected. additionally, the pervasiveness of dead code across different programming languages complicates the development of a universal solution, as each language has its own syntax and semantics. as a result, a robust and versatile dead code elimination framework is essential, one that can automate the processes of classification, detection, explanation, and correction, thereby reducing the need for manual intervention. llms face several key challenges when it comes to dead code elimination. first, llms are trained on publicly available code datasets, many of which contain dead code. however, these datasets lack specific labels for dead code, meaning llms do not explicitly learn to identify or handle it during training. for example, we find that over 45.3% of java code in the codenet @xcite dataset contains dead code, but this is not filtered or annotated, leading to gaps in the models' understanding. second, llms struggle to locate dead code in long inputs. dead code can appear in various parts of a program, often scattered across lines of code. llms have difficulty maintaining robust context over long inputs, making it challenging to accurately detect and address dead code in complex or extended codebases. third, while llms excel at tasks like code generation and refactoring, explaining and correcting dead code presents a different challenge. properly addressing dead code requires a deep understanding of the intended functionality and context of the program, which llms may fail to grasp. additionally, generating semantically correct patches for dead code requires advanced reasoning capabilities that go beyond simple code completion or modification tasks. to address these challenges, we present dce-llm, an llm-empowered framework for comprehensive dead code elimination. we leverage a relatively small codebert model with a novel attribution-value-based line selector to effectively and accurately locate suspect dead code snippets in long code inputs. this step filters out most normal code, reducing the call time for llms. subsequently, llms generate final judgments and explanations for dead code, focusing on the highlighted suspect lines. furthermore, we fine-tune llms on the first large-scale dead code dataset, which is automatically annotated, equipping them with the capability to generate explanations and patches for dead code. as the first neural-based framework for dead code elimination, dce-llm demonstrates several advantages over existing tools: unreachability checking. dce-llm effectively detects complex unreachable dead code that can bypass compilers and ides, addressing vulnerabilities used in adversarial attacks. explanation and correction. dce-llm automatically provides explanations and patches for dead code, significantly reducing the manual effort required by developers and enhancing overall development efficiency. programming language support. dce-llm leverages the natural ability of llms to understand and generate code in multiple programming languages. it supports multiple programming languages, including python and java, making it applicable across diverse development environments. our experimental results demonstrate that dce-llm achieves f1 scores over 94% for both unused and unreachable code, along with f1 scores significantly surpassing those of existing llms and ides. moreover, in terms of the quality of generated content, including explanations and repaired code, dce-llm outperformed baseline models in arena-style human evaluations. we highlight our contributions as follows: ## background as illustrated in figure 1 , python method fill_str contains several issues about two primary types of dead code. unused code refers to code defined or executed but whose result is never used in any other computation. the execution of dead code wastes computation time and memory. in our example, the author of this method has accidentally put quotes around s3 in line 11, resulting in an unused variable s3 in the fourth line in light ## methodology this section introduces dce-llm (figure 2 ), our novel approach for automated dead code elimination, comprising training (yellow) and inference (green) phases. in training, we first train a codebert-based pivot model for high recall on a constructed dead code dataset. we then leverage gpt-4o to generate high-quality annotations, including explanations and improved code, for finetuning an llm to enhance detection accuracy and code suggestion quality. inference begins with the pivot model filtering suspect code. our novel dead code attribution technique then precisely locates and incorporates potential dead code lines into llm prompts. finally, the llm uses pivot model predictions as hints to generate practical dead code removal suggestions. ## conclusion in this paper, we proposed dce-llm, the first learning-based solution for the dead code elimination task that leverages a pivot model for filtering and recall, and an llm for explanation and code fixing. the llm is empowered with location information provided by the dead code attribution technique. by fine-tuning both the pivot model and llm on the first large-scale dead code dataset, we successfully constructed a complete pipeline for dead code elimination, encompassing classification, location, explanation, and repair. our proposed framework demonstrates superior performance in both classification and generation tasks. additionally, the versatility of dce-llm enables generalization on other programming languages.