Does a compiler look for recurrent expressions to convert it into 'function' to reduce binary size and improve performance?

Of course, the obvious answer might be "some do it, some don't", so I ask for compiler of mainstream language, let's take for example : Java SDK compiler, GCC compiler and Clang.

If "yes, they do it", is it frequent or in very specific case? (A code example where it actually happens might be very instructive)

  • 4
    This optimization is known as Common Subexpression Elimination, and is fairly common for off-line compilers.
    – amon
    Sep 22 '14 at 9:57
  • 1
    @amon If I understand your link, it isn't what I'm asking for, the Common Subexpression Elimination looks like storing a result that is used multiple times, in my case I'm asking if compiler avoid repeating instruction in the binary.
    – anopse
    Sep 22 '14 at 10:01
  • 2
    They could and they often do, especially when optimizing for space (e.g. the -Os flag of GCC) Sep 22 '14 at 10:09
  • If you don't mean common subexpression elimination, you might be interested in Threaded Code, which is a technique for extremely space-efficient compilation. It was popular when using the Forth language, but today it's not generally useful outside of embedded programming.
    – amon
    Sep 22 '14 at 10:15
  • 1
    I remember reading about the visual studio debugger somewhere. It sometimes jumps into the wrong function. The reason is that the linker noticed that 2 functions have identical assembler code (usually getters/setters) even though they are different at c++ level, so the linker uses the exact same implementation for different functions and the debugger cannot tell the original function and just picks one, sometimes the wrong one.
    – nwp
    Sep 22 '14 at 22:09

They don‘t do it.

What you are looking for is clone detection and automatic code deduplication. There are many tools to discover duplicate code (some quite advanced, see AST based clone detectors) and a good literature about this topic but usually only two cases of duplication are managed by the compiler:

  • common subexpression elimination
  • code vectorization

Anyway consider that code duplication is bad for readability/maintainability of source code not necessarily for performance of compiled code.

Some related questions are:


Some linkers can perform the Identical Code Folding (ICF): at link time, ICF detects functions with identical object code and merges them into a single copy.


  • ICF can be unsafe as it can change the run-time behaviour of code that relies on each function having a unique address (C99 guarantees that the addresses of two different functions are not equal; for C++11 it isn't so clear). ICF can be used in a safe mode where it identifies and folds functions whose addresses are guaranteed not to have been used in comparison operations. Further, profiling and debugging binaries with merged functions can be confusing, as the PC values of merged functions cannot be always disambiguated to point to the correct function
  • this is a somewhat "passive" optimization with a limited scope: mainly useful for C++ templates.
  • I know that it is very bad for maintainability, but I was just asking anyway because I was wondering if the compiler would do it or not.
    – anopse
    Sep 22 '14 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.