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: unassignedb...@nondot.org
          Reporter: david.bolvan...@gmail.com
                CC: llvm-bugs@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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to