X86Optimizer.cs 8.5 KB

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