This is an archive of the discontinued LLVM Phabricator instance.

SimplifyCFG: don't remove unreachable defaults from switch instructions; exploit them instead!
ClosedPublic

Authored by hans on Dec 1 2014, 6:01 PM.

Details

Summary

This patch actually does a number of related things. I'm happy to split it up if it makes reviewing easier:

  1. Make SimplifyCFG stop removing unreachable defaults so they can be exploited by other transformations.
  1. Omit range checks in switch-to-lookup table formation if the default is unreachable. This saves 64 kB off a Clang bootstrap on 64-bit Linux. No effect on Chromium unfortunately.
  1. Make ConstantFoldTerminator() work when faced with unreachable default.
  1. Make TurnSwitchRangeIntoICmp() work with unreachable default. This also makes us handle some cases we'd previously get wrong, see the unreachable2 test.

Please take a look!

Diff Detail

Repository
rL LLVM

Event Timeline

hans updated this revision to Diff 16794.Dec 1 2014, 6:01 PM
hans retitled this revision from to SimplifyCFG: don't remove unreachable defaults from switch instructions; exploit them instead!.
hans updated this object.
hans edited the test plan for this revision. (Show Details)
hans added reviewers: rnk, majnemer, eeckstein.
hans added subscribers: Unknown Object (MLST), hansw.
majnemer edited edge metadata.Dec 3 2014, 10:56 AM

I think it would be nice to split the patch into middle-end and backend pieces.

hans updated this revision to Diff 16877.Dec 3 2014, 11:26 AM
hans updated this object.
hans edited edge metadata.

I think it would be nice to split the patch into middle-end and backend pieces.

I've uploaded the backend part here: http://reviews.llvm.org/D6510

hans updated this revision to Diff 16955.Dec 4 2014, 2:40 PM

I committed the tests that were checking existing functionality in r223395 and r223396 to reduce the size of this patch a little.

majnemer added inline comments.Dec 4 2014, 3:26 PM
lib/Transforms/Utils/SimplifyCFG.cpp
3228 ↗(On Diff #16955)

!ContiguousCases->empty()

3247 ↗(On Diff #16955)

I'd go with UINT32_MAX to match the casts bellow.

4202 ↗(On Diff #16955)
/*DontDeleteUselessPHIs=*/true
hans added inline comments.Dec 4 2014, 3:43 PM
lib/Transforms/Utils/SimplifyCFG.cpp
3228 ↗(On Diff #16955)

Done.

3247 ↗(On Diff #16955)

Done.

4202 ↗(On Diff #16955)

Done.

hans updated this revision to Diff 16960.Dec 4 2014, 3:44 PM

Addressing David's patches.

Can you update LowerSwitch.cpp to incorporate this optimization? Otherwise targets that rely on it are losing this functionality.

—Owen

hans added a comment.Dec 5 2014, 3:24 PM
In D6471#14, @resistor wrote:

Can you update LowerSwitch.cpp to incorporate this optimization? Otherwise targets that rely on it are losing this functionality.

It seems LowerSwitch.cpp is already set up to exploit unreachable defaults, see the test in test/Transforms/LowerSwitch/2014-06-11-SwitchDefaultUnreachableOpt.ll

I guess it's a choice of whether that's important, or if moving popular cases into the default is better. If it's the latter I'm happy to implement it.

hans added a comment.Dec 16 2014, 3:22 PM

I've uploaded a patch for this here: http://reviews.llvm.org/D6697

  • Hans
This revision was automatically updated to reflect the committed changes.
This revision was automatically updated to reflect the committed changes.