GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc Performance Win

Improve boolean logical RTL sequences using De Morgan's laws

Optimizes boolean logic in RTL by applying De Morgan's laws to simplify operations.

This commit optimizes RTL sequences for boolean logical operations, particularly for targets like RISC-V with limited conditional instruction sets. It leverages De Morgan’s laws to rewrite sequences involving OR and XOR operations. Specifically, it transforms (ior (xor A 1) (xor B 1)) into (xor (and A B) 1) when A and B are known to be 0/1 values. This optimization can significantly reduce the number of dynamic instructions, as demonstrated by a reduction in x264’s performance.

In Details

This patch refactors simplify_context::simpify_binary_operation_1 to apply De Morgan's laws for simplifying boolean logical operations within RTL. The specific transformation is (ior (xor A 1) (xor B 1)) → (xor (and A B) 1) when operands A and B are constrained to 0/1 values. This is particularly beneficial for RISC-V, which has a minimal set of conditional compare instructions, forcing the compiler to derive others through sequences that can be simplified by this transformation.

For Context
RTL
Register Transfer Language, GCC's internal intermediate representation used for lower-level optimizations and machine code generation.
De Morgan's laws
A pair of transformation rules in Boolean algebra that relate the logical operators AND, OR, and NOT. They state that NOT (A AND B) is equivalent to (NOT A) OR (NOT B), and NOT (A OR B) is equivalent to (NOT A) AND (NOT B).
RISC-V
An open-standard instruction set architecture (ISA) based on reduced instruction set computing principles.
sCC style instructions
Conditional compare instructions that set condition flags based on the comparison result.
GIMPLE
GCC's internal three-address intermediate representation, used for high-level optimization passes.
x264
A popular open-source library for encoding video into the H.264/MPEG-4 AVC format.
Filed Under: optimizationrisc-vrtl