[llvm] [CodeGen] Optimize switch on ucmp/scmp to branch sequence (PR #176582)

Kamini Banait via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 28 12:53:58 PST 2026


================
@@ -8127,9 +8125,88 @@ bool CodeGenPrepare::optimizeSwitchPhiConstants(SwitchInst *SI) {
   return Changed;
 }
 
-bool CodeGenPrepare::optimizeSwitchInst(SwitchInst *SI) {
+// Converts switch(ucmp(x,y)) into direct branches to avoid materializing the
+// -1/0/1 value.
+static bool optimizeSwitchOnCompare(SwitchInst *SI, Function &F,
+                                    const TargetTransformInfo &TTI,
+                                    ModifyDT &ModifiedDT) {
+  Value *Cond = SI->getCondition();
+
+  if (auto *Cast = dyn_cast<CastInst>(Cond)) {
+    if (Cast->getOpcode() == Instruction::ZExt ||
+        Cast->getOpcode() == Instruction::SExt) {
+      Cond = Cast->getOperand(0);
+    }
+  }
----------------
kamini08 wrote:

I thought that there'd be casts added by previous passes, so explicitly handling casts was important.

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


More information about the llvm-commits mailing list