[PATCH] D109428: [SImplifyCFG] Creating removeUndefIntroducingPredecessor in SwitchInst

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


dbakunevich created this revision.
dbakunevich added reviewers: mkazantsev, lebedev.ri, xbolva00.
Herald added a subscriber: hiraditya.
dbakunevich requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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()


Repository:
  rG LLVM Github Monorepo

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,8 +6613,24 @@
           if (DTU)
             DTU->applyUpdates({{DominatorTree::Delete, Predecessor, BB}});
           return true;
+        } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
+          // Create the new block in which I 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.371292.patch
Type: text/x-patch
Size: 1247 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210908/cc3f4da1/attachment.bin>


More information about the llvm-commits mailing list