This is an archive of the discontinued LLVM Phabricator instance.

[SCCP] Remove dead switch cases based on range information
ClosedPublic

Authored by nikic on Jul 21 2020, 2:21 PM.

Details

Summary

Determine whether switch edges are feasible based on range information, and remove non-feasible edges lateron.

This does not try to determine whether the default edge is dead, as we'd have to determine that the range is fully covered by the cases for that.

Another limitation here is that we don't remove dead cases that have the same successor as a live case. I'm not handling this because I wanted to keep the edge removal based on feasible edges only, rather than inspecting ranges again there -- this does not seem like a particularly useful case to handle.

Diff Detail

Event Timeline

nikic created this revision.Jul 21 2020, 2:21 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 21 2020, 2:21 PM
fhahn added inline comments.Jul 25 2020, 10:39 AM
llvm/lib/Transforms/Scalar/SCCP.cpp
658

Do we have to exclude constant ranges that may be undef here? Should all successors be feasible in that case?

1866

Recently c++20 style contains members have been added to various data types. Might be good to use instead of count here. (https://en.cppreference.com/w/cpp/container/set/contains).

Also, maybe consider using an early continue/increment if the current case is a feasible successor.

nikic marked an inline comment as done.Jul 25 2020, 11:49 AM
nikic added inline comments.
llvm/lib/Transforms/Scalar/SCCP.cpp
658

Depends on how conservative we want to be :) Per langref, switch on undef is UB. In practical terms, I would expect the situation for switch to be less problematic than for br, because we generally don't create switches "out of thin air", e.g. loop unswitch does not create switch statements.

fhahn added inline comments.Jul 27 2020, 3:21 AM
llvm/lib/Transforms/Scalar/SCCP.cpp
658

I think it would be more consistent to follow to same logic as we do for branches here and flip the switch together. It might not be as big of a problem as for branches and there's no clear line, but it might be surprising for readers of the code. And we should really work towards flipping globally :)

nikic marked an inline comment as done.Jul 27 2020, 3:26 AM
nikic added inline comments.
llvm/lib/Transforms/Scalar/SCCP.cpp
658

Makes sense. In that case I'll wait on the assume change in D83643 (or I could split that off into a separate revision), to allow easy testing.

nikic updated this revision to Diff 281720.Jul 29 2020, 1:31 PM
nikic marked an inline comment as done.

Set AllowUndef=false, switch tests to use !range instead of comparisons.

fhahn accepted this revision.Jul 30 2020, 2:15 AM

LGTM, thanks!

This revision is now accepted and ready to land.Jul 30 2020, 2:15 AM
This revision was automatically updated to reflect the committed changes.