This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Fold (sra (add (shl X, 32), C1), 32 - C) -> (shl (sext_inreg (add X, C1), C)
ClosedPublic

Authored by craig.topper on Jun 29 2022, 5:31 PM.

Details

Summary

Similar for a subtract with a constant left hand side.

(sra (add (shl X, 32), C1<<32), 32) is the canonical IR from InstCombine
for (sext (add (trunc X to i32), 32) to i32).

For RISCV, we should lower this as addiw which means turning it into
(sext_inreg (add X, C1)).

There is an existing DAG combine to convert back to (sext (add (trunc X
to i32), 32) to i32), but it requires isTruncateFree to return true
and for i32 to be a legal type as it used sign_extend and truncate
nodes. So that doesn't work for RISCV.

If the outer sra happens be used by a shl by constant, it will be
folded and the shift amount of the sra will be changed before we
can do our own DAG combine. This requires us to match the more
general pattern and restore the shl.

I had wanted to do this as a separate (add (shl X, 32), C1<<32) ->
(shl (add X, C1), 32) combine, but that hit an infinite loop for some
values of C1.

Diff Detail

Event Timeline

craig.topper created this revision.Jun 29 2022, 5:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2022, 5:31 PM
craig.topper requested review of this revision.Jun 29 2022, 5:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2022, 5:31 PM
asb accepted this revision.EditedJun 30 2022, 7:44 AM

Really nice improvement - LGTM! (though I think the new tests aren't pre-committed yet or uploaded directly to phab)

This revision is now accepted and ready to land.Jun 30 2022, 7:44 AM