Skip to content

Commit f112e46

Browse files
committed
[InstCombine] Don't transform sub X, ~Y -> add X, -Y unless Y is actually negatable
This combine was previously adding instruction in some cases (see the tests). Closes #72767
1 parent 3af514e commit f112e46

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,11 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
447447
// `xor` is negatible if one of its operands is invertible.
448448
// FIXME: InstCombineInverter? But how to connect Inverter and Negator?
449449
if (auto *C = dyn_cast<Constant>(Ops[1])) {
450-
Value *Xor = Builder.CreateXor(Ops[0], ConstantExpr::getNot(C));
451-
return Builder.CreateAdd(Xor, ConstantInt::get(Xor->getType(), 1),
452-
I->getName() + ".neg");
450+
if (IsTrulyNegation) {
451+
Value *Xor = Builder.CreateXor(Ops[0], ConstantExpr::getNot(C));
452+
return Builder.CreateAdd(Xor, ConstantInt::get(Xor->getType(), 1),
453+
I->getName() + ".neg");
454+
}
453455
}
454456
return nullptr;
455457
}

llvm/test/Analysis/ValueTracking/knownbits-and-or-xor-lowbit.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ define <2 x i1> @sub_XY_and_bit0_is_zero_fail(<2 x i8> %x, <2 x i8> %C) nounwind
106106

107107
define i1 @sub_XY_xor_bit0_is_one_fail(i8 %x, i8 %C) nounwind {
108108
; CHECK-LABEL: @sub_XY_xor_bit0_is_one_fail(
109-
; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[C:%.*]], -2
110-
; CHECK-NEXT: [[C1_NEG:%.*]] = add i8 [[TMP1]], 1
111-
; CHECK-NEXT: [[Y:%.*]] = add i8 [[C1_NEG]], [[X:%.*]]
109+
; CHECK-NEXT: [[C1:%.*]] = xor i8 [[C:%.*]], 1
110+
; CHECK-NEXT: [[Y:%.*]] = sub i8 [[X:%.*]], [[C1]]
112111
; CHECK-NEXT: [[W:%.*]] = xor i8 [[Y]], [[X]]
113112
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[W]], 10
114113
; CHECK-NEXT: ret i1 [[R]]

llvm/test/Transforms/InstCombine/fold-sub-of-not-to-inc-of-add.ll

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ define i32 @p0_scalar(i32 %x, i32 %y) {
2525
define i8 @p0_scalar_not_truly_negatable(i8 %x, i8 %y) {
2626
; CHECK-LABEL: @p0_scalar_not_truly_negatable(
2727
; CHECK-NEXT: [[XX:%.*]] = xor i8 [[X:%.*]], 123
28-
; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[Y:%.*]], -46
29-
; CHECK-NEXT: [[YY_NEG:%.*]] = add i8 [[TMP1]], 1
30-
; CHECK-NEXT: [[R:%.*]] = add i8 [[YY_NEG]], [[XX]]
28+
; CHECK-NEXT: [[YY:%.*]] = xor i8 [[Y:%.*]], 45
29+
; CHECK-NEXT: [[R:%.*]] = sub i8 [[XX]], [[YY]]
3130
; CHECK-NEXT: ret i8 [[R]]
3231
;
3332
%xx = xor i8 %x, 123
@@ -99,8 +98,8 @@ define i32 @n4(i32 %x, i32 %y) {
9998

10099
define i32 @n5_is_not_not(i32 %x, i32 %y) {
101100
; CHECK-LABEL: @n5_is_not_not(
102-
; CHECK-NEXT: [[T0_NEG:%.*]] = add i32 [[X:%.*]], -2147483647
103-
; CHECK-NEXT: [[T1:%.*]] = add i32 [[T0_NEG]], [[Y:%.*]]
101+
; CHECK-NEXT: [[T0:%.*]] = xor i32 [[X:%.*]], 2147483647
102+
; CHECK-NEXT: [[T1:%.*]] = sub i32 [[Y:%.*]], [[T0]]
104103
; CHECK-NEXT: ret i32 [[T1]]
105104
;
106105
%t0 = xor i32 %x, 2147483647 ; not -1
@@ -110,8 +109,8 @@ define i32 @n5_is_not_not(i32 %x, i32 %y) {
110109

111110
define <2 x i32> @n5_is_not_not_vec_splat(<2 x i32> %x, <2 x i32> %y) {
112111
; CHECK-LABEL: @n5_is_not_not_vec_splat(
113-
; CHECK-NEXT: [[T0_NEG:%.*]] = add <2 x i32> [[X:%.*]], <i32 -2147483647, i32 -2147483647>
114-
; CHECK-NEXT: [[T1:%.*]] = add <2 x i32> [[T0_NEG]], [[Y:%.*]]
112+
; CHECK-NEXT: [[T0:%.*]] = xor <2 x i32> [[X:%.*]], <i32 2147483647, i32 2147483647>
113+
; CHECK-NEXT: [[T1:%.*]] = sub <2 x i32> [[Y:%.*]], [[T0]]
115114
; CHECK-NEXT: ret <2 x i32> [[T1]]
116115
;
117116
%t0 = xor <2 x i32> %x, <i32 2147483647, i32 2147483647> ; signmask, but not -1

0 commit comments

Comments
 (0)