What are Optimization Steps in LLVM

I want to know steps of optimizations in LLVM with details and if is possible explore these steps.
I saw O0 codes from clang that is very bad and also O2 codes from clang that is very optimized and intelligent. Want to know is it exactly O0 code that is reformed to O2 step by step or not?

Does LLVM generate directedly unoptimized code and then it will be optimized in next steps?

Generally, yes. Though, each frontend is different. The above is mostly true for Clang.

1 Like

Concretely, I think you can take a look at PassBuilder.cpp to see the name of the optimizations.


Thanks for kindly answering.
Another question what is the best way to dive in to the source code of LLVM?

You can take a look at Github issues or find the existing FIXMEs. This is what I did.

1 Like

If you pass -mllvm -print-after-all to clang then it will print the IR after every pass. You might find this interesting to understand how each transform works. In particular, you might notice that a few passes make the code worse but easier to reason about, to enable later passes to work better.

1 Like


While at it, what are the missing optimizations steps? The industrial LLVMs are quite a bit faster.