Writing a custom LLVM IR generator

It is possible to use the LLVM IR generator API to programmatically build the IR for sum.ll (created at the -O0 optimization level, that is, without optimizations). In this section, you will see how to do it step by step. First, take a look at which header files are needed:

  • #include <llvm/ADT/SmallVector.h>: This is used to make the SmallVector<> template available, a data structure to aid us in building efficient vectors when the number of elements is not large. Check http://llvm.org/docs/ProgrammersManual.html for help on LLVM data structures.
  • #include <llvm/Analysis/Verifier.h>: The verifier pass is an important analysis that checks whether your LLVM module is well formed with respect to the IR rules.
  • #include ...

Get Getting Started with LLVM Core Libraries now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.