Hello, we found some optimizations (regarding Loop Unswitch) that LLVM may have missed. We would greatly appreicate if you can take a look and let us know you think.
extern int var_17;
extern int var_29;
extern int var_32;
extern int var_33;
void test(int var_0, int var_1, int var_2, int var_3, int var_4, int var_5, int var_6, int var_7, int var_8, int var_9, int var_10, int var_11, int var_12, int var_13) {
for (int i_0 = ((var_9) - (1181835238))/*2*/; i_0 < ((var_4) + (1034010885))/*11*/; i_0 += 1/*1*/)
{
if (var_12)
{
var_29 += (var_4 ? (var_2 ? var_0 : var_5) : var_12) ? (var_13 ? var_2 : var_6 ? (-1897050213) : var_6) : (var_1 ? var_5 ? (-296177307) : (-12666761) : 921754019);
var_32 += (var_12 ? var_0 : var_4) ? (var_2 ? 2097151 : (var_3 ? var_8 : var_0)) : var_2;
var_33 += ((var_5 ? (-1) : (-1550420726)) ? (var_3 ? 2147483647 : var_6) : 2147483647) ? (var_0 ? (var_5 ? var_8 : var_9) : 699136968) : (var_2 ? var_7 : (var_7 ? -1 : 133169152));
var_17 += var_13 ? (var_5 ? var_1 : 2147483625) : var_4;
}
}
}
Because var_12 is a loop invariant so we can hoist the if condition out of the loop, as a result, the if condition will only be evaluated once. In particular, if var_12 is false, the for loop doesn't need to be executed at all.
But LLVM misses this optimization with a trunk build (https://godbolt.org/z/czcPThGrT).
Also, this example works as expected on LLVM 16.
Hello, we found some optimizations (regarding Loop Unswitch) that LLVM may have missed. We would greatly appreicate if you can take a look and let us know you think.
Here are two examples:
Example 1: https://godbolt.org/z/czcPThGrT
Because
var_12
is a loop invariant so we can hoist theif
condition out of the loop, as a result, theif
condition will only be evaluated once. In particular, ifvar_12
is false, thefor
loop doesn't need to be executed at all.But LLVM misses this optimization with a trunk build (https://godbolt.org/z/czcPThGrT). Also, this example works as expected on LLVM 16.
The result of a trunk build:
Example 2: https://godbolt.org/z/1Mz7b7d6a
Similarly,
var_3
is an loop invariant and should be hoisted out of the loop as well.The result with a trunk build (main parts):
Thank you very much for your time and effort! We look forward to hearing from you.