Book Image

Getting started with LLVM core libraries

Book Image

Getting started with LLVM core libraries

Overview of this book

Table of Contents (17 chapters)
Getting Started with LLVM Core Libraries
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Writing your own machine pass


In this section, we will show how you can write a custom machine pass to count, just before code emission, how many machine instructions each function has. Differing from IR passes, you cannot run this pass with the opt tool, or load the pass and schedule it to happen via the command line. Machine passes are determined by the backend code. Therefore, we will modify an existing backend to run with our custom pass to see it in practice. We will choose SPARC for that end.

Recall from the Demonstrating the pluggable pass interface section in Chapter 3, Tools and Design, and from the white boxes in the first diagram of this chapter, that we have many options to decide where our pass should run. To use these methods, we should look for the TargetPassConfig subclass that our backend implements. If you use grep, you will find it at SparcTargetMachine.cpp:

$ cd <llvmsource>/lib/Target/Sparc
$ vim SparcTargetMachine.cpp  # use your preferred editor

Looking into the...