Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/opto/Node.java
41161 views
/*1* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.opto;2526import java.io.*;27import java.lang.reflect.Constructor;28import java.util.*;29import sun.jvm.hotspot.debugger.*;30import sun.jvm.hotspot.runtime.*;31import sun.jvm.hotspot.oops.*;32import sun.jvm.hotspot.types.*;33import sun.jvm.hotspot.utilities.Observable;34import sun.jvm.hotspot.utilities.Observer;3536public class Node extends VMObject {37static {38VM.registerVMInitializedObserver(new Observer() {39public void update(Observable o, Object data) {40initialize(VM.getVM().getTypeDataBase());41}42});43}4445private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {46Type type = db.lookupType("Node");47outmaxField = new CIntField(type.getCIntegerField("_outmax"), 0);48outcntField = new CIntField(type.getCIntegerField("_outcnt"), 0);49maxField = new CIntField(type.getCIntegerField("_max"), 0);50cntField = new CIntField(type.getCIntegerField("_cnt"), 0);51idxField = new CIntField(type.getCIntegerField("_idx"), 0);52outField = type.getAddressField("_out");53inField = type.getAddressField("_in");5455nodeType = db.lookupType("Node");5657virtualConstructor = new VirtualBaseConstructor<>(db, nodeType, "sun.jvm.hotspot.opto", Node.class);58}5960private static CIntField outmaxField;61private static CIntField outcntField;62private static CIntField maxField;63private static CIntField cntField;64private static CIntField idxField;65private static AddressField outField;66private static AddressField inField;6768private static VirtualBaseConstructor<Node> virtualConstructor;6970private static Type nodeType;7172static HashMap<Address, Node> nodes = new HashMap<>();7374static HashMap constructors = new HashMap();7576static abstract class Instantiator {77abstract Node create(Address addr);78}7980static public Node create(Address addr) {81if (addr == null) return null;82Node result = nodes.get(addr);83if (result == null) {84result = (Node)virtualConstructor.instantiateWrapperFor(addr);85nodes.put(addr, result);86}87return result;88}8990public Node(Address addr) {91super(addr);92}9394public int outcnt() {95return (int)outcntField.getValue(this.getAddress());96}9798public int req() {99return (int)cntField.getValue(this.getAddress());100}101102public int len() {103return (int)maxField.getValue(this.getAddress());104}105106public int idx() {107return (int)idxField.getValue(this.getAddress());108}109110private Node[] _out;111private Node[] _in;112113public Node rawOut(int i) {114if (_out == null) {115int addressSize = (int)VM.getVM().getAddressSize();116_out = new Node[outcnt()];117Address ptr = outField.getValue(this.getAddress());118for (int j = 0; j < outcnt(); j++) {119_out[j] = Node.create(ptr.getAddressAt(j * addressSize));120}121}122return _out[i];123}124125public Node in(int i) {126if (_in == null) {127int addressSize = (int)VM.getVM().getAddressSize();128_in = new Node[len()];129Address ptr = inField.getValue(this.getAddress());130for (int j = 0; j < len(); j++) {131_in[j] = Node.create(ptr.getAddressAt(j * addressSize));132}133}134return _in[i];135}136137public ArrayList<Node> collect(int d, boolean onlyCtrl) {138int depth = Math.abs(d);139ArrayList<Node> nstack = new ArrayList<>();140BitSet set = new BitSet();141142nstack.add(this);143set.set(idx());144int begin = 0;145int end = 0;146for (int i = 0; i < depth; i++) {147end = nstack.size();148for(int j = begin; j < end; j++) {149Node tp = (Node)nstack.get(j);150int limit = d > 0 ? tp.len() : tp.outcnt();151for(int k = 0; k < limit; k++) {152Node n = d > 0 ? tp.in(k) : tp.rawOut(k);153154// if (not_a_node(n)) continue;155if (n == null) continue;156// do not recurse through top or the root (would reach unrelated stuff)157// if (n.isRoot() || n.isTop()) continue;158// if (onlyCtrl && !n.isCfg()) continue;159160if (!set.get(n.idx())) {161nstack.add(n);162set.set(n.idx());163}164}165}166begin = end;167}168return nstack;169}170171protected void dumpNodes(Node s, int d, boolean onlyCtrl, PrintStream out) {172if (s == null) return;173174ArrayList nstack = s.collect(d, onlyCtrl);175int end = nstack.size();176if (d > 0) {177for(int j = end-1; j >= 0; j--) {178((Node)nstack.get(j)).dump(out);179}180} else {181for(int j = 0; j < end; j++) {182((Node)nstack.get(j)).dump(out);183}184}185}186187public void dump(int depth, PrintStream out) {188dumpNodes(this, depth, false, out);189}190191public String Name() {192Type t = VM.getVM().getTypeDataBase().findDynamicTypeForAddress(getAddress(), nodeType);193String name = null;194if (t != null) {195name = t.toString();196} else {197Class c = getClass();198if (c == Node.class) {199// couldn't identify class type200return "UnknownNode<" + getAddress().getAddressAt(0) + ">";201}202name = getClass().getName();203if (name.startsWith("sun.jvm.hotspot.opto.")) {204name = name.substring("sun.jvm.hotspot.opto.".length());205}206}207if (name.endsWith("Node")) {208return name.substring(0, name.length() - 4);209}210return name;211}212213public void dump(PrintStream out) {214out.print(" ");215out.print(idx());216out.print("\t");217out.print(Name());218out.print("\t=== ");219int i = 0;220for (i = 0; i < req(); i++) {221Node n = in(i);222if (n != null) {223out.print(' ');224out.print(in(i).idx());225} else {226out.print("_");227}228out.print(" ");229}230if (len() != req()) {231int prec = 0;232for (; i < len(); i++) {233Node n = in(i);234if (n != null) {235if (prec++ == 0) {236out.print("| ");237}238out.print(in(i).idx());239}240out.print(" ");241}242}243dumpOut(out);244dumpSpec(out);245out.println();246}247248void dumpOut(PrintStream out) {249// Delimit the output edges250out.print(" [[");251// Dump the output edges252for (int i = 0; i < outcnt(); i++) { // For all outputs253Node u = rawOut(i);254if (u == null) {255out.print("_ ");256// } else if (not_a_node(u)) {257// out.print("not_a_node ");258} else {259// out.print("%c%d ", Compile::current()->nodeArena()->contains(u) ? ' ' : 'o', u->_idx);260out.print(' ');261out.print(u.idx());262out.print(' ');263}264}265out.print("]] ");266}267268public void dumpSpec(PrintStream out) {269}270}271272273