[llvm-bugs] [Bug 42700] New: Replace loop-based variable with loop invariant variable

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Jul 21 04:48:26 PDT 2019


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

            Bug ID: 42700
           Summary: Replace loop-based variable with loop invariant
                    variable
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Loop Optimizer
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

int f(bool* arr, bool x) {

    int ss = 0;
    for (int i = 0; i < 16; ++i) {
        if (arr[i] == x) { 
            // [0]
            ss += (arr[i]) ? 1 : -1;
        }
    }
    return ss;
}


[0] x is not modified in this branch so try to replace:
ss += (arr[i]) ? 1 : -1;
with:
ss += (x) ? 1 : -1;


int f2(bool* arr, bool x) {

    int ss = 0;
    for (int i = 0; i < 16; ++i) {
        if (arr[i] == x) { 
            ss += (x) ? 1 : -1;
        }
    }
    return ss;
}


It may help LICM to hoist some code, help Loop
distribution/unswitch/vectorization pass, etc..

For my motivating example, current Clang trunk -O3 just unrolls 'f' (for N=16)
/ vectorizes 'f' (N=64+). Clang vectorizes 'f2' even for N=16+.

Godbolt: https://godbolt.org/z/c7HwFi

-- 
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/20190721/be71b1ed/attachment.html>


More information about the llvm-bugs mailing list