[cfe-dev] Help with optimizations.

John McCall rjmccall at apple.com
Fri Mar 8 14:20:34 PST 2013


On Mar 8, 2013, at 1:45 PM, James Courtier-Dutton <james.dutton at gmail.com> wrote:
> Does clang do any optimizations, e.g. removing unused variables,
> or, does clang leave all that to llvm?

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.

John.



More information about the cfe-dev mailing list