This is an archive of the discontinued LLVM Phabricator instance.

Add an instcombine rule to optimize max(~a,~b) to ~min(a, b) when profitable
ClosedPublic

Authored by sanjoy on Feb 22 2015, 7:51 PM.

Details

Summary

This case is interesting because ScalarEvolutionExpander lowers min(a, b) as ~max(~a,~b). I think the profitability heuristics can be made more clever/aggressive, but this is a start.

A clang bootstrapped with this change passes make check. The rule fires 159 times.

Diff Detail

Repository
rL LLVM

Event Timeline

sanjoy updated this revision to Diff 20488.Feb 22 2015, 7:51 PM
sanjoy retitled this revision from to Add an instcombine rule to optimize max(~a,~b) to ~min(a, b) when profitable.
sanjoy updated this object.
sanjoy edited the test plan for this revision. (Show Details)
sanjoy added reviewers: majnemer, hfinkel.
sanjoy added a subscriber: Unknown Object (MLST).
majnemer added inline comments.Feb 22 2015, 10:51 PM
lib/Transforms/InstCombine/InstCombineSelect.cpp
1169–1172 ↗(On Diff #20488)

Can we improve isFreeToInvert and just use that instead?

1183–1184 ↗(On Diff #20488)

This doesn't look formatted correctly, can you please run clang-format on this?

filcab added a subscriber: filcab.Feb 22 2015, 11:05 PM
filcab added inline comments.
lib/Transforms/InstCombine/InstCombineSelect.cpp
1182 ↗(On Diff #20488)

We do this test here, but the calculation for Profitable only cares about Not.
Do we really need this (or should we just check for this in the Porfitable calculation too)?

sanjoy added inline comments.Feb 22 2015, 11:17 PM
lib/Transforms/InstCombine/InstCombineSelect.cpp
1182 ↗(On Diff #20488)

The Profitable check is intentionally more restrictive. You don't gain or lose anything if you compute (-1-Const) - A instead of Const + A, so there is no penalty. But the profitable check is specifically about figuring out if computing ~V instead of V *gains* you anything -- if V was xor P, -1 then you can now get away without actually computing the xor. The profitability check has to be stronger than a break-even since this rule introduces a not and that extra work has to be justified.

sanjoy updated this revision to Diff 20492.Feb 23 2015, 12:16 AM

Address review comments.

majnemer added inline comments.Feb 23 2015, 12:29 AM
lib/Transforms/InstCombine/InstCombineInternal.h
85 ↗(On Diff #20492)

I think you are missing a "to" between "intends" and "remove".

88 ↗(On Diff #20492)

Is WillInvertAllUses ever different from V->hasOneUse()? If not, I think it would be nicer to have this function not take the extra argument and just compute the predicate locally.

sanjoy added inline comments.Feb 23 2015, 12:45 AM
lib/Transforms/InstCombine/InstCombineInternal.h
85 ↗(On Diff #20492)

Yup, will fix.

88 ↗(On Diff #20492)

The new uses of IsFreeToInvert (in InstCombineSelect) have WillInvertAllUses as V->hasNUses(2).

majnemer accepted this revision.Feb 23 2015, 2:36 PM
majnemer edited edge metadata.

LGTM

This revision is now accepted and ready to land.Feb 23 2015, 2:36 PM
This revision was automatically updated to reflect the committed changes.