This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] Extending G_SBFX/G_UBFX combine to consider G_SEXT_INREG as well
Needs RevisionPublic

Authored by DavidFerencSzabo on Jan 7 2023, 11:46 AM.

Details

Summary

Currently simple C code like

typedef struct { int a:1; int b:4; int c:3; } s_t;
int f(s_t s) { return s.b; }

will produce the following assembly with O3

lsl     w8, w0, #3
sxtb    w8, w8
sbfx    w0, w8, #4, #8
ret

while GCC produce this

sbfx    x0, x0, 1, 4
ret

Extended the implementation of matchBitfieldExtractFromShr to consider cases where the shifts fed to a G_SEXT_INREG.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJan 7 2023, 11:46 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
DavidFerencSzabo requested review of this revision.Jan 7 2023, 11:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 7 2023, 11:46 AM
foad requested changes to this revision.Jan 9 2023, 2:11 AM

This should be implemented by teaching computeNumSignBits about G_SBFX.

This revision now requires changes to proceed.Jan 9 2023, 2:11 AM
DavidFerencSzabo edited the summary of this revision. (Show Details)

This problem aroused in our down stream target, where G_SEXT_INREG(G_SBFX) were also produced, but for AArch64 it seems that not be the case, therefore removed the change in matchRedundantSExtInReg.

This should be implemented by teaching computeNumSignBits about G_SBFX.

Thanks for the feedback. Upon inspection it turned out that AArch64 does not require that part of the change, hence I removed it.

DavidFerencSzabo set the repository for this revision to rG LLVM Github Monorepo.Jan 12 2023, 6:08 AM
foad added a comment.Jan 12 2023, 6:13 AM

Please upload diffs with full context, either with git diff -U9999999 or by using the arcanist tool (arc diff).

DavidFerencSzabo edited the summary of this revision. (Show Details)

[GlobalISel] Extending G_SBFX/G_UBFX combine to consider G_SEXT_INREG as well

foad added a comment.Jan 12 2023, 6:30 AM

I wonder if a simpler approach would be just to combine the ashr (sextinreg x, 8), 4 into sextinreg (ashr x, 4), 4 (i.e. canonicalize ashr-of-sext into sext-of-ashr) and then let other combines simplify it further?

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
4399

Update the comment to say what else you are trying to match.

llvm/test/CodeGen/AArch64/GlobalISel/form-bitfield-extract-from-shr.mir
194

I don't think you need any of these COPYs of G_CONSTANTs.

211

This just seems wrong. You are combining (sextinreg ($w0 << 3), 8) >> 4 to ubfx $w0, 1, 4.

If $w0 was 0x000000FF then the original code gives 0x0FFFFFFF but the combined code gives 0x0000000F.

arsenm requested changes to this revision.Aug 17 2023, 3:13 PM

Would probably be simpler to combine shifts and ext_inreg, plus should try to use new tablegen combiners

This revision now requires changes to proceed.Aug 17 2023, 3:13 PM