[llvm] [GlobalISel] Add integer_reassoc_combines from SelectionDAG (PR #177931)
Osman Yasar via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 26 04:47:44 PST 2026
https://github.com/osmanyasar05 updated https://github.com/llvm/llvm-project/pull/177931
>From 7b9f40ffcab5c916115ab6a4bb6f68646b12d7ac Mon Sep 17 00:00:00 2001
From: osmanyasar05 <osmanyas05 at gmail.com>
Date: Mon, 26 Jan 2026 10:47:00 +0000
Subject: [PATCH 1/3] [GlobalISel] Add integer_reassoc_combines from
SelectionDAG
---
.../include/llvm/Target/GlobalISel/Combine.td | 17 +++++++
.../AArch64/GlobalISel/combine-integer.mir | 46 +++++++++++++++++++
2 files changed, 63 insertions(+)
diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index a9b4932b2e317..06ae35ac8947d 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -1799,6 +1799,21 @@ shl_of_vscale,
sub_of_vscale,
]>;
+// fold ((A+(B-C))-B) -> A-C
+def APlusBMinusCMinusB : GICombineRule<
+ (defs root:$root),
+ (match (G_SUB $sub1, $B, $C),
+ (G_ADD $add1, $A, $sub1),
+ (G_SUB $root, $add1, $B)),
+ (apply (G_SUB $root, $A, $C))>;
+
+// fold ((A-(B-C))-C) -> A-B
+def AMinusBMinusCMinusC : GICombineRule<
+ (defs root:$root),
+ (match (G_SUB $sub1, $B, $C),
+ (G_SUB $sub2, $A, $sub1),
+ (G_SUB $root, $sub2, $C)),
+ (apply (G_SUB $root, $A, $B))>;
// fold ((0-A) + B) -> B-A
def ZeroMinusAPlusB : GICombineRule<
@@ -1911,6 +1926,8 @@ def AMinusC1PlusC2: GICombineRule<
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
def integer_reassoc_combines: GICombineGroup<[
+ APlusBMinusCMinusB,
+ AMinusBMinusCMinusC,
ZeroMinusAPlusB,
APlusZeroMinusB,
APlusBMinusB,
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
index 5cbff0f0c74cb..c9b24ad75ce27 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
@@ -2,6 +2,52 @@
# RUN: llc -mtriple aarch64 -run-pass=aarch64-prelegalizer-combiner %s -o - | FileCheck %s
+---
+name: APlusBMinusCMinusB
+body: |
+ bb.0:
+ liveins: $x0, $x1, $x2
+ ; CHECK-LABEL: name: APlusBMinusCMinusB
+ ; CHECK: liveins: $x0, $x1, $x2
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %a:_(s64) = COPY $x0
+ ; CHECK-NEXT: %c:_(s64) = COPY $x2
+ ; CHECK-NEXT: %sub:_(s64) = G_SUB %a, %c
+ ; CHECK-NEXT: $x0 = COPY %sub(s64)
+ ; CHECK-NEXT: RET_ReallyLR implicit $x0
+ %a:_(s64) = COPY $x0
+ %b:_(s64) = COPY $x1
+ %c:_(s64) = COPY $x2
+ %sub1:_(s64) = G_SUB %b, %c
+ %add1:_(s64) = G_ADD %a, %sub1
+ %sub:_(s64) = G_SUB %add1, %b
+ $x0 = COPY %sub
+ RET_ReallyLR implicit $x0
+
+...
+---
+name: AMinusBMinusCMinusC
+body: |
+ bb.0:
+ liveins: $x0, $x1, $x2
+ ; CHECK-LABEL: name: AMinusBMinusCMinusC
+ ; CHECK: liveins: $x0, $x1, $x2
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %a:_(s64) = COPY $x0
+ ; CHECK-NEXT: %b:_(s64) = COPY $x1
+ ; CHECK-NEXT: %sub:_(s64) = G_SUB %a, %b
+ ; CHECK-NEXT: $x0 = COPY %sub(s64)
+ ; CHECK-NEXT: RET_ReallyLR implicit $x0
+ %a:_(s64) = COPY $x0
+ %b:_(s64) = COPY $x1
+ %c:_(s64) = COPY $x2
+ %sub1:_(s64) = G_SUB %b, %c
+ %sub2:_(s64) = G_SUB %a, %sub1
+ %sub:_(s64) = G_SUB %sub2, %c
+ $x0 = COPY %sub
+ RET_ReallyLR implicit $x0
+
+...
---
name: ZeroMinusAPlusB
body: |
>From 93a4ac047224f459216b35b4392e86693f95070a Mon Sep 17 00:00:00 2001
From: osmanyasar05 <osmanyas05 at gmail.com>
Date: Mon, 26 Jan 2026 11:33:42 +0000
Subject: [PATCH 2/3] comments
---
llvm/include/llvm/Target/GlobalISel/Combine.td | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index 06ae35ac8947d..d77bf9e9aa632 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -1799,7 +1799,7 @@ shl_of_vscale,
sub_of_vscale,
]>;
-// fold ((A+(B-C))-B) -> A-C
+// fold ((A + (B - C)) - B) -> A-C
def APlusBMinusCMinusB : GICombineRule<
(defs root:$root),
(match (G_SUB $sub1, $B, $C),
@@ -1807,7 +1807,7 @@ def APlusBMinusCMinusB : GICombineRule<
(G_SUB $root, $add1, $B)),
(apply (G_SUB $root, $A, $C))>;
-// fold ((A-(B-C))-C) -> A-B
+// fold ((A - (B - C)) - C) -> A-B
def AMinusBMinusCMinusC : GICombineRule<
(defs root:$root),
(match (G_SUB $sub1, $B, $C),
>From 3d440b616487a25a5552846d1f67d81316060d8d Mon Sep 17 00:00:00 2001
From: Osman Yasar <37121924+osmanyasar05 at users.noreply.github.com>
Date: Mon, 26 Jan 2026 12:47:34 +0000
Subject: [PATCH 3/3] Fix comments formatting in Combine.td
---
llvm/include/llvm/Target/GlobalISel/Combine.td | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index d77bf9e9aa632..06ae35ac8947d 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -1799,7 +1799,7 @@ shl_of_vscale,
sub_of_vscale,
]>;
-// fold ((A + (B - C)) - B) -> A-C
+// fold ((A+(B-C))-B) -> A-C
def APlusBMinusCMinusB : GICombineRule<
(defs root:$root),
(match (G_SUB $sub1, $B, $C),
@@ -1807,7 +1807,7 @@ def APlusBMinusCMinusB : GICombineRule<
(G_SUB $root, $add1, $B)),
(apply (G_SUB $root, $A, $C))>;
-// fold ((A - (B - C)) - C) -> A-B
+// fold ((A-(B-C))-C) -> A-B
def AMinusBMinusCMinusC : GICombineRule<
(defs root:$root),
(match (G_SUB $sub1, $B, $C),
More information about the llvm-commits
mailing list