LLVM's Analysis and Transform Passes

1. Introduction
2. Analysis Passes
3. Transform Passes
4. Utility Passes

Written by Reid Spencer and Gordon Henriksen

Introduction

This document serves as a high level summary of the optimization features that LLVM provides. Optimizations are implemented as Passes that traverse some portion of a program to either collect information or transform the program. The table below divides the passes that LLVM provides into three categories. Analysis passes compute information that other passes can use or for debugging or program visualization purposes. Transform passes can use (or invalidate) the analysis passes. Transform passes all mutate the program in some way. Utility passes provides some utility but don't otherwise fit categorization. For example passes to extract functions to bitcode or write a module to bitcode are neither analysis nor transform passes.

The table below provides a quick summary of each pass and links to the more complete pass description later in the document.

ANALYSIS PASSES
Option | Name
--- | ---
-aa-eval | Exhaustive Alias Analysis Precision Evaluator
-anders-aa | Andersen's Interprocedural Alias Analysis
-basicaa | Basic Alias Analysis (default AA impl)
-basiccg | Basic CallGraph Construction
-basicvn | Basic Value Numbering (default GVN impl)
-callgraph | Print a call graph
-callscc | Print SCCs of the Call Graph
-cfgscc | Print SCCs of each function CFG
-codegenprepare | Optimize for code generation
-count-aa | Count Alias Analysis Query Responses
-debug-aa | AA use debugger
-domfrontier | Dominance Frontier Construction
-domtree | Dominator Tree Construction
-externalfnconstants | Print external fn callsites passed constants
-globalsmodref-aa | Simple mod/ref analysis for globals
-instcount | Counts the various types of Instructions
-intervals | Interval Partition Construction
-load-vn | Load Value Numbering
-loops | Natural Loop Construction
-memdep | Memory Dependence Analysis
-no-aa | No Alias Analysis (always returns 'may' alias)
-no-profile | No Profile Information
-postdomfrontier | Post-Dominance Frontier Construction
-postdomtree | Post-Dominator Tree Construction
-print | Print function to stderr
-print-alias-sets | Alias Set Printer
-print-callgraph | Print Call Graph to 'dot' file
-print-cfg | Print CFG of function to 'dot' file
-print-cfg-only | Print CFG of function to 'dot' file (with no function bodies)
-printm | Print module to stderr
-printusedtypes | Find Used Types
-profile-loader | Load profile information from llvmprof.out
-scalar-evolution | Scalar Evolution Analysis
-targetdata | Target Data Layout
TRANSFORM PASSES
Option | Name
-adce | Aggressive Dead Code Elimination
-argpromotion | Promote 'by reference' arguments to scalars
-block-placement | Profile Guided Basic Block Placement
-break-crit-edges | Break critical edges in CFG
-cee | Correlated Expression Elimination
-condprop | Conditional Propagation
-constmerge | Merge Duplicate Global Constants
-constprop | Simple constant propagation
-dce | Dead Code Elimination
-deadargelim | Dead Argument Elimination
-deadtypeelim | Dead Type Elimination
-die | Dead Instruction Elimination
-dse | Dead Store Elimination
-gcse | Global Common Subexpression Elimination
-globaldce | Dead Global Elimination
-globalopt | Global Variable Optimizer
-gvn | Global Value Numbering
-gvnpre | Global Value Numbering/Partial Redundancy Elimination
-indmemrem | Indirect Malloc and Free Removal
-indvars | Canonicalize Induction Variables
-inline | Function Integration/Inlining
-insert-block-profiling | Insert instrumentation for block profiling
-insert-edge-profiling | Insert instrumentation for edge profiling
-insert-function-profiling | Insert instrumentation for function profiling
-insert-null-profiling-rs | Measure profiling framework overhead
-insert-rs-profiling-framework | Insert random sampling instrumentation framework
-instcombine | Combine redundant instructions
-internalize | Internalize Global Symbols
-ipconstprop | Interprocedural constant propagation
-ipsccp | Interprocedural Sparse Conditional Constant Propagation
-lcssa | Loop-Closed SSA Form Pass
-licm | Loop Invariant Code Motion
-loop-extract | Extract loops into new functions
-loop-extract-single | Extract at most one loop into a new function
-loop-index-split | Index Split Loops
-loop-reduce | Loop Strength Reduction
-loop-rotate | Rotate Loops
-loop-unroll | Unroll loops
-loop-unswitch | Unswitch loops
-loopsimplify | Canonicalize natural loops
-lower-packed | lowers packed operations to operations on smaller packed datatypes
-lowerallocs | Lower allocations from instructions to calls
-lowergc | Lower GC intrinsics, for GCless code generators
-lowerinvoke | Lower invoke and unwind, for unwindless code generators
-lowerselect | Lower select instructions to branches
-lowersetjmp | Lower Set Jump
-lowerswitch | Lower SwitchInst's to branches
-mem2reg | Promote Memory to Register
-mergereturn | Unify function exit nodes
-predsimplify | Predicate Simplifier
-prune-eh | Remove unused exception handling info
-raiseallocs | Raise allocations from calls to instructions
-reassociate | Reassociate expressions
-reg2mem | Demote all values to stack slots
-scalarrepl | Scalar Replacement of Aggregates
-sccp | Sparse Conditional Constant Propagation
-simplify-libcalls | Simplify well-known library calls
-simplifycfg | Simplify the CFG
-strip | Strip all symbols from a module
-tailcallelim | Tail Call Elimination
-tailduplicate | Tail Duplication
UTILITY PASSES
Option | Name
-deadarghaX0r | Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)
-extract-blocks | Extract Basic Blocks From Module (for bugpoint use)
-emitbitcode | Bitcode Writer
-verify | Module Verifier
-view-cfg | View CFG of function
-view-cfg-only | View CFG of function (with no function bodies)

Analysis Passes

This section describes the LLVM Analysis Passes.

Exhaustive Alias Analysis Precision Evaluator

This is a simple N^2 alias analysis accuracy evaluator. Basically, for each function in the program, it simply queries to see how the alias analysis implementation answers alias queries between each pair of pointers in the function.

This is inspired and adapted from code by: Naveen Neelakantam, Francesco Spadini, and Wojciech Stryjewski.

Andersen's Interprocedural Alias Analysis

This is an implementation of Andersen's interprocedural alias analysis

In pointer analysis terms, this is a subset-based, flow-insensitive, field-sensitive, and context-insensitive algorithm pointer algorithm.

This algorithm is implemented as three stages:

1. Object identification.
2. Inclusion constraint identification.
3. Offline constraint graph optimization.
4. Inclusion constraint solving.

The object identification stage identifies all of the memory objects in the program, which includes globals, heap allocated objects, and stack allocated objects.

The inclusion constraint identification stage finds all inclusion constraints in the program by scanning the program, looking for pointer assignments and other statements that effect the points-to graph. For a statement like `A = B`, this statement is processed to indicate that A can point to anything that B can point to. Constraints can handle copies, loads, and stores, and address taking.

The offline constraint graph optimization portion includes offline variable substitution algorithms intended to computer pointer and location equivalences. Pointer equivalences are those pointers that will have the same points-to sets, and location equivalences are those variables that always appear together in points-to sets.

The inclusion constraint solving phase iteratively propagates the inclusion constraints until a fixed point is reached. This is an O(n³) algorithm.

Function constraints are handled as if they were structs with X fields. Thus, an access to argument X of function Y is an access to node index `getNode(Y) + X`. This representation allows handling of indirect calls without any issues. To wit, an indirect call `Y(a,b)` is equivalent to `*(Y + 1) = a, *(Y + 2) = b`. The return node for a function F is always located at `getNode(F) + CallReturnPos`. The arguments start at `getNode(F) + CallArgPos`.

Basic Alias Analysis (default AA impl)

This is the default implementation of the Alias Analysis interface that simply implements a few identities (two different globals cannot alias, etc), but otherwise does no analysis.

Basic CallGraph Construction

Yet to be written.

Basic Value Numbering (default GVN impl)

This is the default implementation of the `ValueNumbering` interface. It walks the SSA def-use chains to trivially identify lexically identical expressions. This does not require any ahead of time analysis, so it is a very fast default implementation.

Print a call graph

This pass, only available in `opt`, prints the call graph to standard output in a human-readable form.

Print SCCs of the Call Graph

This pass, only available in `opt`, prints the SCCs of the call graph to standard output in a human-readable form.

Print SCCs of each function CFG

This pass, only available in `opt`, prints the SCCs of each function CFG to standard output in a human-readable form.

Optimize for code generation

This pass munges the code in the input function to better prepare it for SelectionDAG-based code generation. This works around limitations in it's basic-block-at-a-time approach. It should eventually be removed.

Count Alias Analysis Query Responses

A pass which can be used to count how many alias queries are being made and how the alias analysis implementation being used responds.

AA use debugger

This simple pass checks alias analysis users to ensure that if they create a new value, they do not query AA without informing it of the value. It acts as a shim over any other AA pass you want.

Yes keeping track of every value in the program is expensive, but this is a debugging pass.

Dominance Frontier Construction

This pass is a simple dominator construction algorithm for finding forward dominator frontiers.

Dominator Tree Construction

This pass is a simple dominator construction algorithm for finding forward dominators.

Print external fn callsites passed constants

This pass, only available in `opt`, prints out call sites to external functions that are called with constant arguments. This can be useful when looking for standard library functions we should constant fold or handle in alias analyses.

Simple mod/ref analysis for globals

This simple pass provides alias and mod/ref information for global values that do not have their address taken, and keeps track of whether functions read or write memory (are "pure"). For this simple (but very common) case, we can provide pretty accurate and useful information.

Counts the various types of Instructions

This pass collects the count of all instructions and reports them

Interval Partition Construction

This analysis calculates and represents the interval partition of a function, or a preexisting interval partition.

In this way, the interval partition may be used to reduce a flow graph down to its degenerate single node interval partition (unless it is irreducible).

Load Value Numbering

This pass value numbers load and call instructions. To do this, it finds lexically identical load instructions, and uses alias analysis to determine which loads are guaranteed to produce the same value. To value number call instructions, it looks for calls to functions that do not write to memory which do not have intervening instructions that clobber the memory that is read from.

This pass builds off of another value numbering pass to implement value numbering for non-load and non-call instructions. It uses Alias Analysis so that it can disambiguate the load instructions. The more powerful these base analyses are, the more powerful the resultant value numbering will be.

Natural Loop Construction

This analysis is used to identify natural loops and determine the loop depth of various nodes of the CFG. Note that the loops identified may actually be several natural loops that share the same header node... not just a single natural loop.

Memory Dependence Analysis

An analysis that determines, for a given memory operation, what preceding memory operations it depends on. It builds on alias analysis information, and tries to provide a lazy, caching interface to a common kind of alias information query.

No Alias Analysis (always returns 'may' alias)

Always returns "I don't know" for alias queries. NoAA is unlike other alias analysis implementations, in that it does not chain to a previous analysis. As such it doesn't follow many of the rules that other alias analyses must.

No Profile Information

The default "no profile" implementation of the abstract `ProfileInfo` interface.

Post-Dominance Frontier Construction

This pass is a simple post-dominator construction algorithm for finding post-dominator frontiers.

Post-Dominator Tree Construction

This pass is a simple post-dominator construction algorithm for finding post-dominators.

Print function to stderr

The `PrintFunctionPass` class is designed to be pipelined with other `FunctionPass`es, and prints out the functions of the module as they are processed.

Alias Set Printer

Yet to be written.

Print Call Graph to 'dot' file

This pass, only available in `opt`, prints the call graph into a `.dot` graph. This graph can then be processed with the "dot" tool to convert it to postscript or some other suitable format.

Print CFG of function to 'dot' file

This pass, only available in `opt`, prints the control flow graph into a `.dot` graph. This graph can then be processed with the "dot" tool to convert it to postscript or some other suitable format.

Print CFG of function to 'dot' file (with no function bodies)

This pass, only available in `opt`, prints the control flow graph into a `.dot` graph, omitting the function bodies. This graph can then be processed with the "dot" tool to convert it to postscript or some other suitable format.

Print module to stderr

This pass simply prints out the entire module when it is executed.

Find Used Types

This pass is used to seek out all of the types in use by the program. Note that this analysis explicitly does not include types only used by the symbol table.

Load profile information from llvmprof.out

A concrete implementation of profiling information that loads the information from a profile dump file.

Scalar Evolution Analysis

The `ScalarEvolution` analysis can be used to analyze and catagorize scalar expressions in loops. It specializes in recognizing general induction variables, representing them with the abstract and opaque `SCEV` class. Given this analysis, trip counts of loops and other important properties can be obtained.

This analysis is primarily useful for induction variable substitution and strength reduction.

Target Data Layout

Provides other passes access to information on how the size and alignment required by the the target ABI for various data types.

Transform Passes

This section describes the LLVM Transform Passes.

Aggressive Dead Code Elimination

ADCE aggressively tries to eliminate code. This pass is similar to DCE but it assumes that values are dead until proven otherwise. This is similar to SCCP, except applied to the liveness of values.

Promote 'by reference' arguments to scalars

This pass promotes "by reference" arguments to be "by value" arguments. In practice, this means looking for internal functions that have pointer arguments. If it can prove, through the use of alias analysis, that an argument is *only* loaded, then it can pass the value into the function instead of the address of the value. This can cause recursive simplification of code and lead to the elimination of allocas (especially in C++ template code like the STL).

This pass also handles aggregate arguments that are passed into a function, scalarizing them if the elements of the aggregate are only loaded. Note that it refuses to scalarize aggregates which would require passing in more than three operands to the function, because passing thousands of operands for a large array or structure is unprofitable!

Note that this transformation could also be done for arguments that are only stored to (returning the value instead), but does not currently. This case would be best handled when and if LLVM starts supporting multiple return values from functions.

Profile Guided Basic Block Placement

This pass is a very simple profile guided basic block placement algorithm. The idea is to put frequently executed blocks together at the start of the function and hopefully increase the number of fall-through conditional branches. If there is no profile information for a particular function, this pass basically orders blocks in depth-first order.

Break critical edges in CFG

Break all of the critical edges in the CFG by inserting a dummy basic block. It may be "required" by passes that cannot deal with critical edges. This transformation obviously invalidates the CFG, but can update forward dominator (set, immediate dominators, tree, and frontier) information.

Correlated Expression Elimination

Correlated Expression Elimination propagates information from conditional branches to blocks dominated by destinations of the branch. It propagates information from the condition check itself into the body of the branch, allowing transformations like these for example:

>     if (i == 7)
>       ... 4*i;  // constant propagation

>     M = i+1; N = j+1;
>     if (i == j)
>       X = M-N;  // = M-M == 0;

This is called Correlated Expression Elimination because we eliminate or simplify expressions that are correlated with the direction of a branch. In this way we use static information to give us some information about the dynamic value of a variable.

Conditional Propagation

This pass propagates information about conditional expressions through the program, allowing it to eliminate conditional branches in some cases.

Merge Duplicate Global Constants

Merges duplicate global constants together into a single constant that is shared. This is useful because some passes (ie TraceValues) insert a lot of string constants into the program, regardless of whether or not an existing string is available.

Simple constant propagation

This file implements constant propagation and merging. It looks for instructions involving only constant operands and replaces them with a constant value instead of an instruction. For example:

>     add i32 1, 2

becomes

>     i32 3

NOTE: this pass has a habit of making definitions be dead. It is a good idea to to run a DIE (Dead Instruction Elimination) pass sometime after running this pass.

Dead Code Elimination

Dead code elimination is similar to dead instruction elimination, but it rechecks instructions that were used by removed instructions to see if they are newly dead.

Dead Argument Elimination

This pass deletes dead arguments from internal functions. Dead argument elimination removes arguments which are directly dead, as well as arguments only passed into function calls as dead arguments of other functions. This pass also deletes dead arguments in a similar way.

This pass is often useful as a cleanup pass to run after aggressive interprocedural passes, which add possibly-dead arguments.

Dead Type Elimination

This pass is used to cleanup the output of GCC. It eliminate names for types that are unused in the entire translation unit, using the find used types pass.

Dead Instruction Elimination

Dead instruction elimination performs a single pass over the function, removing instructions that are obviously dead.

Dead Store Elimination

A trivial dead store elimination that only considers basic-block local redundant stores.

Global Common Subexpression Elimination

This pass is designed to be a very quick global transformation that eliminates global common subexpressions from a function. It does this by using an existing value numbering implementation to identify the common subexpressions, eliminating them when possible.

Dead Global Elimination

This transform is designed to eliminate unreachable internal globals from the program. It uses an aggressive algorithm, searching out globals that are known to be alive. After it finds all of the globals which are needed, it deletes whatever is left over. This allows it to delete recursive chunks of the program which are unreachable.

Global Variable Optimizer

This pass transforms simple global variables that never have their address taken. If obviously true, it marks read/write globals as constant, deletes variables only stored to, etc.

Global Value Numbering

This pass performs global value numbering to eliminate fully redundant instructions. It also performs simple dead load elimination.

Global Value Numbering/Partial Redundancy Elimination

This pass performs a hybrid of global value numbering and partial redundancy elimination, known as GVN-PRE. It performs partial redundancy elimination on values, rather than lexical expressions, allowing a more comprehensive view the optimization. It replaces redundant values with uses of earlier occurences of the same value. While this is beneficial in that it eliminates unneeded computation, it also increases register pressure by creating large live ranges, and should be used with caution on platforms that are very sensitive to register pressure.

Indirect Malloc and Free Removal

This pass finds places where memory allocation functions may escape into indirect land. Some transforms are much easier (aka possible) only if free or malloc are not called indirectly.

Thus find places where the address of memory functions are taken and construct bounce functions with direct calls of those functions.

Canonicalize Induction Variables

This transformation analyzes and transforms the induction variables (and computations derived from them) into simpler forms suitable for subsequent analysis and transformation.

This transformation makes the following changes to each loop with an identifiable induction variable:

1. All loops are transformed to have a single canonical induction variable which starts at zero and steps by one.
2. The canonical induction variable is guaranteed to be the first PHI node in the loop header block.
3. Any pointer arithmetic recurrences are raised to use array subscripts.

If the trip count of a loop is computable, this pass also makes the following changes:

1. The exit condition for the loop is canonicalized to compare the induction value against the exit value. This turns loops like:

   >     for (i = 7; i*i < 1000; ++i)

   into

   >     for (i = 0; i != 25; ++i)

2. Any use outside of the loop of an expression derived from the indvar is changed to compute the derived value outside of the loop, eliminating the dependence on the exit value of the induction variable. If the only purpose of the loop is to compute the exit value of some derived expression, this transformation will make the loop dead.

This transformation should be followed by strength reduction after all of the desired loop transformations have been performed. Additionally, on targets where it is profitable, the loop could be transformed to count down to zero (the "do loop" optimization).

Function Integration/Inlining

Bottom-up inlining of functions into callees.

Insert instrumentation for block profiling

This pass instruments the specified program with counters for basic block profiling, which counts the number of times each basic block executes. This is the most basic form of profiling, which can tell which blocks are hot, but cannot reliably detect hot paths through the CFG.

Note that this implementation is very naïve. Control equivalent regions of the CFG should not require duplicate counters, but it does put duplicate counters in.

Insert instrumentation for edge profiling

This pass instruments the specified program with counters for edge profiling. Edge profiling can give a reasonable approximation of the hot paths through a program, and is used for a wide variety of program transformations.

Note that this implementation is very naïve. It inserts a counter for every edge in the program, instead of using control flow information to prune the number of counters inserted.

Insert instrumentation for function profiling

This pass instruments the specified program with counters for function profiling, which counts the number of times each function is called.

Measure profiling framework overhead

The basic profiler that does nothing. It is the default profiler and thus terminates `RSProfiler` chains. It is useful for measuring framework overhead.

Insert random sampling instrumentation framework

The second stage of the random-sampling instrumentation framework, duplicates all instructions in a function, ignoring the profiling code, then connects the two versions together at the entry and at backedges. At each connection point a choice is made as to whether to jump to the profiled code (take a sample) or execute the unprofiled code.

After this pass, it is highly recommended to runmem2reg and adce. instcombine, load-vn, gdce, and dse also are good to run afterwards.

Combine redundant instructions

Combine instructions to form fewer, simple instructions. This pass does not modify the CFG This pass is where algebraic simplification happens.

This pass combines things like:

>     %Y = add i32 %X, 1
>     %Z = add i32 %Y, 1

into:

>     %Z = add i32 %X, 2

This is a simple worklist driven algorithm.

This pass guarantees that the following canonicalizations are performed on the program:

- If a binary operator has a constant operand, it is moved to the right- hand side.
- Bitwise operators with constant operands are always grouped so that shifts are performed first, then `or`s, then `and`s, then `xor`s.
- Compare instructions are converted from `<`, `>`, `≤`, or `≥` to `=` or `≠` if possible.
- All `cmp` instructions on boolean values are replaced with logical operations.
- `add X, X` is represented as `mul X, 2` ⇒ `shl X, 1`
- Multiplies with a constant power-of-two argument are transformed into shifts.
- … etc.

Internalize Global Symbols

Yet to be written.

Interprocedural constant propagation

Yet to be written.

Interprocedural Sparse Conditional Constant Propagation

Yet to be written.

Loop-Closed SSA Form Pass

Yet to be written.

Loop Invariant Code Motion

Yet to be written.

Extract loops into new functions

Yet to be written.

Extract at most one loop into a new function

Yet to be written.

Index Split Loops

Yet to be written.

Loop Strength Reduction

Yet to be written.

Rotate Loops

Yet to be written.

Unroll loops

Yet to be written.

Unswitch loops

Yet to be written.

Canonicalize natural loops

Yet to be written.

lowers packed operations to operations on smaller packed datatypes

Yet to be written.

Lower allocations from instructions to calls

Yet to be written.

Lower GC intrinsics, for GCless code generators

Yet to be written.

Lower invoke and unwind, for unwindless code generators

Yet to be written.

Lower select instructions to branches

Yet to be written.

Lower Set Jump

Yet to be written.

Lower SwitchInst's to branches

Yet to be written.

Promote Memory to Register

Yet to be written.

Unify function exit nodes

Yet to be written.

Predicate Simplifier

Yet to be written.

Remove unused exception handling info

Yet to be written.

Raise allocations from calls to instructions

Yet to be written.

Reassociate expressions

Yet to be written.

Demote all values to stack slots

Yet to be written.

Scalar Replacement of Aggregates

Yet to be written.

Sparse Conditional Constant Propagation

Yet to be written.

Simplify well-known library calls

Yet to be written.

Simplify the CFG

Yet to be written.

Strip all symbols from a module

Yet to be written.

Tail Call Elimination

Yet to be written.

Tail Duplication

Yet to be written.

Utility Passes

This section describes the LLVM Utility Passes.

Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)

Yet to be written.

Extract Basic Blocks From Module (for bugpoint use)

Yet to be written.

Bitcode Writer

Yet to be written.

Module Verifier

Yet to be written.

View CFG of function

Yet to be written.

View CFG of function (with no function bodies)

Yet to be written.