This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Allow some arithmetic optimizations for add/sub/div/rem look through freeze
AbandonedPublic

Authored by aqjune on Mar 30 2020, 11:15 AM.

Details

Summary

This patch makes optimizations in add/sub/div/rem in InstSimplify look through freeze.
For example, X - X => 0 can be expanded so X - freeze(X) and freeze(X) - X are also converted.
This is targeting a hypothetical case, not performance degradation that actually happened after D76483 is committed.

Proofs:

X + (Y - freeze(X)) -> Y
http://volta.cs.utah.edu:8080/z/yn2mcx

freeze(X) + (Y - X) -> Y
http://volta.cs.utah.edu:8080/z/EmNlwy

freeze(X) - X -> 0
http://volta.cs.utah.edu:8080/z/JVRhLa

freeze(X) div X -> 1, freeze(X) rem X -> 0
http://volta.cs.utah.edu:8080/z/5F6yCt

(X * freeze(Y)) / Y -> X
http://volta.cs.utah.edu:8080/z/MlbJgN

(X % freeze(Y)) / Y -> 0
http://volta.cs.utah.edu:8080/z/CQNyVN

Diff Detail

Event Timeline

aqjune created this revision.Mar 30 2020, 11:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 30 2020, 11:16 AM
aqjune edited the summary of this revision. (Show Details)Mar 30 2020, 11:17 AM
aqjune marked an inline comment as done.Mar 30 2020, 11:23 AM
aqjune added inline comments.
llvm/test/Transforms/InstSimplify/add.ll
26

I wrote tests in a way that the result is passed to call f() rather than being returned, for brevity of tests.

aqjune edited the summary of this revision. (Show Details)Mar 30 2020, 11:24 AM
xbolva00 added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
646–648

Maybe m_SpecificAllowFreeze or so is more readable than passing true?

nikic added a comment.Mar 30 2020, 2:52 PM

This is a patch to relieve possible performance degradation after D76483.

To clarify, does this address a specific issue encountered there, or is this more of a hypothetical optimization? (The last comment on that thread indicates that the issue is lack of Freeze support in SCEV.)

This is a patch to relieve possible performance degradation after D76483.

To clarify, does this address a specific issue encountered there, or is this more of a hypothetical optimization? (The last comment on that thread indicates that the issue is lack of Freeze support in SCEV.)

This is more of a hypothetical optimization, but making a few existing optimizations in InstSimplify to expand seemed straightforward to me. I'll clarify this in the summary.

aqjune edited the summary of this revision. (Show Details)Mar 30 2020, 6:53 PM
lebedev.ri requested changes to this revision.EditedApr 4 2020, 7:20 AM

I don't like pattternmatching changes.
This should just add two new matchers: m_Freeze(<...>) and m_FreezeOrSelf(<...>),
where m_Freeze() matches when the instruction is freeze and the inner matcher matches on the freeze's operand,
and m_FreezeOrSelf(<...>) is m_CombineOr(m_Freeze(<...>), <...>).

This revision now requires changes to proceed.Apr 4 2020, 7:20 AM
aqjune updated this revision to Diff 255248.Apr 6 2020, 1:13 AM

Make matching patterns more explicit, by using m_FreezeOrSelf

aqjune marked 2 inline comments as done.Apr 6 2020, 1:16 AM
aqjune added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
646–648

Removed the true argument

lebedev.ri added inline comments.Apr 6 2020, 5:29 AM
llvm/lib/Analysis/InstructionSimplify.cpp
127

Hm, do we have precedent naming scheme for things like this?
Alternative variants could include considered IgnoreFreeze() or SkipFreeze().

646–648

Hmm yeah, i agree with @xbolva00 here, m_Specific(StripFreeze()) seems not ideal,
i'd suggest two more matchers - m_SpecificMaybeFrozen() and m_DeferredMaybeFrozen()
that would do that.

aqjune updated this revision to Diff 255382.Apr 6 2020, 9:37 AM
aqjune marked an inline comment as done.

Add & use m_SpecificMaybeFrozen / m_DeferredMaybeFrozen

aqjune marked 2 inline comments as done.Apr 6 2020, 9:38 AM
aqjune added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
127

I think Value::stripPointerCasts might be the one, though it starts with a small letter & it is at Value.

aqjune edited the summary of this revision. (Show Details)Apr 6 2020, 9:40 AM
aqjune abandoned this revision.Apr 27 2020, 2:55 AM

I checked whether applying this patch generates better assembly in SPEC or test-suite benchmarks with LTO enabled, and it didn't.
To make this not be seen as 'needs review', I'll abandon this patch.
If there is need to implement matchers in PatternMatch.h in the future, I'll use the reviewed code snippets from here.