[IndVars] Fix usage of SCEVExpander to not mess with SCEVConstant. PR38674
authorMax Kazantsev <max.kazantsev@azul.com>
Tue, 4 Sep 2018 05:01:35 +0000 (05:01 +0000)
committerMax Kazantsev <max.kazantsev@azul.com>
Tue, 4 Sep 2018 05:01:35 +0000 (05:01 +0000)
commit2cbba5633753552a984572c8b9a5997e5c96496d
treea900bb79706f535da925a223e42953de9104d578
parentbd203e03f89a0664c22c7b9ca74fd1b2fdf6f0c0
[IndVars] Fix usage of SCEVExpander to not mess with SCEVConstant. PR38674

This patch removes the function `expandSCEVIfNeeded` which behaves not as
it was intended. This function tries to make a lookup for exact existing expansion
and only goes to normal expansion via `expandCodeFor` if this lookup hasn't found
anything. As a result of this, if some instruction above the loop has a `SCEVConstant`
SCEV, this logic will return this instruction when asked for this `SCEVConstant` rather
than return a constant value. This is both non-profitable and in some cases leads to
breach of LCSSA form (as in PR38674).

Whether or not it is possible to break LCSSA with this algorithm and with some
non-constant SCEVs is still in question, this is still being investigated. I wasn't
able to construct such a test so far, so maybe this situation is impossible. If it is,
it will go as a separate fix.

Rather than do it, it is always correct to just invoke `expandCodeFor` unconditionally:
it behaves smarter about insertion points, and as side effect of this it will choose a
constant value for SCEVConstants. For other SCEVs it may end up finding a better insertion
point. So it should not be worse in any case.

NOTE: So far the only known case for which this transform may break LCSSA is mapping
of SCEVConstant to an instruction. However there is a suspicion that the entire algorithm
can compromise LCSSA form for other cases as well (yet not proved).

Differential Revision: https://reviews.llvm.org/D51286
Reviewed By: etherzhhb

llvm-svn: 341345
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
llvm/test/Transforms/IndVarSimplify/pr38674.ll [new file with mode: 0644]