https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122755
--- Comment #4 from Di Zhao <dizhao at os dot amperecomputing.com> ---
I agree that in the case I've posted, lsplit should be able to optimize
the loop if the loop boundary check is in the loop header. It might also
work for the GCC "FOR_EACH_EDGE" loop. However, I guess it's still
beneficial to enhance lsplit to handle more general cases. For example,
in this slightly modified version, the predicate is not in loop header?
class A
{
public:
int a1;
int a2;
void * data1;
void * data2;
};
int dom_computed;
extern void* some_predicate;
extern bool
maybe_pure_maybe_not (void *, void *);
bool
foo (unsigned num, class A** elems)
{
int cnt = 0;
for (unsigned i = 0; i < num; ++i)
{
class A *e = elems[i];
if (some_predicate)
break;
if (dom_computed == 2)
{
// simple branch
if (e->a1 >= e->a2)
cnt++;
}
else
{
// complex branch
if (maybe_pure_maybe_not (e->data1, e->data2))
cnt++;
}
}
return cnt == 1;
}
I’m still planning to improve my patch a bit and propose it, if that’s okay.
At least, I think the second change is helpful. Since lsplit can make use
of info like ipa-modref, it might spot invariant that loop-unswitch misses.
Say if maybe_pure_maybe_not is marked not changing dom_computed.