X86Optimizer.cs 8.3 KB

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