PhiNode.cs 2.4 KB

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