This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Fixes ADD/SUB opt bug and abstracts shared behavior in MIPeepholeOpt for ADD, SUB, and AND.
ClosedPublic

Authored by red1bluelost on Jan 23 2022, 11:23 AM.

Details

Summary

This fixes a bug where (SUBREG_TO_REG 0 (MOVi32imm <negative-number>) sub_32)
would generate invalid code since the top 32-bits were not zeroed when inspecting the
immediate value. A new test was added for this case.

Change to abstract shared behavior in MIPeepholeOpt. Both
visitAND and visitADDSUB attempt to split an RR instruction with an immediate
operand into two RI instructions with the immediate split.

The differing behavior lies in how the immediate is split into two pieces and
how the new instructions are built. The rest of the behavior (adding new VRegs,
checking for the MOVImm, constraining reg classes, removing old intructions)
are shared between the operations.

The new helper function splitTwoPartImm implements the shared behavior and
delegates differing behavior to two function objects passed by the caller.
One function object splits the immediate into two values and returns the
opcode to use if it is a valid split. The other function object builds
the new instructions.

I felt this abstraction would help since I believe it will help reduce the
code repetition when adding new instructions of the pattern, such as
SUBS for this conditional optimization.

Tested it locally by running check all with compiler-rt, mlir, clang-tools-extra,
flang, llvm, and clang enabled.

Diff Detail

Event Timeline

red1bluelost created this revision.Jan 23 2022, 11:23 AM
red1bluelost requested review of this revision.Jan 23 2022, 11:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 23 2022, 11:23 AM
dmgreen added inline comments.Jan 25 2022, 1:37 AM
llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
354–355

Do we have test case for this case with an ADD?

Adds test case for edge case that happens when a MOVi32imm <negative-number> into SUBREG_TO_REG occurs.

red1bluelost added inline comments.Jan 25 2022, 1:27 PM
llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
354–355

Turns out this was a very subtle but that I didn't catch in D117429. See example here: https://llvm.godbolt.org/z/Gnq1x8rh1

I added a test for this. We definitely want to have the Imm &= 0xFFFF'FFFF since assigning to T Imm will sign extend the value, not zero extend.

I guess in this case the revision is no longer NFC.

red1bluelost retitled this revision from [AArch64][NFC] Abstracts shared behavior in MIPeepholeOpt for ADD, SUB, and AND. to [AArch64] Fixes ADD/SUB opt bug and abstracts shared behavior in MIPeepholeOpt for ADD, SUB, and AND..Jan 25 2022, 1:29 PM
red1bluelost edited the summary of this revision. (Show Details)
dmgreen accepted this revision.Jan 25 2022, 2:00 PM

OK nice. I see. LGTM then

This revision is now accepted and ready to land.Jan 25 2022, 2:00 PM

Could you commit on my behalf when you have the chance. Same info: Micah Weston - micahsweston@gmail.com
Thanks!

This revision was landed with ongoing or failed builds.Jan 25 2022, 8:22 PM
This revision was automatically updated to reflect the committed changes.