Performing my own pass with a single command line?

Hello,

As far as I know, the LLVM pass manager only perform at the llvm
bytecode level.

This means for each program, I have to convert it to a LLVM bytecode by:

llvm-gcc -c -emit-llvm test.c

then, I can issue the llvm pass manager to invoke my own pass and
produce an output as LLVM bytecode, such as:

opt -my-pass < test.o > test.new.o

After this point, I need to convert it to assembly code using llc,
then to use 'as' to compile the assembly code to an object file, and
finally I can
use gcc to generate an executable program.

*****My question is*****, is there any way to automatically evoke the
pass manager, such as:

llvm-gcc -c test.c

and in such a way my own pass will be evoked?

Cheers,
Zheng

Dear Zheng Wang,

You have a few options:

1) You can add your pass to the set of passes that llvm-gcc runs when it optimizes code. This requires re-compiling llvm-gcc; it also means that your pass must be able to run on incomplete programs and be run multiple times over the same code (i.e., it cannot do whole-program analysis).

2) You can change the build system of the program you're working with to generate an LLVM bitcode file. It's the same as what you're doing now, except that the program's Makefiles automate the task for you.

3) You can add your passes to libLTO (http://llvm.org/docs/GoldPlugin.html and http://llvm.org/docs/LinkTimeOptimization.html). This will allow your passes to be run at link-time by the linker. This method takes some time to set up, but it should work beautfully once set up. Furthermore, it can do whole-program analysis and optimization (the linker knows when it is creating a final executable vs. an object file and can defer whole program analysis and transformation until the final link stage).

-- John T.

Zheng Wang wrote:

Hi John,

Are there any documents about how to add a pass into LTO?

Cheers,
Zheng

Zheng Wang wrote:

Hi John,

Are there any documents about how to add a pass into LTO?
  
No, but I believe you may want to look at LTOCodeGenerator::generateAssemblyCode in LTOCodeGenerator.cpp. That is where I added the SAFECode passes in libLTO from LLVM 2.6.

I should warn you that using libLTO for running additional passes is rather new. I've only used it once or twice myself. That said, it looks to be a very promising approach; I've got a graduate student here who is trying the approach out for his own project.

-- John T.