This is an archive of the discontinued LLVM Phabricator instance.

[X86] Add custom isel to select ADD/SUB/OR/XOR/AND to their non-immediate forms under optsize when the immediate has additional users.
ClosedPublic

Authored by craig.topper on Mar 27 2019, 4:31 PM.

Details

Summary

We attempt to prevent folding immediates with multiple users under optsize. But we only do this from store nodes and X86ISD::ADD/SUB/XOR/OR/AND patterns. We don't do it for ISD::ADD/SUB/XOR/OR/AND even though we count them as users when deciding whether to fold into other nodes. This leads to situations where we block folding to a compare for example, but still fold into an AND or OR as seen in PR27202.

Unfortunately touching the isel patterns in tablegen for the ISD::ADD/SUB/XOR/OR/AND opcodes will cause the patterns to be unusable for fast isel. And we don't have a way to make a fast isel only pattern.

To workaround this, this patch adds custom isel in front of the isel table that will select the non-immediate forms if the immediate has additional users. This may create some issues for ANDN and NOT matching. And there's room for improvement with unsigned 32 immediates on 64-bit AND.

This patch needs more thorough test cases, but I wanted to get feedback on the direction. Please send me any other test cases you've seen in the wild.

I think we probably have the same issue with the immediate matching when we fold RMW from X86ISD::ADD/SUB/XOR/OR/AND. And our TEST immedaite shrinking logic. Our cost modeling for immediates that can fit in a sign extended 8-bit immediate on a 16/32/64 bit operation is completely wrong.

I also wonder if we should update the ConstantHoisting cost model and block folding for "opaque" constants. But of course constants can still be created by DAG combine and lowering optimizations.

Fixes PR27202

Diff Detail

Repository
rL LLVM

Event Timeline

craig.topper created this revision.Mar 27 2019, 4:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 27 2019, 4:31 PM

@spatel @andreadb Any comments?

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
3347 ↗(On Diff #192537)

auto

3571 ↗(On Diff #192537)

auto

@spatel @andreadb Any comments?

I don't know of any better way to accomplish this, so no objection to the direction. Agree that we should have more tests. Can the addition of tryShrinkShlLogicImm() be an NFC preliminary step?

Rebase after pre-commiting the helper function X86ISelDAGToDAG and adding PR41151 test cases.

RKSimon accepted this revision.Jun 26 2019, 8:34 AM

I've chatted with @andreadb and other than taking the time and effort to create a separate pass to perform this generally we can't think of another way to do this. We do seem to be putting more and more strain on isel though....

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
4271 ↗(On Diff #205252)

auto *Cst

This revision is now accepted and ready to land.Jun 26 2019, 8:34 AM
This revision was automatically updated to reflect the committed changes.