Skip to content

Commit fe67255

Browse files
committedFeb 18, 2017
[InstSimplify] add nsw/nuw (xor X, signbit), signbit --> X
The change to InstCombine in: https://reviews.llvm.org/D29729 ...exposes this missing fold in InstSimplify, so adding this first to avoid a regression. llvm-svn: 295573
1 parent 308eb22 commit fe67255

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed
 

‎llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,19 @@ static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
557557
return Y;
558558

559559
// X + ~X -> -1 since ~X = -X-1
560+
Type *Ty = Op0->getType();
560561
if (match(Op0, m_Not(m_Specific(Op1))) ||
561562
match(Op1, m_Not(m_Specific(Op0))))
562-
return Constant::getAllOnesValue(Op0->getType());
563+
return Constant::getAllOnesValue(Ty);
564+
565+
// add nsw/nuw (xor Y, signbit), signbit --> Y
566+
// The no-wrapping add guarantees that the top bit will be set by the add.
567+
// Therefore, the xor must be clearing the already set sign bit of Y.
568+
Constant *SignBit =
569+
ConstantInt::get(Ty, APInt::getSignBit(Ty->getScalarSizeInBits()));
570+
if ((isNSW || isNUW) && match(Op1, m_Specific(SignBit)) &&
571+
match(Op0, m_Xor(m_Value(Y), m_Specific(SignBit))))
572+
return Y;
563573

564574
/// i1 add -> xor.
565575
if (MaxRecurse && Op0->getType()->isIntegerTy(1))

‎llvm/test/Transforms/InstCombine/add.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ define i32 @test19(i1 %C) {
244244
ret i32 %V
245245
}
246246

247-
; Add of sign bit -> xor of sign bit.
247+
; This is an InstSimplify fold, but test it here to make sure that
248+
; InstCombine does not prevent the fold.
249+
; With NSW, add of sign bit -> or of sign bit.
248250

249251
define i32 @test20(i32 %x) {
250252
; CHECK-LABEL: @test20(

‎llvm/test/Transforms/InstSimplify/AndOrXor.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
define <2 x i32> @add_nsw_signbit(<2 x i32> %x) {
77
; CHECK-LABEL: @add_nsw_signbit(
8-
; CHECK-NEXT: [[Y:%.*]] = xor <2 x i32> %x, <i32 -2147483648, i32 -2147483648>
9-
; CHECK-NEXT: [[Z:%.*]] = add nsw <2 x i32> [[Y]], <i32 -2147483648, i32 -2147483648>
10-
; CHECK-NEXT: ret <2 x i32> [[Z]]
8+
; CHECK-NEXT: ret <2 x i32> %x
119
;
1210
%y = xor <2 x i32> %x, <i32 -2147483648, i32 -2147483648>
1311
%z = add nsw <2 x i32> %y, <i32 -2147483648, i32 -2147483648>
@@ -18,9 +16,7 @@ define <2 x i32> @add_nsw_signbit(<2 x i32> %x) {
1816

1917
define <2 x i5> @add_nuw_signbit(<2 x i5> %x) {
2018
; CHECK-LABEL: @add_nuw_signbit(
21-
; CHECK-NEXT: [[Y:%.*]] = xor <2 x i5> %x, <i5 -16, i5 -16>
22-
; CHECK-NEXT: [[Z:%.*]] = add nuw <2 x i5> [[Y]], <i5 -16, i5 -16>
23-
; CHECK-NEXT: ret <2 x i5> [[Z]]
19+
; CHECK-NEXT: ret <2 x i5> %x
2420
;
2521
%y = xor <2 x i5> %x, <i5 -16, i5 -16>
2622
%z = add nuw <2 x i5> %y, <i5 -16, i5 -16>

0 commit comments

Comments
 (0)
Please sign in to comment.