Skip to content

Commit 7f1c8fc

Browse files
authored
[InstCombine] Use ConstantInt::getSigned to sign extend -2 for large types. (llvm#76464)
Using ContantInt::get will zero extend. Fixes llvm#76441
1 parent a01b58a commit 7f1c8fc

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,8 +1685,8 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
16851685
assert(NotLHS != nullptr && NotRHS != nullptr &&
16861686
"isFreeToInvert desynced with getFreelyInverted");
16871687
Value *LHSPlusRHS = Builder.CreateAdd(NotLHS, NotRHS);
1688-
return BinaryOperator::CreateSub(ConstantInt::get(RHS->getType(), -2),
1689-
LHSPlusRHS);
1688+
return BinaryOperator::CreateSub(
1689+
ConstantInt::getSigned(RHS->getType(), -2), LHSPlusRHS);
16901690
}
16911691
}
16921692

llvm/test/Transforms/InstCombine/free-inversion.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ define i8 @sub_2(i8 %a, i1 %c, i8 %x, i8 %y) {
133133
ret i8 %not_ab
134134
}
135135

136+
; Same as above but with a type larger than i64 to make sure we create -2
137+
; correctly.
138+
define i128 @sub_3(i128 %a, i1 %c, i128 %x, i128 %y) {
139+
; CHECK-LABEL: @sub_3(
140+
; CHECK-NEXT: [[TMP1:%.*]] = xor i128 [[Y:%.*]], -124
141+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[C:%.*]], i128 [[X:%.*]], i128 [[TMP1]]
142+
; CHECK-NEXT: [[TMP3:%.*]] = add i128 [[TMP2]], [[A:%.*]]
143+
; CHECK-NEXT: [[NOT_AB:%.*]] = sub i128 -2, [[TMP3]]
144+
; CHECK-NEXT: ret i128 [[NOT_AB]]
145+
;
146+
%nx = xor i128 %x, -1
147+
%yy = xor i128 %y, 123
148+
%b = select i1 %c, i128 %nx, i128 %yy
149+
%ab = sub i128 %a, %b
150+
%not_ab = xor i128 %ab, -1
151+
ret i128 %not_ab
152+
}
153+
136154
define i8 @sub_fail(i8 %a, i1 %c, i8 %x, i8 %y) {
137155
; CHECK-LABEL: @sub_fail(
138156
; CHECK-NEXT: [[NX:%.*]] = xor i8 [[X:%.*]], -1

0 commit comments

Comments
 (0)