[llvm] Reland "[CVP] Check whether the default case is reachable (#79993)" (PR #96089)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 19 10:27:42 PDT 2024


================
@@ -402,6 +403,33 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
 
       // Increment the case iterator since we didn't delete it.
       ++CI;
+      ++ReachableCaseCount;
+    }
+
+    BasicBlock *DefaultDest = SI->getDefaultDest();
+    if (ReachableCaseCount > 1 &&
+        !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())) {
+      ConstantRange CR = LVI->getConstantRangeAtUse(I->getOperandUse(0),
+                                                    /*UndefAllowed*/ false);
+      // The default dest is unreachable if all cases are covered.
+      if (!CR.isSizeLargerThan(ReachableCaseCount)) {
+        BasicBlock *NewUnreachableBB =
+            BasicBlock::Create(BB->getContext(), "default.unreachable",
+                               BB->getParent(), DefaultDest);
+        new UnreachableInst(BB->getContext(), NewUnreachableBB);
+
+        DefaultDest->removePredecessor(BB);
+        SI->setDefaultDest(NewUnreachableBB);
+
+        if (SuccessorsCount[DefaultDest] == 1)
+          DTU.applyUpdatesPermissive(
+              {{DominatorTree::Delete, BB, DefaultDest}});
+        DTU.applyUpdatesPermissive(
+            {{DominatorTree::Insert, BB, NewUnreachableBB}});
----------------
nikic wrote:

```suggestion
          DTU.applyUpdates(
              {{DominatorTree::Delete, BB, DefaultDest}});
        DTU.applyUpdates(
            {{DominatorTree::Insert, BB, NewUnreachableBB}});
```

https://github.com/llvm/llvm-project/pull/96089


More information about the llvm-commits mailing list