[llvm] [llvm][CodeGen] Added a check in CodeGenPrepare::optimizeSwitchType (PR #83322)

Panagiotis K via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 27 06:58:42 PDT 2024


================
@@ -7387,17 +7387,38 @@ bool CodeGenPrepare::optimizeSwitchType(SwitchInst *SI) {
   // attribute. In that case, we can avoid an unnecessary mask/extension by
   // matching the argument extension instead.
   Instruction::CastOps ExtType = Instruction::ZExt;
-  // Some targets prefer SExt over ZExt.
-  if (TLI->isSExtCheaperThanZExt(OldVT, RegType))
-    ExtType = Instruction::SExt;
 
+  unsigned SExtUsers = 0;
+  unsigned ZExtUsers = 0;
   if (auto *Arg = dyn_cast<Argument>(Cond)) {
     if (Arg->hasSExtAttr())
-      ExtType = Instruction::SExt;
+      SExtUsers++;
     if (Arg->hasZExtAttr())
-      ExtType = Instruction::ZExt;
+      ZExtUsers++;
+  }
+  for (auto U : Cond->users()) {
+    if (isa<SExtInst>(U)) {
+      SExtUsers++;
+    } else if (isa<ZExtInst>(U)) {
+      ZExtUsers++;
+    } else if (auto *IC = dyn_cast<ICmpInst>(U)) {
+      if (IC->isSigned()) {
+        SExtUsers++;
+      } else if (IC->isUnsigned()) {
+        ZExtUsers++;
+      }
+    }
+  }
+  if (SExtUsers > ZExtUsers) {
+    ExtType = Instruction::SExt;
+  } else {
+    ExtType = Instruction::ZExt;
   }
 
+  // Some targets prefer SExt over ZExt.
+  if (TLI->isSExtCheaperThanZExt(OldVT, RegType))
----------------
karouzakisp wrote:

This is the text sizes with SExtIsCheaperThanZExt take no precedence over the user scan 
![SextIsCheaperThanZextisBeforeUserScan](https://github.com/llvm/llvm-project/assets/45971450/3608815f-28d3-483d-be3a-21d61b0340c9)
And this is the text sizes with SExtIsCheaperThanZExt taking precedence than user scan.
![SextIsCheaperThanZextisAfterUserScan](https://github.com/llvm/llvm-project/assets/45971450/d4809dba-67f9-4087-84da-0c4121030d62)



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


More information about the llvm-commits mailing list