[llvm] [InstCombine] Canonicalize `switch(X^C)` expressions to `switch(X)` (PR #143677)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 12 04:53:33 PDT 2025


================
@@ -3948,6 +3948,18 @@ Instruction *InstCombinerImpl::visitSwitchInst(SwitchInst &SI) {
     }
   }
 
+  ConstantInt *XorRHS;
+  if (match(Cond, m_Xor(m_Value(Op0), m_ConstantInt(XorRHS)))) {
+    // Fold 'switch (X^C) case A' into 'switch (X) case A^C'.
+    for (auto &Case : SI.cases()) {
+      Constant *NewCase = ConstantExpr::getXor(Case.getCaseValue(), XorRHS);
+      assert(isa<ConstantInt>(NewCase) &&
+             "Result of expression should be constant");
+      Case.setValue(cast<ConstantInt>(NewCase));
+    }
+    return replaceOperand(SI, 0, Op0);
+  }
----------------
antoniofrighetto wrote:

Generalized this, thanks, hope it aligns with what you had in mind.

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


More information about the llvm-commits mailing list