Skip to content

Commit 5ac89ad

Browse files
committed
[InstCombine] remove unnecessary one-use check from add-xor transform
Pre-conditions seem to be optimal, but we don't need a use check because we are only replacing an add with a sub. https://rise4fun.com/Alive/hzN Pre: (~C1 | C2 == -1) && isPowerOf2(C2+1) %m = and i8 %x, C1 %f = xor i8 %m, C2 %r = add i8 %f, C3 => %r = sub i8 C2 + C3, %m
1 parent a52159a commit 5ac89ad

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,9 +1296,9 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
12961296
return BinaryOperator::CreateAShr(NewShl, ShAmt);
12971297
}
12981298

1299-
// If this is a xor that was canonicalized from a sub, turn it back into
1300-
// a sub and fuse this add with it.
1301-
if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) {
1299+
// If X has no high-bits above the xor mask set:
1300+
// add (xor X, LowMaskC), C --> sub (LowMaskC + C), X
1301+
if ((XorRHS->getValue() + 1).isPowerOf2()) {
13021302
KnownBits LHSKnown = computeKnownBits(XorLHS, 0, &I);
13031303
if ((XorRHS->getValue() | LHSKnown.Zero).isAllOnesValue())
13041304
return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI),

llvm/test/Transforms/InstCombine/sub-xor.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ define i32 @xor_add_extra_use(i32 %x) {
5555
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 31
5656
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[AND]], 31
5757
; CHECK-NEXT: call void @use(i32 [[XOR]])
58-
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i32 [[XOR]], 42
58+
; CHECK-NEXT: [[ADD:%.*]] = sub nuw nsw i32 73, [[AND]]
5959
; CHECK-NEXT: ret i32 [[ADD]]
6060
;
6161
%and = and i32 %x, 31

0 commit comments

Comments
 (0)