[llvm] [ConstraintElim] Decompose xor %a, -1 as -1 - %a in the signed system. (PR #213476)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 1 11:27:38 PDT 2026
https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/213476
InstCombine canonicalizes sub nsw i8 -1, %a -> xor i8 %a, -1. Decompose
the XOR as `sub nsw -1, %a` in the signed system
Alive2 Proof: https://alive2.llvm.org/ce/z/f_YVjH
This triggers quite rarely in C/C++ workloads (no end-to-end changes in
https://github.com/dtcxzyw/llvm-opt-benchmark-nightly/pull/840), found
one instance in Blender. One simple end-to-end C example is
https://clang.godbolt.org/z/G343shYje
This is part of an effort to improve ConstraintElimination support for
IR generated by the Swift compiler, where such patterns are more common
due to a number of signed runtime checks.
>From 9a2dbf5148072907003ce1a15fc1f3fcd119ead0 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sat, 1 Aug 2026 19:24:48 +0100
Subject: [PATCH 1/2] [ConstraintElim] Precommit test
---
.../ConstraintElimination/xor-as-sub.ll | 46 +++++++++++++++++++
1 file changed, 46 insertions(+)
create mode 100644 llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll
diff --git a/llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll b/llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll
new file mode 100644
index 0000000000000..6a91d07d8027d
--- /dev/null
+++ b/llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll
@@ -0,0 +1,46 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
+
+define i1 @xor_neg_one_signed(i16 %count, i16 %i) {
+; CHECK-LABEL: define i1 @xor_neg_one_signed(
+; CHECK-SAME: i16 [[COUNT:%.*]], i16 [[I:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[PRE_0:%.*]] = icmp sgt i16 [[COUNT]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[PRE_0]])
+; CHECK-NEXT: [[PRE_1:%.*]] = icmp sgt i16 [[I]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[PRE_1]])
+; CHECK-NEXT: [[PRE_2:%.*]] = icmp slt i16 [[I]], [[COUNT]]
+; CHECK-NEXT: [[NEG:%.*]] = xor i16 [[I]], -1
+; CHECK-NEXT: [[IDX:%.*]] = add nsw i16 [[COUNT]], [[NEG]]
+; CHECK-NEXT: [[C:%.*]] = icmp slt i16 [[IDX]], [[COUNT]]
+; CHECK-NEXT: ret i1 [[C]]
+;
+entry:
+ %pre.0 = icmp sgt i16 %count, -1
+ call void @llvm.assume(i1 %pre.0)
+ %pre.1 = icmp sgt i16 %i, -1
+ call void @llvm.assume(i1 %pre.1)
+ %pre.2 = icmp slt i16 %i, %count
+ %neg = xor i16 %i, -1
+ %idx = add nsw i16 %count, %neg
+ %c = icmp slt i16 %idx, %count
+ ret i1 %c
+}
+
+define i1 @xor_neg_1_unsigned(i16 %x) {
+; CHECK-LABEL: define i1 @xor_neg_1_unsigned(
+; CHECK-SAME: i16 [[X:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[PRE_0:%.*]] = icmp eq i16 [[X]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[PRE_0]])
+; CHECK-NEXT: [[N:%.*]] = xor i16 [[X]], -1
+; CHECK-NEXT: [[C:%.*]] = icmp ult i16 [[N]], 5
+; CHECK-NEXT: ret i1 [[C]]
+;
+entry:
+ %pre.0 = icmp eq i16 %x, 0
+ call void @llvm.assume(i1 %pre.0)
+ %n = xor i16 %x, -1
+ %c = icmp ult i16 %n, 5
+ ret i1 %c
+}
>From 297f2354fc661827377b9dad475cefb5db0c3b55 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Thu, 30 Jul 2026 15:46:23 +0100
Subject: [PATCH 2/2] [ConstraintElim] Decompose `xor %a, -1` as `-1 - %a` in
the signed system.
InstCombine canonicalizes sub nsw i8 -1, %a -> xor i8 %a, -1. Decompose
the XOR as `sub nsw -1, %a` in the signed system
Alive2 Proof: https://alive2.llvm.org/ce/z/f_YVjH
This triggers quite rarely in C/C++ workloads (no end-to-end changes in
https://github.com/dtcxzyw/llvm-opt-benchmark-nightly/pull/840), found
one instance in Blender. One simple end-to-end C example is
https://clang.godbolt.org/z/G343shYje
This is part of an effort to improve ConstraintElimination support for
IR generated by the Swift compiler, where such patterns are more common
due to a number of signed runtime checks.
---
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp | 8 ++++++++
llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll | 3 +--
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 8b7018f6c8433..3d5ef33318c50 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -552,6 +552,14 @@ static Decomposition decompose(Value *V, const ConstraintInfo &Info,
return V;
}
+ // `xor %x, -1` is equivalent to `sub nsw -1, %x`.
+ if (match(V, m_Not(m_Value(Op0)))) {
+ Decomposition Result(-1);
+ if (!Result.sub(decompose(Op0, Info, IsSigned, DL)))
+ return Result;
+ return V;
+ }
+
if (match(V, m_NSWSub(m_Value(Op0), m_Value(Op1)))) {
auto ResA = decompose(Op0, Info, IsSigned, DL);
auto ResB = decompose(Op1, Info, IsSigned, DL);
diff --git a/llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll b/llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll
index 6a91d07d8027d..d20cf3df23572 100644
--- a/llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll
+++ b/llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll
@@ -12,8 +12,7 @@ define i1 @xor_neg_one_signed(i16 %count, i16 %i) {
; CHECK-NEXT: [[PRE_2:%.*]] = icmp slt i16 [[I]], [[COUNT]]
; CHECK-NEXT: [[NEG:%.*]] = xor i16 [[I]], -1
; CHECK-NEXT: [[IDX:%.*]] = add nsw i16 [[COUNT]], [[NEG]]
-; CHECK-NEXT: [[C:%.*]] = icmp slt i16 [[IDX]], [[COUNT]]
-; CHECK-NEXT: ret i1 [[C]]
+; CHECK-NEXT: ret i1 true
;
entry:
%pre.0 = icmp sgt i16 %count, -1
More information about the llvm-commits
mailing list