This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] Combine G_SHL, G_ASHR, G_SHL of undef shifts to undef.
ClosedPublic

Authored by aemerson on May 5 2022, 1:58 PM.

Diff Detail

Event Timeline

aemerson created this revision.May 5 2022, 1:58 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 5 2022, 1:58 PM
Herald added a subscriber: rovka. · View Herald Transcript
aemerson requested review of this revision.May 5 2022, 1:58 PM
aemerson retitled this revision from [GlobalISel] Combine G_SHL, G_ASHR, G_SHL of undef shifts undef. to [GlobalISel] Combine G_SHL, G_ASHR, G_SHL of undef shifts to undef..May 5 2022, 1:58 PM
arsenm added inline comments.May 5 2022, 1:59 PM
llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-undef.mir
59–60

Should pre-regenerate these checks to include -NEXT

281

Should also test undef lhs?

aemerson updated this revision to Diff 427485.May 5 2022, 4:12 PM

Actually G_SHL with undef LHS needs to be optimized to zero, not undef.

foad added a comment.May 6 2022, 3:51 AM

Actually G_SHL with undef LHS needs to be optimized to zero, not undef.

I don't understand why that would be true for SHL but not for the other shifts. What are the rules for undef in MIR? Is it like undef in IR, or like poison in IR, or neither?

llvm/include/llvm/Target/GlobalISel/Combine.td
238

I like 'rhs' but all the existing combine names seem to use 'left' and 'right'.

Actually G_SHL with undef LHS needs to be optimized to zero, not undef.

I don't understand why that would be true for SHL but not for the other shifts. What are the rules for undef in MIR? Is it like undef in IR, or like poison in IR, or neither?

I'm not exactly sure of the reasoning, it's just what the IR optimizations and SelectionDAG do. SelectionDAG optimizes the other shifts of undef to undef too.

aemerson updated this revision to Diff 427733.May 6 2022, 1:55 PM
foad added a comment.May 9 2022, 2:02 AM

Actually G_SHL with undef LHS needs to be optimized to zero, not undef.

At the IR level, InstSimplify does this for all shifts (in the absence of exact/nsw/nuw flags). See SimplifyRightShift and SimplifyShlInst.

Actually G_SHL with undef LHS needs to be optimized to zero, not undef.

At the IR level, InstSimplify does this for all shifts (in the absence of exact/nsw/nuw flags). See SimplifyRightShift and SimplifyShlInst.

Ok I see. To be more conservative then I'll just fold to undef for the shifts only.

aemerson updated this revision to Diff 428141.May 9 2022, 11:29 AM

Only fold undef shifts, not LHS.

foad accepted this revision.May 13 2022, 4:12 AM

LGTM. (As I understand it the rationale is that if the rhs is undef, you can assume it is a value >= the bitwidth, so the result is undef.)

This revision is now accepted and ready to land.May 13 2022, 4:12 AM
This revision was landed with ongoing or failed builds.May 13 2022, 12:20 PM
This revision was automatically updated to reflect the committed changes.