Created attachment 46659 [details] testcase For the attached code, gcc "10.0.0 20190730" -O3 generates: foo: movsx rdi, edi mov eax, 1 mov edx, DWORD PTR arr[0+rdi*4] test edx, edx je .L1 xor eax, eax cmp edx, 10 setg al add eax, eax .L1: ret bar: xor eax, eax cmp edi, 2 seta al ret baz: xor eax, eax ret clang "7.1.0" and "8.0.1" -O3 generate the optimal code for all 3 versions: foo: xor eax, eax ret bar: xor eax, eax ret baz: xor eax, eax ret
For bar, we don't take advantage of the uninitialized variable enough, we still have "a_3(D) == 0" at expansion time (RTL gets rid of it, but too conservatively). For foo, indeed computing the min and max values of the array to get a range for any access to it would be nice.
Associating the minimum and maximum with the declaration of each constant array object would be a simple way of doing it but I'm not sure how much it would gain. Doing better would mean a linear traversal of the array. That seems to be what Clang does -- it folds even equalities with values in the range [MIN, MAX] that aren't in the array. An only slightly less costly approach would be to only search the elements between the lower and upper bounds of the index. I pondered doing this for strcmp and strlen but the linear search felt too expensive (Clang doesn't do it). It might be worth getting some data on how often it would trigger and what it would cost.
Related to https://gcc.gnu.org/ml/gcc-patches/2017-04/msg01559.html (which hasn't been applied).
Dup. *** This bug has been marked as a duplicate of bug 80603 ***