1 /* 2 * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.compiler.core.common.cfg; 26 27 import java.util.Comparator; 28 29 public abstract class AbstractBlockBase<T extends AbstractBlockBase<T>> { 30 31 protected int id; 32 protected int domDepth; 33 34 protected T[] predecessors; 35 protected T[] successors; 36 37 private T dominator; 38 private T firstDominated; 39 private T dominatedSibling; 40 private int domNumber; 41 private int maxChildDomNumber; 42 43 private boolean align; 44 private int linearScanNumber; 45 46 protected AbstractBlockBase() { 47 this.id = AbstractControlFlowGraph.BLOCK_ID_INITIAL; 48 this.linearScanNumber = -1; 49 this.domNumber = -1; 50 this.maxChildDomNumber = -1; 51 } 52 53 public void setDominatorNumber(int domNumber) { 54 this.domNumber = domNumber; 55 } 56 57 public void setMaxChildDomNumber(int maxChildDomNumber) { 58 this.maxChildDomNumber = maxChildDomNumber; 59 } 60 61 public int getDominatorNumber() { 62 return domNumber; 63 } 64 65 public int getMaxChildDominatorNumber() { 66 return this.maxChildDomNumber; 67 } 68 69 public int getId() { 70 return id; 71 } 72 73 public void setId(int id) { 74 this.id = id; 75 } 76 77 public T[] getPredecessors() { 78 return predecessors; 79 } 80 81 public void setPredecessors(T[] predecessors) { 82 this.predecessors = predecessors; 83 } 84 85 public T[] getSuccessors() { 86 return successors; 87 } 88 89 public void setSuccessors(T[] successors) { 90 this.successors = successors; 91 } 92 93 public T getDominator() { 94 return dominator; 95 } 96 97 public void setDominator(T dominator) { 98 this.dominator = dominator; 99 this.domDepth = dominator.domDepth + 1; 100 } 101 102 /** 103 * Level in the dominator tree starting with 0 for the start block. 104 */ 105 public int getDominatorDepth() { 106 return domDepth; 107 } 108 109 public T getFirstDominated() { 110 return this.firstDominated; 111 } 112 113 public void setFirstDominated(T block) { 114 this.firstDominated = block; 115 } 116 117 public T getDominatedSibling() { 118 return this.dominatedSibling; 119 } 120 121 public void setDominatedSibling(T block) { 122 this.dominatedSibling = block; 123 } 124 125 @Override 126 public String toString() { 127 return "B" + id; 128 } 129 130 public int getPredecessorCount() { 131 return getPredecessors().length; 132 } 133 134 public int getSuccessorCount() { 135 return getSuccessors().length; 136 } 137 138 public int getLinearScanNumber() { 139 return linearScanNumber; 140 } 141 142 public void setLinearScanNumber(int linearScanNumber) { 143 this.linearScanNumber = linearScanNumber; 144 } 145 146 public boolean isAligned() { 147 return align; 148 } 149 150 public void setAlign(boolean align) { 151 this.align = align; 152 } 153 154 public abstract boolean isExceptionEntry(); 155 156 public abstract Loop<T> getLoop(); 157 158 public abstract int getLoopDepth(); 159 160 public abstract void delete(); 161 162 public abstract boolean isLoopEnd(); 163 164 public abstract boolean isLoopHeader(); 165 166 public abstract T getPostdominator(); 167 168 public abstract double getRelativeFrequency(); 169 170 public abstract T getDominator(int distance); 171 172 @Override 173 public int hashCode() { 174 return id; 175 } 176 177 public static class BlockIdComparator implements Comparator<AbstractBlockBase<?>> { 178 @Override 179 public int compare(AbstractBlockBase<?> o1, AbstractBlockBase<?> o2) { 180 return Integer.compare(o1.getId(), o2.getId()); 181 } 182 } 183 184 public static final BlockIdComparator BLOCK_ID_COMPARATOR = new BlockIdComparator(); 185 }