|
| 1 | +package com.compilerprogramming.ezlang.bytecode; |
| 2 | + |
| 3 | +import com.compilerprogramming.ezlang.types.Register; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class BasicBlock { |
| 8 | + public final int bid; |
| 9 | + public final boolean loopHead; |
| 10 | + public final List<BasicBlock> successors = new ArrayList<>(); // successors |
| 11 | + public final List<BasicBlock> predecessors = new ArrayList<>(); |
| 12 | + public final List<Instruction> instructions = new ArrayList<>(); |
| 13 | + /** |
| 14 | + * The preorder traversal number, also acts as a flag indicating whether the |
| 15 | + * BB is yet to be visited (_pre==0 means not yet visited). |
| 16 | + */ |
| 17 | + int pre; |
| 18 | + /** |
| 19 | + * The depth of the BB in the dominator tree |
| 20 | + */ |
| 21 | + int domDepth; |
| 22 | + /** |
| 23 | + * Reverse post order traversal number; |
| 24 | + * Sort node list in ascending order by this to traverse graph in reverse post order. |
| 25 | + * In RPO order if an edge exists from A to B we visit A followed by B, but cycles have to |
| 26 | + * be dealt with in another way. |
| 27 | + */ |
| 28 | + int rpo; |
| 29 | + /** |
| 30 | + * Immediate dominator is the closest strict dominator. |
| 31 | + * @see DominatorTree |
| 32 | + */ |
| 33 | + public BasicBlock idom; |
| 34 | + /** |
| 35 | + * Nodes for whom this node is the immediate dominator, |
| 36 | + * thus the dominator tree. |
| 37 | + */ |
| 38 | + public List<BasicBlock> dominatedChildren = new ArrayList<>(); |
| 39 | + /** |
| 40 | + * Dominance frontier |
| 41 | + */ |
| 42 | + public Set<BasicBlock> dominationFrontier = new HashSet<>(); |
| 43 | + |
| 44 | + /** |
| 45 | + * Nearest Loop to which this BB belongs |
| 46 | + */ |
| 47 | + public LoopNest loop; |
| 48 | + |
| 49 | + public BasicBlock(int bid, boolean loopHead) { |
| 50 | + this.bid = bid; |
| 51 | + this.loopHead = loopHead; |
| 52 | + } |
| 53 | + public BasicBlock(int bid) { |
| 54 | + this(bid, false); |
| 55 | + } |
| 56 | + // For testing only |
| 57 | + public BasicBlock(int bid, BasicBlock... preds) { |
| 58 | + this.bid = bid; |
| 59 | + this.loopHead = false; |
| 60 | + for (BasicBlock bb : preds) |
| 61 | + bb.addSuccessor(this); |
| 62 | + } |
| 63 | + public void add(Instruction instruction) { |
| 64 | + instructions.add(instruction); |
| 65 | + } |
| 66 | + public void addSuccessor(BasicBlock successor) { |
| 67 | + successors.add(successor); |
| 68 | + successor.predecessors.add(this); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Initially the phi has the form |
| 73 | + * v = phi(v,v,...) |
| 74 | + */ |
| 75 | + public void insertPhiFor(Register var) { |
| 76 | + for (Instruction i: instructions) { |
| 77 | + if (i instanceof Instruction.Phi phi) { |
| 78 | + if (phi.dest.id == var.id) |
| 79 | + // already added |
| 80 | + return; |
| 81 | + } |
| 82 | + else break; |
| 83 | + } |
| 84 | + List<Register> inputs = new ArrayList<>(); |
| 85 | + for (int i = 0; i < predecessors.size(); i++) |
| 86 | + inputs.add(var); |
| 87 | + Instruction.Phi phi = new Instruction.Phi(var, inputs); |
| 88 | + instructions.add(0, phi); |
| 89 | + } |
| 90 | + public List<Instruction.Phi> phis() { |
| 91 | + List<Instruction.Phi> list = new ArrayList<>(); |
| 92 | + for (Instruction i: instructions) { |
| 93 | + if (i instanceof Instruction.Phi phi) |
| 94 | + list.add(phi); |
| 95 | + else break; |
| 96 | + } |
| 97 | + return list; |
| 98 | + } |
| 99 | + public static StringBuilder toStr(StringBuilder sb, BasicBlock bb, BitSet visited) |
| 100 | + { |
| 101 | + if (visited.get(bb.bid)) |
| 102 | + return sb; |
| 103 | + visited.set(bb.bid); |
| 104 | + sb.append("L").append(bb.bid).append(":\n"); |
| 105 | + for (Instruction n: bb.instructions) { |
| 106 | + sb.append(" "); |
| 107 | + n.toStr(sb).append("\n"); |
| 108 | + } |
| 109 | + for (BasicBlock succ: bb.successors) { |
| 110 | + toStr(sb, succ, visited); |
| 111 | + } |
| 112 | + return sb; |
| 113 | + } |
| 114 | + @Override |
| 115 | + public boolean equals(Object o) { |
| 116 | + if (this == o) return true; |
| 117 | + if (o == null || getClass() != o.getClass()) return false; |
| 118 | + BasicBlock that = (BasicBlock) o; |
| 119 | + return bid == that.bid; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public int hashCode() { |
| 124 | + return Objects.hash(bid); |
| 125 | + } |
| 126 | + |
| 127 | + public String label() { |
| 128 | + return "BB(" + bid + ")"; |
| 129 | + } |
| 130 | + |
| 131 | + public String uniqueName() { |
| 132 | + return "BB_" + bid; |
| 133 | + } |
| 134 | + |
| 135 | + //////////////// dominator calculations ///////////////////// |
| 136 | + |
| 137 | + public void resetDomInfo() { |
| 138 | + domDepth = 0; |
| 139 | + idom = null; |
| 140 | + dominatedChildren.clear(); |
| 141 | + dominationFrontier.clear(); |
| 142 | + } |
| 143 | + |
| 144 | + public void resetRPO() { |
| 145 | + pre = 0; |
| 146 | + rpo = 0; |
| 147 | + } |
| 148 | + |
| 149 | + public boolean dominates(BasicBlock other) { |
| 150 | + if (this == other) return true; |
| 151 | + while (other.domDepth > domDepth) other = other.idom; |
| 152 | + return this == other; |
| 153 | + } |
| 154 | + |
| 155 | + /////////////////// End of dominator calculations ////////////////////////////////// |
| 156 | +} |
0 commit comments