Can C ++ compilers automatically eliminate duplicate code?

Copying duplicates is usually bad and often quite easy to spot. I believe that compilers could automatically detect this in the simplest cases β€” they already parse the text and get an intermediate representation, which they parse differently β€” they detect suspicious patterns such as uninitialized variables, optimize the emitted code, etc. I assume that they could often detect functionally duplicate code in the same way and consider it when issuing machine code.

Are there C ++ compilers that can detect duplicate code and only select the corresponding machine code once instead of each duplicate in the source code?

+6
c ++ compiler-construction
source share
4 answers

Some, some do not.

On the LLVM optimization page: - mergefunc ( Skipping MergeFunctions, how it works )

Functions are separated by small blocks in an intermediate LLVM view; this optimization session tries to combine similar blocks. This does not guarantee success, though.

On this page you will find many other optimizations, although some of them may seem mysterious at first sight.

I would add, however, that duplicate code is not so bad for the compiler / executable, it is bad from a maintenance point of view, and the compiler cannot do anything about it.

+9
source share

I think the question makes a false assumption that compilers would always like to eliminate code duplication. code duplication is bad for readability / maintainability of the source code, which does not necessarily lead to compiled code; one could consider the deployment cycle as a compiler that adds duplicate code to increase speed. compiled code should not follow the same principles as the source code, and, as a rule, not like for a machine that people don’t read.

usually compilers are busy compiling without converting the source code, of course, IDEs can both.

+8
source share

As far as I know, code exception usually does not occur in all functions. Therefore, if you write several duplicate code in two different functions, there is very little chance (almost none) that part of the code will be eliminated.

There are some optimizations, such as return value optimization , function inlining , that can be performed through functions. However, most of the optimization is done inside the function itself. Usually this is not done at a higher level of the language, so I mean that the compiler does not look at C ++ code and starts optimizing it. Compilers basically have an intermediate representation, between a high-level language (C ++) and a machine language. This intermediate representation (IR) is somewhat similar to machine language, but is not the machine language of the system on which the code is compiled. See the wiki page http://en.wikipedia.org/wiki/Compiler_optimization for a list of some of these optimizations

+3
source share

Visual C ++ does this if you specify "minimize code size" (/ O1). The provided feature is described in the docs for / Og , which is deprecated in favor of simpler choices to support size or favorable speed (/ O2).

+1
source share

All Articles