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