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