[llvm] 1c48e72 - [GlobalISel] Add integer combines from SelectionDAG (#181126)

via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 26 07:07:00 PDT 2026


Author: Osman Yasar
Date: 2026-03-26T14:06:54Z
New Revision: 1c48e728a2d3cccd4bd395a8862a98a306a68906

URL: https://github.com/llvm/llvm-project/commit/1c48e728a2d3cccd4bd395a8862a98a306a68906
DIFF: https://github.com/llvm/llvm-project/commit/1c48e728a2d3cccd4bd395a8862a98a306a68906.diff

LOG: [GlobalISel] Add integer combines from SelectionDAG (#181126)

This PR adds the patterns` // fold (A - (0 - B)) to (A + B)` and `//
fold A - (A - B) -> B` from SelectionDAG:
https://github.com/llvm/llvm-project/blob/838be78e44cd1f70006eb508bfc925e3e56aac03/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp#L4312

These rewrites should not need HasOneUse checks, since even with
multiple uses, they cannot increase the instruction count.

Added: 
    

Modified: 
    llvm/include/llvm/Target/GlobalISel/Combine.td
    llvm/test/CodeGen/AArch64/GlobalISel/combine-integer-ll.ll
    llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
    llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index 959cd8b083d52..fd3bd755f58c5 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -1985,6 +1985,20 @@ def APlusBMinusCPlusA : GICombineRule<
           (G_SUB $sub1, $B, $add1),
           (G_ADD $root, $A, $sub1)),
    (apply (G_SUB $root, $B, $C))>;
+  
+// fold (A - (0 - B)) to (A + B)
+def AMinusZeroMinusB : GICombineRule<
+   (defs root:$root),
+   (match (G_SUB $sub1, 0, $B),
+          (G_SUB $root, $A, $sub1)),
+   (apply (G_ADD $root, $A, $B))>;
+
+// fold A - (A - B) -> B
+def AMinusBMinusA: GICombineRule<
+   (defs root:$root),
+   (match (G_SUB $add, $A, $B),
+          (G_SUB $root, $A, $add)),
+   (apply (GIReplaceReg $root, $B))>;
    
 // fold (not (add X, -1)) -> (sub 0, X)
 def NotAPlusNegOne: GICombineRule<
@@ -2056,6 +2070,8 @@ def integer_reassoc_combines: GICombineGroup<[
   APlusBMinusAplusC,
   AMinusBMinusC,
   APlusBMinusCPlusA,
+  AMinusZeroMinusB,
+  AMinusBMinusA,
   NotAPlusNegOne,
   APlusC1MinusC2,
   C2MinusAPlusC1,

diff  --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer-ll.ll b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer-ll.ll
index 44e3ca6330f53..0a94dc25bd5d6 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer-ll.ll
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer-ll.ll
@@ -66,6 +66,56 @@ entry:
   ret i64 %add1
 }
 
+define i64 @AMinusZeroMinusB(i64 %a, i64 %b) {
+; CHECK-LABEL: AMinusZeroMinusB:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    add x0, x0, x1
+; CHECK-NEXT:    ret
+
+entry:
+; fold (A - (0 - B)) to (A + B)
+  %sub1 = sub i64 0, %b
+  %sub2 = sub i64 %a, %sub1
+  ret i64 %sub2
+}
+
+define <4 x i16> @AMinusZeroMinusB_vector(<4 x i16> %a, <4 x i16> %b) {
+; CHECK-LABEL: AMinusZeroMinusB_vector:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    add v0.4h, v0.4h, v1.4h
+; CHECK-NEXT:    ret
+
+entry:
+; fold (A - (0 - B)) to (A + B)
+  %sub1 = sub <4 x i16> <i16 0, i16 0, i16 0, i16 0>, %b
+  %sub2 = sub <4 x i16> %a, %sub1
+  ret  <4 x i16> %sub2
+}
+
+define i64 @AMinusBMinusA(i64 %a, i64 %b) {
+; CHECK-LABEL: AMinusBMinusA:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    mov x0, x1
+; CHECK-NEXT:    ret
+entry:
+; fold A - (A - B) -> B
+  %sub1 = sub i64 %a, %b
+  %sub2 = sub i64 %a, %sub1
+  ret i64 %sub2
+}
+
+define <4 x i16> @AMinusBMinusA_vec(<4 x i16> %a, <4 x i16> %b) {
+; CHECK-LABEL: AMinusBMinusA_vec:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    fmov d0, d1
+; CHECK-NEXT:    ret
+entry:
+; fold A - (A - B) -> B
+  %sub1 = sub <4 x i16> %a, %b
+  %sub2 = sub <4 x i16> %a, %sub1
+  ret <4 x i16> %sub2
+}
+
 ;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
 ; CHECK-GI: {{.*}}
 ; CHECK-SD: {{.*}}

diff  --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
index 1100997321a09..f77ab013e20f2 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-integer.mir
@@ -496,3 +496,92 @@ body:             |
     $x0 = COPY %sub2
     RET_ReallyLR implicit $x0
 
+...
+---
+name:   AMinusZeroMinusB
+body:             |
+  bb.0:
+    liveins: $w0, $w1
+
+
+    ; CHECK-LABEL: name: AMinusZeroMinusB
+    ; CHECK: liveins: $w0, $w1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: %a:_(s64) = COPY $x0
+    ; CHECK-NEXT: %b:_(s64) = COPY $x1
+    ; CHECK-NEXT: %sub2:_(s64) = G_ADD %a, %b
+    ; CHECK-NEXT: $x0 = COPY %sub2(s64)
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    %a:_(s64) = COPY $x0
+    %b:_(s64) = COPY $x1
+    %c1:_(s64) = G_CONSTANT i64 0
+    %sub1:_(s64) = G_SUB %c1, %b
+    %sub2:_(s64) = G_SUB %a, %sub1
+    $x0 = COPY %sub2
+    RET_ReallyLR implicit $x0
+
+...
+---
+name:   AMinusZeroMinusB_BV
+body:             |
+  bb.0:
+    liveins: $q0, $q1
+
+    ; CHECK-LABEL: name: AMinusZeroMinusB_BV
+    ; CHECK: liveins: $q0, $q1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: %a:_(<4 x s32>) = COPY $q0
+    ; CHECK-NEXT: %b:_(<4 x s32>) = COPY $q1
+    ; CHECK-NEXT: %sub2:_(<4 x s32>) = G_ADD %a, %b
+    ; CHECK-NEXT: $q0 = COPY %sub2(<4 x s32>)
+    ; CHECK-NEXT: RET_ReallyLR implicit $q0
+    %a:_(<4 x s32>) = COPY $q0
+    %b:_(<4 x s32>) = COPY $q1
+    %c:_(s32) = G_CONSTANT i32 0
+    %c1:_(<4 x s32>) = G_BUILD_VECTOR %c(s32), %c(s32), %c(s32), %c(s32)
+    %sub1:_(<4 x s32>) = G_SUB %c1, %b
+    %sub2:_(<4 x s32>) = G_SUB %a, %sub1
+    $q0 = COPY %sub2
+    RET_ReallyLR implicit $q0
+
+...
+---
+name:   AMinusBMinusA
+body:             |
+  bb.0:
+    liveins: $w0, $w1
+
+    ; CHECK-LABEL: name: AMinusBMinusA
+    ; CHECK: liveins: $w0, $w1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: %b:_(s64) = COPY $x1
+    ; CHECK-NEXT: $x0 = COPY %b(s64)
+    ; CHECK-NEXT: RET_ReallyLR implicit $x0
+    %a:_(s64) = COPY $x0
+    %b:_(s64) = COPY $x1
+    %sub1:_(s64) = G_SUB %a, %b
+    %sub2:_(s64) = G_SUB %a, %sub1
+    $x0 = COPY %sub2
+    RET_ReallyLR implicit $x0
+
+...
+---
+name:   AMinusBMinusA_BV
+body:             |
+  bb.0:
+    liveins: $q0, $q1
+
+    ; CHECK-LABEL: name: AMinusBMinusA_BV
+    ; CHECK: liveins: $q0, $q1
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: %b:_(<4 x s32>) = COPY $q1
+    ; CHECK-NEXT: $q0 = COPY %b(<4 x s32>)
+    ; CHECK-NEXT: RET_ReallyLR implicit $q0
+    %a:_(<4 x s32>) = COPY $q0
+    %b:_(<4 x s32>) = COPY $q1
+    %sub1:_(<4 x s32>) = G_SUB %a, %b
+    %sub2:_(<4 x s32>) = G_SUB %a, %sub1
+    $q0 = COPY %sub2
+    RET_ReallyLR implicit $q0
+
+...

diff  --git a/llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll b/llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll
index 6d61b7cf9be9c..e72d79ef45757 100644
--- a/llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll
+++ b/llvm/test/CodeGen/AArch64/neon-bitwise-instructions.ll
@@ -2849,14 +2849,11 @@ 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:    movi v1.2d, #0000000000000000
-; CHECK-GI-NEXT:    movi v3.8h, #1
-; CHECK-GI-NEXT:    add v1.8h, v1.8h, v2.8h
+; CHECK-GI-NEXT:    movi v1.8h, #1
 ; 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
-; CHECK-GI-NEXT:    orr v0.16b, v1.16b, v0.16b
-; CHECK-GI-NEXT:    sqadd v0.8h, v3.8h, v0.8h
+; CHECK-GI-NEXT:    and v0.16b, v0.16b, v1.16b
+; CHECK-GI-NEXT:    orr v0.16b, v2.16b, v0.16b
+; CHECK-GI-NEXT:    sqadd v0.8h, v1.8h, v0.8h
 ; CHECK-GI-NEXT:    ret
 entry:
   %mul.i = mul <8 x i16> %vqshlu_n169, < i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1 >


        


More information about the llvm-commits mailing list