Emit shorter variants in ILGenerator.Emit(OpCode, int) (#35427)
authorStephen Toub <stoub@microsoft.com>
Sat, 25 Apr 2020 13:45:24 +0000 (09:45 -0400)
committerGitHub <noreply@github.com>
Sat, 25 Apr 2020 13:45:24 +0000 (06:45 -0700)
commitcfaaf7738f4c6dc686dbc2599f480cb56c4285a5
treef0abacebf61c38f95a48be0b1966f2b396afd8b6
parent424b90d7c2a74b96e7fc750278b075b466520457
Emit shorter variants in ILGenerator.Emit(OpCode, int) (#35427)

The Ldloc, Stloc, Ldloca, Ldc_I4, Ldarg, Ldarga, and Starg opcodes all have shorter variants that take up less space in the IL instruction stream.  ILGenerator.Emit(OpCode, LocalBuilder) already special cases Ldloc, Stloc, and Ldloca to automatically translate those into their shorter forms where applicable, but similar logic doesn't exist in Emit(OpCode, int) for Ldc_I4, Ldarg, Ldarga, and Starg.  Instead, various other libraries higher in the stack that use reflection emit either end up doing all the special-casing with their own helper routines to do the shrinking, or they just forego it and end up with larger IL than is necessary.

This PR just moves the logic down into Emit(OpCode, int) such that all uses can benefit, and removes the special-casing duplication from the other libraries.
src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/ComInterop/ComRuntimeHelpers.cs
src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Compiler/ILGen.cs
src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/CodeGenerator.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/CodeGenerator.cs
src/libraries/System.Private.Xml/src/System/Xml/Xsl/IlGen/GenerateHelper.cs
src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs