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

Panagiotis K via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 28 11:35:48 PST 2024


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

When a sext/zext exists in the Switch Condition we need to check to not insert another sext/zext. Also we check if the extended width is less than the preferred width. If it isn't we mutate the type of the existing extension.

>From bcaca8e49e80995493016e3508006b57a484adee Mon Sep 17 00:00:00 2001
From: Panagiotis K <karouzakisp at gmail.com>
Date: Wed, 28 Feb 2024 18:50:33 +0000
Subject: [PATCH] [llvm][CodeGen] Added a check in
 CodeGenPrepare::optimizeSwitchType so that when a sext/zext exists in the
 Switch Condition we do not insert another sext/zext. Also we check if the
 extended width is less than the preferred width. If it isn't we mutate the
 type of the existing extension.

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp     |  29 +++++--
 llvm/test/CodeGen/RISCV/switch-width.ll | 103 +++++++++++++++++++++---
 2 files changed, 115 insertions(+), 17 deletions(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index feefe87f406365..39643c17d3b064 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -7397,11 +7397,30 @@ bool CodeGenPrepare::optimizeSwitchType(SwitchInst *SI) {
     if (Arg->hasZExtAttr())
       ExtType = Instruction::ZExt;
   }
-
-  auto *ExtInst = CastInst::Create(ExtType, Cond, NewType);
-  ExtInst->insertBefore(SI);
-  ExtInst->setDebugLoc(SI->getDebugLoc());
-  SI->setCondition(ExtInst);
+  // If Cond has already a zext or sext don't insert another one.
+  bool ExtExists = false;
+  if(auto *I = dyn_cast<Instruction>(Cond)){
+    unsigned Opc = I->getOpcode();
+    if(Opc == Instruction::SExt || Opc == Instruction::ZExt){
+      ExtExists = true;
+    }
+    /* TODO check if something can be done with signed compares
+    else if(Opc == Instruction::ICmp){
+      auto *CI = dyn_cast<ICmpInst>(I);
+      if(CI->isSigned()){
+      }
+    }*/
+  }
+  // Extension exists but has smaller type we need to widen it to 
+  // the preferred switch type
+  if(ExtExists){
+    Cond->mutateType(NewType); 
+  }else{
+    auto *ExtInst = CastInst::Create(ExtType, Cond, NewType);
+    ExtInst->insertBefore(SI);
+    ExtInst->setDebugLoc(SI->getDebugLoc());
+    SI->setCondition(ExtInst);
+  }
   for (auto Case : SI->cases()) {
     const APInt &NarrowConst = Case.getCaseValue()->getValue();
     APInt WideConst = (ExtType == Instruction::ZExt)
diff --git a/llvm/test/CodeGen/RISCV/switch-width.ll b/llvm/test/CodeGen/RISCV/switch-width.ll
index d902bd3276a3ce..1568f5710fa75d 100644
--- a/llvm/test/CodeGen/RISCV/switch-width.ll
+++ b/llvm/test/CodeGen/RISCV/switch-width.ll
@@ -194,7 +194,86 @@ return:
   %retval = phi i32 [ -1, %sw.default ], [ 0, %sw.bb0 ], [ 1, %sw.bb1 ]
   ret i32 %retval
 }
+define i32 @sext_i32(i16 %a)  {
+; CHECK-LABEL: sext_i32:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    lui a1, 16
+; CHECK-NEXT:    addiw a1, a1, -1
+; CHECK-NEXT:    and a2, a0, a1
+; CHECK-NEXT:    beq a2, a1, .LBB5_3
+; CHECK-NEXT:  # %bb.1: # %entry
+; CHECK-NEXT:    slli a0, a0, 48
+; CHECK-NEXT:    srai a0, a0, 48
+; CHECK-NEXT:    li a1, 1
+; CHECK-NEXT:    bne a0, a1, .LBB5_4
+; CHECK-NEXT:  # %bb.2: # %sw.bb0
+; CHECK-NEXT:    li a0, 0
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB5_3: # %sw.bb1
+; CHECK-NEXT:    li a0, 1
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB5_4: # %sw.default
+; CHECK-NEXT:    li a0, -1
+; CHECK-NEXT:    ret
+entry:
+  %sext = sext i16 %a to i32
+  switch i32 %sext, label %sw.default [
+  i32 1, label %sw.bb0
+  i32 -1, label %sw.bb1
+  ]
+
+sw.bb0:
+  br label %return
+
+sw.bb1:
+  br label %return
+
+sw.default:
+  br label %return
+
+return:
+  %retval = phi i32 [ -1, %sw.default ], [ 0, %sw.bb0 ], [ 1, %sw.bb1 ]
+  ret i32 %retval
+}
+
+define i32 @sext_i16(i8 %a)  {
+; CHECK-LABEL: sext_i16:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    bnez zero, .LBB6_3
+; CHECK-NEXT:  # %bb.1: # %entry
+; CHECK-NEXT:    slli a0, a0, 56
+; CHECK-NEXT:    srai a0, a0, 56
+; CHECK-NEXT:    li a1, 1
+; CHECK-NEXT:    bne a0, a1, .LBB6_4
+; CHECK-NEXT:  # %bb.2: # %sw.bb0
+; CHECK-NEXT:    li a0, 0
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB6_3: # %sw.bb1
+; CHECK-NEXT:    li a0, 1
+; CHECK-NEXT:    ret
+; CHECK-NEXT:  .LBB6_4: # %sw.default
+; CHECK-NEXT:    li a0, -1
+; CHECK-NEXT:    ret
+entry:
+  %sext = sext i8 %a to i16
+  switch i16 %sext, label %sw.default [
+  i16 1, label %sw.bb0
+  i16 -1, label %sw.bb1
+  ]
+
+sw.bb0:
+  br label %return
 
+sw.bb1:
+  br label %return
+
+sw.default:
+  br label %return
+
+return:
+  %retval = phi i32 [ -1, %sw.default ], [ 0, %sw.bb0 ], [ 1, %sw.bb1 ]
+  ret i32 %retval
+}
 
 define i32 @trunc_i12(i64 %a)  {
 ; CHECK-LABEL: trunc_i12:
@@ -202,17 +281,17 @@ define i32 @trunc_i12(i64 %a)  {
 ; CHECK-NEXT:    lui a1, 1
 ; CHECK-NEXT:    addiw a1, a1, -1
 ; CHECK-NEXT:    and a0, a0, a1
-; CHECK-NEXT:    beq a0, a1, .LBB5_3
+; CHECK-NEXT:    beq a0, a1, .LBB7_3
 ; CHECK-NEXT:  # %bb.1: # %entry
 ; CHECK-NEXT:    li a1, 1
-; CHECK-NEXT:    bne a0, a1, .LBB5_4
+; CHECK-NEXT:    bne a0, a1, .LBB7_4
 ; CHECK-NEXT:  # %bb.2: # %sw.bb0
 ; CHECK-NEXT:    li a0, 0
 ; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB5_3: # %sw.bb1
+; CHECK-NEXT:  .LBB7_3: # %sw.bb1
 ; CHECK-NEXT:    li a0, 1
 ; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB5_4: # %sw.default
+; CHECK-NEXT:  .LBB7_4: # %sw.default
 ; CHECK-NEXT:    li a0, -1
 ; CHECK-NEXT:    ret
 entry:
@@ -241,17 +320,17 @@ define i32 @trunc_i11(i64 %a)  {
 ; CHECK:       # %bb.0: # %entry
 ; CHECK-NEXT:    andi a0, a0, 2047
 ; CHECK-NEXT:    li a1, 2047
-; CHECK-NEXT:    beq a0, a1, .LBB6_3
+; CHECK-NEXT:    beq a0, a1, .LBB8_3
 ; CHECK-NEXT:  # %bb.1: # %entry
 ; CHECK-NEXT:    li a1, 1
-; CHECK-NEXT:    bne a0, a1, .LBB6_4
+; CHECK-NEXT:    bne a0, a1, .LBB8_4
 ; CHECK-NEXT:  # %bb.2: # %sw.bb0
 ; CHECK-NEXT:    li a0, 0
 ; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB6_3: # %sw.bb1
+; CHECK-NEXT:  .LBB8_3: # %sw.bb1
 ; CHECK-NEXT:    li a0, 1
 ; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB6_4: # %sw.default
+; CHECK-NEXT:  .LBB8_4: # %sw.default
 ; CHECK-NEXT:    li a0, -1
 ; CHECK-NEXT:    ret
 entry:
@@ -281,17 +360,17 @@ define i32 @trunc_i10(i64 %a)  {
 ; CHECK:       # %bb.0: # %entry
 ; CHECK-NEXT:    andi a0, a0, 1023
 ; CHECK-NEXT:    li a1, 1023
-; CHECK-NEXT:    beq a0, a1, .LBB7_3
+; CHECK-NEXT:    beq a0, a1, .LBB9_3
 ; CHECK-NEXT:  # %bb.1: # %entry
 ; CHECK-NEXT:    li a1, 1
-; CHECK-NEXT:    bne a0, a1, .LBB7_4
+; CHECK-NEXT:    bne a0, a1, .LBB9_4
 ; CHECK-NEXT:  # %bb.2: # %sw.bb0
 ; CHECK-NEXT:    li a0, 0
 ; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB7_3: # %sw.bb1
+; CHECK-NEXT:  .LBB9_3: # %sw.bb1
 ; CHECK-NEXT:    li a0, 1
 ; CHECK-NEXT:    ret
-; CHECK-NEXT:  .LBB7_4: # %sw.default
+; CHECK-NEXT:  .LBB9_4: # %sw.default
 ; CHECK-NEXT:    li a0, -1
 ; CHECK-NEXT:    ret
 entry:



More information about the llvm-commits mailing list