Exploiting Undefined Behavior in C/C++ Programs for Optimization: A Study on the Performance Impact

Lucian Popescu and Nuno P. Lopes

 

Abstract:

The C and C++ languages define hundreds of cases as having undefined behavior (UB). These include, for example, corner cases where different CPU architectures disagree on the semantics of an instruction and the language does not want to force a specific implementation (e.g., shift by a value larger than the bitwidth). Another class of UB involves errors that the language chooses not to detect because it would be too expensive or impractical, such as dereferencing out-of-bounds pointers.
Although there is a common belief within the compiler community that UB enables certain optimizations that would not be possible otherwise, no rigorous large-scale studies have been conducted on this subject. At the same time, there is growing interest in eliminating UB from programming languages to improve security.
In this paper, we present the first comprehensive study that examines the performance impact of exploiting UB in C and C++ applications across multiple CPU architectures. Using LLVM, a compiler known for its extensive use of UB for optimizations, we demonstrate that, for the benchmarks and UB categories that we evaluated, the end-to-end performance gains are minimal. Moreover, when performance regresses, it can often be recovered through small improvements to optimization algorithms or by using link-time optimizations.

 

Published:

L. Popescu and N. P. Lopes. Exploiting Undefined Behavior in C/C++ Programs for Optimization: A Study on the Performance Impact. Proc. of the ACM on Programming Languages, Volume 9 Issue PLDI, June 2025.

 

Download:

 

Bibtex:

@article{ub-pldi25,
  title =	{Exploiting Undefined Behavior in C/C++ Programs for Optimization: A Study on the Performance Impact},
  author =	{Lucian Popescu and Nuno P. Lopes},
  journal =	{Proc. of the ACM on Programming Languages},
  volume =	{9},
  number =	{PLDI},
  publisher =	{ACM},
  doi =		{10.1145/3729260},
  month =	jun,
  year =	2025
}

 

Copyright notice:

© The Authors, 2025. Licensed under the Creative Commons Attribution 4.0 International License.

 

<-- Return