@@ -898,83 +898,6 @@ public bool RewriteBody()
898898 return true ;
899899 }
900900
901- // Recognizes the OS platform-guard idiom and evaluates it to a constant. Unlike the
902- // OperatingSystem.IsX() predicates (per-target literals the generic analyzer already folds),
903- // IsOSPlatform routes through an instance String.Equals and, for the RuntimeInformation
904- // overload, an opaque OSPlatform struct, so it needs direct handling. The recognized platform
905- // token is mapped to its OperatingSystem.IsX() predicate, which is then folded normally -- that
906- // predicate also encodes the OSX/MacCatalyst aliasing, so no manual replication is needed.
907- bool TryEvaluateOSPlatformGuard ( in UnreachableBlocksOptimizer optimizer , MethodDefinition md , Collection < Instruction > instructions , int callIndex , [ NotNullWhen ( true ) ] out Instruction ? result )
908- {
909- result = null ;
910-
911- if ( md . Name != "IsOSPlatform" || ! md . IsStatic || md . GetMetadataParametersCount ( ) != 1 || callIndex < 1 )
912- return false ;
913-
914- bool isRuntimeInformation = md . DeclaringType . IsTypeOf ( "System.Runtime.InteropServices" , "RuntimeInformation" ) ;
915- bool isOperatingSystem = md . DeclaringType . IsTypeOf ( "System" , "OperatingSystem" ) ;
916- if ( ! isRuntimeInformation && ! isOperatingSystem )
917- return false ;
918-
919- Instruction argProducer = instructions [ callIndex - 1 ] ;
920- string ? token ;
921-
922- if ( isOperatingSystem )
923- {
924- // OperatingSystem.IsOSPlatform(string) with a literal platform name.
925- token = argProducer . OpCode . Code == Code . Ldstr ? ( string ) argProducer . Operand : null ;
926- }
927- else
928- {
929- // RuntimeInformation.IsOSPlatform(OSPlatform) built from a well-known OSPlatform value.
930- if ( argProducer . OpCode . Code != Code . Call )
931- return false ;
932-
933- MethodDefinition ? producer = context . TryResolve ( ( MethodReference ) argProducer . Operand ) ;
934- if ( producer == null || ! producer . DeclaringType . IsTypeOf ( "System.Runtime.InteropServices" , "OSPlatform" ) )
935- return false ;
936-
937- token = producer . Name switch
938- {
939- "get_Windows" => "WINDOWS" ,
940- "get_Linux" => "LINUX" ,
941- "get_OSX" => "OSX" ,
942- "get_FreeBSD" => "FREEBSD" ,
943- "Create" when callIndex >= 2 && instructions [ callIndex - 2 ] . OpCode . Code == Code . Ldstr
944- => ( string ) instructions [ callIndex - 2 ] . Operand ,
945- _ => null ,
946- } ;
947- }
948-
949- string ? predicate = token ? . ToUpperInvariant ( ) switch
950- {
951- "WINDOWS" => "IsWindows" ,
952- "LINUX" => "IsLinux" ,
953- "OSX" or "MACOS" => "IsMacOS" ,
954- "BROWSER" => "IsBrowser" ,
955- "WASI" => "IsWasi" ,
956- "ANDROID" => "IsAndroid" ,
957- "IOS" => "IsIOS" ,
958- "MACCATALYST" => "IsMacCatalyst" ,
959- "TVOS" => "IsTvOS" ,
960- "WATCHOS" => "IsWatchOS" ,
961- "FREEBSD" => "IsFreeBSD" ,
962- _ => null , // unknown/custom platform: leave the guard untouched
963- } ;
964- if ( predicate == null )
965- return false ;
966-
967- TypeDefinition ? operatingSystem = md . DeclaringType . Module . GetType ( "System.OperatingSystem" ) ;
968- MethodDefinition ? isPlatform = operatingSystem ? . Methods . FirstOrDefault (
969- m => m . Name == predicate && m . IsStatic && m . HasBody && m . GetMetadataParametersCount ( ) == 0 ) ;
970- if ( isPlatform == null )
971- return false ;
972-
973- // The predicate body is a per-target 'return true/false', which the constant analyzer folds.
974- result = optimizer . TryGetMethodCallResult ( new CalleePayload ( isPlatform , Array . Empty < Instruction > ( ) ) ) ? . Instruction ;
975- return result != null ;
976- }
977-
978901 public bool ApplyTemporaryInlining ( in UnreachableBlocksOptimizer optimizer )
979902 {
980903 bool changed = false ;
@@ -997,19 +920,6 @@ public bool ApplyTemporaryInlining(in UnreachableBlocksOptimizer optimizer)
997920 if ( md . IsVirtual || md . CallingConvention == MethodCallingConvention . VarArg )
998921 break ;
999922
1000- // RuntimeInformation.IsOSPlatform(OSPlatform.X) / OperatingSystem.IsOSPlatform("X")
1001- // don't fold through the generic constant analyzer (the argument flows through an
1002- // opaque OSPlatform struct or an instance String.Equals). Recognize the guard idiom
1003- // directly so its dead branch (and any foreign P/Invoke it protects) can be trimmed.
1004- if ( context . IsOptimizationEnabled ( CodeOptimizations . IPConstantPropagation , Body . Method )
1005- && TryEvaluateOSPlatformGuard ( optimizer , md , instructions , i , out Instruction ? osGuardResult ) )
1006- {
1007- RewriteToNop ( i - 1 , 1 ) ;
1008- Rewrite ( i , osGuardResult ) ;
1009- changed = true ;
1010- break ;
1011- }
1012-
1013923 Instruction [ ] ? args = GetArgumentsOnStack ( md , FoldedInstructions ?? instructions , i ) ;
1014924 targetResult = args ? . Length > 0 && md . IsStatic ? EvaluateIntrinsicCall ( md , args ) : null ;
1015925
0 commit comments