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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 30 08:26:54 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);
----------------
dtcxzyw wrote:

Done.

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


More information about the llvm-commits mailing list