Skip to content

Commit

Permalink
[InstCombine] Use ConstantInt::getSigned to sign extend -2 for large …
Browse files Browse the repository at this point in the history
…types. (#76464)

Using ContantInt::get will zero extend.

Fixes #76441
  • Loading branch information
topperc committed Dec 27, 2023
1 parent a01b58a commit 7f1c8fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,8 +1685,8 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
assert(NotLHS != nullptr && NotRHS != nullptr &&
"isFreeToInvert desynced with getFreelyInverted");
Value *LHSPlusRHS = Builder.CreateAdd(NotLHS, NotRHS);
return BinaryOperator::CreateSub(ConstantInt::get(RHS->getType(), -2),
LHSPlusRHS);
return BinaryOperator::CreateSub(
ConstantInt::getSigned(RHS->getType(), -2), LHSPlusRHS);
}
}

Expand Down
18 changes: 18 additions & 0 deletions llvm/test/Transforms/InstCombine/free-inversion.ll
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ define i8 @sub_2(i8 %a, i1 %c, i8 %x, i8 %y) {
ret i8 %not_ab
}

; Same as above but with a type larger than i64 to make sure we create -2
; correctly.
define i128 @sub_3(i128 %a, i1 %c, i128 %x, i128 %y) {
; CHECK-LABEL: @sub_3(
; CHECK-NEXT: [[TMP1:%.*]] = xor i128 [[Y:%.*]], -124
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[C:%.*]], i128 [[X:%.*]], i128 [[TMP1]]
; CHECK-NEXT: [[TMP3:%.*]] = add i128 [[TMP2]], [[A:%.*]]
; CHECK-NEXT: [[NOT_AB:%.*]] = sub i128 -2, [[TMP3]]
; CHECK-NEXT: ret i128 [[NOT_AB]]
;
%nx = xor i128 %x, -1
%yy = xor i128 %y, 123
%b = select i1 %c, i128 %nx, i128 %yy
%ab = sub i128 %a, %b
%not_ab = xor i128 %ab, -1
ret i128 %not_ab
}

define i8 @sub_fail(i8 %a, i1 %c, i8 %x, i8 %y) {
; CHECK-LABEL: @sub_fail(
; CHECK-NEXT: [[NX:%.*]] = xor i8 [[X:%.*]], -1
Expand Down

0 comments on commit 7f1c8fc

Please sign in to comment.