[llvm] [GISel] import pattern `(A-(B-C)) to A+(C-B)` (PR #181676)
Luisa Cicolini via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 20 14:00:25 PST 2026
https://github.com/luisacicolini updated https://github.com/llvm/llvm-project/pull/181676
>From 2b7651fa752564bfbf0a884d2f3356dc323035d9 Mon Sep 17 00:00:00 2001
From: luisacicolini <luisacicolini at gmail.com>
Date: Mon, 16 Feb 2026 14:40:50 +0000
Subject: [PATCH 1/4] chore: add
---
.../include/llvm/Target/GlobalISel/Combine.td | 9 +++++++
.../AArch64/GlobalISel/combine-integer.mir | 27 +++++++++++++++++++
2 files changed, 36 insertions(+)
diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index f5c940bffc8fb..151e261136c7b 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -1938,6 +1938,14 @@ def APlusBMinusAplusC : GICombineRule<
(G_SUB $sub1, $B, $add1),
(G_ADD $root, $A, $sub1)),
(apply (G_SUB $root, $B, $C))>;
+
+// fold (A-(B-C)) to A+(C-B)
+def AMinusBMinusC : GICombineRule<
+ (defs root:$root),
+ (match (G_SUB $sub1, $B, $C),
+ (G_SUB $root, $A, $sub1)),
+ (apply (G_SUB $add, $C, $B),
+ (G_ADD $root, $A, $add))>;
// fold (A+(B-(C+A))) to (B-C)
def APlusBMinusCPlusA : GICombineRule<
@@ -2007,6 +2015,7 @@ def integer_reassoc_combines: GICombineGroup<[
AMinusBPlusCMinusA,
AMinusBPlusBMinusC,
APlusBMinusAplusC,
+ AMinusBMinusC,
APlusBMinusCPlusA,
APlusC1MinusC2,
C2MinusAPlusC1,
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
index c9b24ad75ce27..f48af87f97df8 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
@@ -413,3 +413,30 @@ body: |
$x0 = COPY %add
RET_ReallyLR implicit $x0
+
+...
+---
+name: AMinusBMinusC
+body: |
+ bb.0:
+ liveins: $x0, $x1, $x2
+
+ ; CHECK-LABEL: name: AMinusBMinusC
+ ; CHECK: liveins: $x0, $x1, $x2
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %a:_(s64) = COPY $x0
+ ; CHECK-NEXT: %b:_(s64) = COPY $x1
+ ; CHECK-NEXT: %c:_(s64) = COPY $x2
+ ; CHECK-NEXT: [[SUB:%[0-9]+]]:_(s64) = G_SUB %c, %b
+ ; CHECK-NEXT: %sub2:_(s64) = G_ADD %a, [[SUB]]
+ ; CHECK-NEXT: $x0 = COPY %sub2(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
+ %mul:_(s64) = G_MUL %a, %sub1
+ $x0 = COPY %sub2
+ RET_ReallyLR implicit $x0
+
>From 92086b90f48ae72e262c5092bf54ca106981646d Mon Sep 17 00:00:00 2001
From: luisacicolini <luisacicolini at gmail.com>
Date: Mon, 16 Feb 2026 14:47:40 +0000
Subject: [PATCH 2/4] chore: add tests
---
.../include/llvm/Target/GlobalISel/Combine.td | 3 +-
.../AArch64/GlobalISel/combine-integer.mir | 29 ++++++++++++++++++-
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index 151e261136c7b..ab0044693de13 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -1943,7 +1943,8 @@ def APlusBMinusAplusC : GICombineRule<
def AMinusBMinusC : GICombineRule<
(defs root:$root),
(match (G_SUB $sub1, $B, $C),
- (G_SUB $root, $A, $sub1)),
+ (G_SUB $root, $A, $sub1),
+ [{ return MRI.hasOneNonDBGUse(${sub1}.getReg()); }]),
(apply (G_SUB $add, $C, $B),
(G_ADD $root, $A, $add))>;
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
index f48af87f97df8..9195cce51ac6f 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
@@ -413,6 +413,33 @@ body: |
$x0 = COPY %add
RET_ReallyLR implicit $x0
+...
+---
+name: AMinusBMinusC_oneUse
+body: |
+ bb.0:
+ liveins: $x0, $x1, $x2
+
+
+ ; CHECK-LABEL: name: AMinusBMinusC_oneUse
+ ; CHECK: liveins: $x0, $x1, $x2
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %a:_(s64) = COPY $x0
+ ; CHECK-NEXT: %b:_(s64) = COPY $x1
+ ; CHECK-NEXT: %c:_(s64) = COPY $x2
+ ; CHECK-NEXT: %sub1:_(s64) = G_SUB %b, %c
+ ; CHECK-NEXT: %sub2:_(s64) = G_SUB %a, %sub1
+ ; CHECK-NEXT: %mul:_(s64) = G_MUL %sub2, %sub1
+ ; CHECK-NEXT: $x0 = COPY %mul(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
+ %mul:_(s64) = G_MUL %sub2, %sub1
+ $x0 = COPY %mul
+ RET_ReallyLR implicit $x0
...
---
@@ -421,6 +448,7 @@ body: |
bb.0:
liveins: $x0, $x1, $x2
+
; CHECK-LABEL: name: AMinusBMinusC
; CHECK: liveins: $x0, $x1, $x2
; CHECK-NEXT: {{ $}}
@@ -436,7 +464,6 @@ body: |
%c:_(s64) = COPY $x2
%sub1:_(s64) = G_SUB %b, %c
%sub2:_(s64) = G_SUB %a, %sub1
- %mul:_(s64) = G_MUL %a, %sub1
$x0 = COPY %sub2
RET_ReallyLR implicit $x0
>From 706bfd6dfe6f540e2354c9d6b24824f791c29d73 Mon Sep 17 00:00:00 2001
From: luisacicolini <luisacicolini at gmail.com>
Date: Mon, 16 Feb 2026 17:10:27 +0000
Subject: [PATCH 3/4] chore: fix tests
---
llvm/test/CodeGen/AArch64/neg-selects.ll | 4 +---
llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll | 4 ++--
2 files changed, 3 insertions(+), 5 deletions(-)
diff --git a/llvm/test/CodeGen/AArch64/neg-selects.ll b/llvm/test/CodeGen/AArch64/neg-selects.ll
index b643ee7670e42..f656fc2e62040 100644
--- a/llvm/test/CodeGen/AArch64/neg-selects.ll
+++ b/llvm/test/CodeGen/AArch64/neg-selects.ll
@@ -35,9 +35,7 @@ define i32 @negneg_select_nega(i32 %a, i32 %b, i1 %bb) {
; CHECK-GI: // %bb.0:
; CHECK-GI-NEXT: and w8, w2, #0x1
; CHECK-GI-NEXT: tst w8, #0x1
-; CHECK-GI-NEXT: csneg w8, w1, w0, eq
-; CHECK-GI-NEXT: neg w8, w8
-; CHECK-GI-NEXT: neg w0, w8
+; CHECK-GI-NEXT: csneg w0, w1, w0, eq
; CHECK-GI-NEXT: ret
%nega = sub i32 0, %a
%sel = select i1 %bb, i32 %nega, i32 %b
diff --git a/llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll b/llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll
index 01aea72d77114..8ce2e520cef83 100644
--- a/llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll
+++ b/llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll
@@ -2849,9 +2849,9 @@ define <8 x i16> @pr149380(<4 x i16> %u1, <1 x i64> %u2, <8 x i16> %vqshlu_n169)
; CHECK-GI-LABEL: pr149380:
; CHECK-GI: // %bb.0: // %entry
; CHECK-GI-NEXT: movi v0.2d, #0xffffffffffffffff
-; CHECK-GI-NEXT: neg v1.8h, v2.8h
+; CHECK-GI-NEXT: movi v1.2d, #0000000000000000
; CHECK-GI-NEXT: movi v3.8h, #1
-; CHECK-GI-NEXT: neg v1.8h, v1.8h
+; CHECK-GI-NEXT: add v1.8h, v1.8h, v2.8h
; CHECK-GI-NEXT: sub v0.8h, v0.8h, v2.8h
; CHECK-GI-NEXT: and v1.16b, v1.16b, v2.16b
; CHECK-GI-NEXT: and v0.16b, v0.16b, v3.16b
>From 979da9583da8ea052eb76177a07007e0ee9bdc84 Mon Sep 17 00:00:00 2001
From: luisacicolini <luisacicolini at gmail.com>
Date: Fri, 20 Feb 2026 22:00:10 +0000
Subject: [PATCH 4/4] chore: one more test
---
.../AArch64/GlobalISel/combine-integer.mir | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
index 9195cce51ac6f..1100997321a09 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
@@ -441,6 +441,35 @@ body: |
$x0 = COPY %mul
RET_ReallyLR implicit $x0
+...
+---
+name: AMinusBMinusC_multi
+body: |
+ bb.0:
+ liveins: $x0, $x1, $x2
+
+ ; CHECK-LABEL: name: AMinusBMinusC_multi
+ ; CHECK: liveins: $x0, $x1, $x2
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %a:_(s64) = COPY $x0
+ ; CHECK-NEXT: %b:_(s64) = COPY $x1
+ ; CHECK-NEXT: %c:_(s64) = COPY $x2
+ ; CHECK-NEXT: %sub1:_(s64) = G_SUB %b, %c
+ ; CHECK-NEXT: %sub2:_(s64) = G_SUB %a, %sub1
+ ; CHECK-NEXT: %mul:_(s64) = G_MUL %sub2, %sub1
+ ; CHECK-NEXT: %add:_(s64) = G_ADD %mul, %sub1
+ ; CHECK-NEXT: $x0 = COPY %add(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
+ %mul:_(s64) = G_MUL %sub2, %sub1
+ %add:_(s64) = G_ADD %mul, %sub1
+ $x0 = COPY %add
+ RET_ReallyLR implicit $x0
+
...
---
name: AMinusBMinusC
More information about the llvm-commits
mailing list