Optimizer.ELDC.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ILRuntime.Mono.Cecil;
  6. using ILRuntime.Mono.Cecil.Cil;
  7. using ILRuntime.CLR.TypeSystem;
  8. using ILRuntime.CLR.Method;
  9. using ILRuntime.Runtime.Intepreter.OpCodes;
  10. namespace ILRuntime.Runtime.Intepreter.RegisterVM
  11. {
  12. partial class Optimizer
  13. {
  14. public static void EliminateConstantLoad(List<CodeBasicBlock> blocks, bool hasReturn)
  15. {
  16. foreach (var b in blocks)
  17. {
  18. if (!b.NeedLoadConstantElimination)
  19. continue;
  20. var lst = b.FinalInstructions;
  21. HashSet<int> canRemove = b.CanRemove;
  22. //HashSet<int> pendingBCP = b.PendingCP;
  23. bool isInline = false;
  24. for (int i = 0; i < lst.Count; i++)
  25. {
  26. OpCodeR X = lst[i];
  27. if (X.Code == OpCodeREnum.InlineStart)
  28. {
  29. isInline = true;
  30. continue;
  31. }
  32. if (X.Code == OpCodeREnum.InlineEnd)
  33. {
  34. isInline = false;
  35. continue;
  36. }
  37. if (isInline)
  38. continue;
  39. if (IsLoadConstant(X.Code))
  40. {
  41. short xDst;
  42. GetOpcodeDestRegister(ref X, out xDst);
  43. bool propagationInline = false;
  44. for (int j = i + 1; j < lst.Count; j++)
  45. {
  46. OpCodeR Y = lst[j];
  47. if (Y.Code == OpCodeREnum.InlineStart)
  48. propagationInline = true;
  49. else if (Y.Code == OpCodeREnum.InlineEnd)
  50. {
  51. propagationInline = false;
  52. }
  53. short r1, r2, r3;
  54. GetOpcodeSourceRegister(ref Y, hasReturn, out r1, out r2, out r3);
  55. if (r1 == xDst || r2 == xDst || r3 == xDst)
  56. {
  57. if (SupportIntemediateValue(Y.Code))
  58. {
  59. if (r2 == xDst)
  60. {
  61. if (!propagationInline)
  62. {
  63. Y.Code = GetIntemediateValueOpcode(Y.Code);
  64. ReplaceRegisterWithConstant(ref Y, ref X);
  65. lst[j] = Y;
  66. canRemove.Add(i);
  67. }
  68. }
  69. else if (r1 == xDst)
  70. {
  71. if (!propagationInline)
  72. {
  73. if (SupportOperandSwap(Y.Code))
  74. {
  75. ReplaceOpcodeSource(ref Y, 0, r2);
  76. Y.Code = GetIntemediateValueOpcode(Y.Code);
  77. ReplaceRegisterWithConstant(ref Y, ref X);
  78. lst[j] = Y;
  79. canRemove.Add(i);
  80. }
  81. else if (HasInverseOpcode(Y.Code))
  82. {
  83. ReplaceOpcodeSource(ref Y, 0, r2);
  84. Y.Code = GetIntemediateValueOpcode(GetInverseOpcode(Y.Code));
  85. ReplaceRegisterWithConstant(ref Y, ref X);
  86. lst[j] = Y;
  87. canRemove.Add(i);
  88. }
  89. }
  90. }
  91. }
  92. break;
  93. }
  94. short yDst;
  95. GetOpcodeDestRegister(ref Y, out yDst);
  96. if (yDst == xDst)
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }