Operation.cs 6.7 KB

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