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

Sayan Sivakumaran via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 6 19:46:25 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();
----------------
sivakusayan wrote:

Okay, I opted to fix this by just using the first instruction as the context. I tried to understand whether it would be correct to set `AllowEphemerals`, as I was a bit confused by the comments. However, after digging into the original PR that exposed that parameter, I believe it's correct as we are using the assumes to modify the CFG, and so we won't ever optimize out the assumes themselves.

I also added some more tests to cover different cases of `isValidAssumeForContext`. I didn't add a `noreturn` test because it seems instructions after a `noreturn` call are killed before they ever reach this code, so I think it would be unnecessary, but let me know if that's incorrect.

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


More information about the llvm-commits mailing list