Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:22

0001 //===-------- LLVM-provided High-Level Optimization levels -*- C++ -*------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// This header enumerates the LLVM-provided high-level optimization levels.
0011 /// Each level has a specific goal and rationale.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_PASSES_OPTIMIZATIONLEVEL_H
0016 #define LLVM_PASSES_OPTIMIZATIONLEVEL_H
0017 
0018 #include <assert.h>
0019 
0020 namespace llvm {
0021 
0022 class OptimizationLevel final {
0023   unsigned SpeedLevel = 2;
0024   unsigned SizeLevel = 0;
0025   OptimizationLevel(unsigned SpeedLevel, unsigned SizeLevel)
0026       : SpeedLevel(SpeedLevel), SizeLevel(SizeLevel) {
0027     // Check that only valid combinations are passed.
0028     assert(SpeedLevel <= 3 &&
0029            "Optimization level for speed should be 0, 1, 2, or 3");
0030     assert(SizeLevel <= 2 &&
0031            "Optimization level for size should be 0, 1, or 2");
0032     assert((SizeLevel == 0 || SpeedLevel == 2) &&
0033            "Optimize for size should be encoded with speedup level == 2");
0034   }
0035 
0036 public:
0037   OptimizationLevel() = default;
0038   /// Disable as many optimizations as possible. This doesn't completely
0039   /// disable the optimizer in all cases, for example always_inline functions
0040   /// can be required to be inlined for correctness.
0041   static const OptimizationLevel O0;
0042 
0043   /// Optimize quickly without destroying debuggability.
0044   ///
0045   /// This level is tuned to produce a result from the optimizer as quickly
0046   /// as possible and to avoid destroying debuggability. This tends to result
0047   /// in a very good development mode where the compiled code will be
0048   /// immediately executed as part of testing. As a consequence, where
0049   /// possible, we would like to produce efficient-to-execute code, but not
0050   /// if it significantly slows down compilation or would prevent even basic
0051   /// debugging of the resulting binary.
0052   ///
0053   /// As an example, complex loop transformations such as versioning,
0054   /// vectorization, or fusion don't make sense here due to the degree to
0055   /// which the executed code differs from the source code, and the compile
0056   /// time cost.
0057   static const OptimizationLevel O1;
0058   /// Optimize for fast execution as much as possible without triggering
0059   /// significant incremental compile time or code size growth.
0060   ///
0061   /// The key idea is that optimizations at this level should "pay for
0062   /// themselves". So if an optimization increases compile time by 5% or
0063   /// increases code size by 5% for a particular benchmark, that benchmark
0064   /// should also be one which sees a 5% runtime improvement. If the compile
0065   /// time or code size penalties happen on average across a diverse range of
0066   /// LLVM users' benchmarks, then the improvements should as well.
0067   ///
0068   /// And no matter what, the compile time needs to not grow superlinearly
0069   /// with the size of input to LLVM so that users can control the runtime of
0070   /// the optimizer in this mode.
0071   ///
0072   /// This is expected to be a good default optimization level for the vast
0073   /// majority of users.
0074   static const OptimizationLevel O2;
0075   /// Optimize for fast execution as much as possible.
0076   ///
0077   /// This mode is significantly more aggressive in trading off compile time
0078   /// and code size to get execution time improvements. The core idea is that
0079   /// this mode should include any optimization that helps execution time on
0080   /// balance across a diverse collection of benchmarks, even if it increases
0081   /// code size or compile time for some benchmarks without corresponding
0082   /// improvements to execution time.
0083   ///
0084   /// Despite being willing to trade more compile time off to get improved
0085   /// execution time, this mode still tries to avoid superlinear growth in
0086   /// order to make even significantly slower compile times at least scale
0087   /// reasonably. This does not preclude very substantial constant factor
0088   /// costs though.
0089   static const OptimizationLevel O3;
0090   /// Similar to \c O2 but tries to optimize for small code size instead of
0091   /// fast execution without triggering significant incremental execution
0092   /// time slowdowns.
0093   ///
0094   /// The logic here is exactly the same as \c O2, but with code size and
0095   /// execution time metrics swapped.
0096   ///
0097   /// A consequence of the different core goal is that this should in general
0098   /// produce substantially smaller executables that still run in
0099   /// a reasonable amount of time.
0100   static const OptimizationLevel Os;
0101   /// A very specialized mode that will optimize for code size at any and all
0102   /// costs.
0103   ///
0104   /// This is useful primarily when there are absolute size limitations and
0105   /// any effort taken to reduce the size is worth it regardless of the
0106   /// execution time impact. You should expect this level to produce rather
0107   /// slow, but very small, code.
0108   static const OptimizationLevel Oz;
0109 
0110   bool isOptimizingForSpeed() const { return SizeLevel == 0 && SpeedLevel > 0; }
0111 
0112   bool isOptimizingForSize() const { return SizeLevel > 0; }
0113 
0114   bool operator==(const OptimizationLevel &Other) const {
0115     return SizeLevel == Other.SizeLevel && SpeedLevel == Other.SpeedLevel;
0116   }
0117   bool operator!=(const OptimizationLevel &Other) const {
0118     return SizeLevel != Other.SizeLevel || SpeedLevel != Other.SpeedLevel;
0119   }
0120 
0121   unsigned getSpeedupLevel() const { return SpeedLevel; }
0122 
0123   unsigned getSizeLevel() const { return SizeLevel; }
0124 };
0125 } // namespace llvm
0126 
0127 #endif