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