GotoElimination.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System;
  3. using System.Collections.Generic;
  4. using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
  5. namespace Ryujinx.Graphics.Shader.StructuredIr
  6. {
  7. static class GotoElimination
  8. {
  9. // This is a modified version of the algorithm presented on the paper
  10. // "Taming Control Flow: A Structured Approach to Eliminating Goto Statements".
  11. public static void Eliminate(GotoStatement[] gotos)
  12. {
  13. for (int index = gotos.Length - 1; index >= 0; index--)
  14. {
  15. GotoStatement stmt = gotos[index];
  16. AstBlock gBlock = ParentBlock(stmt.Goto);
  17. AstBlock lBlock = ParentBlock(stmt.Label);
  18. int gLevel = Level(gBlock);
  19. int lLevel = Level(lBlock);
  20. if (IndirectlyRelated(gBlock, lBlock, gLevel, lLevel))
  21. {
  22. AstBlock drBlock = gBlock;
  23. int drLevel = gLevel;
  24. do
  25. {
  26. drBlock = drBlock.Parent;
  27. drLevel--;
  28. }
  29. while (!DirectlyRelated(drBlock, lBlock, drLevel, lLevel));
  30. MoveOutward(stmt, gLevel, drLevel);
  31. gBlock = drBlock;
  32. gLevel = drLevel;
  33. if (Previous(stmt.Goto) is AstBlock elseBlock && elseBlock.Type == AstBlockType.Else)
  34. {
  35. // It's possible that the label was enclosed inside an else block,
  36. // in this case we need to update the block and level.
  37. // We also need to set the IsLoop for the case when the label is
  38. // now before the goto, due to the newly introduced else block.
  39. lBlock = ParentBlock(stmt.Label);
  40. lLevel = Level(lBlock);
  41. if (!IndirectlyRelated(elseBlock, lBlock, gLevel + 1, lLevel))
  42. {
  43. stmt.IsLoop = true;
  44. }
  45. }
  46. }
  47. if (DirectlyRelated(gBlock, lBlock, gLevel, lLevel))
  48. {
  49. if (gLevel > lLevel)
  50. {
  51. MoveOutward(stmt, gLevel, lLevel);
  52. }
  53. else
  54. {
  55. if (stmt.IsLoop)
  56. {
  57. Lift(stmt);
  58. }
  59. MoveInward(stmt);
  60. }
  61. }
  62. gBlock = ParentBlock(stmt.Goto);
  63. if (stmt.IsLoop)
  64. {
  65. EncloseDoWhile(stmt, gBlock, stmt.Label);
  66. }
  67. else
  68. {
  69. Enclose(gBlock, AstBlockType.If, stmt.Condition, Next(stmt.Goto), stmt.Label);
  70. }
  71. gBlock.Remove(stmt.Goto);
  72. }
  73. }
  74. private static bool IndirectlyRelated(AstBlock lBlock, AstBlock rBlock, int lLevel, int rlevel)
  75. {
  76. return !(lBlock == rBlock || DirectlyRelated(lBlock, rBlock, lLevel, rlevel));
  77. }
  78. private static bool DirectlyRelated(AstBlock lBlock, AstBlock rBlock, int lLevel, int rLevel)
  79. {
  80. // If the levels are equal, they can be either siblings or indirectly related.
  81. if (lLevel == rLevel)
  82. {
  83. return false;
  84. }
  85. IAstNode block;
  86. IAstNode other;
  87. int blockLvl, otherLvl;
  88. if (lLevel > rLevel)
  89. {
  90. block = lBlock;
  91. blockLvl = lLevel;
  92. other = rBlock;
  93. otherLvl = rLevel;
  94. }
  95. else /* if (rLevel > lLevel) */
  96. {
  97. block = rBlock;
  98. blockLvl = rLevel;
  99. other = lBlock;
  100. otherLvl = lLevel;
  101. }
  102. while (blockLvl >= otherLvl)
  103. {
  104. if (block == other)
  105. {
  106. return true;
  107. }
  108. block = block.Parent;
  109. blockLvl--;
  110. }
  111. return false;
  112. }
  113. private static void Lift(GotoStatement stmt)
  114. {
  115. AstBlock block = ParentBlock(stmt.Goto);
  116. AstBlock[] path = BackwardsPath(block, ParentBlock(stmt.Label));
  117. AstBlock loopFirstStmt = path[path.Length - 1];
  118. if (loopFirstStmt.Type == AstBlockType.Else)
  119. {
  120. loopFirstStmt = Previous(loopFirstStmt) as AstBlock;
  121. if (loopFirstStmt == null || loopFirstStmt.Type != AstBlockType.If)
  122. {
  123. throw new InvalidOperationException("Found an else without a matching if.");
  124. }
  125. }
  126. AstBlock newBlock = EncloseDoWhile(stmt, block, loopFirstStmt);
  127. block.Remove(stmt.Goto);
  128. newBlock.AddFirst(stmt.Goto);
  129. stmt.IsLoop = false;
  130. }
  131. private static void MoveOutward(GotoStatement stmt, int gLevel, int lLevel)
  132. {
  133. AstBlock origin = ParentBlock(stmt.Goto);
  134. AstBlock block = origin;
  135. // Check if a loop is enclosing the goto, and the block that is
  136. // directly related to the label is above the loop block.
  137. // In that case, we need to introduce a break to get out of the loop.
  138. AstBlock loopBlock = origin;
  139. int loopLevel = gLevel;
  140. while (loopLevel > lLevel)
  141. {
  142. AstBlock child = loopBlock;
  143. loopBlock = loopBlock.Parent;
  144. loopLevel--;
  145. if (child.Type == AstBlockType.DoWhile)
  146. {
  147. EncloseSingleInst(stmt, Instruction.LoopBreak);
  148. block.Remove(stmt.Goto);
  149. loopBlock.AddAfter(child, stmt.Goto);
  150. block = loopBlock;
  151. gLevel = loopLevel;
  152. }
  153. }
  154. // Insert ifs to skip the parts that shouldn't be executed due to the goto.
  155. bool tryInsertElse = stmt.IsUnconditional && origin.Type == AstBlockType.If;
  156. while (gLevel > lLevel)
  157. {
  158. Enclose(block, AstBlockType.If, stmt.Condition, Next(stmt.Goto));
  159. block.Remove(stmt.Goto);
  160. AstBlock child = block;
  161. // We can't move the goto in the middle of a if and a else block, in
  162. // this case we need to move it after the else.
  163. // IsLoop may need to be updated if the label is inside the else, as
  164. // introducing a loop is the only way to ensure the else will be executed.
  165. if (Next(child) is AstBlock elseBlock && elseBlock.Type == AstBlockType.Else)
  166. {
  167. child = elseBlock;
  168. }
  169. block = block.Parent;
  170. block.AddAfter(child, stmt.Goto);
  171. gLevel--;
  172. if (tryInsertElse && child == origin)
  173. {
  174. AstBlock lBlock = ParentBlock(stmt.Label);
  175. IAstNode last = block == lBlock && !stmt.IsLoop ? stmt.Label : null;
  176. AstBlock newBlock = Enclose(block, AstBlockType.Else, null, Next(stmt.Goto), last);
  177. if (newBlock != null)
  178. {
  179. block.Remove(stmt.Goto);
  180. block.AddAfter(newBlock, stmt.Goto);
  181. }
  182. }
  183. }
  184. }
  185. private static void MoveInward(GotoStatement stmt)
  186. {
  187. AstBlock block = ParentBlock(stmt.Goto);
  188. AstBlock[] path = BackwardsPath(block, ParentBlock(stmt.Label));
  189. for (int index = path.Length - 1; index >= 0; index--)
  190. {
  191. AstBlock child = path[index];
  192. AstBlock last = child;
  193. if (child.Type == AstBlockType.If)
  194. {
  195. // Modify the if condition to allow it to be entered by the goto.
  196. if (!ContainsCondComb(child.Condition, Instruction.LogicalOr, stmt.Condition))
  197. {
  198. child.OrCondition(stmt.Condition);
  199. }
  200. }
  201. else if (child.Type == AstBlockType.Else)
  202. {
  203. // Modify the matching if condition to force the else to be entered by the goto.
  204. if (!(Previous(child) is AstBlock ifBlock) || ifBlock.Type != AstBlockType.If)
  205. {
  206. throw new InvalidOperationException("Found an else without a matching if.");
  207. }
  208. IAstNode cond = InverseCond(stmt.Condition);
  209. if (!ContainsCondComb(ifBlock.Condition, Instruction.LogicalAnd, cond))
  210. {
  211. ifBlock.AndCondition(cond);
  212. }
  213. last = ifBlock;
  214. }
  215. Enclose(block, AstBlockType.If, stmt.Condition, Next(stmt.Goto), last);
  216. block.Remove(stmt.Goto);
  217. child.AddFirst(stmt.Goto);
  218. block = child;
  219. }
  220. }
  221. private static bool ContainsCondComb(IAstNode node, Instruction inst, IAstNode newCond)
  222. {
  223. while (node is AstOperation operation && operation.SourcesCount == 2)
  224. {
  225. if (operation.Inst == inst && IsSameCond(operation.GetSource(1), newCond))
  226. {
  227. return true;
  228. }
  229. node = operation.GetSource(0);
  230. }
  231. return false;
  232. }
  233. private static AstBlock EncloseDoWhile(GotoStatement stmt, AstBlock block, IAstNode first)
  234. {
  235. if (block.Type == AstBlockType.DoWhile && first == block.First)
  236. {
  237. // We only need to insert the continue if we're not at the end of the loop,
  238. // or if our condition is different from the loop condition.
  239. if (Next(stmt.Goto) != null || block.Condition != stmt.Condition)
  240. {
  241. EncloseSingleInst(stmt, Instruction.LoopContinue);
  242. }
  243. // Modify the do-while condition to allow it to continue.
  244. if (!ContainsCondComb(block.Condition, Instruction.LogicalOr, stmt.Condition))
  245. {
  246. block.OrCondition(stmt.Condition);
  247. }
  248. return block;
  249. }
  250. return Enclose(block, AstBlockType.DoWhile, stmt.Condition, first, stmt.Goto);
  251. }
  252. private static void EncloseSingleInst(GotoStatement stmt, Instruction inst)
  253. {
  254. AstBlock block = ParentBlock(stmt.Goto);
  255. AstBlock newBlock = new AstBlock(AstBlockType.If, stmt.Condition);
  256. block.AddAfter(stmt.Goto, newBlock);
  257. newBlock.AddFirst(new AstOperation(inst));
  258. }
  259. private static AstBlock Enclose(
  260. AstBlock block,
  261. AstBlockType type,
  262. IAstNode cond,
  263. IAstNode first,
  264. IAstNode last = null)
  265. {
  266. if (first == last)
  267. {
  268. return null;
  269. }
  270. if (type == AstBlockType.If)
  271. {
  272. cond = InverseCond(cond);
  273. }
  274. // Do a quick check, if we are enclosing a single block,
  275. // and the block type/condition matches the one we're going
  276. // to create, then we don't need a new block, we can just
  277. // return the old one.
  278. bool hasSingleNode = Next(first) == last;
  279. if (hasSingleNode && BlockMatches(first, type, cond))
  280. {
  281. return first as AstBlock;
  282. }
  283. AstBlock newBlock = new AstBlock(type, cond);
  284. block.AddBefore(first, newBlock);
  285. while (first != last)
  286. {
  287. IAstNode next = Next(first);
  288. block.Remove(first);
  289. newBlock.Add(first);
  290. first = next;
  291. }
  292. return newBlock;
  293. }
  294. private static bool BlockMatches(IAstNode node, AstBlockType type, IAstNode cond)
  295. {
  296. if (!(node is AstBlock block))
  297. {
  298. return false;
  299. }
  300. return block.Type == type && IsSameCond(block.Condition, cond);
  301. }
  302. private static bool IsSameCond(IAstNode lCond, IAstNode rCond)
  303. {
  304. if (lCond is AstOperation lCondOp && lCondOp.Inst == Instruction.LogicalNot)
  305. {
  306. if (!(rCond is AstOperation rCondOp) || rCondOp.Inst != lCondOp.Inst)
  307. {
  308. return false;
  309. }
  310. lCond = lCondOp.GetSource(0);
  311. rCond = rCondOp.GetSource(0);
  312. }
  313. return lCond == rCond;
  314. }
  315. private static AstBlock ParentBlock(IAstNode node)
  316. {
  317. if (node is AstBlock block)
  318. {
  319. return block.Parent;
  320. }
  321. while (!(node is AstBlock))
  322. {
  323. node = node.Parent;
  324. }
  325. return node as AstBlock;
  326. }
  327. private static AstBlock[] BackwardsPath(AstBlock top, AstBlock bottom)
  328. {
  329. AstBlock block = bottom;
  330. List<AstBlock> path = new List<AstBlock>();
  331. while (block != top)
  332. {
  333. path.Add(block);
  334. block = block.Parent;
  335. }
  336. return path.ToArray();
  337. }
  338. private static int Level(IAstNode node)
  339. {
  340. int level = 0;
  341. while (node != null)
  342. {
  343. level++;
  344. node = node.Parent;
  345. }
  346. return level;
  347. }
  348. }
  349. }