Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/idealGraphPrinter.cpp
32285 views
/*1* Copyright (c) 2007, 2018, 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*/2324#include "precompiled.hpp"25#include "opto/chaitin.hpp"26#include "opto/idealGraphPrinter.hpp"27#include "opto/machnode.hpp"28#include "opto/parse.hpp"29#include "runtime/threadCritical.hpp"3031#ifndef PRODUCT3233// Constants34// Keep consistent with Java constants35const char *IdealGraphPrinter::INDENT = " ";36const char *IdealGraphPrinter::TOP_ELEMENT = "graphDocument";37const char *IdealGraphPrinter::GROUP_ELEMENT = "group";38const char *IdealGraphPrinter::GRAPH_ELEMENT = "graph";39const char *IdealGraphPrinter::PROPERTIES_ELEMENT = "properties";40const char *IdealGraphPrinter::EDGES_ELEMENT = "edges";41const char *IdealGraphPrinter::PROPERTY_ELEMENT = "p";42const char *IdealGraphPrinter::EDGE_ELEMENT = "edge";43const char *IdealGraphPrinter::NODE_ELEMENT = "node";44const char *IdealGraphPrinter::NODES_ELEMENT = "nodes";45const char *IdealGraphPrinter::REMOVE_EDGE_ELEMENT = "removeEdge";46const char *IdealGraphPrinter::REMOVE_NODE_ELEMENT = "removeNode";47const char *IdealGraphPrinter::METHOD_NAME_PROPERTY = "name";48const char *IdealGraphPrinter::METHOD_IS_PUBLIC_PROPERTY = "public";49const char *IdealGraphPrinter::METHOD_IS_STATIC_PROPERTY = "static";50const char *IdealGraphPrinter::TRUE_VALUE = "true";51const char *IdealGraphPrinter::NODE_NAME_PROPERTY = "name";52const char *IdealGraphPrinter::EDGE_NAME_PROPERTY = "name";53const char *IdealGraphPrinter::NODE_ID_PROPERTY = "id";54const char *IdealGraphPrinter::FROM_PROPERTY = "from";55const char *IdealGraphPrinter::TO_PROPERTY = "to";56const char *IdealGraphPrinter::PROPERTY_NAME_PROPERTY = "name";57const char *IdealGraphPrinter::GRAPH_NAME_PROPERTY = "name";58const char *IdealGraphPrinter::INDEX_PROPERTY = "index";59const char *IdealGraphPrinter::METHOD_ELEMENT = "method";60const char *IdealGraphPrinter::INLINE_ELEMENT = "inline";61const char *IdealGraphPrinter::BYTECODES_ELEMENT = "bytecodes";62const char *IdealGraphPrinter::METHOD_BCI_PROPERTY = "bci";63const char *IdealGraphPrinter::METHOD_SHORT_NAME_PROPERTY = "shortName";64const char *IdealGraphPrinter::CONTROL_FLOW_ELEMENT = "controlFlow";65const char *IdealGraphPrinter::BLOCK_NAME_PROPERTY = "name";66const char *IdealGraphPrinter::BLOCK_DOMINATOR_PROPERTY = "dom";67const char *IdealGraphPrinter::BLOCK_ELEMENT = "block";68const char *IdealGraphPrinter::SUCCESSORS_ELEMENT = "successors";69const char *IdealGraphPrinter::SUCCESSOR_ELEMENT = "successor";70const char *IdealGraphPrinter::ASSEMBLY_ELEMENT = "assembly";7172int IdealGraphPrinter::_file_count = 0;7374IdealGraphPrinter *IdealGraphPrinter::printer() {75if (PrintIdealGraphLevel == 0) return NULL;7677JavaThread *thread = JavaThread::current();78if (!thread->is_Compiler_thread()) return NULL;7980CompilerThread *compiler_thread = (CompilerThread *)thread;81if (compiler_thread->ideal_graph_printer() == NULL) {82IdealGraphPrinter *printer = new IdealGraphPrinter();83compiler_thread->set_ideal_graph_printer(printer);84}8586return compiler_thread->ideal_graph_printer();87}8889void IdealGraphPrinter::clean_up() {90JavaThread *p;91for (p = Threads::first(); p; p = p->next()) {92if (p->is_Compiler_thread()) {93CompilerThread *c = (CompilerThread *)p;94IdealGraphPrinter *printer = c->ideal_graph_printer();95if (printer) {96delete printer;97}98c->set_ideal_graph_printer(NULL);99}100}101}102103// Constructor, either file or network output104IdealGraphPrinter::IdealGraphPrinter() {105106// By default dump both ins and outs since dead or unreachable code107// needs to appear in the graph. There are also some special cases108// in the mach where kill projections have no users but should109// appear in the dump.110_traverse_outs = true;111_should_send_method = true;112_output = NULL;113buffer[0] = 0;114_depth = 0;115_current_method = NULL;116assert(!_current_method, "current method must be initialized to NULL");117_stream = NULL;118119if (PrintIdealGraphFile != NULL) {120ThreadCritical tc;121// User wants all output to go to files122if (_file_count != 0) {123ResourceMark rm;124stringStream st;125const char* dot = strrchr(PrintIdealGraphFile, '.');126if (dot) {127st.write(PrintIdealGraphFile, dot - PrintIdealGraphFile);128st.print("%d%s", _file_count, dot);129} else {130st.print("%s%d", PrintIdealGraphFile, _file_count);131}132fileStream *stream = new (ResourceObj::C_HEAP, mtCompiler) fileStream(st.as_string());133_output = stream;134} else {135fileStream *stream = new (ResourceObj::C_HEAP, mtCompiler) fileStream(PrintIdealGraphFile);136_output = stream;137}138_file_count++;139} else {140_stream = new (ResourceObj::C_HEAP, mtCompiler) networkStream();141142// Try to connect to visualizer143if (_stream->connect(PrintIdealGraphAddress, PrintIdealGraphPort)) {144char c = 0;145_stream->read(&c, 1);146if (c != 'y') {147tty->print_cr("Client available, but does not want to receive data!");148_stream->close();149delete _stream;150_stream = NULL;151return;152}153_output = _stream;154} else {155// It would be nice if we could shut down cleanly but it should156// be an error if we can't connect to the visualizer.157fatal(err_msg_res("Couldn't connect to visualizer at %s:" INTX_FORMAT,158PrintIdealGraphAddress, PrintIdealGraphPort));159}160}161162_xml = new (ResourceObj::C_HEAP, mtCompiler) xmlStream(_output);163164head(TOP_ELEMENT);165}166167// Destructor, close file or network stream168IdealGraphPrinter::~IdealGraphPrinter() {169170tail(TOP_ELEMENT);171172// tty->print_cr("Walk time: %d", (int)_walk_time.milliseconds());173// tty->print_cr("Output time: %d", (int)_output_time.milliseconds());174// tty->print_cr("Build blocks time: %d", (int)_build_blocks_time.milliseconds());175176if(_xml) {177delete _xml;178_xml = NULL;179}180181if (_stream) {182delete _stream;183if (_stream == _output) {184_output = NULL;185}186_stream = NULL;187}188189if (_output) {190delete _output;191_output = NULL;192}193}194195196void IdealGraphPrinter::begin_elem(const char *s) {197_xml->begin_elem("%s", s);198}199200void IdealGraphPrinter::end_elem() {201_xml->end_elem();202}203204void IdealGraphPrinter::begin_head(const char *s) {205_xml->begin_head("%s", s);206}207208void IdealGraphPrinter::end_head() {209_xml->end_head();210}211212void IdealGraphPrinter::print_attr(const char *name, intptr_t val) {213stringStream stream;214stream.print(INTX_FORMAT, val);215print_attr(name, stream.as_string());216}217218void IdealGraphPrinter::print_attr(const char *name, const char *val) {219_xml->print(" %s='", name);220text(val);221_xml->print("'");222}223224void IdealGraphPrinter::head(const char *name) {225_xml->head("%s", name);226}227228void IdealGraphPrinter::tail(const char *name) {229_xml->tail(name);230}231232void IdealGraphPrinter::text(const char *s) {233_xml->text("%s", s);234}235236void IdealGraphPrinter::print_prop(const char *name, int val) {237238stringStream stream;239stream.print("%d", val);240print_prop(name, stream.as_string());241}242243void IdealGraphPrinter::print_prop(const char *name, const char *val) {244begin_head(PROPERTY_ELEMENT);245print_attr(PROPERTY_NAME_PROPERTY, name);246end_head();247text(val);248tail(PROPERTY_ELEMENT);249}250251void IdealGraphPrinter::print_method(ciMethod *method, int bci, InlineTree *tree) {252begin_head(METHOD_ELEMENT);253254stringStream str;255method->print_name(&str);256257stringStream shortStr;258method->print_short_name(&shortStr);259260print_attr(METHOD_NAME_PROPERTY, str.as_string());261print_attr(METHOD_SHORT_NAME_PROPERTY, shortStr.as_string());262print_attr(METHOD_BCI_PROPERTY, bci);263264end_head();265266head(BYTECODES_ELEMENT);267output()->print_cr("<![CDATA[");268method->print_codes_on(output());269output()->print_cr("]]>");270tail(BYTECODES_ELEMENT);271272head(INLINE_ELEMENT);273if (tree != NULL) {274GrowableArray<InlineTree *> subtrees = tree->subtrees();275for (int i = 0; i < subtrees.length(); i++) {276print_inline_tree(subtrees.at(i));277}278}279tail(INLINE_ELEMENT);280281tail(METHOD_ELEMENT);282output()->flush();283}284285void IdealGraphPrinter::print_inline_tree(InlineTree *tree) {286287if (tree == NULL) return;288289ciMethod *method = tree->method();290print_method(tree->method(), tree->caller_bci(), tree);291292}293294void IdealGraphPrinter::print_inlining(Compile* compile) {295296// Print inline tree297if (_should_send_method) {298InlineTree *inlineTree = compile->ilt();299if (inlineTree != NULL) {300print_inline_tree(inlineTree);301} else {302// print this method only303}304}305}306307// Has to be called whenever a method is compiled308void IdealGraphPrinter::begin_method(Compile* compile) {309310ciMethod *method = compile->method();311assert(_output, "output stream must exist!");312assert(method, "null methods are not allowed!");313assert(!_current_method, "current method must be null!");314315head(GROUP_ELEMENT);316317head(PROPERTIES_ELEMENT);318319// Print properties320// Add method name321stringStream strStream;322method->print_name(&strStream);323print_prop(METHOD_NAME_PROPERTY, strStream.as_string());324325if (method->flags().is_public()) {326print_prop(METHOD_IS_PUBLIC_PROPERTY, TRUE_VALUE);327}328329if (method->flags().is_static()) {330print_prop(METHOD_IS_STATIC_PROPERTY, TRUE_VALUE);331}332333tail(PROPERTIES_ELEMENT);334335if (_stream) {336char answer = 0;337_xml->flush();338int result = _stream->read(&answer, 1);339_should_send_method = (answer == 'y');340}341342this->_current_method = method;343344_xml->flush();345}346347// Has to be called whenever a method has finished compilation348void IdealGraphPrinter::end_method() {349350nmethod* method = (nmethod*)this->_current_method->code();351352tail(GROUP_ELEMENT);353_current_method = NULL;354_xml->flush();355}356357// Print indent358void IdealGraphPrinter::print_indent() {359tty->print_cr("printing ident %d", _depth);360for (int i = 0; i < _depth; i++) {361_xml->print("%s", INDENT);362}363}364365bool IdealGraphPrinter::traverse_outs() {366return _traverse_outs;367}368369void IdealGraphPrinter::set_traverse_outs(bool b) {370_traverse_outs = b;371}372373intptr_t IdealGraphPrinter::get_node_id(Node *n) {374return (intptr_t)(n);375}376377void IdealGraphPrinter::visit_node(Node *n, bool edges, VectorSet* temp_set) {378379if (edges) {380381// Output edge382intptr_t dest_id = get_node_id(n);383for ( uint i = 0; i < n->len(); i++ ) {384if ( n->in(i) ) {385Node *source = n->in(i);386begin_elem(EDGE_ELEMENT);387intptr_t source_id = get_node_id(source);388print_attr(FROM_PROPERTY, source_id);389print_attr(TO_PROPERTY, dest_id);390print_attr(INDEX_PROPERTY, i);391end_elem();392}393}394395} else {396397// Output node398begin_head(NODE_ELEMENT);399print_attr(NODE_ID_PROPERTY, get_node_id(n));400end_head();401402head(PROPERTIES_ELEMENT);403404Node *node = n;405#ifndef PRODUCT406Compile::current()->_in_dump_cnt++;407print_prop(NODE_NAME_PROPERTY, (const char *)node->Name());408const Type *t = node->bottom_type();409print_prop("type", t->msg());410print_prop("idx", node->_idx);411#ifdef ASSERT412print_prop("debug_idx", node->_debug_idx);413#endif414415if (C->cfg() != NULL) {416Block* block = C->cfg()->get_block_for_node(node);417if (block == NULL) {418print_prop("block", C->cfg()->get_block(0)->_pre_order);419} else {420print_prop("block", block->_pre_order);421}422}423424const jushort flags = node->flags();425if (flags & Node::Flag_is_Copy) {426print_prop("is_copy", "true");427}428if (flags & Node::Flag_rematerialize) {429print_prop("rematerialize", "true");430}431if (flags & Node::Flag_needs_anti_dependence_check) {432print_prop("needs_anti_dependence_check", "true");433}434if (flags & Node::Flag_is_macro) {435print_prop("is_macro", "true");436}437if (flags & Node::Flag_is_Con) {438print_prop("is_con", "true");439}440if (flags & Node::Flag_is_cisc_alternate) {441print_prop("is_cisc_alternate", "true");442}443if (flags & Node::Flag_is_dead_loop_safe) {444print_prop("is_dead_loop_safe", "true");445}446if (flags & Node::Flag_may_be_short_branch) {447print_prop("may_be_short_branch", "true");448}449if (flags & Node::Flag_has_call) {450print_prop("has_call", "true");451}452453if (C->matcher() != NULL) {454if (C->matcher()->is_shared(node)) {455print_prop("is_shared", "true");456} else {457print_prop("is_shared", "false");458}459if (C->matcher()->is_dontcare(node)) {460print_prop("is_dontcare", "true");461} else {462print_prop("is_dontcare", "false");463}464465#ifdef ASSERT466Node* old = C->matcher()->find_old_node(node);467if (old != NULL) {468print_prop("old_node_idx", old->_idx);469}470#endif471}472473if (node->is_Proj()) {474print_prop("con", (int)node->as_Proj()->_con);475}476477if (node->is_Mach()) {478print_prop("idealOpcode", (const char *)NodeClassNames[node->as_Mach()->ideal_Opcode()]);479}480481buffer[0] = 0;482stringStream s2(buffer, sizeof(buffer) - 1);483484node->dump_spec(&s2);485if (t != NULL && (t->isa_instptr() || t->isa_klassptr())) {486const TypeInstPtr *toop = t->isa_instptr();487const TypeKlassPtr *tkls = t->isa_klassptr();488ciKlass* klass = toop ? toop->klass() : (tkls ? tkls->klass() : NULL );489if( klass && klass->is_loaded() && klass->is_interface() ) {490s2.print(" Interface:");491} else if( toop ) {492s2.print(" Oop:");493} else if( tkls ) {494s2.print(" Klass:");495}496t->dump_on(&s2);497} else if( t == Type::MEMORY ) {498s2.print(" Memory:");499MemNode::dump_adr_type(node, node->adr_type(), &s2);500}501502assert(s2.size() < sizeof(buffer), "size in range");503print_prop("dump_spec", buffer);504505if (node->is_block_proj()) {506print_prop("is_block_proj", "true");507}508509if (node->is_block_start()) {510print_prop("is_block_start", "true");511}512513const char *short_name = "short_name";514if (strcmp(node->Name(), "Parm") == 0 && node->as_Proj()->_con >= TypeFunc::Parms) {515int index = node->as_Proj()->_con - TypeFunc::Parms;516if (index >= 10) {517print_prop(short_name, "PA");518} else {519sprintf(buffer, "P%d", index);520print_prop(short_name, buffer);521}522} else if (strcmp(node->Name(), "IfTrue") == 0) {523print_prop(short_name, "T");524} else if (strcmp(node->Name(), "IfFalse") == 0) {525print_prop(short_name, "F");526} else if ((node->is_Con() && node->is_Type()) || node->is_Proj()) {527528if (t->base() == Type::Int && t->is_int()->is_con()) {529const TypeInt *typeInt = t->is_int();530assert(typeInt->is_con(), "must be constant");531jint value = typeInt->get_con();532533// max. 2 chars allowed534if (value >= -9 && value <= 99) {535sprintf(buffer, "%d", value);536print_prop(short_name, buffer);537} else {538print_prop(short_name, "I");539}540} else if (t == Type::TOP) {541print_prop(short_name, "^");542} else if (t->base() == Type::Long && t->is_long()->is_con()) {543const TypeLong *typeLong = t->is_long();544assert(typeLong->is_con(), "must be constant");545jlong value = typeLong->get_con();546547// max. 2 chars allowed548if (value >= -9 && value <= 99) {549sprintf(buffer, JLONG_FORMAT, value);550print_prop(short_name, buffer);551} else {552print_prop(short_name, "L");553}554} else if (t->base() == Type::KlassPtr) {555const TypeKlassPtr *typeKlass = t->is_klassptr();556print_prop(short_name, "CP");557} else if (t->base() == Type::Control) {558print_prop(short_name, "C");559} else if (t->base() == Type::Memory) {560print_prop(short_name, "M");561} else if (t->base() == Type::Abio) {562print_prop(short_name, "IO");563} else if (t->base() == Type::Return_Address) {564print_prop(short_name, "RA");565} else if (t->base() == Type::AnyPtr) {566print_prop(short_name, "P");567} else if (t->base() == Type::RawPtr) {568print_prop(short_name, "RP");569} else if (t->base() == Type::AryPtr) {570print_prop(short_name, "AP");571}572}573574JVMState* caller = NULL;575if (node->is_SafePoint()) {576caller = node->as_SafePoint()->jvms();577} else {578Node_Notes* notes = C->node_notes_at(node->_idx);579if (notes != NULL) {580caller = notes->jvms();581}582}583584if (caller != NULL) {585stringStream bciStream;586ciMethod* last = NULL;587int last_bci;588while(caller) {589if (caller->has_method()) {590last = caller->method();591last_bci = caller->bci();592}593bciStream.print("%d ", caller->bci());594caller = caller->caller();595}596print_prop("bci", bciStream.as_string());597if (last != NULL && last->has_linenumber_table() && last_bci >= 0) {598print_prop("line", last->line_number_from_bci(last_bci));599}600}601602#ifdef ASSERT603if (node->debug_orig() != NULL) {604temp_set->Clear();605stringStream dorigStream;606Node* dorig = node->debug_orig();607while (dorig && temp_set->test_set(dorig->_idx)) {608dorigStream.print("%d ", dorig->_idx);609}610print_prop("debug_orig", dorigStream.as_string());611}612#endif613614if (_chaitin && _chaitin != (PhaseChaitin *)((intptr_t)0xdeadbeef)) {615buffer[0] = 0;616_chaitin->dump_register(node, buffer);617print_prop("reg", buffer);618uint lrg_id = 0;619if (node->_idx < _chaitin->_lrg_map.size()) {620lrg_id = _chaitin->_lrg_map.live_range_id(node);621}622print_prop("lrg", lrg_id);623}624625Compile::current()->_in_dump_cnt--;626#endif627628tail(PROPERTIES_ELEMENT);629tail(NODE_ELEMENT);630}631}632633void IdealGraphPrinter::walk_nodes(Node *start, bool edges, VectorSet* temp_set) {634635636VectorSet visited(Thread::current()->resource_area());637GrowableArray<Node *> nodeStack(Thread::current()->resource_area(), 0, 0, NULL);638nodeStack.push(start);639visited.test_set(start->_idx);640if (C->cfg() != NULL) {641// once we have a CFG there are some nodes that aren't really642// reachable but are in the CFG so add them here.643for (uint i = 0; i < C->cfg()->number_of_blocks(); i++) {644Block* block = C->cfg()->get_block(i);645for (uint s = 0; s < block->number_of_nodes(); s++) {646nodeStack.push(block->get_node(s));647}648}649}650651while(nodeStack.length() > 0) {652653Node *n = nodeStack.pop();654visit_node(n, edges, temp_set);655656if (_traverse_outs) {657for (DUIterator i = n->outs(); n->has_out(i); i++) {658Node* p = n->out(i);659if (!visited.test_set(p->_idx)) {660nodeStack.push(p);661}662}663}664665for ( uint i = 0; i < n->len(); i++ ) {666if ( n->in(i) ) {667if (!visited.test_set(n->in(i)->_idx)) {668nodeStack.push(n->in(i));669}670}671}672}673}674675void IdealGraphPrinter::print_method(Compile* compile, const char *name, int level, bool clear_nodes) {676print(compile, name, (Node *)compile->root(), level, clear_nodes);677}678679// Print current ideal graph680void IdealGraphPrinter::print(Compile* compile, const char *name, Node *node, int level, bool clear_nodes) {681682if (!_current_method || !_should_send_method || level > PrintIdealGraphLevel) return;683684this->C = compile;685686// Warning, unsafe cast?687_chaitin = (PhaseChaitin *)C->regalloc();688689begin_head(GRAPH_ELEMENT);690print_attr(GRAPH_NAME_PROPERTY, (const char *)name);691end_head();692693VectorSet temp_set(Thread::current()->resource_area());694695head(NODES_ELEMENT);696walk_nodes(node, false, &temp_set);697tail(NODES_ELEMENT);698699head(EDGES_ELEMENT);700walk_nodes(node, true, &temp_set);701tail(EDGES_ELEMENT);702if (C->cfg() != NULL) {703head(CONTROL_FLOW_ELEMENT);704for (uint i = 0; i < C->cfg()->number_of_blocks(); i++) {705Block* block = C->cfg()->get_block(i);706begin_head(BLOCK_ELEMENT);707print_attr(BLOCK_NAME_PROPERTY, block->_pre_order);708end_head();709710head(SUCCESSORS_ELEMENT);711for (uint s = 0; s < block->_num_succs; s++) {712begin_elem(SUCCESSOR_ELEMENT);713print_attr(BLOCK_NAME_PROPERTY, block->_succs[s]->_pre_order);714end_elem();715}716tail(SUCCESSORS_ELEMENT);717718head(NODES_ELEMENT);719for (uint s = 0; s < block->number_of_nodes(); s++) {720begin_elem(NODE_ELEMENT);721print_attr(NODE_ID_PROPERTY, get_node_id(block->get_node(s)));722end_elem();723}724tail(NODES_ELEMENT);725726tail(BLOCK_ELEMENT);727}728tail(CONTROL_FLOW_ELEMENT);729}730tail(GRAPH_ELEMENT);731output()->flush();732}733734extern const char *NodeClassNames[];735736outputStream *IdealGraphPrinter::output() {737return _xml;738}739740#endif741742743