| 1 | /* |
| 2 | * Copyright (c) 2007, 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 | #include "precompiled.hpp" |
| 26 | #include "memory/resourceArea.hpp" |
| 27 | #include "opto/chaitin.hpp" |
| 28 | #include "opto/idealGraphPrinter.hpp" |
| 29 | #include "opto/machnode.hpp" |
| 30 | #include "opto/parse.hpp" |
| 31 | #include "runtime/threadCritical.hpp" |
| 32 | #include "runtime/threadSMR.hpp" |
| 33 | |
| 34 | #ifndef PRODUCT |
| 35 | |
| 36 | // Constants |
| 37 | // Keep consistent with Java constants |
| 38 | const char *IdealGraphPrinter::INDENT = " " ; |
| 39 | const char *IdealGraphPrinter::TOP_ELEMENT = "graphDocument" ; |
| 40 | const char *IdealGraphPrinter::GROUP_ELEMENT = "group" ; |
| 41 | const char *IdealGraphPrinter::GRAPH_ELEMENT = "graph" ; |
| 42 | const char *IdealGraphPrinter::PROPERTIES_ELEMENT = "properties" ; |
| 43 | const char *IdealGraphPrinter::EDGES_ELEMENT = "edges" ; |
| 44 | const char *IdealGraphPrinter::PROPERTY_ELEMENT = "p" ; |
| 45 | const char *IdealGraphPrinter::EDGE_ELEMENT = "edge" ; |
| 46 | const char *IdealGraphPrinter::NODE_ELEMENT = "node" ; |
| 47 | const char *IdealGraphPrinter::NODES_ELEMENT = "nodes" ; |
| 48 | const char *IdealGraphPrinter::REMOVE_EDGE_ELEMENT = "removeEdge" ; |
| 49 | const char *IdealGraphPrinter::REMOVE_NODE_ELEMENT = "removeNode" ; |
| 50 | const char *IdealGraphPrinter::METHOD_NAME_PROPERTY = "name" ; |
| 51 | const char *IdealGraphPrinter::METHOD_IS_PUBLIC_PROPERTY = "public" ; |
| 52 | const char *IdealGraphPrinter::METHOD_IS_STATIC_PROPERTY = "static" ; |
| 53 | const char *IdealGraphPrinter::TRUE_VALUE = "true" ; |
| 54 | const char *IdealGraphPrinter::NODE_NAME_PROPERTY = "name" ; |
| 55 | const char *IdealGraphPrinter::EDGE_NAME_PROPERTY = "name" ; |
| 56 | const char *IdealGraphPrinter::NODE_ID_PROPERTY = "id" ; |
| 57 | const char *IdealGraphPrinter::FROM_PROPERTY = "from" ; |
| 58 | const char *IdealGraphPrinter::TO_PROPERTY = "to" ; |
| 59 | const char *IdealGraphPrinter::PROPERTY_NAME_PROPERTY = "name" ; |
| 60 | const char *IdealGraphPrinter::GRAPH_NAME_PROPERTY = "name" ; |
| 61 | const char *IdealGraphPrinter::INDEX_PROPERTY = "index" ; |
| 62 | const char *IdealGraphPrinter::METHOD_ELEMENT = "method" ; |
| 63 | const char *IdealGraphPrinter::INLINE_ELEMENT = "inlined" ; |
| 64 | const char *IdealGraphPrinter::BYTECODES_ELEMENT = "bytecodes" ; |
| 65 | const char *IdealGraphPrinter::METHOD_BCI_PROPERTY = "bci" ; |
| 66 | const char *IdealGraphPrinter::METHOD_SHORT_NAME_PROPERTY = "shortName" ; |
| 67 | const char *IdealGraphPrinter::CONTROL_FLOW_ELEMENT = "controlFlow" ; |
| 68 | const char *IdealGraphPrinter::BLOCK_NAME_PROPERTY = "name" ; |
| 69 | const char *IdealGraphPrinter::BLOCK_DOMINATOR_PROPERTY = "dom" ; |
| 70 | const char *IdealGraphPrinter::BLOCK_ELEMENT = "block" ; |
| 71 | const char *IdealGraphPrinter::SUCCESSORS_ELEMENT = "successors" ; |
| 72 | const char *IdealGraphPrinter::SUCCESSOR_ELEMENT = "successor" ; |
| 73 | const char *IdealGraphPrinter::ASSEMBLY_ELEMENT = "assembly" ; |
| 74 | |
| 75 | int IdealGraphPrinter::_file_count = 0; |
| 76 | |
| 77 | IdealGraphPrinter *IdealGraphPrinter::printer() { |
| 78 | if (!PrintIdealGraph) { |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | JavaThread *thread = JavaThread::current(); |
| 83 | if (!thread->is_Compiler_thread()) return NULL; |
| 84 | |
| 85 | CompilerThread *compiler_thread = (CompilerThread *)thread; |
| 86 | if (compiler_thread->ideal_graph_printer() == NULL) { |
| 87 | IdealGraphPrinter *printer = new IdealGraphPrinter(); |
| 88 | compiler_thread->set_ideal_graph_printer(printer); |
| 89 | } |
| 90 | |
| 91 | return compiler_thread->ideal_graph_printer(); |
| 92 | } |
| 93 | |
| 94 | void IdealGraphPrinter::clean_up() { |
| 95 | for (JavaThreadIteratorWithHandle jtiwh; JavaThread *p = jtiwh.next(); ) { |
| 96 | if (p->is_Compiler_thread()) { |
| 97 | CompilerThread *c = (CompilerThread *)p; |
| 98 | IdealGraphPrinter *printer = c->ideal_graph_printer(); |
| 99 | if (printer) { |
| 100 | delete printer; |
| 101 | } |
| 102 | c->set_ideal_graph_printer(NULL); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Constructor, either file or network output |
| 108 | IdealGraphPrinter::IdealGraphPrinter() { |
| 109 | |
| 110 | // By default dump both ins and outs since dead or unreachable code |
| 111 | // needs to appear in the graph. There are also some special cases |
| 112 | // in the mach where kill projections have no users but should |
| 113 | // appear in the dump. |
| 114 | _traverse_outs = true; |
| 115 | _should_send_method = true; |
| 116 | _output = NULL; |
| 117 | buffer[0] = 0; |
| 118 | _depth = 0; |
| 119 | _current_method = NULL; |
| 120 | assert(!_current_method, "current method must be initialized to NULL" ); |
| 121 | _stream = NULL; |
| 122 | |
| 123 | if (PrintIdealGraphFile != NULL) { |
| 124 | ThreadCritical tc; |
| 125 | // User wants all output to go to files |
| 126 | if (_file_count != 0) { |
| 127 | ResourceMark rm; |
| 128 | stringStream st; |
| 129 | const char* dot = strrchr(PrintIdealGraphFile, '.'); |
| 130 | if (dot) { |
| 131 | st.write(PrintIdealGraphFile, dot - PrintIdealGraphFile); |
| 132 | st.print("%d%s" , _file_count, dot); |
| 133 | } else { |
| 134 | st.print("%s%d" , PrintIdealGraphFile, _file_count); |
| 135 | } |
| 136 | fileStream *stream = new (ResourceObj::C_HEAP, mtCompiler) fileStream(st.as_string()); |
| 137 | _output = stream; |
| 138 | } else { |
| 139 | fileStream *stream = new (ResourceObj::C_HEAP, mtCompiler) fileStream(PrintIdealGraphFile); |
| 140 | _output = stream; |
| 141 | } |
| 142 | _file_count++; |
| 143 | } else { |
| 144 | _stream = new (ResourceObj::C_HEAP, mtCompiler) networkStream(); |
| 145 | |
| 146 | // Try to connect to visualizer |
| 147 | if (_stream->connect(PrintIdealGraphAddress, PrintIdealGraphPort)) { |
| 148 | char c = 0; |
| 149 | _stream->read(&c, 1); |
| 150 | if (c != 'y') { |
| 151 | tty->print_cr("Client available, but does not want to receive data!" ); |
| 152 | _stream->close(); |
| 153 | delete _stream; |
| 154 | _stream = NULL; |
| 155 | return; |
| 156 | } |
| 157 | _output = _stream; |
| 158 | } else { |
| 159 | // It would be nice if we could shut down cleanly but it should |
| 160 | // be an error if we can't connect to the visualizer. |
| 161 | fatal("Couldn't connect to visualizer at %s:" INTX_FORMAT, |
| 162 | PrintIdealGraphAddress, PrintIdealGraphPort); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | _xml = new (ResourceObj::C_HEAP, mtCompiler) xmlStream(_output); |
| 167 | |
| 168 | head(TOP_ELEMENT); |
| 169 | } |
| 170 | |
| 171 | // Destructor, close file or network stream |
| 172 | IdealGraphPrinter::~IdealGraphPrinter() { |
| 173 | |
| 174 | tail(TOP_ELEMENT); |
| 175 | |
| 176 | // tty->print_cr("Walk time: %d", (int)_walk_time.milliseconds()); |
| 177 | // tty->print_cr("Output time: %d", (int)_output_time.milliseconds()); |
| 178 | // tty->print_cr("Build blocks time: %d", (int)_build_blocks_time.milliseconds()); |
| 179 | |
| 180 | if(_xml) { |
| 181 | delete _xml; |
| 182 | _xml = NULL; |
| 183 | } |
| 184 | |
| 185 | if (_stream) { |
| 186 | delete _stream; |
| 187 | if (_stream == _output) { |
| 188 | _output = NULL; |
| 189 | } |
| 190 | _stream = NULL; |
| 191 | } |
| 192 | |
| 193 | if (_output) { |
| 194 | delete _output; |
| 195 | _output = NULL; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void IdealGraphPrinter::begin_elem(const char *s) { |
| 200 | _xml->begin_elem("%s" , s); |
| 201 | } |
| 202 | |
| 203 | void IdealGraphPrinter::end_elem() { |
| 204 | _xml->end_elem(); |
| 205 | } |
| 206 | |
| 207 | void IdealGraphPrinter::begin_head(const char *s) { |
| 208 | _xml->begin_head("%s" , s); |
| 209 | } |
| 210 | |
| 211 | void IdealGraphPrinter::end_head() { |
| 212 | _xml->end_head(); |
| 213 | } |
| 214 | |
| 215 | void IdealGraphPrinter::print_attr(const char *name, intptr_t val) { |
| 216 | stringStream stream; |
| 217 | stream.print(INTX_FORMAT, val); |
| 218 | print_attr(name, stream.as_string()); |
| 219 | } |
| 220 | |
| 221 | void IdealGraphPrinter::print_attr(const char *name, const char *val) { |
| 222 | _xml->print(" %s='" , name); |
| 223 | text(val); |
| 224 | _xml->print("'" ); |
| 225 | } |
| 226 | |
| 227 | void IdealGraphPrinter::head(const char *name) { |
| 228 | _xml->head("%s" , name); |
| 229 | } |
| 230 | |
| 231 | void IdealGraphPrinter::tail(const char *name) { |
| 232 | _xml->tail(name); |
| 233 | } |
| 234 | |
| 235 | void IdealGraphPrinter::text(const char *s) { |
| 236 | _xml->text("%s" , s); |
| 237 | } |
| 238 | |
| 239 | void IdealGraphPrinter::print_prop(const char *name, int val) { |
| 240 | stringStream stream; |
| 241 | stream.print("%d" , val); |
| 242 | print_prop(name, stream.as_string()); |
| 243 | } |
| 244 | |
| 245 | void IdealGraphPrinter::print_prop(const char *name, const char *val) { |
| 246 | begin_head(PROPERTY_ELEMENT); |
| 247 | print_attr(PROPERTY_NAME_PROPERTY, name); |
| 248 | end_head(); |
| 249 | text(val); |
| 250 | tail(PROPERTY_ELEMENT); |
| 251 | } |
| 252 | |
| 253 | void IdealGraphPrinter::print_method(ciMethod *method, int bci, InlineTree *tree) { |
| 254 | begin_head(METHOD_ELEMENT); |
| 255 | |
| 256 | stringStream str; |
| 257 | method->print_name(&str); |
| 258 | |
| 259 | stringStream shortStr; |
| 260 | method->print_short_name(&shortStr); |
| 261 | |
| 262 | print_attr(METHOD_NAME_PROPERTY, str.as_string()); |
| 263 | print_attr(METHOD_SHORT_NAME_PROPERTY, shortStr.as_string()); |
| 264 | print_attr(METHOD_BCI_PROPERTY, bci); |
| 265 | |
| 266 | end_head(); |
| 267 | |
| 268 | head(BYTECODES_ELEMENT); |
| 269 | _xml->print_cr("<![CDATA[" ); |
| 270 | method->print_codes_on(_xml); |
| 271 | _xml->print_cr("]]>" ); |
| 272 | tail(BYTECODES_ELEMENT); |
| 273 | |
| 274 | if (tree != NULL && tree->subtrees().length() > 0) { |
| 275 | head(INLINE_ELEMENT); |
| 276 | GrowableArray<InlineTree *> subtrees = tree->subtrees(); |
| 277 | for (int i = 0; i < subtrees.length(); i++) { |
| 278 | print_inline_tree(subtrees.at(i)); |
| 279 | } |
| 280 | tail(INLINE_ELEMENT); |
| 281 | } |
| 282 | |
| 283 | tail(METHOD_ELEMENT); |
| 284 | _xml->flush(); |
| 285 | } |
| 286 | |
| 287 | void IdealGraphPrinter::print_inline_tree(InlineTree *tree) { |
| 288 | |
| 289 | if (tree == NULL) return; |
| 290 | |
| 291 | ciMethod *method = tree->method(); |
| 292 | print_method(tree->method(), tree->caller_bci(), tree); |
| 293 | |
| 294 | } |
| 295 | |
| 296 | void IdealGraphPrinter::print_inlining() { |
| 297 | |
| 298 | // Print inline tree |
| 299 | if (_should_send_method) { |
| 300 | InlineTree *inlineTree = C->ilt(); |
| 301 | if (inlineTree != NULL) { |
| 302 | print_inline_tree(inlineTree); |
| 303 | } else { |
| 304 | // print this method only |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // Has to be called whenever a method is compiled |
| 310 | void IdealGraphPrinter::begin_method() { |
| 311 | |
| 312 | ciMethod *method = C->method(); |
| 313 | assert(_output, "output stream must exist!" ); |
| 314 | assert(method, "null methods are not allowed!" ); |
| 315 | assert(!_current_method, "current method must be null!" ); |
| 316 | |
| 317 | head(GROUP_ELEMENT); |
| 318 | |
| 319 | head(PROPERTIES_ELEMENT); |
| 320 | |
| 321 | // Print properties |
| 322 | // Add method name |
| 323 | stringStream strStream; |
| 324 | method->print_name(&strStream); |
| 325 | print_prop(METHOD_NAME_PROPERTY, strStream.as_string()); |
| 326 | |
| 327 | if (method->flags().is_public()) { |
| 328 | print_prop(METHOD_IS_PUBLIC_PROPERTY, TRUE_VALUE); |
| 329 | } |
| 330 | |
| 331 | if (method->flags().is_static()) { |
| 332 | print_prop(METHOD_IS_STATIC_PROPERTY, TRUE_VALUE); |
| 333 | } |
| 334 | |
| 335 | tail(PROPERTIES_ELEMENT); |
| 336 | |
| 337 | _should_send_method = true; |
| 338 | this->_current_method = method; |
| 339 | |
| 340 | _xml->flush(); |
| 341 | } |
| 342 | |
| 343 | // Has to be called whenever a method has finished compilation |
| 344 | void IdealGraphPrinter::end_method() { |
| 345 | |
| 346 | nmethod* method = (nmethod*)this->_current_method->code(); |
| 347 | |
| 348 | tail(GROUP_ELEMENT); |
| 349 | _current_method = NULL; |
| 350 | _xml->flush(); |
| 351 | } |
| 352 | |
| 353 | bool IdealGraphPrinter::traverse_outs() { |
| 354 | return _traverse_outs; |
| 355 | } |
| 356 | |
| 357 | void IdealGraphPrinter::set_traverse_outs(bool b) { |
| 358 | _traverse_outs = b; |
| 359 | } |
| 360 | |
| 361 | void IdealGraphPrinter::visit_node(Node *n, bool edges, VectorSet* temp_set) { |
| 362 | |
| 363 | if (edges) { |
| 364 | |
| 365 | // Output edge |
| 366 | node_idx_t dest_id = n->_idx; |
| 367 | for ( uint i = 0; i < n->len(); i++ ) { |
| 368 | if ( n->in(i) ) { |
| 369 | Node *source = n->in(i); |
| 370 | begin_elem(EDGE_ELEMENT); |
| 371 | print_attr(FROM_PROPERTY, source->_idx); |
| 372 | print_attr(TO_PROPERTY, dest_id); |
| 373 | print_attr(INDEX_PROPERTY, i); |
| 374 | end_elem(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | } else { |
| 379 | |
| 380 | // Output node |
| 381 | begin_head(NODE_ELEMENT); |
| 382 | print_attr(NODE_ID_PROPERTY, n->_idx); |
| 383 | end_head(); |
| 384 | |
| 385 | head(PROPERTIES_ELEMENT); |
| 386 | |
| 387 | Node *node = n; |
| 388 | #ifndef PRODUCT |
| 389 | Compile::current()->_in_dump_cnt++; |
| 390 | print_prop(NODE_NAME_PROPERTY, (const char *)node->Name()); |
| 391 | const Type *t = node->bottom_type(); |
| 392 | print_prop("type" , t->msg()); |
| 393 | print_prop("idx" , node->_idx); |
| 394 | #ifdef ASSERT |
| 395 | print_prop("debug_idx" , node->_debug_idx); |
| 396 | #endif |
| 397 | |
| 398 | if (C->cfg() != NULL) { |
| 399 | Block* block = C->cfg()->get_block_for_node(node); |
| 400 | if (block == NULL) { |
| 401 | print_prop("block" , C->cfg()->get_block(0)->_pre_order); |
| 402 | } else { |
| 403 | print_prop("block" , block->_pre_order); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | const jushort flags = node->flags(); |
| 408 | if (flags & Node::Flag_is_Copy) { |
| 409 | print_prop("is_copy" , "true" ); |
| 410 | } |
| 411 | if (flags & Node::Flag_rematerialize) { |
| 412 | print_prop("rematerialize" , "true" ); |
| 413 | } |
| 414 | if (flags & Node::Flag_needs_anti_dependence_check) { |
| 415 | print_prop("needs_anti_dependence_check" , "true" ); |
| 416 | } |
| 417 | if (flags & Node::Flag_is_macro) { |
| 418 | print_prop("is_macro" , "true" ); |
| 419 | } |
| 420 | if (flags & Node::Flag_is_Con) { |
| 421 | print_prop("is_con" , "true" ); |
| 422 | } |
| 423 | if (flags & Node::Flag_is_cisc_alternate) { |
| 424 | print_prop("is_cisc_alternate" , "true" ); |
| 425 | } |
| 426 | if (flags & Node::Flag_is_dead_loop_safe) { |
| 427 | print_prop("is_dead_loop_safe" , "true" ); |
| 428 | } |
| 429 | if (flags & Node::Flag_may_be_short_branch) { |
| 430 | print_prop("may_be_short_branch" , "true" ); |
| 431 | } |
| 432 | if (flags & Node::Flag_has_call) { |
| 433 | print_prop("has_call" , "true" ); |
| 434 | } |
| 435 | |
| 436 | if (C->matcher() != NULL) { |
| 437 | if (C->matcher()->is_shared(node)) { |
| 438 | print_prop("is_shared" , "true" ); |
| 439 | } else { |
| 440 | print_prop("is_shared" , "false" ); |
| 441 | } |
| 442 | if (C->matcher()->is_dontcare(node)) { |
| 443 | print_prop("is_dontcare" , "true" ); |
| 444 | } else { |
| 445 | print_prop("is_dontcare" , "false" ); |
| 446 | } |
| 447 | |
| 448 | #ifdef ASSERT |
| 449 | Node* old = C->matcher()->find_old_node(node); |
| 450 | if (old != NULL) { |
| 451 | print_prop("old_node_idx" , old->_idx); |
| 452 | } |
| 453 | #endif |
| 454 | } |
| 455 | |
| 456 | if (node->is_Proj()) { |
| 457 | print_prop("con" , (int)node->as_Proj()->_con); |
| 458 | } |
| 459 | |
| 460 | if (node->is_Mach()) { |
| 461 | print_prop("idealOpcode" , (const char *)NodeClassNames[node->as_Mach()->ideal_Opcode()]); |
| 462 | } |
| 463 | |
| 464 | buffer[0] = 0; |
| 465 | stringStream s2(buffer, sizeof(buffer) - 1); |
| 466 | |
| 467 | node->dump_spec(&s2); |
| 468 | if (t != NULL && (t->isa_instptr() || t->isa_klassptr())) { |
| 469 | const TypeInstPtr *toop = t->isa_instptr(); |
| 470 | const TypeKlassPtr *tkls = t->isa_klassptr(); |
| 471 | ciKlass* klass = toop ? toop->klass() : (tkls ? tkls->klass() : NULL ); |
| 472 | if( klass && klass->is_loaded() && klass->is_interface() ) { |
| 473 | s2.print(" Interface:" ); |
| 474 | } else if( toop ) { |
| 475 | s2.print(" Oop:" ); |
| 476 | } else if( tkls ) { |
| 477 | s2.print(" Klass:" ); |
| 478 | } |
| 479 | t->dump_on(&s2); |
| 480 | } else if( t == Type::MEMORY ) { |
| 481 | s2.print(" Memory:" ); |
| 482 | MemNode::dump_adr_type(node, node->adr_type(), &s2); |
| 483 | } |
| 484 | |
| 485 | assert(s2.size() < sizeof(buffer), "size in range" ); |
| 486 | print_prop("dump_spec" , buffer); |
| 487 | |
| 488 | if (node->is_block_proj()) { |
| 489 | print_prop("is_block_proj" , "true" ); |
| 490 | } |
| 491 | |
| 492 | if (node->is_block_start()) { |
| 493 | print_prop("is_block_start" , "true" ); |
| 494 | } |
| 495 | |
| 496 | const char *short_name = "short_name" ; |
| 497 | if (strcmp(node->Name(), "Parm" ) == 0 && node->as_Proj()->_con >= TypeFunc::Parms) { |
| 498 | int index = node->as_Proj()->_con - TypeFunc::Parms; |
| 499 | if (index >= 10) { |
| 500 | print_prop(short_name, "PA" ); |
| 501 | } else { |
| 502 | sprintf(buffer, "P%d" , index); |
| 503 | print_prop(short_name, buffer); |
| 504 | } |
| 505 | } else if (strcmp(node->Name(), "IfTrue" ) == 0) { |
| 506 | print_prop(short_name, "T" ); |
| 507 | } else if (strcmp(node->Name(), "IfFalse" ) == 0) { |
| 508 | print_prop(short_name, "F" ); |
| 509 | } else if ((node->is_Con() && node->is_Type()) || node->is_Proj()) { |
| 510 | |
| 511 | if (t->base() == Type::Int && t->is_int()->is_con()) { |
| 512 | const TypeInt *typeInt = t->is_int(); |
| 513 | assert(typeInt->is_con(), "must be constant" ); |
| 514 | jint value = typeInt->get_con(); |
| 515 | |
| 516 | // max. 2 chars allowed |
| 517 | if (value >= -9 && value <= 99) { |
| 518 | sprintf(buffer, "%d" , value); |
| 519 | print_prop(short_name, buffer); |
| 520 | } else { |
| 521 | print_prop(short_name, "I" ); |
| 522 | } |
| 523 | } else if (t == Type::TOP) { |
| 524 | print_prop(short_name, "^" ); |
| 525 | } else if (t->base() == Type::Long && t->is_long()->is_con()) { |
| 526 | const TypeLong *typeLong = t->is_long(); |
| 527 | assert(typeLong->is_con(), "must be constant" ); |
| 528 | jlong value = typeLong->get_con(); |
| 529 | |
| 530 | // max. 2 chars allowed |
| 531 | if (value >= -9 && value <= 99) { |
| 532 | sprintf(buffer, JLONG_FORMAT, value); |
| 533 | print_prop(short_name, buffer); |
| 534 | } else { |
| 535 | print_prop(short_name, "L" ); |
| 536 | } |
| 537 | } else if (t->base() == Type::KlassPtr) { |
| 538 | const TypeKlassPtr *typeKlass = t->is_klassptr(); |
| 539 | print_prop(short_name, "CP" ); |
| 540 | } else if (t->base() == Type::Control) { |
| 541 | print_prop(short_name, "C" ); |
| 542 | } else if (t->base() == Type::Memory) { |
| 543 | print_prop(short_name, "M" ); |
| 544 | } else if (t->base() == Type::Abio) { |
| 545 | print_prop(short_name, "IO" ); |
| 546 | } else if (t->base() == Type::Return_Address) { |
| 547 | print_prop(short_name, "RA" ); |
| 548 | } else if (t->base() == Type::AnyPtr) { |
| 549 | print_prop(short_name, "P" ); |
| 550 | } else if (t->base() == Type::RawPtr) { |
| 551 | print_prop(short_name, "RP" ); |
| 552 | } else if (t->base() == Type::AryPtr) { |
| 553 | print_prop(short_name, "AP" ); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | JVMState* caller = NULL; |
| 558 | if (node->is_SafePoint()) { |
| 559 | caller = node->as_SafePoint()->jvms(); |
| 560 | } else { |
| 561 | Node_Notes* notes = C->node_notes_at(node->_idx); |
| 562 | if (notes != NULL) { |
| 563 | caller = notes->jvms(); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if (caller != NULL) { |
| 568 | stringStream bciStream; |
| 569 | ciMethod* last = NULL; |
| 570 | int last_bci; |
| 571 | while(caller) { |
| 572 | if (caller->has_method()) { |
| 573 | last = caller->method(); |
| 574 | last_bci = caller->bci(); |
| 575 | } |
| 576 | bciStream.print("%d " , caller->bci()); |
| 577 | caller = caller->caller(); |
| 578 | } |
| 579 | print_prop("bci" , bciStream.as_string()); |
| 580 | if (last != NULL && last->has_linenumber_table() && last_bci >= 0) { |
| 581 | print_prop("line" , last->line_number_from_bci(last_bci)); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | #ifdef ASSERT |
| 586 | if (node->debug_orig() != NULL) { |
| 587 | temp_set->Clear(); |
| 588 | stringStream dorigStream; |
| 589 | Node* dorig = node->debug_orig(); |
| 590 | while (dorig && temp_set->test_set(dorig->_idx)) { |
| 591 | dorigStream.print("%d " , dorig->_idx); |
| 592 | } |
| 593 | print_prop("debug_orig" , dorigStream.as_string()); |
| 594 | } |
| 595 | #endif |
| 596 | |
| 597 | if (_chaitin && _chaitin != (PhaseChaitin *)((intptr_t)0xdeadbeef)) { |
| 598 | buffer[0] = 0; |
| 599 | _chaitin->dump_register(node, buffer); |
| 600 | print_prop("reg" , buffer); |
| 601 | uint lrg_id = 0; |
| 602 | if (node->_idx < _chaitin->_lrg_map.size()) { |
| 603 | lrg_id = _chaitin->_lrg_map.live_range_id(node); |
| 604 | } |
| 605 | print_prop("lrg" , lrg_id); |
| 606 | } |
| 607 | |
| 608 | Compile::current()->_in_dump_cnt--; |
| 609 | #endif |
| 610 | |
| 611 | tail(PROPERTIES_ELEMENT); |
| 612 | tail(NODE_ELEMENT); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | void IdealGraphPrinter::walk_nodes(Node *start, bool edges, VectorSet* temp_set) { |
| 617 | |
| 618 | |
| 619 | VectorSet visited(Thread::current()->resource_area()); |
| 620 | GrowableArray<Node *> nodeStack(Thread::current()->resource_area(), 0, 0, NULL); |
| 621 | nodeStack.push(start); |
| 622 | visited.test_set(start->_idx); |
| 623 | if (C->cfg() != NULL) { |
| 624 | // once we have a CFG there are some nodes that aren't really |
| 625 | // reachable but are in the CFG so add them here. |
| 626 | for (uint i = 0; i < C->cfg()->number_of_blocks(); i++) { |
| 627 | Block* block = C->cfg()->get_block(i); |
| 628 | for (uint s = 0; s < block->number_of_nodes(); s++) { |
| 629 | nodeStack.push(block->get_node(s)); |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | while(nodeStack.length() > 0) { |
| 635 | |
| 636 | Node *n = nodeStack.pop(); |
| 637 | visit_node(n, edges, temp_set); |
| 638 | |
| 639 | if (_traverse_outs) { |
| 640 | for (DUIterator i = n->outs(); n->has_out(i); i++) { |
| 641 | Node* p = n->out(i); |
| 642 | if (!visited.test_set(p->_idx)) { |
| 643 | nodeStack.push(p); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | for ( uint i = 0; i < n->len(); i++ ) { |
| 649 | if ( n->in(i) ) { |
| 650 | if (!visited.test_set(n->in(i)->_idx)) { |
| 651 | nodeStack.push(n->in(i)); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void IdealGraphPrinter::print_method(const char *name, int level) { |
| 659 | if (should_print(level)) { |
| 660 | print(name, (Node *) C->root()); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // Print current ideal graph |
| 665 | void IdealGraphPrinter::print(const char *name, Node *node) { |
| 666 | |
| 667 | if (!_current_method || !_should_send_method) return; |
| 668 | |
| 669 | // Warning, unsafe cast? |
| 670 | _chaitin = (PhaseChaitin *)C->regalloc(); |
| 671 | |
| 672 | begin_head(GRAPH_ELEMENT); |
| 673 | print_attr(GRAPH_NAME_PROPERTY, (const char *)name); |
| 674 | end_head(); |
| 675 | |
| 676 | VectorSet temp_set(Thread::current()->resource_area()); |
| 677 | |
| 678 | head(NODES_ELEMENT); |
| 679 | walk_nodes(node, false, &temp_set); |
| 680 | tail(NODES_ELEMENT); |
| 681 | |
| 682 | head(EDGES_ELEMENT); |
| 683 | walk_nodes(node, true, &temp_set); |
| 684 | tail(EDGES_ELEMENT); |
| 685 | if (C->cfg() != NULL) { |
| 686 | head(CONTROL_FLOW_ELEMENT); |
| 687 | for (uint i = 0; i < C->cfg()->number_of_blocks(); i++) { |
| 688 | Block* block = C->cfg()->get_block(i); |
| 689 | begin_head(BLOCK_ELEMENT); |
| 690 | print_attr(BLOCK_NAME_PROPERTY, block->_pre_order); |
| 691 | end_head(); |
| 692 | |
| 693 | head(SUCCESSORS_ELEMENT); |
| 694 | for (uint s = 0; s < block->_num_succs; s++) { |
| 695 | begin_elem(SUCCESSOR_ELEMENT); |
| 696 | print_attr(BLOCK_NAME_PROPERTY, block->_succs[s]->_pre_order); |
| 697 | end_elem(); |
| 698 | } |
| 699 | tail(SUCCESSORS_ELEMENT); |
| 700 | |
| 701 | head(NODES_ELEMENT); |
| 702 | for (uint s = 0; s < block->number_of_nodes(); s++) { |
| 703 | begin_elem(NODE_ELEMENT); |
| 704 | print_attr(NODE_ID_PROPERTY, block->get_node(s)->_idx); |
| 705 | end_elem(); |
| 706 | } |
| 707 | tail(NODES_ELEMENT); |
| 708 | |
| 709 | tail(BLOCK_ELEMENT); |
| 710 | } |
| 711 | tail(CONTROL_FLOW_ELEMENT); |
| 712 | } |
| 713 | tail(GRAPH_ELEMENT); |
| 714 | _xml->flush(); |
| 715 | } |
| 716 | |
| 717 | // Should method be printed? |
| 718 | bool IdealGraphPrinter::should_print(int level) { |
| 719 | return C->directive()->IGVPrintLevelOption >= level; |
| 720 | } |
| 721 | |
| 722 | extern const char *NodeClassNames[]; |
| 723 | |
| 724 | #endif |
| 725 | |