LLVM Cookbook

Book description

Over 80 engaging recipes that will help you build a compiler frontend, optimizer, and code generator using LLVM

  • Write a frontend for any language to generate LLVM IR

  • Create optimization passes to optimize the IR code using LLVM Pass Infrastructure and Pass Manager

  • Design and implement structures for highly-optimized compilers using LLVM, through detailed step-by-step recipes

  • In Detail

    LLVM is a compiler framework with libraries that provides a modern source-and target-independent optimizer, along with a code generator.

    This book not only explains the effective use of the compiler infrastructure that LLVM provides, but also helps you implement it in one of your projects. You start with a simple task to get you up-and-running with LLVM, followed by learning the process of writing a frontend for a language, which includes writing a lexer, a parser, and generating IR code. You will then see how to implement optimizations at different levels, generate target-independent code, and then map this generated code to a backend. Finally, you will look into the functionalities that the LLVM infrastructure provides, such as exception handling, LLVM Utility Passes, using sanitizers, the garbage collector, and how we can use these in our projects.

    Table of contents

    1. LLVM Cookbook
      1. Table of Contents
      2. LLVM Cookbook
      3. Credits
      4. About the Authors
      5. About the Reviewers
      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. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. LLVM Design and Use
        1. Introduction
        2. Understanding modular design
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Cross-compiling Clang/LLVM
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Converting a C source code to LLVM assembly
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Converting IR to LLVM bitcode
          1. Getting Ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Converting LLVM bitcode to target machine assembly
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Converting LLVM bitcode back to LLVM assembly
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Transforming LLVM IR
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Linking LLVM bitcode
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Executing LLVM bitcode
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        11. Using the C frontend Clang
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. See also
        12. Using the GO frontend
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        13. Using DragonEgg
          1. Getting ready
          2. How to do It…
          3. See also
      9. 2. Steps in Writing a Frontend
        1. Introduction
        2. Defining a TOY language
          1. How to do it…
        3. Implementing a lexer
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Defining Abstract Syntax Tree
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Implementing a parser
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Parsing simple expressions
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Parsing binary expressions
          1. Getting ready
          2. How to do it…
          3. See also
        8. Invoking a driver for parsing
          1. How to do it…
          2. How it works…
          3. See also
        9. Running lexer and parser on our TOY language
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        10. Defining IR code generation methods for each AST class
          1. Getting ready
          2. How to do it…
          3. How it works…
        11. Generating IR code for expressions
          1. How to do it…
          2. See also
        12. Generating IR code for functions
          1. How to do it…
          2. How it works…
          3. See also
        13. Adding IR optimization support
          1. How to do it…
          2. See also
      10. 3. Extending the Frontend and Adding JIT Support
        1. Introduction
        2. Handling decision making paradigms – if/then/else constructs
          1. Getting ready
          2. How to do it...
          3. How it works…
          4. See also
        3. Generating code for loops
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Handling user-defined operators – binary operators
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Handling user-defined operators – unary operators
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Adding JIT support
          1. How to do it...
          2. How it works…
      11. 4. Preparing Optimizations
        1. Introduction
        2. Various levels of optimization
          1. Getting ready...
          2. How to do it…
          3. How it works…
          4. See Also
        3. Writing your own LLVM pass
          1. Getting ready
          2. How to do it…
          3. How it works
          4. See also
        4. Running your own pass with the opt tool
          1. How to do it…
          2. How it works…
          3. See also
        5. Using another pass in a new pass
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Registering a pass with pass manager
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See Also
        7. Writing an analysis pass
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Writing an alias analysis pass
          1. Getting ready
          2. How to do it...
          3. How it works…
          4. See also
        9. Using other analysis passes
          1. Getting ready…
          2. How to do it…
          3. How it works…
          4. See also
      12. 5. Implementing Optimizations
        1. Introduction
        2. Writing a dead code elimination pass
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Writing an inlining transformation pass
          1. Getting ready
          2. How to do it…
          3. How it works...
        4. Writing a pass for memory optimization
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Combining LLVM IR
          1. Getting started
          2. How to do it…
          3. How it works…
          4. See also
        6. Transforming and optimizing loops
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Reassociating expressions
          1. Getting Ready
          2. How to do it…
          3. How it works …
        8. Vectorizing IR
          1. Getting ready
          2. How to do it...
          3. How it works…
          4. See also…
        9. Other optimization passes
          1. Getting ready…
          2. How to do it…
          3. How it works…
          4. See also
      13. 6. Target-independent Code Generator
        1. Introduction
        2. The life of an LLVM IR instruction
          1. C Code to LLVM IR
          2. IR optimization
          3. LLVM IR to SelectionDAG
          4. SelectionDAG legalization
          5. Conversion from target-independent DAG to machine DAG
          6. Scheduling instructions
          7. Register allocation
          8. Code emission
        3. Visualizing LLVM IR CFG using GraphViz
          1. Getting ready
          2. How to do it…
          3. See also
        4. Describing targets using TableGen
          1. Getting ready
          2. How to do it
          3. How it works
          4. See also
        5. Defining an instruction set
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Adding a machine code descriptor
          1. How it's done…
          2. How it works…
        7. Implementing the MachineInstrBuilder class
          1. How to do it…
          2. How it works…
        8. Implementing the MachineBasicBlock class
          1. How to do it…
          2. How it works…
          3. See also
        9. Implementing the MachineFunction class
          1. How to do it…
          2. How it works…
          3. See also
        10. Writing an instruction selector
          1. How to do it…
          2. How it works…
        11. Legalizing SelectionDAG
          1. How to do it…
          2. How it works…
        12. Optimizing SelectionDAG
          1. How to do it…
          2. How it works…
          3. See also
        13. Selecting instruction from the DAG
          1. How to do it…
          2. How it works…
          3. See also
        14. Scheduling instructions in SelectionDAG
          1. How to do it…
          2. How it works…
          3. See also
      14. 7. Optimizing the Machine Code
        1. Introduction
        2. Eliminating common subexpression from machine code
          1. How to do it…
          2. How it works…
          3. See more
        3. Analyzing live intervals
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Allocating registers
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Inserting the prologue-epilogue code
          1. How to do it…
          2. How it works…
        6. Code emission
          1. How to do it…
        7. Tail call optimization
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Sibling call optimisation
          1. Getting ready
          2. How to do it…
          3. How it works…
      15. 8. Writing an LLVM Backend
        1. Introduction
          1. A sample backend
        2. Defining registers and registers sets
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Defining the calling convention
          1. How to do it…
          2. How it works…
          3. See also
        4. Defining the instruction set
          1. How to do it…
          2. How it works…
          3. See also
        5. Implementing frame lowering
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Printing an instruction
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Selecting an instruction
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Adding instruction encoding
          1. How to do it…
          2. How it works…
          3. See also
        9. Supporting a subtarget
          1. How to do it…
          2. See also
        10. Lowering to multiple instructions
          1. How to do it…
          2. How it works…
          3. See also
        11. Registering a target
          1. How to do it…
          2. How it works…
          3. See also
      16. 9. Using LLVM for Various Useful Projects
        1. Introduction
        2. Exception handling in LLVM
          1. Getting ready...
          2. How to do it…
          3. How it works…
          4. See also
        3. Using sanitizers
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also…
        4. Writing the garbage collector with LLVM
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Converting LLVM IR to JavaScript
          1. Getting ready
          2. How to do it…
          3. See more
        6. Using the Clang Static Analyzer
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        7. Using bugpoint
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Using LLDB
          1. Getting ready
          2. How to do it…
          3. See also
        9. Using LLVM utility passes
          1. Getting ready
          2. How to do it...
          3. See also
      17. Index

    Product information

    • Title: LLVM Cookbook
    • Author(s): Mayur Pandey, Suyog Sarda
    • Release date: May 2015
    • Publisher(s): Packt Publishing
    • ISBN: 9781785285981