[llvm] [SimplifyCFG] Simplify switch default branch when branch proves operand value (PR #206597)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 5 08:27:23 PDT 2026


================
@@ -7813,6 +7813,42 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
   return true;
 }
 
+static bool simplifySwitchDefaultBranch(SwitchInst *SI, DomTreeUpdater *DTU,
+                                        const DataLayout &DL,
+                                        AssumptionCache *AC) {
+  assert(SI);
+  if (SI->defaultDestUnreachable())
+    return false;
+
+  // If it can be proved that the switch condition takes some concrete value
+  // in the default block, we can make some nice simplifications to the
+  // switch.
+  BasicBlock *Default = SI->getDefaultDest();
+  const Instruction *CxtI = Default->getTerminator();
----------------
dtcxzyw wrote:

Missing `isGuaranteedToTransferExecutionToSuccessor` check. If you want to use all the assumption information from the whole BB, use this to make sure the call to `llvm.assume` is reachable.

Another simple way is to use the first instruction as CxtI. Then `isValidAssumeForContext` will handle it automatically.


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


More information about the llvm-commits mailing list