This is an archive of the discontinued LLVM Phabricator instance.

[CVP] Eliminate urem when LHS < RHS
ClosedPublic

Authored by caojoshua on Nov 19 2022, 11:31 AM.

Diff Detail

Event Timeline

caojoshua created this revision.Nov 19 2022, 11:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 19 2022, 11:31 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
caojoshua added a comment.EditedNov 19 2022, 11:42 AM

Most arithmetic optimizations that use ConstantRange data defer logic to
ConstantRange::binaryOp. ConstantRange::binaryOp can determine that
[0, 12) % [13, ...) -> [0, 12). If X is the LHS, the urem is just X, but
ConstantRange::binaryOp does not know that.

We can do this in either SCCP or CVP. I don't think we can do this in InstCombine or InstSimplify because they don't have ConstantRange data.

caojoshua edited the summary of this revision. (Show Details)Nov 19 2022, 11:48 AM
caojoshua added a reviewer: majnemer.

Check for vector type

caojoshua published this revision for review.Nov 19 2022, 12:12 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 19 2022, 12:12 PM
nikic added inline comments.Dec 8 2022, 6:51 AM
llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
773

Already checked in processUDivOrURem.

780

I'd suggest writing this as CR1.icmp(ICmpInst::ICMP_ULT, CR2).

790

Already checked in processUDivOrURem

caojoshua updated this revision to Diff 481554.Dec 9 2022, 1:22 AM

Change redundant ifs into asserts. Change constant range check to use CR1.icmp(icmp_ult, CR2).

nikic added inline comments.Dec 9 2022, 1:24 AM
llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
157

Rerun update_test_checks.py please. It looks like you either manually wrote these or edited them.

caojoshua updated this revision to Diff 481831.Dec 9 2022, 10:28 PM
caojoshua marked 3 inline comments as done.

update test with update_test_checks.py

This revision is now accepted and ready to land.Dec 10 2022, 12:43 AM
caojoshua marked an inline comment as done.Dec 10 2022, 10:53 AM

Can I get help merging this? I'm not a committer. I have the patch in https://github.com/caojoshua/llvm-project/commit/cac29ca43207f316e2b0904bee45d778d903a76e

This revision was landed with ongoing or failed builds.Dec 13 2022, 2:43 AM
Closed by commit rGcca01df29130: [CVP] Eliminate urem when LHS < RHS (authored by caojoshua, committed by nikic). · Explain Why
This revision was automatically updated to reflect the committed changes.