void RewriteBody(MethodDefinition method) { var reducer = new BodyReducer(method.Body, Context); // // Temporary inlines any calls which return contant expression // if (!TryInlineBodyDependencies(ref reducer)) { return; } // // This is the main step which evaluates if inlined calls can // produce folded branches. When it finds them the unreachable // branch is replaced with nops. // if (!reducer.RewriteBody()) { return; } Context.LogMessage(MessageContainer.CreateInfoMessage($"Reduced '{reducer.InstructionsReplaced}' instructions in conditional branches for [{method.DeclaringType.Module.Assembly.Name}] method {method.FullName}")); if (method.ReturnType.MetadataType == MetadataType.Void) { return; } // // Re-run the analyzer in case body change rewrote it to constant expression // var analyzer = new ConstantExpressionMethodAnalyzer(method, reducer.FoldedInstructions); if (analyzer.Analyze()) { constExprMethods[method] = analyzer.Result; } }
void FindConstantExpressionsMethods(Collection <TypeDefinition> types) { foreach (var type in types) { if (type.IsInterface) { continue; } if (!type.HasMethods) { continue; } foreach (var method in type.Methods) { if (!method.HasBody) { continue; } if (method.ReturnType.MetadataType == MetadataType.Void) { continue; } switch (Annotations.GetAction(method)) { case MethodAction.ConvertToThrow: continue; case MethodAction.ConvertToStub: var instruction = CodeRewriterStep.CreateConstantResultInstruction(Context, method); if (instruction != null) { constExprMethods[method] = instruction; } continue; } if (method.IsIntrinsic()) { continue; } if (constExprMethods.ContainsKey(method)) { continue; } if (!Context.IsOptimizationEnabled(CodeOptimizations.IPConstantPropagation, method)) { continue; } var analyzer = new ConstantExpressionMethodAnalyzer(method); if (analyzer.Analyze()) { constExprMethods[method] = analyzer.Result; } } if (type.HasNestedTypes) { FindConstantExpressionsMethods(type.NestedTypes); } } }