Skip to content

Commit b914464

Browse files
carlos4242stephentyrone
authored andcommitted
Remove apparently obsolete builtin functions (#20947)
* Remove apparently obsolete builtin functions. - Remove s_to_u_checked_conversion and u_to_s_checked_conversion functions from builtin AST parsing, SIL/IR generation and from SIL optimisations. * Remove apparently obsolete builtin functions - unit tests. - Remove unit tests for SIL transformations relating to s_to_u_checked_conversion and u_to_s_checked_conversion builtin functions.
1 parent e4d1841 commit b914464

File tree

13 files changed

+39
-535
lines changed

13 files changed

+39
-535
lines changed

include/swift/AST/Builtins.def

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,6 @@ BUILTIN_MISC_OPERATION(SToSCheckedTrunc, "s_to_s_checked_trunc", "n", Special)
478478
BUILTIN_MISC_OPERATION(SToUCheckedTrunc, "s_to_u_checked_trunc", "n", Special)
479479
BUILTIN_MISC_OPERATION(UToUCheckedTrunc, "u_to_u_checked_trunc", "n", Special)
480480

481-
/// Checked conversions for signed <-> unsigned integers of the same size.
482-
/// Returns a tuple containing the conversion result as well as
483-
/// the sign error / overflow bit.
484-
BUILTIN_MISC_OPERATION(SUCheckedConversion,
485-
"s_to_u_checked_conversion", "n", Special)
486-
BUILTIN_MISC_OPERATION(USCheckedConversion,
487-
"u_to_s_checked_conversion", "n", Special)
488-
489481
/// IntToFPWithOverflow has type (Integer) -> Float
490482
BUILTIN_MISC_OPERATION(IntToFPWithOverflow, "itofp_with_overflow", "n", Special)
491483

include/swift/SIL/PatternMatch.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -715,13 +715,6 @@ using BuiltinApplyTy = typename Apply_match<BuiltinValueKind, Tys...>::Ty;
715715
// if any of the sub-matchers succeed.
716716
//
717717

718-
/// Matcher for any of the builtin checked conversions.
719-
template <typename T0>
720-
inline typename OneOf_match<BuiltinApplyTy<T0>, BuiltinApplyTy<T0>>::Ty
721-
m_CheckedConversion(const T0 &Op0) {
722-
return m_USCheckedConversion(Op0) || m_SUCheckedConversion(Op0);
723-
}
724-
725718
/// Matcher for any of the builtin ExtOrBitCast instructions.
726719
template <typename T0>
727720
inline typename OneOf_match<BuiltinApplyTy<T0>, BuiltinApplyTy<T0>>::Ty

lib/AST/Builtins.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,19 +1106,6 @@ static ValueDecl *getCheckedTruncOperation(ASTContext &Context, Identifier Id,
11061106
return getBuiltinFunction(Id, { InTy }, ResultTy);
11071107
}
11081108

1109-
static ValueDecl *getCheckedConversionOperation(ASTContext &Context,
1110-
Identifier Id,
1111-
Type Ty) {
1112-
Type BuiltinTy = Ty->getAs<BuiltinIntegerType>();
1113-
if (!BuiltinTy)
1114-
return nullptr;
1115-
1116-
Type SignErrorBitTy = BuiltinIntegerType::get(1, Context);
1117-
TupleTypeElt ResultElts[] = { BuiltinTy, SignErrorBitTy };
1118-
Type ResultTy = TupleType::get(ResultElts, Context);
1119-
return getBuiltinFunction(Id, { BuiltinTy }, ResultTy);
1120-
}
1121-
11221109
static ValueDecl *getIntToFPWithOverflowOperation(ASTContext &Context,
11231110
Identifier Id, Type InputTy,
11241111
Type OutputTy) {
@@ -1853,11 +1840,6 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
18531840
if (Types.size() != 2) return nullptr;
18541841
return getCheckedTruncOperation(Context, Id, Types[0], Types[1], false);
18551842

1856-
case BuiltinValueKind::SUCheckedConversion:
1857-
case BuiltinValueKind::USCheckedConversion:
1858-
if (Types.size() != 1) return nullptr;
1859-
return getCheckedConversionOperation(Context, Id, Types[0]);
1860-
18611843
case BuiltinValueKind::ClassifyBridgeObject:
18621844
if (!Types.empty()) return nullptr;
18631845
return getClassifyBridgeObject(Context, Id);

lib/IRGen/GenBuiltin.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -758,22 +758,6 @@ if (Builtin.ID == BuiltinValueKind::id) { \
758758
return out.add(OverflowFlag);
759759
}
760760

761-
if (Builtin.ID == BuiltinValueKind::SUCheckedConversion ||
762-
Builtin.ID == BuiltinValueKind::USCheckedConversion) {
763-
auto Ty =
764-
IGF.IGM.getStorageTypeForLowered(Builtin.Types[0]->getCanonicalType());
765-
766-
// Report a sign error if the input parameter is a negative number, when
767-
// interpreted as signed.
768-
llvm::Value *Arg = args.claimNext();
769-
llvm::Value *Zero = llvm::ConstantInt::get(Ty, 0);
770-
llvm::Value *OverflowFlag = IGF.Builder.CreateICmpSLT(Arg, Zero);
771-
772-
// Return the tuple: (the result (same as input), the overflow flag).
773-
out.add(Arg);
774-
return out.add(OverflowFlag);
775-
}
776-
777761
// We are currently emitting code for '_convertFromBuiltinIntegerLiteral',
778762
// which will call the builtin and pass it a non-compile-time-const parameter.
779763
if (Builtin.ID == BuiltinValueKind::IntToFPWithOverflow) {

lib/SIL/OperandOwnership.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,6 @@ CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, SRem)
11221122
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, SSubOver)
11231123
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, SToSCheckedTrunc)
11241124
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, SToUCheckedTrunc)
1125-
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, SUCheckedConversion)
11261125
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, Shl)
11271126
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, Sizeof)
11281127
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, StaticReport)
@@ -1140,7 +1139,6 @@ CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, UDiv)
11401139
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, UIToFP)
11411140
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, UMulOver)
11421141
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, URem)
1143-
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, USCheckedConversion)
11441142
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, USubOver)
11451143
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, UToSCheckedTrunc)
11461144
CONSTANT_OWNERSHIP_BUILTIN(Trivial, MustBeLive, UToUCheckedTrunc)

lib/SIL/ValueOwnership.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,6 @@ CONSTANT_OWNERSHIP_BUILTIN(Trivial, UToSCheckedTrunc)
479479
CONSTANT_OWNERSHIP_BUILTIN(Trivial, SToSCheckedTrunc)
480480
CONSTANT_OWNERSHIP_BUILTIN(Trivial, SToUCheckedTrunc)
481481
CONSTANT_OWNERSHIP_BUILTIN(Trivial, UToUCheckedTrunc)
482-
CONSTANT_OWNERSHIP_BUILTIN(Trivial, SUCheckedConversion)
483-
CONSTANT_OWNERSHIP_BUILTIN(Trivial, USCheckedConversion)
484482
CONSTANT_OWNERSHIP_BUILTIN(Trivial, IntToFPWithOverflow)
485483

486484
// This is surprising, Builtin.unreachable returns a "Never" value which is

lib/SILOptimizer/Analysis/SimplifyInstruction.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -492,18 +492,6 @@ static SILValue simplifyBuiltin(BuiltinInst *BI) {
492492
return Result;
493493
}
494494

495-
// trunc(tuple_extract(conversion(extOrBitCast(x))))) -> x
496-
if (match(Op, m_TupleExtractInst(
497-
m_CheckedConversion(
498-
m_ExtOrBitCast(m_SILValue(Result))), 0))) {
499-
// If the top bit of Result is known to be 0, then
500-
// it is safe to replace the whole pattern by original bits of x
501-
if (Result->getType() == BI->getType()) {
502-
if (auto signBit = computeSignBit(Result))
503-
if (!signBit.getValue())
504-
return Result;
505-
}
506-
}
507495
return SILValue();
508496
}
509497

@@ -639,23 +627,6 @@ SILValue InstSimplifier::simplifyOverflowBuiltin(BuiltinInst *BI) {
639627
switch (Builtin.ID) {
640628
default: break;
641629

642-
case BuiltinValueKind::SUCheckedConversion:
643-
case BuiltinValueKind::USCheckedConversion: {
644-
OperandValueArrayRef Args = BI->getArguments();
645-
const SILValue &Op = Args[0];
646-
if (auto signBit = computeSignBit(Op))
647-
if (!signBit.getValue())
648-
return Op;
649-
SILValue Result;
650-
// CheckedConversion(ExtOrBitCast(x)) -> x
651-
if (match(BI, m_CheckedConversion(m_ExtOrBitCast(m_SILValue(Result)))))
652-
if (Result->getType() == BI->getType().getTupleElementType(0)) {
653-
assert (!computeSignBit(Result).getValue() && "Sign bit should be 0");
654-
return Result;
655-
}
656-
}
657-
break;
658-
659630
case BuiltinValueKind::UToSCheckedTrunc:
660631
case BuiltinValueKind::UToUCheckedTrunc:
661632
case BuiltinValueKind::SToUCheckedTrunc:

lib/SILOptimizer/Analysis/ValueTracking.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -296,25 +296,6 @@ Optional<bool> swift::computeSignBit(SILValue V) {
296296
continue;
297297
}
298298

299-
// Source and target type sizes are the same.
300-
// S->U conversion can only succeed if
301-
// the sign bit of its operand is 0, i.e. it is >= 0.
302-
// The sign bit of a result is 0 only if the sign
303-
// bit of a source operand is 0.
304-
case BuiltinValueKind::SUCheckedConversion:
305-
Value = BI->getArguments()[0];
306-
continue;
307-
308-
// Source and target type sizes are the same.
309-
// U->S conversion can only succeed if
310-
// the top bit of its operand is 0, i.e.
311-
// it is representable as a signed integer >=0.
312-
// The sign bit of a result is 0 only if the sign
313-
// bit of a source operand is 0.
314-
case BuiltinValueKind::USCheckedConversion:
315-
Value = BI->getArguments()[0];
316-
continue;
317-
318299
// Sign bit of the operand is promoted.
319300
case BuiltinValueKind::SExt:
320301
Value = BI->getArguments()[0];

lib/SILOptimizer/Transforms/AccessEnforcementReleaseSinking.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ static bool isBarrier(SILInstruction *inst) {
122122
case BuiltinValueKind::SToUCheckedTrunc:
123123
case BuiltinValueKind::SToSCheckedTrunc:
124124
case BuiltinValueKind::UToUCheckedTrunc:
125-
case BuiltinValueKind::SUCheckedConversion:
126-
case BuiltinValueKind::USCheckedConversion:
127125
case BuiltinValueKind::IntToFPWithOverflow:
128126
case BuiltinValueKind::ZeroInitializer:
129127
case BuiltinValueKind::Once:

lib/SILOptimizer/Utils/ConstExpr.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,6 @@ ConstExprFunctionState::computeConstantValueBuiltin(BuiltinInst *inst) {
210210
if (!operand.isConstant())
211211
return operand;
212212

213-
// TODO: SUCheckedConversion/USCheckedConversion
214-
215213
// Implement support for s_to_s_checked_trunc_Int2048_Int64 and other
216214
// checking integer truncates. These produce a tuple of the result value
217215
// and an overflow bit.

lib/SILOptimizer/Utils/ConstantFolding.cpp

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -674,9 +674,7 @@ constantFoldAndCheckIntegerConversions(BuiltinInst *BI,
674674
assert(Builtin.ID == BuiltinValueKind::SToSCheckedTrunc ||
675675
Builtin.ID == BuiltinValueKind::UToUCheckedTrunc ||
676676
Builtin.ID == BuiltinValueKind::SToUCheckedTrunc ||
677-
Builtin.ID == BuiltinValueKind::UToSCheckedTrunc ||
678-
Builtin.ID == BuiltinValueKind::SUCheckedConversion ||
679-
Builtin.ID == BuiltinValueKind::USCheckedConversion);
677+
Builtin.ID == BuiltinValueKind::UToSCheckedTrunc);
680678

681679
// Check if we are converting a constant integer.
682680
OperandValueArrayRef Args = BI->getArguments();
@@ -688,11 +686,9 @@ constantFoldAndCheckIntegerConversions(BuiltinInst *BI,
688686
auto SrcBitWidth = SrcVal.getBitWidth();
689687

690688
bool DstTySigned = (Builtin.ID == BuiltinValueKind::SToSCheckedTrunc ||
691-
Builtin.ID == BuiltinValueKind::UToSCheckedTrunc ||
692-
Builtin.ID == BuiltinValueKind::USCheckedConversion);
689+
Builtin.ID == BuiltinValueKind::UToSCheckedTrunc);
693690
bool SrcTySigned = (Builtin.ID == BuiltinValueKind::SToSCheckedTrunc ||
694-
Builtin.ID == BuiltinValueKind::SToUCheckedTrunc ||
695-
Builtin.ID == BuiltinValueKind::SUCheckedConversion);
691+
Builtin.ID == BuiltinValueKind::SToUCheckedTrunc);
696692

697693
// Get source type and bit width.
698694
auto SrcTy = Builtin.Types[0]->castTo<AnyBuiltinIntegerType>();
@@ -705,44 +701,33 @@ constantFoldAndCheckIntegerConversions(BuiltinInst *BI,
705701
bool OverflowError;
706702
Type DstTy;
707703

708-
// Process conversions signed <-> unsigned for same size integers.
709-
if (Builtin.ID == BuiltinValueKind::SUCheckedConversion ||
710-
Builtin.ID == BuiltinValueKind::USCheckedConversion) {
711-
DstTy = SrcTy;
704+
assert(Builtin.Types.size() == 2);
705+
DstTy = Builtin.Types[1];
706+
uint32_t DstBitWidth =
707+
DstTy->castTo<BuiltinIntegerType>()->getGreatestWidth();
708+
709+
assert((DstBitWidth < SrcBitWidth || !SrcTy->getWidth().isFixedWidth()) &&
710+
"preconditions on builtin trunc operations should prevent"
711+
"fixed-width truncations that actually extend");
712+
713+
// The only way a true extension can overflow is if the value is
714+
// negative and the result is unsigned.
715+
if (DstBitWidth > SrcBitWidth) {
716+
OverflowError = (SrcTySigned && !DstTySigned && SrcVal.isNegative());
717+
Result = (SrcTySigned ? SrcVal.sext(DstBitWidth)
718+
: SrcVal.zext(DstBitWidth));
719+
720+
// A same-width change can overflow if the top bit disagrees.
721+
} else if (DstBitWidth == SrcBitWidth) {
722+
OverflowError = (SrcTySigned != DstTySigned && SrcVal.isNegative());
712723
Result = SrcVal;
713-
// Report an error if the sign bit is set.
714-
OverflowError = SrcVal.isNegative();
715724

716-
// Process the checked truncations.
725+
// A truncation can overflow if the value changes.
717726
} else {
718-
assert(Builtin.Types.size() == 2);
719-
DstTy = Builtin.Types[1];
720-
uint32_t DstBitWidth =
721-
DstTy->castTo<BuiltinIntegerType>()->getGreatestWidth();
722-
723-
assert((DstBitWidth < SrcBitWidth || !SrcTy->getWidth().isFixedWidth()) &&
724-
"preconditions on builtin trunc operations should prevent"
725-
"fixed-width truncations that actually extend");
726-
727-
// The only way a true extension can overflow is if the value is
728-
// negative and the result is unsigned.
729-
if (DstBitWidth > SrcBitWidth) {
730-
OverflowError = (SrcTySigned && !DstTySigned && SrcVal.isNegative());
731-
Result = (SrcTySigned ? SrcVal.sext(DstBitWidth)
732-
: SrcVal.zext(DstBitWidth));
733-
734-
// A same-width change can overflow if the top bit disagrees.
735-
} else if (DstBitWidth == SrcBitWidth) {
736-
OverflowError = (SrcTySigned != DstTySigned && SrcVal.isNegative());
737-
Result = SrcVal;
738-
739-
// A truncation can overflow if the value changes.
740-
} else {
741-
Result = SrcVal.trunc(DstBitWidth);
742-
APInt Ext = (DstTySigned ? Result.sext(SrcBitWidth)
743-
: Result.zext(SrcBitWidth));
744-
OverflowError = (SrcVal != Ext);
745-
}
727+
Result = SrcVal.trunc(DstBitWidth);
728+
APInt Ext = (DstTySigned ? Result.sext(SrcBitWidth)
729+
: Result.zext(SrcBitWidth));
730+
OverflowError = (SrcVal != Ext);
746731
}
747732

748733
// Check for overflow.
@@ -819,25 +804,19 @@ constantFoldAndCheckIntegerConversions(BuiltinInst *BI,
819804
DstTySigned, DstTy, SrcAsString);
820805
}
821806
} else {
822-
if (Builtin.ID == BuiltinValueKind::SUCheckedConversion) {
807+
// Try to print user-visible types if they are available.
808+
if (!UserSrcTy.isNull()) {
823809
diagnose(M.getASTContext(), Loc.getSourceLoc(),
824-
diag::integer_conversion_sign_error,
825-
UserDstTy.isNull() ? DstTy : UserDstTy);
826-
} else {
827-
// Try to print user-visible types if they are available.
828-
if (!UserSrcTy.isNull()) {
829-
diagnose(M.getASTContext(), Loc.getSourceLoc(),
830-
diag::integer_conversion_overflow,
831-
UserSrcTy, UserDstTy);
810+
diag::integer_conversion_overflow,
811+
UserSrcTy, UserDstTy);
832812

833-
// Otherwise, print the Builtin Types.
834-
} else {
835-
// Since builtin types are sign-agnostic, print the signedness
836-
// separately.
837-
diagnose(M.getASTContext(), Loc.getSourceLoc(),
838-
diag::integer_conversion_overflow_builtin_types,
839-
SrcTySigned, SrcTy, DstTySigned, DstTy);
840-
}
813+
// Otherwise, print the Builtin Types.
814+
} else {
815+
// Since builtin types are sign-agnostic, print the signedness
816+
// separately.
817+
diagnose(M.getASTContext(), Loc.getSourceLoc(),
818+
diag::integer_conversion_overflow_builtin_types,
819+
SrcTySigned, SrcTy, DstTySigned, DstTy);
841820
}
842821
}
843822

@@ -1220,9 +1199,7 @@ case BuiltinValueKind::id:
12201199
case BuiltinValueKind::SToSCheckedTrunc:
12211200
case BuiltinValueKind::UToUCheckedTrunc:
12221201
case BuiltinValueKind::SToUCheckedTrunc:
1223-
case BuiltinValueKind::UToSCheckedTrunc:
1224-
case BuiltinValueKind::SUCheckedConversion:
1225-
case BuiltinValueKind::USCheckedConversion: {
1202+
case BuiltinValueKind::UToSCheckedTrunc: {
12261203
return constantFoldAndCheckIntegerConversions(BI, Builtin, ResultsInError);
12271204
}
12281205

0 commit comments

Comments
 (0)