Easy way to add common pass for optimization

Hi all,
I wrote a compiler front end which generates IR codes, but due to the logic, the IR codes have a lot of instructions like unnecessary br jump, basic block creation, load instructions and expression which can be simplified.
now I want to use built in passes to optimize the generated code, but after I read the tutorial: https://llvm.org/docs/tutorial/MyFirstLanguageFrontend, I found that I have to add passess one by one, on function level.

I know these passes for module level, but I don’t know what passes I should add, and I do not have special optimization requirements, so built-in passes will meet my requirements, So this there any easy way to add most common passes which all compilers will probable used?

It depends on if you’re using the new or old pass managers, though if you’re following that tutorial you’re probably using the old

one (looking at https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.html#llvm-optimization-passes).

If so then you can use PassManagerBuilder (http://llvm.org/doxygen/classllvm_1_1PassManagerBuilder.html). It may be also

worthwhile looking at what opt does (AddOptimizationLevel.cpp in opt.cpp) as it uses PassManagerBuilder but does something

a bit more complex than just setting the optlevel then populating the pass manager (but maybe just setting optlevel is all you

need for your purposes).


Hi John, I didn’t find any file named AddOptimizationLevel.cpp in my llvm project, where is it?

Sorry, I meant the function AddOptimizationPasses (line 365 of llvm/tools/opt/opt.cpp in latest trunk).