Optimizer.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using ARMeilleure.IntermediateRepresentation;
  2. using ARMeilleure.Translation;
  3. using System.Diagnostics;
  4. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  5. namespace ARMeilleure.CodeGen.Optimizations
  6. {
  7. static class Optimizer
  8. {
  9. public static void RunPass(ControlFlowGraph cfg)
  10. {
  11. bool modified;
  12. do
  13. {
  14. modified = false;
  15. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  16. {
  17. Node node = block.Operations.First;
  18. while (node != null)
  19. {
  20. Node nextNode = node.ListNext;
  21. bool isUnused = IsUnused(node);
  22. if (!(node is Operation operation) || isUnused)
  23. {
  24. if (isUnused)
  25. {
  26. RemoveNode(block, node);
  27. modified = true;
  28. }
  29. node = nextNode;
  30. continue;
  31. }
  32. ConstantFolding.RunPass(operation);
  33. Simplification.RunPass(operation);
  34. if (DestIsLocalVar(operation))
  35. {
  36. if (IsPropagableCompare(operation))
  37. {
  38. modified |= PropagateCompare(operation);
  39. if (modified && IsUnused(operation))
  40. {
  41. RemoveNode(block, node);
  42. }
  43. }
  44. else if (IsPropagableCopy(operation))
  45. {
  46. PropagateCopy(operation);
  47. RemoveNode(block, node);
  48. modified = true;
  49. }
  50. }
  51. node = nextNode;
  52. }
  53. }
  54. }
  55. while (modified);
  56. }
  57. public static void RemoveUnusedNodes(ControlFlowGraph cfg)
  58. {
  59. bool modified;
  60. do
  61. {
  62. modified = false;
  63. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  64. {
  65. Node node = block.Operations.First;
  66. while (node != null)
  67. {
  68. Node nextNode = node.ListNext;
  69. if (IsUnused(node))
  70. {
  71. RemoveNode(block, node);
  72. modified = true;
  73. }
  74. node = nextNode;
  75. }
  76. }
  77. }
  78. while (modified);
  79. }
  80. private static bool PropagateCompare(Operation compOp)
  81. {
  82. // Try to propagate Compare operations into their BranchIf uses, when these BranchIf uses are in the form
  83. // of:
  84. //
  85. // - BranchIf %x, 0x0, Equal ;; i.e BranchIfFalse %x
  86. // - BranchIf %x, 0x0, NotEqual ;; i.e BranchIfTrue %x
  87. //
  88. // The commutative property of Equal and NotEqual is taken into consideration as well.
  89. //
  90. // For example:
  91. //
  92. // %x = Compare %a, %b, comp
  93. // BranchIf %x, 0x0, NotEqual
  94. //
  95. // =>
  96. //
  97. // BranchIf %a, %b, comp
  98. static bool IsZeroBranch(Operation operation, out Comparison compType)
  99. {
  100. compType = Comparison.Equal;
  101. if (operation.Instruction != Instruction.BranchIf)
  102. {
  103. return false;
  104. }
  105. Operand src1 = operation.GetSource(0);
  106. Operand src2 = operation.GetSource(1);
  107. Operand comp = operation.GetSource(2);
  108. compType = (Comparison)comp.AsInt32();
  109. return (src1.Kind == OperandKind.Constant && src1.Value == 0) ||
  110. (src2.Kind == OperandKind.Constant && src2.Value == 0);
  111. }
  112. bool modified = false;
  113. Operand dest = compOp.Destination;
  114. Operand src1 = compOp.GetSource(0);
  115. Operand src2 = compOp.GetSource(1);
  116. Operand comp = compOp.GetSource(2);
  117. Comparison compType = (Comparison)comp.AsInt32();
  118. Node[] uses = dest.Uses.ToArray();
  119. foreach (Node use in uses)
  120. {
  121. if (!(use is Operation operation))
  122. {
  123. continue;
  124. }
  125. // If operation is a BranchIf and has a constant value 0 in its RHS or LHS source operands.
  126. if (IsZeroBranch(operation, out Comparison otherCompType))
  127. {
  128. Comparison propCompType;
  129. if (otherCompType == Comparison.NotEqual)
  130. {
  131. propCompType = compType;
  132. }
  133. else if (otherCompType == Comparison.Equal)
  134. {
  135. propCompType = compType.Invert();
  136. }
  137. else
  138. {
  139. continue;
  140. }
  141. operation.SetSource(0, src1);
  142. operation.SetSource(1, src2);
  143. operation.SetSource(2, Const((int)propCompType));
  144. modified = true;
  145. }
  146. }
  147. return modified;
  148. }
  149. private static void PropagateCopy(Operation copyOp)
  150. {
  151. // Propagate copy source operand to all uses of the destination operand.
  152. Operand dest = copyOp.Destination;
  153. Operand source = copyOp.GetSource(0);
  154. Node[] uses = dest.Uses.ToArray();
  155. foreach (Node use in uses)
  156. {
  157. for (int index = 0; index < use.SourcesCount; index++)
  158. {
  159. if (use.GetSource(index) == dest)
  160. {
  161. use.SetSource(index, source);
  162. }
  163. }
  164. }
  165. }
  166. private static void RemoveNode(BasicBlock block, Node node)
  167. {
  168. // Remove a node from the nodes list, and also remove itself
  169. // from all the use lists on the operands that this node uses.
  170. block.Operations.Remove(node);
  171. for (int index = 0; index < node.SourcesCount; index++)
  172. {
  173. node.SetSource(index, null);
  174. }
  175. Debug.Assert(node.Destination == null || node.Destination.Uses.Count == 0);
  176. node.Destination = null;
  177. }
  178. private static bool IsUnused(Node node)
  179. {
  180. return DestIsLocalVar(node) && node.Destination.Uses.Count == 0 && !HasSideEffects(node);
  181. }
  182. private static bool DestIsLocalVar(Node node)
  183. {
  184. return node.Destination != null && node.Destination.Kind == OperandKind.LocalVariable;
  185. }
  186. private static bool HasSideEffects(Node node)
  187. {
  188. return (node is Operation operation) && (operation.Instruction == Instruction.Call
  189. || operation.Instruction == Instruction.Tailcall
  190. || operation.Instruction == Instruction.CompareAndSwap
  191. || operation.Instruction == Instruction.CompareAndSwap16
  192. || operation.Instruction == Instruction.CompareAndSwap8);
  193. }
  194. private static bool IsPropagableCompare(Operation operation)
  195. {
  196. return operation.Instruction == Instruction.Compare;
  197. }
  198. private static bool IsPropagableCopy(Operation operation)
  199. {
  200. if (operation.Instruction != Instruction.Copy)
  201. {
  202. return false;
  203. }
  204. return operation.Destination.Type == operation.GetSource(0).Type;
  205. }
  206. }
  207. }