This is an archive of the discontinued LLVM Phabricator instance.

[SCEV] Generalize the SCEV algorithm for creating expressions for PHI nodes
ClosedPublic

Authored by sbaranga on Oct 29 2015, 7:11 AM.

Details

Summary

When forming expressions for phi nodes having an incoming value from
outside the loop A and a value coming from the previous iteration B
we were forming an AddRec if:

  • B was an AddRec
  • the value A was equal to the value for B at iteration -1 (or equal to the value of B shifted by one iteration, at iteration 0)

In this case, we were computing the expression to be the expression of
B, shifted by one iteration.

This changes generalizes the logic above by removing the restriction that
B needs to be an AddRec. For this we introduce two expression rewriters
that allow us to

  • shift an expression by one iteration
  • get the value of an expression at iteration 0

This allows us to get SCEV expressions for PHI nodes when these expressions
are not AddRecExprs.

Diff Detail

Repository
rL LLVM

Event Timeline

sbaranga updated this revision to Diff 38735.Oct 29 2015, 7:11 AM
sbaranga retitled this revision from to Generalize the SCEV algorithm for creating expressions for PHI nodes.
sbaranga updated this object.
sbaranga added a reviewer: sanjoy.
sbaranga added a subscriber: llvm-commits.
sbaranga retitled this revision from Generalize the SCEV algorithm for creating expressions for PHI nodes to [SCEV] Generalize the SCEV algorithm for creating expressions for PHI nodes.Oct 29 2015, 7:13 AM
sanjoy requested changes to this revision.Oct 29 2015, 11:41 AM
sanjoy edited edge metadata.

Overall, looks good. I just have one comment inline.

lib/Analysis/ScalarEvolution.cpp
3818 ↗(On Diff #38735)

Why not do {A,+,B} => (A-B) as a single transform?

Also, a nice way to document this rule is (totally up to you if you like this language):

PHI(f(I), f({I+S,+,S})) --> f({I,+,S})
This revision now requires changes to proceed.Oct 29 2015, 11:41 AM
sanjoy accepted this revision.Oct 30 2015, 7:33 AM
sanjoy edited edge metadata.

My previous comment was not correct, so no need to address it. But please add a one line documentation for the two rewriters you added (something like F({S,+,X}) ==> F({S-X,+,X}) would be fine).

lib/Analysis/ScalarEvolution.cpp
3818 ↗(On Diff #38735)

Actually, I'll take back that comment -- you need to use Shift below.

This revision is now accepted and ready to land.Oct 30 2015, 7:33 AM
sbaranga updated this revision to Diff 38808.Oct 30 2015, 7:39 AM
sbaranga edited edge metadata.

Formally document the generatlized Phi formation rule.

sbaranga added inline comments.Oct 30 2015, 7:42 AM
lib/Analysis/ScalarEvolution.cpp
3818 ↗(On Diff #38808)

Thanks, Sanjoy! Yes, we needed Shift anyway.

I've added a slightly modified rule (it should be equivalent to the one you've posted above).

sbaranga closed this revision.Oct 30 2015, 8:04 AM
This revision was automatically updated to reflect the committed changes.