X86Optimizer.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using ARMeilleure.CodeGen.Optimizations;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System.Collections.Generic;
  5. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  6. using static ARMeilleure.IntermediateRepresentation.Operation.Factory;
  7. namespace ARMeilleure.CodeGen.X86
  8. {
  9. static class X86Optimizer
  10. {
  11. private const int MaxConstantUses = 10000;
  12. public static void RunPass(ControlFlowGraph cfg)
  13. {
  14. var constants = new Dictionary<ulong, Operand>();
  15. Operand GetConstantCopy(BasicBlock block, Operation operation, Operand source)
  16. {
  17. // If the constant has many uses, we also force a new constant mov to be added, in order
  18. // to avoid overflow of the counts field (that is limited to 16 bits).
  19. if (!constants.TryGetValue(source.Value, out var constant) || constant.UsesCount > MaxConstantUses)
  20. {
  21. constant = Local(source.Type);
  22. Operation copyOp = Operation(Instruction.Copy, constant, source);
  23. block.Operations.AddBefore(operation, copyOp);
  24. constants[source.Value] = constant;
  25. }
  26. return constant;
  27. }
  28. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  29. {
  30. constants.Clear();
  31. Operation nextNode;
  32. for (Operation node = block.Operations.First; node != default; node = nextNode)
  33. {
  34. nextNode = node.ListNext;
  35. // Insert copies for constants that can't fit on a 32-bits immediate.
  36. // Doing this early unblocks a few optimizations.
  37. if (node.Instruction == Instruction.Add)
  38. {
  39. Operand src1 = node.GetSource(0);
  40. Operand src2 = node.GetSource(1);
  41. if (src1.Kind == OperandKind.Constant && (src1.Relocatable || CodeGenCommon.IsLongConst(src1)))
  42. {
  43. node.SetSource(0, GetConstantCopy(block, node, src1));
  44. }
  45. if (src2.Kind == OperandKind.Constant && (src2.Relocatable || CodeGenCommon.IsLongConst(src2)))
  46. {
  47. node.SetSource(1, GetConstantCopy(block, node, src2));
  48. }
  49. }
  50. // Try to fold something like:
  51. // shl rbx, 2
  52. // add rax, rbx
  53. // add rax, 0xcafe
  54. // mov rax, [rax]
  55. // Into:
  56. // mov rax, [rax+rbx*4+0xcafe]
  57. if (IsMemoryLoadOrStore(node.Instruction))
  58. {
  59. OperandType type;
  60. if (node.Destination != default)
  61. {
  62. type = node.Destination.Type;
  63. }
  64. else
  65. {
  66. type = node.GetSource(1).Type;
  67. }
  68. Operand memOp = GetMemoryOperandOrNull(node.GetSource(0), type);
  69. if (memOp != default)
  70. {
  71. node.SetSource(0, memOp);
  72. }
  73. }
  74. }
  75. }
  76. Optimizer.RemoveUnusedNodes(cfg);
  77. }
  78. private static Operand GetMemoryOperandOrNull(Operand addr, OperandType type)
  79. {
  80. Operand baseOp = addr;
  81. // First we check if the address is the result of a local X with 32-bits immediate
  82. // addition. If that is the case, then the baseOp is X, and the memory operand immediate
  83. // becomes the addition immediate. Otherwise baseOp keeps being the address.
  84. int imm = GetConstOp(ref baseOp);
  85. // Now we check if the baseOp is the result of a local Y with a local Z addition.
  86. // If that is the case, we now set baseOp to Y and indexOp to Z. We further check
  87. // if Z is the result of a left shift of local W by a value >= 0 and <= 3, if that
  88. // is the case, we set indexOp to W and adjust the scale value of the memory operand
  89. // to match that of the left shift.
  90. // There is one missed case, which is the address being a shift result, but this is
  91. // probably not worth optimizing as it should never happen.
  92. (Operand indexOp, Multiplier scale) = GetIndexOp(ref baseOp);
  93. // If baseOp is still equal to address, then there's nothing that can be optimized.
  94. if (baseOp == addr)
  95. {
  96. return default;
  97. }
  98. if (imm == 0 && scale == Multiplier.x1 && indexOp != default)
  99. {
  100. imm = GetConstOp(ref indexOp);
  101. }
  102. return MemoryOp(type, baseOp, indexOp, scale, imm);
  103. }
  104. private static int GetConstOp(ref Operand baseOp)
  105. {
  106. Operation operation = GetAsgOpWithInst(baseOp, Instruction.Add);
  107. if (operation == default)
  108. {
  109. return 0;
  110. }
  111. Operand src1 = operation.GetSource(0);
  112. Operand src2 = operation.GetSource(1);
  113. Operand constOp;
  114. Operand otherOp;
  115. if (src1.Kind == OperandKind.Constant && src2.Kind == OperandKind.LocalVariable)
  116. {
  117. constOp = src1;
  118. otherOp = src2;
  119. }
  120. else if (src1.Kind == OperandKind.LocalVariable && src2.Kind == OperandKind.Constant)
  121. {
  122. constOp = src2;
  123. otherOp = src1;
  124. }
  125. else
  126. {
  127. return 0;
  128. }
  129. // If we have addition by 64-bits constant, then we can't optimize it further,
  130. // as we can't encode a 64-bits immediate on the memory operand.
  131. if (CodeGenCommon.IsLongConst(constOp))
  132. {
  133. return 0;
  134. }
  135. baseOp = otherOp;
  136. return constOp.AsInt32();
  137. }
  138. private static (Operand, Multiplier) GetIndexOp(ref Operand baseOp)
  139. {
  140. Operand indexOp = default;
  141. Multiplier scale = Multiplier.x1;
  142. Operation addOp = GetAsgOpWithInst(baseOp, Instruction.Add);
  143. if (addOp == default)
  144. {
  145. return (indexOp, scale);
  146. }
  147. Operand src1 = addOp.GetSource(0);
  148. Operand src2 = addOp.GetSource(1);
  149. if (src1.Kind != OperandKind.LocalVariable || src2.Kind != OperandKind.LocalVariable)
  150. {
  151. return (indexOp, scale);
  152. }
  153. baseOp = src1;
  154. indexOp = src2;
  155. Operation shlOp = GetAsgOpWithInst(src1, Instruction.ShiftLeft);
  156. bool indexOnSrc2 = false;
  157. if (shlOp == default)
  158. {
  159. shlOp = GetAsgOpWithInst(src2, Instruction.ShiftLeft);
  160. indexOnSrc2 = true;
  161. }
  162. if (shlOp != default)
  163. {
  164. Operand shSrc = shlOp.GetSource(0);
  165. Operand shift = shlOp.GetSource(1);
  166. if (shSrc.Kind == OperandKind.LocalVariable && shift.Kind == OperandKind.Constant && shift.Value <= 3)
  167. {
  168. scale = shift.Value switch
  169. {
  170. 1 => Multiplier.x2,
  171. 2 => Multiplier.x4,
  172. 3 => Multiplier.x8,
  173. _ => Multiplier.x1
  174. };
  175. baseOp = indexOnSrc2 ? src1 : src2;
  176. indexOp = shSrc;
  177. }
  178. }
  179. return (indexOp, scale);
  180. }
  181. private static Operation GetAsgOpWithInst(Operand op, Instruction inst)
  182. {
  183. // If we have multiple assignments, folding is not safe
  184. // as the value may be different depending on the
  185. // control flow path.
  186. if (op.AssignmentsCount != 1)
  187. {
  188. return default;
  189. }
  190. Operation asgOp = op.Assignments[0];
  191. if (asgOp.Instruction != inst)
  192. {
  193. return default;
  194. }
  195. return asgOp;
  196. }
  197. private static bool IsMemoryLoadOrStore(Instruction inst)
  198. {
  199. return inst == Instruction.Load ||
  200. inst == Instruction.Load16 ||
  201. inst == Instruction.Load8 ||
  202. inst == Instruction.Store ||
  203. inst == Instruction.Store16 ||
  204. inst == Instruction.Store8;
  205. }
  206. }
  207. }