This is an archive of the discontinued LLVM Phabricator instance.

[X86]Remove TEST in AND32ri+TEST16rr in peephole-opt
ClosedPublic

Authored by XinWang10 on Jun 30 2023, 2:15 AM.

Details

Summary

Previously we remove a pattern like:

%reg = and32ri %in_reg, 5
...                         // EFLAGS not changed.
%src_reg = subreg_to_reg 0, %reg, %subreg.sub_index
test64rr %src_reg, %src_reg, implicit-def $eflags

We can remove test64rr since it has same functionality as and subreg_to_reg avoid the opt in previous code, so we handle this case specially.
And this case is also can be opted for the same reason, like:

%reg = and32ri %in_reg, 5
...                         // EFLAGS not changed.
%src_reg = copy %reg.sub_16bit:gr32
test16rr %src_reg, %src_reg, implicit-def $eflags

The COPY from gr32 to gr16 prevent the opt in previous code too, just handle it specially as what we did for test64rr.

Diff Detail

Event Timeline

XinWang10 created this revision.Jun 30 2023, 2:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 30 2023, 2:15 AM
XinWang10 requested review of this revision.Jun 30 2023, 2:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 30 2023, 2:15 AM
XinWang10 edited the summary of this revision. (Show Details)Jun 30 2023, 2:16 AM
XinWang10 edited the summary of this revision. (Show Details)
pengfei added inline comments.Jun 30 2023, 8:07 AM
llvm/lib/Target/X86/X86InstrInfo.cpp
985–986

Can they be merged to one line?

llvm/test/CodeGen/X86/peephole-test-after-add.mir
41

Maybe pre-commit the test?

If the AND immediate is more than 16 bits, I don't think this transform is valid. Do we have a test for that?

XinWang10 added a comment.EditedJul 2 2023, 6:24 PM

If the AND immediate is more than 16 bits, I don't think this transform is valid. Do we have a test for that?

Good point! Will update.

llvm/lib/Target/X86/X86InstrInfo.cpp
985–986

Then the merged line would exceed 80 chars.

llvm/test/CodeGen/X86/peephole-test-after-add.mir
41

Will do, to make the change clear.

pengfei added inline comments.Jul 2 2023, 7:06 PM
llvm/lib/Target/X86/X86InstrInfo.cpp
985–986

Then you can put the exceeded part in the next line.

XinWang10 updated this revision to Diff 536647.Jul 2 2023, 7:49 PM
  • Make sure instr is andri and limit imm for it
XinWang10 retitled this revision from [X86]Remove TEST in AND32+TEST16rr in peephole-opt to [X86]Remove TEST in AND32ri+TEST16rr in peephole-opt.Jul 2 2023, 8:06 PM
XinWang10 marked an inline comment as done.Jul 2 2023, 10:57 PM
XinWang10 added inline comments.
llvm/test/CodeGen/X86/peephole-test-after-add.mir
41
craig.topper added inline comments.Jul 3 2023, 9:51 AM
llvm/lib/Target/X86/X86InstrInfo.cpp
1007

Doesn't it need to be isUint<16>?

Bit 15 would also need to be 0 if the sign flag is being used.

XinWang10 updated this revision to Diff 536934.Jul 3 2023, 7:29 PM
XinWang10 marked an inline comment as done.

use isUint instead

llvm/lib/Target/X86/X86InstrInfo.cpp
1007

Yes, It should be.

skan added a comment.Jul 3 2023, 7:55 PM

I think you update the revision incorrectly...

XinWang10 updated this revision to Diff 536941.Jul 3 2023, 8:23 PM

update revision

skan added inline comments.Jul 3 2023, 8:32 PM
llvm/lib/Target/X86/X86InstrInfo.cpp
1000

Should this be an assert?

XinWang10 added inline comments.Jul 3 2023, 10:07 PM
llvm/lib/Target/X86/X86InstrInfo.cpp
1000

No, VregDefInstr could be nullptr if CmpValDefInstr is just a copy from reg, and not a copy sub16bit from an AND32ri.

XinWang10 updated this revision to Diff 536953.Jul 3 2023, 10:47 PM
  • update test
XinWang10 updated this revision to Diff 536965.Jul 3 2023, 11:49 PM
  • update bigimm test because its imm is not 32bit
skan added inline comments.Jul 4 2023, 12:11 AM
llvm/test/CodeGen/X86/peephole-test-after-add.mir
57

Why change this?

XinWang10 added inline comments.Jul 4 2023, 12:24 AM
llvm/test/CodeGen/X86/peephole-test-after-add.mir
57

The original intention is to test andri whose imm is not fit in 16bit, but the mir convert the 123456 to 57920, it offend the intention.

skan added inline comments.Jul 4 2023, 1:40 AM
llvm/test/CodeGen/X86/peephole-test-after-add.mir
57

Then remove %2, or reuse %2

XinWang10 updated this revision to Diff 536987.Jul 4 2023, 1:40 AM
  • rm redundant var %store in bigimm test
skan added inline comments.Jul 4 2023, 2:40 AM
llvm/lib/Target/X86/X86InstrInfo.cpp
998

Only one check is needed

1011

Only one check is needed

XinWang10 updated this revision to Diff 537079.Jul 4 2023, 6:23 AM
  • less the condition
craig.topper added inline comments.Jul 4 2023, 1:24 PM
llvm/lib/Target/X86/X86InstrInfo.cpp
1007

Do we have a test where we use the sign flag? The AND would set the sign flag based on bit 31. the TEST16rr would set the sign flag based on bit 15. So we could only do the transform if bit 31 and bit 15 are known to be zero (bit 31 is handled by the isUint<16> check. Or we need to not be using the sign flag from the test.

XinWang10 added inline comments.Jul 4 2023, 6:31 PM
llvm/lib/Target/X86/X86InstrInfo.cpp
1007

I would add a test for it. It will be the same as the original logic for test64rr since the following code, if user use sf we will disable the removal.

craig.topper added inline comments.Jul 4 2023, 6:35 PM
llvm/lib/Target/X86/X86InstrInfo.cpp
1007

Ok I missed the NoSignFlag=true in the original code.

XinWang10 updated this revision to Diff 537195.Jul 4 2023, 6:50 PM
  • add sign flag test for it
XinWang10 marked 7 inline comments as done.Jul 4 2023, 6:51 PM
XinWang10 updated this revision to Diff 537196.Jul 4 2023, 6:53 PM
  • restore unneed change
skan accepted this revision.Jul 5 2023, 10:34 PM

LGTM

This revision is now accepted and ready to land.Jul 5 2023, 10:34 PM
This revision was automatically updated to reflect the committed changes.

This causes the crash in stage2 x86 clang. Could you investigate or revert?

XinWang10 added a comment.EditedJul 10 2023, 12:18 AM

This causes the crash in stage2 x86 clang. Could you investigate or revert?

OK, I'd revert it first. Done in 284a059b33064764d5ebb1699b5ac8f6f5dad6e8, will invest it later.

The following crashes with the patch

llc -march=x86-64 -o /dev/null reduced.ll

The following crashes with the patch

llc -march=x86-64 -o /dev/null reduced.ll

Thanks a lot!

The following crashes with the patch

llc -march=x86-64 -o /dev/null reduced.ll

Thanks a lot!

Note: I found this with llvm-stress. I don't know if it's related to the build bot failure mentioned earlier here at all.

luporl added a subscriber: luporl.Jul 10 2023, 6:32 AM