[PATCH] D109428: [SImplifyCFG] SwitchInst processing redirecting the UB edges from the BB with the phiNode, that contains the UB, to the new BB.

Dmitry Bakunevich via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 8 03:34:01 PDT 2021


dbakunevich updated this revision to Diff 371304.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D109428/new/

https://reviews.llvm.org/D109428

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp


Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6613,10 +6613,25 @@
           if (DTU)
             DTU->applyUpdates({{DominatorTree::Delete, Predecessor, BB}});
           return true;
+        } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
+          // Create the new block in which will redirect the edges that 
+          // lead to undefine behavior
+          BasicBlock *Unreachable = BasicBlock::Create(
+              Predecessor->getContext(), "unreachable", SI->getFunction());
+          Builder.SetInsertPoint(Unreachable);
+          // The new block contains only one instruction: Unreachable
+          Builder.CreateUnreachable();
+          for (auto &Case : SI->cases())
+            if (Case.getCaseSuccessor() == BB)
+              Case.setSuccessor(Unreachable);
+
+          if (DTU)
+            DTU->applyUpdates(
+                { { DominatorTree::Delete, Predecessor, BB },
+                  { DominatorTree::Insert, Predecessor, Unreachable } });
+          return true;
         }
-        // TODO: SwitchInst.
       }
-
   return false;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109428.371304.patch
Type: text/x-patch
Size: 1251 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210908/26353c3b/attachment.bin>


More information about the llvm-commits mailing list