|
12 | 12 |
|
13 | 13 | namespace biscuit { |
14 | 14 |
|
| 15 | +/** |
| 16 | + * Biscuit can attempt to optimize emitted assembly. These optimization modes are disabled by default. |
| 17 | + * To enable an optimization mode, use the EnableOptimization function: |
| 18 | + * |
| 19 | + * @code{.cpp} |
| 20 | + * biscuit::Assembler as; |
| 21 | + * as.EnableOptimization(Optimization::AutoCompress); |
| 22 | + * @endcode |
| 23 | + * |
| 24 | + * In some cases, it may be preferrable to disable an optimization for a short while. This can be done like so: |
| 25 | + * |
| 26 | + * @code{.cpp} |
| 27 | + * as.DisableOptimization(Optimization::AutoCompress); |
| 28 | + * // ... |
| 29 | + * as.EnableOptimization(Optimization::AutoCompress); |
| 30 | + * @endcode |
| 31 | + * |
| 32 | + * The Optimization enum can be used as a bitmask, which means it is possible to enable or disable multiple optimizations at once: |
| 33 | + * |
| 34 | + * @code{.cpp} |
| 35 | + * as.EnableOptimization(Optimization::AutoCompress | Optimization::Placeholder); |
| 36 | + * @endcode |
| 37 | + */ |
| 38 | +enum class Optimization : uint32_t { |
| 39 | + None = 0, |
| 40 | + |
| 41 | + /** |
| 42 | + * Automatically converts instructions to their compressed 2-byte form whenever possible. |
| 43 | + * For example, this optimization mode will convert a MV instruction to a C.MV instruction |
| 44 | + * as long as rd and rs are not the zero register. |
| 45 | + */ |
| 46 | + AutoCompress = 1, |
| 47 | +}; |
| 48 | + |
| 49 | +constexpr Optimization operator|(Optimization lhs, Optimization rhs) { |
| 50 | + return static_cast<Optimization>( |
| 51 | + static_cast<std::underlying_type_t<Optimization>>(lhs) | static_cast<std::underlying_type_t<Optimization>>(rhs) |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +constexpr Optimization operator&(Optimization lhs, Optimization rhs) { |
| 56 | + return static_cast<Optimization>( |
| 57 | + static_cast<std::underlying_type_t<Optimization>>(lhs) & static_cast<std::underlying_type_t<Optimization>>(rhs) |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +constexpr Optimization operator^(Optimization lhs, Optimization rhs) { |
| 62 | + return static_cast<Optimization>( |
| 63 | + static_cast<std::underlying_type_t<Optimization>>(lhs) ^ static_cast<std::underlying_type_t<Optimization>>(rhs) |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +constexpr Optimization operator~(Optimization opt) { |
| 68 | + return static_cast<Optimization>(~static_cast<std::underlying_type_t<Optimization>>(opt)); |
| 69 | +} |
| 70 | + |
| 71 | +constexpr Optimization& operator|=(Optimization& lhs, Optimization rhs) { |
| 72 | + lhs = lhs | rhs; |
| 73 | + return lhs; |
| 74 | +} |
| 75 | + |
| 76 | +constexpr Optimization& operator&=(Optimization& lhs, Optimization rhs) { |
| 77 | + lhs = lhs & rhs; |
| 78 | + return lhs; |
| 79 | +} |
| 80 | + |
| 81 | +constexpr Optimization& operator^=(Optimization& lhs, Optimization rhs) { |
| 82 | + lhs = lhs ^ rhs; |
| 83 | + return lhs; |
| 84 | +} |
| 85 | + |
15 | 86 | /** |
16 | 87 | * Defines the set of features that a particular assembler instance |
17 | 88 | * would like to assemble for. |
@@ -150,6 +221,35 @@ class Assembler { |
150 | 221 | return m_buffer.GetOffsetPointer(offset); |
151 | 222 | } |
152 | 223 |
|
| 224 | + /** |
| 225 | + * Checks whether the optimizations set in a bitmask are enabled |
| 226 | + * |
| 227 | + * @param opt The bitmask to check. |
| 228 | + * |
| 229 | + * @returns Whether all of the optimizations in the bitmask were enabled |
| 230 | + */ |
| 231 | + [[nodiscard]] bool IsOptimizationEnabled(Optimization opt) const noexcept { |
| 232 | + return (m_optimizations & opt) == opt; |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Enables the optimizations set in a bitmask |
| 237 | + * |
| 238 | + * @param opt The bitmask to enable. |
| 239 | + */ |
| 240 | + void EnableOptimization(Optimization opt) noexcept { |
| 241 | + m_optimizations |= opt; |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Disables the optimizations set in a bitmask |
| 246 | + * |
| 247 | + * @param opt The bitmask to disable. |
| 248 | + */ |
| 249 | + void DisableOptimization(Optimization opt) noexcept { |
| 250 | + m_optimizations &= ~opt; |
| 251 | + } |
| 252 | + |
153 | 253 | /** |
154 | 254 | * Binds a label to the current offset within the code buffer |
155 | 255 | * |
@@ -1622,6 +1722,7 @@ class Assembler { |
1622 | 1722 |
|
1623 | 1723 | CodeBuffer m_buffer; |
1624 | 1724 | ArchFeature m_features = ArchFeature::RV64; |
| 1725 | + Optimization m_optimizations = Optimization::None; |
1625 | 1726 | }; |
1626 | 1727 |
|
1627 | 1728 | } // namespace biscuit |
0 commit comments