This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] SwitchInst processing redirecting the UB edges from the BB with the phiNode, that contains the UB, to the new BB.
ClosedPublic

Authored by dbakunevich on Sep 8 2021, 3:01 AM.

Details

Summary

This revision update function removeUndefIntroducingPredecessor in SImplifyCFG.
Adding processing of SwitchInst cases.
How it works:

main:
x = random(1, 2);
switch (x) {
     case 1: br BB1
     case 2: br BB2
     case 3: br BB3 ---------------------------> UnreachebleBlock
     case 4: br BB4 ---------------------------> UnreachebleBlock
}

BB1 and BB2:
phi = [...], ...
todo something


BB3 and BB4:---------------------------------> UnreachebleBlock:
phi = [undef, main] --------------------------> CreateUnreacheble()

Diff Detail

Event Timeline

dbakunevich created this revision.Sep 8 2021, 3:01 AM
dbakunevich requested review of this revision.Sep 8 2021, 3:01 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 8 2021, 3:01 AM

Tests missing

Tests missing

Should I create tests?

Tests missing

Should I create tests?

Yes, please

dbakunevich retitled this revision from [SImplifyCFG] Creating removeUndefIntroducingPredecessor in SwitchInst to [SImplifyCFG] SwitchInst processing redirecting the UB edges from the BB with the phiNode, that contains the UB, to the new BB..
mkazantsev added inline comments.Sep 8 2021, 4:14 AM
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
6647

Interesting follow-up can be done here. Imagine that after this is done, only one edge that doesn't go to unreachable remains. In this case, we can remove this switch and insert assume(switched_value == remained_case_value).

Totally fine if it's a separate patch.

mkazantsev added inline comments.Sep 8 2021, 4:19 AM
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
6638

"Create an unreachable block and redirect all edges going to BB into this block".

mkazantsev requested changes to this revision.Sep 10 2021, 1:22 AM

You need to update Phis somehow.

This revision now requires changes to proceed.Sep 10 2021, 1:22 AM

Are you sure you've updated the tests? I don't see unreachable block in them.

mkazantsev added inline comments.Sep 13 2021, 1:12 AM
llvm/test/Transforms/SimplifyCFG/switch_ub.ll
9

You can remove this TODO as well.

Seems that optimizer has become too smart for these tests, I'll add more now. :)

Please rebase on top of

commit 7e337d8ba2ff5015d83f355be1049e3e13fa4d18 (HEAD -> main, origin/main)
Author: Max Kazantsev <mkazantsev@azul.com>
Date:   Mon Sep 13 15:27:33 2021 +0700

    [Test] Add more sophisticated tests for switch UB opt

    Optimizer is being too smart with existing tests, and the transform
    gets concealed by following transforms.

It should now be showing what this transform actually does.

Please check new updates in this patch.

mkazantsev accepted this revision.Sep 16 2021, 10:46 PM

LGTM with nits

llvm/lib/Transforms/Utils/SimplifyCFG.cpp
6616

Better

Redirect all branches leading to UB into a newly created unreachable block.
llvm/test/Transforms/SimplifyCFG/switch_ub.ll
64–65

Pls remove TODOs

This revision is now accepted and ready to land.Sep 16 2021, 10:46 PM
dbakunevich retitled this revision from [SImplifyCFG] SwitchInst processing redirecting the UB edges from the BB with the phiNode, that contains the UB, to the new BB. to [SimplifyCFG] SwitchInst processing redirecting the UB edges from the BB with the phiNode, that contains the UB, to the new BB..Sep 16 2021, 10:47 PM
lebedev.ri accepted this revision.Sep 17 2021, 12:20 AM

lg

llvm/lib/Transforms/Utils/SimplifyCFG.cpp
6633–6635

Is the code clang-formatted?
Let's for consistency always do Insert first.

dbakunevich updated this revision to Diff 373535.
dbakunevich marked an inline comment as done.
This revision was landed with ongoing or failed builds.Sep 20 2021, 8:45 PM
This revision was automatically updated to reflect the committed changes.