This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add a RISCV specific CodeGenPrepare pass.
ClosedPublic

Authored by craig.topper on Jul 13 2022, 9:44 PM.

Details

Summary

Initial optimization is to convert (i64 (zext (i32 X))) to
(i64 (sext (i32 X))) if the dominating condition for the basic block
guaranteed the sign bit of X is zero.

This frequently occurs in loop preheaders where a signed induction
variable that can never be negative has been widened. There will be
a dominating check that the 32-bit trip count isn't negative or zero.
The check here is not restricted to that specific case though.

A i32->i64 sext is cheaper than zext on RV64 without the Zba
extension. Later optimizations can often remove the sext from the
preheader basic block because the dominating block also needs a sext to
evaluate the greater than 0 check.

Diff Detail

Event Timeline

craig.topper created this revision.Jul 13 2022, 9:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 13 2022, 9:44 PM
craig.topper requested review of this revision.Jul 13 2022, 9:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 13 2022, 9:44 PM
asb accepted this revision.Jul 14 2022, 1:36 AM

See my very minor comments inline, but this LGTM.

llvm/lib/Target/RISCV/RISCVCodeGenPrepare.cpp
29

Perhaps worth defining and updating a statistic. e.g. STATISTIC(NumZExtToSExt, "Number of SExt instructions converted to ZExt")

69

We already have the DL as a member of RISCVCodeGenPrepare, so just use *DL as an arg to isImpliedByDomCondition instead?

llvm/test/CodeGen/RISCV/riscv-codegenprepare.ll
5

Trivial nit: slightly better to move this to -mtriple=riscv64 in the RUN line like we do for other tests.

This revision is now accepted and ready to land.Jul 14 2022, 1:36 AM

Address review comments

This revision was landed with ongoing or failed builds.Jul 14 2022, 10:21 AM
This revision was automatically updated to reflect the committed changes.