https://bugs.llvm.org/show_bug.cgi?id=52392

            Bug ID: 52392
           Summary: Missed optimization opportunity for loads with a
                    dynamic index with a known small value range (e.g
                    0,1).
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedb...@nondot.org
          Reporter: t...@google.com
                CC: alina.sbir...@gmail.com, htmldevelo...@gmail.com,
                    llvm-bugs@lists.llvm.org

Loads from an read-only array of known values with a dynamic index which is
known to have only few values (e.g 0 and 1), results in an unnecessary read
from memory.

E.g: https://godbolt.org/z/4r675d6dM

// This keeps 'a' on stack and reads return value from there.
int slow(int num, int v1, int v2) {
    int a[2] = {v1, v2};
    return a[num&1];
}

//slow (int, int, int):
//        mov     dword ptr [rsp - 8], esi
//        mov     dword ptr [rsp - 4], edx
//        and     edi, 1
//        mov     eax, dword ptr [rsp + 4*rdi - 8]
//        ret

// If we add more code to manually fetch values from 'a', we end up 
// with returning one v1/v1 directly and no longer need 'a'.
int fast(int num, int v1, int v2) {
    int a[2] = {v1, v2};
    int i0 = a[0];
    int i1 = a[1];
    return (num&1) ? i1:i0;
}
// fast (int, int, int):
//        mov     eax, esi
//        test    dil, 1
//        cmovne  eax, edx
//        ret

Being able to get rid of local variables is rather important for performance 
on GPUs (NVPTX, AMDGPU) and picking one of the few items in an array is a
fairly common pattern.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to