PhiNode.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections.Generic;
  2. namespace Ryujinx.Graphics.Shader.IntermediateRepresentation
  3. {
  4. class PhiNode : INode
  5. {
  6. private Operand _dest;
  7. public Operand Dest
  8. {
  9. get => _dest;
  10. set => _dest = AssignDest(value);
  11. }
  12. private HashSet<BasicBlock> _blocks;
  13. private class PhiSource
  14. {
  15. public BasicBlock Block { get; }
  16. public Operand Operand { get; set; }
  17. public PhiSource(BasicBlock block, Operand operand)
  18. {
  19. Block = block;
  20. Operand = operand;
  21. }
  22. }
  23. private List<PhiSource> _sources;
  24. public int SourcesCount => _sources.Count;
  25. public PhiNode(Operand dest)
  26. {
  27. _blocks = new HashSet<BasicBlock>();
  28. _sources = new List<PhiSource>();
  29. dest.AsgOp = this;
  30. Dest = dest;
  31. }
  32. private Operand AssignDest(Operand dest)
  33. {
  34. if (dest != null && dest.Type == OperandType.LocalVariable)
  35. {
  36. dest.AsgOp = this;
  37. }
  38. return dest;
  39. }
  40. public void AddSource(BasicBlock block, Operand operand)
  41. {
  42. if (_blocks.Add(block))
  43. {
  44. if (operand.Type == OperandType.LocalVariable)
  45. {
  46. operand.UseOps.Add(this);
  47. }
  48. _sources.Add(new PhiSource(block, operand));
  49. }
  50. }
  51. public Operand GetSource(int index)
  52. {
  53. return _sources[index].Operand;
  54. }
  55. public BasicBlock GetBlock(int index)
  56. {
  57. return _sources[index].Block;
  58. }
  59. public void SetSource(int index, Operand source)
  60. {
  61. Operand oldSrc = _sources[index].Operand;
  62. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  63. {
  64. oldSrc.UseOps.Remove(this);
  65. }
  66. if (source.Type == OperandType.LocalVariable)
  67. {
  68. source.UseOps.Add(this);
  69. }
  70. _sources[index].Operand = source;
  71. }
  72. }
  73. }