This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] Combine things like (z = x <= 0 ? z = x : z = 0) -> x & (x >> bw-1)
AcceptedPublic

Authored by paquette on Oct 10 2022, 6:25 PM.

Details

Reviewers
aemerson
arsenm
Summary

If we have a signed comparison like this:

%cmp = icmp sge i32 %x, 0
%select = select i1 %cmp, i32 %x, i32 0
ret i32 %select

We can represent it using an and + a right shift.

On AArch64, this allows us to do the cmp + select in a single instruction.

This also works with signed less-than operators, but instead we compliment the
shift first.

Saves 0.12% code size on CTMark/lencod @ -Os for AArch64. Other minor code size
savings as well with no regressions.

https://godbolt.org/z/7E4vYWah6

Diff Detail

Event Timeline

paquette created this revision.Oct 10 2022, 6:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 10 2022, 6:25 PM
paquette requested review of this revision.Oct 10 2022, 6:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 10 2022, 6:25 PM
Herald added a subscriber: wdng. · View Herald Transcript
arsenm added inline comments.Oct 10 2022, 6:59 PM
llvm/include/llvm/CodeGen/TargetLowering.h
724–731 ↗(On Diff #466676)

Wouldn't the target just not add this combine if it didn't want it?

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
6087–6090

These assignments on the right aren't part of the pattern and confusing

6101

I guess this is complicated by this sub-variant, which would probably be uglier to split into a separate combine

llvm/test/CodeGen/AArch64/combine-select-to-and-shift.mir
264

Vector case? Even if it's not directly legal I would expect this to combine to the vector for later scalarization if needed

HsiangKai added inline comments.
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
6106

You can use the captured NeedsNot here.

paquette updated this revision to Diff 466871.Oct 11 2022, 11:15 AM
  • simplify comment
  • use captured NeedsNot
  • support vector case + add test
aemerson added inline comments.Oct 12 2022, 9:22 PM
llvm/include/llvm/CodeGen/TargetLowering.h
724–731 ↗(On Diff #466676)

@paquette Echoing Matt's question: is this hook actually needed?

paquette updated this revision to Diff 467598.Oct 13 2022, 1:58 PM

Split into two combines to avoid the hook. The "and-not" combine is now opt-in by the target.

Split into two combines to avoid the hook. The "and-not" combine is now opt-in by the target.

If someone needed different behavior per type, it would still need a hook (or some other mechanism to parameterize combines by type)

Split into two combines to avoid the hook. The "and-not" combine is now opt-in by the target.

If someone needed different behavior per type, it would still need a hook (or some other mechanism to parameterize combines by type)

Should I add the hook back in then?

llvm/include/llvm/CodeGen/TargetLowering.h
724–731 ↗(On Diff #466676)

yeah I can drop it

Split into two combines to avoid the hook. The "and-not" combine is now opt-in by the target.

If someone needed different behavior per type, it would still need a hook (or some other mechanism to parameterize combines by type)

Should I add the hook back in then?

No, not without a real reason to. But some general type control would be helpful

arsenm accepted this revision.Nov 11 2022, 7:59 AM

LGTM

This revision is now accepted and ready to land.Nov 11 2022, 7:59 AM

reverse ping