Optimizer.FCP.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 ForwardCopyPropagation(List<CodeBasicBlock> blocks, bool hasReturn,short stackRegisterBegin)
  15. {
  16. foreach (var b in blocks)
  17. {
  18. var lst = b.FinalInstructions;
  19. HashSet<int> canRemove = b.CanRemove;
  20. HashSet<int> pendingFCP = b.PendingCP;
  21. bool isInline = false;
  22. for (int i = 0; i < lst.Count; i++)
  23. {
  24. if (canRemove.Contains(i))
  25. continue;
  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 (X.Code == OpCodeREnum.Nop || X.Code == OpCodeREnum.Castclass || X.Code == OpCodeREnum.Readonly || X.Code == OpCodeREnum.Volatile)
  40. {
  41. canRemove.Add(i);
  42. continue;
  43. }
  44. if (X.Code == OpCodeREnum.Move)
  45. {
  46. short xSrc, xSrc2, xSrc3, xDst;
  47. GetOpcodeSourceRegister(ref X, hasReturn, out xSrc, out xSrc2, out xSrc3);
  48. GetOpcodeDestRegister(ref X, out xDst);
  49. if (xDst == xSrc)
  50. {
  51. canRemove.Add(i);
  52. continue;
  53. }
  54. //Only deal with local->stack, local->local, stack->stack
  55. if (xSrc >= stackRegisterBegin && xDst < stackRegisterBegin || isInline)
  56. continue;
  57. bool postPropagation = false;
  58. bool ended = false;
  59. bool propagationInline = false;
  60. for (int j = i + 1; j < lst.Count; j++)
  61. {
  62. OpCodeR Y = lst[j];
  63. if (Y.Code == OpCodeREnum.InlineStart)
  64. propagationInline = true;
  65. else if (Y.Code == OpCodeREnum.InlineEnd)
  66. {
  67. propagationInline = false;
  68. }
  69. short ySrc, ySrc2, ySrc3;
  70. if (GetOpcodeSourceRegister(ref Y, hasReturn, out ySrc, out ySrc2, out ySrc3))
  71. {
  72. bool replaced = false;
  73. if (ySrc >= 0 && ySrc == xDst)
  74. {
  75. if (postPropagation)
  76. {
  77. postPropagation = false;
  78. ended = true;
  79. break;
  80. }
  81. if(propagationInline)
  82. {
  83. ended = true;
  84. break;
  85. }
  86. ReplaceOpcodeSource(ref Y, 0, xSrc);
  87. replaced = true;
  88. }
  89. if (ySrc2 >= 0 && ySrc2 == xDst)
  90. {
  91. if (postPropagation)
  92. {
  93. postPropagation = false;
  94. ended = true;
  95. break;
  96. }
  97. if (propagationInline)
  98. {
  99. ended = true;
  100. break;
  101. }
  102. ReplaceOpcodeSource(ref Y, 1, xSrc);
  103. replaced = true;
  104. }
  105. if (ySrc3 >= 0 && ySrc3 == xDst)
  106. {
  107. if (postPropagation)
  108. {
  109. postPropagation = false;
  110. ended = true;
  111. break;
  112. }
  113. if (propagationInline)
  114. {
  115. ended = true;
  116. break;
  117. }
  118. ReplaceOpcodeSource(ref Y, 2, xSrc);
  119. replaced = true;
  120. }
  121. if (replaced)
  122. lst[j] = Y;
  123. }
  124. short yDst;
  125. if (GetOpcodeDestRegister(ref Y, out yDst))
  126. {
  127. if (xSrc == yDst)
  128. {
  129. postPropagation = true;
  130. }
  131. if (xDst == yDst)
  132. {
  133. postPropagation = false;
  134. ended = true;
  135. if (!propagationInline)
  136. canRemove.Add(i);
  137. break;
  138. }
  139. }
  140. if(Y.Code == OpCodeREnum.Ret && !propagationInline)
  141. {
  142. postPropagation = false;
  143. canRemove.Add(i);
  144. ended = true;
  145. break;
  146. }
  147. }
  148. if (postPropagation || !ended)
  149. {
  150. if (xDst >= stackRegisterBegin)
  151. pendingFCP.Add(i);
  152. }
  153. }
  154. }
  155. }
  156. foreach(var b in blocks)
  157. {
  158. var pendingFCP = b.PendingCP;
  159. if (pendingFCP.Count > 0)
  160. {
  161. var originBlock = b;
  162. HashSet<CodeBasicBlock> processedBlocks = new HashSet<CodeBasicBlock>();
  163. Queue<CodeBasicBlock> pendingBlocks = new Queue<CodeBasicBlock>();
  164. foreach (var idx in pendingFCP)
  165. {
  166. var X = originBlock.FinalInstructions[idx];
  167. short xDst, xSrc, xSrc2, xSrc3;
  168. GetOpcodeDestRegister(ref X, out xDst);
  169. GetOpcodeSourceRegister(ref X, hasReturn, out xSrc, out xSrc2, out xSrc3);
  170. pendingBlocks.Clear();
  171. bool cannotRemove = false;
  172. bool isAbort = false;
  173. processedBlocks.Clear();
  174. foreach (var nb in originBlock.NextBlocks)
  175. pendingBlocks.Enqueue(nb);
  176. processedBlocks.Add(originBlock);
  177. while (pendingBlocks.Count > 0)
  178. {
  179. var cur = pendingBlocks.Dequeue();
  180. var ins = cur.FinalInstructions;
  181. bool propagationInline = false;
  182. for (int j = 0; j < ins.Count; j++)
  183. {
  184. if(cur == originBlock && j == idx)
  185. {
  186. isAbort = true;
  187. break;
  188. }
  189. var Y = ins[j];
  190. if (Y.Code == OpCodeREnum.InlineStart)
  191. propagationInline = true;
  192. else if (Y.Code == OpCodeREnum.InlineEnd)
  193. {
  194. propagationInline = false;
  195. }
  196. short ySrc, ySrc2, ySrc3, yDst;
  197. if (GetOpcodeSourceRegister(ref Y, hasReturn, out ySrc, out ySrc2, out ySrc3))
  198. {
  199. bool replaced = false;
  200. if (ySrc == xDst)
  201. {
  202. if (propagationInline || cur.PreviousBlocks.Count > 1)
  203. {
  204. cannotRemove = true;
  205. break;
  206. }
  207. replaced = true;
  208. ReplaceOpcodeSource(ref Y, 0, xSrc);
  209. }
  210. if (ySrc2 == xDst)
  211. {
  212. if (propagationInline || cur.PreviousBlocks.Count > 1)
  213. {
  214. cannotRemove = true;
  215. break;
  216. }
  217. replaced = true;
  218. ReplaceOpcodeSource(ref Y, 1, xSrc);
  219. }
  220. if (ySrc3 == xDst)
  221. {
  222. if (propagationInline || cur.PreviousBlocks.Count > 1)
  223. {
  224. cannotRemove = true;
  225. break;
  226. }
  227. replaced = true;
  228. ReplaceOpcodeSource(ref Y, 2, xSrc);
  229. }
  230. if (replaced)
  231. ins[j] = Y;
  232. }
  233. if(GetOpcodeDestRegister(ref Y, out yDst))
  234. {
  235. if(yDst == xDst)
  236. {
  237. isAbort = true;
  238. break;
  239. }
  240. }
  241. if(Y.Code == OpCodeREnum.Ret && !propagationInline)
  242. {
  243. isAbort = true;
  244. break;
  245. }
  246. }
  247. if (cannotRemove)
  248. break;
  249. processedBlocks.Add(cur);
  250. if (!isAbort)
  251. {
  252. foreach (var nb in cur.NextBlocks)
  253. {
  254. if (!processedBlocks.Contains(nb))
  255. pendingBlocks.Enqueue(nb);
  256. }
  257. }
  258. }
  259. if(!cannotRemove)
  260. {
  261. originBlock.CanRemove.Add(idx);
  262. }
  263. }
  264. pendingFCP.Clear();
  265. }
  266. }
  267. }
  268. }
  269. }