This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] Share code to compute switch density between ShouldBuildLookupTable() and ReduceSwitchRange()
ClosedPublic

Authored by hans on Jun 10 2022, 3:36 AM.

Details

Summary

They're computing the same thing. No functionality change.

Diff Detail

Event Timeline

hans created this revision.Jun 10 2022, 3:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 10 2022, 3:36 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
hans requested review of this revision.Jun 10 2022, 3:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 10 2022, 3:36 AM
thakis accepted this revision.Jun 10 2022, 5:58 AM
thakis added inline comments.
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
6119

Before we could handle table size up to UINT64_MAX / 10, after only up to UINT64_MAX / 100. That's not "no functionality change", is it?

This revision is now accepted and ready to land.Jun 10 2022, 5:58 AM
hans marked an inline comment as done.Jun 10 2022, 6:20 AM
hans added inline comments.
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
6119

I think it's still NFC in practice; we'd return false for table sizes of that magnitude anyway.

thakis added inline comments.Jun 10 2022, 6:23 AM
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
6119

Should we just remove all the overflow checks then?

This revision was landed with ongoing or failed builds.Jun 10 2022, 6:30 AM
This revision was automatically updated to reflect the committed changes.
hans marked an inline comment as done.
hans added inline comments.Jun 10 2022, 6:36 AM
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
6119

No, you can still trigger overflows here, and overflowing would cause us to return the wrong answer.

For example:

switch (x) {
case 0: foo();
case VERY_BIG_NUMBER: bar();
}

TableSize will be VERY_BIG_NUMBER + 1, so overflowing when multiplying with 10 or 100 is a real concern.

But in practice, for huge table sizes, we'd return false later anyway, since it's not practically possible to have enough cases that the switch would be considered dense enough.

Makes sense, thanks!