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

Panagiotis K via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 20:54:53 PDT 2024


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

>From 31888ace26e72432a0824201702e47675e72f628 Mon Sep 17 00:00:00 2001
From: Panagiotis K <karouzakisp at gmail.com>
Date: Thu, 29 Feb 2024 00:02:05 +0000
Subject: [PATCH] [llvm][CodeGen] Added a check in
 CodeGenPrepare::OptimizeSwitchType,         so that if we have more SExt
 Users than ZExt Users we use SExt         instead of a ZExt in the Switch
 Condition because that creates a redundant instruction.

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp     |  34 ++++++--
 llvm/opt-switch-type.ll                 |  95 +++++++++++++++++++++
 llvm/test/CodeGen/RISCV/switch-width.ll | 107 +++++++++++++++++++++---
 llvm/test/CodeGen/RISCV/t.c             |  22 +++++
 llvm/test/CodeGen/RISCV/t.ll            |  68 +++++++++++++++
 5 files changed, 308 insertions(+), 18 deletions(-)
 create mode 100644 llvm/opt-switch-type.ll
 create mode 100644 llvm/test/CodeGen/RISCV/t.c
 create mode 100644 llvm/test/CodeGen/RISCV/t.ll

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index feefe87f406365..0070d0747dee01 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -7387,17 +7387,39 @@ 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))
+    ExtType = Instruction::SExt;
+ 
   auto *ExtInst = CastInst::Create(ExtType, Cond, NewType);
   ExtInst->insertBefore(SI);
   ExtInst->setDebugLoc(SI->getDebugLoc());
diff --git a/llvm/opt-switch-type.ll b/llvm/opt-switch-type.ll
new file mode 100644
index 00000000000000..bdd525b2d71b85
--- /dev/null
+++ b/llvm/opt-switch-type.ll
@@ -0,0 +1,95 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
+; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
+; RUN:   | FileCheck -check-prefix=RV64I %s
+
+define dso_local void @test_switch_2_users(ptr nocapture noundef readonly %x) local_unnamed_addr {
+; RV64I-LABEL: test_switch_2_users:
+; RV64I:       # %bb.0: # %entry
+; RV64I-NEXT:    addi sp, sp, -16
+; RV64I-NEXT:    .cfi_def_cfa_offset 16
+; RV64I-NEXT:    sd ra, 8(sp) # 8-byte Folded Spill
+; RV64I-NEXT:    .cfi_offset ra, -8
+; RV64I-NEXT:    lh a0, 0(a0)
+; RV64I-NEXT:    li a1, 16
+; RV64I-NEXT:    blt a0, a1, .LBB0_5
+; RV64I-NEXT:  # %bb.1: # %if.then
+; RV64I-NEXT:    addi a1, a0, -17
+; RV64I-NEXT:    li a2, 2
+; RV64I-NEXT:    bgeu a1, a2, .LBB0_3
+; RV64I-NEXT:  # %bb.2: # %sw.bb3
+; RV64I-NEXT:    call foo2
+; RV64I-NEXT:    j .LBB0_5
+; RV64I-NEXT:  .LBB0_3: # %if.then
+; RV64I-NEXT:    li a1, 16
+; RV64I-NEXT:    bne a0, a1, .LBB0_5
+; RV64I-NEXT:  # %bb.4: # %sw.bb
+; RV64I-NEXT:    call foo1
+; RV64I-NEXT:  .LBB0_5: # %if.end
+; RV64I-NEXT:    ld ra, 8(sp) # 8-byte Folded Reload
+; RV64I-NEXT:    addi sp, sp, 16
+; RV64I-NEXT:    tail foo3
+entry:
+  %0 = load i16, ptr %x, align 2
+  %cmp = icmp sgt i16 %0, 15
+  br i1 %cmp, label %if.then, label %if.end
+
+if.then:
+  switch i16 %0, label %if.end [
+    i16 16, label %sw.bb
+    i16 17, label %sw.bb3
+    i16 18, label %sw.bb3
+  ]
+
+sw.bb:
+  tail call void @foo1() #2
+  br label %if.end
+
+sw.bb3:
+  tail call void @foo2() #2
+  br label %if.end
+
+if.end:
+  tail call void @foo3() #2
+  ret void
+}
+
+declare void @foo1(...) local_unnamed_addr #1
+
+declare void @foo2(...) local_unnamed_addr #1
+
+declare void @foo3(...) local_unnamed_addr #1
+
+define dso_local void @foo(ptr nocapture noundef readonly %x) local_unnamed_addr {
+entry:
+  %0 = load i16, ptr %x, align 2
+  %1 = and i16 %0, -4
+  %cmp = icmp eq i16 %1, 4
+  %cmp4 = icmp sgt i16 %0, 15
+  %or.cond10 = and i1 %cmp4, %cmp
+  br i1 %or.cond10, label %if.then6, label %if.end9
+
+if.then6:
+  switch i16 %0, label %if.end9 [
+    i16 16, label %sw.bb
+    i16 17, label %sw.bb8
+    i16 18, label %sw.bb8
+  ]
+
+sw.bb:
+  tail call void @bar() #2
+  br label %if.end9
+
+sw.bb8:
+  tail call void @baz() #2
+  br label %if.end9
+
+if.end9:
+  tail call void @bat() #2
+  ret void
+}
+
+declare void @bar(...) local_unnamed_addr #1
+
+declare void @baz(...) local_unnamed_addr #1
+
+declare void @bat(...) local_unnamed_addr #1
diff --git a/llvm/test/CodeGen/RISCV/switch-width.ll b/llvm/test/CodeGen/RISCV/switch-width.ll
index d902bd3276a3ce..fa3a321d85c471 100644
--- a/llvm/test/CodeGen/RISCV/switch-width.ll
+++ b/llvm/test/CodeGen/RISCV/switch-width.ll
@@ -194,7 +194,90 @@ 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:    andi a1, a0, 255
+; CHECK-NEXT:    li a2, 255
+; CHECK-NEXT:    beq a1, a2, .LBB6_3
+; CHECK-NEXT:  # %bb.1: # %entry
+; CHECK-NEXT:    slli a0, a0, 56
+; CHECK-NEXT:    srai a0, a0, 56
+; CHECK-NEXT:    slli a0, a0, 48
+; CHECK-NEXT:    srli a0, a0, 48
+; 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 +285,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 +324,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 +364,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:
diff --git a/llvm/test/CodeGen/RISCV/t.c b/llvm/test/CodeGen/RISCV/t.c
new file mode 100644
index 00000000000000..cebb6c75f015d1
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/t.c
@@ -0,0 +1,22 @@
+void bar();
+void foo1();
+void foo2();
+void bat();
+
+void foo(short *x) {
+  short y = *x;
+  if (y > 15) {
+  switch (y) {
+  case 16:
+    bar();
+    break;
+  case 17:
+    foo1();
+  case 18:
+    foo2();
+    break;
+  }
+  }
+
+  bat();
+}
diff --git a/llvm/test/CodeGen/RISCV/t.ll b/llvm/test/CodeGen/RISCV/t.ll
new file mode 100644
index 00000000000000..ac13298dece803
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/t.ll
@@ -0,0 +1,68 @@
+; ModuleID = 't.c'
+source_filename = "t.c"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local void @foo(ptr noundef %x) #0 {
+entry:
+  %x.addr = alloca ptr, align 8
+  %y = alloca i16, align 2
+  store ptr %x, ptr %x.addr, align 8
+  %0 = load ptr, ptr %x.addr, align 8
+  %1 = load i16, ptr %0, align 2
+  store i16 %1, ptr %y, align 2
+  %2 = load i16, ptr %y, align 2
+  %conv = sext i16 %2 to i32
+  %cmp = icmp sgt i32 %conv, 15
+  br i1 %cmp, label %if.then, label %if.end
+
+if.then:                                          ; preds = %entry
+  %3 = load i16, ptr %y, align 2
+  %conv2 = sext i16 %3 to i32
+  switch i32 %conv2, label %sw.epilog [
+    i32 16, label %sw.bb
+    i32 17, label %sw.bb3
+    i32 18, label %sw.bb4
+  ]
+
+sw.bb:                                            ; preds = %if.then
+  call void (...) @bar()
+  br label %sw.epilog
+
+sw.bb3:                                           ; preds = %if.then
+  call void (...) @foo1()
+  br label %sw.bb4
+
+sw.bb4:                                           ; preds = %if.then, %sw.bb3
+  call void (...) @foo2()
+  br label %sw.epilog
+
+sw.epilog:                                        ; preds = %if.then, %sw.bb4, %sw.bb
+  br label %if.end
+
+if.end:                                           ; preds = %sw.epilog, %entry
+  call void (...) @bat()
+  ret void
+}
+
+declare void @bar(...) #1
+
+declare void @foo1(...) #1
+
+declare void @foo2(...) #1
+
+declare void @bat(...) #1
+
+attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+
+!llvm.module.flags = !{!0, !1, !2, !3, !4}
+!llvm.ident = !{!5}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 8, !"PIC Level", i32 2}
+!2 = !{i32 7, !"PIE Level", i32 2}
+!3 = !{i32 7, !"uwtable", i32 2}
+!4 = !{i32 7, !"frame-pointer", i32 2}
+!5 = !{!"clang version 16.0.6 (git at github.com:karouzakisp/llvm-project.git 41a95c5c390d1c2854d15f0ee9cd7ca37c4b6dcf)"}



More information about the llvm-commits mailing list