Skip to content

Commit 34c7d7a

Browse files
committed
Determine if an APInt is zero by using bool APInt::operator() rather than APInt::getZExtValue().
APInt::getZExtValue assumes that the APInt can be represented in a 64 bit integer which is not necessarily true in a bignum context. rdar://22632470 Swift SVN r31831
1 parent b458a5d commit 34c7d7a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Diff for: lib/SILAnalysis/ValueTracking.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ bool swift::isNonEscapingLocalObject(SILValue V) {
341341
IsZeroKind swift::isZeroValue(SILValue Value) {
342342
// Inspect integer literals.
343343
if (auto *L = dyn_cast<IntegerLiteralInst>(Value.getDef())) {
344-
if (L->getValue().getZExtValue() == 0)
344+
if (!L->getValue())
345345
return IsZeroKind::Zero;
346346
return IsZeroKind::NotZero;
347347
}

Diff for: test/SILPasses/sil_combine.sil

+16
Original file line numberDiff line numberDiff line change
@@ -2718,3 +2718,19 @@ bb0(%0 : $*B, %1 : $B, %2 : $Builtin.Int1):
27182718
%7 = tuple()
27192719
return %7 : $()
27202720
}
2721+
2722+
// Make sure that we do not crash when determining if a value is not zero and
2723+
// has a value that can not be stored in a UInt64. The specific issue is that we
2724+
// were using getZExtValue before which assumes that an APInt can be stored in a
2725+
// UInt64.
2726+
//
2727+
// CHECK-LABEL: sil @large_int_zero_comparison : $@convention(thin) () -> (Builtin.Int1) {
2728+
// CHECK: [[CMP_RESULT:%.*]] = integer_literal $Builtin.Int1, 0
2729+
// CHECK: return [[CMP_RESULT]]
2730+
sil @large_int_zero_comparison : $@convention(thin) () -> (Builtin.Int1) {
2731+
bb0:
2732+
%0 = integer_literal $Builtin.Int1024, 0
2733+
%1 = integer_literal $Builtin.Int1024, 0xFFFFFFFFFFFFFFFFFFFF
2734+
%2 = builtin "cmp_eq_Int1024"(%0 : $Builtin.Int1024, %1 : $Builtin.Int1024) : $Builtin.Int1
2735+
return %2 : $Builtin.Int1
2736+
}

0 commit comments

Comments
 (0)