This is an archive of the discontinued LLVM Phabricator instance.

[SCCP] Propagate equality of a not-constant
ClosedPublic

Authored by StephenFan on Dec 4 2022, 8:27 PM.

Details

Summary

The equality state of a not-constant can be used to do constant
propagation. For example,

define i32 @equal_not_constant(ptr noundef %p, ptr noundef %q) {
entry:
  %cmp = icmp ne ptr %p, null
  br i1 %cmp, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  %cmp.then = icmp eq ptr %p, %q
  br i1 %cmp.then, label %if.then1, label %if.end

if.then1:                                         ; preds = %if.then
  %cmp.then1 = icmp ne ptr %q, null
  call void @use(i1 %cmp.then1)
  br label %if.end

if.end:
  ret i32 0
}

In this case, we can fold %cmp.then1 as true.

Diff Detail

Event Timeline

StephenFan created this revision.Dec 4 2022, 8:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 4 2022, 8:27 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
StephenFan requested review of this revision.Dec 4 2022, 8:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 4 2022, 8:27 PM
nikic requested changes to this revision.Dec 5 2022, 2:49 AM
nikic added inline comments.
llvm/lib/Transforms/Utils/SCCPSolver.cpp
1360

Shouldn't this be getNotConstant()? Probably missing test coverage, I'd expect this to assert.

This revision now requires changes to proceed.Dec 5 2022, 2:49 AM
nikic added inline comments.Dec 5 2022, 2:52 AM
llvm/lib/Transforms/Utils/SCCPSolver.cpp
1360

Actually, isn't this case entirely incorrect? If A != B and B != C, then that does not imply that A == C, right?

StephenFan updated this revision to Diff 480037.Dec 5 2022, 3:19 AM

Don't propagate the inequality of not-constant.

llvm/lib/Transforms/Utils/SCCPSolver.cpp
1360

Yes, you are right. Thanks!

nikic added inline comments.Dec 5 2022, 3:23 AM
llvm/lib/Transforms/Utils/SCCPSolver.cpp
1360

Could you please still add a test for this, to make sure the optimization does not happen?

StephenFan updated this revision to Diff 480038.Dec 5 2022, 3:25 AM
StephenFan retitled this revision from [SCCP] Propagate equality or inequality state of a not-constant to [SCCP] Propagate equality of a not-constant.
StephenFan edited the summary of this revision. (Show Details)

Update title.

StephenFan updated this revision to Diff 480039.Dec 5 2022, 3:29 AM

Add not_equal_not_constant test case.

llvm/lib/Transforms/Utils/SCCPSolver.cpp
1360

Done.

nikic accepted this revision.Dec 5 2022, 3:34 AM

LGTM

This revision is now accepted and ready to land.Dec 5 2022, 3:34 AM
This revision was landed with ongoing or failed builds.Dec 5 2022, 3:54 AM
This revision was automatically updated to reflect the committed changes.