This is an archive of the discontinued LLVM Phabricator instance.

[X86] Replace AND+IMM64 with SRL/SHL
ClosedPublic

Authored by n.bozhenov on Dec 31 2016, 6:55 AM.

Details

Summary

Emit SHRQ/SHLQ instead of ANDQ with a 64 bit constant mask if the result
is unused and the mask has only higher/lower bits set. For example, with
this patch LLVM emits

shrq $41, %rdi
je

instead of

movabsq $0xFFFFFE0000000000, %rcx
testq   %rcx, %rdi
je

This reduces number of instructions, code size and register pressure.
The transformation is applied only for cases where the mask cannot be
encoded as an immediate value within TESTQ instruction.

Diff Detail

Repository
rL LLVM

Event Timeline

n.bozhenov updated this revision to Diff 82765.Dec 31 2016, 6:55 AM
n.bozhenov retitled this revision from to [X86] Replace AND+IMM64 with SRL/SHL.
n.bozhenov updated this object.
jlebar removed a reviewer: jlebar.Dec 31 2016, 10:25 AM
jlebar added a subscriber: jlebar.
RKSimon added inline comments.
lib/Target/X86/X86ISelLowering.cpp
15796 ↗(On Diff #82765)

Bad style

const int ShiftToAndMaxMaskWidth = 32;
15883 ↗(On Diff #82765)
assert(isa<ConstantSDNode>(Op0) && "AND node isn't canonicalized");
15884 ↗(On Diff #82765)

Use auto with casts and dyn_casts

test/CodeGen/X86/cmp.ll
335 ↗(On Diff #82765)

Newline

n.bozhenov updated this revision to Diff 83939.Jan 11 2017, 2:54 AM
n.bozhenov marked 4 inline comments as done.
RKSimon edited edge metadata.Jan 11 2017, 10:27 AM

A few final minors

lib/Target/X86/X86ISelLowering.cpp
15892 ↗(On Diff #83939)

Pull out Mask.countTrailingZeros() to avoid duplication?

15896 ↗(On Diff #83939)

Use getScalarShiftAmountTy for the type

15901 ↗(On Diff #83939)

Pull out Mask.countLeadingZeros() to avoid duplication?

15905 ↗(On Diff #83939)

Use getScalarShiftAmountTy for the type

n.bozhenov edited edge metadata.
n.bozhenov marked 4 inline comments as done.
RKSimon accepted this revision.Jan 12 2017, 1:41 AM
RKSimon edited edge metadata.

LGTM - thanks.

This revision is now accepted and ready to land.Jan 12 2017, 1:41 AM
This revision was automatically updated to reflect the committed changes.