llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[InstCombine] Known range pessimizes a pointer advance by narrowing it to i8 + mask

Open chfast opened this issue 3 months ago • 2 comments

Telling clang the true range of a byte makes the code it generates worse. plain and assumed differ only by a __builtin_assume that holds anyway; with it, InstCombine narrows the pointer advance to i8 and has to mask the result, costing an instruction on x86-64, AArch64 and RISC-V alike. Widening the operand before the arithmetic avoids it. Godbolt: https://godbolt.org/z/GK4Phh8jT

typedef __PTRDIFF_TYPE__ ptrdiff_t;

const signed char *plain(const signed char *p)
{
    const signed char op = *p;
    return p + (op - 94);
}

const signed char *assumed(const signed char *p)
{
    const signed char op = *p;
    __builtin_assume(op >= 96);
    return p + (op - 94);
}

const signed char *assumed_widened(const signed char *p)
{
    const signed char op = *p;
    __builtin_assume(op >= 96);
    return p + ((ptrdiff_t)op - 94);
}

The IR, where %2 is the loaded i8:

; plain, assumed_widened
  sext i8 %2 to i64          ; resp. zext nneg i8 %2 to i64, then -94 folded into a getelementptr

; assumed
  %4 = add nuw i8 %2, 34
  %5 = and i8 %4, 63
  %6 = zext nneg i8 %5 to i64

op - 94 is int arithmetic in the source and the load already materializes the value in a wide register, so narrowing it to i8 only creates work: the and exists solely to make the narrow form correct. Instruction counts, clang 23 (trunk) at -O2, same on clang 21:

x86-64 AArch64 rv64im
plain 4 4 4
assumed 5 5 5
assumed_widened 4 4 4

The same thing happens without __builtin_assume when a dominating comparison supplies the range, which is how I ran into it: a loop with an if (op < 91) { ++p; continue; } guard ahead of p += op - 94. There the masked advance is no longer a constant on the short path, so it also costs the loop its private increment.

chfast avatar Aug 19 '26 11:08 chfast

@llvm/issue-subscribers-backend-risc-v

Author: Paweł Bylica (chfast)

`f_good` and `f_bad` below differ only in how one constant advance is spelled — `op - 94` versus `op - 96 + 2`, the same value either way — but on rv64 clang gives the second a longer hot path: for `f_bad` it narrows the advance to `i8` arithmetic and has to mask the result (`add nuw i8 %op, 34` / `and i8 %14, 63`), and because the advance is no longer a constant on the short path it sinks a single shared `add` into the latch and materializes the step in a register every iteration, so the `op < 91` path becomes `lb; li; blt; add; bgeu` (5) instead of `lb; bge; addi; bgeu` (4), and the `op >= 96` path goes from 7 instructions to 10; GCC emits the same instruction sequence for both spellings. Godbolt: https://godbolt.org/z/3nMPojTKv
void f_good(long *out, const signed char *p, const signed char *end)
{
    while (p &lt; end)
    {
        const signed char op = *p;
        if (op &lt; 91) { ++p; continue; }
        if (op &gt;= 96) { p += op - 94; continue; }
        ++*out;
        ++p;
    }
}

void f_bad(long *out, const signed char *p, const signed char *end)
{
    while (p &lt; end)
    {
        const signed char op = *p;
        if (op &lt; 91) { ++p; continue; }
        if (op &gt;= 96) { p += op - 96 + 2; continue; }   // same value as op - 94
        ++*out;
        ++p;
    }
}

clang --target=riscv64 -march=rv64im -mabi=lp64 -O2, loops only:

f_good:                             f_bad:
.LBB0_3:                            .LBB1_3:
        addi    a1, a1, 1                   add     a1, a1, a5      # shared advance
        bgeu    a1, a2, .LBB0_7             bgeu    a1, a2, .LBB1_7
.LBB0_4:                            .LBB1_4:
        lb      a5, 0(a1)                   lb      a6, 0(a1)
        bge     a3, a5, .LBB0_3             li      a5, 1           # step materialized
        zext.b  a5, a5                      blt     a6, a3, .LBB1_3
        bltu    a5, a4, .LBB0_2             zext.b  a6, a6
        add     a1, a1, a5                  bltu    a6, a4, .LBB1_2
        addi    a1, a1, -94                 addi    a5, a6, 34      # narrowed to i8
        bltu    a1, a2, .LBB0_4             andi    a5, a5, 63      # ...so it needs a mask
                                            j       .LBB1_3

The relevant IR difference — f_good keeps the loaded byte intact and folds the constant into a GEP, f_bad does the arithmetic in i8:

; f_good
  %16 = zext nneg i8 %9 to i64
  %17 = getelementptr i8, ptr %8, i64 %16
  %18 = getelementptr i8, ptr %17, i64 -94

; f_bad
  %14 = add nuw i8 %9, 34
  %15 = and i8 %14, 63
  %16 = zext nneg i8 %15 to i64

The and is what stops the short path from keeping its own addi. Both spellings are int arithmetic in the source, so the narrowing to i8 is a choice InstCombine makes only for the second one. Reproduces with clang 21 and clang 23 (trunk). The two paths also collapse to identical code if the third branch is removed, so the narrowing appears to need the range information the second comparison provides.

I'd like to take a look at this one.

Bagodiya avatar Aug 20 '26 19:08 Bagodiya