LLVM Cookbook

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

LLVM Cookbook

Mayur Pandey, Suyog Sarda

1 customer reviews
Over 80 engaging recipes that will help you build a compiler frontend, optimizer, and code generator using LLVM
Mapt Subscription
FREE
$29.99/m after trial
eBook
$25.20
RRP $35.99
Save 29%
Print + eBook
$44.99
RRP $44.99
What do I get with a Mapt Pro subscription?
  • Unlimited access to all Packt’s 5,000+ eBooks and Videos
  • Early Access content, Progress Tracking, and Assessments
  • 1 Free eBook or Video to download and keep every month after trial
What do I get with an eBook?
  • Download this book in EPUB, PDF, MOBI formats
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the Mapt reader
What do I get with Print & eBook?
  • Get a paperback copy of the book delivered to you
  • Download this book in EPUB, PDF, MOBI formats
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the Mapt reader
What do I get with a Video?
  • Download this Video course in MP4 format
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the Mapt reader
$0.00
$25.20
$44.99
$29.99 p/m after trial
RRP $35.99
RRP $44.99
Subscription
eBook
Print + eBook
Start 14 Day Trial

Frequently bought together


LLVM Cookbook Book Cover
LLVM Cookbook
$ 35.99
$ 25.20
Learning Vulkan Book Cover
Learning Vulkan
$ 43.99
$ 30.80
Buy 2 for $35.00
Save $44.98
Add to Cart

Book Details

ISBN 139781785285981
Paperback296 pages

Book Description

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

Chapter 1: LLVM Design and Use
Introduction
Understanding modular design
Cross-compiling Clang/LLVM
Converting a C source code to LLVM assembly
Converting IR to LLVM bitcode
Converting LLVM bitcode to target machine assembly
Converting LLVM bitcode back to LLVM assembly
Transforming LLVM IR
Linking LLVM bitcode
Executing LLVM bitcode
Using the C frontend Clang
Using the GO frontend
Using DragonEgg
Chapter 2: Steps in Writing a Frontend
Introduction
Defining a TOY language
Implementing a lexer
Defining Abstract Syntax Tree
Implementing a parser
Parsing simple expressions
Parsing binary expressions
Invoking a driver for parsing
Running lexer and parser on our TOY language
Defining IR code generation methods for each AST class
Generating IR code for expressions
Generating IR code for functions
Adding IR optimization support
Chapter 3: Extending the Frontend and Adding JIT Support
Introduction
Handling decision making paradigms – if/then/else constructs
Generating code for loops
Handling user-defined operators – binary operators
Handling user-defined operators – unary operators
Adding JIT support
Chapter 4: Preparing Optimizations
Introduction
Various levels of optimization
Writing your own LLVM pass
Running your own pass with the opt tool
Using another pass in a new pass
Registering a pass with pass manager
Writing an analysis pass
Writing an alias analysis pass
Using other analysis passes
Chapter 5: Implementing Optimizations
Introduction
Writing a dead code elimination pass
Writing an inlining transformation pass
Writing a pass for memory optimization
Combining LLVM IR
Transforming and optimizing loops
Reassociating expressions
Vectorizing IR
Other optimization passes
Chapter 6: Target-independent Code Generator
Introduction
The life of an LLVM IR instruction
Visualizing LLVM IR CFG using GraphViz
Describing targets using TableGen
Defining an instruction set
Adding a machine code descriptor
Implementing the MachineInstrBuilder class
Implementing the MachineBasicBlock class
Implementing the MachineFunction class
Writing an instruction selector
Legalizing SelectionDAG
Optimizing SelectionDAG
Selecting instruction from the DAG
Scheduling instructions in SelectionDAG
Chapter 7: Optimizing the Machine Code
Introduction
Eliminating common subexpression from machine code
Analyzing live intervals
Allocating registers
Inserting the prologue-epilogue code
Code emission
Tail call optimization
Sibling call optimisation
Chapter 8: Writing an LLVM Backend
Introduction
Defining registers and registers sets
Defining the calling convention
Defining the instruction set
Implementing frame lowering
Printing an instruction
Selecting an instruction
Adding instruction encoding
Supporting a subtarget
Lowering to multiple instructions
Registering a target
Chapter 9: Using LLVM for Various Useful Projects
Introduction
Exception handling in LLVM
Using sanitizers
Writing the garbage collector with LLVM
Converting LLVM IR to JavaScript
Using the Clang Static Analyzer
Using bugpoint
Using LLDB
Using LLVM utility passes

What You Will Learn

  • Introduction to LLVM modular design and LLVM tools
  • Write a frontend for a language
  • Add JIT support and use frontends for different languages
  • Learn about the LLVM Pass infrastructure and the LLVM Pass Manager
  • Create analyses and transform optimization passes
  • Build a LLVM TOY backend from scratch
  • Optimize the code at SelectionDAG level and allocate registers to variables

Authors

Table of Contents

Chapter 1: LLVM Design and Use
Introduction
Understanding modular design
Cross-compiling Clang/LLVM
Converting a C source code to LLVM assembly
Converting IR to LLVM bitcode
Converting LLVM bitcode to target machine assembly
Converting LLVM bitcode back to LLVM assembly
Transforming LLVM IR
Linking LLVM bitcode
Executing LLVM bitcode
Using the C frontend Clang
Using the GO frontend
Using DragonEgg
Chapter 2: Steps in Writing a Frontend
Introduction
Defining a TOY language
Implementing a lexer
Defining Abstract Syntax Tree
Implementing a parser
Parsing simple expressions
Parsing binary expressions
Invoking a driver for parsing
Running lexer and parser on our TOY language
Defining IR code generation methods for each AST class
Generating IR code for expressions
Generating IR code for functions
Adding IR optimization support
Chapter 3: Extending the Frontend and Adding JIT Support
Introduction
Handling decision making paradigms – if/then/else constructs
Generating code for loops
Handling user-defined operators – binary operators
Handling user-defined operators – unary operators
Adding JIT support
Chapter 4: Preparing Optimizations
Introduction
Various levels of optimization
Writing your own LLVM pass
Running your own pass with the opt tool
Using another pass in a new pass
Registering a pass with pass manager
Writing an analysis pass
Writing an alias analysis pass
Using other analysis passes
Chapter 5: Implementing Optimizations
Introduction
Writing a dead code elimination pass
Writing an inlining transformation pass
Writing a pass for memory optimization
Combining LLVM IR
Transforming and optimizing loops
Reassociating expressions
Vectorizing IR
Other optimization passes
Chapter 6: Target-independent Code Generator
Introduction
The life of an LLVM IR instruction
Visualizing LLVM IR CFG using GraphViz
Describing targets using TableGen
Defining an instruction set
Adding a machine code descriptor
Implementing the MachineInstrBuilder class
Implementing the MachineBasicBlock class
Implementing the MachineFunction class
Writing an instruction selector
Legalizing SelectionDAG
Optimizing SelectionDAG
Selecting instruction from the DAG
Scheduling instructions in SelectionDAG
Chapter 7: Optimizing the Machine Code
Introduction
Eliminating common subexpression from machine code
Analyzing live intervals
Allocating registers
Inserting the prologue-epilogue code
Code emission
Tail call optimization
Sibling call optimisation
Chapter 8: Writing an LLVM Backend
Introduction
Defining registers and registers sets
Defining the calling convention
Defining the instruction set
Implementing frame lowering
Printing an instruction
Selecting an instruction
Adding instruction encoding
Supporting a subtarget
Lowering to multiple instructions
Registering a target
Chapter 9: Using LLVM for Various Useful Projects
Introduction
Exception handling in LLVM
Using sanitizers
Writing the garbage collector with LLVM
Converting LLVM IR to JavaScript
Using the Clang Static Analyzer
Using bugpoint
Using LLDB
Using LLVM utility passes

Book Details

ISBN 139781785285981
Paperback296 pages
Read More
From 1 reviews

Read More Reviews

Recommended for You

Learning Vulkan Book Cover
Learning Vulkan
$ 43.99
$ 30.80
Programming Kotlin Book Cover
Programming Kotlin
$ 35.99
$ 25.20
Mastering Qt 5 Book Cover
Mastering Qt 5
$ 35.99
$ 25.20
Boost C++ Application Development Cookbook Book Cover
Boost C++ Application Development Cookbook
$ 29.99
$ 21.00
Learning CoreOS Book Cover
Learning CoreOS
$ 31.99
$ 22.40
Lua Game Development Cookbook Book Cover
Lua Game Development Cookbook
$ 39.99
$ 28.00