LLVM Essentials

Book description

Become familiar with the LLVM infrastructure and start using LLVM libraries to design a compiler

About This Book

  • Learn to use the LLVM libraries to emit intermediate representation (IR) from high-level language
  • Build your own optimization pass for better code generation
  • Understand AST generation and use it in a meaningful way

Who This Book Is For

This book is intended for those who already know some of the concepts of compilers and want to quickly get familiar with the LLVM infrastructure and the rich set of libraries that it provides.

What You Will Learn

  • Get an introduction to LLVM modular design and LLVM tools
  • Convert frontend code to LLVM IR
  • Implement advanced LLVM IR paradigms
  • Understand the LLVM IR Optimization Pass Manager infrastructure and write an optimization pass
  • Absorb LLVM IR transformations
  • Understand the steps involved in converting LLVM IR to Selection DAG
  • Implement a custom target using the LLVM infrastructure
  • Get a grasp of C's frontend clang, an AST dump, and static analysis

In Detail

LLVM is currently the point of interest for many firms, and has a very active open source community. It provides us with a compiler infrastructure that can be used to write a compiler for a language. It provides us with a set of reusable libraries that can be used to optimize code, and a target-independent code generator to generate code for different backends. It also provides us with a lot of other utility tools that can be easily integrated into compiler projects.

This book details how you can use the LLVM compiler infrastructure libraries effectively, and will enable you to design your own custom compiler with LLVM in a snap.

We start with the basics, where you'll get to know all about LLVM. We then cover how you can use LLVM library calls to emit intermediate representation (IR) of simple and complex high-level language paradigms. Moving on, we show you how to implement optimizations at different levels, write an optimization pass, generate code that is independent of a target, and then map the code generated to a backend. The book also walks you through CLANG, IR to IR transformations, advanced IR block transformations, and target machines.

By the end of this book, you'll be able to easily utilize the LLVM libraries in your own projects.

Style and approach

This book deals with topics sequentially, increasing the difficulty level in a step-by-step approach. Each topic is explained with a detailed example, and screenshots are included to help you understand the examples.

Table of contents

  1. LLVM Essentials
    1. Table of Contents
    2. LLVM Essentials
    3. Credits
    4. About the Authors
    5. About the Reviewer
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Playing with LLVM
      1. Modular design and collection of libraries
      2. Getting familiar with LLVM IR
      3. LLVM tools and using them in the command line
      4. Summary
    9. 2. Building LLVM IR
      1. Creating an LLVM module
      2. Emitting a function in a module
      3. Adding a block to a function
      4. Emitting a global variable
      5. Emitting a return statement
      6. Emitting function arguments
      7. Emitting a simple arithmetic statement in a basic block
      8. Emitting if-else condition IR
      9. Emitting LLVM IR for loop
      10. Summary
    10. 3. Advanced LLVM IR
      1. Memory access operations
      2. Getting the address of an element
      3. Reading from the memory
      4. Writing into a memory location
      5. Inserting a scalar into a vector
      6. Extracting a scalar from a vector
      7. Summary
    11. 4. Basic IR Transformations
      1. Opt Tool
      2. Pass and Pass Manager
      3. Using other Pass info in current Pass
        1. AnalysisUsage::addRequired<> method
        2. AnalysisUsage:addRequiredTransitive<> method
        3. AnalysisUsage::addPreserved<> method
      4. Instruction simplification example
      5. Instruction Combining
      6. Summary
    12. 5. Advanced IR Block Transformations
      1. Loop processing
      2. Scalar evolution
      3. LLVM intrinsics
      4. Vectorization
      5. Summary
    13. 6. IR to Selection DAG phase
      1. Converting IR to selectionDAG
      2. Legalizing SelectionDAG
      3. Optimizing SelectionDAG
      4. Instruction Selection
      5. Scheduling and emitting machine instructions
      6. Register allocation
      7. Code Emission
      8. Summary
    14. 7. Generating Code for Target Architecture
      1. Sample backend
        1. Defining registers and register sets
        2. Defining the calling convention
        3. Defining the instruction set
      2. Implementing frame lowering
      3. Lowering instructions
      4. Printing an instruction
      5. Summary
    15. Index

Product information

  • Title: LLVM Essentials
  • Author(s): Suyog Sarda, Mayur Pandey
  • Release date: December 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781785280801