[llvm] [GlobalISel] Constant fold sitofp/uitofp of 0. (PR #65307)

Amara Emerson via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 5 01:39:38 PDT 2023


https://github.com/aemerson created https://github.com/llvm/llvm-project/pull/65307:

None

>From 8abcecbbdcb847ab7b9a02947f393fbda6be3d84 Mon Sep 17 00:00:00 2001
From: Amara Emerson <amara at apple.com>
Date: Fri, 1 Sep 2023 08:07:50 -0700
Subject: [PATCH] [GlobalISel] Constant fold sitofp/uitofp of 0.

---
 .../include/llvm/Target/GlobalISel/Combine.td | 15 ++++++-
 .../combine-constant-fold-itofp-zero.mir      | 43 +++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/combine-constant-fold-itofp-zero.mir

diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index b76f739cdcaa22..80b80cfa914c82 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -524,12 +524,25 @@ def constant_fold_fsqrt : constant_fold_unary_fp_op_rule<G_FSQRT>;
 def constant_fold_flog2 : constant_fold_unary_fp_op_rule<G_FLOG2>;
 def constant_fold_fptrunc : constant_fold_unary_fp_op_rule<G_FPTRUNC>;
 
+// Fold constant zero int to fp conversions.
+class itof_const_zero_fold_rule<Instruction opcode> : GICombineRule <
+  (defs root:$dst),
+  (match (opcode $dst, 0)),
+  // Can't use COPY $dst, 0 here because the 0 operand may be a smaller type
+  // than the destination for itofp.
+  (apply [{ Helper.replaceInstWithFConstant(*${dst}.getParent(), 0.0); }])
+>;
+def itof_const_zero_fold_si : itof_const_zero_fold_rule<G_SITOFP>;
+def itof_const_zero_fold_ui : itof_const_zero_fold_rule<G_UITOFP>;
+
 def constant_fold_fp_ops : GICombineGroup<[
   constant_fold_fneg,
   constant_fold_fabs,
   constant_fold_fsqrt,
   constant_fold_flog2,
-  constant_fold_fptrunc
+  constant_fold_fptrunc,
+  itof_const_zero_fold_si,
+  itof_const_zero_fold_ui
 ]>;
 
 // Fold int2ptr(ptr2int(x)) -> x
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-constant-fold-itofp-zero.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-constant-fold-itofp-zero.mir
new file mode 100644
index 00000000000000..b696cdf3cb28a8
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-constant-fold-itofp-zero.mir
@@ -0,0 +1,43 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
+# RUN: llc -mtriple aarch64 -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s
+
+---
+name:            sitofp
+liveins:
+  - { reg: '$d0' }
+body:             |
+  bb.1.entry:
+    liveins: $d0
+
+    ; CHECK-LABEL: name: sitofp
+    ; CHECK: liveins: $d0
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: %res:_(s64) = G_FCONSTANT double 0.000000e+00
+    ; CHECK-NEXT: $d0 = COPY %res(s64)
+    ; CHECK-NEXT: RET_ReallyLR implicit $d0
+    %a:_(s64) = G_CONSTANT i64 0
+    %res:_(s64) = G_SITOFP %a
+    $d0 = COPY %res(s64)
+    RET_ReallyLR implicit $d0
+
+...
+---
+name:            uitofp
+liveins:
+  - { reg: '$d0' }
+body:             |
+  bb.1.entry:
+    liveins: $d0
+
+    ; CHECK-LABEL: name: uitofp
+    ; CHECK: liveins: $d0
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: %res:_(s64) = G_FCONSTANT double 0.000000e+00
+    ; CHECK-NEXT: $d0 = COPY %res(s64)
+    ; CHECK-NEXT: RET_ReallyLR implicit $d0
+    %a:_(s64) = G_CONSTANT i64 0
+    %res:_(s64) = G_UITOFP %a
+    $d0 = COPY %res(s64)
+    RET_ReallyLR implicit $d0
+
+...



More information about the llvm-commits mailing list