Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Open a Makefile in the llvm lib/Transform folder:

    $ vi Makefile
    
  2. 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 in a...