This is an archive of the discontinued LLVM Phabricator instance.

[SCEV] Add one more case in computeConstantDifference
ClosedPublic

Authored by mkazantsev on Feb 26 2018, 3:05 AM.

Details

Summary

This patch teaches computeConstantDifference handle calculation of constant
difference between (X + C1) and (X + C2) which is (C2 - C1).

Diff Detail

Repository
rL LLVM

Event Timeline

mkazantsev created this revision.Feb 26 2018, 3:05 AM
mkazantsev added inline comments.Feb 28 2018, 3:33 AM
lib/Analysis/ScalarEvolution.cpp
9805 ↗(On Diff #135879)

This is incorrect. LHS and FoundLHS can have different types. I will fix this before merging this patch.

mkazantsev added inline comments.Feb 28 2018, 3:38 AM
lib/Analysis/ScalarEvolution.cpp
9455 ↗(On Diff #135879)

Also incorrect, because types of LHS and FoundLHS doesn't have to match.

sanjoy requested changes to this revision.Feb 28 2018, 9:59 PM
sanjoy added inline comments.
lib/Analysis/ScalarEvolution.cpp
9396 ↗(On Diff #135879)

I'd much rather not put all of this (albeit non-recursive) work here when it is possible to easily extend the existing code to handle your pattern.

9455 ↗(On Diff #135879)

Do you have a test case for this? ScalarEvolution::isImpliedCond should be zero/sign extending LHS/FoundLHS to make both of them have the same type before calling ScalarEvolution::isImpliedCondOperands. If there are other callsites that don't respect this invariant they should zero/sign extend as well.

This revision now requires changes to proceed.Feb 28 2018, 9:59 PM
mkazantsev added inline comments.Mar 1 2018, 2:46 AM
lib/Analysis/ScalarEvolution.cpp
9455 ↗(On Diff #135879)

Never mind, it seems that I saw this failing due to a bug in another patch I'm working on. False alarm, most likely.

mkazantsev added inline comments.Mar 1 2018, 2:49 AM
lib/Analysis/ScalarEvolution.cpp
9396 ↗(On Diff #135879)

Do we really want to add every easy pattern we find here in future just because it is easy? It looks counter-general. And we don't know how many other relatively simple cases we do not handle.

sanjoy added inline comments.Mar 2 2018, 9:53 AM
lib/Analysis/ScalarEvolution.cpp
9396 ↗(On Diff #135879)

Do we really want to add every easy pattern we find here in future just because it is easy?

If this function starts growing too much with a lot of ad-hoc strategies, we need some other way to handle the kinds of cases you're trying to handle (perhaps by making code higher up the stack smarter).

But for now you should be able to easily handle LHS = {X + C1,+,step}, RHS = {X + C2,+,step},

mkazantsev retitled this revision from [SCEV] Smarter logic in computeConstantDifference to [SCEV] Add one more case in computeConstantDifference.
mkazantsev edited the summary of this revision. (Show Details)

Allright, let's do it without getAddExpr.

anna added inline comments.Mar 23 2018, 11:02 AM
test/Transforms/IndVarSimplify/promote-iv-to-eliminate-casts.ll
105 ↗(On Diff #137975)

IIUC, this test uses the logic in computeConstantDifference for (X + C2) - (X + C1)`.
Is it possible to add tests for the other rules that you've added:
X - (X+ C1).
That should just be setting iv to len (instead of len.minus.1). That's an out of bounds, but something along these lines should work as a test right?

mkazantsev added inline comments.Mar 25 2018, 10:47 PM
test/Transforms/IndVarSimplify/promote-iv-to-eliminate-casts.ll
105 ↗(On Diff #137975)

I haven't added these cases for X - (X + C1), they were here before, I've just refactored and commented the code slightly. :) But OK, I will add some tests on these situations.

mkazantsev marked an inline comment as done.

Added some tests.

anna accepted this revision.Mar 26 2018, 5:51 AM

lgtm!

This revision was not accepted when it landed; it landed in state Needs Review.Mar 26 2018, 9:56 PM
This revision was automatically updated to reflect the committed changes.