Skip to content

Commit

Permalink
[InstCombine] move code to remove repeated constant check; NFCI
Browse files Browse the repository at this point in the history
Also, consolidate tests for this fold in one place.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315745 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rotateright committed Oct 13, 2017
1 parent 9a68752 commit ab26bdd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
15 changes: 7 additions & 8 deletions lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,15 @@ Instruction *InstCombiner::foldAddWithConstant(BinaryOperator &Add) {
return NV;

Value *X;
if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->getScalarSizeInBits() == 1)
// zext(bool) + C -> bool ? C + 1 : C
// zext(bool) + C -> bool ? C + 1 : C
if (match(Op0, m_ZExt(m_Value(X))) &&
X->getType()->getScalarSizeInBits() == 1)
return SelectInst::Create(X, AddOne(Op1C), Op1);

// ~X + C --> (C-1) - X
if (match(Op0, m_Not(m_Value(X))))
return BinaryOperator::CreateSub(SubOne(Op1C), X);

const APInt *C;
if (!match(Op1, m_APInt(C)))
return nullptr;
Expand Down Expand Up @@ -1117,12 +1122,6 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
if (haveNoCommonBitsSet(LHS, RHS, DL, &AC, &I, &DT))
return BinaryOperator::CreateOr(LHS, RHS);

if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
Value *X;
if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
return BinaryOperator::CreateSub(SubOne(CRHS), X);
}

// FIXME: We already did a check for ConstantInt RHS above this.
// FIXME: Is this pattern covered by another fold? No regression tests fail on
// removal.
Expand Down
12 changes: 10 additions & 2 deletions test/Transforms/InstCombine/add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ define i32 @test17(i32 %A) {
; CHECK-NEXT: ret i32 [[C]]
;
%B = xor i32 %A, -1
; == sub int 0, %A
%C = add i32 %B, 1
ret i32 %C
}
Expand All @@ -277,11 +276,20 @@ define i8 @test18(i8 %A) {
; CHECK-NEXT: ret i8 [[C]]
;
%B = xor i8 %A, -1
; == sub ubyte 16, %A
%C = add i8 %B, 17
ret i8 %C
}

define <2 x i64> @test18vec(<2 x i64> %A) {
; CHECK-LABEL: @test18vec(
; CHECK-NEXT: [[ADD:%.*]] = sub <2 x i64> <i64 1, i64 2>, %A
; CHECK-NEXT: ret <2 x i64> [[ADD]]
;
%xor = xor <2 x i64> %A, <i64 -1, i64 -1>
%add = add <2 x i64> %xor, <i64 2, i64 3>
ret <2 x i64> %add
}

define i32 @test19(i1 %C) {
; CHECK-LABEL: @test19(
; CHECK-NEXT: [[V:%.*]] = select i1 %C, i32 1123, i32 133
Expand Down
9 changes: 0 additions & 9 deletions test/Transforms/InstCombine/add2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,6 @@ define <2 x i64> @test7(<2 x i64> %A) {
; CHECK-NEXT: ret <2 x i64> %add
}

define <2 x i64> @test8(<2 x i64> %A) {
%xor = xor <2 x i64> %A, <i64 -1, i64 -1>
%add = add <2 x i64> %xor, <i64 2, i64 3>
ret <2 x i64> %add
; CHECK-LABEL: @test8(
; CHECK-NEXT: %add = sub <2 x i64> <i64 1, i64 2>, %A
; CHECK-NEXT: ret <2 x i64> %add
}

define i16 @test9(i16 %a) {
%b = mul i16 %a, 2
%c = mul i16 %a, 32767
Expand Down

0 comments on commit ab26bdd

Please sign in to comment.