Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

[WIP] Removes unnecessary shift count masking #8744

Closed
wants to merge 1 commit into from

Conversation

mikedn
Copy link

@mikedn mikedn commented Dec 28, 2016

The C# compiler translates code like x << y to x << (y & 31) because the behavior of IL shift operations is unspecified if the shift count is greater than or equal to the bit width of the shifted value. However, x86/x64 shift instructions do mask the shift count so the & 31 is redundant.

Contributes to #1741

@mikedn
Copy link
Author

mikedn commented Dec 28, 2016

FX diff:

Total bytes of diff: -515 (-0.01 % of base)
    diff is an improvement.
Total byte diff includes 0 bytes from reconciling methods
        Base had    0 unique methods,        0 unique bytes
        Diff had    0 unique methods,        0 unique bytes
Top file improvements by size (bytes):
        -109 : System.Private.CoreLib.dasm (0.00 % of base)
        -105 : System.Reflection.Metadata.dasm (-0.03 % of base)
         -79 : Microsoft.CodeAnalysis.dasm (-0.01 % of base)
         -74 : System.Runtime.Numerics.dasm (-0.13 % of base)
         -64 : System.Linq.Expressions.dasm (-0.01 % of base)
13 total files with size differences.
Top method regessions by size (bytes):
           4 : Microsoft.CodeAnalysis.dasm - Microsoft.CodeAnalysis.RealParser:RightShiftWithRounding(long,int,bool):long
           1 : Microsoft.CodeAnalysis.dasm - ArityEnumerator[__Canon][System.__Canon]:MoveNext():bool:this
           1 : System.Runtime.Numerics.dasm - System.Numerics.BigIntegerCalculator:Divide(long,int,long,int,long,int)
Top method improvements by size (bytes):
         -24 : Microsoft.CodeAnalysis.CSharp.dasm - Microsoft.CodeAnalysis.CSharp.Binder:FoldNeverOverflowBinaryOperators(int,ref,ref):ref
         -19 : System.Private.CoreLib.dasm - System.Text.UTF7Encoding:GetChars(long,int,long,int,ref):int:this
         -18 : Microsoft.CodeAnalysis.dasm - FloatingPointType:AssembleFloatingPointValue(long,int,bool,byref):int:this
         -18 : System.Runtime.Numerics.dasm - System.Numerics.BigInteger:op_RightShift(struct,int):struct
         -15 : System.Runtime.Numerics.dasm - System.Numerics.BigInteger:.ctor(double):this
120 total methods with size differences.

Considering that the eliminated and is usually 3 bytes long this means that there are probably some 170 shift count masking occurrences in the framework.

The few size regressions are a bit odd, it seems that in some cases the elimination of and somehow affected register allocation and we ended up with additional spills/reloads.

@mikedn
Copy link
Author

mikedn commented Jan 13, 2017

Closing for now, I can't convince myself that this belongs in morph. Perhaps after #8117.

@mikedn mikedn closed this Jan 13, 2017
@karelz karelz modified the milestone: 2.0.0 Aug 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants