c1_CFGPrinter.cpp 9.6 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
 *
 */

#include "incls/_precompiled.incl"
#include "incls/_c1_CFGPrinter.cpp.incl"

#ifndef PRODUCT


class CFGPrinterOutput : public CHeapObj {
 private:
  outputStream* _output;

  Compilation*  _compilation;
  bool _do_print_HIR;
  bool _do_print_LIR;

  class PrintBlockClosure: public BlockClosure {
    void block_do(BlockBegin* block) { if (block != NULL) CFGPrinter::output()->print_block(block); }
  };


  outputStream* output() { assert(_output != NULL, ""); return _output; }

  void inc_indent();
  void dec_indent();
  void print(const char* format, ...);
  void print_begin(const char* tag);
  void print_end(const char* tag);

  char* method_name(ciMethod* method, bool short_name = false);

 public:
  CFGPrinterOutput();

  void set_compilation(Compilation* compilation) { _compilation = compilation; }
  void set_print_flags(bool do_print_HIR, bool do_print_LIR) { _do_print_HIR = do_print_HIR; _do_print_LIR = do_print_LIR; }

  void print_compilation();
  void print_intervals(IntervalList* intervals, const char* name);

  void print_state(BlockBegin* block);
  void print_operand(Value instr);
  void print_HIR(Value instr);
  void print_HIR(BlockBegin* block);
  void print_LIR(BlockBegin* block);
  void print_block(BlockBegin* block);
  void print_cfg(BlockList* blocks, const char* name);
  void print_cfg(IR* blocks, const char* name);
};

CFGPrinterOutput* CFGPrinter::_output = NULL;




void CFGPrinter::print_compilation(Compilation* compilation) {
  if (_output == NULL) {
    _output = new CFGPrinterOutput();
  }
  output()->set_compilation(compilation);
  output()->print_compilation();
}

void CFGPrinter::print_cfg(BlockList* blocks, const char* name, bool do_print_HIR, bool do_print_LIR) {
  output()->set_print_flags(do_print_HIR, do_print_LIR);
  output()->print_cfg(blocks, name);
}

void CFGPrinter::print_cfg(IR* blocks, const char* name, bool do_print_HIR, bool do_print_LIR) {
  output()->set_print_flags(do_print_HIR, do_print_LIR);
  output()->print_cfg(blocks, name);
}


void CFGPrinter::print_intervals(IntervalList* intervals, const char* name) {
  output()->print_intervals(intervals, name);
}



CFGPrinterOutput::CFGPrinterOutput()
 : _output(new(ResourceObj::C_HEAP) fileStream("output.cfg"))
{
}



void CFGPrinterOutput::inc_indent() {
  output()->inc();
  output()->inc();
}

void CFGPrinterOutput::dec_indent() {
  output()->dec();
  output()->dec();
}

void CFGPrinterOutput::print(const char* format, ...) {
  output()->indent();

  va_list ap;
  va_start(ap, format);
  output()->vprint_cr(format, ap);
  va_end(ap);
}

void CFGPrinterOutput::print_begin(const char* tag) {
  output()->indent();
  output()->print_cr("begin_%s", tag);
  inc_indent();
}

void CFGPrinterOutput::print_end(const char* tag) {
  dec_indent();
  output()->indent();
  output()->print_cr("end_%s", tag);
}


char* CFGPrinterOutput::method_name(ciMethod* method, bool short_name) {
  stringStream name;
  if (short_name) {
    method->print_short_name(&name);
  } else {
    method->print_name(&name);
  }
  return name.as_string();

}


void CFGPrinterOutput::print_compilation() {
  print_begin("compilation");

  print("name \"%s\"", method_name(_compilation->method(), true));
  print("method \"%s\"", method_name(_compilation->method()));
  print("date "INT64_FORMAT, os::javaTimeMillis());

  print_end("compilation");
}





void CFGPrinterOutput::print_state(BlockBegin* block) {
  print_begin("states");

  InstructionPrinter ip(true, output());

  ValueStack* state = block->state();
  int index;
  Value value;

  for_each_state(state) {
    print_begin("locals");
    print("size %d", state->locals_size());
    print("method \"%s\"", method_name(state->scope()->method()));

    for_each_local_value(state, index, value) {
      ip.print_phi(index, value, block);
      print_operand(value);
      output()->cr();
    }
    print_end("locals");
R
roland 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

    if (state->stack_size() > 0) {
      print_begin("stack");
      print("size %d", state->stack_size());
      print("method \"%s\"", method_name(state->scope()->method()));

      for_each_stack_value(state, index, value) {
        ip.print_phi(index, value, block);
        print_operand(value);
        output()->cr();
      }

      print_end("stack");
    }

    if (state->locks_size() > 0) {
      print_begin("locks");
      print("size %d", state->locks_size());
      print("method \"%s\"", method_name(state->scope()->method()));

      for_each_lock_value(state, index, value) {
        ip.print_phi(index, value, block);
        print_operand(value);
        output()->cr();
      }
      print_end("locks");
    }
D
duke 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  }

  print_end("states");
}


void CFGPrinterOutput::print_operand(Value instr) {
  if (instr->operand()->is_virtual()) {
    output()->print(" \"");
    instr->operand()->print(output());
    output()->print("\" ");
  }
}

void CFGPrinterOutput::print_HIR(Value instr) {
  InstructionPrinter ip(true, output());

  if (instr->is_pinned()) {
    output()->put('.');
  }
R
roland 已提交
235 236

  output()->print("%d %d ", instr->printable_bci(), instr->use_count());
D
duke 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

  print_operand(instr);

  ip.print_temp(instr);
  output()->print(" ");
  ip.print_instr(instr);

  output()->print_cr(" <|@");
}

void CFGPrinterOutput::print_HIR(BlockBegin* block) {
  print_begin("HIR");

  Value cur = block->next();
  while (cur != NULL) {
    print_HIR(cur);
    cur = cur->next();
  }

  print_end("HIR");
}

void CFGPrinterOutput::print_LIR(BlockBegin* block) {
  print_begin("LIR");

  for (int i = 0; i < block->lir()->length(); i++) {
    block->lir()->at(i)->print_on(output());
    output()->print_cr(" <|@ ");
  }

  print_end("LIR");
}


void CFGPrinterOutput::print_block(BlockBegin* block) {
  print_begin("block");

  print("name \"B%d\"", block->block_id());

  print("from_bci %d", block->bci());
R
roland 已提交
277
  print("to_bci %d", (block->end() == NULL ? -1 : block->end()->printable_bci()));
D
duke 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

  output()->indent();
  output()->print("predecessors ");
  int i;
  for (i = 0; i < block->number_of_preds(); i++) {
    output()->print("\"B%d\" ", block->pred_at(i)->block_id());
  }
  output()->cr();

  output()->indent();
  output()->print("successors ");
  for (i = 0; i < block->number_of_sux(); i++) {
    output()->print("\"B%d\" ", block->sux_at(i)->block_id());
  }
  output()->cr();

  output()->indent();
  output()->print("xhandlers");
  for (i = 0; i < block->number_of_exception_handlers(); i++) {
    output()->print("\"B%d\" ", block->exception_handler_at(i)->block_id());
  }
  output()->cr();

  output()->indent();
  output()->print("flags ");
  if (block->is_set(BlockBegin::std_entry_flag))                output()->print("\"std\" ");
  if (block->is_set(BlockBegin::osr_entry_flag))                output()->print("\"osr\" ");
  if (block->is_set(BlockBegin::exception_entry_flag))          output()->print("\"ex\" ");
  if (block->is_set(BlockBegin::subroutine_entry_flag))         output()->print("\"sr\" ");
  if (block->is_set(BlockBegin::backward_branch_target_flag))   output()->print("\"bb\" ");
  if (block->is_set(BlockBegin::parser_loop_header_flag))       output()->print("\"plh\" ");
  if (block->is_set(BlockBegin::critical_edge_split_flag))      output()->print("\"ces\" ");
  if (block->is_set(BlockBegin::linear_scan_loop_header_flag))  output()->print("\"llh\" ");
  if (block->is_set(BlockBegin::linear_scan_loop_end_flag))     output()->print("\"lle\" ");
  output()->cr();

  if (block->dominator() != NULL) {
    print("dominator \"B%d\"", block->dominator()->block_id());
  }
  if (block->loop_index() != -1) {
    print("loop_index %d", block->loop_index());
    print("loop_depth %d", block->loop_depth());
  }

  if (block->first_lir_instruction_id() != -1) {
    print("first_lir_id %d", block->first_lir_instruction_id());
    print("last_lir_id %d", block->last_lir_instruction_id());
  }

  if (_do_print_HIR) {
    print_state(block);
    print_HIR(block);
  }

  if (_do_print_LIR) {
    print_LIR(block);
  }

  print_end("block");
}



void CFGPrinterOutput::print_cfg(BlockList* blocks, const char* name) {
  print_begin("cfg");
  print("name \"%s\"", name);

  PrintBlockClosure print_block;
  blocks->iterate_forward(&print_block);

  print_end("cfg");
  output()->flush();
}

void CFGPrinterOutput::print_cfg(IR* blocks, const char* name) {
  print_begin("cfg");
  print("name \"%s\"", name);

  PrintBlockClosure print_block;
  blocks->iterate_preorder(&print_block);

  print_end("cfg");
  output()->flush();
}




void CFGPrinterOutput::print_intervals(IntervalList* intervals, const char* name) {
  print_begin("intervals");
  print("name \"%s\"", name);

  for (int i = 0; i < intervals->length(); i++) {
    if (intervals->at(i) != NULL) {
      intervals->at(i)->print(output());
    }
  }

  print_end("intervals");
  output()->flush();
}


#endif