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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 30 04:59:11 PST 2024


================
@@ -407,6 +408,35 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
 
       // Increment the case iterator since we didn't delete it.
       ++CI;
+      ++ReachableCaseCount;
+    }
+
+    BasicBlock *DefaultDest = SI->getDefaultDest();
+    if (!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);
+
+        bool RemoveOldBB = --SuccessorsCount[DefaultDest] == 0;
+
+        if (RemoveOldBB)
+          DefaultDest->removePredecessor(BB);
----------------
nikic wrote:

Why is this conditional? Please make sure you have a test where there is a phi node in the default destination.

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


More information about the llvm-commits mailing list