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 file]

PARAMETERS

-O[0-3]
    Optimization level. -O0 disables most optimizations. -O1 enables basic optimizations. -O2 enables more aggressive optimizations. -O3 enables the most aggressive optimizations.

-S
    Write output in LLVM assembly language instead of bitcode.

-disable-basic-aa
    Disable basic alias analysis.

-instcombine-mergereturn
    Enable merging of return instructions.

-mem2reg
    Promote memory to registers.

-passes
    A comma separated list of passes to run.

-help
    Display available options.

-version
    Display the version of the opt tool.

DESCRIPTION

The opt command is the LLVM optimizer. It takes LLVM bitcode files as input and transforms them according to a sequence of optimization passes. The transformed bitcode is then outputted. opt is a powerful tool for improving the performance and size of LLVM-based programs. It applies a series of analyses and transformations to the input bitcode, attempting to simplify the code, remove redundancies, and improve its overall efficiency. opt supports a wide range of optimization passes, including dead code elimination, constant propagation, instruction combining, and loop optimizations. These passes can be enabled or disabled individually or in combination using command-line options.
This allows fine-grained control over the optimization process. Its capabilities extend to transforming code for various architectures. It is primarily used by compiler developers and those needing highly optimized code.

CAVEATS

opt requires well-formed LLVM bitcode as input. Incorrect bitcode may lead to unexpected results or crashes. Understanding the various optimization passes and their interactions is crucial for effective use.

PASS MANAGER

The opt command relies on LLVM's Pass Manager to orchestrate the execution of optimization passes. The Pass Manager handles pass dependencies, scheduling, and analysis invalidation.

ALIAS ANALYSIS

Alias analysis determines which memory locations may be accessed by the same pointer. This information is critical for many optimizations. opt supports multiple alias analysis algorithms, each with its own trade-offs between precision and performance.

HISTORY

opt is a core component of the LLVM compiler infrastructure. It has been under continuous development since the early 2000s and has become an industry standard for LLVM-based code optimization. Its development is intricately linked to the evolution of LLVM, reflecting advancements in compiler technology and optimization techniques. Its design emphasizes modularity allowing integration of new optimization algorithms.
The usage of opt has expanded beyond traditional compiler toolchains, finding applications in areas such as code analysis, security research, and high-performance computing.

SEE ALSO

llvm-as(1), llvm-dis(1), llc(1), lli(1)

Copied to clipboard