You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.6 KiB

  1. // Copyright (c) 2021 Daniel Grunwald, Siegfried Pammer
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. #nullable enable
  19. using System.Diagnostics.CodeAnalysis;
  20. using System.Linq;
  21. namespace ICSharpCode.Decompiler.IL.Transforms
  22. {
  23. /// <summary>
  24. /// Block IL_0018 (incoming: *) {
  25. /// stloc s(ldc.i4 1)
  26. /// br IL_0019
  27. /// }
  28. ///
  29. /// Block IL_0019 (incoming: > 1) {
  30. /// if (logic.not(ldloc s)) br IL_0027
  31. /// br IL_001d
  32. /// }
  33. ///
  34. /// replace br IL_0019 with br IL_0027
  35. /// </summary>
  36. class RemoveInfeasiblePathTransform : IILTransform
  37. {
  38. void IILTransform.Run(ILFunction function, ILTransformContext context)
  39. {
  40. foreach (var container in function.Descendants.OfType<BlockContainer>())
  41. {
  42. bool changed = false;
  43. foreach (var block in container.Blocks)
  44. {
  45. changed |= DoTransform(block, context);
  46. }
  47. if (changed)
  48. {
  49. container.SortBlocks(deleteUnreachableBlocks: true);
  50. }
  51. }
  52. }
  53. private bool DoTransform(Block block, ILTransformContext context)
  54. {
  55. if (!MatchBlock1(block, out var s, out int value, out var br))
  56. return false;
  57. if (!MatchBlock2(br.TargetBlock, s, value, out var exitInst))
  58. return false;
  59. context.Step("RemoveInfeasiblePath", br);
  60. br.ReplaceWith(exitInst.Clone());
  61. s.RemoveIfRedundant = true;
  62. return true;
  63. }
  64. // Block IL_0018 (incoming: *) {
  65. // stloc s(ldc.i4 1)
  66. // br IL_0019
  67. // }
  68. private bool MatchBlock1(Block block, [NotNullWhen(true)] out ILVariable? variable, out int constantValue, [NotNullWhen(true)] out Branch? branch)
  69. {
  70. variable = null;
  71. constantValue = 0;
  72. branch = null;
  73. if (block.Instructions.Count != 2)
  74. return false;
  75. if (block.Instructions[0] is not StLoc {
  76. Variable: { Kind: VariableKind.StackSlot } s,
  77. Value: LdcI4 { Value: 0 or 1 } valueInst
  78. })
  79. {
  80. return false;
  81. }
  82. if (block.Instructions[1] is not Branch br)
  83. return false;
  84. variable = s;
  85. constantValue = valueInst.Value;
  86. branch = br;
  87. return true;
  88. }
  89. // Block IL_0019 (incoming: > 1) {
  90. // if (logic.not(ldloc s)) br IL_0027
  91. // br IL_001d
  92. // }
  93. bool MatchBlock2(Block block, ILVariable s, int constantValue, [NotNullWhen(true)] out ILInstruction? exitInst)
  94. {
  95. exitInst = null;
  96. if (block.Instructions.Count != 2)
  97. return false;
  98. if (block.IncomingEdgeCount <= 1)
  99. return false;
  100. if (!block.MatchIfAtEndOfBlock(out var load, out var trueInst, out var falseInst))
  101. return false;
  102. if (!load.MatchLdLoc(s))
  103. return false;
  104. exitInst = constantValue != 0 ? trueInst : falseInst;
  105. return exitInst is Branch or Leave { Value: Nop };
  106. }
  107. }
  108. }