BasicBlock.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections.Generic;
  2. namespace ARMeilleure.IntermediateRepresentation
  3. {
  4. class BasicBlock : IIntrusiveListNode<BasicBlock>
  5. {
  6. public int Index { get; set; }
  7. public BasicBlock ListPrevious { get; set; }
  8. public BasicBlock ListNext { get; set; }
  9. public IntrusiveList<Node> Operations { get; }
  10. private BasicBlock _next;
  11. private BasicBlock _branch;
  12. public BasicBlock Next
  13. {
  14. get => _next;
  15. set => _next = AddSuccessor(_next, value);
  16. }
  17. public BasicBlock Branch
  18. {
  19. get => _branch;
  20. set => _branch = AddSuccessor(_branch, value);
  21. }
  22. public List<BasicBlock> Predecessors { get; }
  23. public HashSet<BasicBlock> DominanceFrontiers { get; }
  24. public BasicBlock ImmediateDominator { get; set; }
  25. public BasicBlock()
  26. {
  27. Operations = new IntrusiveList<Node>();
  28. Predecessors = new List<BasicBlock>();
  29. DominanceFrontiers = new HashSet<BasicBlock>();
  30. Index = -1;
  31. }
  32. public BasicBlock(int index) : this()
  33. {
  34. Index = index;
  35. }
  36. private BasicBlock AddSuccessor(BasicBlock oldBlock, BasicBlock newBlock)
  37. {
  38. oldBlock?.Predecessors.Remove(this);
  39. newBlock?.Predecessors.Add(this);
  40. return newBlock;
  41. }
  42. public void Append(Node node)
  43. {
  44. // If the branch block is not null, then the list of operations
  45. // should end with a branch instruction. We insert the new operation
  46. // before this branch.
  47. if (_branch != null || (Operations.Last != null && IsLeafBlock()))
  48. {
  49. Operations.AddBefore(Operations.Last, node);
  50. }
  51. else
  52. {
  53. Operations.AddLast(node);
  54. }
  55. }
  56. private bool IsLeafBlock()
  57. {
  58. return _branch == null && _next == null;
  59. }
  60. public Node GetLastOp()
  61. {
  62. return Operations.Last;
  63. }
  64. }
  65. }