未验证 提交 cce4ee97 编写于 作者: N Neal Gafter 提交者: GitHub

Merge pull request #26846 from gafter/master-25692

Produce much better code for CInt(Fix(number))
......@@ -433,6 +433,9 @@ internal enum WellKnownMember
System_Runtime_CompilerServices_IsUnmanagedAttribute__ctor,
Microsoft_VisualBasic_Conversion__FixSingle,
Microsoft_VisualBasic_Conversion__FixDouble,
Count
}
}
因为 它太大了无法显示 source diff 。你可以改为 查看blob
......@@ -274,6 +274,8 @@ internal enum WellKnownType
System_Runtime_InteropServices_UnmanagedType,
System_Runtime_CompilerServices_IsUnmanagedAttribute,
Microsoft_VisualBasic_Conversion,
NextAvailable,
}
......@@ -541,6 +543,7 @@ internal static class WellKnownTypes
"System.ReadOnlySpan`1",
"System.Runtime.InteropServices.UnmanagedType",
"System.Runtime.CompilerServices.IsUnmanagedAttribute",
"Microsoft.VisualBasic.Conversion",
};
private readonly static Dictionary<string, WellKnownType> s_nameToTypeIdMap = new Dictionary<string, WellKnownType>((int)Count);
......
......@@ -123,13 +123,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGen
Private Sub EmitConvertSimpleNumeric(conversion As BoundConversion, typeFrom As PrimitiveTypeCode, typeTo As PrimitiveTypeCode, checked As Boolean)
Debug.Assert(typeFrom.IsIntegral() OrElse typeFrom.IsFloatingPoint() OrElse typeFrom = PrimitiveTypeCode.Char)
Debug.Assert(typeTo.IsIntegral() OrElse typeTo.IsFloatingPoint())
Debug.Assert(Not (typeFrom.IsFloatingPoint() AndAlso typeTo.IsIntegral() AndAlso
Not (conversion.Operand.Kind = BoundKind.Call AndAlso
DirectCast(conversion.Operand, BoundCall).Method.Equals(
Me._module.SourceModule.ContainingSourceAssembly.DeclaringCompilation.GetWellKnownTypeMember(WellKnownMember.System_Math__RoundDouble)))),
"About to ignore VB rules for rounding float numbers.")
_builder.EmitNumericConversion(typeFrom, typeTo, checked)
End Sub
......
......@@ -1295,19 +1295,24 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Private Function RewriteFloatingToIntegralConversion(node As BoundConversion, typeFrom As TypeSymbol, underlyingTypeTo As TypeSymbol) As BoundExpression
Debug.Assert(typeFrom.IsFloatingType() AndAlso underlyingTypeTo.IsIntegralType())
Dim result As BoundExpression = node
Dim operand = node.Operand
Dim mathRound As MethodSymbol
' Call Math.Round method to enforce VB style rounding.
' CInt(Fix(number)) and the like can be simplified to just truncate the number to the integral type
If operand.Kind = BoundKind.Call Then
Dim callOperand = DirectCast(operand, BoundCall)
If IsFixInvocation(callOperand) Then
Return New BoundConversion(node.Syntax, callOperand.Arguments(0), node.ConversionKind, node.Checked, node.ExplicitCastInCode, node.Type)
End If
End If
' Call Math.Round method to enforce VB style rounding.
Const memberId As WellKnownMember = WellKnownMember.System_Math__RoundDouble
mathRound = DirectCast(Compilation.GetWellKnownTypeMember(memberId), MethodSymbol)
Dim mathRound As MethodSymbol = DirectCast(Compilation.GetWellKnownTypeMember(memberId), MethodSymbol)
If Not ReportMissingOrBadRuntimeHelper(node, memberId, mathRound) Then
' If we got here and passed badness check, it should be safe to assume that we have
' a "good" symbol for Double type
Dim operand = node.Operand
#If DEBUG Then
Dim useSiteDiagnostics As HashSet(Of DiagnosticInfo) = Nothing
#End If
......@@ -1334,6 +1339,22 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return result
End Function
Private Function IsFixInvocation(node As BoundCall) As Boolean
' Quick test to eliminate most calls to something other that Conversion.Fix
If Not "Fix".Equals(node.Method.Name) Then
Return False
End If
Select Case node.Type.SpecialType
Case SpecialType.System_Single
Return node.Method = Me.Compilation.GetWellKnownTypeMember(WellKnownMember.Microsoft_VisualBasic_Conversion__FixSingle)
Case SpecialType.System_Double
Return node.Method = Me.Compilation.GetWellKnownTypeMember(WellKnownMember.Microsoft_VisualBasic_Conversion__FixDouble)
Case Else
Return False
End Select
End Function
#End Region
#Region "DirectCast"
......
......@@ -537,7 +537,8 @@ End Namespace
WellKnownType.Microsoft_VisualBasic_ApplicationServices_ApplicationBase,
WellKnownType.Microsoft_VisualBasic_ApplicationServices_WindowsFormsApplicationBase,
WellKnownType.Microsoft_VisualBasic_Information,
WellKnownType.Microsoft_VisualBasic_Interaction
WellKnownType.Microsoft_VisualBasic_Interaction,
WellKnownType.Microsoft_VisualBasic_Conversion
' Not embedded, so not available.
Continue For
Case WellKnownType.System_FormattableString,
......@@ -674,7 +675,9 @@ End Namespace
WellKnownMember.Microsoft_VisualBasic_Information__SystemTypeName,
WellKnownMember.Microsoft_VisualBasic_Information__TypeName,
WellKnownMember.Microsoft_VisualBasic_Information__VbTypeName,
WellKnownMember.Microsoft_VisualBasic_Interaction__CallByName
WellKnownMember.Microsoft_VisualBasic_Interaction__CallByName,
WellKnownMember.Microsoft_VisualBasic_Conversion__FixSingle,
WellKnownMember.Microsoft_VisualBasic_Conversion__FixDouble
' The type is not embedded, so the member is not available.
Continue For
Case WellKnownMember.System_Array__Empty,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册