1 /* 2 * Copyright (c) 1999, 2016, 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_Canonicalizer.hpp" 28 #include "c1/c1_Compilation.hpp" 29 #include "c1/c1_GraphBuilder.hpp" 30 #include "c1/c1_InstructionPrinter.hpp" 31 #include "ci/ciCallSite.hpp" 32 #include "ci/ciField.hpp" 33 #include "ci/ciKlass.hpp" 34 #include "ci/ciMemberName.hpp" 35 #include "compiler/compileBroker.hpp" 36 #include "interpreter/bytecode.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "runtime/sharedRuntime.hpp" 40 #include "runtime/compilationPolicy.hpp" 41 #include "runtime/vm_version.hpp" 42 #include "utilities/bitMap.inline.hpp" 43 44 class BlockListBuilder VALUE_OBJ_CLASS_SPEC { 45 private: 46 Compilation* _compilation; 47 IRScope* _scope; 48 49 BlockList _blocks; // internal list of all blocks 50 BlockList* _bci2block; // mapping from bci to blocks for GraphBuilder 51 52 // fields used by mark_loops 53 BitMap _active; // for iteration of control flow graph 54 BitMap _visited; // for iteration of control flow graph 55 intArray _loop_map; // caches the information if a block is contained in a loop 56 int _next_loop_index; // next free loop number 57 int _next_block_number; // for reverse postorder numbering of blocks 58 59 // accessors 60 Compilation* compilation() const { return _compilation; } 61 IRScope* scope() const { return _scope; } 62 ciMethod* method() const { return scope()->method(); } 63 XHandlers* xhandlers() const { return scope()->xhandlers(); } 64 65 // unified bailout support 66 void bailout(const char* msg) const { compilation()->bailout(msg); } 67 bool bailed_out() const { return compilation()->bailed_out(); } 68 69 // helper functions 70 BlockBegin* make_block_at(int bci, BlockBegin* predecessor); 71 void handle_exceptions(BlockBegin* current, int cur_bci); 72 void handle_jsr(BlockBegin* current, int sr_bci, int next_bci); 73 void store_one(BlockBegin* current, int local); 74 void store_two(BlockBegin* current, int local); 75 void set_entries(int osr_bci); 76 void set_leaders(); 77 78 void make_loop_header(BlockBegin* block); 79 void mark_loops(); 80 int mark_loops(BlockBegin* b, bool in_subroutine); 81 82 // debugging 83 #ifndef PRODUCT 84 void print(); 85 #endif 86 87 public: 88 // creation 89 BlockListBuilder(Compilation* compilation, IRScope* scope, int osr_bci); 90 91 // accessors for GraphBuilder 92 BlockList* bci2block() const { return _bci2block; } 93 }; 94 95 96 // Implementation of BlockListBuilder 97 98 BlockListBuilder::BlockListBuilder(Compilation* compilation, IRScope* scope, int osr_bci) 99 : _compilation(compilation) 100 , _scope(scope) 101 , _blocks(16) 102 , _bci2block(new BlockList(scope->method()->code_size(), NULL)) 103 , _next_block_number(0) 104 , _active() // size not known yet 105 , _visited() // size not known yet 106 , _next_loop_index(0) 107 , _loop_map() // size not known yet 108 { 109 set_entries(osr_bci); 110 set_leaders(); 111 CHECK_BAILOUT(); 112 113 mark_loops(); 114 NOT_PRODUCT(if (PrintInitialBlockList) print()); 115 116 #ifndef PRODUCT 117 if (PrintCFGToFile) { 118 stringStream title; 119 title.print("BlockListBuilder "); 120 scope->method()->print_name(&title); 121 CFGPrinter::print_cfg(_bci2block, title.as_string(), false, false); 122 } 123 #endif 124 } 125 126 127 void BlockListBuilder::set_entries(int osr_bci) { 128 // generate start blocks 129 BlockBegin* std_entry = make_block_at(0, NULL); 130 if (scope()->caller() == NULL) { 131 std_entry->set(BlockBegin::std_entry_flag); 132 } 133 if (osr_bci != -1) { 134 BlockBegin* osr_entry = make_block_at(osr_bci, NULL); 135 osr_entry->set(BlockBegin::osr_entry_flag); 136 } 137 138 // generate exception entry blocks 139 XHandlers* list = xhandlers(); 140 const int n = list->length(); 141 for (int i = 0; i < n; i++) { 142 XHandler* h = list->handler_at(i); 143 BlockBegin* entry = make_block_at(h->handler_bci(), NULL); 144 entry->set(BlockBegin::exception_entry_flag); 145 h->set_entry_block(entry); 146 } 147 } 148 149 150 BlockBegin* BlockListBuilder::make_block_at(int cur_bci, BlockBegin* predecessor) { 151 assert(method()->bci_block_start().at(cur_bci), "wrong block starts of MethodLivenessAnalyzer"); 152 153 BlockBegin* block = _bci2block->at(cur_bci); 154 if (block == NULL) { 155 block = new BlockBegin(cur_bci); 156 block->init_stores_to_locals(method()->max_locals()); 157 _bci2block->at_put(cur_bci, block); 158 _blocks.append(block); 159 160 assert(predecessor == NULL || predecessor->bci() < cur_bci, "targets for backward branches must already exist"); 161 } 162 163 if (predecessor != NULL) { 164 if (block->is_set(BlockBegin::exception_entry_flag)) { 165 BAILOUT_("Exception handler can be reached by both normal and exceptional control flow", block); 166 } 167 168 predecessor->add_successor(block); 169 block->increment_total_preds(); 170 } 171 172 return block; 173 } 174 175 176 inline void BlockListBuilder::store_one(BlockBegin* current, int local) { 177 current->stores_to_locals().set_bit(local); 178 } 179 inline void BlockListBuilder::store_two(BlockBegin* current, int local) { 180 store_one(current, local); 181 store_one(current, local + 1); 182 } 183 184 185 void BlockListBuilder::handle_exceptions(BlockBegin* current, int cur_bci) { 186 // Draws edges from a block to its exception handlers 187 XHandlers* list = xhandlers(); 188 const int n = list->length(); 189 190 for (int i = 0; i < n; i++) { 191 XHandler* h = list->handler_at(i); 192 193 if (h->covers(cur_bci)) { 194 BlockBegin* entry = h->entry_block(); 195 assert(entry != NULL && entry == _bci2block->at(h->handler_bci()), "entry must be set"); 196 assert(entry->is_set(BlockBegin::exception_entry_flag), "flag must be set"); 197 198 // add each exception handler only once 199 if (!current->is_successor(entry)) { 200 current->add_successor(entry); 201 entry->increment_total_preds(); 202 } 203 204 // stop when reaching catchall 205 if (h->catch_type() == 0) break; 206 } 207 } 208 } 209 210 void BlockListBuilder::handle_jsr(BlockBegin* current, int sr_bci, int next_bci) { 211 // start a new block after jsr-bytecode and link this block into cfg 212 make_block_at(next_bci, current); 213 214 // start a new block at the subroutine entry at mark it with special flag 215 BlockBegin* sr_block = make_block_at(sr_bci, current); 216 if (!sr_block->is_set(BlockBegin::subroutine_entry_flag)) { 217 sr_block->set(BlockBegin::subroutine_entry_flag); 218 } 219 } 220 221 222 void BlockListBuilder::set_leaders() { 223 bool has_xhandlers = xhandlers()->has_handlers(); 224 BlockBegin* current = NULL; 225 226 // The information which bci starts a new block simplifies the analysis 227 // Without it, backward branches could jump to a bci where no block was created 228 // during bytecode iteration. This would require the creation of a new block at the 229 // branch target and a modification of the successor lists. 230 BitMap bci_block_start = method()->bci_block_start(); 231 232 ciBytecodeStream s(method()); 233 while (s.next() != ciBytecodeStream::EOBC()) { 234 int cur_bci = s.cur_bci(); 235 236 if (bci_block_start.at(cur_bci)) { 237 current = make_block_at(cur_bci, current); 238 } 239 assert(current != NULL, "must have current block"); 240 241 if (has_xhandlers && GraphBuilder::can_trap(method(), s.cur_bc())) { 242 handle_exceptions(current, cur_bci); 243 } 244 245 switch (s.cur_bc()) { 246 // track stores to local variables for selective creation of phi functions 247 case Bytecodes::_iinc: store_one(current, s.get_index()); break; 248 case Bytecodes::_istore: store_one(current, s.get_index()); break; 249 case Bytecodes::_lstore: store_two(current, s.get_index()); break; 250 case Bytecodes::_fstore: store_one(current, s.get_index()); break; 251 case Bytecodes::_dstore: store_two(current, s.get_index()); break; 252 case Bytecodes::_astore: store_one(current, s.get_index()); break; 253 case Bytecodes::_istore_0: store_one(current, 0); break; 254 case Bytecodes::_istore_1: store_one(current, 1); break; 255 case Bytecodes::_istore_2: store_one(current, 2); break; 256 case Bytecodes::_istore_3: store_one(current, 3); break; 257 case Bytecodes::_lstore_0: store_two(current, 0); break; 258 case Bytecodes::_lstore_1: store_two(current, 1); break; 259 case Bytecodes::_lstore_2: store_two(current, 2); break; 260 case Bytecodes::_lstore_3: store_two(current, 3); break; 261 case Bytecodes::_fstore_0: store_one(current, 0); break; 262 case Bytecodes::_fstore_1: store_one(current, 1); break; 263 case Bytecodes::_fstore_2: store_one(current, 2); break; 264 case Bytecodes::_fstore_3: store_one(current, 3); break; 265 case Bytecodes::_dstore_0: store_two(current, 0); break; 266 case Bytecodes::_dstore_1: store_two(current, 1); break; 267 case Bytecodes::_dstore_2: store_two(current, 2); break; 268 case Bytecodes::_dstore_3: store_two(current, 3); break; 269 case Bytecodes::_astore_0: store_one(current, 0); break; 270 case Bytecodes::_astore_1: store_one(current, 1); break; 271 case Bytecodes::_astore_2: store_one(current, 2); break; 272 case Bytecodes::_astore_3: store_one(current, 3); break; 273 274 // track bytecodes that affect the control flow 275 case Bytecodes::_athrow: // fall through 276 case Bytecodes::_ret: // fall through 277 case Bytecodes::_ireturn: // fall through 278 case Bytecodes::_lreturn: // fall through 279 case Bytecodes::_freturn: // fall through 280 case Bytecodes::_dreturn: // fall through 281 case Bytecodes::_areturn: // fall through 282 case Bytecodes::_return: 283 current = NULL; 284 break; 285 286 case Bytecodes::_ifeq: // fall through 287 case Bytecodes::_ifne: // fall through 288 case Bytecodes::_iflt: // fall through 289 case Bytecodes::_ifge: // fall through 290 case Bytecodes::_ifgt: // fall through 291 case Bytecodes::_ifle: // fall through 292 case Bytecodes::_if_icmpeq: // fall through 293 case Bytecodes::_if_icmpne: // fall through 294 case Bytecodes::_if_icmplt: // fall through 295 case Bytecodes::_if_icmpge: // fall through 296 case Bytecodes::_if_icmpgt: // fall through 297 case Bytecodes::_if_icmple: // fall through 298 case Bytecodes::_if_acmpeq: // fall through 299 case Bytecodes::_if_acmpne: // fall through 300 case Bytecodes::_ifnull: // fall through 301 case Bytecodes::_ifnonnull: 302 make_block_at(s.next_bci(), current); 303 make_block_at(s.get_dest(), current); 304 current = NULL; 305 break; 306 307 case Bytecodes::_goto: 308 make_block_at(s.get_dest(), current); 309 current = NULL; 310 break; 311 312 case Bytecodes::_goto_w: 313 make_block_at(s.get_far_dest(), current); 314 current = NULL; 315 break; 316 317 case Bytecodes::_jsr: 318 handle_jsr(current, s.get_dest(), s.next_bci()); 319 current = NULL; 320 break; 321 322 case Bytecodes::_jsr_w: 323 handle_jsr(current, s.get_far_dest(), s.next_bci()); 324 current = NULL; 325 break; 326 327 case Bytecodes::_tableswitch: { 328 // set block for each case 329 Bytecode_tableswitch sw(&s); 330 int l = sw.length(); 331 for (int i = 0; i < l; i++) { 332 make_block_at(cur_bci + sw.dest_offset_at(i), current); 333 } 334 make_block_at(cur_bci + sw.default_offset(), current); 335 current = NULL; 336 break; 337 } 338 339 case Bytecodes::_lookupswitch: { 340 // set block for each case 341 Bytecode_lookupswitch sw(&s); 342 int l = sw.number_of_pairs(); 343 for (int i = 0; i < l; i++) { 344 make_block_at(cur_bci + sw.pair_at(i).offset(), current); 345 } 346 make_block_at(cur_bci + sw.default_offset(), current); 347 current = NULL; 348 break; 349 } 350 } 351 } 352 } 353 354 355 void BlockListBuilder::mark_loops() { 356 ResourceMark rm; 357 358 _active = BitMap(BlockBegin::number_of_blocks()); _active.clear(); 359 _visited = BitMap(BlockBegin::number_of_blocks()); _visited.clear(); 360 _loop_map = intArray(BlockBegin::number_of_blocks(), BlockBegin::number_of_blocks(), 0); 361 _next_loop_index = 0; 362 _next_block_number = _blocks.length(); 363 364 // recursively iterate the control flow graph 365 mark_loops(_bci2block->at(0), false); 366 assert(_next_block_number >= 0, "invalid block numbers"); 367 } 368 369 void BlockListBuilder::make_loop_header(BlockBegin* block) { 370 if (block->is_set(BlockBegin::exception_entry_flag)) { 371 // exception edges may look like loops but don't mark them as such 372 // since it screws up block ordering. 373 return; 374 } 375 if (!block->is_set(BlockBegin::parser_loop_header_flag)) { 376 block->set(BlockBegin::parser_loop_header_flag); 377 378 assert(_loop_map.at(block->block_id()) == 0, "must not be set yet"); 379 assert(0 <= _next_loop_index && _next_loop_index < BitsPerInt, "_next_loop_index is used as a bit-index in integer"); 380 _loop_map.at_put(block->block_id(), 1 << _next_loop_index); 381 if (_next_loop_index < 31) _next_loop_index++; 382 } else { 383 // block already marked as loop header 384 assert(is_power_of_2((unsigned int)_loop_map.at(block->block_id())), "exactly one bit must be set"); 385 } 386 } 387 388 int BlockListBuilder::mark_loops(BlockBegin* block, bool in_subroutine) { 389 int block_id = block->block_id(); 390 391 if (_visited.at(block_id)) { 392 if (_active.at(block_id)) { 393 // reached block via backward branch 394 make_loop_header(block); 395 } 396 // return cached loop information for this block 397 return _loop_map.at(block_id); 398 } 399 400 if (block->is_set(BlockBegin::subroutine_entry_flag)) { 401 in_subroutine = true; 402 } 403 404 // set active and visited bits before successors are processed 405 _visited.set_bit(block_id); 406 _active.set_bit(block_id); 407 408 intptr_t loop_state = 0; 409 for (int i = block->number_of_sux() - 1; i >= 0; i--) { 410 // recursively process all successors 411 loop_state |= mark_loops(block->sux_at(i), in_subroutine); 412 } 413 414 // clear active-bit after all successors are processed 415 _active.clear_bit(block_id); 416 417 // reverse-post-order numbering of all blocks 418 block->set_depth_first_number(_next_block_number); 419 _next_block_number--; 420 421 if (loop_state != 0 || in_subroutine ) { 422 // block is contained at least in one loop, so phi functions are necessary 423 // phi functions are also necessary for all locals stored in a subroutine 424 scope()->requires_phi_function().set_union(block->stores_to_locals()); 425 } 426 427 if (block->is_set(BlockBegin::parser_loop_header_flag)) { 428 int header_loop_state = _loop_map.at(block_id); 429 assert(is_power_of_2((unsigned)header_loop_state), "exactly one bit must be set"); 430 431 // If the highest bit is set (i.e. when integer value is negative), the method 432 // has 32 or more loops. This bit is never cleared because it is used for multiple loops 433 if (header_loop_state >= 0) { 434 clear_bits(loop_state, header_loop_state); 435 } 436 } 437 438 // cache and return loop information for this block 439 _loop_map.at_put(block_id, loop_state); 440 return loop_state; 441 } 442 443 444 #ifndef PRODUCT 445 446 int compare_depth_first(BlockBegin** a, BlockBegin** b) { 447 return (*a)->depth_first_number() - (*b)->depth_first_number(); 448 } 449 450 void BlockListBuilder::print() { 451 tty->print("----- initial block list of BlockListBuilder for method "); 452 method()->print_short_name(); 453 tty->cr(); 454 455 // better readability if blocks are sorted in processing order 456 _blocks.sort(compare_depth_first); 457 458 for (int i = 0; i < _blocks.length(); i++) { 459 BlockBegin* cur = _blocks.at(i); 460 tty->print("%4d: B%-4d bci: %-4d preds: %-4d ", cur->depth_first_number(), cur->block_id(), cur->bci(), cur->total_preds()); 461 462 tty->print(cur->is_set(BlockBegin::std_entry_flag) ? " std" : " "); 463 tty->print(cur->is_set(BlockBegin::osr_entry_flag) ? " osr" : " "); 464 tty->print(cur->is_set(BlockBegin::exception_entry_flag) ? " ex" : " "); 465 tty->print(cur->is_set(BlockBegin::subroutine_entry_flag) ? " sr" : " "); 466 tty->print(cur->is_set(BlockBegin::parser_loop_header_flag) ? " lh" : " "); 467 468 if (cur->number_of_sux() > 0) { 469 tty->print(" sux: "); 470 for (int j = 0; j < cur->number_of_sux(); j++) { 471 BlockBegin* sux = cur->sux_at(j); 472 tty->print("B%d ", sux->block_id()); 473 } 474 } 475 tty->cr(); 476 } 477 } 478 479 #endif 480 481 482 // A simple growable array of Values indexed by ciFields 483 class FieldBuffer: public CompilationResourceObj { 484 private: 485 GrowableArray<Value> _values; 486 487 public: 488 FieldBuffer() {} 489 490 void kill() { 491 _values.trunc_to(0); 492 } 493 494 Value at(ciField* field) { 495 assert(field->holder()->is_loaded(), "must be a loaded field"); 496 int offset = field->offset(); 497 if (offset < _values.length()) { 498 return _values.at(offset); 499 } else { 500 return NULL; 501 } 502 } 503 504 void at_put(ciField* field, Value value) { 505 assert(field->holder()->is_loaded(), "must be a loaded field"); 506 int offset = field->offset(); 507 _values.at_put_grow(offset, value, NULL); 508 } 509 510 }; 511 512 513 // MemoryBuffer is fairly simple model of the current state of memory. 514 // It partitions memory into several pieces. The first piece is 515 // generic memory where little is known about the owner of the memory. 516 // This is conceptually represented by the tuple <O, F, V> which says 517 // that the field F of object O has value V. This is flattened so 518 // that F is represented by the offset of the field and the parallel 519 // arrays _objects and _values are used for O and V. Loads of O.F can 520 // simply use V. Newly allocated objects are kept in a separate list 521 // along with a parallel array for each object which represents the 522 // current value of its fields. Stores of the default value to fields 523 // which have never been stored to before are eliminated since they 524 // are redundant. Once newly allocated objects are stored into 525 // another object or they are passed out of the current compile they 526 // are treated like generic memory. 527 528 class MemoryBuffer: public CompilationResourceObj { 529 private: 530 FieldBuffer _values; 531 GrowableArray<Value> _objects; 532 GrowableArray<Value> _newobjects; 533 GrowableArray<FieldBuffer*> _fields; 534 535 public: 536 MemoryBuffer() {} 537 538 StoreField* store(StoreField* st) { 539 if (!EliminateFieldAccess) { 540 return st; 541 } 542 543 Value object = st->obj(); 544 Value value = st->value(); 545 ciField* field = st->field(); 546 if (field->holder()->is_loaded()) { 547 int offset = field->offset(); 548 int index = _newobjects.find(object); 549 if (index != -1) { 550 // newly allocated object with no other stores performed on this field 551 FieldBuffer* buf = _fields.at(index); 552 if (buf->at(field) == NULL && is_default_value(value)) { 553 #ifndef PRODUCT 554 if (PrintIRDuringConstruction && Verbose) { 555 tty->print_cr("Eliminated store for object %d:", index); 556 st->print_line(); 557 } 558 #endif 559 return NULL; 560 } else { 561 buf->at_put(field, value); 562 } 563 } else { 564 _objects.at_put_grow(offset, object, NULL); 565 _values.at_put(field, value); 566 } 567 568 store_value(value); 569 } else { 570 // if we held onto field names we could alias based on names but 571 // we don't know what's being stored to so kill it all. 572 kill(); 573 } 574 return st; 575 } 576 577 578 // return true if this value correspond to the default value of a field. 579 bool is_default_value(Value value) { 580 Constant* con = value->as_Constant(); 581 if (con) { 582 switch (con->type()->tag()) { 583 case intTag: return con->type()->as_IntConstant()->value() == 0; 584 case longTag: return con->type()->as_LongConstant()->value() == 0; 585 case floatTag: return jint_cast(con->type()->as_FloatConstant()->value()) == 0; 586 case doubleTag: return jlong_cast(con->type()->as_DoubleConstant()->value()) == jlong_cast(0); 587 case objectTag: return con->type() == objectNull; 588 default: ShouldNotReachHere(); 589 } 590 } 591 return false; 592 } 593 594 595 // return either the actual value of a load or the load itself 596 Value load(LoadField* load) { 597 if (!EliminateFieldAccess) { 598 return load; 599 } 600 601 if (RoundFPResults && UseSSE < 2 && load->type()->is_float_kind()) { 602 // can't skip load since value might get rounded as a side effect 603 return load; 604 } 605 606 ciField* field = load->field(); 607 Value object = load->obj(); 608 if (field->holder()->is_loaded() && !field->is_volatile()) { 609 int offset = field->offset(); 610 Value result = NULL; 611 int index = _newobjects.find(object); 612 if (index != -1) { 613 result = _fields.at(index)->at(field); 614 } else if (_objects.at_grow(offset, NULL) == object) { 615 result = _values.at(field); 616 } 617 if (result != NULL) { 618 #ifndef PRODUCT 619 if (PrintIRDuringConstruction && Verbose) { 620 tty->print_cr("Eliminated load: "); 621 load->print_line(); 622 } 623 #endif 624 assert(result->type()->tag() == load->type()->tag(), "wrong types"); 625 return result; 626 } 627 } 628 return load; 629 } 630 631 // Record this newly allocated object 632 void new_instance(NewInstance* object) { 633 int index = _newobjects.length(); 634 _newobjects.append(object); 635 if (_fields.at_grow(index, NULL) == NULL) { 636 _fields.at_put(index, new FieldBuffer()); 637 } else { 638 _fields.at(index)->kill(); 639 } 640 } 641 642 void store_value(Value value) { 643 int index = _newobjects.find(value); 644 if (index != -1) { 645 // stored a newly allocated object into another object. 646 // Assume we've lost track of it as separate slice of memory. 647 // We could do better by keeping track of whether individual 648 // fields could alias each other. 649 _newobjects.remove_at(index); 650 // pull out the field info and store it at the end up the list 651 // of field info list to be reused later. 652 _fields.append(_fields.at(index)); 653 _fields.remove_at(index); 654 } 655 } 656 657 void kill() { 658 _newobjects.trunc_to(0); 659 _objects.trunc_to(0); 660 _values.kill(); 661 } 662 }; 663 664 665 // Implementation of GraphBuilder's ScopeData 666 667 GraphBuilder::ScopeData::ScopeData(ScopeData* parent) 668 : _parent(parent) 669 , _bci2block(NULL) 670 , _scope(NULL) 671 , _has_handler(false) 672 , _stream(NULL) 673 , _work_list(NULL) 674 , _parsing_jsr(false) 675 , _jsr_xhandlers(NULL) 676 , _caller_stack_size(-1) 677 , _continuation(NULL) 678 , _num_returns(0) 679 , _cleanup_block(NULL) 680 , _cleanup_return_prev(NULL) 681 , _cleanup_state(NULL) 682 { 683 if (parent != NULL) { 684 _max_inline_size = (intx) ((float) NestedInliningSizeRatio * (float) parent->max_inline_size() / 100.0f); 685 } else { 686 _max_inline_size = MaxInlineSize; 687 } 688 if (_max_inline_size < MaxTrivialSize) { 689 _max_inline_size = MaxTrivialSize; 690 } 691 } 692 693 694 void GraphBuilder::kill_all() { 695 if (UseLocalValueNumbering) { 696 vmap()->kill_all(); 697 } 698 _memory->kill(); 699 } 700 701 702 BlockBegin* GraphBuilder::ScopeData::block_at(int bci) { 703 if (parsing_jsr()) { 704 // It is necessary to clone all blocks associated with a 705 // subroutine, including those for exception handlers in the scope 706 // of the method containing the jsr (because those exception 707 // handlers may contain ret instructions in some cases). 708 BlockBegin* block = bci2block()->at(bci); 709 if (block != NULL && block == parent()->bci2block()->at(bci)) { 710 BlockBegin* new_block = new BlockBegin(block->bci()); 711 if (PrintInitialBlockList) { 712 tty->print_cr("CFG: cloned block %d (bci %d) as block %d for jsr", 713 block->block_id(), block->bci(), new_block->block_id()); 714 } 715 // copy data from cloned blocked 716 new_block->set_depth_first_number(block->depth_first_number()); 717 if (block->is_set(BlockBegin::parser_loop_header_flag)) new_block->set(BlockBegin::parser_loop_header_flag); 718 // Preserve certain flags for assertion checking 719 if (block->is_set(BlockBegin::subroutine_entry_flag)) new_block->set(BlockBegin::subroutine_entry_flag); 720 if (block->is_set(BlockBegin::exception_entry_flag)) new_block->set(BlockBegin::exception_entry_flag); 721 722 // copy was_visited_flag to allow early detection of bailouts 723 // if a block that is used in a jsr has already been visited before, 724 // it is shared between the normal control flow and a subroutine 725 // BlockBegin::try_merge returns false when the flag is set, this leads 726 // to a compilation bailout 727 if (block->is_set(BlockBegin::was_visited_flag)) new_block->set(BlockBegin::was_visited_flag); 728 729 bci2block()->at_put(bci, new_block); 730 block = new_block; 731 } 732 return block; 733 } else { 734 return bci2block()->at(bci); 735 } 736 } 737 738 739 XHandlers* GraphBuilder::ScopeData::xhandlers() const { 740 if (_jsr_xhandlers == NULL) { 741 assert(!parsing_jsr(), ""); 742 return scope()->xhandlers(); 743 } 744 assert(parsing_jsr(), ""); 745 return _jsr_xhandlers; 746 } 747 748 749 void GraphBuilder::ScopeData::set_scope(IRScope* scope) { 750 _scope = scope; 751 bool parent_has_handler = false; 752 if (parent() != NULL) { 753 parent_has_handler = parent()->has_handler(); 754 } 755 _has_handler = parent_has_handler || scope->xhandlers()->has_handlers(); 756 } 757 758 759 void GraphBuilder::ScopeData::set_inline_cleanup_info(BlockBegin* block, 760 Instruction* return_prev, 761 ValueStack* return_state) { 762 _cleanup_block = block; 763 _cleanup_return_prev = return_prev; 764 _cleanup_state = return_state; 765 } 766 767 768 void GraphBuilder::ScopeData::add_to_work_list(BlockBegin* block) { 769 if (_work_list == NULL) { 770 _work_list = new BlockList(); 771 } 772 773 if (!block->is_set(BlockBegin::is_on_work_list_flag)) { 774 // Do not start parsing the continuation block while in a 775 // sub-scope 776 if (parsing_jsr()) { 777 if (block == jsr_continuation()) { 778 return; 779 } 780 } else { 781 if (block == continuation()) { 782 return; 783 } 784 } 785 block->set(BlockBegin::is_on_work_list_flag); 786 _work_list->push(block); 787 788 sort_top_into_worklist(_work_list, block); 789 } 790 } 791 792 793 void GraphBuilder::sort_top_into_worklist(BlockList* worklist, BlockBegin* top) { 794 assert(worklist->top() == top, ""); 795 // sort block descending into work list 796 const int dfn = top->depth_first_number(); 797 assert(dfn != -1, "unknown depth first number"); 798 int i = worklist->length()-2; 799 while (i >= 0) { 800 BlockBegin* b = worklist->at(i); 801 if (b->depth_first_number() < dfn) { 802 worklist->at_put(i+1, b); 803 } else { 804 break; 805 } 806 i --; 807 } 808 if (i >= -1) worklist->at_put(i + 1, top); 809 } 810 811 812 BlockBegin* GraphBuilder::ScopeData::remove_from_work_list() { 813 if (is_work_list_empty()) { 814 return NULL; 815 } 816 return _work_list->pop(); 817 } 818 819 820 bool GraphBuilder::ScopeData::is_work_list_empty() const { 821 return (_work_list == NULL || _work_list->length() == 0); 822 } 823 824 825 void GraphBuilder::ScopeData::setup_jsr_xhandlers() { 826 assert(parsing_jsr(), ""); 827 // clone all the exception handlers from the scope 828 XHandlers* handlers = new XHandlers(scope()->xhandlers()); 829 const int n = handlers->length(); 830 for (int i = 0; i < n; i++) { 831 // The XHandlers need to be adjusted to dispatch to the cloned 832 // handler block instead of the default one but the synthetic 833 // unlocker needs to be handled specially. The synthetic unlocker 834 // should be left alone since there can be only one and all code 835 // should dispatch to the same one. 836 XHandler* h = handlers->handler_at(i); 837 assert(h->handler_bci() != SynchronizationEntryBCI, "must be real"); 838 h->set_entry_block(block_at(h->handler_bci())); 839 } 840 _jsr_xhandlers = handlers; 841 } 842 843 844 int GraphBuilder::ScopeData::num_returns() { 845 if (parsing_jsr()) { 846 return parent()->num_returns(); 847 } 848 return _num_returns; 849 } 850 851 852 void GraphBuilder::ScopeData::incr_num_returns() { 853 if (parsing_jsr()) { 854 parent()->incr_num_returns(); 855 } else { 856 ++_num_returns; 857 } 858 } 859 860 861 // Implementation of GraphBuilder 862 863 #define INLINE_BAILOUT(msg) { inline_bailout(msg); return false; } 864 865 866 void GraphBuilder::load_constant() { 867 ciConstant con = stream()->get_constant(); 868 if (con.basic_type() == T_ILLEGAL) { 869 BAILOUT("could not resolve a constant"); 870 } else { 871 ValueType* t = illegalType; 872 ValueStack* patch_state = NULL; 873 switch (con.basic_type()) { 874 case T_BOOLEAN: t = new IntConstant (con.as_boolean()); break; 875 case T_BYTE : t = new IntConstant (con.as_byte ()); break; 876 case T_CHAR : t = new IntConstant (con.as_char ()); break; 877 case T_SHORT : t = new IntConstant (con.as_short ()); break; 878 case T_INT : t = new IntConstant (con.as_int ()); break; 879 case T_LONG : t = new LongConstant (con.as_long ()); break; 880 case T_FLOAT : t = new FloatConstant (con.as_float ()); break; 881 case T_DOUBLE : t = new DoubleConstant (con.as_double ()); break; 882 case T_ARRAY : t = new ArrayConstant (con.as_object ()->as_array ()); break; 883 case T_OBJECT : 884 { 885 ciObject* obj = con.as_object(); 886 if (!obj->is_loaded() 887 || (PatchALot && obj->klass() != ciEnv::current()->String_klass())) { 888 patch_state = copy_state_before(); 889 t = new ObjectConstant(obj); 890 } else { 891 assert(obj->is_instance(), "must be java_mirror of klass"); 892 t = new InstanceConstant(obj->as_instance()); 893 } 894 break; 895 } 896 default : ShouldNotReachHere(); 897 } 898 Value x; 899 if (patch_state != NULL) { 900 x = new Constant(t, patch_state); 901 } else { 902 x = new Constant(t); 903 } 904 push(t, append(x)); 905 } 906 } 907 908 909 void GraphBuilder::load_local(ValueType* type, int index) { 910 Value x = state()->local_at(index); 911 assert(x != NULL && !x->type()->is_illegal(), "access of illegal local variable"); 912 push(type, x); 913 } 914 915 916 void GraphBuilder::store_local(ValueType* type, int index) { 917 Value x = pop(type); 918 store_local(state(), x, index); 919 } 920 921 922 void GraphBuilder::store_local(ValueStack* state, Value x, int index) { 923 if (parsing_jsr()) { 924 // We need to do additional tracking of the location of the return 925 // address for jsrs since we don't handle arbitrary jsr/ret 926 // constructs. Here we are figuring out in which circumstances we 927 // need to bail out. 928 if (x->type()->is_address()) { 929 scope_data()->set_jsr_return_address_local(index); 930 931 // Also check parent jsrs (if any) at this time to see whether 932 // they are using this local. We don't handle skipping over a 933 // ret. 934 for (ScopeData* cur_scope_data = scope_data()->parent(); 935 cur_scope_data != NULL && cur_scope_data->parsing_jsr() && cur_scope_data->scope() == scope(); 936 cur_scope_data = cur_scope_data->parent()) { 937 if (cur_scope_data->jsr_return_address_local() == index) { 938 BAILOUT("subroutine overwrites return address from previous subroutine"); 939 } 940 } 941 } else if (index == scope_data()->jsr_return_address_local()) { 942 scope_data()->set_jsr_return_address_local(-1); 943 } 944 } 945 946 state->store_local(index, round_fp(x)); 947 } 948 949 950 void GraphBuilder::load_indexed(BasicType type) { 951 // In case of in block code motion in range check elimination 952 ValueStack* state_before = copy_state_indexed_access(); 953 compilation()->set_has_access_indexed(true); 954 Value index = ipop(); 955 Value array = apop(); 956 Value length = NULL; 957 if (CSEArrayLength || 958 (array->as_AccessField() && array->as_AccessField()->field()->is_constant()) || 959 (array->as_NewArray() && array->as_NewArray()->length() && array->as_NewArray()->length()->type()->is_constant())) { 960 length = append(new ArrayLength(array, state_before)); 961 } 962 push(as_ValueType(type), append(new LoadIndexed(array, index, length, type, state_before))); 963 } 964 965 966 void GraphBuilder::store_indexed(BasicType type) { 967 // In case of in block code motion in range check elimination 968 ValueStack* state_before = copy_state_indexed_access(); 969 compilation()->set_has_access_indexed(true); 970 Value value = pop(as_ValueType(type)); 971 Value index = ipop(); 972 Value array = apop(); 973 Value length = NULL; 974 if (CSEArrayLength || 975 (array->as_AccessField() && array->as_AccessField()->field()->is_constant()) || 976 (array->as_NewArray() && array->as_NewArray()->length() && array->as_NewArray()->length()->type()->is_constant())) { 977 length = append(new ArrayLength(array, state_before)); 978 } 979 ciType* array_type = array->declared_type(); 980 bool check_boolean = false; 981 if (array_type != NULL) { 982 if (array_type->is_loaded() && 983 array_type->as_array_klass()->element_type()->basic_type() == T_BOOLEAN) { 984 assert(type == T_BYTE, "boolean store uses bastore"); 985 Value mask = append(new Constant(new IntConstant(1))); 986 value = append(new LogicOp(Bytecodes::_iand, value, mask)); 987 } 988 } else if (type == T_BYTE) { 989 check_boolean = true; 990 } 991 StoreIndexed* result = new StoreIndexed(array, index, length, type, value, state_before, check_boolean); 992 append(result); 993 _memory->store_value(value); 994 995 if (type == T_OBJECT && is_profiling()) { 996 // Note that we'd collect profile data in this method if we wanted it. 997 compilation()->set_would_profile(true); 998 999 if (profile_checkcasts()) { 1000 result->set_profiled_method(method()); 1001 result->set_profiled_bci(bci()); 1002 result->set_should_profile(true); 1003 } 1004 } 1005 } 1006 1007 1008 void GraphBuilder::stack_op(Bytecodes::Code code) { 1009 switch (code) { 1010 case Bytecodes::_pop: 1011 { state()->raw_pop(); 1012 } 1013 break; 1014 case Bytecodes::_pop2: 1015 { state()->raw_pop(); 1016 state()->raw_pop(); 1017 } 1018 break; 1019 case Bytecodes::_dup: 1020 { Value w = state()->raw_pop(); 1021 state()->raw_push(w); 1022 state()->raw_push(w); 1023 } 1024 break; 1025 case Bytecodes::_dup_x1: 1026 { Value w1 = state()->raw_pop(); 1027 Value w2 = state()->raw_pop(); 1028 state()->raw_push(w1); 1029 state()->raw_push(w2); 1030 state()->raw_push(w1); 1031 } 1032 break; 1033 case Bytecodes::_dup_x2: 1034 { Value w1 = state()->raw_pop(); 1035 Value w2 = state()->raw_pop(); 1036 Value w3 = state()->raw_pop(); 1037 state()->raw_push(w1); 1038 state()->raw_push(w3); 1039 state()->raw_push(w2); 1040 state()->raw_push(w1); 1041 } 1042 break; 1043 case Bytecodes::_dup2: 1044 { Value w1 = state()->raw_pop(); 1045 Value w2 = state()->raw_pop(); 1046 state()->raw_push(w2); 1047 state()->raw_push(w1); 1048 state()->raw_push(w2); 1049 state()->raw_push(w1); 1050 } 1051 break; 1052 case Bytecodes::_dup2_x1: 1053 { Value w1 = state()->raw_pop(); 1054 Value w2 = state()->raw_pop(); 1055 Value w3 = state()->raw_pop(); 1056 state()->raw_push(w2); 1057 state()->raw_push(w1); 1058 state()->raw_push(w3); 1059 state()->raw_push(w2); 1060 state()->raw_push(w1); 1061 } 1062 break; 1063 case Bytecodes::_dup2_x2: 1064 { Value w1 = state()->raw_pop(); 1065 Value w2 = state()->raw_pop(); 1066 Value w3 = state()->raw_pop(); 1067 Value w4 = state()->raw_pop(); 1068 state()->raw_push(w2); 1069 state()->raw_push(w1); 1070 state()->raw_push(w4); 1071 state()->raw_push(w3); 1072 state()->raw_push(w2); 1073 state()->raw_push(w1); 1074 } 1075 break; 1076 case Bytecodes::_swap: 1077 { Value w1 = state()->raw_pop(); 1078 Value w2 = state()->raw_pop(); 1079 state()->raw_push(w1); 1080 state()->raw_push(w2); 1081 } 1082 break; 1083 default: 1084 ShouldNotReachHere(); 1085 break; 1086 } 1087 } 1088 1089 1090 void GraphBuilder::arithmetic_op(ValueType* type, Bytecodes::Code code, ValueStack* state_before) { 1091 Value y = pop(type); 1092 Value x = pop(type); 1093 // NOTE: strictfp can be queried from current method since we don't 1094 // inline methods with differing strictfp bits 1095 Value res = new ArithmeticOp(code, x, y, method()->is_strict(), state_before); 1096 // Note: currently single-precision floating-point rounding on Intel is handled at the LIRGenerator level 1097 res = append(res); 1098 if (method()->is_strict()) { 1099 res = round_fp(res); 1100 } 1101 push(type, res); 1102 } 1103 1104 1105 void GraphBuilder::negate_op(ValueType* type) { 1106 push(type, append(new NegateOp(pop(type)))); 1107 } 1108 1109 1110 void GraphBuilder::shift_op(ValueType* type, Bytecodes::Code code) { 1111 Value s = ipop(); 1112 Value x = pop(type); 1113 // try to simplify 1114 // Note: This code should go into the canonicalizer as soon as it can 1115 // can handle canonicalized forms that contain more than one node. 1116 if (CanonicalizeNodes && code == Bytecodes::_iushr) { 1117 // pattern: x >>> s 1118 IntConstant* s1 = s->type()->as_IntConstant(); 1119 if (s1 != NULL) { 1120 // pattern: x >>> s1, with s1 constant 1121 ShiftOp* l = x->as_ShiftOp(); 1122 if (l != NULL && l->op() == Bytecodes::_ishl) { 1123 // pattern: (a << b) >>> s1 1124 IntConstant* s0 = l->y()->type()->as_IntConstant(); 1125 if (s0 != NULL) { 1126 // pattern: (a << s0) >>> s1 1127 const int s0c = s0->value() & 0x1F; // only the low 5 bits are significant for shifts 1128 const int s1c = s1->value() & 0x1F; // only the low 5 bits are significant for shifts 1129 if (s0c == s1c) { 1130 if (s0c == 0) { 1131 // pattern: (a << 0) >>> 0 => simplify to: a 1132 ipush(l->x()); 1133 } else { 1134 // pattern: (a << s0c) >>> s0c => simplify to: a & m, with m constant 1135 assert(0 < s0c && s0c < BitsPerInt, "adjust code below to handle corner cases"); 1136 const int m = (1 << (BitsPerInt - s0c)) - 1; 1137 Value s = append(new Constant(new IntConstant(m))); 1138 ipush(append(new LogicOp(Bytecodes::_iand, l->x(), s))); 1139 } 1140 return; 1141 } 1142 } 1143 } 1144 } 1145 } 1146 // could not simplify 1147 push(type, append(new ShiftOp(code, x, s))); 1148 } 1149 1150 1151 void GraphBuilder::logic_op(ValueType* type, Bytecodes::Code code) { 1152 Value y = pop(type); 1153 Value x = pop(type); 1154 push(type, append(new LogicOp(code, x, y))); 1155 } 1156 1157 1158 void GraphBuilder::compare_op(ValueType* type, Bytecodes::Code code) { 1159 ValueStack* state_before = copy_state_before(); 1160 Value y = pop(type); 1161 Value x = pop(type); 1162 ipush(append(new CompareOp(code, x, y, state_before))); 1163 } 1164 1165 1166 void GraphBuilder::convert(Bytecodes::Code op, BasicType from, BasicType to) { 1167 push(as_ValueType(to), append(new Convert(op, pop(as_ValueType(from)), as_ValueType(to)))); 1168 } 1169 1170 1171 void GraphBuilder::increment() { 1172 int index = stream()->get_index(); 1173 int delta = stream()->is_wide() ? (signed short)Bytes::get_Java_u2(stream()->cur_bcp() + 4) : (signed char)(stream()->cur_bcp()[2]); 1174 load_local(intType, index); 1175 ipush(append(new Constant(new IntConstant(delta)))); 1176 arithmetic_op(intType, Bytecodes::_iadd); 1177 store_local(intType, index); 1178 } 1179 1180 1181 void GraphBuilder::_goto(int from_bci, int to_bci) { 1182 Goto *x = new Goto(block_at(to_bci), to_bci <= from_bci); 1183 if (is_profiling()) { 1184 compilation()->set_would_profile(true); 1185 x->set_profiled_bci(bci()); 1186 if (profile_branches()) { 1187 x->set_profiled_method(method()); 1188 x->set_should_profile(true); 1189 } 1190 } 1191 append(x); 1192 } 1193 1194 1195 void GraphBuilder::if_node(Value x, If::Condition cond, Value y, ValueStack* state_before) { 1196 BlockBegin* tsux = block_at(stream()->get_dest()); 1197 BlockBegin* fsux = block_at(stream()->next_bci()); 1198 bool is_bb = tsux->bci() < stream()->cur_bci() || fsux->bci() < stream()->cur_bci(); 1199 // In case of loop invariant code motion or predicate insertion 1200 // before the body of a loop the state is needed 1201 Instruction *i = append(new If(x, cond, false, y, tsux, fsux, (is_bb || compilation()->is_optimistic()) ? state_before : NULL, is_bb)); 1202 1203 assert(i->as_Goto() == NULL || 1204 (i->as_Goto()->sux_at(0) == tsux && i->as_Goto()->is_safepoint() == tsux->bci() < stream()->cur_bci()) || 1205 (i->as_Goto()->sux_at(0) == fsux && i->as_Goto()->is_safepoint() == fsux->bci() < stream()->cur_bci()), 1206 "safepoint state of Goto returned by canonicalizer incorrect"); 1207 1208 if (is_profiling()) { 1209 If* if_node = i->as_If(); 1210 if (if_node != NULL) { 1211 // Note that we'd collect profile data in this method if we wanted it. 1212 compilation()->set_would_profile(true); 1213 // At level 2 we need the proper bci to count backedges 1214 if_node->set_profiled_bci(bci()); 1215 if (profile_branches()) { 1216 // Successors can be rotated by the canonicalizer, check for this case. 1217 if_node->set_profiled_method(method()); 1218 if_node->set_should_profile(true); 1219 if (if_node->tsux() == fsux) { 1220 if_node->set_swapped(true); 1221 } 1222 } 1223 return; 1224 } 1225 1226 // Check if this If was reduced to Goto. 1227 Goto *goto_node = i->as_Goto(); 1228 if (goto_node != NULL) { 1229 compilation()->set_would_profile(true); 1230 goto_node->set_profiled_bci(bci()); 1231 if (profile_branches()) { 1232 goto_node->set_profiled_method(method()); 1233 goto_node->set_should_profile(true); 1234 // Find out which successor is used. 1235 if (goto_node->default_sux() == tsux) { 1236 goto_node->set_direction(Goto::taken); 1237 } else if (goto_node->default_sux() == fsux) { 1238 goto_node->set_direction(Goto::not_taken); 1239 } else { 1240 ShouldNotReachHere(); 1241 } 1242 } 1243 return; 1244 } 1245 } 1246 } 1247 1248 1249 void GraphBuilder::if_zero(ValueType* type, If::Condition cond) { 1250 Value y = append(new Constant(intZero)); 1251 ValueStack* state_before = copy_state_before(); 1252 Value x = ipop(); 1253 if_node(x, cond, y, state_before); 1254 } 1255 1256 1257 void GraphBuilder::if_null(ValueType* type, If::Condition cond) { 1258 Value y = append(new Constant(objectNull)); 1259 ValueStack* state_before = copy_state_before(); 1260 Value x = apop(); 1261 if_node(x, cond, y, state_before); 1262 } 1263 1264 1265 void GraphBuilder::if_same(ValueType* type, If::Condition cond) { 1266 ValueStack* state_before = copy_state_before(); 1267 Value y = pop(type); 1268 Value x = pop(type); 1269 if_node(x, cond, y, state_before); 1270 } 1271 1272 1273 void GraphBuilder::jsr(int dest) { 1274 // We only handle well-formed jsrs (those which are "block-structured"). 1275 // If the bytecodes are strange (jumping out of a jsr block) then we 1276 // might end up trying to re-parse a block containing a jsr which 1277 // has already been activated. Watch for this case and bail out. 1278 for (ScopeData* cur_scope_data = scope_data(); 1279 cur_scope_data != NULL && cur_scope_data->parsing_jsr() && cur_scope_data->scope() == scope(); 1280 cur_scope_data = cur_scope_data->parent()) { 1281 if (cur_scope_data->jsr_entry_bci() == dest) { 1282 BAILOUT("too-complicated jsr/ret structure"); 1283 } 1284 } 1285 1286 push(addressType, append(new Constant(new AddressConstant(next_bci())))); 1287 if (!try_inline_jsr(dest)) { 1288 return; // bailed out while parsing and inlining subroutine 1289 } 1290 } 1291 1292 1293 void GraphBuilder::ret(int local_index) { 1294 if (!parsing_jsr()) BAILOUT("ret encountered while not parsing subroutine"); 1295 1296 if (local_index != scope_data()->jsr_return_address_local()) { 1297 BAILOUT("can not handle complicated jsr/ret constructs"); 1298 } 1299 1300 // Rets simply become (NON-SAFEPOINT) gotos to the jsr continuation 1301 append(new Goto(scope_data()->jsr_continuation(), false)); 1302 } 1303 1304 1305 void GraphBuilder::table_switch() { 1306 Bytecode_tableswitch sw(stream()); 1307 const int l = sw.length(); 1308 if (CanonicalizeNodes && l == 1) { 1309 // total of 2 successors => use If instead of switch 1310 // Note: This code should go into the canonicalizer as soon as it can 1311 // can handle canonicalized forms that contain more than one node. 1312 Value key = append(new Constant(new IntConstant(sw.low_key()))); 1313 BlockBegin* tsux = block_at(bci() + sw.dest_offset_at(0)); 1314 BlockBegin* fsux = block_at(bci() + sw.default_offset()); 1315 bool is_bb = tsux->bci() < bci() || fsux->bci() < bci(); 1316 // In case of loop invariant code motion or predicate insertion 1317 // before the body of a loop the state is needed 1318 ValueStack* state_before = copy_state_if_bb(is_bb); 1319 append(new If(ipop(), If::eql, true, key, tsux, fsux, state_before, is_bb)); 1320 } else { 1321 // collect successors 1322 BlockList* sux = new BlockList(l + 1, NULL); 1323 int i; 1324 bool has_bb = false; 1325 for (i = 0; i < l; i++) { 1326 sux->at_put(i, block_at(bci() + sw.dest_offset_at(i))); 1327 if (sw.dest_offset_at(i) < 0) has_bb = true; 1328 } 1329 // add default successor 1330 if (sw.default_offset() < 0) has_bb = true; 1331 sux->at_put(i, block_at(bci() + sw.default_offset())); 1332 // In case of loop invariant code motion or predicate insertion 1333 // before the body of a loop the state is needed 1334 ValueStack* state_before = copy_state_if_bb(has_bb); 1335 Instruction* res = append(new TableSwitch(ipop(), sux, sw.low_key(), state_before, has_bb)); 1336 #ifdef ASSERT 1337 if (res->as_Goto()) { 1338 for (i = 0; i < l; i++) { 1339 if (sux->at(i) == res->as_Goto()->sux_at(0)) { 1340 assert(res->as_Goto()->is_safepoint() == sw.dest_offset_at(i) < 0, "safepoint state of Goto returned by canonicalizer incorrect"); 1341 } 1342 } 1343 } 1344 #endif 1345 } 1346 } 1347 1348 1349 void GraphBuilder::lookup_switch() { 1350 Bytecode_lookupswitch sw(stream()); 1351 const int l = sw.number_of_pairs(); 1352 if (CanonicalizeNodes && l == 1) { 1353 // total of 2 successors => use If instead of switch 1354 // Note: This code should go into the canonicalizer as soon as it can 1355 // can handle canonicalized forms that contain more than one node. 1356 // simplify to If 1357 LookupswitchPair pair = sw.pair_at(0); 1358 Value key = append(new Constant(new IntConstant(pair.match()))); 1359 BlockBegin* tsux = block_at(bci() + pair.offset()); 1360 BlockBegin* fsux = block_at(bci() + sw.default_offset()); 1361 bool is_bb = tsux->bci() < bci() || fsux->bci() < bci(); 1362 // In case of loop invariant code motion or predicate insertion 1363 // before the body of a loop the state is needed 1364 ValueStack* state_before = copy_state_if_bb(is_bb);; 1365 append(new If(ipop(), If::eql, true, key, tsux, fsux, state_before, is_bb)); 1366 } else { 1367 // collect successors & keys 1368 BlockList* sux = new BlockList(l + 1, NULL); 1369 intArray* keys = new intArray(l, l, 0); 1370 int i; 1371 bool has_bb = false; 1372 for (i = 0; i < l; i++) { 1373 LookupswitchPair pair = sw.pair_at(i); 1374 if (pair.offset() < 0) has_bb = true; 1375 sux->at_put(i, block_at(bci() + pair.offset())); 1376 keys->at_put(i, pair.match()); 1377 } 1378 // add default successor 1379 if (sw.default_offset() < 0) has_bb = true; 1380 sux->at_put(i, block_at(bci() + sw.default_offset())); 1381 // In case of loop invariant code motion or predicate insertion 1382 // before the body of a loop the state is needed 1383 ValueStack* state_before = copy_state_if_bb(has_bb); 1384 Instruction* res = append(new LookupSwitch(ipop(), sux, keys, state_before, has_bb)); 1385 #ifdef ASSERT 1386 if (res->as_Goto()) { 1387 for (i = 0; i < l; i++) { 1388 if (sux->at(i) == res->as_Goto()->sux_at(0)) { 1389 assert(res->as_Goto()->is_safepoint() == sw.pair_at(i).offset() < 0, "safepoint state of Goto returned by canonicalizer incorrect"); 1390 } 1391 } 1392 } 1393 #endif 1394 } 1395 } 1396 1397 void GraphBuilder::call_register_finalizer() { 1398 // If the receiver requires finalization then emit code to perform 1399 // the registration on return. 1400 1401 // Gather some type information about the receiver 1402 Value receiver = state()->local_at(0); 1403 assert(receiver != NULL, "must have a receiver"); 1404 ciType* declared_type = receiver->declared_type(); 1405 ciType* exact_type = receiver->exact_type(); 1406 if (exact_type == NULL && 1407 receiver->as_Local() && 1408 receiver->as_Local()->java_index() == 0) { 1409 ciInstanceKlass* ik = compilation()->method()->holder(); 1410 if (ik->is_final()) { 1411 exact_type = ik; 1412 } else if (UseCHA && !(ik->has_subklass() || ik->is_interface())) { 1413 // test class is leaf class 1414 compilation()->dependency_recorder()->assert_leaf_type(ik); 1415 exact_type = ik; 1416 } else { 1417 declared_type = ik; 1418 } 1419 } 1420 1421 // see if we know statically that registration isn't required 1422 bool needs_check = true; 1423 if (exact_type != NULL) { 1424 needs_check = exact_type->as_instance_klass()->has_finalizer(); 1425 } else if (declared_type != NULL) { 1426 ciInstanceKlass* ik = declared_type->as_instance_klass(); 1427 if (!Dependencies::has_finalizable_subclass(ik)) { 1428 compilation()->dependency_recorder()->assert_has_no_finalizable_subclasses(ik); 1429 needs_check = false; 1430 } 1431 } 1432 1433 if (needs_check) { 1434 // Perform the registration of finalizable objects. 1435 ValueStack* state_before = copy_state_for_exception(); 1436 load_local(objectType, 0); 1437 append_split(new Intrinsic(voidType, vmIntrinsics::_Object_init, 1438 state()->pop_arguments(1), 1439 true, state_before, true)); 1440 } 1441 } 1442 1443 1444 void GraphBuilder::method_return(Value x) { 1445 if (RegisterFinalizersAtInit && 1446 method()->intrinsic_id() == vmIntrinsics::_Object_init) { 1447 call_register_finalizer(); 1448 } 1449 1450 bool need_mem_bar = false; 1451 if (method()->name() == ciSymbol::object_initializer_name() && 1452 (scope()->wrote_final() || (AlwaysSafeConstructors && scope()->wrote_fields()) 1453 || (support_IRIW_for_not_multiple_copy_atomic_cpu && scope()->wrote_volatile()) 1454 )){ 1455 need_mem_bar = true; 1456 } 1457 1458 BasicType bt = method()->return_type()->basic_type(); 1459 switch (bt) { 1460 case T_BYTE: 1461 { 1462 Value shift = append(new Constant(new IntConstant(24))); 1463 x = append(new ShiftOp(Bytecodes::_ishl, x, shift)); 1464 x = append(new ShiftOp(Bytecodes::_ishr, x, shift)); 1465 break; 1466 } 1467 case T_SHORT: 1468 { 1469 Value shift = append(new Constant(new IntConstant(16))); 1470 x = append(new ShiftOp(Bytecodes::_ishl, x, shift)); 1471 x = append(new ShiftOp(Bytecodes::_ishr, x, shift)); 1472 break; 1473 } 1474 case T_CHAR: 1475 { 1476 Value mask = append(new Constant(new IntConstant(0xFFFF))); 1477 x = append(new LogicOp(Bytecodes::_iand, x, mask)); 1478 break; 1479 } 1480 case T_BOOLEAN: 1481 { 1482 Value mask = append(new Constant(new IntConstant(1))); 1483 x = append(new LogicOp(Bytecodes::_iand, x, mask)); 1484 break; 1485 } 1486 } 1487 1488 // Check to see whether we are inlining. If so, Return 1489 // instructions become Gotos to the continuation point. 1490 if (continuation() != NULL) { 1491 assert(!method()->is_synchronized() || InlineSynchronizedMethods, "can not inline synchronized methods yet"); 1492 1493 if (compilation()->env()->dtrace_method_probes()) { 1494 // Report exit from inline methods 1495 Values* args = new Values(1); 1496 args->push(append(new Constant(new MethodConstant(method())))); 1497 append(new RuntimeCall(voidType, "dtrace_method_exit", CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), args)); 1498 } 1499 1500 // If the inlined method is synchronized, the monitor must be 1501 // released before we jump to the continuation block. 1502 if (method()->is_synchronized()) { 1503 assert(state()->locks_size() == 1, "receiver must be locked here"); 1504 monitorexit(state()->lock_at(0), SynchronizationEntryBCI); 1505 } 1506 1507 if (need_mem_bar) { 1508 append(new MemBar(lir_membar_storestore)); 1509 } 1510 1511 // State at end of inlined method is the state of the caller 1512 // without the method parameters on stack, including the 1513 // return value, if any, of the inlined method on operand stack. 1514 int invoke_bci = state()->caller_state()->bci(); 1515 set_state(state()->caller_state()->copy_for_parsing()); 1516 if (x != NULL) { 1517 state()->push(x->type(), x); 1518 if (profile_return() && x->type()->is_object_kind()) { 1519 ciMethod* caller = state()->scope()->method(); 1520 ciMethodData* md = caller->method_data_or_null(); 1521 ciProfileData* data = md->bci_to_data(invoke_bci); 1522 if (data->is_CallTypeData() || data->is_VirtualCallTypeData()) { 1523 bool has_return = data->is_CallTypeData() ? ((ciCallTypeData*)data)->has_return() : ((ciVirtualCallTypeData*)data)->has_return(); 1524 // May not be true in case of an inlined call through a method handle intrinsic. 1525 if (has_return) { 1526 profile_return_type(x, method(), caller, invoke_bci); 1527 } 1528 } 1529 } 1530 } 1531 Goto* goto_callee = new Goto(continuation(), false); 1532 1533 // See whether this is the first return; if so, store off some 1534 // of the state for later examination 1535 if (num_returns() == 0) { 1536 set_inline_cleanup_info(); 1537 } 1538 1539 // The current bci() is in the wrong scope, so use the bci() of 1540 // the continuation point. 1541 append_with_bci(goto_callee, scope_data()->continuation()->bci()); 1542 incr_num_returns(); 1543 return; 1544 } 1545 1546 state()->truncate_stack(0); 1547 if (method()->is_synchronized()) { 1548 // perform the unlocking before exiting the method 1549 Value receiver; 1550 if (!method()->is_static()) { 1551 receiver = _initial_state->local_at(0); 1552 } else { 1553 receiver = append(new Constant(new ClassConstant(method()->holder()))); 1554 } 1555 append_split(new MonitorExit(receiver, state()->unlock())); 1556 } 1557 1558 if (need_mem_bar) { 1559 append(new MemBar(lir_membar_storestore)); 1560 } 1561 1562 append(new Return(x)); 1563 } 1564 1565 Value GraphBuilder::make_constant(ciConstant field_value, ciField* field) { 1566 if (!field_value.is_valid()) return NULL; 1567 1568 BasicType field_type = field_value.basic_type(); 1569 ValueType* value = as_ValueType(field_value); 1570 1571 // Attach dimension info to stable arrays. 1572 if (FoldStableValues && 1573 field->is_stable() && field_type == T_ARRAY && !field_value.is_null_or_zero()) { 1574 ciArray* array = field_value.as_object()->as_array(); 1575 jint dimension = field->type()->as_array_klass()->dimension(); 1576 value = new StableArrayConstant(array, dimension); 1577 } 1578 1579 switch (field_type) { 1580 case T_ARRAY: 1581 case T_OBJECT: 1582 if (field_value.as_object()->should_be_constant()) { 1583 return new Constant(value); 1584 } 1585 return NULL; // Not a constant. 1586 default: 1587 return new Constant(value); 1588 } 1589 } 1590 1591 void GraphBuilder::access_field(Bytecodes::Code code) { 1592 bool will_link; 1593 ciField* field = stream()->get_field(will_link); 1594 ciInstanceKlass* holder = field->holder(); 1595 BasicType field_type = field->type()->basic_type(); 1596 ValueType* type = as_ValueType(field_type); 1597 // call will_link again to determine if the field is valid. 1598 const bool needs_patching = !holder->is_loaded() || 1599 !field->will_link(method()->holder(), code) || 1600 PatchALot; 1601 1602 ValueStack* state_before = NULL; 1603 if (!holder->is_initialized() || needs_patching) { 1604 // save state before instruction for debug info when 1605 // deoptimization happens during patching 1606 state_before = copy_state_before(); 1607 } 1608 1609 Value obj = NULL; 1610 if (code == Bytecodes::_getstatic || code == Bytecodes::_putstatic) { 1611 if (state_before != NULL) { 1612 // build a patching constant 1613 obj = new Constant(new InstanceConstant(holder->java_mirror()), state_before); 1614 } else { 1615 obj = new Constant(new InstanceConstant(holder->java_mirror())); 1616 } 1617 } 1618 1619 if (field->is_final() && (code == Bytecodes::_putfield)) { 1620 scope()->set_wrote_final(); 1621 } 1622 1623 if (code == Bytecodes::_putfield) { 1624 scope()->set_wrote_fields(); 1625 if (field->is_volatile()) { 1626 scope()->set_wrote_volatile(); 1627 } 1628 } 1629 1630 const int offset = !needs_patching ? field->offset() : -1; 1631 switch (code) { 1632 case Bytecodes::_getstatic: { 1633 // check for compile-time constants, i.e., initialized static final fields 1634 Value constant = NULL; 1635 if (field->is_static_constant() && !PatchALot) { 1636 ciConstant field_value = field->constant_value(); 1637 assert(!field->is_stable() || !field_value.is_null_or_zero(), 1638 "stable static w/ default value shouldn't be a constant"); 1639 constant = make_constant(field_value, field); 1640 } 1641 if (constant != NULL) { 1642 push(type, append(constant)); 1643 } else { 1644 if (state_before == NULL) { 1645 state_before = copy_state_for_exception(); 1646 } 1647 push(type, append(new LoadField(append(obj), offset, field, true, 1648 state_before, needs_patching))); 1649 } 1650 break; 1651 } 1652 case Bytecodes::_putstatic: { 1653 Value val = pop(type); 1654 if (state_before == NULL) { 1655 state_before = copy_state_for_exception(); 1656 } 1657 if (field->type()->basic_type() == T_BOOLEAN) { 1658 Value mask = append(new Constant(new IntConstant(1))); 1659 val = append(new LogicOp(Bytecodes::_iand, val, mask)); 1660 } 1661 append(new StoreField(append(obj), offset, field, val, true, state_before, needs_patching)); 1662 break; 1663 } 1664 case Bytecodes::_getfield: { 1665 // Check for compile-time constants, i.e., trusted final non-static fields. 1666 Value constant = NULL; 1667 obj = apop(); 1668 ObjectType* obj_type = obj->type()->as_ObjectType(); 1669 if (field->is_constant() && obj_type->is_constant() && !PatchALot) { 1670 ciObject* const_oop = obj_type->constant_value(); 1671 if (!const_oop->is_null_object() && const_oop->is_loaded()) { 1672 ciConstant field_value = field->constant_value_of(const_oop); 1673 if (field_value.is_valid()) { 1674 constant = make_constant(field_value, field); 1675 // For CallSite objects add a dependency for invalidation of the optimization. 1676 if (field->is_call_site_target()) { 1677 ciCallSite* call_site = const_oop->as_call_site(); 1678 if (!call_site->is_constant_call_site()) { 1679 ciMethodHandle* target = field_value.as_object()->as_method_handle(); 1680 dependency_recorder()->assert_call_site_target_value(call_site, target); 1681 } 1682 } 1683 } 1684 } 1685 } 1686 if (constant != NULL) { 1687 push(type, append(constant)); 1688 } else { 1689 if (state_before == NULL) { 1690 state_before = copy_state_for_exception(); 1691 } 1692 LoadField* load = new LoadField(obj, offset, field, false, state_before, needs_patching); 1693 Value replacement = !needs_patching ? _memory->load(load) : load; 1694 if (replacement != load) { 1695 assert(replacement->is_linked() || !replacement->can_be_linked(), "should already by linked"); 1696 push(type, replacement); 1697 } else { 1698 push(type, append(load)); 1699 } 1700 } 1701 break; 1702 } 1703 case Bytecodes::_putfield: { 1704 Value val = pop(type); 1705 obj = apop(); 1706 if (state_before == NULL) { 1707 state_before = copy_state_for_exception(); 1708 } 1709 if (field->type()->basic_type() == T_BOOLEAN) { 1710 Value mask = append(new Constant(new IntConstant(1))); 1711 val = append(new LogicOp(Bytecodes::_iand, val, mask)); 1712 } 1713 StoreField* store = new StoreField(obj, offset, field, val, false, state_before, needs_patching); 1714 if (!needs_patching) store = _memory->store(store); 1715 if (store != NULL) { 1716 append(store); 1717 } 1718 break; 1719 } 1720 default: 1721 ShouldNotReachHere(); 1722 break; 1723 } 1724 } 1725 1726 1727 Dependencies* GraphBuilder::dependency_recorder() const { 1728 assert(DeoptC1, "need debug information"); 1729 return compilation()->dependency_recorder(); 1730 } 1731 1732 // How many arguments do we want to profile? 1733 Values* GraphBuilder::args_list_for_profiling(ciMethod* target, int& start, bool may_have_receiver) { 1734 int n = 0; 1735 bool has_receiver = may_have_receiver && Bytecodes::has_receiver(method()->java_code_at_bci(bci())); 1736 start = has_receiver ? 1 : 0; 1737 if (profile_arguments()) { 1738 ciProfileData* data = method()->method_data()->bci_to_data(bci()); 1739 if (data->is_CallTypeData() || data->is_VirtualCallTypeData()) { 1740 n = data->is_CallTypeData() ? data->as_CallTypeData()->number_of_arguments() : data->as_VirtualCallTypeData()->number_of_arguments(); 1741 } 1742 } 1743 // If we are inlining then we need to collect arguments to profile parameters for the target 1744 if (profile_parameters() && target != NULL) { 1745 if (target->method_data() != NULL && target->method_data()->parameters_type_data() != NULL) { 1746 // The receiver is profiled on method entry so it's included in 1747 // the number of parameters but here we're only interested in 1748 // actual arguments. 1749 n = MAX2(n, target->method_data()->parameters_type_data()->number_of_parameters() - start); 1750 } 1751 } 1752 if (n > 0) { 1753 return new Values(n); 1754 } 1755 return NULL; 1756 } 1757 1758 void GraphBuilder::check_args_for_profiling(Values* obj_args, int expected) { 1759 #ifdef ASSERT 1760 bool ignored_will_link; 1761 ciSignature* declared_signature = NULL; 1762 ciMethod* real_target = method()->get_method_at_bci(bci(), ignored_will_link, &declared_signature); 1763 assert(expected == obj_args->max_length() || real_target->is_method_handle_intrinsic(), "missed on arg?"); 1764 #endif 1765 } 1766 1767 // Collect arguments that we want to profile in a list 1768 Values* GraphBuilder::collect_args_for_profiling(Values* args, ciMethod* target, bool may_have_receiver) { 1769 int start = 0; 1770 Values* obj_args = args_list_for_profiling(target, start, may_have_receiver); 1771 if (obj_args == NULL) { 1772 return NULL; 1773 } 1774 int s = obj_args->max_length(); 1775 // if called through method handle invoke, some arguments may have been popped 1776 for (int i = start, j = 0; j < s && i < args->length(); i++) { 1777 if (args->at(i)->type()->is_object_kind()) { 1778 obj_args->push(args->at(i)); 1779 j++; 1780 } 1781 } 1782 check_args_for_profiling(obj_args, s); 1783 return obj_args; 1784 } 1785 1786 1787 void GraphBuilder::invoke(Bytecodes::Code code) { 1788 bool will_link; 1789 ciSignature* declared_signature = NULL; 1790 ciMethod* target = stream()->get_method(will_link, &declared_signature); 1791 ciKlass* holder = stream()->get_declared_method_holder(); 1792 const Bytecodes::Code bc_raw = stream()->cur_bc_raw(); 1793 assert(declared_signature != NULL, "cannot be null"); 1794 1795 ciInstanceKlass* klass = target->holder(); 1796 1797 // Make sure there are no evident problems with linking the instruction. 1798 bool is_resolved = true; 1799 if (klass->is_loaded() && !target->is_loaded()) { 1800 is_resolved = false; // method not found 1801 } 1802 1803 // check if CHA possible: if so, change the code to invoke_special 1804 ciInstanceKlass* calling_klass = method()->holder(); 1805 ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder); 1806 ciInstanceKlass* actual_recv = callee_holder; 1807 1808 CompileLog* log = compilation()->log(); 1809 if (log != NULL) 1810 log->elem("call method='%d' instr='%s'", 1811 log->identify(target), 1812 Bytecodes::name(code)); 1813 1814 // Some methods are obviously bindable without any type checks so 1815 // convert them directly to an invokespecial or invokestatic. 1816 if (target->is_loaded() && !target->is_abstract() && target->can_be_statically_bound()) { 1817 switch (bc_raw) { 1818 case Bytecodes::_invokevirtual: 1819 code = Bytecodes::_invokespecial; 1820 break; 1821 case Bytecodes::_invokehandle: 1822 code = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokespecial; 1823 break; 1824 } 1825 } else { 1826 if (bc_raw == Bytecodes::_invokehandle) { 1827 assert(!will_link, "should come here only for unlinked call"); 1828 code = Bytecodes::_invokespecial; 1829 } 1830 } 1831 1832 // Push appendix argument (MethodType, CallSite, etc.), if one. 1833 bool patch_for_appendix = false; 1834 int patching_appendix_arg = 0; 1835 if (Bytecodes::has_optional_appendix(bc_raw) && (!will_link || PatchALot)) { 1836 Value arg = append(new Constant(new ObjectConstant(compilation()->env()->unloaded_ciinstance()), copy_state_before())); 1837 apush(arg); 1838 patch_for_appendix = true; 1839 patching_appendix_arg = (will_link && stream()->has_appendix()) ? 0 : 1; 1840 } else if (stream()->has_appendix()) { 1841 ciObject* appendix = stream()->get_appendix(); 1842 Value arg = append(new Constant(new ObjectConstant(appendix))); 1843 apush(arg); 1844 } 1845 1846 ciMethod* cha_monomorphic_target = NULL; 1847 ciMethod* exact_target = NULL; 1848 Value better_receiver = NULL; 1849 if (UseCHA && DeoptC1 && klass->is_loaded() && target->is_loaded() && 1850 !(// %%% FIXME: Are both of these relevant? 1851 target->is_method_handle_intrinsic() || 1852 target->is_compiled_lambda_form()) && 1853 !patch_for_appendix) { 1854 Value receiver = NULL; 1855 ciInstanceKlass* receiver_klass = NULL; 1856 bool type_is_exact = false; 1857 // try to find a precise receiver type 1858 if (will_link && !target->is_static()) { 1859 int index = state()->stack_size() - (target->arg_size_no_receiver() + 1); 1860 receiver = state()->stack_at(index); 1861 ciType* type = receiver->exact_type(); 1862 if (type != NULL && type->is_loaded() && 1863 type->is_instance_klass() && !type->as_instance_klass()->is_interface()) { 1864 receiver_klass = (ciInstanceKlass*) type; 1865 type_is_exact = true; 1866 } 1867 if (type == NULL) { 1868 type = receiver->declared_type(); 1869 if (type != NULL && type->is_loaded() && 1870 type->is_instance_klass() && !type->as_instance_klass()->is_interface()) { 1871 receiver_klass = (ciInstanceKlass*) type; 1872 if (receiver_klass->is_leaf_type() && !receiver_klass->is_final()) { 1873 // Insert a dependency on this type since 1874 // find_monomorphic_target may assume it's already done. 1875 dependency_recorder()->assert_leaf_type(receiver_klass); 1876 type_is_exact = true; 1877 } 1878 } 1879 } 1880 } 1881 if (receiver_klass != NULL && type_is_exact && 1882 receiver_klass->is_loaded() && code != Bytecodes::_invokespecial) { 1883 // If we have the exact receiver type we can bind directly to 1884 // the method to call. 1885 exact_target = target->resolve_invoke(calling_klass, receiver_klass); 1886 if (exact_target != NULL) { 1887 target = exact_target; 1888 code = Bytecodes::_invokespecial; 1889 } 1890 } 1891 if (receiver_klass != NULL && 1892 receiver_klass->is_subtype_of(actual_recv) && 1893 actual_recv->is_initialized()) { 1894 actual_recv = receiver_klass; 1895 } 1896 1897 if ((code == Bytecodes::_invokevirtual && callee_holder->is_initialized()) || 1898 (code == Bytecodes::_invokeinterface && callee_holder->is_initialized() && !actual_recv->is_interface())) { 1899 // Use CHA on the receiver to select a more precise method. 1900 cha_monomorphic_target = target->find_monomorphic_target(calling_klass, callee_holder, actual_recv); 1901 } else if (code == Bytecodes::_invokeinterface && callee_holder->is_loaded() && receiver != NULL) { 1902 // if there is only one implementor of this interface then we 1903 // may be able bind this invoke directly to the implementing 1904 // klass but we need both a dependence on the single interface 1905 // and on the method we bind to. Additionally since all we know 1906 // about the receiver type is the it's supposed to implement the 1907 // interface we have to insert a check that it's the class we 1908 // expect. Interface types are not checked by the verifier so 1909 // they are roughly equivalent to Object. 1910 ciInstanceKlass* singleton = NULL; 1911 if (target->holder()->nof_implementors() == 1) { 1912 singleton = target->holder()->implementor(); 1913 assert(singleton != NULL && singleton != target->holder(), 1914 "just checking"); 1915 1916 assert(holder->is_interface(), "invokeinterface to non interface?"); 1917 ciInstanceKlass* decl_interface = (ciInstanceKlass*)holder; 1918 // the number of implementors for decl_interface is less or 1919 // equal to the number of implementors for target->holder() so 1920 // if number of implementors of target->holder() == 1 then 1921 // number of implementors for decl_interface is 0 or 1. If 1922 // it's 0 then no class implements decl_interface and there's 1923 // no point in inlining. 1924 if (!holder->is_loaded() || decl_interface->nof_implementors() != 1 || decl_interface->has_default_methods()) { 1925 singleton = NULL; 1926 } 1927 } 1928 if (singleton) { 1929 cha_monomorphic_target = target->find_monomorphic_target(calling_klass, target->holder(), singleton); 1930 if (cha_monomorphic_target != NULL) { 1931 // If CHA is able to bind this invoke then update the class 1932 // to match that class, otherwise klass will refer to the 1933 // interface. 1934 klass = cha_monomorphic_target->holder(); 1935 actual_recv = target->holder(); 1936 1937 // insert a check it's really the expected class. 1938 CheckCast* c = new CheckCast(klass, receiver, copy_state_for_exception()); 1939 c->set_incompatible_class_change_check(); 1940 c->set_direct_compare(klass->is_final()); 1941 // pass the result of the checkcast so that the compiler has 1942 // more accurate type info in the inlinee 1943 better_receiver = append_split(c); 1944 } 1945 } 1946 } 1947 } 1948 1949 if (cha_monomorphic_target != NULL) { 1950 if (cha_monomorphic_target->is_abstract()) { 1951 // Do not optimize for abstract methods 1952 cha_monomorphic_target = NULL; 1953 } 1954 } 1955 1956 if (cha_monomorphic_target != NULL) { 1957 if (!(target->is_final_method())) { 1958 // If we inlined because CHA revealed only a single target method, 1959 // then we are dependent on that target method not getting overridden 1960 // by dynamic class loading. Be sure to test the "static" receiver 1961 // dest_method here, as opposed to the actual receiver, which may 1962 // falsely lead us to believe that the receiver is final or private. 1963 dependency_recorder()->assert_unique_concrete_method(actual_recv, cha_monomorphic_target); 1964 } 1965 code = Bytecodes::_invokespecial; 1966 } 1967 1968 // check if we could do inlining 1969 if (!PatchALot && Inline && is_resolved && 1970 klass->is_loaded() && target->is_loaded() && 1971 (klass->is_initialized() || klass->is_interface() && target->holder()->is_initialized()) 1972 && !patch_for_appendix) { 1973 // callee is known => check if we have static binding 1974 if (code == Bytecodes::_invokestatic || 1975 code == Bytecodes::_invokespecial || 1976 code == Bytecodes::_invokevirtual && target->is_final_method() || 1977 code == Bytecodes::_invokedynamic) { 1978 ciMethod* inline_target = (cha_monomorphic_target != NULL) ? cha_monomorphic_target : target; 1979 // static binding => check if callee is ok 1980 bool success = try_inline(inline_target, (cha_monomorphic_target != NULL) || (exact_target != NULL), code, better_receiver); 1981 1982 CHECK_BAILOUT(); 1983 clear_inline_bailout(); 1984 1985 if (success) { 1986 // Register dependence if JVMTI has either breakpoint 1987 // setting or hotswapping of methods capabilities since they may 1988 // cause deoptimization. 1989 if (compilation()->env()->jvmti_can_hotswap_or_post_breakpoint()) { 1990 dependency_recorder()->assert_evol_method(inline_target); 1991 } 1992 return; 1993 } 1994 } else { 1995 print_inlining(target, "no static binding", /*success*/ false); 1996 } 1997 } else { 1998 print_inlining(target, "not inlineable", /*success*/ false); 1999 } 2000 2001 // If we attempted an inline which did not succeed because of a 2002 // bailout during construction of the callee graph, the entire 2003 // compilation has to be aborted. This is fairly rare and currently 2004 // seems to only occur for jasm-generated classes which contain 2005 // jsr/ret pairs which are not associated with finally clauses and 2006 // do not have exception handlers in the containing method, and are 2007 // therefore not caught early enough to abort the inlining without 2008 // corrupting the graph. (We currently bail out with a non-empty 2009 // stack at a ret in these situations.) 2010 CHECK_BAILOUT(); 2011 2012 // inlining not successful => standard invoke 2013 bool is_loaded = target->is_loaded(); 2014 ValueType* result_type = as_ValueType(declared_signature->return_type()); 2015 ValueStack* state_before = copy_state_exhandling(); 2016 2017 // The bytecode (code) might change in this method so we are checking this very late. 2018 const bool has_receiver = 2019 code == Bytecodes::_invokespecial || 2020 code == Bytecodes::_invokevirtual || 2021 code == Bytecodes::_invokeinterface; 2022 Values* args = state()->pop_arguments(target->arg_size_no_receiver() + patching_appendix_arg); 2023 Value recv = has_receiver ? apop() : NULL; 2024 int vtable_index = Method::invalid_vtable_index; 2025 2026 #ifdef SPARC 2027 // Currently only supported on Sparc. 2028 // The UseInlineCaches only controls dispatch to invokevirtuals for 2029 // loaded classes which we weren't able to statically bind. 2030 if (!UseInlineCaches && is_resolved && is_loaded && code == Bytecodes::_invokevirtual 2031 && !target->can_be_statically_bound()) { 2032 // Find a vtable index if one is available 2033 // For arrays, callee_holder is Object. Resolving the call with 2034 // Object would allow an illegal call to finalize() on an 2035 // array. We use holder instead: illegal calls to finalize() won't 2036 // be compiled as vtable calls (IC call resolution will catch the 2037 // illegal call) and the few legal calls on array types won't be 2038 // either. 2039 vtable_index = target->resolve_vtable_index(calling_klass, holder); 2040 } 2041 #endif 2042 2043 if (is_resolved) { 2044 // invokespecial always needs a NULL check. invokevirtual where the target is 2045 // final or where it's not known whether the target is final requires a NULL check. 2046 // Otherwise normal invokevirtual will perform the null check during the lookup 2047 // logic or the unverified entry point. Profiling of calls requires that 2048 // the null check is performed in all cases. 2049 bool do_null_check = (recv != NULL) && 2050 (code == Bytecodes::_invokespecial || !is_loaded || target->is_final() || (is_profiling() && profile_calls())); 2051 2052 if (do_null_check) { 2053 null_check(recv); 2054 } 2055 2056 if (is_profiling()) { 2057 // Note that we'd collect profile data in this method if we wanted it. 2058 compilation()->set_would_profile(true); 2059 2060 if (profile_calls()) { 2061 assert(cha_monomorphic_target == NULL || exact_target == NULL, "both can not be set"); 2062 ciKlass* target_klass = NULL; 2063 if (cha_monomorphic_target != NULL) { 2064 target_klass = cha_monomorphic_target->holder(); 2065 } else if (exact_target != NULL) { 2066 target_klass = exact_target->holder(); 2067 } 2068 profile_call(target, recv, target_klass, collect_args_for_profiling(args, NULL, false), false); 2069 } 2070 } 2071 } else { 2072 // No need in null check or profiling: linkage error will be thrown at runtime 2073 // during resolution. 2074 } 2075 2076 Invoke* result = new Invoke(code, result_type, recv, args, vtable_index, target, state_before); 2077 // push result 2078 append_split(result); 2079 2080 if (result_type != voidType) { 2081 if (method()->is_strict()) { 2082 push(result_type, round_fp(result)); 2083 } else { 2084 push(result_type, result); 2085 } 2086 } 2087 if (profile_return() && result_type->is_object_kind()) { 2088 profile_return_type(result, target); 2089 } 2090 } 2091 2092 2093 void GraphBuilder::new_instance(int klass_index) { 2094 ValueStack* state_before = copy_state_exhandling(); 2095 bool will_link; 2096 ciKlass* klass = stream()->get_klass(will_link); 2097 assert(klass->is_instance_klass(), "must be an instance klass"); 2098 NewInstance* new_instance = new NewInstance(klass->as_instance_klass(), state_before, stream()->is_unresolved_klass()); 2099 _memory->new_instance(new_instance); 2100 apush(append_split(new_instance)); 2101 } 2102 2103 2104 void GraphBuilder::new_type_array() { 2105 ValueStack* state_before = copy_state_exhandling(); 2106 apush(append_split(new NewTypeArray(ipop(), (BasicType)stream()->get_index(), state_before))); 2107 } 2108 2109 2110 void GraphBuilder::new_object_array() { 2111 bool will_link; 2112 ciKlass* klass = stream()->get_klass(will_link); 2113 ValueStack* state_before = !klass->is_loaded() || PatchALot ? copy_state_before() : copy_state_exhandling(); 2114 NewArray* n = new NewObjectArray(klass, ipop(), state_before); 2115 apush(append_split(n)); 2116 } 2117 2118 2119 bool GraphBuilder::direct_compare(ciKlass* k) { 2120 if (k->is_loaded() && k->is_instance_klass() && !UseSlowPath) { 2121 ciInstanceKlass* ik = k->as_instance_klass(); 2122 if (ik->is_final()) { 2123 return true; 2124 } else { 2125 if (DeoptC1 && UseCHA && !(ik->has_subklass() || ik->is_interface())) { 2126 // test class is leaf class 2127 dependency_recorder()->assert_leaf_type(ik); 2128 return true; 2129 } 2130 } 2131 } 2132 return false; 2133 } 2134 2135 2136 void GraphBuilder::check_cast(int klass_index) { 2137 bool will_link; 2138 ciKlass* klass = stream()->get_klass(will_link); 2139 ValueStack* state_before = !klass->is_loaded() || PatchALot ? copy_state_before() : copy_state_for_exception(); 2140 CheckCast* c = new CheckCast(klass, apop(), state_before); 2141 apush(append_split(c)); 2142 c->set_direct_compare(direct_compare(klass)); 2143 2144 if (is_profiling()) { 2145 // Note that we'd collect profile data in this method if we wanted it. 2146 compilation()->set_would_profile(true); 2147 2148 if (profile_checkcasts()) { 2149 c->set_profiled_method(method()); 2150 c->set_profiled_bci(bci()); 2151 c->set_should_profile(true); 2152 } 2153 } 2154 } 2155 2156 2157 void GraphBuilder::instance_of(int klass_index) { 2158 bool will_link; 2159 ciKlass* klass = stream()->get_klass(will_link); 2160 ValueStack* state_before = !klass->is_loaded() || PatchALot ? copy_state_before() : copy_state_exhandling(); 2161 InstanceOf* i = new InstanceOf(klass, apop(), state_before); 2162 ipush(append_split(i)); 2163 i->set_direct_compare(direct_compare(klass)); 2164 2165 if (is_profiling()) { 2166 // Note that we'd collect profile data in this method if we wanted it. 2167 compilation()->set_would_profile(true); 2168 2169 if (profile_checkcasts()) { 2170 i->set_profiled_method(method()); 2171 i->set_profiled_bci(bci()); 2172 i->set_should_profile(true); 2173 } 2174 } 2175 } 2176 2177 2178 void GraphBuilder::monitorenter(Value x, int bci) { 2179 // save state before locking in case of deoptimization after a NullPointerException 2180 ValueStack* state_before = copy_state_for_exception_with_bci(bci); 2181 append_with_bci(new MonitorEnter(x, state()->lock(x), state_before), bci); 2182 kill_all(); 2183 } 2184 2185 2186 void GraphBuilder::monitorexit(Value x, int bci) { 2187 append_with_bci(new MonitorExit(x, state()->unlock()), bci); 2188 kill_all(); 2189 } 2190 2191 2192 void GraphBuilder::new_multi_array(int dimensions) { 2193 bool will_link; 2194 ciKlass* klass = stream()->get_klass(will_link); 2195 ValueStack* state_before = !klass->is_loaded() || PatchALot ? copy_state_before() : copy_state_exhandling(); 2196 2197 Values* dims = new Values(dimensions, dimensions, NULL); 2198 // fill in all dimensions 2199 int i = dimensions; 2200 while (i-- > 0) dims->at_put(i, ipop()); 2201 // create array 2202 NewArray* n = new NewMultiArray(klass, dims, state_before); 2203 apush(append_split(n)); 2204 } 2205 2206 2207 void GraphBuilder::throw_op(int bci) { 2208 // We require that the debug info for a Throw be the "state before" 2209 // the Throw (i.e., exception oop is still on TOS) 2210 ValueStack* state_before = copy_state_before_with_bci(bci); 2211 Throw* t = new Throw(apop(), state_before); 2212 // operand stack not needed after a throw 2213 state()->truncate_stack(0); 2214 append_with_bci(t, bci); 2215 } 2216 2217 2218 Value GraphBuilder::round_fp(Value fp_value) { 2219 // no rounding needed if SSE2 is used 2220 if (RoundFPResults && UseSSE < 2) { 2221 // Must currently insert rounding node for doubleword values that 2222 // are results of expressions (i.e., not loads from memory or 2223 // constants) 2224 if (fp_value->type()->tag() == doubleTag && 2225 fp_value->as_Constant() == NULL && 2226 fp_value->as_Local() == NULL && // method parameters need no rounding 2227 fp_value->as_RoundFP() == NULL) { 2228 return append(new RoundFP(fp_value)); 2229 } 2230 } 2231 return fp_value; 2232 } 2233 2234 2235 Instruction* GraphBuilder::append_with_bci(Instruction* instr, int bci) { 2236 Canonicalizer canon(compilation(), instr, bci); 2237 Instruction* i1 = canon.canonical(); 2238 if (i1->is_linked() || !i1->can_be_linked()) { 2239 // Canonicalizer returned an instruction which was already 2240 // appended so simply return it. 2241 return i1; 2242 } 2243 2244 if (UseLocalValueNumbering) { 2245 // Lookup the instruction in the ValueMap and add it to the map if 2246 // it's not found. 2247 Instruction* i2 = vmap()->find_insert(i1); 2248 if (i2 != i1) { 2249 // found an entry in the value map, so just return it. 2250 assert(i2->is_linked(), "should already be linked"); 2251 return i2; 2252 } 2253 ValueNumberingEffects vne(vmap()); 2254 i1->visit(&vne); 2255 } 2256 2257 // i1 was not eliminated => append it 2258 assert(i1->next() == NULL, "shouldn't already be linked"); 2259 _last = _last->set_next(i1, canon.bci()); 2260 2261 if (++_instruction_count >= InstructionCountCutoff && !bailed_out()) { 2262 // set the bailout state but complete normal processing. We 2263 // might do a little more work before noticing the bailout so we 2264 // want processing to continue normally until it's noticed. 2265 bailout("Method and/or inlining is too large"); 2266 } 2267 2268 #ifndef PRODUCT 2269 if (PrintIRDuringConstruction) { 2270 InstructionPrinter ip; 2271 ip.print_line(i1); 2272 if (Verbose) { 2273 state()->print(); 2274 } 2275 } 2276 #endif 2277 2278 // save state after modification of operand stack for StateSplit instructions 2279 StateSplit* s = i1->as_StateSplit(); 2280 if (s != NULL) { 2281 if (EliminateFieldAccess) { 2282 Intrinsic* intrinsic = s->as_Intrinsic(); 2283 if (s->as_Invoke() != NULL || (intrinsic && !intrinsic->preserves_state())) { 2284 _memory->kill(); 2285 } 2286 } 2287 s->set_state(state()->copy(ValueStack::StateAfter, canon.bci())); 2288 } 2289 2290 // set up exception handlers for this instruction if necessary 2291 if (i1->can_trap()) { 2292 i1->set_exception_handlers(handle_exception(i1)); 2293 assert(i1->exception_state() != NULL || !i1->needs_exception_state() || bailed_out(), "handle_exception must set exception state"); 2294 } 2295 return i1; 2296 } 2297 2298 2299 Instruction* GraphBuilder::append(Instruction* instr) { 2300 assert(instr->as_StateSplit() == NULL || instr->as_BlockEnd() != NULL, "wrong append used"); 2301 return append_with_bci(instr, bci()); 2302 } 2303 2304 2305 Instruction* GraphBuilder::append_split(StateSplit* instr) { 2306 return append_with_bci(instr, bci()); 2307 } 2308 2309 2310 void GraphBuilder::null_check(Value value) { 2311 if (value->as_NewArray() != NULL || value->as_NewInstance() != NULL) { 2312 return; 2313 } else { 2314 Constant* con = value->as_Constant(); 2315 if (con) { 2316 ObjectType* c = con->type()->as_ObjectType(); 2317 if (c && c->is_loaded()) { 2318 ObjectConstant* oc = c->as_ObjectConstant(); 2319 if (!oc || !oc->value()->is_null_object()) { 2320 return; 2321 } 2322 } 2323 } 2324 } 2325 append(new NullCheck(value, copy_state_for_exception())); 2326 } 2327 2328 2329 2330 XHandlers* GraphBuilder::handle_exception(Instruction* instruction) { 2331 if (!has_handler() && (!instruction->needs_exception_state() || instruction->exception_state() != NULL)) { 2332 assert(instruction->exception_state() == NULL 2333 || instruction->exception_state()->kind() == ValueStack::EmptyExceptionState 2334 || (instruction->exception_state()->kind() == ValueStack::ExceptionState && _compilation->env()->should_retain_local_variables()), 2335 "exception_state should be of exception kind"); 2336 return new XHandlers(); 2337 } 2338 2339 XHandlers* exception_handlers = new XHandlers(); 2340 ScopeData* cur_scope_data = scope_data(); 2341 ValueStack* cur_state = instruction->state_before(); 2342 ValueStack* prev_state = NULL; 2343 int scope_count = 0; 2344 2345 assert(cur_state != NULL, "state_before must be set"); 2346 do { 2347 int cur_bci = cur_state->bci(); 2348 assert(cur_scope_data->scope() == cur_state->scope(), "scopes do not match"); 2349 assert(cur_bci == SynchronizationEntryBCI || cur_bci == cur_scope_data->stream()->cur_bci(), "invalid bci"); 2350 2351 // join with all potential exception handlers 2352 XHandlers* list = cur_scope_data->xhandlers(); 2353 const int n = list->length(); 2354 for (int i = 0; i < n; i++) { 2355 XHandler* h = list->handler_at(i); 2356 if (h->covers(cur_bci)) { 2357 // h is a potential exception handler => join it 2358 compilation()->set_has_exception_handlers(true); 2359 2360 BlockBegin* entry = h->entry_block(); 2361 if (entry == block()) { 2362 // It's acceptable for an exception handler to cover itself 2363 // but we don't handle that in the parser currently. It's 2364 // very rare so we bailout instead of trying to handle it. 2365 BAILOUT_("exception handler covers itself", exception_handlers); 2366 } 2367 assert(entry->bci() == h->handler_bci(), "must match"); 2368 assert(entry->bci() == -1 || entry == cur_scope_data->block_at(entry->bci()), "blocks must correspond"); 2369 2370 // previously this was a BAILOUT, but this is not necessary 2371 // now because asynchronous exceptions are not handled this way. 2372 assert(entry->state() == NULL || cur_state->total_locks_size() == entry->state()->total_locks_size(), "locks do not match"); 2373 2374 // xhandler start with an empty expression stack 2375 if (cur_state->stack_size() != 0) { 2376 cur_state = cur_state->copy(ValueStack::ExceptionState, cur_state->bci()); 2377 } 2378 if (instruction->exception_state() == NULL) { 2379 instruction->set_exception_state(cur_state); 2380 } 2381 2382 // Note: Usually this join must work. However, very 2383 // complicated jsr-ret structures where we don't ret from 2384 // the subroutine can cause the objects on the monitor 2385 // stacks to not match because blocks can be parsed twice. 2386 // The only test case we've seen so far which exhibits this 2387 // problem is caught by the infinite recursion test in 2388 // GraphBuilder::jsr() if the join doesn't work. 2389 if (!entry->try_merge(cur_state)) { 2390 BAILOUT_("error while joining with exception handler, prob. due to complicated jsr/rets", exception_handlers); 2391 } 2392 2393 // add current state for correct handling of phi functions at begin of xhandler 2394 int phi_operand = entry->add_exception_state(cur_state); 2395 2396 // add entry to the list of xhandlers of this block 2397 _block->add_exception_handler(entry); 2398 2399 // add back-edge from xhandler entry to this block 2400 if (!entry->is_predecessor(_block)) { 2401 entry->add_predecessor(_block); 2402 } 2403 2404 // clone XHandler because phi_operand and scope_count can not be shared 2405 XHandler* new_xhandler = new XHandler(h); 2406 new_xhandler->set_phi_operand(phi_operand); 2407 new_xhandler->set_scope_count(scope_count); 2408 exception_handlers->append(new_xhandler); 2409 2410 // fill in exception handler subgraph lazily 2411 assert(!entry->is_set(BlockBegin::was_visited_flag), "entry must not be visited yet"); 2412 cur_scope_data->add_to_work_list(entry); 2413 2414 // stop when reaching catchall 2415 if (h->catch_type() == 0) { 2416 return exception_handlers; 2417 } 2418 } 2419 } 2420 2421 if (exception_handlers->length() == 0) { 2422 // This scope and all callees do not handle exceptions, so the local 2423 // variables of this scope are not needed. However, the scope itself is 2424 // required for a correct exception stack trace -> clear out the locals. 2425 if (_compilation->env()->should_retain_local_variables()) { 2426 cur_state = cur_state->copy(ValueStack::ExceptionState, cur_state->bci()); 2427 } else { 2428 cur_state = cur_state->copy(ValueStack::EmptyExceptionState, cur_state->bci()); 2429 } 2430 if (prev_state != NULL) { 2431 prev_state->set_caller_state(cur_state); 2432 } 2433 if (instruction->exception_state() == NULL) { 2434 instruction->set_exception_state(cur_state); 2435 } 2436 } 2437 2438 // Set up iteration for next time. 2439 // If parsing a jsr, do not grab exception handlers from the 2440 // parent scopes for this method (already got them, and they 2441 // needed to be cloned) 2442 2443 while (cur_scope_data->parsing_jsr()) { 2444 cur_scope_data = cur_scope_data->parent(); 2445 } 2446 2447 assert(cur_scope_data->scope() == cur_state->scope(), "scopes do not match"); 2448 assert(cur_state->locks_size() == 0 || cur_state->locks_size() == 1, "unlocking must be done in a catchall exception handler"); 2449 2450 prev_state = cur_state; 2451 cur_state = cur_state->caller_state(); 2452 cur_scope_data = cur_scope_data->parent(); 2453 scope_count++; 2454 } while (cur_scope_data != NULL); 2455 2456 return exception_handlers; 2457 } 2458 2459 2460 // Helper class for simplifying Phis. 2461 class PhiSimplifier : public BlockClosure { 2462 private: 2463 bool _has_substitutions; 2464 Value simplify(Value v); 2465 2466 public: 2467 PhiSimplifier(BlockBegin* start) : _has_substitutions(false) { 2468 start->iterate_preorder(this); 2469 if (_has_substitutions) { 2470 SubstitutionResolver sr(start); 2471 } 2472 } 2473 void block_do(BlockBegin* b); 2474 bool has_substitutions() const { return _has_substitutions; } 2475 }; 2476 2477 2478 Value PhiSimplifier::simplify(Value v) { 2479 Phi* phi = v->as_Phi(); 2480 2481 if (phi == NULL) { 2482 // no phi function 2483 return v; 2484 } else if (v->has_subst()) { 2485 // already substituted; subst can be phi itself -> simplify 2486 return simplify(v->subst()); 2487 } else if (phi->is_set(Phi::cannot_simplify)) { 2488 // already tried to simplify phi before 2489 return phi; 2490 } else if (phi->is_set(Phi::visited)) { 2491 // break cycles in phi functions 2492 return phi; 2493 } else if (phi->type()->is_illegal()) { 2494 // illegal phi functions are ignored anyway 2495 return phi; 2496 2497 } else { 2498 // mark phi function as processed to break cycles in phi functions 2499 phi->set(Phi::visited); 2500 2501 // simplify x = [y, x] and x = [y, y] to y 2502 Value subst = NULL; 2503 int opd_count = phi->operand_count(); 2504 for (int i = 0; i < opd_count; i++) { 2505 Value opd = phi->operand_at(i); 2506 assert(opd != NULL, "Operand must exist!"); 2507 2508 if (opd->type()->is_illegal()) { 2509 // if one operand is illegal, the entire phi function is illegal 2510 phi->make_illegal(); 2511 phi->clear(Phi::visited); 2512 return phi; 2513 } 2514 2515 Value new_opd = simplify(opd); 2516 assert(new_opd != NULL, "Simplified operand must exist!"); 2517 2518 if (new_opd != phi && new_opd != subst) { 2519 if (subst == NULL) { 2520 subst = new_opd; 2521 } else { 2522 // no simplification possible 2523 phi->set(Phi::cannot_simplify); 2524 phi->clear(Phi::visited); 2525 return phi; 2526 } 2527 } 2528 } 2529 2530 // sucessfully simplified phi function 2531 assert(subst != NULL, "illegal phi function"); 2532 _has_substitutions = true; 2533 phi->clear(Phi::visited); 2534 phi->set_subst(subst); 2535 2536 #ifndef PRODUCT 2537 if (PrintPhiFunctions) { 2538 tty->print_cr("simplified phi function %c%d to %c%d (Block B%d)", phi->type()->tchar(), phi->id(), subst->type()->tchar(), subst->id(), phi->block()->block_id()); 2539 } 2540 #endif 2541 2542 return subst; 2543 } 2544 } 2545 2546 2547 void PhiSimplifier::block_do(BlockBegin* b) { 2548 for_each_phi_fun(b, phi, 2549 simplify(phi); 2550 ); 2551 2552 #ifdef ASSERT 2553 for_each_phi_fun(b, phi, 2554 assert(phi->operand_count() != 1 || phi->subst() != phi, "missed trivial simplification"); 2555 ); 2556 2557 ValueStack* state = b->state()->caller_state(); 2558 for_each_state_value(state, value, 2559 Phi* phi = value->as_Phi(); 2560 assert(phi == NULL || phi->block() != b, "must not have phi function to simplify in caller state"); 2561 ); 2562 #endif 2563 } 2564 2565 // This method is called after all blocks are filled with HIR instructions 2566 // It eliminates all Phi functions of the form x = [y, y] and x = [y, x] 2567 void GraphBuilder::eliminate_redundant_phis(BlockBegin* start) { 2568 PhiSimplifier simplifier(start); 2569 } 2570 2571 2572 void GraphBuilder::connect_to_end(BlockBegin* beg) { 2573 // setup iteration 2574 kill_all(); 2575 _block = beg; 2576 _state = beg->state()->copy_for_parsing(); 2577 _last = beg; 2578 iterate_bytecodes_for_block(beg->bci()); 2579 } 2580 2581 2582 BlockEnd* GraphBuilder::iterate_bytecodes_for_block(int bci) { 2583 #ifndef PRODUCT 2584 if (PrintIRDuringConstruction) { 2585 tty->cr(); 2586 InstructionPrinter ip; 2587 ip.print_instr(_block); tty->cr(); 2588 ip.print_stack(_block->state()); tty->cr(); 2589 ip.print_inline_level(_block); 2590 ip.print_head(); 2591 tty->print_cr("locals size: %d stack size: %d", state()->locals_size(), state()->stack_size()); 2592 } 2593 #endif 2594 _skip_block = false; 2595 assert(state() != NULL, "ValueStack missing!"); 2596 CompileLog* log = compilation()->log(); 2597 ciBytecodeStream s(method()); 2598 s.reset_to_bci(bci); 2599 int prev_bci = bci; 2600 scope_data()->set_stream(&s); 2601 // iterate 2602 Bytecodes::Code code = Bytecodes::_illegal; 2603 bool push_exception = false; 2604 2605 if (block()->is_set(BlockBegin::exception_entry_flag) && block()->next() == NULL) { 2606 // first thing in the exception entry block should be the exception object. 2607 push_exception = true; 2608 } 2609 2610 while (!bailed_out() && last()->as_BlockEnd() == NULL && 2611 (code = stream()->next()) != ciBytecodeStream::EOBC() && 2612 (block_at(s.cur_bci()) == NULL || block_at(s.cur_bci()) == block())) { 2613 assert(state()->kind() == ValueStack::Parsing, "invalid state kind"); 2614 2615 if (log != NULL) 2616 log->set_context("bc code='%d' bci='%d'", (int)code, s.cur_bci()); 2617 2618 // Check for active jsr during OSR compilation 2619 if (compilation()->is_osr_compile() 2620 && scope()->is_top_scope() 2621 && parsing_jsr() 2622 && s.cur_bci() == compilation()->osr_bci()) { 2623 bailout("OSR not supported while a jsr is active"); 2624 } 2625 2626 if (push_exception) { 2627 apush(append(new ExceptionObject())); 2628 push_exception = false; 2629 } 2630 2631 // handle bytecode 2632 switch (code) { 2633 case Bytecodes::_nop : /* nothing to do */ break; 2634 case Bytecodes::_aconst_null : apush(append(new Constant(objectNull ))); break; 2635 case Bytecodes::_iconst_m1 : ipush(append(new Constant(new IntConstant (-1)))); break; 2636 case Bytecodes::_iconst_0 : ipush(append(new Constant(intZero ))); break; 2637 case Bytecodes::_iconst_1 : ipush(append(new Constant(intOne ))); break; 2638 case Bytecodes::_iconst_2 : ipush(append(new Constant(new IntConstant ( 2)))); break; 2639 case Bytecodes::_iconst_3 : ipush(append(new Constant(new IntConstant ( 3)))); break; 2640 case Bytecodes::_iconst_4 : ipush(append(new Constant(new IntConstant ( 4)))); break; 2641 case Bytecodes::_iconst_5 : ipush(append(new Constant(new IntConstant ( 5)))); break; 2642 case Bytecodes::_lconst_0 : lpush(append(new Constant(new LongConstant ( 0)))); break; 2643 case Bytecodes::_lconst_1 : lpush(append(new Constant(new LongConstant ( 1)))); break; 2644 case Bytecodes::_fconst_0 : fpush(append(new Constant(new FloatConstant ( 0)))); break; 2645 case Bytecodes::_fconst_1 : fpush(append(new Constant(new FloatConstant ( 1)))); break; 2646 case Bytecodes::_fconst_2 : fpush(append(new Constant(new FloatConstant ( 2)))); break; 2647 case Bytecodes::_dconst_0 : dpush(append(new Constant(new DoubleConstant( 0)))); break; 2648 case Bytecodes::_dconst_1 : dpush(append(new Constant(new DoubleConstant( 1)))); break; 2649 case Bytecodes::_bipush : ipush(append(new Constant(new IntConstant(((signed char*)s.cur_bcp())[1])))); break; 2650 case Bytecodes::_sipush : ipush(append(new Constant(new IntConstant((short)Bytes::get_Java_u2(s.cur_bcp()+1))))); break; 2651 case Bytecodes::_ldc : // fall through 2652 case Bytecodes::_ldc_w : // fall through 2653 case Bytecodes::_ldc2_w : load_constant(); break; 2654 case Bytecodes::_iload : load_local(intType , s.get_index()); break; 2655 case Bytecodes::_lload : load_local(longType , s.get_index()); break; 2656 case Bytecodes::_fload : load_local(floatType , s.get_index()); break; 2657 case Bytecodes::_dload : load_local(doubleType , s.get_index()); break; 2658 case Bytecodes::_aload : load_local(instanceType, s.get_index()); break; 2659 case Bytecodes::_iload_0 : load_local(intType , 0); break; 2660 case Bytecodes::_iload_1 : load_local(intType , 1); break; 2661 case Bytecodes::_iload_2 : load_local(intType , 2); break; 2662 case Bytecodes::_iload_3 : load_local(intType , 3); break; 2663 case Bytecodes::_lload_0 : load_local(longType , 0); break; 2664 case Bytecodes::_lload_1 : load_local(longType , 1); break; 2665 case Bytecodes::_lload_2 : load_local(longType , 2); break; 2666 case Bytecodes::_lload_3 : load_local(longType , 3); break; 2667 case Bytecodes::_fload_0 : load_local(floatType , 0); break; 2668 case Bytecodes::_fload_1 : load_local(floatType , 1); break; 2669 case Bytecodes::_fload_2 : load_local(floatType , 2); break; 2670 case Bytecodes::_fload_3 : load_local(floatType , 3); break; 2671 case Bytecodes::_dload_0 : load_local(doubleType, 0); break; 2672 case Bytecodes::_dload_1 : load_local(doubleType, 1); break; 2673 case Bytecodes::_dload_2 : load_local(doubleType, 2); break; 2674 case Bytecodes::_dload_3 : load_local(doubleType, 3); break; 2675 case Bytecodes::_aload_0 : load_local(objectType, 0); break; 2676 case Bytecodes::_aload_1 : load_local(objectType, 1); break; 2677 case Bytecodes::_aload_2 : load_local(objectType, 2); break; 2678 case Bytecodes::_aload_3 : load_local(objectType, 3); break; 2679 case Bytecodes::_iaload : load_indexed(T_INT ); break; 2680 case Bytecodes::_laload : load_indexed(T_LONG ); break; 2681 case Bytecodes::_faload : load_indexed(T_FLOAT ); break; 2682 case Bytecodes::_daload : load_indexed(T_DOUBLE); break; 2683 case Bytecodes::_aaload : load_indexed(T_OBJECT); break; 2684 case Bytecodes::_baload : load_indexed(T_BYTE ); break; 2685 case Bytecodes::_caload : load_indexed(T_CHAR ); break; 2686 case Bytecodes::_saload : load_indexed(T_SHORT ); break; 2687 case Bytecodes::_istore : store_local(intType , s.get_index()); break; 2688 case Bytecodes::_lstore : store_local(longType , s.get_index()); break; 2689 case Bytecodes::_fstore : store_local(floatType , s.get_index()); break; 2690 case Bytecodes::_dstore : store_local(doubleType, s.get_index()); break; 2691 case Bytecodes::_astore : store_local(objectType, s.get_index()); break; 2692 case Bytecodes::_istore_0 : store_local(intType , 0); break; 2693 case Bytecodes::_istore_1 : store_local(intType , 1); break; 2694 case Bytecodes::_istore_2 : store_local(intType , 2); break; 2695 case Bytecodes::_istore_3 : store_local(intType , 3); break; 2696 case Bytecodes::_lstore_0 : store_local(longType , 0); break; 2697 case Bytecodes::_lstore_1 : store_local(longType , 1); break; 2698 case Bytecodes::_lstore_2 : store_local(longType , 2); break; 2699 case Bytecodes::_lstore_3 : store_local(longType , 3); break; 2700 case Bytecodes::_fstore_0 : store_local(floatType , 0); break; 2701 case Bytecodes::_fstore_1 : store_local(floatType , 1); break; 2702 case Bytecodes::_fstore_2 : store_local(floatType , 2); break; 2703 case Bytecodes::_fstore_3 : store_local(floatType , 3); break; 2704 case Bytecodes::_dstore_0 : store_local(doubleType, 0); break; 2705 case Bytecodes::_dstore_1 : store_local(doubleType, 1); break; 2706 case Bytecodes::_dstore_2 : store_local(doubleType, 2); break; 2707 case Bytecodes::_dstore_3 : store_local(doubleType, 3); break; 2708 case Bytecodes::_astore_0 : store_local(objectType, 0); break; 2709 case Bytecodes::_astore_1 : store_local(objectType, 1); break; 2710 case Bytecodes::_astore_2 : store_local(objectType, 2); break; 2711 case Bytecodes::_astore_3 : store_local(objectType, 3); break; 2712 case Bytecodes::_iastore : store_indexed(T_INT ); break; 2713 case Bytecodes::_lastore : store_indexed(T_LONG ); break; 2714 case Bytecodes::_fastore : store_indexed(T_FLOAT ); break; 2715 case Bytecodes::_dastore : store_indexed(T_DOUBLE); break; 2716 case Bytecodes::_aastore : store_indexed(T_OBJECT); break; 2717 case Bytecodes::_bastore : store_indexed(T_BYTE ); break; 2718 case Bytecodes::_castore : store_indexed(T_CHAR ); break; 2719 case Bytecodes::_sastore : store_indexed(T_SHORT ); break; 2720 case Bytecodes::_pop : // fall through 2721 case Bytecodes::_pop2 : // fall through 2722 case Bytecodes::_dup : // fall through 2723 case Bytecodes::_dup_x1 : // fall through 2724 case Bytecodes::_dup_x2 : // fall through 2725 case Bytecodes::_dup2 : // fall through 2726 case Bytecodes::_dup2_x1 : // fall through 2727 case Bytecodes::_dup2_x2 : // fall through 2728 case Bytecodes::_swap : stack_op(code); break; 2729 case Bytecodes::_iadd : arithmetic_op(intType , code); break; 2730 case Bytecodes::_ladd : arithmetic_op(longType , code); break; 2731 case Bytecodes::_fadd : arithmetic_op(floatType , code); break; 2732 case Bytecodes::_dadd : arithmetic_op(doubleType, code); break; 2733 case Bytecodes::_isub : arithmetic_op(intType , code); break; 2734 case Bytecodes::_lsub : arithmetic_op(longType , code); break; 2735 case Bytecodes::_fsub : arithmetic_op(floatType , code); break; 2736 case Bytecodes::_dsub : arithmetic_op(doubleType, code); break; 2737 case Bytecodes::_imul : arithmetic_op(intType , code); break; 2738 case Bytecodes::_lmul : arithmetic_op(longType , code); break; 2739 case Bytecodes::_fmul : arithmetic_op(floatType , code); break; 2740 case Bytecodes::_dmul : arithmetic_op(doubleType, code); break; 2741 case Bytecodes::_idiv : arithmetic_op(intType , code, copy_state_for_exception()); break; 2742 case Bytecodes::_ldiv : arithmetic_op(longType , code, copy_state_for_exception()); break; 2743 case Bytecodes::_fdiv : arithmetic_op(floatType , code); break; 2744 case Bytecodes::_ddiv : arithmetic_op(doubleType, code); break; 2745 case Bytecodes::_irem : arithmetic_op(intType , code, copy_state_for_exception()); break; 2746 case Bytecodes::_lrem : arithmetic_op(longType , code, copy_state_for_exception()); break; 2747 case Bytecodes::_frem : arithmetic_op(floatType , code); break; 2748 case Bytecodes::_drem : arithmetic_op(doubleType, code); break; 2749 case Bytecodes::_ineg : negate_op(intType ); break; 2750 case Bytecodes::_lneg : negate_op(longType ); break; 2751 case Bytecodes::_fneg : negate_op(floatType ); break; 2752 case Bytecodes::_dneg : negate_op(doubleType); break; 2753 case Bytecodes::_ishl : shift_op(intType , code); break; 2754 case Bytecodes::_lshl : shift_op(longType, code); break; 2755 case Bytecodes::_ishr : shift_op(intType , code); break; 2756 case Bytecodes::_lshr : shift_op(longType, code); break; 2757 case Bytecodes::_iushr : shift_op(intType , code); break; 2758 case Bytecodes::_lushr : shift_op(longType, code); break; 2759 case Bytecodes::_iand : logic_op(intType , code); break; 2760 case Bytecodes::_land : logic_op(longType, code); break; 2761 case Bytecodes::_ior : logic_op(intType , code); break; 2762 case Bytecodes::_lor : logic_op(longType, code); break; 2763 case Bytecodes::_ixor : logic_op(intType , code); break; 2764 case Bytecodes::_lxor : logic_op(longType, code); break; 2765 case Bytecodes::_iinc : increment(); break; 2766 case Bytecodes::_i2l : convert(code, T_INT , T_LONG ); break; 2767 case Bytecodes::_i2f : convert(code, T_INT , T_FLOAT ); break; 2768 case Bytecodes::_i2d : convert(code, T_INT , T_DOUBLE); break; 2769 case Bytecodes::_l2i : convert(code, T_LONG , T_INT ); break; 2770 case Bytecodes::_l2f : convert(code, T_LONG , T_FLOAT ); break; 2771 case Bytecodes::_l2d : convert(code, T_LONG , T_DOUBLE); break; 2772 case Bytecodes::_f2i : convert(code, T_FLOAT , T_INT ); break; 2773 case Bytecodes::_f2l : convert(code, T_FLOAT , T_LONG ); break; 2774 case Bytecodes::_f2d : convert(code, T_FLOAT , T_DOUBLE); break; 2775 case Bytecodes::_d2i : convert(code, T_DOUBLE, T_INT ); break; 2776 case Bytecodes::_d2l : convert(code, T_DOUBLE, T_LONG ); break; 2777 case Bytecodes::_d2f : convert(code, T_DOUBLE, T_FLOAT ); break; 2778 case Bytecodes::_i2b : convert(code, T_INT , T_BYTE ); break; 2779 case Bytecodes::_i2c : convert(code, T_INT , T_CHAR ); break; 2780 case Bytecodes::_i2s : convert(code, T_INT , T_SHORT ); break; 2781 case Bytecodes::_lcmp : compare_op(longType , code); break; 2782 case Bytecodes::_fcmpl : compare_op(floatType , code); break; 2783 case Bytecodes::_fcmpg : compare_op(floatType , code); break; 2784 case Bytecodes::_dcmpl : compare_op(doubleType, code); break; 2785 case Bytecodes::_dcmpg : compare_op(doubleType, code); break; 2786 case Bytecodes::_ifeq : if_zero(intType , If::eql); break; 2787 case Bytecodes::_ifne : if_zero(intType , If::neq); break; 2788 case Bytecodes::_iflt : if_zero(intType , If::lss); break; 2789 case Bytecodes::_ifge : if_zero(intType , If::geq); break; 2790 case Bytecodes::_ifgt : if_zero(intType , If::gtr); break; 2791 case Bytecodes::_ifle : if_zero(intType , If::leq); break; 2792 case Bytecodes::_if_icmpeq : if_same(intType , If::eql); break; 2793 case Bytecodes::_if_icmpne : if_same(intType , If::neq); break; 2794 case Bytecodes::_if_icmplt : if_same(intType , If::lss); break; 2795 case Bytecodes::_if_icmpge : if_same(intType , If::geq); break; 2796 case Bytecodes::_if_icmpgt : if_same(intType , If::gtr); break; 2797 case Bytecodes::_if_icmple : if_same(intType , If::leq); break; 2798 case Bytecodes::_if_acmpeq : if_same(objectType, If::eql); break; 2799 case Bytecodes::_if_acmpne : if_same(objectType, If::neq); break; 2800 case Bytecodes::_goto : _goto(s.cur_bci(), s.get_dest()); break; 2801 case Bytecodes::_jsr : jsr(s.get_dest()); break; 2802 case Bytecodes::_ret : ret(s.get_index()); break; 2803 case Bytecodes::_tableswitch : table_switch(); break; 2804 case Bytecodes::_lookupswitch : lookup_switch(); break; 2805 case Bytecodes::_ireturn : method_return(ipop()); break; 2806 case Bytecodes::_lreturn : method_return(lpop()); break; 2807 case Bytecodes::_freturn : method_return(fpop()); break; 2808 case Bytecodes::_dreturn : method_return(dpop()); break; 2809 case Bytecodes::_areturn : method_return(apop()); break; 2810 case Bytecodes::_return : method_return(NULL ); break; 2811 case Bytecodes::_getstatic : // fall through 2812 case Bytecodes::_putstatic : // fall through 2813 case Bytecodes::_getfield : // fall through 2814 case Bytecodes::_putfield : access_field(code); break; 2815 case Bytecodes::_invokevirtual : // fall through 2816 case Bytecodes::_invokespecial : // fall through 2817 case Bytecodes::_invokestatic : // fall through 2818 case Bytecodes::_invokedynamic : // fall through 2819 case Bytecodes::_invokeinterface: invoke(code); break; 2820 case Bytecodes::_new : new_instance(s.get_index_u2()); break; 2821 case Bytecodes::_newarray : new_type_array(); break; 2822 case Bytecodes::_anewarray : new_object_array(); break; 2823 case Bytecodes::_arraylength : { ValueStack* state_before = copy_state_for_exception(); ipush(append(new ArrayLength(apop(), state_before))); break; } 2824 case Bytecodes::_athrow : throw_op(s.cur_bci()); break; 2825 case Bytecodes::_checkcast : check_cast(s.get_index_u2()); break; 2826 case Bytecodes::_instanceof : instance_of(s.get_index_u2()); break; 2827 case Bytecodes::_monitorenter : monitorenter(apop(), s.cur_bci()); break; 2828 case Bytecodes::_monitorexit : monitorexit (apop(), s.cur_bci()); break; 2829 case Bytecodes::_wide : ShouldNotReachHere(); break; 2830 case Bytecodes::_multianewarray : new_multi_array(s.cur_bcp()[3]); break; 2831 case Bytecodes::_ifnull : if_null(objectType, If::eql); break; 2832 case Bytecodes::_ifnonnull : if_null(objectType, If::neq); break; 2833 case Bytecodes::_goto_w : _goto(s.cur_bci(), s.get_far_dest()); break; 2834 case Bytecodes::_jsr_w : jsr(s.get_far_dest()); break; 2835 case Bytecodes::_breakpoint : BAILOUT_("concurrent setting of breakpoint", NULL); 2836 default : ShouldNotReachHere(); break; 2837 } 2838 2839 if (log != NULL) 2840 log->clear_context(); // skip marker if nothing was printed 2841 2842 // save current bci to setup Goto at the end 2843 prev_bci = s.cur_bci(); 2844 2845 } 2846 CHECK_BAILOUT_(NULL); 2847 // stop processing of this block (see try_inline_full) 2848 if (_skip_block) { 2849 _skip_block = false; 2850 assert(_last && _last->as_BlockEnd(), ""); 2851 return _last->as_BlockEnd(); 2852 } 2853 // if there are any, check if last instruction is a BlockEnd instruction 2854 BlockEnd* end = last()->as_BlockEnd(); 2855 if (end == NULL) { 2856 // all blocks must end with a BlockEnd instruction => add a Goto 2857 end = new Goto(block_at(s.cur_bci()), false); 2858 append(end); 2859 } 2860 assert(end == last()->as_BlockEnd(), "inconsistency"); 2861 2862 assert(end->state() != NULL, "state must already be present"); 2863 assert(end->as_Return() == NULL || end->as_Throw() == NULL || end->state()->stack_size() == 0, "stack not needed for return and throw"); 2864 2865 // connect to begin & set state 2866 // NOTE that inlining may have changed the block we are parsing 2867 block()->set_end(end); 2868 // propagate state 2869 for (int i = end->number_of_sux() - 1; i >= 0; i--) { 2870 BlockBegin* sux = end->sux_at(i); 2871 assert(sux->is_predecessor(block()), "predecessor missing"); 2872 // be careful, bailout if bytecodes are strange 2873 if (!sux->try_merge(end->state())) BAILOUT_("block join failed", NULL); 2874 scope_data()->add_to_work_list(end->sux_at(i)); 2875 } 2876 2877 scope_data()->set_stream(NULL); 2878 2879 // done 2880 return end; 2881 } 2882 2883 2884 void GraphBuilder::iterate_all_blocks(bool start_in_current_block_for_inlining) { 2885 do { 2886 if (start_in_current_block_for_inlining && !bailed_out()) { 2887 iterate_bytecodes_for_block(0); 2888 start_in_current_block_for_inlining = false; 2889 } else { 2890 BlockBegin* b; 2891 while ((b = scope_data()->remove_from_work_list()) != NULL) { 2892 if (!b->is_set(BlockBegin::was_visited_flag)) { 2893 if (b->is_set(BlockBegin::osr_entry_flag)) { 2894 // we're about to parse the osr entry block, so make sure 2895 // we setup the OSR edge leading into this block so that 2896 // Phis get setup correctly. 2897 setup_osr_entry_block(); 2898 // this is no longer the osr entry block, so clear it. 2899 b->clear(BlockBegin::osr_entry_flag); 2900 } 2901 b->set(BlockBegin::was_visited_flag); 2902 connect_to_end(b); 2903 } 2904 } 2905 } 2906 } while (!bailed_out() && !scope_data()->is_work_list_empty()); 2907 } 2908 2909 2910 bool GraphBuilder::_can_trap [Bytecodes::number_of_java_codes]; 2911 2912 void GraphBuilder::initialize() { 2913 // the following bytecodes are assumed to potentially 2914 // throw exceptions in compiled code - note that e.g. 2915 // monitorexit & the return bytecodes do not throw 2916 // exceptions since monitor pairing proved that they 2917 // succeed (if monitor pairing succeeded) 2918 Bytecodes::Code can_trap_list[] = 2919 { Bytecodes::_ldc 2920 , Bytecodes::_ldc_w 2921 , Bytecodes::_ldc2_w 2922 , Bytecodes::_iaload 2923 , Bytecodes::_laload 2924 , Bytecodes::_faload 2925 , Bytecodes::_daload 2926 , Bytecodes::_aaload 2927 , Bytecodes::_baload 2928 , Bytecodes::_caload 2929 , Bytecodes::_saload 2930 , Bytecodes::_iastore 2931 , Bytecodes::_lastore 2932 , Bytecodes::_fastore 2933 , Bytecodes::_dastore 2934 , Bytecodes::_aastore 2935 , Bytecodes::_bastore 2936 , Bytecodes::_castore 2937 , Bytecodes::_sastore 2938 , Bytecodes::_idiv 2939 , Bytecodes::_ldiv 2940 , Bytecodes::_irem 2941 , Bytecodes::_lrem 2942 , Bytecodes::_getstatic 2943 , Bytecodes::_putstatic 2944 , Bytecodes::_getfield 2945 , Bytecodes::_putfield 2946 , Bytecodes::_invokevirtual 2947 , Bytecodes::_invokespecial 2948 , Bytecodes::_invokestatic 2949 , Bytecodes::_invokedynamic 2950 , Bytecodes::_invokeinterface 2951 , Bytecodes::_new 2952 , Bytecodes::_newarray 2953 , Bytecodes::_anewarray 2954 , Bytecodes::_arraylength 2955 , Bytecodes::_athrow 2956 , Bytecodes::_checkcast 2957 , Bytecodes::_instanceof 2958 , Bytecodes::_monitorenter 2959 , Bytecodes::_multianewarray 2960 }; 2961 2962 // inititialize trap tables 2963 for (int i = 0; i < Bytecodes::number_of_java_codes; i++) { 2964 _can_trap[i] = false; 2965 } 2966 // set standard trap info 2967 for (uint j = 0; j < ARRAY_SIZE(can_trap_list); j++) { 2968 _can_trap[can_trap_list[j]] = true; 2969 } 2970 } 2971 2972 2973 BlockBegin* GraphBuilder::header_block(BlockBegin* entry, BlockBegin::Flag f, ValueStack* state) { 2974 assert(entry->is_set(f), "entry/flag mismatch"); 2975 // create header block 2976 BlockBegin* h = new BlockBegin(entry->bci()); 2977 h->set_depth_first_number(0); 2978 2979 Value l = h; 2980 BlockEnd* g = new Goto(entry, false); 2981 l->set_next(g, entry->bci()); 2982 h->set_end(g); 2983 h->set(f); 2984 // setup header block end state 2985 ValueStack* s = state->copy(ValueStack::StateAfter, entry->bci()); // can use copy since stack is empty (=> no phis) 2986 assert(s->stack_is_empty(), "must have empty stack at entry point"); 2987 g->set_state(s); 2988 return h; 2989 } 2990 2991 2992 2993 BlockBegin* GraphBuilder::setup_start_block(int osr_bci, BlockBegin* std_entry, BlockBegin* osr_entry, ValueStack* state) { 2994 BlockBegin* start = new BlockBegin(0); 2995 2996 // This code eliminates the empty start block at the beginning of 2997 // each method. Previously, each method started with the 2998 // start-block created below, and this block was followed by the 2999 // header block that was always empty. This header block is only 3000 // necesary if std_entry is also a backward branch target because 3001 // then phi functions may be necessary in the header block. It's 3002 // also necessary when profiling so that there's a single block that 3003 // can increment the interpreter_invocation_count. 3004 BlockBegin* new_header_block; 3005 if (std_entry->number_of_preds() > 0 || count_invocations() || count_backedges()) { 3006 new_header_block = header_block(std_entry, BlockBegin::std_entry_flag, state); 3007 } else { 3008 new_header_block = std_entry; 3009 } 3010 3011 // setup start block (root for the IR graph) 3012 Base* base = 3013 new Base( 3014 new_header_block, 3015 osr_entry 3016 ); 3017 start->set_next(base, 0); 3018 start->set_end(base); 3019 // create & setup state for start block 3020 start->set_state(state->copy(ValueStack::StateAfter, std_entry->bci())); 3021 base->set_state(state->copy(ValueStack::StateAfter, std_entry->bci())); 3022 3023 if (base->std_entry()->state() == NULL) { 3024 // setup states for header blocks 3025 base->std_entry()->merge(state); 3026 } 3027 3028 assert(base->std_entry()->state() != NULL, ""); 3029 return start; 3030 } 3031 3032 3033 void GraphBuilder::setup_osr_entry_block() { 3034 assert(compilation()->is_osr_compile(), "only for osrs"); 3035 3036 int osr_bci = compilation()->osr_bci(); 3037 ciBytecodeStream s(method()); 3038 s.reset_to_bci(osr_bci); 3039 s.next(); 3040 scope_data()->set_stream(&s); 3041 3042 // create a new block to be the osr setup code 3043 _osr_entry = new BlockBegin(osr_bci); 3044 _osr_entry->set(BlockBegin::osr_entry_flag); 3045 _osr_entry->set_depth_first_number(0); 3046 BlockBegin* target = bci2block()->at(osr_bci); 3047 assert(target != NULL && target->is_set(BlockBegin::osr_entry_flag), "must be there"); 3048 // the osr entry has no values for locals 3049 ValueStack* state = target->state()->copy(); 3050 _osr_entry->set_state(state); 3051 3052 kill_all(); 3053 _block = _osr_entry; 3054 _state = _osr_entry->state()->copy(); 3055 assert(_state->bci() == osr_bci, "mismatch"); 3056 _last = _osr_entry; 3057 Value e = append(new OsrEntry()); 3058 e->set_needs_null_check(false); 3059 3060 // OSR buffer is 3061 // 3062 // locals[nlocals-1..0] 3063 // monitors[number_of_locks-1..0] 3064 // 3065 // locals is a direct copy of the interpreter frame so in the osr buffer 3066 // so first slot in the local array is the last local from the interpreter 3067 // and last slot is local[0] (receiver) from the interpreter 3068 // 3069 // Similarly with locks. The first lock slot in the osr buffer is the nth lock 3070 // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock 3071 // in the interpreter frame (the method lock if a sync method) 3072 3073 // Initialize monitors in the compiled activation. 3074 3075 int index; 3076 Value local; 3077 3078 // find all the locals that the interpreter thinks contain live oops 3079 const BitMap live_oops = method()->live_local_oops_at_bci(osr_bci); 3080 3081 // compute the offset into the locals so that we can treat the buffer 3082 // as if the locals were still in the interpreter frame 3083 int locals_offset = BytesPerWord * (method()->max_locals() - 1); 3084 for_each_local_value(state, index, local) { 3085 int offset = locals_offset - (index + local->type()->size() - 1) * BytesPerWord; 3086 Value get; 3087 if (local->type()->is_object_kind() && !live_oops.at(index)) { 3088 // The interpreter thinks this local is dead but the compiler 3089 // doesn't so pretend that the interpreter passed in null. 3090 get = append(new Constant(objectNull)); 3091 } else { 3092 get = append(new UnsafeGetRaw(as_BasicType(local->type()), e, 3093 append(new Constant(new IntConstant(offset))), 3094 0, 3095 true /*unaligned*/, true /*wide*/)); 3096 } 3097 _state->store_local(index, get); 3098 } 3099 3100 // the storage for the OSR buffer is freed manually in the LIRGenerator. 3101 3102 assert(state->caller_state() == NULL, "should be top scope"); 3103 state->clear_locals(); 3104 Goto* g = new Goto(target, false); 3105 append(g); 3106 _osr_entry->set_end(g); 3107 target->merge(_osr_entry->end()->state()); 3108 3109 scope_data()->set_stream(NULL); 3110 } 3111 3112 3113 ValueStack* GraphBuilder::state_at_entry() { 3114 ValueStack* state = new ValueStack(scope(), NULL); 3115 3116 // Set up locals for receiver 3117 int idx = 0; 3118 if (!method()->is_static()) { 3119 // we should always see the receiver 3120 state->store_local(idx, new Local(method()->holder(), objectType, idx, true)); 3121 idx = 1; 3122 } 3123 3124 // Set up locals for incoming arguments 3125 ciSignature* sig = method()->signature(); 3126 for (int i = 0; i < sig->count(); i++) { 3127 ciType* type = sig->type_at(i); 3128 BasicType basic_type = type->basic_type(); 3129 // don't allow T_ARRAY to propagate into locals types 3130 if (basic_type == T_ARRAY) basic_type = T_OBJECT; 3131 ValueType* vt = as_ValueType(basic_type); 3132 state->store_local(idx, new Local(type, vt, idx, false)); 3133 idx += type->size(); 3134 } 3135 3136 // lock synchronized method 3137 if (method()->is_synchronized()) { 3138 state->lock(NULL); 3139 } 3140 3141 return state; 3142 } 3143 3144 3145 GraphBuilder::GraphBuilder(Compilation* compilation, IRScope* scope) 3146 : _scope_data(NULL) 3147 , _instruction_count(0) 3148 , _osr_entry(NULL) 3149 , _memory(new MemoryBuffer()) 3150 , _compilation(compilation) 3151 , _inline_bailout_msg(NULL) 3152 { 3153 int osr_bci = compilation->osr_bci(); 3154 3155 // determine entry points and bci2block mapping 3156 BlockListBuilder blm(compilation, scope, osr_bci); 3157 CHECK_BAILOUT(); 3158 3159 BlockList* bci2block = blm.bci2block(); 3160 BlockBegin* start_block = bci2block->at(0); 3161 3162 push_root_scope(scope, bci2block, start_block); 3163 3164 // setup state for std entry 3165 _initial_state = state_at_entry(); 3166 start_block->merge(_initial_state); 3167 3168 // complete graph 3169 _vmap = new ValueMap(); 3170 switch (scope->method()->intrinsic_id()) { 3171 case vmIntrinsics::_dabs : // fall through 3172 case vmIntrinsics::_dsqrt : // fall through 3173 case vmIntrinsics::_dsin : // fall through 3174 case vmIntrinsics::_dcos : // fall through 3175 case vmIntrinsics::_dtan : // fall through 3176 case vmIntrinsics::_dlog : // fall through 3177 case vmIntrinsics::_dlog10 : // fall through 3178 case vmIntrinsics::_dexp : // fall through 3179 case vmIntrinsics::_dpow : // fall through 3180 { 3181 // Compiles where the root method is an intrinsic need a special 3182 // compilation environment because the bytecodes for the method 3183 // shouldn't be parsed during the compilation, only the special 3184 // Intrinsic node should be emitted. If this isn't done the the 3185 // code for the inlined version will be different than the root 3186 // compiled version which could lead to monotonicity problems on 3187 // intel. 3188 if (CheckIntrinsics && !scope->method()->intrinsic_candidate()) { 3189 BAILOUT("failed to inline intrinsic, method not annotated"); 3190 } 3191 3192 // Set up a stream so that appending instructions works properly. 3193 ciBytecodeStream s(scope->method()); 3194 s.reset_to_bci(0); 3195 scope_data()->set_stream(&s); 3196 s.next(); 3197 3198 // setup the initial block state 3199 _block = start_block; 3200 _state = start_block->state()->copy_for_parsing(); 3201 _last = start_block; 3202 load_local(doubleType, 0); 3203 if (scope->method()->intrinsic_id() == vmIntrinsics::_dpow) { 3204 load_local(doubleType, 2); 3205 } 3206 3207 // Emit the intrinsic node. 3208 bool result = try_inline_intrinsics(scope->method()); 3209 if (!result) BAILOUT("failed to inline intrinsic"); 3210 method_return(dpop()); 3211 3212 // connect the begin and end blocks and we're all done. 3213 BlockEnd* end = last()->as_BlockEnd(); 3214 block()->set_end(end); 3215 break; 3216 } 3217 3218 case vmIntrinsics::_Reference_get: 3219 { 3220 { 3221 // With java.lang.ref.reference.get() we must go through the 3222 // intrinsic - when G1 is enabled - even when get() is the root 3223 // method of the compile so that, if necessary, the value in 3224 // the referent field of the reference object gets recorded by 3225 // the pre-barrier code. 3226 // Specifically, if G1 is enabled, the value in the referent 3227 // field is recorded by the G1 SATB pre barrier. This will 3228 // result in the referent being marked live and the reference 3229 // object removed from the list of discovered references during 3230 // reference processing. 3231 if (CheckIntrinsics && !scope->method()->intrinsic_candidate()) { 3232 BAILOUT("failed to inline intrinsic, method not annotated"); 3233 } 3234 3235 // Also we need intrinsic to prevent commoning reads from this field 3236 // across safepoint since GC can change its value. 3237 3238 // Set up a stream so that appending instructions works properly. 3239 ciBytecodeStream s(scope->method()); 3240 s.reset_to_bci(0); 3241 scope_data()->set_stream(&s); 3242 s.next(); 3243 3244 // setup the initial block state 3245 _block = start_block; 3246 _state = start_block->state()->copy_for_parsing(); 3247 _last = start_block; 3248 load_local(objectType, 0); 3249 3250 // Emit the intrinsic node. 3251 bool result = try_inline_intrinsics(scope->method()); 3252 if (!result) BAILOUT("failed to inline intrinsic"); 3253 method_return(apop()); 3254 3255 // connect the begin and end blocks and we're all done. 3256 BlockEnd* end = last()->as_BlockEnd(); 3257 block()->set_end(end); 3258 break; 3259 } 3260 // Otherwise, fall thru 3261 } 3262 3263 default: 3264 scope_data()->add_to_work_list(start_block); 3265 iterate_all_blocks(); 3266 break; 3267 } 3268 CHECK_BAILOUT(); 3269 3270 _start = setup_start_block(osr_bci, start_block, _osr_entry, _initial_state); 3271 3272 eliminate_redundant_phis(_start); 3273 3274 NOT_PRODUCT(if (PrintValueNumbering && Verbose) print_stats()); 3275 // for osr compile, bailout if some requirements are not fulfilled 3276 if (osr_bci != -1) { 3277 BlockBegin* osr_block = blm.bci2block()->at(osr_bci); 3278 assert(osr_block->is_set(BlockBegin::was_visited_flag),"osr entry must have been visited for osr compile"); 3279 3280 // check if osr entry point has empty stack - we cannot handle non-empty stacks at osr entry points 3281 if (!osr_block->state()->stack_is_empty()) { 3282 BAILOUT("stack not empty at OSR entry point"); 3283 } 3284 } 3285 #ifndef PRODUCT 3286 if (PrintCompilation && Verbose) tty->print_cr("Created %d Instructions", _instruction_count); 3287 #endif 3288 } 3289 3290 3291 ValueStack* GraphBuilder::copy_state_before() { 3292 return copy_state_before_with_bci(bci()); 3293 } 3294 3295 ValueStack* GraphBuilder::copy_state_exhandling() { 3296 return copy_state_exhandling_with_bci(bci()); 3297 } 3298 3299 ValueStack* GraphBuilder::copy_state_for_exception() { 3300 return copy_state_for_exception_with_bci(bci()); 3301 } 3302 3303 ValueStack* GraphBuilder::copy_state_before_with_bci(int bci) { 3304 return state()->copy(ValueStack::StateBefore, bci); 3305 } 3306 3307 ValueStack* GraphBuilder::copy_state_exhandling_with_bci(int bci) { 3308 if (!has_handler()) return NULL; 3309 return state()->copy(ValueStack::StateBefore, bci); 3310 } 3311 3312 ValueStack* GraphBuilder::copy_state_for_exception_with_bci(int bci) { 3313 ValueStack* s = copy_state_exhandling_with_bci(bci); 3314 if (s == NULL) { 3315 if (_compilation->env()->should_retain_local_variables()) { 3316 s = state()->copy(ValueStack::ExceptionState, bci); 3317 } else { 3318 s = state()->copy(ValueStack::EmptyExceptionState, bci); 3319 } 3320 } 3321 return s; 3322 } 3323 3324 int GraphBuilder::recursive_inline_level(ciMethod* cur_callee) const { 3325 int recur_level = 0; 3326 for (IRScope* s = scope(); s != NULL; s = s->caller()) { 3327 if (s->method() == cur_callee) { 3328 ++recur_level; 3329 } 3330 } 3331 return recur_level; 3332 } 3333 3334 3335 bool GraphBuilder::try_inline(ciMethod* callee, bool holder_known, Bytecodes::Code bc, Value receiver) { 3336 const char* msg = NULL; 3337 3338 // clear out any existing inline bailout condition 3339 clear_inline_bailout(); 3340 3341 // exclude methods we don't want to inline 3342 msg = should_not_inline(callee); 3343 if (msg != NULL) { 3344 print_inlining(callee, msg, /*success*/ false); 3345 return false; 3346 } 3347 3348 // method handle invokes 3349 if (callee->is_method_handle_intrinsic()) { 3350 if (try_method_handle_inline(callee)) { 3351 if (callee->has_reserved_stack_access()) { 3352 compilation()->set_has_reserved_stack_access(true); 3353 } 3354 return true; 3355 } 3356 return false; 3357 } 3358 3359 // handle intrinsics 3360 if (callee->intrinsic_id() != vmIntrinsics::_none && 3361 (CheckIntrinsics ? callee->intrinsic_candidate() : true)) { 3362 if (try_inline_intrinsics(callee)) { 3363 print_inlining(callee, "intrinsic"); 3364 if (callee->has_reserved_stack_access()) { 3365 compilation()->set_has_reserved_stack_access(true); 3366 } 3367 return true; 3368 } 3369 // try normal inlining 3370 } 3371 3372 // certain methods cannot be parsed at all 3373 msg = check_can_parse(callee); 3374 if (msg != NULL) { 3375 print_inlining(callee, msg, /*success*/ false); 3376 return false; 3377 } 3378 3379 // If bytecode not set use the current one. 3380 if (bc == Bytecodes::_illegal) { 3381 bc = code(); 3382 } 3383 if (try_inline_full(callee, holder_known, bc, receiver)) { 3384 if (callee->has_reserved_stack_access()) { 3385 compilation()->set_has_reserved_stack_access(true); 3386 } 3387 return true; 3388 } 3389 3390 // Entire compilation could fail during try_inline_full call. 3391 // In that case printing inlining decision info is useless. 3392 if (!bailed_out()) 3393 print_inlining(callee, _inline_bailout_msg, /*success*/ false); 3394 3395 return false; 3396 } 3397 3398 3399 const char* GraphBuilder::check_can_parse(ciMethod* callee) const { 3400 // Certain methods cannot be parsed at all: 3401 if ( callee->is_native()) return "native method"; 3402 if ( callee->is_abstract()) return "abstract method"; 3403 if (!callee->can_be_compiled()) return "not compilable (disabled)"; 3404 return NULL; 3405 } 3406 3407 // negative filter: should callee NOT be inlined? returns NULL, ok to inline, or rejection msg 3408 const char* GraphBuilder::should_not_inline(ciMethod* callee) const { 3409 if ( compilation()->directive()->should_not_inline(callee)) return "disallowed by CompileCommand"; 3410 if ( callee->dont_inline()) return "don't inline by annotation"; 3411 return NULL; 3412 } 3413 3414 void GraphBuilder::build_graph_for_intrinsic(ciMethod* callee) { 3415 vmIntrinsics::ID id = callee->intrinsic_id(); 3416 assert(id != vmIntrinsics::_none, "must be a VM intrinsic"); 3417 3418 // Some intrinsics need special IR nodes. 3419 switch(id) { 3420 case vmIntrinsics::_getObject : append_unsafe_get_obj(callee, T_OBJECT, false); return; 3421 case vmIntrinsics::_getBoolean : append_unsafe_get_obj(callee, T_BOOLEAN, false); return; 3422 case vmIntrinsics::_getByte : append_unsafe_get_obj(callee, T_BYTE, false); return; 3423 case vmIntrinsics::_getShort : append_unsafe_get_obj(callee, T_SHORT, false); return; 3424 case vmIntrinsics::_getChar : append_unsafe_get_obj(callee, T_CHAR, false); return; 3425 case vmIntrinsics::_getInt : append_unsafe_get_obj(callee, T_INT, false); return; 3426 case vmIntrinsics::_getLong : append_unsafe_get_obj(callee, T_LONG, false); return; 3427 case vmIntrinsics::_getFloat : append_unsafe_get_obj(callee, T_FLOAT, false); return; 3428 case vmIntrinsics::_getDouble : append_unsafe_get_obj(callee, T_DOUBLE, false); return; 3429 case vmIntrinsics::_putObject : append_unsafe_put_obj(callee, T_OBJECT, false); return; 3430 case vmIntrinsics::_putBoolean : append_unsafe_put_obj(callee, T_BOOLEAN, false); return; 3431 case vmIntrinsics::_putByte : append_unsafe_put_obj(callee, T_BYTE, false); return; 3432 case vmIntrinsics::_putShort : append_unsafe_put_obj(callee, T_SHORT, false); return; 3433 case vmIntrinsics::_putChar : append_unsafe_put_obj(callee, T_CHAR, false); return; 3434 case vmIntrinsics::_putInt : append_unsafe_put_obj(callee, T_INT, false); return; 3435 case vmIntrinsics::_putLong : append_unsafe_put_obj(callee, T_LONG, false); return; 3436 case vmIntrinsics::_putFloat : append_unsafe_put_obj(callee, T_FLOAT, false); return; 3437 case vmIntrinsics::_putDouble : append_unsafe_put_obj(callee, T_DOUBLE, false); return; 3438 case vmIntrinsics::_getShortUnaligned : append_unsafe_get_obj(callee, T_SHORT, false); return; 3439 case vmIntrinsics::_getCharUnaligned : append_unsafe_get_obj(callee, T_CHAR, false); return; 3440 case vmIntrinsics::_getIntUnaligned : append_unsafe_get_obj(callee, T_INT, false); return; 3441 case vmIntrinsics::_getLongUnaligned : append_unsafe_get_obj(callee, T_LONG, false); return; 3442 case vmIntrinsics::_putShortUnaligned : append_unsafe_put_obj(callee, T_SHORT, false); return; 3443 case vmIntrinsics::_putCharUnaligned : append_unsafe_put_obj(callee, T_CHAR, false); return; 3444 case vmIntrinsics::_putIntUnaligned : append_unsafe_put_obj(callee, T_INT, false); return; 3445 case vmIntrinsics::_putLongUnaligned : append_unsafe_put_obj(callee, T_LONG, false); return; 3446 case vmIntrinsics::_getObjectVolatile : append_unsafe_get_obj(callee, T_OBJECT, true); return; 3447 case vmIntrinsics::_getBooleanVolatile : append_unsafe_get_obj(callee, T_BOOLEAN, true); return; 3448 case vmIntrinsics::_getByteVolatile : append_unsafe_get_obj(callee, T_BYTE, true); return; 3449 case vmIntrinsics::_getShortVolatile : append_unsafe_get_obj(callee, T_SHORT, true); return; 3450 case vmIntrinsics::_getCharVolatile : append_unsafe_get_obj(callee, T_CHAR, true); return; 3451 case vmIntrinsics::_getIntVolatile : append_unsafe_get_obj(callee, T_INT, true); return; 3452 case vmIntrinsics::_getLongVolatile : append_unsafe_get_obj(callee, T_LONG, true); return; 3453 case vmIntrinsics::_getFloatVolatile : append_unsafe_get_obj(callee, T_FLOAT, true); return; 3454 case vmIntrinsics::_getDoubleVolatile : append_unsafe_get_obj(callee, T_DOUBLE, true); return; 3455 case vmIntrinsics::_putObjectVolatile : append_unsafe_put_obj(callee, T_OBJECT, true); return; 3456 case vmIntrinsics::_putBooleanVolatile : append_unsafe_put_obj(callee, T_BOOLEAN, true); return; 3457 case vmIntrinsics::_putByteVolatile : append_unsafe_put_obj(callee, T_BYTE, true); return; 3458 case vmIntrinsics::_putShortVolatile : append_unsafe_put_obj(callee, T_SHORT, true); return; 3459 case vmIntrinsics::_putCharVolatile : append_unsafe_put_obj(callee, T_CHAR, true); return; 3460 case vmIntrinsics::_putIntVolatile : append_unsafe_put_obj(callee, T_INT, true); return; 3461 case vmIntrinsics::_putLongVolatile : append_unsafe_put_obj(callee, T_LONG, true); return; 3462 case vmIntrinsics::_putFloatVolatile : append_unsafe_put_obj(callee, T_FLOAT, true); return; 3463 case vmIntrinsics::_putDoubleVolatile : append_unsafe_put_obj(callee, T_DOUBLE, true); return; 3464 case vmIntrinsics::_getByte_raw : append_unsafe_get_raw(callee, T_BYTE ); return; 3465 case vmIntrinsics::_getShort_raw : append_unsafe_get_raw(callee, T_SHORT ); return; 3466 case vmIntrinsics::_getChar_raw : append_unsafe_get_raw(callee, T_CHAR ); return; 3467 case vmIntrinsics::_getInt_raw : append_unsafe_get_raw(callee, T_INT ); return; 3468 case vmIntrinsics::_getLong_raw : append_unsafe_get_raw(callee, T_LONG ); return; 3469 case vmIntrinsics::_getFloat_raw : append_unsafe_get_raw(callee, T_FLOAT ); return; 3470 case vmIntrinsics::_getDouble_raw : append_unsafe_get_raw(callee, T_DOUBLE); return; 3471 case vmIntrinsics::_putByte_raw : append_unsafe_put_raw(callee, T_BYTE ); return; 3472 case vmIntrinsics::_putShort_raw : append_unsafe_put_raw(callee, T_SHORT ); return; 3473 case vmIntrinsics::_putChar_raw : append_unsafe_put_raw(callee, T_CHAR ); return; 3474 case vmIntrinsics::_putInt_raw : append_unsafe_put_raw(callee, T_INT ); return; 3475 case vmIntrinsics::_putLong_raw : append_unsafe_put_raw(callee, T_LONG ); return; 3476 case vmIntrinsics::_putFloat_raw : append_unsafe_put_raw(callee, T_FLOAT ); return; 3477 case vmIntrinsics::_putDouble_raw : append_unsafe_put_raw(callee, T_DOUBLE); return; 3478 case vmIntrinsics::_compareAndSwapLong: 3479 case vmIntrinsics::_compareAndSwapInt: 3480 case vmIntrinsics::_compareAndSwapObject: append_unsafe_CAS(callee); return; 3481 case vmIntrinsics::_getAndAddInt: 3482 case vmIntrinsics::_getAndAddLong : append_unsafe_get_and_set_obj(callee, true); return; 3483 case vmIntrinsics::_getAndSetInt : 3484 case vmIntrinsics::_getAndSetLong : 3485 case vmIntrinsics::_getAndSetObject : append_unsafe_get_and_set_obj(callee, false); return; 3486 case vmIntrinsics::_getCharStringU : append_char_access(callee, false); return; 3487 case vmIntrinsics::_putCharStringU : append_char_access(callee, true); return; 3488 default: 3489 break; 3490 } 3491 3492 // create intrinsic node 3493 const bool has_receiver = !callee->is_static(); 3494 ValueType* result_type = as_ValueType(callee->return_type()); 3495 ValueStack* state_before = copy_state_for_exception(); 3496 3497 Values* args = state()->pop_arguments(callee->arg_size()); 3498 3499 if (is_profiling()) { 3500 // Don't profile in the special case where the root method 3501 // is the intrinsic 3502 if (callee != method()) { 3503 // Note that we'd collect profile data in this method if we wanted it. 3504 compilation()->set_would_profile(true); 3505 if (profile_calls()) { 3506 Value recv = NULL; 3507 if (has_receiver) { 3508 recv = args->at(0); 3509 null_check(recv); 3510 } 3511 profile_call(callee, recv, NULL, collect_args_for_profiling(args, callee, true), true); 3512 } 3513 } 3514 } 3515 3516 Intrinsic* result = new Intrinsic(result_type, callee->intrinsic_id(), 3517 args, has_receiver, state_before, 3518 vmIntrinsics::preserves_state(id), 3519 vmIntrinsics::can_trap(id)); 3520 // append instruction & push result 3521 Value value = append_split(result); 3522 if (result_type != voidType) push(result_type, value); 3523 3524 if (callee != method() && profile_return() && result_type->is_object_kind()) { 3525 profile_return_type(result, callee); 3526 } 3527 } 3528 3529 bool GraphBuilder::try_inline_intrinsics(ciMethod* callee) { 3530 // For calling is_intrinsic_available we need to transition to 3531 // the '_thread_in_vm' state because is_intrinsic_available() 3532 // accesses critical VM-internal data. 3533 bool is_available = false; 3534 { 3535 VM_ENTRY_MARK; 3536 methodHandle mh(THREAD, callee->get_Method()); 3537 is_available = _compilation->compiler()->is_intrinsic_available(mh, _compilation->directive()); 3538 } 3539 3540 if (!is_available) { 3541 if (!InlineNatives) { 3542 // Return false and also set message that the inlining of 3543 // intrinsics has been disabled in general. 3544 INLINE_BAILOUT("intrinsic method inlining disabled"); 3545 } else { 3546 return false; 3547 } 3548 } 3549 build_graph_for_intrinsic(callee); 3550 return true; 3551 } 3552 3553 3554 bool GraphBuilder::try_inline_jsr(int jsr_dest_bci) { 3555 // Introduce a new callee continuation point - all Ret instructions 3556 // will be replaced with Gotos to this point. 3557 BlockBegin* cont = block_at(next_bci()); 3558 assert(cont != NULL, "continuation must exist (BlockListBuilder starts a new block after a jsr"); 3559 3560 // Note: can not assign state to continuation yet, as we have to 3561 // pick up the state from the Ret instructions. 3562 3563 // Push callee scope 3564 push_scope_for_jsr(cont, jsr_dest_bci); 3565 3566 // Temporarily set up bytecode stream so we can append instructions 3567 // (only using the bci of this stream) 3568 scope_data()->set_stream(scope_data()->parent()->stream()); 3569 3570 BlockBegin* jsr_start_block = block_at(jsr_dest_bci); 3571 assert(jsr_start_block != NULL, "jsr start block must exist"); 3572 assert(!jsr_start_block->is_set(BlockBegin::was_visited_flag), "should not have visited jsr yet"); 3573 Goto* goto_sub = new Goto(jsr_start_block, false); 3574 // Must copy state to avoid wrong sharing when parsing bytecodes 3575 assert(jsr_start_block->state() == NULL, "should have fresh jsr starting block"); 3576 jsr_start_block->set_state(copy_state_before_with_bci(jsr_dest_bci)); 3577 append(goto_sub); 3578 _block->set_end(goto_sub); 3579 _last = _block = jsr_start_block; 3580 3581 // Clear out bytecode stream 3582 scope_data()->set_stream(NULL); 3583 3584 scope_data()->add_to_work_list(jsr_start_block); 3585 3586 // Ready to resume parsing in subroutine 3587 iterate_all_blocks(); 3588 3589 // If we bailed out during parsing, return immediately (this is bad news) 3590 CHECK_BAILOUT_(false); 3591 3592 // Detect whether the continuation can actually be reached. If not, 3593 // it has not had state set by the join() operations in 3594 // iterate_bytecodes_for_block()/ret() and we should not touch the 3595 // iteration state. The calling activation of 3596 // iterate_bytecodes_for_block will then complete normally. 3597 if (cont->state() != NULL) { 3598 if (!cont->is_set(BlockBegin::was_visited_flag)) { 3599 // add continuation to work list instead of parsing it immediately 3600 scope_data()->parent()->add_to_work_list(cont); 3601 } 3602 } 3603 3604 assert(jsr_continuation() == cont, "continuation must not have changed"); 3605 assert(!jsr_continuation()->is_set(BlockBegin::was_visited_flag) || 3606 jsr_continuation()->is_set(BlockBegin::parser_loop_header_flag), 3607 "continuation can only be visited in case of backward branches"); 3608 assert(_last && _last->as_BlockEnd(), "block must have end"); 3609 3610 // continuation is in work list, so end iteration of current block 3611 _skip_block = true; 3612 pop_scope_for_jsr(); 3613 3614 return true; 3615 } 3616 3617 3618 // Inline the entry of a synchronized method as a monitor enter and 3619 // register the exception handler which releases the monitor if an 3620 // exception is thrown within the callee. Note that the monitor enter 3621 // cannot throw an exception itself, because the receiver is 3622 // guaranteed to be non-null by the explicit null check at the 3623 // beginning of inlining. 3624 void GraphBuilder::inline_sync_entry(Value lock, BlockBegin* sync_handler) { 3625 assert(lock != NULL && sync_handler != NULL, "lock or handler missing"); 3626 3627 monitorenter(lock, SynchronizationEntryBCI); 3628 assert(_last->as_MonitorEnter() != NULL, "monitor enter expected"); 3629 _last->set_needs_null_check(false); 3630 3631 sync_handler->set(BlockBegin::exception_entry_flag); 3632 sync_handler->set(BlockBegin::is_on_work_list_flag); 3633 3634 ciExceptionHandler* desc = new ciExceptionHandler(method()->holder(), 0, method()->code_size(), -1, 0); 3635 XHandler* h = new XHandler(desc); 3636 h->set_entry_block(sync_handler); 3637 scope_data()->xhandlers()->append(h); 3638 scope_data()->set_has_handler(); 3639 } 3640 3641 3642 // If an exception is thrown and not handled within an inlined 3643 // synchronized method, the monitor must be released before the 3644 // exception is rethrown in the outer scope. Generate the appropriate 3645 // instructions here. 3646 void GraphBuilder::fill_sync_handler(Value lock, BlockBegin* sync_handler, bool default_handler) { 3647 BlockBegin* orig_block = _block; 3648 ValueStack* orig_state = _state; 3649 Instruction* orig_last = _last; 3650 _last = _block = sync_handler; 3651 _state = sync_handler->state()->copy(); 3652 3653 assert(sync_handler != NULL, "handler missing"); 3654 assert(!sync_handler->is_set(BlockBegin::was_visited_flag), "is visited here"); 3655 3656 assert(lock != NULL || default_handler, "lock or handler missing"); 3657 3658 XHandler* h = scope_data()->xhandlers()->remove_last(); 3659 assert(h->entry_block() == sync_handler, "corrupt list of handlers"); 3660 3661 block()->set(BlockBegin::was_visited_flag); 3662 Value exception = append_with_bci(new ExceptionObject(), SynchronizationEntryBCI); 3663 assert(exception->is_pinned(), "must be"); 3664 3665 int bci = SynchronizationEntryBCI; 3666 if (compilation()->env()->dtrace_method_probes()) { 3667 // Report exit from inline methods. We don't have a stream here 3668 // so pass an explicit bci of SynchronizationEntryBCI. 3669 Values* args = new Values(1); 3670 args->push(append_with_bci(new Constant(new MethodConstant(method())), bci)); 3671 append_with_bci(new RuntimeCall(voidType, "dtrace_method_exit", CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), args), bci); 3672 } 3673 3674 if (lock) { 3675 assert(state()->locks_size() > 0 && state()->lock_at(state()->locks_size() - 1) == lock, "lock is missing"); 3676 if (!lock->is_linked()) { 3677 lock = append_with_bci(lock, bci); 3678 } 3679 3680 // exit the monitor in the context of the synchronized method 3681 monitorexit(lock, bci); 3682 3683 // exit the context of the synchronized method 3684 if (!default_handler) { 3685 pop_scope(); 3686 bci = _state->caller_state()->bci(); 3687 _state = _state->caller_state()->copy_for_parsing(); 3688 } 3689 } 3690 3691 // perform the throw as if at the the call site 3692 apush(exception); 3693 throw_op(bci); 3694 3695 BlockEnd* end = last()->as_BlockEnd(); 3696 block()->set_end(end); 3697 3698 _block = orig_block; 3699 _state = orig_state; 3700 _last = orig_last; 3701 } 3702 3703 3704 bool GraphBuilder::try_inline_full(ciMethod* callee, bool holder_known, Bytecodes::Code bc, Value receiver) { 3705 assert(!callee->is_native(), "callee must not be native"); 3706 if (CompilationPolicy::policy()->should_not_inline(compilation()->env(), callee)) { 3707 INLINE_BAILOUT("inlining prohibited by policy"); 3708 } 3709 // first perform tests of things it's not possible to inline 3710 if (callee->has_exception_handlers() && 3711 !InlineMethodsWithExceptionHandlers) INLINE_BAILOUT("callee has exception handlers"); 3712 if (callee->is_synchronized() && 3713 !InlineSynchronizedMethods ) INLINE_BAILOUT("callee is synchronized"); 3714 if (!callee->holder()->is_initialized()) INLINE_BAILOUT("callee's klass not initialized yet"); 3715 if (!callee->has_balanced_monitors()) INLINE_BAILOUT("callee's monitors do not match"); 3716 3717 // Proper inlining of methods with jsrs requires a little more work. 3718 if (callee->has_jsrs() ) INLINE_BAILOUT("jsrs not handled properly by inliner yet"); 3719 3720 // When SSE2 is used on intel, then no special handling is needed 3721 // for strictfp because the enum-constant is fixed at compile time, 3722 // the check for UseSSE2 is needed here 3723 if (strict_fp_requires_explicit_rounding && UseSSE < 2 && method()->is_strict() != callee->is_strict()) { 3724 INLINE_BAILOUT("caller and callee have different strict fp requirements"); 3725 } 3726 3727 if (is_profiling() && !callee->ensure_method_data()) { 3728 INLINE_BAILOUT("mdo allocation failed"); 3729 } 3730 3731 // now perform tests that are based on flag settings 3732 bool inlinee_by_directive = compilation()->directive()->should_inline(callee); 3733 if (callee->force_inline() || inlinee_by_directive) { 3734 if (inline_level() > MaxForceInlineLevel ) INLINE_BAILOUT("MaxForceInlineLevel"); 3735 if (recursive_inline_level(callee) > MaxRecursiveInlineLevel) INLINE_BAILOUT("recursive inlining too deep"); 3736 3737 const char* msg = ""; 3738 if (callee->force_inline()) msg = "force inline by annotation"; 3739 if (inlinee_by_directive) msg = "force inline by CompileCommand"; 3740 print_inlining(callee, msg); 3741 } else { 3742 // use heuristic controls on inlining 3743 if (inline_level() > MaxInlineLevel ) INLINE_BAILOUT("inlining too deep"); 3744 if (recursive_inline_level(callee) > MaxRecursiveInlineLevel) INLINE_BAILOUT("recursive inlining too deep"); 3745 if (callee->code_size_for_inlining() > max_inline_size() ) INLINE_BAILOUT("callee is too large"); 3746 3747 // don't inline throwable methods unless the inlining tree is rooted in a throwable class 3748 if (callee->name() == ciSymbol::object_initializer_name() && 3749 callee->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) { 3750 // Throwable constructor call 3751 IRScope* top = scope(); 3752 while (top->caller() != NULL) { 3753 top = top->caller(); 3754 } 3755 if (!top->method()->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) { 3756 INLINE_BAILOUT("don't inline Throwable constructors"); 3757 } 3758 } 3759 3760 if (compilation()->env()->num_inlined_bytecodes() > DesiredMethodLimit) { 3761 INLINE_BAILOUT("total inlining greater than DesiredMethodLimit"); 3762 } 3763 // printing 3764 print_inlining(callee); 3765 } 3766 3767 // NOTE: Bailouts from this point on, which occur at the 3768 // GraphBuilder level, do not cause bailout just of the inlining but 3769 // in fact of the entire compilation. 3770 3771 BlockBegin* orig_block = block(); 3772 3773 const bool is_invokedynamic = bc == Bytecodes::_invokedynamic; 3774 const bool has_receiver = (bc != Bytecodes::_invokestatic && !is_invokedynamic); 3775 3776 const int args_base = state()->stack_size() - callee->arg_size(); 3777 assert(args_base >= 0, "stack underflow during inlining"); 3778 3779 // Insert null check if necessary 3780 Value recv = NULL; 3781 if (has_receiver) { 3782 // note: null check must happen even if first instruction of callee does 3783 // an implicit null check since the callee is in a different scope 3784 // and we must make sure exception handling does the right thing 3785 assert(!callee->is_static(), "callee must not be static"); 3786 assert(callee->arg_size() > 0, "must have at least a receiver"); 3787 recv = state()->stack_at(args_base); 3788 null_check(recv); 3789 } 3790 3791 if (is_profiling()) { 3792 // Note that we'd collect profile data in this method if we wanted it. 3793 // this may be redundant here... 3794 compilation()->set_would_profile(true); 3795 3796 if (profile_calls()) { 3797 int start = 0; 3798 Values* obj_args = args_list_for_profiling(callee, start, has_receiver); 3799 if (obj_args != NULL) { 3800 int s = obj_args->max_length(); 3801 // if called through method handle invoke, some arguments may have been popped 3802 for (int i = args_base+start, j = 0; j < obj_args->max_length() && i < state()->stack_size(); ) { 3803 Value v = state()->stack_at_inc(i); 3804 if (v->type()->is_object_kind()) { 3805 obj_args->push(v); 3806 j++; 3807 } 3808 } 3809 check_args_for_profiling(obj_args, s); 3810 } 3811 profile_call(callee, recv, holder_known ? callee->holder() : NULL, obj_args, true); 3812 } 3813 } 3814 3815 // Introduce a new callee continuation point - if the callee has 3816 // more than one return instruction or the return does not allow 3817 // fall-through of control flow, all return instructions of the 3818 // callee will need to be replaced by Goto's pointing to this 3819 // continuation point. 3820 BlockBegin* cont = block_at(next_bci()); 3821 bool continuation_existed = true; 3822 if (cont == NULL) { 3823 cont = new BlockBegin(next_bci()); 3824 // low number so that continuation gets parsed as early as possible 3825 cont->set_depth_first_number(0); 3826 if (PrintInitialBlockList) { 3827 tty->print_cr("CFG: created block %d (bci %d) as continuation for inline at bci %d", 3828 cont->block_id(), cont->bci(), bci()); 3829 } 3830 continuation_existed = false; 3831 } 3832 // Record number of predecessors of continuation block before 3833 // inlining, to detect if inlined method has edges to its 3834 // continuation after inlining. 3835 int continuation_preds = cont->number_of_preds(); 3836 3837 // Push callee scope 3838 push_scope(callee, cont); 3839 3840 // the BlockListBuilder for the callee could have bailed out 3841 if (bailed_out()) 3842 return false; 3843 3844 // Temporarily set up bytecode stream so we can append instructions 3845 // (only using the bci of this stream) 3846 scope_data()->set_stream(scope_data()->parent()->stream()); 3847 3848 // Pass parameters into callee state: add assignments 3849 // note: this will also ensure that all arguments are computed before being passed 3850 ValueStack* callee_state = state(); 3851 ValueStack* caller_state = state()->caller_state(); 3852 for (int i = args_base; i < caller_state->stack_size(); ) { 3853 const int arg_no = i - args_base; 3854 Value arg = caller_state->stack_at_inc(i); 3855 store_local(callee_state, arg, arg_no); 3856 } 3857 3858 // Remove args from stack. 3859 // Note that we preserve locals state in case we can use it later 3860 // (see use of pop_scope() below) 3861 caller_state->truncate_stack(args_base); 3862 assert(callee_state->stack_size() == 0, "callee stack must be empty"); 3863 3864 Value lock = NULL; 3865 BlockBegin* sync_handler = NULL; 3866 3867 // Inline the locking of the receiver if the callee is synchronized 3868 if (callee->is_synchronized()) { 3869 lock = callee->is_static() ? append(new Constant(new InstanceConstant(callee->holder()->java_mirror()))) 3870 : state()->local_at(0); 3871 sync_handler = new BlockBegin(SynchronizationEntryBCI); 3872 inline_sync_entry(lock, sync_handler); 3873 } 3874 3875 if (compilation()->env()->dtrace_method_probes()) { 3876 Values* args = new Values(1); 3877 args->push(append(new Constant(new MethodConstant(method())))); 3878 append(new RuntimeCall(voidType, "dtrace_method_entry", CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), args)); 3879 } 3880 3881 if (profile_inlined_calls()) { 3882 profile_invocation(callee, copy_state_before_with_bci(SynchronizationEntryBCI)); 3883 } 3884 3885 BlockBegin* callee_start_block = block_at(0); 3886 if (callee_start_block != NULL) { 3887 assert(callee_start_block->is_set(BlockBegin::parser_loop_header_flag), "must be loop header"); 3888 Goto* goto_callee = new Goto(callee_start_block, false); 3889 // The state for this goto is in the scope of the callee, so use 3890 // the entry bci for the callee instead of the call site bci. 3891 append_with_bci(goto_callee, 0); 3892 _block->set_end(goto_callee); 3893 callee_start_block->merge(callee_state); 3894 3895 _last = _block = callee_start_block; 3896 3897 scope_data()->add_to_work_list(callee_start_block); 3898 } 3899 3900 // Clear out bytecode stream 3901 scope_data()->set_stream(NULL); 3902 3903 CompileLog* log = compilation()->log(); 3904 if (log != NULL) log->head("parse method='%d'", log->identify(callee)); 3905 3906 // Ready to resume parsing in callee (either in the same block we 3907 // were in before or in the callee's start block) 3908 iterate_all_blocks(callee_start_block == NULL); 3909 3910 if (log != NULL) log->done("parse"); 3911 3912 // If we bailed out during parsing, return immediately (this is bad news) 3913 if (bailed_out()) 3914 return false; 3915 3916 // iterate_all_blocks theoretically traverses in random order; in 3917 // practice, we have only traversed the continuation if we are 3918 // inlining into a subroutine 3919 assert(continuation_existed || 3920 !continuation()->is_set(BlockBegin::was_visited_flag), 3921 "continuation should not have been parsed yet if we created it"); 3922 3923 // At this point we are almost ready to return and resume parsing of 3924 // the caller back in the GraphBuilder. The only thing we want to do 3925 // first is an optimization: during parsing of the callee we 3926 // generated at least one Goto to the continuation block. If we 3927 // generated exactly one, and if the inlined method spanned exactly 3928 // one block (and we didn't have to Goto its entry), then we snip 3929 // off the Goto to the continuation, allowing control to fall 3930 // through back into the caller block and effectively performing 3931 // block merging. This allows load elimination and CSE to take place 3932 // across multiple callee scopes if they are relatively simple, and 3933 // is currently essential to making inlining profitable. 3934 if (num_returns() == 1 3935 && block() == orig_block 3936 && block() == inline_cleanup_block()) { 3937 _last = inline_cleanup_return_prev(); 3938 _state = inline_cleanup_state(); 3939 } else if (continuation_preds == cont->number_of_preds()) { 3940 // Inlining caused that the instructions after the invoke in the 3941 // caller are not reachable any more. So skip filling this block 3942 // with instructions! 3943 assert(cont == continuation(), ""); 3944 assert(_last && _last->as_BlockEnd(), ""); 3945 _skip_block = true; 3946 } else { 3947 // Resume parsing in continuation block unless it was already parsed. 3948 // Note that if we don't change _last here, iteration in 3949 // iterate_bytecodes_for_block will stop when we return. 3950 if (!continuation()->is_set(BlockBegin::was_visited_flag)) { 3951 // add continuation to work list instead of parsing it immediately 3952 assert(_last && _last->as_BlockEnd(), ""); 3953 scope_data()->parent()->add_to_work_list(continuation()); 3954 _skip_block = true; 3955 } 3956 } 3957 3958 // Fill the exception handler for synchronized methods with instructions 3959 if (callee->is_synchronized() && sync_handler->state() != NULL) { 3960 fill_sync_handler(lock, sync_handler); 3961 } else { 3962 pop_scope(); 3963 } 3964 3965 compilation()->notice_inlined_method(callee); 3966 3967 return true; 3968 } 3969 3970 3971 bool GraphBuilder::try_method_handle_inline(ciMethod* callee) { 3972 ValueStack* state_before = copy_state_before(); 3973 vmIntrinsics::ID iid = callee->intrinsic_id(); 3974 switch (iid) { 3975 case vmIntrinsics::_invokeBasic: 3976 { 3977 // get MethodHandle receiver 3978 const int args_base = state()->stack_size() - callee->arg_size(); 3979 ValueType* type = state()->stack_at(args_base)->type(); 3980 if (type->is_constant()) { 3981 ciMethod* target = type->as_ObjectType()->constant_value()->as_method_handle()->get_vmtarget(); 3982 // We don't do CHA here so only inline static and statically bindable methods. 3983 if (target->is_static() || target->can_be_statically_bound()) { 3984 Bytecodes::Code bc = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual; 3985 if (try_inline(target, /*holder_known*/ true, bc)) { 3986 return true; 3987 } 3988 } else { 3989 print_inlining(target, "not static or statically bindable", /*success*/ false); 3990 } 3991 } else { 3992 print_inlining(callee, "receiver not constant", /*success*/ false); 3993 } 3994 } 3995 break; 3996 3997 case vmIntrinsics::_linkToVirtual: 3998 case vmIntrinsics::_linkToStatic: 3999 case vmIntrinsics::_linkToSpecial: 4000 case vmIntrinsics::_linkToInterface: 4001 { 4002 // pop MemberName argument 4003 const int args_base = state()->stack_size() - callee->arg_size(); 4004 ValueType* type = apop()->type(); 4005 if (type->is_constant()) { 4006 ciMethod* target = type->as_ObjectType()->constant_value()->as_member_name()->get_vmtarget(); 4007 // If the target is another method handle invoke, try to recursively get 4008 // a better target. 4009 if (target->is_method_handle_intrinsic()) { 4010 if (try_method_handle_inline(target)) { 4011 return true; 4012 } 4013 } else { 4014 ciSignature* signature = target->signature(); 4015 const int receiver_skip = target->is_static() ? 0 : 1; 4016 // Cast receiver to its type. 4017 if (!target->is_static()) { 4018 ciKlass* tk = signature->accessing_klass(); 4019 Value obj = state()->stack_at(args_base); 4020 if (obj->exact_type() == NULL && 4021 obj->declared_type() != tk && tk != compilation()->env()->Object_klass()) { 4022 TypeCast* c = new TypeCast(tk, obj, state_before); 4023 append(c); 4024 state()->stack_at_put(args_base, c); 4025 } 4026 } 4027 // Cast reference arguments to its type. 4028 for (int i = 0, j = 0; i < signature->count(); i++) { 4029 ciType* t = signature->type_at(i); 4030 if (t->is_klass()) { 4031 ciKlass* tk = t->as_klass(); 4032 Value obj = state()->stack_at(args_base + receiver_skip + j); 4033 if (obj->exact_type() == NULL && 4034 obj->declared_type() != tk && tk != compilation()->env()->Object_klass()) { 4035 TypeCast* c = new TypeCast(t, obj, state_before); 4036 append(c); 4037 state()->stack_at_put(args_base + receiver_skip + j, c); 4038 } 4039 } 4040 j += t->size(); // long and double take two slots 4041 } 4042 // We don't do CHA here so only inline static and statically bindable methods. 4043 if (target->is_static() || target->can_be_statically_bound()) { 4044 Bytecodes::Code bc = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual; 4045 if (try_inline(target, /*holder_known*/ true, bc)) { 4046 return true; 4047 } 4048 } else { 4049 print_inlining(target, "not static or statically bindable", /*success*/ false); 4050 } 4051 } 4052 } else { 4053 print_inlining(callee, "MemberName not constant", /*success*/ false); 4054 } 4055 } 4056 break; 4057 4058 default: 4059 fatal("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid)); 4060 break; 4061 } 4062 set_state(state_before->copy_for_parsing()); 4063 return false; 4064 } 4065 4066 4067 void GraphBuilder::inline_bailout(const char* msg) { 4068 assert(msg != NULL, "inline bailout msg must exist"); 4069 _inline_bailout_msg = msg; 4070 } 4071 4072 4073 void GraphBuilder::clear_inline_bailout() { 4074 _inline_bailout_msg = NULL; 4075 } 4076 4077 4078 void GraphBuilder::push_root_scope(IRScope* scope, BlockList* bci2block, BlockBegin* start) { 4079 ScopeData* data = new ScopeData(NULL); 4080 data->set_scope(scope); 4081 data->set_bci2block(bci2block); 4082 _scope_data = data; 4083 _block = start; 4084 } 4085 4086 4087 void GraphBuilder::push_scope(ciMethod* callee, BlockBegin* continuation) { 4088 IRScope* callee_scope = new IRScope(compilation(), scope(), bci(), callee, -1, false); 4089 scope()->add_callee(callee_scope); 4090 4091 BlockListBuilder blb(compilation(), callee_scope, -1); 4092 CHECK_BAILOUT(); 4093 4094 if (!blb.bci2block()->at(0)->is_set(BlockBegin::parser_loop_header_flag)) { 4095 // this scope can be inlined directly into the caller so remove 4096 // the block at bci 0. 4097 blb.bci2block()->at_put(0, NULL); 4098 } 4099 4100 set_state(new ValueStack(callee_scope, state()->copy(ValueStack::CallerState, bci()))); 4101 4102 ScopeData* data = new ScopeData(scope_data()); 4103 data->set_scope(callee_scope); 4104 data->set_bci2block(blb.bci2block()); 4105 data->set_continuation(continuation); 4106 _scope_data = data; 4107 } 4108 4109 4110 void GraphBuilder::push_scope_for_jsr(BlockBegin* jsr_continuation, int jsr_dest_bci) { 4111 ScopeData* data = new ScopeData(scope_data()); 4112 data->set_parsing_jsr(); 4113 data->set_jsr_entry_bci(jsr_dest_bci); 4114 data->set_jsr_return_address_local(-1); 4115 // Must clone bci2block list as we will be mutating it in order to 4116 // properly clone all blocks in jsr region as well as exception 4117 // handlers containing rets 4118 BlockList* new_bci2block = new BlockList(bci2block()->length()); 4119 new_bci2block->appendAll(bci2block()); 4120 data->set_bci2block(new_bci2block); 4121 data->set_scope(scope()); 4122 data->setup_jsr_xhandlers(); 4123 data->set_continuation(continuation()); 4124 data->set_jsr_continuation(jsr_continuation); 4125 _scope_data = data; 4126 } 4127 4128 4129 void GraphBuilder::pop_scope() { 4130 int number_of_locks = scope()->number_of_locks(); 4131 _scope_data = scope_data()->parent(); 4132 // accumulate minimum number of monitor slots to be reserved 4133 scope()->set_min_number_of_locks(number_of_locks); 4134 } 4135 4136 4137 void GraphBuilder::pop_scope_for_jsr() { 4138 _scope_data = scope_data()->parent(); 4139 } 4140 4141 void GraphBuilder::append_unsafe_get_obj(ciMethod* callee, BasicType t, bool is_volatile) { 4142 Values* args = state()->pop_arguments(callee->arg_size()); 4143 null_check(args->at(0)); 4144 Instruction* offset = args->at(2); 4145 #ifndef _LP64 4146 offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT))); 4147 #endif 4148 Instruction* op = append(new UnsafeGetObject(t, args->at(1), offset, is_volatile)); 4149 push(op->type(), op); 4150 compilation()->set_has_unsafe_access(true); 4151 } 4152 4153 4154 void GraphBuilder::append_unsafe_put_obj(ciMethod* callee, BasicType t, bool is_volatile) { 4155 Values* args = state()->pop_arguments(callee->arg_size()); 4156 null_check(args->at(0)); 4157 Instruction* offset = args->at(2); 4158 #ifndef _LP64 4159 offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT))); 4160 #endif 4161 Value val = args->at(3); 4162 if (t == T_BOOLEAN) { 4163 Value mask = append(new Constant(new IntConstant(1))); 4164 val = append(new LogicOp(Bytecodes::_iand, val, mask)); 4165 } 4166 Instruction* op = append(new UnsafePutObject(t, args->at(1), offset, val, is_volatile)); 4167 compilation()->set_has_unsafe_access(true); 4168 kill_all(); 4169 } 4170 4171 4172 void GraphBuilder::append_unsafe_get_raw(ciMethod* callee, BasicType t) { 4173 Values* args = state()->pop_arguments(callee->arg_size()); 4174 null_check(args->at(0)); 4175 Instruction* op = append(new UnsafeGetRaw(t, args->at(1), false)); 4176 push(op->type(), op); 4177 compilation()->set_has_unsafe_access(true); 4178 } 4179 4180 4181 void GraphBuilder::append_unsafe_put_raw(ciMethod* callee, BasicType t) { 4182 Values* args = state()->pop_arguments(callee->arg_size()); 4183 null_check(args->at(0)); 4184 Instruction* op = append(new UnsafePutRaw(t, args->at(1), args->at(2))); 4185 compilation()->set_has_unsafe_access(true); 4186 } 4187 4188 4189 void GraphBuilder::append_unsafe_CAS(ciMethod* callee) { 4190 ValueStack* state_before = copy_state_for_exception(); 4191 ValueType* result_type = as_ValueType(callee->return_type()); 4192 assert(result_type->is_int(), "int result"); 4193 Values* args = state()->pop_arguments(callee->arg_size()); 4194 4195 // Pop off some args to specially handle, then push back 4196 Value newval = args->pop(); 4197 Value cmpval = args->pop(); 4198 Value offset = args->pop(); 4199 Value src = args->pop(); 4200 Value unsafe_obj = args->pop(); 4201 4202 // Separately handle the unsafe arg. It is not needed for code 4203 // generation, but must be null checked 4204 null_check(unsafe_obj); 4205 4206 #ifndef _LP64 4207 offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT))); 4208 #endif 4209 4210 args->push(src); 4211 args->push(offset); 4212 args->push(cmpval); 4213 args->push(newval); 4214 4215 // An unsafe CAS can alias with other field accesses, but we don't 4216 // know which ones so mark the state as no preserved. This will 4217 // cause CSE to invalidate memory across it. 4218 bool preserves_state = false; 4219 Intrinsic* result = new Intrinsic(result_type, callee->intrinsic_id(), args, false, state_before, preserves_state); 4220 append_split(result); 4221 push(result_type, result); 4222 compilation()->set_has_unsafe_access(true); 4223 } 4224 4225 void GraphBuilder::append_char_access(ciMethod* callee, bool is_store) { 4226 // This intrinsic accesses byte[] array as char[] array. Computing the offsets 4227 // correctly requires matched array shapes. 4228 assert (arrayOopDesc::base_offset_in_bytes(T_CHAR) == arrayOopDesc::base_offset_in_bytes(T_BYTE), 4229 "sanity: byte[] and char[] bases agree"); 4230 assert (type2aelembytes(T_CHAR) == type2aelembytes(T_BYTE)*2, 4231 "sanity: byte[] and char[] scales agree"); 4232 4233 ValueStack* state_before = copy_state_indexed_access(); 4234 compilation()->set_has_access_indexed(true); 4235 Values* args = state()->pop_arguments(callee->arg_size()); 4236 Value array = args->at(0); 4237 Value index = args->at(1); 4238 if (is_store) { 4239 Value value = args->at(2); 4240 Instruction* store = append(new StoreIndexed(array, index, NULL, T_CHAR, value, state_before, false)); 4241 store->set_flag(Instruction::NeedsRangeCheckFlag, false); 4242 _memory->store_value(value); 4243 } else { 4244 Instruction* load = append(new LoadIndexed(array, index, NULL, T_CHAR, state_before)); 4245 load->set_flag(Instruction::NeedsRangeCheckFlag, false); 4246 push(load->type(), load); 4247 } 4248 } 4249 4250 void GraphBuilder::print_inlining(ciMethod* callee, const char* msg, bool success) { 4251 CompileLog* log = compilation()->log(); 4252 if (log != NULL) { 4253 if (success) { 4254 if (msg != NULL) 4255 log->inline_success(msg); 4256 else 4257 log->inline_success("receiver is statically known"); 4258 } else { 4259 if (msg != NULL) 4260 log->inline_fail(msg); 4261 else 4262 log->inline_fail("reason unknown"); 4263 } 4264 } 4265 #if INCLUDE_TRACE 4266 EventCompilerInlining event; 4267 if (event.should_commit()) { 4268 event.set_compileID(compilation()->env()->task()->compile_id()); 4269 event.set_message(msg); 4270 event.set_succeeded(success); 4271 event.set_bci(bci()); 4272 event.set_caller(method()->get_Method()); 4273 event.set_callee(callee->to_trace_struct()); 4274 event.commit(); 4275 } 4276 #endif // INCLUDE_TRACE 4277 4278 if (!compilation()->directive()->PrintInliningOption) { 4279 return; 4280 } 4281 CompileTask::print_inlining_tty(callee, scope()->level(), bci(), msg); 4282 if (success && CIPrintMethodCodes) { 4283 callee->print_codes(); 4284 } 4285 } 4286 4287 void GraphBuilder::append_unsafe_get_and_set_obj(ciMethod* callee, bool is_add) { 4288 Values* args = state()->pop_arguments(callee->arg_size()); 4289 BasicType t = callee->return_type()->basic_type(); 4290 null_check(args->at(0)); 4291 Instruction* offset = args->at(2); 4292 #ifndef _LP64 4293 offset = append(new Convert(Bytecodes::_l2i, offset, as_ValueType(T_INT))); 4294 #endif 4295 Instruction* op = append(new UnsafeGetAndSetObject(t, args->at(1), offset, args->at(3), is_add)); 4296 compilation()->set_has_unsafe_access(true); 4297 kill_all(); 4298 push(op->type(), op); 4299 } 4300 4301 #ifndef PRODUCT 4302 void GraphBuilder::print_stats() { 4303 vmap()->print(); 4304 } 4305 #endif // PRODUCT 4306 4307 void GraphBuilder::profile_call(ciMethod* callee, Value recv, ciKlass* known_holder, Values* obj_args, bool inlined) { 4308 assert(known_holder == NULL || (known_holder->is_instance_klass() && 4309 (!known_holder->is_interface() || 4310 ((ciInstanceKlass*)known_holder)->has_default_methods())), "should be default method"); 4311 if (known_holder != NULL) { 4312 if (known_holder->exact_klass() == NULL) { 4313 known_holder = compilation()->cha_exact_type(known_holder); 4314 } 4315 } 4316 4317 append(new ProfileCall(method(), bci(), callee, recv, known_holder, obj_args, inlined)); 4318 } 4319 4320 void GraphBuilder::profile_return_type(Value ret, ciMethod* callee, ciMethod* m, int invoke_bci) { 4321 assert((m == NULL) == (invoke_bci < 0), "invalid method and invalid bci together"); 4322 if (m == NULL) { 4323 m = method(); 4324 } 4325 if (invoke_bci < 0) { 4326 invoke_bci = bci(); 4327 } 4328 ciMethodData* md = m->method_data_or_null(); 4329 ciProfileData* data = md->bci_to_data(invoke_bci); 4330 if (data->is_CallTypeData() || data->is_VirtualCallTypeData()) { 4331 append(new ProfileReturnType(m , invoke_bci, callee, ret)); 4332 } 4333 } 4334 4335 void GraphBuilder::profile_invocation(ciMethod* callee, ValueStack* state) { 4336 append(new ProfileInvoke(callee, state)); 4337 }