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