Operation.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Diagnostics;
  3. namespace Ryujinx.Graphics.Shader.IntermediateRepresentation
  4. {
  5. class Operation : INode
  6. {
  7. public Instruction Inst { get; private set; }
  8. public StorageKind StorageKind { get; }
  9. public bool ForcePrecise { get; set; }
  10. private Operand[] _dests;
  11. public Operand Dest
  12. {
  13. get
  14. {
  15. return _dests.Length != 0 ? _dests[0] : null;
  16. }
  17. set
  18. {
  19. if (value != null)
  20. {
  21. if (value.Type == OperandType.LocalVariable)
  22. {
  23. value.AsgOp = this;
  24. }
  25. _dests = new[] { value };
  26. }
  27. else
  28. {
  29. _dests = Array.Empty<Operand>();
  30. }
  31. }
  32. }
  33. public int DestsCount => _dests.Length;
  34. private Operand[] _sources;
  35. public int SourcesCount => _sources.Length;
  36. public int Index { get; }
  37. private Operation(Operand[] sources)
  38. {
  39. // The array may be modified externally, so we store a copy.
  40. _sources = (Operand[])sources.Clone();
  41. for (int index = 0; index < _sources.Length; index++)
  42. {
  43. Operand source = _sources[index];
  44. if (source.Type == OperandType.LocalVariable)
  45. {
  46. source.UseOps.Add(this);
  47. }
  48. }
  49. }
  50. public Operation(Instruction inst, int index, Operand[] dests, Operand[] sources) : this(sources)
  51. {
  52. Inst = inst;
  53. Index = index;
  54. if (dests != null)
  55. {
  56. // The array may be modified externally, so we store a copy.
  57. _dests = (Operand[])dests.Clone();
  58. for (int dstIndex = 0; dstIndex < dests.Length; dstIndex++)
  59. {
  60. Operand dest = dests[dstIndex];
  61. if (dest != null && dest.Type == OperandType.LocalVariable)
  62. {
  63. dest.AsgOp = this;
  64. }
  65. }
  66. }
  67. else
  68. {
  69. _dests = Array.Empty<Operand>();
  70. }
  71. }
  72. public Operation(Instruction inst, Operand dest, params Operand[] sources) : this(sources)
  73. {
  74. Inst = inst;
  75. if (dest != null)
  76. {
  77. dest.AsgOp = this;
  78. _dests = new[] { dest };
  79. }
  80. else
  81. {
  82. _dests = Array.Empty<Operand>();
  83. }
  84. }
  85. public Operation(Instruction inst, StorageKind storageKind, Operand dest, params Operand[] sources) : this(sources)
  86. {
  87. Inst = inst;
  88. StorageKind = storageKind;
  89. if (dest != null)
  90. {
  91. dest.AsgOp = this;
  92. _dests = new[] { dest };
  93. }
  94. else
  95. {
  96. _dests = Array.Empty<Operand>();
  97. }
  98. }
  99. public Operation(Instruction inst, int index, Operand dest, params Operand[] sources) : this(inst, dest, sources)
  100. {
  101. Index = index;
  102. }
  103. public void AppendDests(Operand[] operands)
  104. {
  105. int startIndex = _dests.Length;
  106. Array.Resize(ref _dests, startIndex + operands.Length);
  107. for (int index = 0; index < operands.Length; index++)
  108. {
  109. Operand dest = operands[index];
  110. if (dest != null && dest.Type == OperandType.LocalVariable)
  111. {
  112. Debug.Assert(dest.AsgOp == null);
  113. dest.AsgOp = this;
  114. }
  115. _dests[startIndex + index] = dest;
  116. }
  117. }
  118. public void AppendSources(Operand[] operands)
  119. {
  120. int startIndex = _sources.Length;
  121. Array.Resize(ref _sources, startIndex + operands.Length);
  122. for (int index = 0; index < operands.Length; index++)
  123. {
  124. Operand source = operands[index];
  125. if (source.Type == OperandType.LocalVariable)
  126. {
  127. source.UseOps.Add(this);
  128. }
  129. _sources[startIndex + index] = source;
  130. }
  131. }
  132. public Operand GetDest(int index)
  133. {
  134. return _dests[index];
  135. }
  136. public Operand GetSource(int index)
  137. {
  138. return _sources[index];
  139. }
  140. public void SetDest(int index, Operand dest)
  141. {
  142. Operand oldDest = _dests[index];
  143. if (oldDest != null && oldDest.Type == OperandType.LocalVariable)
  144. {
  145. oldDest.AsgOp = null;
  146. }
  147. if (dest != null && dest.Type == OperandType.LocalVariable)
  148. {
  149. dest.AsgOp = this;
  150. }
  151. _dests[index] = dest;
  152. }
  153. public void SetSource(int index, Operand source)
  154. {
  155. Operand oldSrc = _sources[index];
  156. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  157. {
  158. oldSrc.UseOps.Remove(this);
  159. }
  160. if (source != null && source.Type == OperandType.LocalVariable)
  161. {
  162. source.UseOps.Add(this);
  163. }
  164. _sources[index] = source;
  165. }
  166. public void InsertSource(int index, Operand source)
  167. {
  168. Operand[] newSources = new Operand[_sources.Length + 1];
  169. Array.Copy(_sources, 0, newSources, 0, index);
  170. Array.Copy(_sources, index, newSources, index + 1, _sources.Length - index);
  171. newSources[index] = source;
  172. _sources = newSources;
  173. }
  174. protected void RemoveSource(int index)
  175. {
  176. SetSource(index, null);
  177. Operand[] newSources = new Operand[_sources.Length - 1];
  178. Array.Copy(_sources, 0, newSources, 0, index);
  179. Array.Copy(_sources, index + 1, newSources, index, _sources.Length - (index + 1));
  180. _sources = newSources;
  181. }
  182. public void TurnIntoCopy(Operand source)
  183. {
  184. TurnInto(Instruction.Copy, source);
  185. }
  186. public void TurnInto(Instruction newInst, Operand source)
  187. {
  188. Inst = newInst;
  189. foreach (Operand oldSrc in _sources)
  190. {
  191. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  192. {
  193. oldSrc.UseOps.Remove(this);
  194. }
  195. }
  196. if (source.Type == OperandType.LocalVariable)
  197. {
  198. source.UseOps.Add(this);
  199. }
  200. _sources = new Operand[] { source };
  201. }
  202. public void TurnDoubleIntoFloat()
  203. {
  204. if ((Inst & ~Instruction.Mask) == Instruction.FP64)
  205. {
  206. Inst = (Inst & Instruction.Mask) | Instruction.FP32;
  207. }
  208. else
  209. {
  210. switch (Inst)
  211. {
  212. case Instruction.ConvertFP32ToFP64:
  213. case Instruction.ConvertFP64ToFP32:
  214. Inst = Instruction.Copy;
  215. break;
  216. case Instruction.ConvertFP64ToS32:
  217. Inst = Instruction.ConvertFP32ToS32;
  218. break;
  219. case Instruction.ConvertFP64ToU32:
  220. Inst = Instruction.ConvertFP32ToU32;
  221. break;
  222. case Instruction.ConvertS32ToFP64:
  223. Inst = Instruction.ConvertS32ToFP32;
  224. break;
  225. case Instruction.ConvertU32ToFP64:
  226. Inst = Instruction.ConvertU32ToFP32;
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. }