llvm/llvm-project

Missed Optimization: Aligned Pointer Optimizations Can't Happen With Prefered OR Instead of ADD

Open

#84,401 建立於 2024年3月7日

在 GitHub 查看
 (8 留言) (0 反應) (1 負責人)C++ (10,782 fork)batch import
good first issuellvm:optimizationsmissed-optimization

倉庫指標

Star
 (26,378 star)
PR 合併指標
 (平均合併 1天 2小時) (30 天內合併 1,000 個 PR)

描述

Given code such as:

void src(u8 arr[], u8 offset, u16 value) {
    if ((offset & 1) != 0) {
        __builtin_unreachable();
    }
    arr[offset + 0] = value;
    arr[offset + 1] = value >> 8;
}

void tgt(u8 arr[], u8 offset, u16 value) {
    arr[offset + 0] = value;
    arr[offset + 1] = value >> 8;
}

When the offset isn't known to align and just addition is used, this can compile to a single halfword store -- Whenever the offset is known though, the addition is folded into an OR and the folding optimization (which seems to occur during target selection) is unable to be taken! Notably not all targets have this optimization either. For example, ARMv6 doesn't, but ARMv7 does!! A similar issue was presented previously (#69938) but afaik this is a different optimization entirely? As a pull request which possibly should've remedied that issue does not fix this one! This issue does not persist with a constant offset.

Tested on Godbolt with Clang (trunk) and nightly Rustc Alive2: https://alive2.llvm.org/ce/z/qjcLWd Godbolt: https://godbolt.org/z/93jo9aKTx

貢獻者指南