Skip to content

Commit c495dbb

Browse files
committed
[InstCombine] Simplify add nsw/nuw i1 to or disjoint i1
1 parent 4a07433 commit c495dbb

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,8 +1549,11 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
15491549
I.hasNoUnsignedWrap()))
15501550
return R;
15511551
Type *Ty = I.getType();
1552-
if (Ty->isIntOrIntVectorTy(1))
1552+
if (Ty->isIntOrIntVectorTy(1)) {
1553+
if (I.hasNoUnsignedWrap() || I.hasNoSignedWrap())
1554+
return BinaryOperator::CreateDisjoint(Instruction::Or, LHS, RHS);
15531555
return BinaryOperator::CreateXor(LHS, RHS);
1556+
}
15541557

15551558
// X + X --> X << 1
15561559
if (LHS == RHS) {

llvm/test/Transforms/InstCombine/add2.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,21 @@ define i32 @sub_undemanded_low_bits(i32 %x) {
498498
%shr = lshr i32 %sub, 4
499499
ret i32 %shr
500500
}
501+
502+
define i1 @add_nuw_i1(i1 %x, i1 %y) {
503+
; CHECK-LABEL: @add_nuw_i1(
504+
; CHECK-NEXT: [[Z:%.*]] = or disjoint i1 [[X:%.*]], [[Y:%.*]]
505+
; CHECK-NEXT: ret i1 [[Z]]
506+
;
507+
%z = add nuw i1 %x, %y
508+
ret i1 %z
509+
}
510+
511+
define i1 @add_nsw_i1(i1 %x, i1 %y) {
512+
; CHECK-LABEL: @add_nsw_i1(
513+
; CHECK-NEXT: [[Z:%.*]] = or disjoint i1 [[X:%.*]], [[Y:%.*]]
514+
; CHECK-NEXT: ret i1 [[Z]]
515+
;
516+
%z = add nsw i1 %x, %y
517+
ret i1 %z
518+
}

0 commit comments

Comments
 (0)