BasicBlock.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. namespace ARMeilleure.IntermediateRepresentation
  5. {
  6. class BasicBlock : IEquatable<BasicBlock>, IIntrusiveListNode<BasicBlock>
  7. {
  8. private const uint MaxSuccessors = 2;
  9. private int _succCount;
  10. private BasicBlock _succ0;
  11. private BasicBlock _succ1;
  12. private HashSet<BasicBlock> _domFrontiers;
  13. public int Index { get; set; }
  14. public BasicBlockFrequency Frequency { get; set; }
  15. public BasicBlock ListPrevious { get; set; }
  16. public BasicBlock ListNext { get; set; }
  17. public IntrusiveList<Operation> Operations { get; }
  18. public List<BasicBlock> Predecessors { get; }
  19. public BasicBlock ImmediateDominator { get; set; }
  20. public int SuccessorsCount => _succCount;
  21. public HashSet<BasicBlock> DominanceFrontiers
  22. {
  23. get
  24. {
  25. if (_domFrontiers == null)
  26. {
  27. _domFrontiers = new HashSet<BasicBlock>();
  28. }
  29. return _domFrontiers;
  30. }
  31. }
  32. public BasicBlock() : this(index: -1) { }
  33. public BasicBlock(int index)
  34. {
  35. Operations = new IntrusiveList<Operation>();
  36. Predecessors = new List<BasicBlock>();
  37. Index = index;
  38. }
  39. public void AddSuccessor(BasicBlock block)
  40. {
  41. ArgumentNullException.ThrowIfNull(block);
  42. if ((uint)_succCount + 1 > MaxSuccessors)
  43. {
  44. ThrowSuccessorOverflow();
  45. }
  46. block.Predecessors.Add(this);
  47. GetSuccessorUnsafe(_succCount++) = block;
  48. }
  49. public void RemoveSuccessor(int index)
  50. {
  51. if ((uint)index >= (uint)_succCount)
  52. {
  53. ThrowOutOfRange(nameof(index));
  54. }
  55. ref BasicBlock oldBlock = ref GetSuccessorUnsafe(index);
  56. oldBlock.Predecessors.Remove(this);
  57. oldBlock = null;
  58. if (index == 0)
  59. {
  60. _succ0 = _succ1;
  61. }
  62. _succCount--;
  63. }
  64. public BasicBlock GetSuccessor(int index)
  65. {
  66. if ((uint)index >= (uint)_succCount)
  67. {
  68. ThrowOutOfRange(nameof(index));
  69. }
  70. return GetSuccessorUnsafe(index);
  71. }
  72. private ref BasicBlock GetSuccessorUnsafe(int index)
  73. {
  74. return ref Unsafe.Add(ref _succ0, index);
  75. }
  76. public void SetSuccessor(int index, BasicBlock block)
  77. {
  78. ArgumentNullException.ThrowIfNull(block);
  79. if ((uint)index >= (uint)_succCount)
  80. {
  81. ThrowOutOfRange(nameof(index));
  82. }
  83. ref BasicBlock oldBlock = ref GetSuccessorUnsafe(index);
  84. oldBlock.Predecessors.Remove(this);
  85. block.Predecessors.Add(this);
  86. oldBlock = block;
  87. }
  88. public void Append(Operation node)
  89. {
  90. Operation last = Operations.Last;
  91. // Append node before terminal or to end if no terminal.
  92. if (last == default)
  93. {
  94. Operations.AddLast(node);
  95. return;
  96. }
  97. switch (last.Instruction)
  98. {
  99. case Instruction.Return:
  100. case Instruction.Tailcall:
  101. case Instruction.BranchIf:
  102. Operations.AddBefore(last, node);
  103. break;
  104. default:
  105. Operations.AddLast(node);
  106. break;
  107. }
  108. }
  109. private static void ThrowOutOfRange(string name) => throw new ArgumentOutOfRangeException(name);
  110. private static void ThrowSuccessorOverflow() => throw new OverflowException($"BasicBlock can only have {MaxSuccessors} successors.");
  111. public bool Equals(BasicBlock other)
  112. {
  113. return other == this;
  114. }
  115. public override bool Equals(object obj)
  116. {
  117. return Equals(obj as BasicBlock);
  118. }
  119. public override int GetHashCode()
  120. {
  121. return base.GetHashCode();
  122. }
  123. }
  124. }