[llvm-bugs] [Bug 52392] New: Missed optimization opportunity for loads with a dynamic index with a known small value range (e.g 0, 1).

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Nov 3 16:00:15 PDT 2021


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: unassignedbugs at nondot.org
          Reporter: tra at google.com
                CC: alina.sbirlea at gmail.com, htmldeveloper at gmail.com,
                    llvm-bugs at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20211103/77e5aaf3/attachment-0001.html>


More information about the llvm-bugs mailing list