[llvm] 12e4921 - [GlobalISel] Constant fold sitofp/uitofp of 0. (#65307)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 5 02:34:01 PDT 2023
Author: Amara Emerson
Date: 2023-09-05T17:33:57+08:00
New Revision: 12e492170969939bc0d32975bb19e9e23b4a0146
URL: https://github.com/llvm/llvm-project/commit/12e492170969939bc0d32975bb19e9e23b4a0146
DIFF: https://github.com/llvm/llvm-project/commit/12e492170969939bc0d32975bb19e9e23b4a0146.diff
LOG: [GlobalISel] Constant fold sitofp/uitofp of 0. (#65307)
Added:
llvm/test/CodeGen/AArch64/GlobalISel/combine-constant-fold-itofp-zero.mir
Modified:
llvm/include/llvm/Target/GlobalISel/Combine.td
Removed:
################################################################################
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