How to run all available optimisations

Given a generated module in memory, how do you run all available optimisations on it?

Tutorial section 4.3 at picks out just a few optimisations, which makes sense for a JIT compiler on short running code, but I’m looking at long-running code where it would be worthwhile to start off applying all of them and possibly selectively disable any that turn out not to be useful.

C:\llvm\tools\opt\opt.cpp seems to be a program that runs optimisations from IR to IR, and perhaps runs all of the applicable ones; main() seems to contain a list of optimisation pass initialisations (though only the ones applicable where we aren’t generating machine code yet). Would copy-pasting the code from that file be the right starting point?

On the other hand, if so, I would have expected that code to occur again in the clang source tree, because clang likewise has a -O3 option, but it doesn’t seem to. That suggests to me that I’m confused about how things are organized. What am I missing?


LLVM provides a facility to initialize a “standard” optimization pass pipeline in PassManagerBuilder, with multiple options (including the optimization level).

It is used in clang (clang/CodeGen/BackendUtil.cpp) or in opt.cpp (see AddOptimizationPasses()).

You can compare your pipeline to clang by trying:

echo “” | clang -x c - -mllvm -debug-pass=Structure -c