Skip to content

ConstantFolding: remove a wrong peephole optimization for signed "< 0" and ">= 0" comparisons #39938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions lib/SILOptimizer/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,48 +476,6 @@ static SILValue constantFoldCompare(BuiltinInst *BI, BuiltinValueKind ID) {
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), APInt());
}
}

// Fold x < 0 into false, if x is known to be a result of an unsigned
// operation with overflow checks enabled.
BuiltinInst *BIOp;
if (match(BI, m_BuiltinInst(BuiltinValueKind::ICMP_SLT,
m_TupleExtractOperation(m_BuiltinInst(BIOp), 0),
m_Zero()))) {
// Check if Other is a result of an unsigned operation with overflow.
switch (BIOp->getBuiltinInfo().ID) {
default:
break;
case BuiltinValueKind::UAddOver:
case BuiltinValueKind::USubOver:
case BuiltinValueKind::UMulOver:
// Was it an operation with an overflow check?
if (match(BIOp->getOperand(2), m_One())) {
SILBuilderWithScope B(BI);
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), APInt());
}
}
}

// Fold x >= 0 into true, if x is known to be a result of an unsigned
// operation with overflow checks enabled.
if (match(BI, m_BuiltinInst(BuiltinValueKind::ICMP_SGE,
m_TupleExtractOperation(m_BuiltinInst(BIOp), 0),
m_Zero()))) {
// Check if Other is a result of an unsigned operation with overflow.
switch (BIOp->getBuiltinInfo().ID) {
default:
break;
case BuiltinValueKind::UAddOver:
case BuiltinValueKind::USubOver:
case BuiltinValueKind::UMulOver:
// Was it an operation with an overflow check?
if (match(BIOp->getOperand(2), m_One())) {
SILBuilderWithScope B(BI);
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), APInt(1, 1));
}
}
}

return nullptr;
}

Expand Down
18 changes: 4 additions & 14 deletions test/SILOptimizer/constant_propagation.sil
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,7 @@ bb0:
// CHECK-NEXT: return %4 : $Builtin.Int64
}

// Fold x < 0 into false, if x is known to be a result of an unsigned
// operation with overflow checks enabled.
// At the same time x >= 0 is always true under the same conditions.
sil @fold_unsigned_op_with_overflow_lt_zero : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> Builtin.Int64 {
sil @dont_fold_unsigned_op_with_overflow_lt_zero : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> Builtin.Int64 {
bb0(%0 : $Builtin.Int64, %1 : $Builtin.Int64):
%zero = integer_literal $Builtin.Int64, 0
%2 = integer_literal $Builtin.Int1, -1
Expand Down Expand Up @@ -503,17 +500,10 @@ bb0(%0 : $Builtin.Int64, %1 : $Builtin.Int64):

return %uadd_with_overflow_result : $Builtin.Int64

// CHECK-LABEL: sil @fold_unsigned_op_with_overflow_lt_zero
// CHECK-LABEL: sil @dont_fold_unsigned_op_with_overflow_lt_zero
// CHECK: builtin "uadd_with_overflow_Int64"
// CHECK: integer_literal $Builtin.Int1, 0
// CHECK: integer_literal $Builtin.Int1, -1
// CHECK: builtin "usub_with_overflow_Int64"
// CHECK: integer_literal $Builtin.Int1, 0
// CHECK: integer_literal $Builtin.Int1, -1
// CHECK: builtin "umul_with_overflow_Int64"
// CHECK: integer_literal $Builtin.Int1, 0
// CHECK: integer_literal $Builtin.Int1, -1
// CHECK-NEXT: return {{.*}}$Builtin.Int64
// CHECK-NOT: integer_literal
// CHECK: } // end sil function 'dont_fold_unsigned_op_with_overflow_lt_zero'
}


Expand Down
34 changes: 8 additions & 26 deletions test/SILOptimizer/constant_propagation_ownership.sil
Original file line number Diff line number Diff line change
Expand Up @@ -539,22 +539,11 @@ bb0:
// CHECK-NEXT: return %4 : $Builtin.Int64
}

// Fold x < 0 into false, if x is known to be a result of an unsigned
// operation with overflow checks enabled.
// At the same time x >= 0 is always true under the same conditions.
//
// CHECK-LABEL: sil [ossa] @fold_unsigned_op_with_overflow_lt_zero :
// CHECK-LABEL: sil [ossa] @dont_fold_unsigned_op_with_overflow_lt_zero :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

// CHECK: builtin "uadd_with_overflow_Int64"
// CHECK: integer_literal $Builtin.Int1, 0
// CHECK: integer_literal $Builtin.Int1, -1
// CHECK: builtin "usub_with_overflow_Int64"
// CHECK: integer_literal $Builtin.Int1, 0
// CHECK: integer_literal $Builtin.Int1, -1
// CHECK: builtin "umul_with_overflow_Int64"
// CHECK: integer_literal $Builtin.Int1, 0
// CHECK: integer_literal $Builtin.Int1, -1
// CHECK-NEXT: return {{.*}}$Builtin.Int64
sil [ossa] @fold_unsigned_op_with_overflow_lt_zero : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> Builtin.Int64 {
// CHECK-NOT: integer_literal
// CHECK: } // end sil function 'dont_fold_unsigned_op_with_overflow_lt_zero'
sil [ossa] @dont_fold_unsigned_op_with_overflow_lt_zero : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> Builtin.Int64 {
bb0(%0 : $Builtin.Int64, %1 : $Builtin.Int64):
%zero = integer_literal $Builtin.Int64, 0
%2 = integer_literal $Builtin.Int1, -1
Expand Down Expand Up @@ -585,18 +574,11 @@ bb0(%0 : $Builtin.Int64, %1 : $Builtin.Int64):
return %uadd_with_overflow_result : $Builtin.Int64
}

// CHECK-LABEL: sil [ossa] @fold_unsigned_op_with_overflow_lt_zero_destructure :
// CHECK-LABEL: sil [ossa] @dont_fold_unsigned_op_with_overflow_lt_zero_destructure :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just add a comment here (I am fine in a subsequent PR) that explains why we aren't doing this anymore.

// CHECK: builtin "uadd_with_overflow_Int64"
// CHECK: integer_literal $Builtin.Int1, 0
// CHECK: integer_literal $Builtin.Int1, -1
// CHECK: builtin "usub_with_overflow_Int64"
// CHECK: integer_literal $Builtin.Int1, 0
// CHECK: integer_literal $Builtin.Int1, -1
// CHECK: builtin "umul_with_overflow_Int64"
// CHECK: integer_literal $Builtin.Int1, 0
// CHECK: integer_literal $Builtin.Int1, -1
// CHECK-NEXT: return {{.*}}$Builtin.Int64
sil [ossa] @fold_unsigned_op_with_overflow_lt_zero_destructure : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> Builtin.Int64 {
// CHECK-NOT: integer_literal
// CHECK: } // end sil function 'dont_fold_unsigned_op_with_overflow_lt_zero_destructure'
sil [ossa] @dont_fold_unsigned_op_with_overflow_lt_zero_destructure : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> Builtin.Int64 {
bb0(%0 : $Builtin.Int64, %1 : $Builtin.Int64):
%zero = integer_literal $Builtin.Int64, 0
%2 = integer_literal $Builtin.Int1, -1
Expand Down
6 changes: 6 additions & 0 deletions test/stdlib/RangeTraps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ RangeTraps.test("throughNaN")
_ = ...Double.nan
}

RangeTraps.test("UIntOverflow")
.code {
expectCrashLater()
_blackHole((0 ..< UInt.max).count)
}

if #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) {
// Debug check was introduced in https://github.com/apple/swift/pull/34961
RangeTraps.test("UncheckedHalfOpen")
Expand Down