This is an archive of the discontinued LLVM Phabricator instance.

[Peephole] rewrite INSERT_SUBREG to SUBREG_TO_REG if upper bits zero
ClosedPublic

Authored by Allen on Aug 30 2022, 6:37 AM.

Details

Summary

Restrict the 32-bit form of an instruction of integer as too many test cases
will be clobber as the register number updated.

From %reg = INSERT_SUBREG %reg, %subreg, subidx
To   %reg:subidx =  SUBREG_TO_REG 0, %subreg, subidx

Try to prefix the redundant mov instruction at D132325 as the SUBREG_TO_REG should not generate code.

Diff Detail

Event Timeline

Allen created this revision.Aug 30 2022, 6:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2022, 6:37 AM
Allen requested review of this revision.Aug 30 2022, 6:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2022, 6:37 AM
foad requested changes to this revision.Aug 30 2022, 7:27 AM

Nack. Whatever you are trying to do, I think this is the wrong way to do it.

rewrite INSERT_SUBREG to SUBREG_TO_REG if upper bits zero

Where do you actually check that the upper bits are zero?

llvm/lib/CodeGen/TwoAddressInstructionPass.cpp
1829–1831 ↗(On Diff #456647)

This might be true for some instructions on some targets, but it is not true for Machine IR in general.

1840–1841 ↗(On Diff #456647)

I don't think it's acceptable to restrict this to specific types.

This revision now requires changes to proceed.Aug 30 2022, 7:27 AM
Allen added a comment.Aug 30 2022, 8:06 AM

Where do you actually check that the upper bits are zero?

hi @foad , thanks for you attention, maybe I have some misunderstand. I try this refer to AArch64MIPeepholeOpt::visitORR in file AArch64MIPeepholeOpt.cpp.

Allen abandoned this revision.Aug 30 2022, 8:22 AM

This transform should be in target-specific code so we can ensure the safety. (Or it could use a target hook if it really needs to be in TwoAddressInstructionPass for some reason.)

Allen updated this revision to Diff 457780.Sep 3 2022, 4:11 AM
Allen retitled this revision from [TwoAddressInstruction] rewrite INSERT_SUBREG to SUBREG_TO_REG if upper bits zero to [Peephole] rewrite INSERT_SUBREG to SUBREG_TO_REG if upper bits zero.
Allen edited the summary of this revision. (Show Details)

move this transform in target-specific code to ensure the safety

Allen added a comment.Sep 3 2022, 5:28 PM

This transform should be in target-specific code so we can ensure the safety. (Or it could use a target hook if it really needs to be in TwoAddressInstructionPass for some reason.)

Thanks @efriedma very much for your comment. And I move this transform in the peephole (target-specific code).

Allen removed a reviewer: foad.Sep 6 2022, 8:32 AM
Allen added a comment.Sep 6 2022, 8:54 AM

ping. Any further concerns? Thanks.

efriedma added inline comments.Sep 6 2022, 4:26 PM
llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
261

If I'm understanding correctly, we're assuming the first operand to INSERT_SUBREG is irrelevant because a COPY would destroy the upper part of the register anyway? Maybe makes sense to explicitly note that.

281

Checking "!SrcMI" twice.

282

TargetOpcode::COPY <= TargetOpcode::GENERIC_OP_END should be true.

284

What are you trying to accomplish with these isTypeLegalForClass checks? You're looking for a register class that only contains integer registers? Can you just check that the register class is a subclass of GPR64all?

Allen updated this revision to Diff 458363.Sep 6 2022, 11:09 PM

Address the comment

llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
261

Ok, I add your comment., thanks

281

Thanks, delete the duplicate checking "!SrcMI"

282

Thanks, apply your comment

284

I add the isTypeLegalForClass checks because this changes will cause many test cases to be updated, especially some vector type register class.

def FPR64 : RegisterClass<"AArch64", [f64, i64, v2f32, v1f64, v8i8, v4i16, v2i32,
                                      v1i64, v4f16, v4bf16],
                                     64, (sequence "D%u", 0, 31)>;

Now I updated with AArch64::GPR64allRegClass.hasSubClassEq(RC) as your comment to check integer registers, thanks!

Allen edited the summary of this revision. (Show Details)Sep 7 2022, 6:15 PM
This revision is now accepted and ready to land.Sep 8 2022, 10:39 AM
This revision was landed with ongoing or failed builds.Sep 8 2022, 6:03 PM
This revision was automatically updated to reflect the committed changes.
MaskRay added a subscriber: MaskRay.Sep 8 2022, 6:28 PM
MaskRay added inline comments.
llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
295

SubregMI caused a -Wunused-variable in -DLLVM_ENABLE_ASSERTIONS=off builds. I fixed it

Allen added inline comments.Sep 8 2022, 6:46 PM
llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
295

oh, yes, thanks vey much, I see your commit f9b59249757