This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Optimize (seteq (i64 (and X, 0xffffffff)), C1)
ClosedPublic

Authored by craig.topper on Jul 17 2022, 8:20 PM.

Details

Summary

(and X, 0xffffffff) requires 2 shifts in the base ISA. Since we
know the result is being used by a compare, we can use a sext_inreg
instead of an AND if we also modify C1 to have 33 sign bits instead
of 32 leading zeros. This can also improve the generated code for
materializing C1.

Diff Detail

Event Timeline

craig.topper created this revision.Jul 17 2022, 8:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 17 2022, 8:20 PM
craig.topper requested review of this revision.Jul 17 2022, 8:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 17 2022, 8:20 PM
craig.topper edited the summary of this revision. (Show Details)Jul 17 2022, 8:21 PM
craig.topper added inline comments.Jul 17 2022, 8:22 PM
llvm/test/CodeGen/RISCV/i64-icmp.ll
693

I think we can use addiw here to avoid the sext.w?

708

I think we can use subw instead of xor to avoid the sext.w

733

I think we can use addiw to avoid the sext.w

reames accepted this revision.Jul 18 2022, 8:59 AM

LGTM

Correct me if I'm wrong, but this should improve the idiomatic icmp eq i32 patterns for ELEN=64 right? If so, have you thought about analogous cases for 16 and 8 bits w/Zb*? On the surface, using a sext.h/b over a zext.h/b wouldn't seem to matter, but the constant simplifications are potentially interesting. Should we be canonicalizing towards sext anyways?

This revision is now accepted and ready to land.Jul 18 2022, 8:59 AM

LGTM

Correct me if I'm wrong, but this should improve the idiomatic icmp eq i32 patterns for ELEN=64 right? If so, have you thought about analogous cases for 16 and 8 bits w/Zb*? On the surface, using a sext.h/b over a zext.h/b wouldn't seem to matter, but the constant simplifications are potentially interesting. Should we be canonicalizing towards sext anyways?

I don't think I understand this question. ELEN=64 is a vector term, but we're talking about scalar instructions here. Unless you meant XLEN=64?

LGTM

Correct me if I'm wrong, but this should improve the idiomatic icmp eq i32 patterns for ELEN=64 right? If so, have you thought about analogous cases for 16 and 8 bits w/Zb*? On the surface, using a sext.h/b over a zext.h/b wouldn't seem to matter, but the constant simplifications are potentially interesting. Should we be canonicalizing towards sext anyways?

I don't think I understand this question. ELEN=64 is a vector term, but we're talking about scalar instructions here. Unless you meant XLEN=64?

Oops, yes.

LGTM

Correct me if I'm wrong, but this should improve the idiomatic icmp eq i32 patterns for ELEN=64 right? If so, have you thought about analogous cases for 16 and 8 bits w/Zb*? On the surface, using a sext.h/b over a zext.h/b wouldn't seem to matter, but the constant simplifications are potentially interesting. Should we be canonicalizing towards sext anyways?

I don't think I understand this question. ELEN=64 is a vector term, but we're talking about scalar instructions here. Unless you meant XLEN=64?

Oops, yes.

SelectionDAG type legalization is biased to prefer sign extend for all i32 compares via the isSExtCheaperThanZExt check in DAGTypeLegalizer::PromoteSetCCOperands. The cases I've seen this patch affect seem to come from the middle end with i64 type.

There is a generic fold to convert sext_in_reg to AND for equality compares(the opposite of this patch) in TargetLowering::SimplifySetCC. It is disabled for RISCV-V by isSExtCheaperThanZExt.

I think you're right there might be some be some interesting constant simplifications. I'm not sure it's restricted to Zb either. i16 constants in the range [63488, 65535] could be sign extended to enable the use of li instead of lui+addi. All i8 constants for this should fit in li already, but I think sign extending could enable c.li. Not sure if it makes sense to always use sext for i8 and i16 type legalization via isSExtCheaperThanZExt.

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