BasicBlock.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. if (block == null)
  42. {
  43. ThrowNull(nameof(block));
  44. }
  45. if ((uint)_succCount + 1 > MaxSuccessors)
  46. {
  47. ThrowSuccessorOverflow();
  48. }
  49. block.Predecessors.Add(this);
  50. GetSuccessorUnsafe(_succCount++) = block;
  51. }
  52. public void RemoveSuccessor(int index)
  53. {
  54. if ((uint)index >= (uint)_succCount)
  55. {
  56. ThrowOutOfRange(nameof(index));
  57. }
  58. ref BasicBlock oldBlock = ref GetSuccessorUnsafe(index);
  59. oldBlock.Predecessors.Remove(this);
  60. oldBlock = null;
  61. if (index == 0)
  62. {
  63. _succ0 = _succ1;
  64. }
  65. _succCount--;
  66. }
  67. public BasicBlock GetSuccessor(int index)
  68. {
  69. if ((uint)index >= (uint)_succCount)
  70. {
  71. ThrowOutOfRange(nameof(index));
  72. }
  73. return GetSuccessorUnsafe(index);
  74. }
  75. private ref BasicBlock GetSuccessorUnsafe(int index)
  76. {
  77. return ref Unsafe.Add(ref _succ0, index);
  78. }
  79. public void SetSuccessor(int index, BasicBlock block)
  80. {
  81. if (block == null)
  82. {
  83. ThrowNull(nameof(block));
  84. }
  85. if ((uint)index >= (uint)_succCount)
  86. {
  87. ThrowOutOfRange(nameof(index));
  88. }
  89. ref BasicBlock oldBlock = ref GetSuccessorUnsafe(index);
  90. oldBlock.Predecessors.Remove(this);
  91. block.Predecessors.Add(this);
  92. oldBlock = block;
  93. }
  94. public void Append(Operation node)
  95. {
  96. Operation last = Operations.Last;
  97. // Append node before terminal or to end if no terminal.
  98. if (last == default)
  99. {
  100. Operations.AddLast(node);
  101. return;
  102. }
  103. switch (last.Instruction)
  104. {
  105. case Instruction.Return:
  106. case Instruction.Tailcall:
  107. case Instruction.BranchIf:
  108. Operations.AddBefore(last, node);
  109. break;
  110. default:
  111. Operations.AddLast(node);
  112. break;
  113. }
  114. }
  115. private static void ThrowNull(string name) => throw new ArgumentNullException(name);
  116. private static void ThrowOutOfRange(string name) => throw new ArgumentOutOfRangeException(name);
  117. private static void ThrowSuccessorOverflow() => throw new OverflowException($"BasicBlock can only have {MaxSuccessors} successors.");
  118. public bool Equals(BasicBlock other)
  119. {
  120. return other == this;
  121. }
  122. public override bool Equals(object obj)
  123. {
  124. return Equals(obj as BasicBlock);
  125. }
  126. public override int GetHashCode()
  127. {
  128. return base.GetHashCode();
  129. }
  130. }
  131. }