BasicBlock.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ARMeilleure.IntermediateRepresentation
  4. {
  5. class BasicBlock : IIntrusiveListNode<BasicBlock>
  6. {
  7. private readonly List<BasicBlock> _successors;
  8. public int Index { get; set; }
  9. public BasicBlockFrequency Frequency { get; set; }
  10. public BasicBlock ListPrevious { get; set; }
  11. public BasicBlock ListNext { get; set; }
  12. public IntrusiveList<Node> Operations { get; }
  13. public List<BasicBlock> Predecessors { get; }
  14. public HashSet<BasicBlock> DominanceFrontiers { get; }
  15. public BasicBlock ImmediateDominator { get; set; }
  16. public int SuccessorCount => _successors.Count;
  17. public BasicBlock() : this(index: -1) { }
  18. public BasicBlock(int index)
  19. {
  20. _successors = new List<BasicBlock>();
  21. Operations = new IntrusiveList<Node>();
  22. Predecessors = new List<BasicBlock>();
  23. DominanceFrontiers = new HashSet<BasicBlock>();
  24. Index = index;
  25. }
  26. public void AddSuccessor(BasicBlock block)
  27. {
  28. if (block == null)
  29. {
  30. throw new ArgumentNullException(nameof(block));
  31. }
  32. block.Predecessors.Add(this);
  33. _successors.Add(block);
  34. }
  35. public void RemoveSuccessor(int index)
  36. {
  37. BasicBlock oldBlock = _successors[index];
  38. oldBlock.Predecessors.Remove(this);
  39. _successors.RemoveAt(index);
  40. }
  41. public BasicBlock GetSuccessor(int index)
  42. {
  43. return _successors[index];
  44. }
  45. public void SetSuccessor(int index, BasicBlock block)
  46. {
  47. if (block == null)
  48. {
  49. throw new ArgumentNullException(nameof(block));
  50. }
  51. BasicBlock oldBlock = _successors[index];
  52. oldBlock.Predecessors.Remove(this);
  53. block.Predecessors.Add(this);
  54. _successors[index] = block;
  55. }
  56. public void Append(Node node)
  57. {
  58. var lastOp = Operations.Last as Operation;
  59. // Append node before terminal or to end if no terminal.
  60. switch (lastOp?.Instruction)
  61. {
  62. case Instruction.Return:
  63. case Instruction.Tailcall:
  64. case Instruction.BranchIf:
  65. Operations.AddBefore(lastOp, node);
  66. break;
  67. default:
  68. Operations.AddLast(node);
  69. break;
  70. }
  71. }
  72. public Node GetLastOp()
  73. {
  74. return Operations.Last;
  75. }
  76. }
  77. }