Help with optimizations.


Does clang do any optimizations, e.g. removing unused variables,
or, does clang leave all that to llvm?
If clang does optimizations, where in the source code does it do it?
I am looking to maybe use some of the optimization code from clang in
another decompiler project.

Kind Regards


Clang does a small number of non-obvious transformations itself:

1) Clang's expression emitter does do some peepholing, where the context
of an expression affects how it's emitted. This sort of thing is scattered through
IR-generation. For example, a C99 compound literal generally requires a
temporary object, but if it's used to initialize a local variable, we just evaluate
it in-place.

2) Clang also does a fair amount of constant folding. We generally try to emit
all initializers as constants using Clang's built-in constant-evaluator
(ExprConstant.cpp); when that doesn't apply, we emit code, and LLVM
implicitly does some constant-folding of its own (in IRBuilder.h).

3) Clang also does some implicit "optimizations" like not emitting variables
and functions with static linkage unless we really need them.

We generally do not do optimizations like local variable elimination or
data flow analysis; that's saved for LLVM.