[llvm] [GlobalISel] Add sub_same_val rewrites from SelectionDAG (PR #181134)

Osman Yasar via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 12 05:34:02 PST 2026


https://github.com/osmanyasar05 updated https://github.com/llvm/llvm-project/pull/181134

>From bc9b3a0ca9ab55c3ef9a7028c5f9138b5e47d385 Mon Sep 17 00:00:00 2001
From: osmanyasar05 <osmanyas05 at gmail.com>
Date: Thu, 12 Feb 2026 12:32:35 +0000
Subject: [PATCH 1/2] add same_val rewrites

---
 .../include/llvm/Target/GlobalISel/Combine.td |  8 +++++++-
 .../AArch64/GlobalISel/combine-same-op.mir    | 20 +++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/combine-same-op.mir

diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index f5c940bffc8fb..73b1869cb9deb 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -608,6 +608,12 @@ def binop_same_val: GICombineRule<
   (apply (GIReplaceReg $dst, $src))
 >;
 
+// fold (sub x, x) -> 0
+def sub_same_val: GICombineRule <
+  (defs root:$dst),
+  (match (G_SUB $dst, $x, $x)),
+  (apply (G_CONSTANT $dst, 0))>;
+
 // Fold (0 op x) - > 0
 def binop_left_to_zero_frags : GICombinePatFrag<
   (outs root:$dst, $zero), (ins $rhs),
@@ -2222,7 +2228,7 @@ def undef_combines : GICombineGroup<[undef_to_fp_zero, undef_to_int_zero,
                                      insert_extract_vec_elt_out_of_bounds]>;
 
 def identity_combines : GICombineGroup<[select_same_val, right_identity_zero,
-                                        binop_same_val, binop_left_to_zero,
+                                        binop_same_val, sub_same_val, binop_left_to_zero,
                                         binop_right_to_zero, p2i_to_i2p,
                                         i2p_to_p2i, anyext_trunc_fold,
                                         fneg_fneg_fold, right_identity_one,
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-same-op.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-same-op.mir
new file mode 100644
index 0000000000000..a72ba0c7066f2
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-same-op.mir
@@ -0,0 +1,20 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
+# RUN: llc -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs -mtriple aarch64-unknown-unknown %s -o - | FileCheck %s
+
+---
+name:            sub_same_op
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $x0
+
+    ; CHECK-LABEL: name: sub_same_op
+    ; CHECK: liveins: $x0
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: %x:_(s64) = COPY $x0
+    ; CHECK-NEXT: $x0 = COPY %x(s64)
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    %x:_(s64) = COPY $x0
+    %sub:_(s64) = G_SUB %x, %x
+    $x0 = COPY %sub
+    RET_ReallyLR implicit $x0

>From 0ed02b1d06daf0a014b021a7d6a38720dac8dc96 Mon Sep 17 00:00:00 2001
From: osmanyasar05 <osmanyas05 at gmail.com>
Date: Thu, 12 Feb 2026 13:33:49 +0000
Subject: [PATCH 2/2] fix pattern

---
 llvm/include/llvm/Target/GlobalISel/Combine.td  | 17 ++++++++++++-----
 .../AArch64/GlobalISel/combine-freeze.mir       |  6 ++----
 .../AArch64/GlobalISel/combine-same-op.mir      |  4 +++-
 3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index 73b1869cb9deb..fd7fd605cbbb1 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -608,11 +608,18 @@ def binop_same_val: GICombineRule<
   (apply (GIReplaceReg $dst, $src))
 >;
 
-// fold (sub x, x) -> 0
-def sub_same_val: GICombineRule <
+// Fold x op x -> 0
+def same_val_zero_frags : GICombinePatFrag<
+  (outs root:$dst), (ins $x),
+  !foreach(op,
+           [G_SUB, G_XOR],
+           (pattern (op $dst, $x, $x)))>;
+
+def same_val_zero: GICombineRule<
   (defs root:$dst),
-  (match (G_SUB $dst, $x, $x)),
-  (apply (G_CONSTANT $dst, 0))>;
+  (match (same_val_zero_frags $dst, $x)),
+  (apply (COPY $dst, (GITypeOf<"$x"> 0)))
+>;
 
 // Fold (0 op x) - > 0
 def binop_left_to_zero_frags : GICombinePatFrag<
@@ -2228,7 +2235,7 @@ def undef_combines : GICombineGroup<[undef_to_fp_zero, undef_to_int_zero,
                                      insert_extract_vec_elt_out_of_bounds]>;
 
 def identity_combines : GICombineGroup<[select_same_val, right_identity_zero,
-                                        binop_same_val, sub_same_val, binop_left_to_zero,
+                                        binop_same_val, same_val_zero, binop_left_to_zero,
                                         binop_right_to_zero, p2i_to_i2p,
                                         i2p_to_p2i, anyext_trunc_fold,
                                         fneg_fneg_fold, right_identity_one,
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-freeze.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-freeze.mir
index 1950e602ec83a..fac1ef0242823 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-freeze.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-freeze.mir
@@ -404,10 +404,8 @@ body:             |
     ; CHECK-LABEL: name: freeze_xor_fold_barrier
     ; CHECK: liveins: $w0
     ; CHECK-NEXT: {{  $}}
-    ; CHECK-NEXT: %cst:_(s64) = G_CONSTANT i64 9
-    ; CHECK-NEXT: %c:_(s64) = G_CONSTANT_FOLD_BARRIER %cst
-    ; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s64) = G_XOR %c, %c
-    ; CHECK-NEXT: $x0 = COPY [[XOR]](s64)
+    ; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 0
+    ; CHECK-NEXT: $x0 = COPY [[C]](s64)
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     %0:_(s64) = COPY $x0
     %cst:_(s64) = G_CONSTANT i64 9
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-same-op.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-same-op.mir
index a72ba0c7066f2..11d9586936145 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-same-op.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-same-op.mir
@@ -8,6 +8,7 @@ body: |
   bb.0:
     liveins: $x0
 
+
     ; CHECK-LABEL: name: sub_same_op
     ; CHECK: liveins: $x0
     ; CHECK-NEXT: {{  $}}
@@ -16,5 +17,6 @@ body: |
     ; CHECK-NEXT: RET_ReallyLR implicit $x0
     %x:_(s64) = COPY $x0
     %sub:_(s64) = G_SUB %x, %x
-    $x0 = COPY %sub
+    %add:_(s64) = G_ADD %x, %sub
+    $x0 = COPY %add
     RET_ReallyLR implicit $x0



More information about the llvm-commits mailing list