LinuxCommandLibrary

opt

Optimize LLVM intermediate representation code

TLDR

Run an optimization or analysis on a bitcode file

$ opt -[passname] [path/to/file.bc] -S -o [file_opt.bc]
copy

Output the Control Flow Graph of a function to a .dot file
$ opt [-dot-cfg] -S [path/to/file.bc] -disable-output
copy

Optimize the program at level 2 and output the result to another file
$ opt -O2 [path/to/file.bc] -S -o [path/to/output_file.bc]
copy

SYNOPSIS

opt [options] [input_bitcode_file]

The opt command processes LLVM bitcode, applying various optimization passes. If no input file is specified, it reads from standard input. If no output file is specified with -o, it writes to standard output.

PARAMETERS

-help
    Show available options and optimization passes.

-version
    Display the LLVM version information.

-S
    Emit LLVM assembly (.ll) instead of bitcode (.bc).

-o filename
    Specify the output file. Defaults to standard output if omitted.

-mem2reg
    Promote alloca instructions (memory allocations) to registers.

-instcombine
    Perform peephole optimizations and combine redundant or inefficient instructions.

-gvn
    Eliminate redundant expressions using Global Value Numbering.

-simplifycfg
    Simplify the control flow graph of the program.

-O[0-3saz]
    Apply standard sets of optimizations (0: none, 1-3: increasing aggressiveness, s/z: size optimization).

DESCRIPTION

The opt command is a crucial tool within the LLVM (Low Level Virtual Machine) toolchain, primarily used for applying various optimization passes to LLVM bitcode files. It reads a bitcode module (typically generated by clang or llvm-as) and applies a sequence of transformations to improve its performance, reduce its size, or both. opt is highly modular, allowing developers to apply individual optimization passes or predefined groups of passes (like those enabled by -O1, -O2, etc.). This modularity is a core strength of LLVM, enabling extensive experimentation with different optimization strategies. It plays a vital role in the compilation process, transforming intermediate representations before final code generation by tools like llc. The output can be optimized bitcode, LLVM assembly, or a human-readable representation of the applied optimizations.

CAVEATS

The opt command is part of the LLVM project and requires LLVM to be installed on your system. It is not a standalone general-purpose Linux utility. The order in which optimization passes are applied can significantly affect the final optimized code, and selecting the optimal set of passes often requires deep understanding of compiler optimizations. It can be complex to use effectively without prior knowledge of LLVM and compiler internals.

OPTIMIZATION LEVELS

The -O[0-3saz] options provide convenient shortcuts to apply predefined sets of optimization passes.

-O0: Disable most optimizations, useful for debugging.
-O1: Enable a basic set of optimizations.
-O2: Enable more aggressive optimizations than -O1, aiming for good overall performance. This is often a good default for release builds.
-O3: Enable the most aggressive optimizations, which may increase compile time and sometimes code size, but aims for maximum performance.
-Os: Optimize for code size, similar to -O2 but prioritizing size reduction.
-Oz: Even more aggressively optimize for code size than -Os.

OPTIMIZATION PASSES

opt applies individual transformations known as 'passes.' Each pass performs a specific optimization, such as dead code elimination, loop unrolling, or constant propagation. The power of opt lies in its ability to combine these passes in various orders and sequences. Users can explicitly list individual passes on the command line (e.g., -mem2reg -instcombine) or use optimization level flags (e.g., -O2) which implicitly run a curated list of passes.

HISTORY

The opt command has been a core component of the LLVM project since its inception. LLVM was originally developed by Chris Lattner at the University of Illinois at Urbana-Champaign in 2003. A fundamental design goal of LLVM was modularity and extensibility, which opt embodies by allowing individual optimization passes to be applied independently. This design has enabled rapid development and experimentation with new optimization techniques, making LLVM a popular choice for research and industrial applications. Over the years, opt has evolved with the LLVM project, gaining more passes and sophisticated optimization strategies.

SEE ALSO

llvm-link(1), llc(1), clang(1), llvm-as(1), llvm-dis(1)

Copied to clipboard