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.

310 lines
14 KiB

  1. // Copyright (c) 2014 Daniel Grunwald
  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. using System.Diagnostics;
  19. using System.Linq;
  20. using ICSharpCode.Decompiler.FlowAnalysis;
  21. using ICSharpCode.Decompiler.IL.Transforms;
  22. using ICSharpCode.Decompiler.Util;
  23. namespace ICSharpCode.Decompiler.IL.ControlFlow
  24. {
  25. /// <summary>
  26. /// Detects 'if' structure and other non-loop aspects of control flow.
  27. /// </summary>
  28. /// <remarks>
  29. /// Order dependency: should run after loop detection.
  30. /// Blocks should be basic blocks prior to this transform.
  31. /// After this transform, they will be extended basic blocks.
  32. /// </remarks>
  33. public class ConditionDetection : IBlockTransform
  34. {
  35. BlockTransformContext context;
  36. BlockContainer currentContainer;
  37. /// <summary>
  38. /// Builds structured control flow for the block associated with the control flow node.
  39. /// </summary>
  40. /// <remarks>
  41. /// After a block was processed, it should use structured control flow
  42. /// and have just a single 'regular' exit point (last branch instruction in the block)
  43. /// </remarks>
  44. public void Run(Block block, BlockTransformContext context)
  45. {
  46. this.context = context;
  47. this.currentContainer = (BlockContainer)block.Parent;
  48. // We only embed blocks into this block if they aren't referenced anywhere else,
  49. // so those blocks are dominated by this block.
  50. // BlockILTransform thus guarantees that the blocks being embedded are already
  51. // fully processed.
  52. var cfgNode = context.ControlFlowNode;
  53. Debug.Assert(cfgNode.UserData == block);
  54. // Because this transform runs at the beginning of the block transforms,
  55. // we know that `block` is still a (non-extended) basic block.
  56. // Last instruction is one with unreachable endpoint
  57. // (guaranteed by combination of BlockContainer and Block invariants)
  58. Debug.Assert(block.Instructions.Last().HasFlag(InstructionFlags.EndPointUnreachable));
  59. ILInstruction exitInst = block.Instructions.Last();
  60. // Previous-to-last instruction might have conditional control flow,
  61. // usually an IfInstruction with a branch:
  62. IfInstruction ifInst = block.Instructions.SecondToLastOrDefault() as IfInstruction;
  63. if (ifInst != null && ifInst.FalseInst.OpCode == OpCode.Nop) {
  64. HandleIfInstruction(cfgNode, block, ifInst, ref exitInst);
  65. }
  66. if (IsUsableBranchToChild(cfgNode, exitInst)) {
  67. // "...; goto usableblock;"
  68. // -> embed target block in this block
  69. context.Step("Inline target block of unconditional branch", exitInst);
  70. var targetBlock = ((Branch)exitInst).TargetBlock;
  71. Debug.Assert(exitInst == block.Instructions.Last());
  72. block.Instructions.RemoveAt(block.Instructions.Count - 1);
  73. block.Instructions.AddRange(targetBlock.Instructions);
  74. targetBlock.Remove();
  75. }
  76. }
  77. private void HandleIfInstruction(ControlFlowNode cfgNode, Block block, IfInstruction ifInst, ref ILInstruction exitInst)
  78. {
  79. if (ShouldSwapIfTargets(ifInst.TrueInst, exitInst)) {
  80. // "if (c) goto lateBlock; goto earlierBlock;"
  81. // -> "if (!c)" goto earlierBlock; goto lateBlock;
  82. // This reordering should make the if structure correspond more closely to the original C# source code
  83. context.Step("Negate if", ifInst);
  84. block.Instructions[block.Instructions.Count - 1] = ifInst.TrueInst;
  85. ifInst.TrueInst = exitInst;
  86. exitInst = block.Instructions.Last();
  87. ifInst.Condition = Comp.LogicNot(ifInst.Condition);
  88. }
  89. ILInstruction trueExitInst;
  90. if (IsUsableBranchToChild(cfgNode, ifInst.TrueInst)) {
  91. // "if (...) goto targetBlock; exitInst;"
  92. // -> "if (...) { targetBlock } exitInst;"
  93. context.Step("Inline block as then-branch", ifInst);
  94. var targetBlock = ((Branch)ifInst.TrueInst).TargetBlock;
  95. // The targetBlock was already processed, we can embed it into the if statement:
  96. targetBlock.Remove();
  97. ifInst.TrueInst = targetBlock;
  98. ILInstruction nestedCondition, nestedTrueInst;
  99. while (targetBlock.Instructions.Count > 0
  100. && targetBlock.Instructions[0].MatchIfInstruction(out nestedCondition, out nestedTrueInst))
  101. {
  102. nestedTrueInst = UnpackBlockContainingOnlyBranch(nestedTrueInst);
  103. if (DetectExitPoints.CompatibleExitInstruction(exitInst, nestedTrueInst)) {
  104. // "if (...) { if (nestedCondition) goto exitPoint; ... } goto exitPoint;"
  105. // -> "if (... && !nestedCondition) { ... } goto exitPoint;"
  106. context.Step("Combine 'if (cond1 && !cond2)' in then-branch", ifInst);
  107. ifInst.Condition = IfInstruction.LogicAnd(ifInst.Condition, Comp.LogicNot(nestedCondition));
  108. targetBlock.Instructions.RemoveAt(0);
  109. // Update targetBlock label now that we've removed the first instruction
  110. if (targetBlock.Instructions.FirstOrDefault()?.ILRange.IsEmpty == false) {
  111. int offset = targetBlock.Instructions[0].ILRange.Start;
  112. targetBlock.ILRange = new Interval(offset, offset);
  113. }
  114. continue; // try to find more nested conditions
  115. }
  116. if (nestedTrueInst is Block nestedTrueBlock
  117. && DetectExitPoints.CompatibleExitInstruction(exitInst, nestedTrueBlock.Instructions.Last())
  118. && targetBlock.HasFlag(InstructionFlags.EndPointUnreachable))
  119. {
  120. // "if (...) { if (nestedCondition) { trueInst...; goto exitPoint; } falseInst...; } goto exitPoint;"
  121. // -> "if (...) { if (!nestedCondition) { falseInst...; } trueInst... } goto exitPoint;"
  122. // (only if end-point of 'falseInst...' is unreachable)
  123. context.Step("Invert nested condition to reduce number of gotos", ifInst);
  124. var nestedIfInst = (IfInstruction)targetBlock.Instructions[0];
  125. nestedIfInst.Condition = Comp.LogicNot(nestedCondition);
  126. nestedTrueBlock.Instructions.RemoveAt(nestedTrueBlock.Instructions.Count - 1); // remove nested goto exitPoint;
  127. // remove falseInsts from outer block
  128. var falseInsts = targetBlock.Instructions.Skip(1).ToArray();
  129. targetBlock.Instructions.RemoveRange(1, targetBlock.Instructions.Count - 1);
  130. // add trueInsts to outer block
  131. targetBlock.Instructions.AddRange(nestedTrueBlock.Instructions);
  132. // add falseInsts to inner block
  133. nestedTrueBlock.Instructions.ReplaceList(falseInsts);
  134. nestedIfInst.Condition.AcceptVisitor(new ExpressionTransforms { context = new StatementTransformContext(context) });
  135. }
  136. break;
  137. }
  138. trueExitInst = targetBlock.Instructions.LastOrDefault();
  139. if (DetectExitPoints.CompatibleExitInstruction(exitInst, trueExitInst)) {
  140. // "if (...) { ...; goto exitPoint } goto exitPoint;"
  141. // -> "if (...) { ... } goto exitPoint;"
  142. context.Step("Remove redundant 'goto exitPoint;' in then-branch", ifInst);
  143. targetBlock.Instructions.RemoveAt(targetBlock.Instructions.Count - 1);
  144. trueExitInst = null;
  145. if (targetBlock.Instructions.Count == 1 && targetBlock.Instructions[0].MatchIfInstruction(out nestedCondition, out nestedTrueInst)) {
  146. // "if (...) { if (nestedCondition) nestedTrueInst; } exitInst;"
  147. // --> "if (... && nestedCondition) nestedTrueInst; } exitInst"
  148. context.Step("Combine if conditions into logic.and (in then-branch)", ifInst);
  149. ifInst.Condition = IfInstruction.LogicAnd(ifInst.Condition, nestedCondition);
  150. ifInst.TrueInst = nestedTrueInst;
  151. trueExitInst = (nestedTrueInst as Block)?.Instructions.LastOrDefault();
  152. }
  153. }
  154. } else {
  155. trueExitInst = ifInst.TrueInst;
  156. }
  157. if (IsUsableBranchToChild(cfgNode, exitInst)) {
  158. var targetBlock = ((Branch)exitInst).TargetBlock;
  159. var falseExitInst = targetBlock.Instructions.LastOrDefault();
  160. if (DetectExitPoints.CompatibleExitInstruction(trueExitInst, falseExitInst)) {
  161. // if (...) { ...; goto exitPoint; } goto nextBlock; nextBlock: ...; goto exitPoint;
  162. // -> if (...) { ... } else { ... } goto exitPoint;
  163. // the else block is not empty or nop-only:
  164. if (targetBlock.Children.Any(inst => !(inst is Nop) && inst != falseExitInst)) {
  165. context.Step("Inline block as else-branch", ifInst);
  166. targetBlock.Instructions.RemoveAt(targetBlock.Instructions.Count - 1);
  167. targetBlock.Remove();
  168. ifInst.FalseInst = targetBlock;
  169. } else {
  170. // the else block is empty or nop-only and can be safely removed:
  171. context.Step("Remove empty else-branch", ifInst);
  172. targetBlock.Instructions.RemoveAt(targetBlock.Instructions.Count - 1);
  173. targetBlock.Remove();
  174. }
  175. exitInst = block.Instructions[block.Instructions.Count - 1] = falseExitInst;
  176. Block trueBlock = ifInst.TrueInst as Block;
  177. if (trueBlock != null) {
  178. Debug.Assert(trueExitInst == trueBlock.Instructions.Last());
  179. trueBlock.Instructions.RemoveAt(trueBlock.Instructions.Count - 1);
  180. } else {
  181. Debug.Assert(trueExitInst == ifInst.TrueInst);
  182. ifInst.TrueInst = new Nop { ILRange = ifInst.TrueInst.ILRange };
  183. }
  184. }
  185. }
  186. if (IsEmpty(ifInst.TrueInst)) {
  187. // prefer empty true-branch to empty-else branch
  188. context.Step("Swap empty then-branch with else-branch", ifInst);
  189. var oldTrue = ifInst.TrueInst;
  190. ifInst.TrueInst = ifInst.FalseInst;
  191. ifInst.FalseInst = new Nop { ILRange = oldTrue.ILRange };
  192. ifInst.Condition = Comp.LogicNot(ifInst.Condition);
  193. // After swapping, it's possible that we can introduce a short-circuit operator:
  194. Block trueBlock = ifInst.TrueInst as Block;
  195. ILInstruction nestedCondition, nestedTrueInst;
  196. if (trueBlock != null && trueBlock.Instructions.Count == 1
  197. && trueBlock.FinalInstruction is Nop
  198. && trueBlock.Instructions[0].MatchIfInstruction(out nestedCondition, out nestedTrueInst)) {
  199. // if (cond) if (nestedCond) nestedTrueInst
  200. // ==> if (cond && nestedCond) nestedTrueInst
  201. context.Step("Combine if conditions into logic.and (after branch swapping)", ifInst);
  202. ifInst.Condition = IfInstruction.LogicAnd(ifInst.Condition, nestedCondition);
  203. ifInst.TrueInst = nestedTrueInst;
  204. }
  205. } else if (ifInst.FalseInst.OpCode != OpCode.Nop && ifInst.FalseInst.ILRange.Start < ifInst.TrueInst.ILRange.Start) {
  206. // swap true and false branches of if/else construct,
  207. // to bring them in the same order as the IL code
  208. context.Step("Swap then-branch with else-branch", ifInst);
  209. var oldTrue = ifInst.TrueInst;
  210. ifInst.TrueInst = ifInst.FalseInst;
  211. ifInst.FalseInst = oldTrue;
  212. ifInst.Condition = Comp.LogicNot(ifInst.Condition);
  213. }
  214. }
  215. static bool IsEmpty(ILInstruction inst)
  216. {
  217. var block = inst as Block;
  218. return block != null && block.Instructions.Count == 0 && block.FinalInstruction is Nop
  219. || inst is Nop;
  220. }
  221. private ILInstruction UnpackBlockContainingOnlyBranch(ILInstruction inst)
  222. {
  223. Block block = inst as Block;
  224. if (block != null && block.Instructions.Count == 1 && block.FinalInstruction is Nop && IsBranchOrLeave(block.Instructions[0]))
  225. return block.Instructions.Single();
  226. else
  227. return inst;
  228. }
  229. bool ShouldSwapIfTargets(ILInstruction inst1, ILInstruction inst2)
  230. {
  231. Block block1 = null, block2 = null;
  232. if (inst1.MatchBranch(out block1) && inst2.MatchBranch(out block2)) {
  233. // prefer arranging stuff in IL order
  234. return block1.ILRange.Start > block2.ILRange.Start;
  235. }
  236. BlockContainer container1, container2;
  237. if (inst1.MatchLeave(out container1) && container1.Parent is TryInstruction) {
  238. // 'leave tryBlock' is considered to have a later target than
  239. // any branch within the container, and also a later target
  240. // than a return instruction.
  241. // This is necessary to avoid "goto" statements in the
  242. // ExceptionHandling.ConditionalReturnInThrow test.
  243. if (!inst2.MatchLeave(out container2))
  244. container2 = block2?.Parent as BlockContainer;
  245. return container2 == null || container2.IsDescendantOf(container1);
  246. }
  247. if (inst1.MatchBranch(out block1) && inst2.MatchLeave(out container2)
  248. && block1.IncomingEdgeCount > 1)
  249. {
  250. // if (..) goto x; leave c;
  251. // Unless x can be inlined, it's better to swap the order if the 'leave'
  252. // has a chance to turn into a 'break;' or 'return;'
  253. if (container2.Parent is ILFunction) {
  254. return true; // return
  255. }
  256. if (container2.EntryPoint.IncomingEdgeCount > 1) {
  257. // break
  258. return BlockContainer.FindClosestContainer(inst2) == container2;
  259. }
  260. }
  261. return false;
  262. }
  263. /// <summary>
  264. /// Gets whether <c>potentialBranchInstruction</c> is a branch to a block
  265. /// that is dominated by <c>cfgNode</c>.
  266. /// If this function returns true, we replace the branch instruction with the block itself.
  267. /// </summary>
  268. bool IsUsableBranchToChild(ControlFlowNode cfgNode, ILInstruction potentialBranchInstruction)
  269. {
  270. Branch br = potentialBranchInstruction as Branch;
  271. if (br == null)
  272. return false;
  273. var targetBlock = br.TargetBlock;
  274. return targetBlock.Parent == currentContainer
  275. && targetBlock.IncomingEdgeCount == 1 && targetBlock.FinalInstruction.OpCode == OpCode.Nop
  276. && cfgNode.Dominates(context.ControlFlowGraph.GetNode(targetBlock));
  277. }
  278. private bool IsBranchOrLeave(ILInstruction inst)
  279. {
  280. switch (inst) {
  281. case Branch branch:
  282. return true;
  283. case Leave leave:
  284. // only void returns are supported as 'exit points'
  285. return leave.Value.MatchNop();
  286. default:
  287. return false;
  288. }
  289. }
  290. }
  291. }