This is an archive of the discontinued LLVM Phabricator instance.

[SCCP] Don't use markForcedConstant on branch conditions.
ClosedPublic

Authored by efriedma on Jul 16 2018, 5:10 PM.

Details

Summary

It's more aggressive than we need to be, and leads to strange workarounds in other places like call return value inference. Instead, just directly mark an edge viable.

Tests by Florian Hahn.

Diff Detail

Repository
rL LLVM

Event Timeline

efriedma created this revision.Jul 16 2018, 5:10 PM
fhahn added a comment.Jul 18 2018, 8:03 AM

I just collected stats with this patch on the test-suite with LTO. The only difference in stats counters across IPSCCP and SCCP is that with this patch, we get 2 more instructions eliminated in SCCP (sccp.NumInstRemoved).

Thinking about it, I am not too surprised that the impact of not forcing the condition to false is tiny. IIUC the only benefit would be if our guess (condition false) is actually true and the condition is used somewhere in the else branch. As mentioned in the discussion at D49384, this helps us to avoid obscure bugs caused by back-propagating information to the condition and resolves D49384 and D48327. I think we could come back to actually distinguishing between dead code and an LLVM "undef" value once there is a bigger need, e.g. because IPSCCP became more powerful

lib/Transforms/Scalar/SCCP.cpp
1594

markEdgeExecutable also checks if the edge is already known to be feasible. Maybe it's worth changing markEdgeExecutable to return false, when a unfeasible edge is passed?

efriedma updated this revision to Diff 156199.Jul 18 2018, 6:13 PM
efriedma retitled this revision from [SCCP] [WIP] Don't use markForcedConstant on branch conditions. to [SCCP] Don't use markForcedConstant on branch conditions..
efriedma edited the summary of this revision. (Show Details)

Cleaned up, added some tests.

davide accepted this revision.Jul 18 2018, 6:29 PM

LGTM modulo minor. Thanks.

lib/Transforms/Scalar/SCCP.cpp
1987–1997

do you need the else here? IIRC they are all mutually exclusive.

This revision is now accepted and ready to land.Jul 18 2018, 6:29 PM
efriedma added inline comments.Jul 18 2018, 6:37 PM
lib/Transforms/Scalar/SCCP.cpp
1987–1997

Just trying to make clear that this isn't an exhaustive list of possible terminator instructions (there are also exception-handling instructions, but we always treat their successors as feasible).

davide added inline comments.Jul 18 2018, 6:43 PM
lib/Transforms/Scalar/SCCP.cpp
1987–1997

OK, makes sense.

fhahn accepted this revision.Jul 19 2018, 2:03 AM

LGTM, thanks!

test/Transforms/SCCP/ipsccp-paramstate-forcedconst.ll
1 ↗(On Diff #156199)

nit: forcedconstant is not involved any more, maybe we should drop it from the name to avoid confusion or combine it with return-zapped.ll to something like ipsccp-branch-condition-unknown-initially.ll?

test/Transforms/SCCP/return-zapped.ll
46 ↗(On Diff #156199)

Is this test valuable with the new approach?

efriedma added inline comments.Jul 19 2018, 1:39 PM
test/Transforms/SCCP/ipsccp-paramstate-forcedconst.ll
1 ↗(On Diff #156199)

K

test/Transforms/SCCP/return-zapped.ll
9 ↗(On Diff #156199)

I'll fix this comment before merging.

46 ↗(On Diff #156199)

I don't think it does any harm to keep it.

This revision was automatically updated to reflect the committed changes.