LLVM 23.0.0git
OptimizationLevel.h
Go to the documentation of this file.
1//===-------- LLVM-provided High-Level Optimization levels -*- C++ -*------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This header enumerates the LLVM-provided high-level optimization levels.
11/// Each level has a specific goal and rationale.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_PASSES_OPTIMIZATIONLEVEL_H
16#define LLVM_PASSES_OPTIMIZATIONLEVEL_H
17
19#include <assert.h>
20
21namespace llvm {
22
23class OptimizationLevel final {
24 unsigned SpeedLevel = 2;
25 OptimizationLevel(unsigned SpeedLevel) : SpeedLevel(SpeedLevel) {
26 // Check that only valid values are passed.
27 assert(SpeedLevel <= 3 &&
28 "Optimization level for speed should be 0, 1, 2, or 3");
29 }
30
31public:
32 OptimizationLevel() = default;
33 /// Disable as many optimizations as possible. This doesn't completely
34 /// disable the optimizer in all cases, for example always_inline functions
35 /// can be required to be inlined for correctness.
36 LLVM_ABI static const OptimizationLevel O0;
37
38 /// Optimize quickly without destroying debuggability.
39 ///
40 /// This level is tuned to produce a result from the optimizer as quickly
41 /// as possible and to avoid destroying debuggability. This tends to result
42 /// in a very good development mode where the compiled code will be
43 /// immediately executed as part of testing. As a consequence, where
44 /// possible, we would like to produce efficient-to-execute code, but not
45 /// if it significantly slows down compilation or would prevent even basic
46 /// debugging of the resulting binary.
47 ///
48 /// As an example, complex loop transformations such as versioning,
49 /// vectorization, or fusion don't make sense here due to the degree to
50 /// which the executed code differs from the source code, and the compile
51 /// time cost.
52 LLVM_ABI static const OptimizationLevel O1;
53 /// Optimize for fast execution as much as possible without triggering
54 /// significant incremental compile time or code size growth.
55 ///
56 /// The key idea is that optimizations at this level should "pay for
57 /// themselves". So if an optimization increases compile time by 5% or
58 /// increases code size by 5% for a particular benchmark, that benchmark
59 /// should also be one which sees a 5% runtime improvement. If the compile
60 /// time or code size penalties happen on average across a diverse range of
61 /// LLVM users' benchmarks, then the improvements should as well.
62 ///
63 /// And no matter what, the compile time needs to not grow superlinearly
64 /// with the size of input to LLVM so that users can control the runtime of
65 /// the optimizer in this mode.
66 ///
67 /// This is expected to be a good default optimization level for the vast
68 /// majority of users.
69 LLVM_ABI static const OptimizationLevel O2;
70 /// Optimize for fast execution as much as possible.
71 ///
72 /// This mode is significantly more aggressive in trading off compile time
73 /// and code size to get execution time improvements. The core idea is that
74 /// this mode should include any optimization that helps execution time on
75 /// balance across a diverse collection of benchmarks, even if it increases
76 /// code size or compile time for some benchmarks without corresponding
77 /// improvements to execution time.
78 ///
79 /// Despite being willing to trade more compile time off to get improved
80 /// execution time, this mode still tries to avoid superlinear growth in
81 /// order to make even significantly slower compile times at least scale
82 /// reasonably. This does not preclude very substantial constant factor
83 /// costs though.
84 LLVM_ABI static const OptimizationLevel O3;
85
86 bool isOptimizingForSpeed() const { return SpeedLevel > 0; }
87
88 bool operator==(const OptimizationLevel &Other) const {
89 return SpeedLevel == Other.SpeedLevel;
90 }
91 bool operator!=(const OptimizationLevel &Other) const {
92 return SpeedLevel != Other.SpeedLevel;
93 }
94
95 unsigned getSpeedupLevel() const { return SpeedLevel; }
96};
97} // namespace llvm
98
99#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
static LLVM_ABI const OptimizationLevel O3
Optimize for fast execution as much as possible.
bool operator==(const OptimizationLevel &Other) const
static LLVM_ABI const OptimizationLevel O0
Disable as many optimizations as possible.
bool operator!=(const OptimizationLevel &Other) const
unsigned getSpeedupLevel() const
static LLVM_ABI const OptimizationLevel O2
Optimize for fast execution as much as possible without triggering significant incremental compile ti...
static LLVM_ABI const OptimizationLevel O1
Optimize quickly without destroying debuggability.
This is an optimization pass for GlobalISel generic memory operations.
@ Other
Any other memory.
Definition ModRef.h:68