llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.34k stars 12.13k forks source link

[RISCV] missed optimization with pointer induction in loop #59366

Open vsukhoml opened 1 year ago

vsukhoml commented 1 year ago

In example https://godbolt.org/z/b8z5P1KEz

size_t strlen2(const char *s)
{
    const char *s1 = s;
    while (*s++) ;

    return (size_t)(s-s1-1);
}

GCC frequently produce kernel loop with 3 instructions (sometimes with bad block splitting in other variants):

.L8:
        lbu     a4,0(a5)
        addi    a5,a5,1
        bne     a4,zero,.L8

while LLVM produce 4 instructions:

.LBB2_1:                                # =>This Inner Loop Header: Depth=1
        add     a2, a0, a1
        lbu     a2, 0(a2)
        addi    a1, a1, 1
        bnez    a2, .LBB2_1
llvmbot commented 1 year ago

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

topperc commented 1 year ago

Looks like -mllvm -lsr-drop-solution improves this.

cc: @eopXD

ghost commented 1 year ago

Donnieboi420@outlook.com

lukel97 commented 4 months ago

I think https://github.com/llvm/llvm-project/pull/89927 is relevant here, it enables -mllvm -lsr-drop-solution by default