Writing your own LLVM pass
All LLVM passes are subclasses of the pass class, and they implement functionality by overriding the virtual methods inherited from pass. LLVM applies a chain of analyses and transformations on the target program. A pass is an instance of the Pass LLVM class.
Getting ready
Let's see how to write a pass. Let's name the pass function block counter; once done, it will simply display the name of the function and count the basic blocks in that function when run. First, a Makefile needs to be written for the pass. Follow the given steps to write a Makefile:
- Open a
Makefilein thellvm lib/Transformfolder:$ vi Makefile - Specify the path to the LLVM root folder and the library name, and make this pass a loadable module by specifying it in
Makefile, as follows:LEVEL = ../../.. LIBRARYNAME = FuncBlockCount LOADABLE_MODULE = 1 include $(LEVEL)/Makefile.common
This Makefile specifies that all the .cpp files in the current directory are to be compiled and linked together...