[llvm] ab0dc29 - [RISCV][GISel] Allow s32 G_PHI for RV64 to support f32 phis.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 6 12:00:15 PST 2024


Author: Craig Topper
Date: 2024-12-06T11:53:57-08:00
New Revision: ab0dc290bca14b8a13540a846724dcd034397f5d

URL: https://github.com/llvm/llvm-project/commit/ab0dc290bca14b8a13540a846724dcd034397f5d
DIFF: https://github.com/llvm/llvm-project/commit/ab0dc290bca14b8a13540a846724dcd034397f5d.diff

LOG: [RISCV][GISel] Allow s32 G_PHI for RV64 to support f32 phis.

Added: 
    llvm/test/CodeGen/RISCV/GlobalISel/phi.ll

Modified: 
    llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
    llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-phi-rv64.mir

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp b/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
index 93d8fa92fa863d..1199dabf3d1659 100644
--- a/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
+++ b/llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
@@ -412,9 +412,9 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
   getActionDefinitionsBuilder(G_BRINDIRECT).legalFor({p0});
 
   getActionDefinitionsBuilder(G_PHI)
-      .legalFor({p0, sXLen})
+      .legalFor({p0, s32, sXLen})
       .widenScalarToNextPow2(0)
-      .clampScalar(0, sXLen, sXLen);
+      .clampScalar(0, s32, sXLen);
 
   getActionDefinitionsBuilder({G_GLOBAL_VALUE, G_JUMP_TABLE, G_CONSTANT_POOL})
       .legalFor({p0});

diff  --git a/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-phi-rv64.mir b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-phi-rv64.mir
index 7111f7429e75c1..980682616d1d35 100644
--- a/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-phi-rv64.mir
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-phi-rv64.mir
@@ -139,6 +139,7 @@ body:             |
   ; CHECK-NEXT:   [[COPY:%[0-9]+]]:_(s64) = COPY $x10
   ; CHECK-NEXT:   [[COPY1:%[0-9]+]]:_(s64) = COPY $x11
   ; CHECK-NEXT:   [[COPY2:%[0-9]+]]:_(s64) = COPY $x12
+  ; CHECK-NEXT:   [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[COPY1]](s64)
   ; CHECK-NEXT:   [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
   ; CHECK-NEXT:   [[AND:%[0-9]+]]:_(s64) = G_AND [[COPY]], [[C]]
   ; CHECK-NEXT:   G_BRCOND [[AND]](s64), %bb.2
@@ -147,9 +148,12 @@ body:             |
   ; CHECK-NEXT: bb.1.cond.false:
   ; CHECK-NEXT:   successors: %bb.2(0x80000000)
   ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   [[TRUNC1:%[0-9]+]]:_(s32) = G_TRUNC [[COPY2]](s64)
+  ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.2.cond.end:
-  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:_(s64) = G_PHI [[COPY2]](s64), %bb.1, [[COPY1]](s64), %bb.0
-  ; CHECK-NEXT:   $x10 = COPY [[PHI]](s64)
+  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:_(s32) = G_PHI [[TRUNC1]](s32), %bb.1, [[TRUNC]](s32), %bb.0
+  ; CHECK-NEXT:   [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[PHI]](s32)
+  ; CHECK-NEXT:   $x10 = COPY [[ANYEXT]](s64)
   ; CHECK-NEXT:   PseudoRET implicit $x10
   bb.1.entry:
     liveins: $x10, $x11, $x12
@@ -199,6 +203,7 @@ body:             |
   ; CHECK-NEXT:   [[COPY:%[0-9]+]]:_(s64) = COPY $x10
   ; CHECK-NEXT:   [[COPY1:%[0-9]+]]:_(s64) = COPY $x11
   ; CHECK-NEXT:   [[COPY2:%[0-9]+]]:_(s64) = COPY $x12
+  ; CHECK-NEXT:   [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[COPY1]](s64)
   ; CHECK-NEXT:   [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
   ; CHECK-NEXT:   [[AND:%[0-9]+]]:_(s64) = G_AND [[COPY]], [[C]]
   ; CHECK-NEXT:   G_BRCOND [[AND]](s64), %bb.2
@@ -207,9 +212,12 @@ body:             |
   ; CHECK-NEXT: bb.1.cond.false:
   ; CHECK-NEXT:   successors: %bb.2(0x80000000)
   ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   [[TRUNC1:%[0-9]+]]:_(s32) = G_TRUNC [[COPY2]](s64)
+  ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.2.cond.end:
-  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:_(s64) = G_PHI [[COPY2]](s64), %bb.1, [[COPY1]](s64), %bb.0
-  ; CHECK-NEXT:   $x10 = COPY [[PHI]](s64)
+  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:_(s32) = G_PHI [[TRUNC1]](s32), %bb.1, [[TRUNC]](s32), %bb.0
+  ; CHECK-NEXT:   [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[PHI]](s32)
+  ; CHECK-NEXT:   $x10 = COPY [[ANYEXT]](s64)
   ; CHECK-NEXT:   PseudoRET implicit $x10
   bb.1.entry:
     liveins: $x10, $x11, $x12
@@ -259,6 +267,7 @@ body:             |
   ; CHECK-NEXT:   [[COPY:%[0-9]+]]:_(s64) = COPY $x10
   ; CHECK-NEXT:   [[COPY1:%[0-9]+]]:_(s64) = COPY $x11
   ; CHECK-NEXT:   [[COPY2:%[0-9]+]]:_(s64) = COPY $x12
+  ; CHECK-NEXT:   [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[COPY1]](s64)
   ; CHECK-NEXT:   [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
   ; CHECK-NEXT:   [[AND:%[0-9]+]]:_(s64) = G_AND [[COPY]], [[C]]
   ; CHECK-NEXT:   G_BRCOND [[AND]](s64), %bb.2
@@ -267,9 +276,12 @@ body:             |
   ; CHECK-NEXT: bb.1.cond.false:
   ; CHECK-NEXT:   successors: %bb.2(0x80000000)
   ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   [[TRUNC1:%[0-9]+]]:_(s32) = G_TRUNC [[COPY2]](s64)
+  ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.2.cond.end:
-  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:_(s64) = G_PHI [[COPY2]](s64), %bb.1, [[COPY1]](s64), %bb.0
-  ; CHECK-NEXT:   $x10 = COPY [[PHI]](s64)
+  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:_(s32) = G_PHI [[TRUNC1]](s32), %bb.1, [[TRUNC]](s32), %bb.0
+  ; CHECK-NEXT:   [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[PHI]](s32)
+  ; CHECK-NEXT:   $x10 = COPY [[ANYEXT]](s64)
   ; CHECK-NEXT:   PseudoRET implicit $x10
   bb.1.entry:
     liveins: $x10, $x11, $x12
@@ -318,7 +330,9 @@ body:             |
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   [[COPY:%[0-9]+]]:_(s64) = COPY $x10
   ; CHECK-NEXT:   [[COPY1:%[0-9]+]]:_(s64) = COPY $x11
+  ; CHECK-NEXT:   [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[COPY1]](s64)
   ; CHECK-NEXT:   [[COPY2:%[0-9]+]]:_(s64) = COPY $x12
+  ; CHECK-NEXT:   [[TRUNC1:%[0-9]+]]:_(s32) = G_TRUNC [[COPY2]](s64)
   ; CHECK-NEXT:   [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
   ; CHECK-NEXT:   [[AND:%[0-9]+]]:_(s64) = G_AND [[COPY]], [[C]]
   ; CHECK-NEXT:   G_BRCOND [[AND]](s64), %bb.2
@@ -328,8 +342,9 @@ body:             |
   ; CHECK-NEXT:   successors: %bb.2(0x80000000)
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.2.cond.end:
-  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:_(s64) = G_PHI [[COPY2]](s64), %bb.1, [[COPY1]](s64), %bb.0
-  ; CHECK-NEXT:   $x10 = COPY [[PHI]](s64)
+  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:_(s32) = G_PHI [[TRUNC1]](s32), %bb.1, [[TRUNC]](s32), %bb.0
+  ; CHECK-NEXT:   [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[PHI]](s32)
+  ; CHECK-NEXT:   $x10 = COPY [[ANYEXT]](s64)
   ; CHECK-NEXT:   PseudoRET implicit $x10
   bb.1.entry:
     liveins: $x10, $x11, $x12

diff  --git a/llvm/test/CodeGen/RISCV/GlobalISel/phi.ll b/llvm/test/CodeGen/RISCV/GlobalISel/phi.ll
new file mode 100644
index 00000000000000..494d2c94c181db
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/phi.ll
@@ -0,0 +1,181 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=riscv32 -global-isel < %s | FileCheck %s --check-prefixes=CHECK,CHECKI,RV32
+; RUN: llc -mtriple=riscv64 -global-isel < %s | FileCheck %s --check-prefixes=CHECK,CHECKI,RV64
+; RUN: llc -mtriple=riscv32 -mattr=+f -global-isel < %s | FileCheck %s --check-prefixes=CHECK,CHECKIF,RV32
+; RUN: llc -mtriple=riscv64 -mattr=+f -global-isel < %s | FileCheck %s --check-prefixes=CHECK,CHECKIF,RV64
+
+define i1 @phi_i1(i1 %cnd, i1 %a, i1 %b) {
+; CHECK-LABEL: phi_i1:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    mv a3, a0
+; CHECK-NEXT:    mv a0, a1
+; CHECK-NEXT:    andi a3, a3, 1
+; CHECK-NEXT:    bnez a3, .LBB0_2
+; CHECK-NEXT:  # %bb.1: # %cond.false
+; CHECK-NEXT:    mv a0, a2
+; CHECK-NEXT:  .LBB0_2: # %cond.end
+; CHECK-NEXT:    ret
+entry:
+  br i1 %cnd, label %cond.end, label %cond.false
+
+cond.false:                                       ; preds = %entry
+  br label %cond.end
+
+cond.end:                                         ; preds = %entry, %cond.false
+  %cond = phi i1 [ %b, %cond.false ], [ %a, %entry ]
+  ret i1 %cond
+}
+
+define i8 @phi_i8(i1 %cnd, i8 %a, i8 %b) {
+; CHECK-LABEL: phi_i8:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    mv a3, a0
+; CHECK-NEXT:    mv a0, a1
+; CHECK-NEXT:    andi a3, a3, 1
+; CHECK-NEXT:    bnez a3, .LBB1_2
+; CHECK-NEXT:  # %bb.1: # %cond.false
+; CHECK-NEXT:    mv a0, a2
+; CHECK-NEXT:  .LBB1_2: # %cond.end
+; CHECK-NEXT:    ret
+entry:
+  br i1 %cnd, label %cond.end, label %cond.false
+
+cond.false:                                       ; preds = %entry
+  br label %cond.end
+
+cond.end:                                         ; preds = %entry, %cond.false
+  %cond = phi i8 [ %b, %cond.false ], [ %a, %entry ]
+  ret i8 %cond
+}
+
+define i16 @phi_i16(i1 %cnd, i16 %a, i16 %b) {
+; CHECK-LABEL: phi_i16:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    mv a3, a0
+; CHECK-NEXT:    mv a0, a1
+; CHECK-NEXT:    andi a3, a3, 1
+; CHECK-NEXT:    bnez a3, .LBB2_2
+; CHECK-NEXT:  # %bb.1: # %cond.false
+; CHECK-NEXT:    mv a0, a2
+; CHECK-NEXT:  .LBB2_2: # %cond.end
+; CHECK-NEXT:    ret
+entry:
+  br i1 %cnd, label %cond.end, label %cond.false
+
+cond.false:                                       ; preds = %entry
+  br label %cond.end
+
+cond.end:                                         ; preds = %entry, %cond.false
+  %cond = phi i16 [ %b, %cond.false ], [ %a, %entry ]
+  ret i16 %cond
+}
+
+define i32 @phi_i32(i1 %cnd, i32 %a, i32 %b) {
+; CHECK-LABEL: phi_i32:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    mv a3, a0
+; CHECK-NEXT:    mv a0, a1
+; CHECK-NEXT:    andi a3, a3, 1
+; CHECK-NEXT:    bnez a3, .LBB3_2
+; CHECK-NEXT:  # %bb.1: # %cond.false
+; CHECK-NEXT:    mv a0, a2
+; CHECK-NEXT:  .LBB3_2: # %cond.end
+; CHECK-NEXT:    ret
+entry:
+  br i1 %cnd, label %cond.end, label %cond.false
+
+cond.false:                                       ; preds = %entry
+  br label %cond.end
+
+cond.end:                                         ; preds = %entry, %cond.false
+  %cond = phi i32 [ %b, %cond.false ], [ %a, %entry ]
+  ret i32 %cond
+}
+
+define i64 @phi_i64(i1 %cnd, i64 %a, i64 %b) {
+; RV32-LABEL: phi_i64:
+; RV32:       # %bb.0: # %entry
+; RV32-NEXT:    mv a5, a0
+; RV32-NEXT:    mv a0, a1
+; RV32-NEXT:    mv a1, a2
+; RV32-NEXT:    andi a5, a5, 1
+; RV32-NEXT:    bnez a5, .LBB4_2
+; RV32-NEXT:  # %bb.1: # %cond.false
+; RV32-NEXT:    mv a0, a3
+; RV32-NEXT:    mv a1, a4
+; RV32-NEXT:  .LBB4_2: # %cond.end
+; RV32-NEXT:    ret
+;
+; RV64-LABEL: phi_i64:
+; RV64:       # %bb.0: # %entry
+; RV64-NEXT:    mv a3, a0
+; RV64-NEXT:    mv a0, a1
+; RV64-NEXT:    andi a3, a3, 1
+; RV64-NEXT:    bnez a3, .LBB4_2
+; RV64-NEXT:  # %bb.1: # %cond.false
+; RV64-NEXT:    mv a0, a2
+; RV64-NEXT:  .LBB4_2: # %cond.end
+; RV64-NEXT:    ret
+entry:
+  br i1 %cnd, label %cond.end, label %cond.false
+
+cond.false:                                       ; preds = %entry
+  br label %cond.end
+
+cond.end:                                         ; preds = %entry, %cond.false
+  %cond = phi i64 [ %b, %cond.false ], [ %a, %entry ]
+  ret i64 %cond
+}
+
+define ptr @phi_ptr(i1 %cnd, ptr %a, ptr %b) {
+; CHECK-LABEL: phi_ptr:
+; CHECK:       # %bb.0: # %entry
+; CHECK-NEXT:    mv a3, a0
+; CHECK-NEXT:    mv a0, a1
+; CHECK-NEXT:    andi a3, a3, 1
+; CHECK-NEXT:    bnez a3, .LBB5_2
+; CHECK-NEXT:  # %bb.1: # %cond.false
+; CHECK-NEXT:    mv a0, a2
+; CHECK-NEXT:  .LBB5_2: # %cond.end
+; CHECK-NEXT:    ret
+entry:
+  br i1 %cnd, label %cond.end, label %cond.false
+
+cond.false:                                       ; preds = %entry
+  br label %cond.end
+
+cond.end:                                         ; preds = %entry, %cond.false
+  %cond = phi ptr [ %b, %cond.false ], [ %a, %entry ]
+  ret ptr %cond
+}
+
+define float @phi_float(i1 %cnd, float %a, float %b) {
+; CHECKI-LABEL: phi_float:
+; CHECKI:       # %bb.0: # %entry
+; CHECKI-NEXT:    mv a3, a0
+; CHECKI-NEXT:    mv a0, a1
+; CHECKI-NEXT:    andi a3, a3, 1
+; CHECKI-NEXT:    bnez a3, .LBB6_2
+; CHECKI-NEXT:  # %bb.1: # %cond.false
+; CHECKI-NEXT:    mv a0, a2
+; CHECKI-NEXT:  .LBB6_2: # %cond.end
+; CHECKI-NEXT:    ret
+;
+; CHECKIF-LABEL: phi_float:
+; CHECKIF:       # %bb.0: # %entry
+; CHECKIF-NEXT:    andi a0, a0, 1
+; CHECKIF-NEXT:    bnez a0, .LBB6_2
+; CHECKIF-NEXT:  # %bb.1: # %cond.false
+; CHECKIF-NEXT:    fmv.s fa0, fa1
+; CHECKIF-NEXT:  .LBB6_2: # %cond.end
+; CHECKIF-NEXT:    ret
+entry:
+  br i1 %cnd, label %cond.end, label %cond.false
+
+cond.false:                                       ; preds = %entry
+  br label %cond.end
+
+cond.end:                                         ; preds = %entry, %cond.false
+  %cond = phi float [ %b, %cond.false ], [ %a, %entry ]
+  ret float %cond
+}


        


More information about the llvm-commits mailing list