0

I do not quite understand the definition of pass in the llvm. Does it mean I can only use opt command to run the program?

My situation is like I want to find loops in a CFG of basic blocks and I want to use LLVM API instead of writing code by myself. I found a file called Loopinfo http://llvm.org/docs/doxygen/html/LoopInfo_8h_source.html which includes pass.h and class passinfo inherited from Functionpass. Does it mean I can only use opt command to call instead of writing a normal project which uses some of class's functions and build and execute? I hope I clarified my question clearly.

Min Gao
  • 383
  • 4
  • 16

2 Answers2

0

The easiest way is to add pass executed via opt command. But, you should be able to create dedicated executable which reads LLVM bitcode, performs your pass and writes bitcode back.

See here for an example:

Parsing and Modifying LLVM IR code

Source of opt command might also be useful:

https://llvm.org/svn/llvm-project/llvm/trunk/tools/opt/opt.cpp

Community
  • 1
  • 1
Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65
0

You can analyze and manipulate LLVM IR just fine without knowing anything about passes. Just use the LLVM API and you'll be OK.

So what's the deal with passes? Well, if you do write your analysis or transformation in the form of a pass - by following this guide - you can still just use it as any regular C++ class1, but you get some advantages:

  1. You can use the opt tool to run your pass. It will take care of everything else for you (e.g. loading the IR), it makes it very easy to run other passes before or after your pass (including the useful verification pass), makes it easy to enable/disable debug mode, etc.

  2. You can easily combine your pass with other passes using a pass manager, which is very convenient (will take care of pass dependencies for you, for example).

So in general, writing things in the form of passes is recommended but not required.



1 Well if you define requirements on other passes then you'll have to run those yourself if you're not using opt or a pass manager

Oak
  • 26,231
  • 8
  • 93
  • 152