i386: Avoid false dependency for BSF instruction
i386 backend now correctly handles the BSF instruction to avoid false performance dependencies on zero sources.
This commit addresses a performance issue on the i386 architecture where the BSF (Bit Scan Forward) instruction, like BSR, can create a false data dependency when its source operand is zero. The fix modifies the backend to clear the destination register before executing BSF in such cases, leveraging new tuning options (TARGET_AVOID_FALSE_DEP_FOR_BSR) to prevent performance degradation. It also improves the handling of related instructions like ctz.
In Details
The BSF instruction on x86 processors, similar to BSR, leaves its destination register unmodified when the source is zero. Hardware treats this as a read-modify-write, creating a false dependency that can hinder performance. This patch introduces TARGET_AVOID_FALSE_DEP_FOR_BSR to clear the destination before BSF when TARGET_BMI is not enabled, and refactors ctz splitters to also utilize this tuning. Previously, cases without TZCNT or BMI would incorrectly hit gcc_unreachable.
- BSF
- BSF (Bit Scan Forward) is an x86 instruction that searches for the least significant set bit in a source operand and stores the index of that bit in the destination operand. If the source is zero, the destination is undefined or modified in a way that can cause false dependencies.
- BSR
- BSR (Bit Scan Reverse) is an x86 instruction that searches for the most significant set bit in a source operand and stores the index in the destination operand. Similar to BSF, it can create false dependencies when the source is zero.
- false dependency
- A false dependency, also known as an anti-dependency or output dependency, occurs in processor pipelines when an instruction needs to write to a register that a later instruction has already read or written to. Modern CPUs try to overcome these through techniques like register renaming, but specific instruction sequences can still cause stalls.
- BMI
- BMI (Bit Manipulation Instructions) is a set of x86 instruction set extensions that provide efficient bitwise operations, including instructions like
TZCNT(Trailing Zero Count) andLZCNT(Leading Zero Count), which are often faster than the genericBSF/BSRfor specific tasks. - tuning
- In GCC, 'tuning' refers to architecture-specific optimizations and instruction selections that are tailored to particular processor models or features. This can involve choosing specific instruction sequences to maximize performance or avoid hardware limitations.