|
20 | 20 | #include "swift/SILOptimizer/Utils/Local.h"
|
21 | 21 | #include "llvm/ADT/Statistic.h"
|
22 | 22 | #include "llvm/Support/Debug.h"
|
23 |
| -#include "llvm/ADT/APSInt.h" |
24 | 23 |
|
25 | 24 | #define DEBUG_TYPE "constant-folding"
|
26 | 25 |
|
@@ -860,116 +859,6 @@ constantFoldAndCheckIntegerConversions(BuiltinInst *BI,
|
860 | 859 |
|
861 | 860 | }
|
862 | 861 |
|
863 |
| -/// A utility function that extracts the literal text corresponding |
864 |
| -/// to a given FloatLiteralInst the way it appears in the AST. |
865 |
| -/// This function can be used on FloatLiteralInsts generated by the |
866 |
| -/// constant folding phase. |
867 |
| -/// If the extraction is successful, the function returns true and |
868 |
| -/// 'fpStr' contains the literal the way it appears in the AST. |
869 |
| -/// If the extraction is unsuccessful, e.g. because there is no AST |
870 |
| -/// for the FloatLiteralInst, the function returns false. |
871 |
| -template<unsigned N> |
872 |
| -static bool tryExtractLiteralText(FloatLiteralInst *flitInst, |
873 |
| - SmallString<N> &fpStr) { |
874 |
| - |
875 |
| - Expr *expr = flitInst->getLoc().getAsASTNode<Expr>(); |
876 |
| - if (!expr) |
877 |
| - return false; |
878 |
| - |
879 |
| - // 'expr' may not be a FloatLiteralExpr since 'flitInst' could have been |
880 |
| - // created by the ConstantFolder by folding floating-point constructor calls. |
881 |
| - // So we iterate through the sequence of folded constructors if any, and |
882 |
| - // try to extract the FloatLiteralExpr. |
883 |
| - while (auto *callExpr = dyn_cast<CallExpr>(expr)) { |
884 |
| - if (callExpr->getNumArguments() != 1 || |
885 |
| - !dyn_cast<ConstructorRefCallExpr>(callExpr->getFn())) |
886 |
| - break; |
887 |
| - |
888 |
| - auto *tupleExpr = dyn_cast<TupleExpr>(callExpr->getArg()); |
889 |
| - if (!tupleExpr) |
890 |
| - break; |
891 |
| - |
892 |
| - expr = tupleExpr->getElement(0); |
893 |
| - } |
894 |
| - |
895 |
| - auto *flitExpr = dyn_cast<FloatLiteralExpr>(expr); |
896 |
| - if (!flitExpr) |
897 |
| - return false; |
898 |
| - |
899 |
| - if (flitExpr->isNegative()) |
900 |
| - fpStr += '-'; |
901 |
| - fpStr += flitExpr->getDigitsText(); |
902 |
| - return true; |
903 |
| -} |
904 |
| - |
905 |
| -static SILValue foldFPToIntConversion(BuiltinInst *BI, |
906 |
| - const BuiltinInfo &Builtin, Optional<bool> &ResultsInError) { |
907 |
| - |
908 |
| - assert(Builtin.ID == BuiltinValueKind::FPToSI || |
909 |
| - Builtin.ID == BuiltinValueKind::FPToUI); |
910 |
| - |
911 |
| - OperandValueArrayRef Args = BI->getArguments(); |
912 |
| - bool conversionToUnsigned = (Builtin.ID == BuiltinValueKind::FPToUI); |
913 |
| - |
914 |
| - auto *flitInst = dyn_cast<FloatLiteralInst>(Args[0]); |
915 |
| - if (!flitInst) |
916 |
| - return nullptr; |
917 |
| - APFloat fpVal = flitInst->getValue(); |
918 |
| - auto *destTy = Builtin.Types[1]->castTo<BuiltinIntegerType>(); |
919 |
| - |
920 |
| - // Check non-negativeness of 'fpVal' for conversion to unsigned int. |
921 |
| - if (conversionToUnsigned && fpVal.isNegative() && !fpVal.isZero()) { |
922 |
| - // Stop folding and emit diagnostics if enabled. |
923 |
| - if (ResultsInError.hasValue()) { |
924 |
| - SILModule &M = BI->getModule(); |
925 |
| - const ApplyExpr *CE = BI->getLoc().getAsASTNode<ApplyExpr>(); |
926 |
| - |
927 |
| - SmallString<10> fpStr; |
928 |
| - if (!tryExtractLiteralText(flitInst, fpStr)) |
929 |
| - flitInst->getValue().toString(fpStr); |
930 |
| - |
931 |
| - diagnose(M.getASTContext(), BI->getLoc().getSourceLoc(), |
932 |
| - diag::negative_fp_literal_overflow_unsigned, fpStr, |
933 |
| - CE ? CE->getType() : destTy, |
934 |
| - CE ? false : conversionToUnsigned); |
935 |
| - ResultsInError = Optional<bool>(true); |
936 |
| - } |
937 |
| - return nullptr; |
938 |
| - } |
939 |
| - |
940 |
| - llvm::APSInt resInt(destTy->getFixedWidth(), conversionToUnsigned); |
941 |
| - bool isExact = false; |
942 |
| - APFloat::opStatus status = |
943 |
| - fpVal.convertToInteger(resInt, APFloat::rmTowardZero, &isExact); |
944 |
| - |
945 |
| - if (status & APFloat::opStatus::opInvalidOp) { |
946 |
| - // Stop folding and emit diagnostics if enabled. |
947 |
| - if (ResultsInError.hasValue()) { |
948 |
| - SILModule &M = BI->getModule(); |
949 |
| - const ApplyExpr *CE = BI->getLoc().getAsASTNode<ApplyExpr>(); |
950 |
| - |
951 |
| - SmallString<10> fpStr; |
952 |
| - if (!tryExtractLiteralText(flitInst, fpStr)) |
953 |
| - flitInst->getValue().toString(fpStr); |
954 |
| - |
955 |
| - diagnose(M.getASTContext(), BI->getLoc().getSourceLoc(), |
956 |
| - diag::float_to_int_overflow, fpStr, |
957 |
| - CE ? CE->getType() : destTy, |
958 |
| - CE ? CE->isImplicit() : false); |
959 |
| - ResultsInError = Optional<bool>(true); |
960 |
| - } |
961 |
| - return nullptr; |
962 |
| - } |
963 |
| - |
964 |
| - if (status != APFloat::opStatus::opOK && |
965 |
| - status != APFloat::opStatus::opInexact) { |
966 |
| - return nullptr; |
967 |
| - } |
968 |
| - // The call to the builtin should be replaced with the constant value. |
969 |
| - SILBuilderWithScope B(BI); |
970 |
| - return B.createIntegerLiteral(BI->getLoc(), BI->getType(), resInt); |
971 |
| -} |
972 |
| - |
973 | 862 | static SILValue constantFoldBuiltin(BuiltinInst *BI,
|
974 | 863 | Optional<bool> &ResultsInError) {
|
975 | 864 | const IntrinsicInfo &Intrinsic = BI->getIntrinsicInfo();
|
@@ -1100,12 +989,6 @@ case BuiltinValueKind::id:
|
1100 | 989 | return B.createFloatLiteral(Loc, BI->getType(), TruncVal);
|
1101 | 990 | }
|
1102 | 991 |
|
1103 |
| - // Conversions from floating point to integer, |
1104 |
| - case BuiltinValueKind::FPToSI: |
1105 |
| - case BuiltinValueKind::FPToUI: { |
1106 |
| - return foldFPToIntConversion(BI, Builtin, ResultsInError); |
1107 |
| - } |
1108 |
| - |
1109 | 992 | case BuiltinValueKind::AssumeNonNegative: {
|
1110 | 993 | auto *V = dyn_cast<IntegerLiteralInst>(Args[0]);
|
1111 | 994 | if (!V)
|
|
0 commit comments