[llvm] 0dafeb9 - [GIsel] import `(not (add X, -1)) -> (neg X)` to GlobalISel (#181640)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 19 12:15:05 PST 2026
Author: Luisa Cicolini
Date: 2026-02-19T21:15:00+01:00
New Revision: 0dafeb97a4687c29f5182fa0239c7fa39ee23091
URL: https://github.com/llvm/llvm-project/commit/0dafeb97a4687c29f5182fa0239c7fa39ee23091
DIFF: https://github.com/llvm/llvm-project/commit/0dafeb97a4687c29f5182fa0239c7fa39ee23091.diff
LOG: [GIsel] import `(not (add X, -1)) -> (neg X)` to GlobalISel (#181640)
We port the rewrite `(not (add X, -1)) -> (neg X)` from SelectionDAG to
GlobalISel. I contemplated adding a `G_NEG` primitive initially, but
given that the [SelectionDAG pattern rewrites to a
subtraction](https://github.com/llvm/llvm-project/blob/20aff2085c358d3e112b245494161ddb9ffe0410/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp#L1649),
I implemented the rhs of the rewrite as a subtraction directly,
specifying the comment accordingly.
Added:
Modified:
llvm/include/llvm/Target/GlobalISel/Combine.td
llvm/test/CodeGen/AArch64/GlobalISel/combine-add.mir
llvm/test/CodeGen/AArch64/GlobalISel/combine-addv.mir
Removed:
################################################################################
diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index f5c940bffc8fb..8b427867a4f23 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -1946,6 +1946,14 @@ def APlusBMinusCPlusA : GICombineRule<
(G_SUB $sub1, $B, $add1),
(G_ADD $root, $A, $sub1)),
(apply (G_SUB $root, $B, $C))>;
+
+// fold (not (add X, -1)) -> (sub 0, X)
+def NotAPlusNegOne: GICombineRule<
+ (defs root:$root),
+ (match (G_ADD $add, $A, -1),
+ (G_XOR $root, $add, -1),
+ [{ return MRI.hasOneNonDBGUse(${add}.getReg()); }]),
+ (apply (G_SUB $root, 0, $A))>;
// fold (A+C1)-C2 -> A+(C1-C2)
def APlusC1MinusC2: GICombineRule<
@@ -2008,6 +2016,7 @@ def integer_reassoc_combines: GICombineGroup<[
AMinusBPlusBMinusC,
APlusBMinusAplusC,
APlusBMinusCPlusA,
+ NotAPlusNegOne,
APlusC1MinusC2,
C2MinusAPlusC1,
AMinusC1MinusC2,
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-add.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-add.mir
index 4656dbc4270de..dc1a81f23d733 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-add.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-add.mir
@@ -514,3 +514,54 @@ body: |
$x0 = COPY %mul
RET_ReallyLR implicit $x0
...
+
+---
+name: NotAPlusNegOne
+body: |
+ bb.0:
+ liveins: $x0
+
+ ; CHECK-LABEL: name: NotAPlusNegOne
+ ; CHECK: liveins: $x0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %x:_(s64) = COPY $x0
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 0
+ ; CHECK-NEXT: %xor:_(s64) = G_SUB [[C]], %x
+ ; CHECK-NEXT: $x0 = COPY %xor(s64)
+ ; CHECK-NEXT: RET_ReallyLR implicit $x0
+ %x:_(s64) = COPY $x0
+ %neg_one:_(s64) = G_CONSTANT i64 -1
+ %add:_(s64) = G_ADD %x, %neg_one
+ %xor:_(s64) = G_XOR %add, %neg_one
+ $x0 = COPY %xor
+ RET_ReallyLR implicit $x0
+...
+
+
+---
+name: NotAPlusNegOne_multi
+body: |
+ bb.0:
+ liveins: $x0
+
+
+ ; CHECK-LABEL: name: NotAPlusNegOne_multi
+ ; CHECK: liveins: $x0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %x:_(s64) = COPY $x0
+ ; CHECK-NEXT: %neg_one:_(s64) = G_CONSTANT i64 -1
+ ; CHECK-NEXT: %add:_(s64) = G_ADD %x, %neg_one
+ ; CHECK-NEXT: %xor:_(s64) = G_XOR %add, %neg_one
+ ; CHECK-NEXT: %sub:_(s64) = G_SUB %xor, %add
+ ; CHECK-NEXT: %mul:_(s64) = G_MUL %xor, %sub
+ ; CHECK-NEXT: $x0 = COPY %mul(s64)
+ ; CHECK-NEXT: RET_ReallyLR implicit $x0
+ %x:_(s64) = COPY $x0
+ %neg_one:_(s64) = G_CONSTANT i64 -1
+ %add:_(s64) = G_ADD %x, %neg_one
+ %xor:_(s64) = G_XOR %add, %neg_one
+ %sub:_(s64) = G_SUB %xor, %add
+ %mul:_(s64) = G_MUL %xor, %sub
+ $x0 = COPY %mul
+ RET_ReallyLR implicit $x0
+...
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-addv.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-addv.mir
index 3e4a856aed2ec..9482dc3c41063 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-addv.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-addv.mir
@@ -31,3 +31,29 @@ body: |
RET_ReallyLR implicit $w0
...
+
+---
+name: NotAPlusNegOne_vec
+alignment: 4
+frameInfo:
+ maxAlignment: 1
+machineFunctionInfo: {}
+body: |
+ bb.0:
+ liveins: $x0
+ ; CHECK-LABEL: name: NotAPlusNegOne_vec
+ ; CHECK: liveins: $x0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: %x:_(<4 x s16>) = COPY $x0
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(s16) = G_CONSTANT i16 0
+ ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<4 x s16>) = G_BUILD_VECTOR [[C]](s16), [[C]](s16), [[C]](s16), [[C]](s16)
+ ; CHECK-NEXT: %xor:_(<4 x s16>) = G_SUB [[BUILD_VECTOR]], %x
+ ; CHECK-NEXT: $x0 = COPY %xor(<4 x s16>)
+ %x:_(<4 x s16>) = COPY $x0
+ %neg_one:_(s16) = G_CONSTANT i16 -1
+ %neg_one_vec:_(<4 x s16>) = G_BUILD_VECTOR %neg_one(s16), %neg_one(s16), %neg_one(s16), %neg_one(s16)
+ %add:_(<4 x s16>) = G_ADD %x, %neg_one_vec
+ %xor:_(<4 x s16>) = G_XOR %add, %neg_one_vec
+ $x0 = COPY %xor
+...
+
More information about the llvm-commits
mailing list