[SCEV] Avoid compile time explosion in ScalarEvolution::isImpliedCond
authorBjorn Pettersson <bjorn.a.pettersson@ericsson.com>
Tue, 19 Oct 2021 15:05:11 +0000 (17:05 +0200)
committerBjorn Pettersson <bjorn.a.pettersson@ericsson.com>
Tue, 19 Oct 2021 19:37:57 +0000 (21:37 +0200)
commit08619006a0c0694477f143dc1552eab35701e50b
tree38e3822385509b4e0953e86ca65059f1c7937d02
parent0836a1059dcf8e4fbf408248bf5eed13dfd93f7b
[SCEV] Avoid compile time explosion in ScalarEvolution::isImpliedCond

As seen in PR51869 the ScalarEvolution::isImpliedCond function might
end up spending lots of time when doing the isKnownPredicate checks.

Calling isKnownPredicate for example result in isKnownViaInduction
being called, which might result in isLoopBackedgeGuardedByCond being
called, and then we might get one or more new calls to isImpliedCond.
Even if the scenario described here isn't an infinite loop, using
some random generated C programs as input indicates that those
isKnownPredicate checks quite often returns true. On the other hand,
the third condition that needs to be fulfilled in order to "prove
implications via truncation", i.e. the isImpliedCondBalancedTypes
check, is rarely fulfilled.
I also made some similar experiments to look at how often we would
get the same result when using isKnownViaNonRecursiveReasoning instead
of isKnownPredicate. So far I haven't seen a single case when codegen
is negatively impacted by using isKnownViaNonRecursiveReasoning. On
the other hand, it seems like we get rid of the compile time explosion
seen in PR51869 that way. Hence this patch.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D112080
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/pr51869-scalar-evolution-prove-implications-via-truncation.ll [new file with mode: 0644]