[llvm] db8d8fd - [ConstraintElim] Decompose xor %a, -1 as -1 - %a in the signed system. (#213476)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 2 12:51:22 PDT 2026


Author: Florian Hahn
Date: 2026-08-02T19:51:17Z
New Revision: db8d8fdd9831d2b8b2fdeb65498a41e7a8a7456c

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

LOG: [ConstraintElim] Decompose xor %a, -1 as -1 - %a in the signed system. (#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.

PR: https://github.com/llvm/llvm-project/pull/213476

Added: 
    llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll

Modified: 
    llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 0705c97a9455f..9486ea958dfaa 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
new file mode 100644
index 0000000000000..d20cf3df23572
--- /dev/null
+++ b/llvm/test/Transforms/ConstraintElimination/xor-as-sub.ll
@@ -0,0 +1,45 @@
+; 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:    ret i1 true
+;
+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
+}


        


More information about the llvm-commits mailing list