[llvm] [InstCombine] Generalize `(A + 1) + ~B` fold to any constant (PR #188271)

Piotr Fusik via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 9 23:49:53 PDT 2026


================
@@ -121,6 +182,145 @@ define i32 @t7(i32 %x, i32 %y) {
   ret i32 %t2
 }
 
+define i32 @t5_5(i32 %x, i32 %y) {
+; CHECK-LABEL: @t5_5(
+; CHECK-NEXT:    [[T0:%.*]] = xor i32 [[X:%.*]], -1
+; CHECK-NEXT:    call void @use32(i32 [[T0]])
+; CHECK-NEXT:    [[TMP1:%.*]] = sub i32 [[Y:%.*]], [[X]]
+; CHECK-NEXT:    [[T2:%.*]] = add i32 [[TMP1]], 4
+; CHECK-NEXT:    ret i32 [[T2]]
+;
+  %t0 = xor i32 %x, -1
+  call void @use32(i32 %t0)
+  %t1 = add i32 %t0, %y
+  %t2 = add i32 %t1, 5
+  ret i32 %t2
+}
----------------
pfusik wrote:

Both adds without the transform could be fold into a `lea`.

https://github.com/llvm/llvm-project/pull/188271


More information about the llvm-commits mailing list