[SCEV] Do not fold dominated SCEVUnknown into AddRecExpr start
authorMax Kazantsev <max.kazantsev@azul.com>
Wed, 24 May 2017 08:52:18 +0000 (08:52 +0000)
committerMax Kazantsev <max.kazantsev@azul.com>
Wed, 24 May 2017 08:52:18 +0000 (08:52 +0000)
commit13e016bf48e811a7f852a363211ba97d7af442f6
treeacf200fd711409250fe4b621393fb7a88daa36e8
parent354439a5a1ec7bba0b18a1cb87d8755989103d83
[SCEV] Do not fold dominated SCEVUnknown into AddRecExpr start

When folding arguments of AddExpr or MulExpr with recurrences, we rely on the fact that
the loop of our base recurrency is the bottom-lost in terms of domination. This assumption
may be broken by an expression which is treated as invariant, and which depends on a complex
Phi for which SCEVUnknown was created. If such Phi is a loop Phi, and this loop is lower than
the chosen AddRecExpr's loop, it is invalid to fold our expression with the recurrence.

Another reason why it might be invalid to fold SCEVUnknown into Phi start value is that unlike
other SCEVs, SCEVUnknown are sometimes position-bound. For example, here:

for (...) { // loop
  phi = {A,+,B}
}
X = load ...
Folding phi + X into {A+X,+,B}<loop> actually makes no sense, because X does not exist and cannot
exist while we are iterating in loop (this memory can be even not allocated and not filled by this moment).
It is only valid to make such folding if X is defined before the loop. In this case the recurrence {A+X,+,B}<loop>
may be existant.

This patch prohibits folding of SCEVUnknown (and those who use them) into the start value of an AddRecExpr,
if this instruction is dominated by the loop. Merging the dominating unknown values is still valid. Some tests that
relied on the fact that some SCEVUnknown should be folded into AddRec's are changed so that they no longer
expect such behavior.

llvm-svn: 303730
llvm/include/llvm/Analysis/ScalarEvolution.h
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/IVUsers/quadradic-exit-value.ll
llvm/test/Analysis/ScalarEvolution/different-loops-recs.ll
llvm/test/Transforms/LoopStrengthReduce/X86/incorrect-offset-scaling.ll
llvm/test/Transforms/LoopStrengthReduce/lsr-expand-quadratic.ll
llvm/test/Transforms/LoopStrengthReduce/post-inc-icmpzero.ll