This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] Use known bits to eliminate dead switch defaults
ClosedPublic

Authored by reames on Aug 31 2015, 11:14 AM.

Details

Summary

This is a follow up to http://reviews.llvm.org/D11995 implementing the suggestion by Hans.

If we know some of the bits of the value being switched on, we know that the maximum number of unique cases covers the unknown bits. This allows to eliminate switch defaults for large integers (i32) when most bits in the value are known.

Note that I had to make the transform contingent on not having any dead cases. This is conservatively correct with the old code, but required for the new code since we might have a dead case which varies one of the known bits. Counting that towards our number of covering cases would be bad.

Diff Detail

Repository
rL LLVM

Event Timeline

reames updated this revision to Diff 33601.Aug 31 2015, 11:14 AM
reames retitled this revision from to [SimplifyCFG] Use known bits to eliminate dead switch defaults.
reames updated this object.
reames added reviewers: hans, sanjoy.
reames added a subscriber: llvm-commits.
hans edited edge metadata.Aug 31 2015, 1:36 PM

Thanks for the patch!

lib/Transforms/Utils/SimplifyCFG.cpp
3262 ↗(On Diff #33601)

Instead of making this contingent on DeadCases.empty(), can't we use "SI->getNumCases() - DeadCases.size()" to get the number of live cases?

The situation I have in mind:

x &= 0x3; // NumUnknownBits is now 2
switch (x) {
case 0, 1, 2, 3: stuff
case 5: dead
default: dead
}
reames added inline comments.Aug 31 2015, 5:34 PM
lib/Transforms/Utils/SimplifyCFG.cpp
3262 ↗(On Diff #33601)

This example will work today, just as two rounds. We'll first delete the dead case, then come back around (within the same run of the pass) and delete the dead default. Your requested change would basically just change the order of the two steps. We could try to do everything at once, but doing each step individually seems less error prone.

hans accepted this revision.Sep 1 2015, 7:57 AM
hans edited edge metadata.

lgtm

lib/Transforms/Utils/SimplifyCFG.cpp
3262 ↗(On Diff #33601)

Even if the next round will take care of it, it seems simple enough that we might as well handle it while we're here the first time.

It's no big deal though, and if you prefer to not do it that's fine too; as long as the example works, I'm happy.

This revision is now accepted and ready to land.Sep 1 2015, 7:57 AM
hans added a comment.Sep 8 2015, 8:44 AM

Did this land?

This revision was automatically updated to reflect the committed changes.