1 /*
   2  * Copyright (c) 2005, 2014, 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 "c1/c1_CFGPrinter.hpp"
  27 #include "c1/c1_IR.hpp"
  28 #include "c1/c1_InstructionPrinter.hpp"
  29 #include "c1/c1_LIR.hpp"
  30 #include "c1/c1_LinearScan.hpp"
  31 #include "c1/c1_ValueStack.hpp"
  32 
  33 #ifndef PRODUCT
  34 
  35 void CFGPrinter::print_compilation(Compilation* compilation) {
  36   CFGPrinterOutput* output = compilation->cfg_printer_output();
  37   output->print_compilation();
  38 }
  39 
  40 void CFGPrinter::print_cfg(BlockList* blocks, const char* name, bool do_print_HIR, bool do_print_LIR) {
  41   CFGPrinterOutput* output = Compilation::current()->cfg_printer_output();
  42   output->set_print_flags(do_print_HIR, do_print_LIR);
  43   output->print_cfg(blocks, name);
  44 }
  45 
  46 void CFGPrinter::print_cfg(IR* blocks, const char* name, bool do_print_HIR, bool do_print_LIR) {
  47   CFGPrinterOutput* output = Compilation::current()->cfg_printer_output();
  48   output->set_print_flags(do_print_HIR, do_print_LIR);
  49   output->print_cfg(blocks, name);
  50 }
  51 
  52 void CFGPrinter::print_intervals(IntervalList* intervals, const char* name) {
  53   CFGPrinterOutput* output = Compilation::current()->cfg_printer_output();
  54   output->print_intervals(intervals, name);
  55 }
  56 
  57 
  58 CFGPrinterOutput::CFGPrinterOutput(Compilation* compilation)
  59  : _output(NULL),
  60    _compilation(compilation),
  61    _do_print_HIR(false),
  62    _do_print_LIR(false)
  63 {
  64   char file_name[O_BUFLEN];
  65   jio_snprintf(file_name, sizeof(file_name), "output_tid" UINTX_FORMAT "_pid%u.cfg",
  66                os::current_thread_id(), os::current_process_id());
  67   _output = new(ResourceObj::C_HEAP, mtCompiler) fileStream(file_name, "at");
  68 }
  69 
  70 void CFGPrinterOutput::inc_indent() {
  71   output()->inc();
  72   output()->inc();
  73 }
  74 
  75 void CFGPrinterOutput::dec_indent() {
  76   output()->dec();
  77   output()->dec();
  78 }
  79 
  80 void CFGPrinterOutput::print(const char* format, ...) {
  81   output()->indent();
  82 
  83   va_list ap;
  84   va_start(ap, format);
  85   output()->vprint_cr(format, ap);
  86   va_end(ap);
  87 }
  88 
  89 void CFGPrinterOutput::print_begin(const char* tag) {
  90   output()->indent();
  91   output()->print_cr("begin_%s", tag);
  92   inc_indent();
  93 }
  94 
  95 void CFGPrinterOutput::print_end(const char* tag) {
  96   dec_indent();
  97   output()->indent();
  98   output()->print_cr("end_%s", tag);
  99 }
 100 
 101 
 102 char* CFGPrinterOutput::method_name(ciMethod* method, bool short_name) {
 103   stringStream name;
 104   if (short_name) {
 105     method->print_short_name(&name);
 106   } else {
 107     method->print_name(&name);
 108   }
 109   return name.as_string();
 110 
 111 }
 112 
 113 
 114 void CFGPrinterOutput::print_compilation() {
 115   print_begin("compilation");
 116 
 117   print("name \"%s\"", method_name(_compilation->method(), true));
 118   print("method \"%s\"", method_name(_compilation->method()));
 119   print("date " INT64_FORMAT, (int64_t) os::javaTimeMillis());
 120 
 121   print_end("compilation");
 122 }
 123 
 124 
 125 
 126 
 127 
 128 void CFGPrinterOutput::print_state(BlockBegin* block) {
 129   print_begin("states");
 130 
 131   InstructionPrinter ip(true, output());
 132 
 133   ValueStack* state = block->state();
 134   int index;
 135   Value value;
 136 
 137   for_each_state(state) {
 138     print_begin("locals");
 139     print("size %d", state->locals_size());
 140     print("method \"%s\"", method_name(state->scope()->method()));
 141 
 142     for_each_local_value(state, index, value) {
 143       ip.print_phi(index, value, block);
 144       print_operand(value);
 145       output()->cr();
 146     }
 147     print_end("locals");
 148 
 149     if (state->stack_size() > 0) {
 150       print_begin("stack");
 151       print("size %d", state->stack_size());
 152       print("method \"%s\"", method_name(state->scope()->method()));
 153 
 154       for_each_stack_value(state, index, value) {
 155         ip.print_phi(index, value, block);
 156         print_operand(value);
 157         output()->cr();
 158       }
 159 
 160       print_end("stack");
 161     }
 162 
 163     if (state->locks_size() > 0) {
 164       print_begin("locks");
 165       print("size %d", state->locks_size());
 166       print("method \"%s\"", method_name(state->scope()->method()));
 167 
 168       for_each_lock_value(state, index, value) {
 169         ip.print_phi(index, value, block);
 170         print_operand(value);
 171         output()->cr();
 172       }
 173       print_end("locks");
 174     }
 175   }
 176 
 177   print_end("states");
 178 }
 179 
 180 
 181 void CFGPrinterOutput::print_operand(Value instr) {
 182   if (instr->operand()->is_virtual()) {
 183     output()->print(" \"");
 184     instr->operand()->print(output());
 185     output()->print("\" ");
 186   }
 187 }
 188 
 189 void CFGPrinterOutput::print_HIR(Value instr) {
 190   InstructionPrinter ip(true, output());
 191 
 192   if (instr->is_pinned()) {
 193     output()->put('.');
 194   }
 195 
 196   output()->print("%d %d ", instr->printable_bci(), instr->use_count());
 197 
 198   print_operand(instr);
 199 
 200   ip.print_temp(instr);
 201   output()->print(" ");
 202   ip.print_instr(instr);
 203 
 204   output()->print_cr(" <|@");
 205 }
 206 
 207 void CFGPrinterOutput::print_HIR(BlockBegin* block) {
 208   print_begin("HIR");
 209 
 210   Value cur = block->next();
 211   while (cur != NULL) {
 212     print_HIR(cur);
 213     cur = cur->next();
 214   }
 215 
 216   print_end("HIR");
 217 }
 218 
 219 void CFGPrinterOutput::print_LIR(BlockBegin* block) {
 220   print_begin("LIR");
 221 
 222   for (int i = 0; i < block->lir()->length(); i++) {
 223     block->lir()->at(i)->print_on(output());
 224     output()->print_cr(" <|@ ");
 225   }
 226 
 227   print_end("LIR");
 228 }
 229 
 230 
 231 void CFGPrinterOutput::print_block(BlockBegin* block) {
 232   print_begin("block");
 233 
 234   print("name \"B%d\"", block->block_id());
 235 
 236   print("from_bci %d", block->bci());
 237   print("to_bci %d", (block->end() == NULL ? -1 : block->end()->printable_bci()));
 238 
 239   output()->indent();
 240   output()->print("predecessors ");
 241   int i;
 242   for (i = 0; i < block->number_of_preds(); i++) {
 243     output()->print("\"B%d\" ", block->pred_at(i)->block_id());
 244   }
 245   output()->cr();
 246 
 247   output()->indent();
 248   output()->print("successors ");
 249   for (i = 0; i < block->number_of_sux(); i++) {
 250     output()->print("\"B%d\" ", block->sux_at(i)->block_id());
 251   }
 252   output()->cr();
 253 
 254   output()->indent();
 255   output()->print("xhandlers");
 256   for (i = 0; i < block->number_of_exception_handlers(); i++) {
 257     output()->print("\"B%d\" ", block->exception_handler_at(i)->block_id());
 258   }
 259   output()->cr();
 260 
 261   output()->indent();
 262   output()->print("flags ");
 263   if (block->is_set(BlockBegin::std_entry_flag))                output()->print("\"std\" ");
 264   if (block->is_set(BlockBegin::osr_entry_flag))                output()->print("\"osr\" ");
 265   if (block->is_set(BlockBegin::exception_entry_flag))          output()->print("\"ex\" ");
 266   if (block->is_set(BlockBegin::subroutine_entry_flag))         output()->print("\"sr\" ");
 267   if (block->is_set(BlockBegin::backward_branch_target_flag))   output()->print("\"bb\" ");
 268   if (block->is_set(BlockBegin::parser_loop_header_flag))       output()->print("\"plh\" ");
 269   if (block->is_set(BlockBegin::critical_edge_split_flag))      output()->print("\"ces\" ");
 270   if (block->is_set(BlockBegin::linear_scan_loop_header_flag))  output()->print("\"llh\" ");
 271   if (block->is_set(BlockBegin::linear_scan_loop_end_flag))     output()->print("\"lle\" ");
 272   output()->cr();
 273 
 274   if (block->dominator() != NULL) {
 275     print("dominator \"B%d\"", block->dominator()->block_id());
 276   }
 277   if (block->loop_index() != -1) {
 278     print("loop_index %d", block->loop_index());
 279     print("loop_depth %d", block->loop_depth());
 280   }
 281 
 282   if (block->first_lir_instruction_id() != -1) {
 283     print("first_lir_id %d", block->first_lir_instruction_id());
 284     print("last_lir_id %d", block->last_lir_instruction_id());
 285   }
 286 
 287   if (_do_print_HIR) {
 288     print_state(block);
 289     print_HIR(block);
 290   }
 291 
 292   if (_do_print_LIR) {
 293     print_LIR(block);
 294   }
 295 
 296   print_end("block");
 297 }
 298 
 299 
 300 
 301 void CFGPrinterOutput::print_cfg(BlockList* blocks, const char* name) {
 302   print_begin("cfg");
 303   print("name \"%s\"", name);
 304 
 305   PrintBlockClosure print_block;
 306   blocks->iterate_forward(&print_block);
 307 
 308   print_end("cfg");
 309   output()->flush();
 310 }
 311 
 312 void CFGPrinterOutput::print_cfg(IR* blocks, const char* name) {
 313   print_begin("cfg");
 314   print("name \"%s\"", name);
 315 
 316   PrintBlockClosure print_block;
 317   blocks->iterate_preorder(&print_block);
 318 
 319   print_end("cfg");
 320   output()->flush();
 321 }
 322 
 323 
 324 
 325 
 326 void CFGPrinterOutput::print_intervals(IntervalList* intervals, const char* name) {
 327   print_begin("intervals");
 328   print("name \"%s\"", name);
 329 
 330   for (int i = 0; i < intervals->length(); i++) {
 331     if (intervals->at(i) != NULL) {
 332       intervals->at(i)->print(output());
 333     }
 334   }
 335 
 336   print_end("intervals");
 337   output()->flush();
 338 }
 339 
 340 
 341 #endif