LLVM Writing a Backend Pass

Writing a back-end pass is a bit trickier than writing a font-end pass, but once you get the hang of it it makes perfect sense. The following link is one of the few documents that llvm provides.
http://llvm.org/docs/WritingAnLLVMBackend.html This might not be of much help though, but still a good read.

In this project you will need to write a back-end pass for the x-86 architecture that inserts a function call into the source code. You will take as input a .bc file and then output an assembly (with llc) file that should have the new function call in it.

1. You should start this project off by compiling your original source code to bitcode (a .bc file) using clang and the -emit-llvm flag
2. You will need to add your pass source code file to the X86 folder in the Target directory of the llvm source tree. Name this file anything you like.
3. Add your source file to the CMakeLists.txt file in the X86 folder, so that llvm knows to compile your source code.
4. Add code to call your function pass from the X86.h file. Just follow the convention in that file already. Make sure you add the function that you call from X86.h to your pass source code file. Maybe look at some of the other passes to get an example of what I am talking about.
5. Add your pass by adding a call to the function addPass() in the X86TargetMachine.cpp file. Again you can see the convention for doing this in the file, just follow along to add a call to your pass.
6. Make sure your pass can create a machine function (note: this is not the same as a front-end function)
7. Make sure your pass can generate a machine instruction and make a call to the machine function, then insert the machine instruction into the code.
8. Compile your pass from the llvm build tree
9. Run your pass with the program llc (set the flag that enables your pass to run). Have llc output an assembly file.
10. Verify that your pass works by opening the assembly file and seeing that the function is actually inserted.

Note: llc will automatically have your pass enabled unless you add some extra code in the X86TargetMachine.cpp file.
At the top of the file you will need to insert something like the following code.
cl::opt
SomePassName("name-of-flag-to-enable-your-pass", cl::Hidden,
cl::desc("A good description of your pass for the help file of llc"),
cl::init(false));